InstEmitTexture.cs 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772
  1. using Ryujinx.Graphics.Shader.Decoders;
  2. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  3. using Ryujinx.Graphics.Shader.Translation;
  4. using System;
  5. using System.Collections.Generic;
  6. using static Ryujinx.Graphics.Shader.Instructions.InstEmitHelper;
  7. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  8. namespace Ryujinx.Graphics.Shader.Instructions
  9. {
  10. static partial class InstEmit
  11. {
  12. private const bool Sample1DAs2D = true;
  13. public static void Suld(EmitterContext context)
  14. {
  15. context.Config.SetUsedFeature(FeatureFlags.IntegerSampling);
  16. OpCodeImage op = (OpCodeImage)context.CurrOp;
  17. SamplerType type = ConvertSamplerType(op.Dimensions);
  18. if (type == SamplerType.None)
  19. {
  20. context.Config.GpuAccessor.Log("Invalid image store sampler type.");
  21. return;
  22. }
  23. // Rb is Rd on the SULD instruction.
  24. int rdIndex = op.Rb.Index;
  25. int raIndex = op.Ra.Index;
  26. Operand Ra()
  27. {
  28. if (raIndex > RegisterConsts.RegisterZeroIndex)
  29. {
  30. return Const(0);
  31. }
  32. return context.Copy(Register(raIndex++, RegisterType.Gpr));
  33. }
  34. List<Operand> sourcesList = new List<Operand>();
  35. if (op.IsBindless)
  36. {
  37. sourcesList.Add(context.Copy(Register(op.Rc)));
  38. }
  39. int coordsCount = type.GetDimensions();
  40. for (int index = 0; index < coordsCount; index++)
  41. {
  42. sourcesList.Add(Ra());
  43. }
  44. if (Sample1DAs2D && (type & SamplerType.Mask) == SamplerType.Texture1D)
  45. {
  46. sourcesList.Add(Const(0));
  47. type &= ~SamplerType.Mask;
  48. type |= SamplerType.Texture2D;
  49. }
  50. if (type.HasFlag(SamplerType.Array))
  51. {
  52. sourcesList.Add(Ra());
  53. type |= SamplerType.Array;
  54. }
  55. Operand[] sources = sourcesList.ToArray();
  56. int handle = !op.IsBindless ? op.HandleOffset : 0;
  57. TextureFlags flags = op.IsBindless ? TextureFlags.Bindless : TextureFlags.None;
  58. if (op.UseComponents)
  59. {
  60. int componentMask = (int)op.Components;
  61. for (int compMask = componentMask, compIndex = 0; compMask != 0; compMask >>= 1, compIndex++)
  62. {
  63. if ((compMask & 1) == 0)
  64. {
  65. continue;
  66. }
  67. if (rdIndex == RegisterConsts.RegisterZeroIndex)
  68. {
  69. break;
  70. }
  71. Operand rd = Register(rdIndex++, RegisterType.Gpr);
  72. TextureOperation operation = context.CreateTextureOperation(
  73. Instruction.ImageLoad,
  74. type,
  75. flags,
  76. handle,
  77. compIndex,
  78. rd,
  79. sources);
  80. if (!op.IsBindless)
  81. {
  82. operation.Format = context.Config.GetTextureFormat(handle);
  83. }
  84. context.Add(operation);
  85. }
  86. }
  87. else
  88. {
  89. if (op.ByteAddress)
  90. {
  91. int xIndex = op.IsBindless ? 1 : 0;
  92. sources[xIndex] = context.ShiftRightS32(sources[xIndex], Const(GetComponentSizeInBytesLog2(op.Size)));
  93. }
  94. int components = GetComponents(op.Size);
  95. for (int compIndex = 0; compIndex < components; compIndex++)
  96. {
  97. if (rdIndex == RegisterConsts.RegisterZeroIndex)
  98. {
  99. break;
  100. }
  101. Operand rd = Register(rdIndex++, RegisterType.Gpr);
  102. TextureOperation operation = context.CreateTextureOperation(
  103. Instruction.ImageLoad,
  104. type,
  105. GetTextureFormat(op.Size),
  106. flags,
  107. handle,
  108. compIndex,
  109. rd,
  110. sources);
  111. context.Add(operation);
  112. switch (op.Size)
  113. {
  114. case IntegerSize.U8: context.Copy(rd, ZeroExtendTo32(context, rd, 8)); break;
  115. case IntegerSize.U16: context.Copy(rd, ZeroExtendTo32(context, rd, 16)); break;
  116. case IntegerSize.S8: context.Copy(rd, SignExtendTo32(context, rd, 8)); break;
  117. case IntegerSize.S16: context.Copy(rd, SignExtendTo32(context, rd, 16)); break;
  118. }
  119. }
  120. }
  121. }
  122. public static void Sust(EmitterContext context)
  123. {
  124. OpCodeImage op = (OpCodeImage)context.CurrOp;
  125. SamplerType type = ConvertSamplerType(op.Dimensions);
  126. if (type == SamplerType.None)
  127. {
  128. context.Config.GpuAccessor.Log("Invalid image store sampler type.");
  129. return;
  130. }
  131. int raIndex = op.Ra.Index;
  132. int rbIndex = op.Rb.Index;
  133. Operand Ra()
  134. {
  135. if (raIndex > RegisterConsts.RegisterZeroIndex)
  136. {
  137. return Const(0);
  138. }
  139. return context.Copy(Register(raIndex++, RegisterType.Gpr));
  140. }
  141. Operand Rb()
  142. {
  143. if (rbIndex > RegisterConsts.RegisterZeroIndex)
  144. {
  145. return Const(0);
  146. }
  147. return context.Copy(Register(rbIndex++, RegisterType.Gpr));
  148. }
  149. List<Operand> sourcesList = new List<Operand>();
  150. if (op.IsBindless)
  151. {
  152. sourcesList.Add(context.Copy(Register(op.Rc)));
  153. }
  154. int coordsCount = type.GetDimensions();
  155. for (int index = 0; index < coordsCount; index++)
  156. {
  157. sourcesList.Add(Ra());
  158. }
  159. if (Sample1DAs2D && (type & SamplerType.Mask) == SamplerType.Texture1D)
  160. {
  161. sourcesList.Add(Const(0));
  162. type &= ~SamplerType.Mask;
  163. type |= SamplerType.Texture2D;
  164. }
  165. if (type.HasFlag(SamplerType.Array))
  166. {
  167. sourcesList.Add(Ra());
  168. type |= SamplerType.Array;
  169. }
  170. TextureFormat format = TextureFormat.Unknown;
  171. if (op.UseComponents)
  172. {
  173. int componentMask = (int)op.Components;
  174. for (int compMask = componentMask, compIndex = 0; compMask != 0; compMask >>= 1, compIndex++)
  175. {
  176. if ((compMask & 1) != 0)
  177. {
  178. sourcesList.Add(Rb());
  179. }
  180. }
  181. if (!op.IsBindless)
  182. {
  183. format = context.Config.GetTextureFormat(op.HandleOffset);
  184. }
  185. }
  186. else
  187. {
  188. if (op.ByteAddress)
  189. {
  190. int xIndex = op.IsBindless ? 1 : 0;
  191. sourcesList[xIndex] = context.ShiftRightS32(sourcesList[xIndex], Const(GetComponentSizeInBytesLog2(op.Size)));
  192. }
  193. int components = GetComponents(op.Size);
  194. for (int compIndex = 0; compIndex < components; compIndex++)
  195. {
  196. sourcesList.Add(Rb());
  197. }
  198. format = GetTextureFormat(op.Size);
  199. }
  200. Operand[] sources = sourcesList.ToArray();
  201. int handle = !op.IsBindless ? op.HandleOffset : 0;
  202. TextureFlags flags = op.IsBindless ? TextureFlags.Bindless : TextureFlags.None;
  203. TextureOperation operation = context.CreateTextureOperation(
  204. Instruction.ImageStore,
  205. type,
  206. format,
  207. flags,
  208. handle,
  209. 0,
  210. null,
  211. sources);
  212. context.Add(operation);
  213. }
  214. public static void Sured(EmitterContext context)
  215. {
  216. OpCodeSured op = (OpCodeSured)context.CurrOp;
  217. SamplerType type = ConvertSamplerType(op.Dimensions);
  218. if (type == SamplerType.None)
  219. {
  220. context.Config.GpuAccessor.Log("Invalid image reduction sampler type.");
  221. return;
  222. }
  223. int raIndex = op.Ra.Index;
  224. int rbIndex = op.Rb.Index;
  225. Operand Ra()
  226. {
  227. if (raIndex > RegisterConsts.RegisterZeroIndex)
  228. {
  229. return Const(0);
  230. }
  231. return context.Copy(Register(raIndex++, RegisterType.Gpr));
  232. }
  233. Operand Rb()
  234. {
  235. if (rbIndex > RegisterConsts.RegisterZeroIndex)
  236. {
  237. return Const(0);
  238. }
  239. return context.Copy(Register(rbIndex++, RegisterType.Gpr));
  240. }
  241. List<Operand> sourcesList = new List<Operand>();
  242. if (op.IsBindless)
  243. {
  244. sourcesList.Add(context.Copy(Register(op.Rc)));
  245. }
  246. int coordsCount = type.GetDimensions();
  247. for (int index = 0; index < coordsCount; index++)
  248. {
  249. sourcesList.Add(Ra());
  250. }
  251. if (Sample1DAs2D && (type & SamplerType.Mask) == SamplerType.Texture1D)
  252. {
  253. sourcesList.Add(Const(0));
  254. type &= ~SamplerType.Mask;
  255. type |= SamplerType.Texture2D;
  256. }
  257. if (type.HasFlag(SamplerType.Array))
  258. {
  259. sourcesList.Add(Ra());
  260. type |= SamplerType.Array;
  261. }
  262. TextureFormat format = TextureFormat.R32Sint;
  263. if (op.UseType)
  264. {
  265. if (op.ByteAddress)
  266. {
  267. int xIndex = op.IsBindless ? 1 : 0;
  268. sourcesList[xIndex] = context.ShiftRightS32(sourcesList[xIndex], Const(GetComponentSizeInBytesLog2(op.Type)));
  269. }
  270. // TODO: FP and 64-bit formats.
  271. format = (op.Type == ReductionType.SD32 || op.Type == ReductionType.SD64) ?
  272. context.Config.GetTextureFormatAtomic(op.HandleOffset) :
  273. GetTextureFormat(op.Type);
  274. }
  275. else if (!op.IsBindless)
  276. {
  277. format = context.Config.GetTextureFormatAtomic(op.HandleOffset);
  278. }
  279. sourcesList.Add(Rb());
  280. Operand[] sources = sourcesList.ToArray();
  281. int handle = op.HandleOffset;
  282. TextureFlags flags = GetAtomicOpFlags(op.AtomicOp);
  283. if (op.IsBindless)
  284. {
  285. handle = 0;
  286. flags |= TextureFlags.Bindless;
  287. }
  288. TextureOperation operation = context.CreateTextureOperation(
  289. Instruction.ImageAtomic,
  290. type,
  291. format,
  292. flags,
  293. handle,
  294. 0,
  295. null,
  296. sources);
  297. context.Add(operation);
  298. }
  299. public static void Suatom(EmitterContext context)
  300. {
  301. OpCodeSuatom op = (OpCodeSuatom)context.CurrOp;
  302. SamplerType type = ConvertSamplerType(op.Dimensions);
  303. if (type == SamplerType.None)
  304. {
  305. context.Config.GpuAccessor.Log("Invalid image atomic sampler type.");
  306. return;
  307. }
  308. int raIndex = op.Ra.Index;
  309. int rbIndex = op.Rb.Index;
  310. Operand Ra()
  311. {
  312. if (raIndex > RegisterConsts.RegisterZeroIndex)
  313. {
  314. return Const(0);
  315. }
  316. return context.Copy(Register(raIndex++, RegisterType.Gpr));
  317. }
  318. Operand Rb()
  319. {
  320. if (rbIndex > RegisterConsts.RegisterZeroIndex)
  321. {
  322. return Const(0);
  323. }
  324. return context.Copy(Register(rbIndex++, RegisterType.Gpr));
  325. }
  326. int rdIndex = op.Rd.Index;
  327. Operand GetDest()
  328. {
  329. if (rdIndex > RegisterConsts.RegisterZeroIndex)
  330. {
  331. return Const(0);
  332. }
  333. return Register(rdIndex++, RegisterType.Gpr);
  334. }
  335. List<Operand> sourcesList = new List<Operand>();
  336. if (op.IsBindless)
  337. {
  338. sourcesList.Add(context.Copy(Register(op.Rc)));
  339. }
  340. int coordsCount = type.GetDimensions();
  341. for (int index = 0; index < coordsCount; index++)
  342. {
  343. sourcesList.Add(Ra());
  344. }
  345. if (Sample1DAs2D && (type & SamplerType.Mask) == SamplerType.Texture1D)
  346. {
  347. sourcesList.Add(Const(0));
  348. type &= ~SamplerType.Mask;
  349. type |= SamplerType.Texture2D;
  350. }
  351. if (type.HasFlag(SamplerType.Array))
  352. {
  353. sourcesList.Add(Ra());
  354. type |= SamplerType.Array;
  355. }
  356. TextureFormat format = TextureFormat.R32Sint;
  357. if (op.UseType)
  358. {
  359. if (op.ByteAddress)
  360. {
  361. int xIndex = op.IsBindless ? 1 : 0;
  362. sourcesList[xIndex] = context.ShiftRightS32(sourcesList[xIndex], Const(GetComponentSizeInBytesLog2(op.Type)));
  363. }
  364. // TODO: FP and 64-bit formats.
  365. format = (op.Type == ReductionType.SD32 || op.Type == ReductionType.SD64) ?
  366. context.Config.GetTextureFormatAtomic(op.HandleOffset) :
  367. GetTextureFormat(op.Type);
  368. }
  369. else if (!op.IsBindless)
  370. {
  371. format = context.Config.GetTextureFormatAtomic(op.HandleOffset);
  372. }
  373. if (op.CompareAndSwap)
  374. {
  375. sourcesList.Add(Rb());
  376. }
  377. sourcesList.Add(Rb());
  378. Operand[] sources = sourcesList.ToArray();
  379. int handle = op.HandleOffset;
  380. TextureFlags flags = op.CompareAndSwap ? TextureFlags.CAS : GetAtomicOpFlags(op.AtomicOp);
  381. if (op.IsBindless)
  382. {
  383. handle = 0;
  384. flags |= TextureFlags.Bindless;
  385. }
  386. TextureOperation operation = context.CreateTextureOperation(
  387. Instruction.ImageAtomic,
  388. type,
  389. format,
  390. flags,
  391. handle,
  392. 0,
  393. GetDest(),
  394. sources);
  395. context.Add(operation);
  396. }
  397. public static void Tex(EmitterContext context)
  398. {
  399. EmitTextureSample(context, TextureFlags.None);
  400. }
  401. public static void TexB(EmitterContext context)
  402. {
  403. EmitTextureSample(context, TextureFlags.Bindless);
  404. }
  405. public static void Tld(EmitterContext context)
  406. {
  407. context.Config.SetUsedFeature(FeatureFlags.IntegerSampling);
  408. EmitTextureSample(context, TextureFlags.IntCoords);
  409. }
  410. public static void TldB(EmitterContext context)
  411. {
  412. context.Config.SetUsedFeature(FeatureFlags.IntegerSampling);
  413. EmitTextureSample(context, TextureFlags.IntCoords | TextureFlags.Bindless);
  414. }
  415. public static void Texs(EmitterContext context)
  416. {
  417. OpCodeTextureScalar op = (OpCodeTextureScalar)context.CurrOp;
  418. if (op.Rd0.IsRZ && op.Rd1.IsRZ)
  419. {
  420. return;
  421. }
  422. List<Operand> sourcesList = new List<Operand>();
  423. int raIndex = op.Ra.Index;
  424. int rbIndex = op.Rb.Index;
  425. Operand Ra()
  426. {
  427. if (raIndex > RegisterConsts.RegisterZeroIndex)
  428. {
  429. return Const(0);
  430. }
  431. return context.Copy(Register(raIndex++, RegisterType.Gpr));
  432. }
  433. Operand Rb()
  434. {
  435. if (rbIndex > RegisterConsts.RegisterZeroIndex)
  436. {
  437. return Const(0);
  438. }
  439. return context.Copy(Register(rbIndex++, RegisterType.Gpr));
  440. }
  441. void AddTextureOffset(int coordsCount, int stride, int size)
  442. {
  443. Operand packedOffs = Rb();
  444. for (int index = 0; index < coordsCount; index++)
  445. {
  446. sourcesList.Add(context.BitfieldExtractS32(packedOffs, Const(index * stride), Const(size)));
  447. }
  448. }
  449. SamplerType type;
  450. TextureFlags flags;
  451. if (op is OpCodeTexs texsOp)
  452. {
  453. type = ConvertSamplerType(texsOp.Target);
  454. if (type == SamplerType.None)
  455. {
  456. context.Config.GpuAccessor.Log("Invalid texture sampler type.");
  457. return;
  458. }
  459. flags = ConvertTextureFlags(texsOp.Target);
  460. // We don't need to handle 1D -> Buffer conversions here as
  461. // only texture sample with integer coordinates can ever use buffer targets.
  462. if ((type & SamplerType.Array) != 0)
  463. {
  464. Operand arrayIndex = Ra();
  465. sourcesList.Add(Ra());
  466. sourcesList.Add(Rb());
  467. sourcesList.Add(arrayIndex);
  468. if ((type & SamplerType.Shadow) != 0)
  469. {
  470. sourcesList.Add(Rb());
  471. }
  472. if ((flags & TextureFlags.LodLevel) != 0)
  473. {
  474. sourcesList.Add(ConstF(0));
  475. }
  476. }
  477. else
  478. {
  479. switch (texsOp.Target)
  480. {
  481. case TextureTarget.Texture1DLodZero:
  482. sourcesList.Add(Ra());
  483. if (Sample1DAs2D)
  484. {
  485. sourcesList.Add(ConstF(0));
  486. type &= ~SamplerType.Mask;
  487. type |= SamplerType.Texture2D;
  488. }
  489. sourcesList.Add(ConstF(0));
  490. break;
  491. case TextureTarget.Texture2D:
  492. sourcesList.Add(Ra());
  493. sourcesList.Add(Rb());
  494. break;
  495. case TextureTarget.Texture2DLodZero:
  496. sourcesList.Add(Ra());
  497. sourcesList.Add(Rb());
  498. sourcesList.Add(ConstF(0));
  499. break;
  500. case TextureTarget.Texture2DLodLevel:
  501. case TextureTarget.Texture2DDepthCompare:
  502. case TextureTarget.Texture3D:
  503. case TextureTarget.TextureCube:
  504. sourcesList.Add(Ra());
  505. sourcesList.Add(Ra());
  506. sourcesList.Add(Rb());
  507. break;
  508. case TextureTarget.Texture2DLodZeroDepthCompare:
  509. case TextureTarget.Texture3DLodZero:
  510. sourcesList.Add(Ra());
  511. sourcesList.Add(Ra());
  512. sourcesList.Add(Rb());
  513. sourcesList.Add(ConstF(0));
  514. break;
  515. case TextureTarget.Texture2DLodLevelDepthCompare:
  516. case TextureTarget.TextureCubeLodLevel:
  517. sourcesList.Add(Ra());
  518. sourcesList.Add(Ra());
  519. sourcesList.Add(Rb());
  520. sourcesList.Add(Rb());
  521. break;
  522. }
  523. }
  524. }
  525. else if (op is OpCodeTlds tldsOp)
  526. {
  527. type = ConvertSamplerType(tldsOp.Target);
  528. if (type == SamplerType.None)
  529. {
  530. context.Config.GpuAccessor.Log("Invalid texel fetch sampler type.");
  531. return;
  532. }
  533. context.Config.SetUsedFeature(FeatureFlags.IntegerSampling);
  534. flags = ConvertTextureFlags(tldsOp.Target) | TextureFlags.IntCoords;
  535. if (tldsOp.Target == TexelLoadTarget.Texture1DLodZero && context.Config.GpuAccessor.QuerySamplerType(tldsOp.HandleOffset) == SamplerType.TextureBuffer)
  536. {
  537. type = SamplerType.TextureBuffer;
  538. flags &= ~TextureFlags.LodLevel;
  539. }
  540. switch (tldsOp.Target)
  541. {
  542. case TexelLoadTarget.Texture1DLodZero:
  543. sourcesList.Add(Ra());
  544. if (type != SamplerType.TextureBuffer)
  545. {
  546. if (Sample1DAs2D)
  547. {
  548. sourcesList.Add(ConstF(0));
  549. type &= ~SamplerType.Mask;
  550. type |= SamplerType.Texture2D;
  551. }
  552. sourcesList.Add(ConstF(0));
  553. }
  554. break;
  555. case TexelLoadTarget.Texture1DLodLevel:
  556. sourcesList.Add(Ra());
  557. if (Sample1DAs2D)
  558. {
  559. sourcesList.Add(ConstF(0));
  560. type &= ~SamplerType.Mask;
  561. type |= SamplerType.Texture2D;
  562. }
  563. sourcesList.Add(Rb());
  564. break;
  565. case TexelLoadTarget.Texture2DLodZero:
  566. sourcesList.Add(Ra());
  567. sourcesList.Add(Rb());
  568. sourcesList.Add(Const(0));
  569. break;
  570. case TexelLoadTarget.Texture2DLodZeroOffset:
  571. sourcesList.Add(Ra());
  572. sourcesList.Add(Ra());
  573. sourcesList.Add(Const(0));
  574. break;
  575. case TexelLoadTarget.Texture2DLodZeroMultisample:
  576. case TexelLoadTarget.Texture2DLodLevel:
  577. case TexelLoadTarget.Texture2DLodLevelOffset:
  578. sourcesList.Add(Ra());
  579. sourcesList.Add(Ra());
  580. sourcesList.Add(Rb());
  581. break;
  582. case TexelLoadTarget.Texture3DLodZero:
  583. sourcesList.Add(Ra());
  584. sourcesList.Add(Ra());
  585. sourcesList.Add(Rb());
  586. sourcesList.Add(Const(0));
  587. break;
  588. case TexelLoadTarget.Texture2DArrayLodZero:
  589. sourcesList.Add(Rb());
  590. sourcesList.Add(Rb());
  591. sourcesList.Add(Ra());
  592. sourcesList.Add(Const(0));
  593. break;
  594. }
  595. if ((flags & TextureFlags.Offset) != 0)
  596. {
  597. AddTextureOffset(type.GetDimensions(), 4, 4);
  598. }
  599. }
  600. else if (op is OpCodeTld4s tld4sOp)
  601. {
  602. if (!(tld4sOp.HasDepthCompare || tld4sOp.HasOffset))
  603. {
  604. sourcesList.Add(Ra());
  605. sourcesList.Add(Rb());
  606. }
  607. else
  608. {
  609. sourcesList.Add(Ra());
  610. sourcesList.Add(Ra());
  611. }
  612. type = SamplerType.Texture2D;
  613. flags = TextureFlags.Gather;
  614. if (tld4sOp.HasDepthCompare)
  615. {
  616. sourcesList.Add(Rb());
  617. type |= SamplerType.Shadow;
  618. }
  619. if (tld4sOp.HasOffset)
  620. {
  621. AddTextureOffset(type.GetDimensions(), 8, 6);
  622. flags |= TextureFlags.Offset;
  623. }
  624. sourcesList.Add(Const(tld4sOp.GatherCompIndex));
  625. }
  626. else
  627. {
  628. throw new InvalidOperationException($"Invalid opcode type \"{op.GetType().Name}\".");
  629. }
  630. Operand[] sources = sourcesList.ToArray();
  631. Operand[] rd0 = new Operand[2] { ConstF(0), ConstF(0) };
  632. Operand[] rd1 = new Operand[2] { ConstF(0), ConstF(0) };
  633. int destIncrement = 0;
  634. Operand GetDest()
  635. {
  636. int high = destIncrement >> 1;
  637. int low = destIncrement & 1;
  638. destIncrement++;
  639. if (op.IsFp16)
  640. {
  641. return high != 0
  642. ? (rd1[low] = Local())
  643. : (rd0[low] = Local());
  644. }
  645. else
  646. {
  647. int rdIndex = high != 0 ? op.Rd1.Index : op.Rd0.Index;
  648. if (rdIndex < RegisterConsts.RegisterZeroIndex)
  649. {
  650. rdIndex += low;
  651. }
  652. return Register(rdIndex, RegisterType.Gpr);
  653. }
  654. }
  655. int handle = op.HandleOffset;
  656. for (int compMask = op.ComponentMask, compIndex = 0; compMask != 0; compMask >>= 1, compIndex++)
  657. {
  658. if ((compMask & 1) != 0)
  659. {
  660. Operand dest = GetDest();
  661. TextureOperation operation = context.CreateTextureOperation(
  662. Instruction.TextureSample,
  663. type,
  664. flags,
  665. handle,
  666. compIndex,
  667. dest,
  668. sources);
  669. context.Add(operation);
  670. }
  671. }
  672. if (op.IsFp16)
  673. {
  674. context.Copy(Register(op.Rd0), context.PackHalf2x16(rd0[0], rd0[1]));
  675. context.Copy(Register(op.Rd1), context.PackHalf2x16(rd1[0], rd1[1]));
  676. }
  677. }
  678. public static void Tld4(EmitterContext context)
  679. {
  680. IOpCodeTld4 op = (IOpCodeTld4)context.CurrOp;
  681. if (op.Rd.IsRZ)
  682. {
  683. return;
  684. }
  685. int raIndex = op.Ra.Index;
  686. int rbIndex = op.Rb.Index;
  687. Operand Ra()
  688. {
  689. if (raIndex > RegisterConsts.RegisterZeroIndex)
  690. {
  691. return Const(0);
  692. }
  693. return context.Copy(Register(raIndex++, RegisterType.Gpr));
  694. }
  695. Operand Rb()
  696. {
  697. if (rbIndex > RegisterConsts.RegisterZeroIndex)
  698. {
  699. return Const(0);
  700. }
  701. return context.Copy(Register(rbIndex++, RegisterType.Gpr));
  702. }
  703. Operand arrayIndex = op.IsArray ? Ra() : null;
  704. List<Operand> sourcesList = new List<Operand>();
  705. SamplerType type = ConvertSamplerType(op.Dimensions);
  706. TextureFlags flags = TextureFlags.Gather;
  707. if (op.Bindless)
  708. {
  709. sourcesList.Add(Rb());
  710. flags |= TextureFlags.Bindless;
  711. }
  712. int coordsCount = type.GetDimensions();
  713. for (int index = 0; index < coordsCount; index++)
  714. {
  715. sourcesList.Add(Ra());
  716. }
  717. bool is1DTo2D = Sample1DAs2D && type == SamplerType.Texture1D;
  718. if (is1DTo2D)
  719. {
  720. sourcesList.Add(ConstF(0));
  721. type = SamplerType.Texture2D;
  722. }
  723. if (op.IsArray)
  724. {
  725. sourcesList.Add(arrayIndex);
  726. type |= SamplerType.Array;
  727. }
  728. Operand[] packedOffs = new Operand[2];
  729. packedOffs[0] = op.Offset != TextureGatherOffset.None ? Rb() : null;
  730. packedOffs[1] = op.Offset == TextureGatherOffset.Offsets ? Rb() : null;
  731. if (op.HasDepthCompare)
  732. {
  733. sourcesList.Add(Rb());
  734. type |= SamplerType.Shadow;
  735. }
  736. if (op.Offset != TextureGatherOffset.None)
  737. {
  738. int offsetTexelsCount = op.Offset == TextureGatherOffset.Offsets ? 4 : 1;
  739. for (int index = 0; index < coordsCount * offsetTexelsCount; index++)
  740. {
  741. Operand packed = packedOffs[(index >> 2) & 1];
  742. sourcesList.Add(context.BitfieldExtractS32(packed, Const((index & 3) * 8), Const(6)));
  743. }
  744. if (is1DTo2D)
  745. {
  746. for (int index = 0; index < offsetTexelsCount; index++)
  747. {
  748. sourcesList.Add(Const(0));
  749. }
  750. }
  751. flags |= op.Offset == TextureGatherOffset.Offsets
  752. ? TextureFlags.Offsets
  753. : TextureFlags.Offset;
  754. }
  755. sourcesList.Add(Const(op.GatherCompIndex));
  756. Operand[] sources = sourcesList.ToArray();
  757. int rdIndex = op.Rd.Index;
  758. Operand GetDest()
  759. {
  760. if (rdIndex > RegisterConsts.RegisterZeroIndex)
  761. {
  762. return Const(0);
  763. }
  764. return Register(rdIndex++, RegisterType.Gpr);
  765. }
  766. int handle = op.HandleOffset;
  767. for (int compMask = op.ComponentMask, compIndex = 0; compMask != 0; compMask >>= 1, compIndex++)
  768. {
  769. if ((compMask & 1) != 0)
  770. {
  771. Operand dest = GetDest();
  772. TextureOperation operation = context.CreateTextureOperation(
  773. Instruction.TextureSample,
  774. type,
  775. flags,
  776. handle,
  777. compIndex,
  778. dest,
  779. sources);
  780. context.Add(operation);
  781. }
  782. }
  783. }
  784. public static void TmmlB(EmitterContext context)
  785. {
  786. EmitTextureMipMapLevel(context, true);
  787. }
  788. public static void Tmml(EmitterContext context)
  789. {
  790. EmitTextureMipMapLevel(context, false);
  791. }
  792. private static void EmitTextureMipMapLevel(EmitterContext context, bool isBindless)
  793. {
  794. OpCodeTexture op = (OpCodeTexture)context.CurrOp;
  795. if (op.Rd.IsRZ)
  796. {
  797. return;
  798. }
  799. int raIndex = op.Ra.Index;
  800. int rbIndex = op.Rb.Index;
  801. Operand Ra()
  802. {
  803. if (raIndex > RegisterConsts.RegisterZeroIndex)
  804. {
  805. return Const(0);
  806. }
  807. return context.Copy(Register(raIndex++, RegisterType.Gpr));
  808. }
  809. Operand Rb()
  810. {
  811. if (rbIndex > RegisterConsts.RegisterZeroIndex)
  812. {
  813. return Const(0);
  814. }
  815. return context.Copy(Register(rbIndex++, RegisterType.Gpr));
  816. }
  817. TextureFlags flags = TextureFlags.None;
  818. List<Operand> sourcesList = new List<Operand>();
  819. if (isBindless)
  820. {
  821. sourcesList.Add(Rb());
  822. flags |= TextureFlags.Bindless;
  823. }
  824. SamplerType type = ConvertSamplerType(op.Dimensions);
  825. int coordsCount = type.GetDimensions();
  826. Operand arrayIndex = op.IsArray ? Ra() : null;
  827. for (int index = 0; index < coordsCount; index++)
  828. {
  829. sourcesList.Add(Ra());
  830. }
  831. if (Sample1DAs2D && type == SamplerType.Texture1D)
  832. {
  833. sourcesList.Add(ConstF(0));
  834. type = SamplerType.Texture2D;
  835. }
  836. if (op.IsArray)
  837. {
  838. sourcesList.Add(arrayIndex);
  839. type |= SamplerType.Array;
  840. }
  841. Operand[] sources = sourcesList.ToArray();
  842. int rdIndex = op.Rd.Index;
  843. Operand GetDest()
  844. {
  845. if (rdIndex > RegisterConsts.RegisterZeroIndex)
  846. {
  847. return Const(0);
  848. }
  849. return Register(rdIndex++, RegisterType.Gpr);
  850. }
  851. int handle = !isBindless ? op.HandleOffset : 0;
  852. for (int compMask = op.ComponentMask, compIndex = 0; compMask != 0; compMask >>= 1, compIndex++)
  853. {
  854. if ((compMask & 1) != 0)
  855. {
  856. Operand dest = GetDest();
  857. // Components z and w aren't standard, we return 0 in this case and add a comment.
  858. if (compIndex >= 2)
  859. {
  860. context.Add(new CommentNode("Unsupported component z or w found"));
  861. context.Copy(dest, Const(0));
  862. }
  863. else
  864. {
  865. Operand tempDest = Local();
  866. TextureOperation operation = context.CreateTextureOperation(
  867. Instruction.Lod,
  868. type,
  869. flags,
  870. handle,
  871. compIndex ^ 1, // The instruction component order is the inverse of GLSL's.
  872. tempDest,
  873. sources);
  874. context.Add(operation);
  875. tempDest = context.FPMultiply(tempDest, ConstF(256.0f));
  876. Operand fixedPointValue = context.FPConvertToS32(tempDest);
  877. context.Copy(dest, fixedPointValue);
  878. }
  879. }
  880. }
  881. }
  882. public static void Txd(EmitterContext context)
  883. {
  884. OpCodeTxd op = (OpCodeTxd)context.CurrOp;
  885. if (op.Rd.IsRZ)
  886. {
  887. return;
  888. }
  889. int raIndex = op.Ra.Index;
  890. int rbIndex = op.Rb.Index;
  891. Operand Ra()
  892. {
  893. if (raIndex > RegisterConsts.RegisterZeroIndex)
  894. {
  895. return Const(0);
  896. }
  897. return context.Copy(Register(raIndex++, RegisterType.Gpr));
  898. }
  899. Operand Rb()
  900. {
  901. if (rbIndex > RegisterConsts.RegisterZeroIndex)
  902. {
  903. return Const(0);
  904. }
  905. return context.Copy(Register(rbIndex++, RegisterType.Gpr));
  906. }
  907. TextureFlags flags = TextureFlags.Derivatives;
  908. List<Operand> sourcesList = new List<Operand>();
  909. if (op.IsBindless)
  910. {
  911. sourcesList.Add(Ra());
  912. flags |= TextureFlags.Bindless;
  913. }
  914. SamplerType type = ConvertSamplerType(op.Dimensions);
  915. int coordsCount = type.GetDimensions();
  916. for (int index = 0; index < coordsCount; index++)
  917. {
  918. sourcesList.Add(Ra());
  919. }
  920. bool is1DTo2D = Sample1DAs2D && type == SamplerType.Texture1D;
  921. if (is1DTo2D)
  922. {
  923. sourcesList.Add(ConstF(0));
  924. type = SamplerType.Texture2D;
  925. }
  926. Operand packedParams = Ra();
  927. if (op.IsArray)
  928. {
  929. sourcesList.Add(context.BitwiseAnd(packedParams, Const(0xffff)));
  930. type |= SamplerType.Array;
  931. }
  932. // Derivatives (X and Y).
  933. for (int dIndex = 0; dIndex < 2 * coordsCount; dIndex++)
  934. {
  935. sourcesList.Add(Rb());
  936. if (is1DTo2D)
  937. {
  938. sourcesList.Add(ConstF(0));
  939. }
  940. }
  941. if (op.HasOffset)
  942. {
  943. for (int index = 0; index < coordsCount; index++)
  944. {
  945. sourcesList.Add(context.BitfieldExtractS32(packedParams, Const(16 + index * 4), Const(4)));
  946. }
  947. if (is1DTo2D)
  948. {
  949. sourcesList.Add(Const(0));
  950. }
  951. flags |= TextureFlags.Offset;
  952. }
  953. Operand[] sources = sourcesList.ToArray();
  954. int rdIndex = op.Rd.Index;
  955. Operand GetDest()
  956. {
  957. if (rdIndex > RegisterConsts.RegisterZeroIndex)
  958. {
  959. return Const(0);
  960. }
  961. return Register(rdIndex++, RegisterType.Gpr);
  962. }
  963. int handle = !op.IsBindless ? op.HandleOffset : 0;
  964. for (int compMask = op.ComponentMask, compIndex = 0; compMask != 0; compMask >>= 1, compIndex++)
  965. {
  966. if ((compMask & 1) != 0)
  967. {
  968. Operand dest = GetDest();
  969. TextureOperation operation = context.CreateTextureOperation(
  970. Instruction.TextureSample,
  971. type,
  972. flags,
  973. handle,
  974. compIndex,
  975. dest,
  976. sources);
  977. context.Add(operation);
  978. }
  979. }
  980. }
  981. public static void Txq(EmitterContext context)
  982. {
  983. EmitTextureQuery(context, bindless: false);
  984. }
  985. public static void TxqB(EmitterContext context)
  986. {
  987. EmitTextureQuery(context, bindless: true);
  988. }
  989. private static void EmitTextureQuery(EmitterContext context, bool bindless)
  990. {
  991. OpCodeTex op = (OpCodeTex)context.CurrOp;
  992. if (op.Rd.IsRZ)
  993. {
  994. return;
  995. }
  996. context.Config.SetUsedFeature(FeatureFlags.IntegerSampling);
  997. TextureProperty property = (TextureProperty)op.RawOpCode.Extract(22, 6);
  998. // TODO: Validate and use property.
  999. Instruction inst = Instruction.TextureSize;
  1000. TextureFlags flags = bindless ? TextureFlags.Bindless : TextureFlags.None;
  1001. int raIndex = op.Ra.Index;
  1002. Operand Ra()
  1003. {
  1004. if (raIndex > RegisterConsts.RegisterZeroIndex)
  1005. {
  1006. return Const(0);
  1007. }
  1008. return context.Copy(Register(raIndex++, RegisterType.Gpr));
  1009. }
  1010. List<Operand> sourcesList = new List<Operand>();
  1011. if (bindless)
  1012. {
  1013. sourcesList.Add(Ra());
  1014. }
  1015. sourcesList.Add(Ra());
  1016. Operand[] sources = sourcesList.ToArray();
  1017. int rdIndex = op.Rd.Index;
  1018. Operand GetDest()
  1019. {
  1020. if (rdIndex > RegisterConsts.RegisterZeroIndex)
  1021. {
  1022. return Const(0);
  1023. }
  1024. return Register(rdIndex++, RegisterType.Gpr);
  1025. }
  1026. int handle = !bindless ? op.HandleOffset : 0;
  1027. SamplerType type;
  1028. if (bindless)
  1029. {
  1030. type = (op.ComponentMask & 4) != 0 ? SamplerType.Texture3D : SamplerType.Texture2D;
  1031. }
  1032. else
  1033. {
  1034. type = context.Config.GpuAccessor.QuerySamplerType(handle);
  1035. }
  1036. for (int compMask = op.ComponentMask, compIndex = 0; compMask != 0; compMask >>= 1, compIndex++)
  1037. {
  1038. if ((compMask & 1) != 0)
  1039. {
  1040. Operand dest = GetDest();
  1041. TextureOperation operation = context.CreateTextureOperation(
  1042. inst,
  1043. type,
  1044. flags,
  1045. handle,
  1046. compIndex,
  1047. dest,
  1048. sources);
  1049. context.Add(operation);
  1050. }
  1051. }
  1052. }
  1053. private static void EmitTextureSample(EmitterContext context, TextureFlags flags)
  1054. {
  1055. OpCodeTexture op = (OpCodeTexture)context.CurrOp;
  1056. bool isBindless = (flags & TextureFlags.Bindless) != 0;
  1057. if (op.Rd.IsRZ)
  1058. {
  1059. return;
  1060. }
  1061. int raIndex = op.Ra.Index;
  1062. int rbIndex = op.Rb.Index;
  1063. Operand Ra()
  1064. {
  1065. if (raIndex > RegisterConsts.RegisterZeroIndex)
  1066. {
  1067. return Const(0);
  1068. }
  1069. return context.Copy(Register(raIndex++, RegisterType.Gpr));
  1070. }
  1071. Operand Rb()
  1072. {
  1073. if (rbIndex > RegisterConsts.RegisterZeroIndex)
  1074. {
  1075. return Const(0);
  1076. }
  1077. return context.Copy(Register(rbIndex++, RegisterType.Gpr));
  1078. }
  1079. Operand arrayIndex = op.IsArray ? Ra() : null;
  1080. List<Operand> sourcesList = new List<Operand>();
  1081. if (isBindless)
  1082. {
  1083. sourcesList.Add(Rb());
  1084. }
  1085. SamplerType type = ConvertSamplerType(op.Dimensions);
  1086. bool hasLod = op.LodMode > TextureLodMode.LodZero;
  1087. if (type == SamplerType.Texture1D && (flags & ~TextureFlags.Bindless) == TextureFlags.IntCoords && !(hasLod ||
  1088. op.HasDepthCompare ||
  1089. op.HasOffset ||
  1090. op.IsArray ||
  1091. op.IsMultisample))
  1092. {
  1093. // For bindless, we don't have any way to know the texture type,
  1094. // so we assume it's texture buffer when the sampler type is 1D, since that's more common.
  1095. bool isTypeBuffer = isBindless || context.Config.GpuAccessor.QuerySamplerType(op.HandleOffset) == SamplerType.TextureBuffer;
  1096. if (isTypeBuffer)
  1097. {
  1098. type = SamplerType.TextureBuffer;
  1099. }
  1100. }
  1101. int coordsCount = type.GetDimensions();
  1102. for (int index = 0; index < coordsCount; index++)
  1103. {
  1104. sourcesList.Add(Ra());
  1105. }
  1106. if (Sample1DAs2D && type == SamplerType.Texture1D)
  1107. {
  1108. sourcesList.Add(ConstF(0));
  1109. type = SamplerType.Texture2D;
  1110. }
  1111. if (op.IsArray)
  1112. {
  1113. sourcesList.Add(arrayIndex);
  1114. type |= SamplerType.Array;
  1115. }
  1116. Operand lodValue = hasLod ? Rb() : ConstF(0);
  1117. Operand packedOffs = op.HasOffset ? Rb() : null;
  1118. if (op.HasDepthCompare)
  1119. {
  1120. sourcesList.Add(Rb());
  1121. type |= SamplerType.Shadow;
  1122. }
  1123. if ((op.LodMode == TextureLodMode.LodZero ||
  1124. op.LodMode == TextureLodMode.LodLevel ||
  1125. op.LodMode == TextureLodMode.LodLevelA) && !op.IsMultisample && type != SamplerType.TextureBuffer)
  1126. {
  1127. sourcesList.Add(lodValue);
  1128. flags |= TextureFlags.LodLevel;
  1129. }
  1130. if (op.HasOffset)
  1131. {
  1132. for (int index = 0; index < coordsCount; index++)
  1133. {
  1134. sourcesList.Add(context.BitfieldExtractS32(packedOffs, Const(index * 4), Const(4)));
  1135. }
  1136. flags |= TextureFlags.Offset;
  1137. }
  1138. if (op.LodMode == TextureLodMode.LodBias ||
  1139. op.LodMode == TextureLodMode.LodBiasA)
  1140. {
  1141. sourcesList.Add(lodValue);
  1142. flags |= TextureFlags.LodBias;
  1143. }
  1144. if (op.IsMultisample)
  1145. {
  1146. sourcesList.Add(Rb());
  1147. type |= SamplerType.Multisample;
  1148. }
  1149. Operand[] sources = sourcesList.ToArray();
  1150. int rdIndex = op.Rd.Index;
  1151. Operand GetDest()
  1152. {
  1153. if (rdIndex > RegisterConsts.RegisterZeroIndex)
  1154. {
  1155. return Const(0);
  1156. }
  1157. return Register(rdIndex++, RegisterType.Gpr);
  1158. }
  1159. int handle = !isBindless ? op.HandleOffset : 0;
  1160. for (int compMask = op.ComponentMask, compIndex = 0; compMask != 0; compMask >>= 1, compIndex++)
  1161. {
  1162. if ((compMask & 1) != 0)
  1163. {
  1164. Operand dest = GetDest();
  1165. TextureOperation operation = context.CreateTextureOperation(
  1166. Instruction.TextureSample,
  1167. type,
  1168. flags,
  1169. handle,
  1170. compIndex,
  1171. dest,
  1172. sources);
  1173. context.Add(operation);
  1174. }
  1175. }
  1176. }
  1177. private static int GetComponents(IntegerSize size)
  1178. {
  1179. return size switch
  1180. {
  1181. IntegerSize.B64 => 2,
  1182. IntegerSize.B128 => 4,
  1183. IntegerSize.UB128 => 4,
  1184. _ => 1
  1185. };
  1186. }
  1187. private static int GetComponentSizeInBytesLog2(IntegerSize size)
  1188. {
  1189. return size switch
  1190. {
  1191. IntegerSize.U8 => 0,
  1192. IntegerSize.S8 => 0,
  1193. IntegerSize.U16 => 1,
  1194. IntegerSize.S16 => 1,
  1195. IntegerSize.B32 => 2,
  1196. IntegerSize.B64 => 3,
  1197. IntegerSize.B128 => 4,
  1198. IntegerSize.UB128 => 4,
  1199. _ => 2
  1200. };
  1201. }
  1202. private static TextureFormat GetTextureFormat(IntegerSize size)
  1203. {
  1204. return size switch
  1205. {
  1206. IntegerSize.U8 => TextureFormat.R8Uint,
  1207. IntegerSize.S8 => TextureFormat.R8Sint,
  1208. IntegerSize.U16 => TextureFormat.R16Uint,
  1209. IntegerSize.S16 => TextureFormat.R16Sint,
  1210. IntegerSize.B32 => TextureFormat.R32Uint,
  1211. IntegerSize.B64 => TextureFormat.R32G32Uint,
  1212. IntegerSize.B128 => TextureFormat.R32G32B32A32Uint,
  1213. IntegerSize.UB128 => TextureFormat.R32G32B32A32Uint,
  1214. _ => TextureFormat.R32Uint
  1215. };
  1216. }
  1217. private static int GetComponentSizeInBytesLog2(ReductionType type)
  1218. {
  1219. return type switch
  1220. {
  1221. ReductionType.U32 => 2,
  1222. ReductionType.S32 => 2,
  1223. ReductionType.U64 => 3,
  1224. ReductionType.FP32FtzRn => 2,
  1225. ReductionType.FP16x2FtzRn => 2,
  1226. ReductionType.S64 => 3,
  1227. ReductionType.SD32 => 2,
  1228. ReductionType.SD64 => 3,
  1229. _ => 2
  1230. };
  1231. }
  1232. private static TextureFormat GetTextureFormat(ReductionType type)
  1233. {
  1234. return type switch
  1235. {
  1236. ReductionType.U32 => TextureFormat.R32Uint,
  1237. ReductionType.S32 => TextureFormat.R32Sint,
  1238. ReductionType.U64 => TextureFormat.R32G32Uint,
  1239. ReductionType.FP32FtzRn => TextureFormat.R32Float,
  1240. ReductionType.FP16x2FtzRn => TextureFormat.R16G16Float,
  1241. ReductionType.S64 => TextureFormat.R32G32Uint,
  1242. ReductionType.SD32 => TextureFormat.R32Uint,
  1243. ReductionType.SD64 => TextureFormat.R32G32Uint,
  1244. _ => TextureFormat.R32Uint
  1245. };
  1246. }
  1247. private static TextureFlags GetAtomicOpFlags(AtomicOp op)
  1248. {
  1249. return op switch
  1250. {
  1251. AtomicOp.Add => TextureFlags.Add,
  1252. AtomicOp.Minimum => TextureFlags.Minimum,
  1253. AtomicOp.Maximum => TextureFlags.Maximum,
  1254. AtomicOp.Increment => TextureFlags.Increment,
  1255. AtomicOp.Decrement => TextureFlags.Decrement,
  1256. AtomicOp.BitwiseAnd => TextureFlags.BitwiseAnd,
  1257. AtomicOp.BitwiseOr => TextureFlags.BitwiseOr,
  1258. AtomicOp.BitwiseExclusiveOr => TextureFlags.BitwiseXor,
  1259. AtomicOp.Swap => TextureFlags.Swap,
  1260. _ => TextureFlags.Add
  1261. };
  1262. }
  1263. private static SamplerType ConvertSamplerType(ImageDimensions target)
  1264. {
  1265. return target switch
  1266. {
  1267. ImageDimensions.Image1D => SamplerType.Texture1D,
  1268. ImageDimensions.ImageBuffer => SamplerType.TextureBuffer,
  1269. ImageDimensions.Image1DArray => SamplerType.Texture1D | SamplerType.Array,
  1270. ImageDimensions.Image2D => SamplerType.Texture2D,
  1271. ImageDimensions.Image2DArray => SamplerType.Texture2D | SamplerType.Array,
  1272. ImageDimensions.Image3D => SamplerType.Texture3D,
  1273. _ => SamplerType.None
  1274. };
  1275. }
  1276. private static SamplerType ConvertSamplerType(TextureDimensions dimensions)
  1277. {
  1278. return dimensions switch
  1279. {
  1280. TextureDimensions.Texture1D => SamplerType.Texture1D,
  1281. TextureDimensions.Texture2D => SamplerType.Texture2D,
  1282. TextureDimensions.Texture3D => SamplerType.Texture3D,
  1283. TextureDimensions.TextureCube => SamplerType.TextureCube,
  1284. _ => throw new ArgumentException($"Invalid texture dimensions \"{dimensions}\".")
  1285. };
  1286. }
  1287. private static SamplerType ConvertSamplerType(TextureTarget type)
  1288. {
  1289. switch (type)
  1290. {
  1291. case TextureTarget.Texture1DLodZero:
  1292. return SamplerType.Texture1D;
  1293. case TextureTarget.Texture2D:
  1294. case TextureTarget.Texture2DLodZero:
  1295. case TextureTarget.Texture2DLodLevel:
  1296. return SamplerType.Texture2D;
  1297. case TextureTarget.Texture2DDepthCompare:
  1298. case TextureTarget.Texture2DLodLevelDepthCompare:
  1299. case TextureTarget.Texture2DLodZeroDepthCompare:
  1300. return SamplerType.Texture2D | SamplerType.Shadow;
  1301. case TextureTarget.Texture2DArray:
  1302. case TextureTarget.Texture2DArrayLodZero:
  1303. return SamplerType.Texture2D | SamplerType.Array;
  1304. case TextureTarget.Texture2DArrayLodZeroDepthCompare:
  1305. return SamplerType.Texture2D | SamplerType.Array | SamplerType.Shadow;
  1306. case TextureTarget.Texture3D:
  1307. case TextureTarget.Texture3DLodZero:
  1308. return SamplerType.Texture3D;
  1309. case TextureTarget.TextureCube:
  1310. case TextureTarget.TextureCubeLodLevel:
  1311. return SamplerType.TextureCube;
  1312. }
  1313. return SamplerType.None;
  1314. }
  1315. private static SamplerType ConvertSamplerType(TexelLoadTarget type)
  1316. {
  1317. switch (type)
  1318. {
  1319. case TexelLoadTarget.Texture1DLodZero:
  1320. case TexelLoadTarget.Texture1DLodLevel:
  1321. return SamplerType.Texture1D;
  1322. case TexelLoadTarget.Texture2DLodZero:
  1323. case TexelLoadTarget.Texture2DLodZeroOffset:
  1324. case TexelLoadTarget.Texture2DLodLevel:
  1325. case TexelLoadTarget.Texture2DLodLevelOffset:
  1326. return SamplerType.Texture2D;
  1327. case TexelLoadTarget.Texture2DLodZeroMultisample:
  1328. return SamplerType.Texture2D | SamplerType.Multisample;
  1329. case TexelLoadTarget.Texture3DLodZero:
  1330. return SamplerType.Texture3D;
  1331. case TexelLoadTarget.Texture2DArrayLodZero:
  1332. return SamplerType.Texture2D | SamplerType.Array;
  1333. }
  1334. return SamplerType.None;
  1335. }
  1336. private static TextureFlags ConvertTextureFlags(TextureTarget type)
  1337. {
  1338. switch (type)
  1339. {
  1340. case TextureTarget.Texture1DLodZero:
  1341. case TextureTarget.Texture2DLodZero:
  1342. case TextureTarget.Texture2DLodLevel:
  1343. case TextureTarget.Texture2DLodLevelDepthCompare:
  1344. case TextureTarget.Texture2DLodZeroDepthCompare:
  1345. case TextureTarget.Texture2DArrayLodZero:
  1346. case TextureTarget.Texture2DArrayLodZeroDepthCompare:
  1347. case TextureTarget.Texture3DLodZero:
  1348. case TextureTarget.TextureCubeLodLevel:
  1349. return TextureFlags.LodLevel;
  1350. case TextureTarget.Texture2D:
  1351. case TextureTarget.Texture2DDepthCompare:
  1352. case TextureTarget.Texture2DArray:
  1353. case TextureTarget.Texture3D:
  1354. case TextureTarget.TextureCube:
  1355. return TextureFlags.None;
  1356. }
  1357. return TextureFlags.None;
  1358. }
  1359. private static TextureFlags ConvertTextureFlags(TexelLoadTarget type)
  1360. {
  1361. switch (type)
  1362. {
  1363. case TexelLoadTarget.Texture1DLodZero:
  1364. case TexelLoadTarget.Texture1DLodLevel:
  1365. case TexelLoadTarget.Texture2DLodZero:
  1366. case TexelLoadTarget.Texture2DLodLevel:
  1367. case TexelLoadTarget.Texture2DLodZeroMultisample:
  1368. case TexelLoadTarget.Texture3DLodZero:
  1369. case TexelLoadTarget.Texture2DArrayLodZero:
  1370. return TextureFlags.LodLevel;
  1371. case TexelLoadTarget.Texture2DLodZeroOffset:
  1372. case TexelLoadTarget.Texture2DLodLevelOffset:
  1373. return TextureFlags.LodLevel | TextureFlags.Offset;
  1374. }
  1375. return TextureFlags.None;
  1376. }
  1377. }
  1378. }