Instructions.cs 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using Ryujinx.Graphics.Shader.StructuredIr;
  3. using Ryujinx.Graphics.Shader.Translation;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Numerics;
  9. using static Spv.Specification;
  10. namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
  11. {
  12. using SpvInstruction = Spv.Generator.Instruction;
  13. using SpvLiteralInteger = Spv.Generator.LiteralInteger;
  14. static class Instructions
  15. {
  16. private const MemorySemanticsMask DefaultMemorySemantics =
  17. MemorySemanticsMask.ImageMemory |
  18. MemorySemanticsMask.AtomicCounterMemory |
  19. MemorySemanticsMask.WorkgroupMemory |
  20. MemorySemanticsMask.UniformMemory |
  21. MemorySemanticsMask.AcquireRelease;
  22. private static readonly Func<CodeGenContext, AstOperation, OperationResult>[] InstTable;
  23. static Instructions()
  24. {
  25. InstTable = new Func<CodeGenContext, AstOperation, OperationResult>[(int)Instruction.Count];
  26. Add(Instruction.Absolute, GenerateAbsolute);
  27. Add(Instruction.Add, GenerateAdd);
  28. Add(Instruction.AtomicAdd, GenerateAtomicAdd);
  29. Add(Instruction.AtomicAnd, GenerateAtomicAnd);
  30. Add(Instruction.AtomicCompareAndSwap, GenerateAtomicCompareAndSwap);
  31. Add(Instruction.AtomicMinS32, GenerateAtomicMinS32);
  32. Add(Instruction.AtomicMinU32, GenerateAtomicMinU32);
  33. Add(Instruction.AtomicMaxS32, GenerateAtomicMaxS32);
  34. Add(Instruction.AtomicMaxU32, GenerateAtomicMaxU32);
  35. Add(Instruction.AtomicOr, GenerateAtomicOr);
  36. Add(Instruction.AtomicSwap, GenerateAtomicSwap);
  37. Add(Instruction.AtomicXor, GenerateAtomicXor);
  38. Add(Instruction.Ballot, GenerateBallot);
  39. Add(Instruction.Barrier, GenerateBarrier);
  40. Add(Instruction.BitCount, GenerateBitCount);
  41. Add(Instruction.BitfieldExtractS32, GenerateBitfieldExtractS32);
  42. Add(Instruction.BitfieldExtractU32, GenerateBitfieldExtractU32);
  43. Add(Instruction.BitfieldInsert, GenerateBitfieldInsert);
  44. Add(Instruction.BitfieldReverse, GenerateBitfieldReverse);
  45. Add(Instruction.BitwiseAnd, GenerateBitwiseAnd);
  46. Add(Instruction.BitwiseExclusiveOr, GenerateBitwiseExclusiveOr);
  47. Add(Instruction.BitwiseNot, GenerateBitwiseNot);
  48. Add(Instruction.BitwiseOr, GenerateBitwiseOr);
  49. Add(Instruction.Call, GenerateCall);
  50. Add(Instruction.Ceiling, GenerateCeiling);
  51. Add(Instruction.Clamp, GenerateClamp);
  52. Add(Instruction.ClampU32, GenerateClampU32);
  53. Add(Instruction.Comment, GenerateComment);
  54. Add(Instruction.CompareEqual, GenerateCompareEqual);
  55. Add(Instruction.CompareGreater, GenerateCompareGreater);
  56. Add(Instruction.CompareGreaterOrEqual, GenerateCompareGreaterOrEqual);
  57. Add(Instruction.CompareGreaterOrEqualU32, GenerateCompareGreaterOrEqualU32);
  58. Add(Instruction.CompareGreaterU32, GenerateCompareGreaterU32);
  59. Add(Instruction.CompareLess, GenerateCompareLess);
  60. Add(Instruction.CompareLessOrEqual, GenerateCompareLessOrEqual);
  61. Add(Instruction.CompareLessOrEqualU32, GenerateCompareLessOrEqualU32);
  62. Add(Instruction.CompareLessU32, GenerateCompareLessU32);
  63. Add(Instruction.CompareNotEqual, GenerateCompareNotEqual);
  64. Add(Instruction.ConditionalSelect, GenerateConditionalSelect);
  65. Add(Instruction.ConvertFP32ToFP64, GenerateConvertFP32ToFP64);
  66. Add(Instruction.ConvertFP32ToS32, GenerateConvertFP32ToS32);
  67. Add(Instruction.ConvertFP32ToU32, GenerateConvertFP32ToU32);
  68. Add(Instruction.ConvertFP64ToFP32, GenerateConvertFP64ToFP32);
  69. Add(Instruction.ConvertFP64ToS32, GenerateConvertFP64ToS32);
  70. Add(Instruction.ConvertFP64ToU32, GenerateConvertFP64ToU32);
  71. Add(Instruction.ConvertS32ToFP32, GenerateConvertS32ToFP32);
  72. Add(Instruction.ConvertS32ToFP64, GenerateConvertS32ToFP64);
  73. Add(Instruction.ConvertU32ToFP32, GenerateConvertU32ToFP32);
  74. Add(Instruction.ConvertU32ToFP64, GenerateConvertU32ToFP64);
  75. Add(Instruction.Cosine, GenerateCosine);
  76. Add(Instruction.Ddx, GenerateDdx);
  77. Add(Instruction.Ddy, GenerateDdy);
  78. Add(Instruction.Discard, GenerateDiscard);
  79. Add(Instruction.Divide, GenerateDivide);
  80. Add(Instruction.EmitVertex, GenerateEmitVertex);
  81. Add(Instruction.EndPrimitive, GenerateEndPrimitive);
  82. Add(Instruction.ExponentB2, GenerateExponentB2);
  83. Add(Instruction.FSIBegin, GenerateFSIBegin);
  84. Add(Instruction.FSIEnd, GenerateFSIEnd);
  85. Add(Instruction.FindLSB, GenerateFindLSB);
  86. Add(Instruction.FindMSBS32, GenerateFindMSBS32);
  87. Add(Instruction.FindMSBU32, GenerateFindMSBU32);
  88. Add(Instruction.Floor, GenerateFloor);
  89. Add(Instruction.FusedMultiplyAdd, GenerateFusedMultiplyAdd);
  90. Add(Instruction.GroupMemoryBarrier, GenerateGroupMemoryBarrier);
  91. Add(Instruction.ImageAtomic, GenerateImageAtomic);
  92. Add(Instruction.ImageLoad, GenerateImageLoad);
  93. Add(Instruction.ImageStore, GenerateImageStore);
  94. Add(Instruction.IsNan, GenerateIsNan);
  95. Add(Instruction.LoadAttribute, GenerateLoadAttribute);
  96. Add(Instruction.LoadConstant, GenerateLoadConstant);
  97. Add(Instruction.LoadLocal, GenerateLoadLocal);
  98. Add(Instruction.LoadShared, GenerateLoadShared);
  99. Add(Instruction.LoadStorage, GenerateLoadStorage);
  100. Add(Instruction.Lod, GenerateLod);
  101. Add(Instruction.LogarithmB2, GenerateLogarithmB2);
  102. Add(Instruction.LogicalAnd, GenerateLogicalAnd);
  103. Add(Instruction.LogicalExclusiveOr, GenerateLogicalExclusiveOr);
  104. Add(Instruction.LogicalNot, GenerateLogicalNot);
  105. Add(Instruction.LogicalOr, GenerateLogicalOr);
  106. Add(Instruction.LoopBreak, GenerateLoopBreak);
  107. Add(Instruction.LoopContinue, GenerateLoopContinue);
  108. Add(Instruction.Maximum, GenerateMaximum);
  109. Add(Instruction.MaximumU32, GenerateMaximumU32);
  110. Add(Instruction.MemoryBarrier, GenerateMemoryBarrier);
  111. Add(Instruction.Minimum, GenerateMinimum);
  112. Add(Instruction.MinimumU32, GenerateMinimumU32);
  113. Add(Instruction.Multiply, GenerateMultiply);
  114. Add(Instruction.MultiplyHighS32, GenerateMultiplyHighS32);
  115. Add(Instruction.MultiplyHighU32, GenerateMultiplyHighU32);
  116. Add(Instruction.Negate, GenerateNegate);
  117. Add(Instruction.PackDouble2x32, GeneratePackDouble2x32);
  118. Add(Instruction.PackHalf2x16, GeneratePackHalf2x16);
  119. Add(Instruction.ReciprocalSquareRoot, GenerateReciprocalSquareRoot);
  120. Add(Instruction.Return, GenerateReturn);
  121. Add(Instruction.Round, GenerateRound);
  122. Add(Instruction.ShiftLeft, GenerateShiftLeft);
  123. Add(Instruction.ShiftRightS32, GenerateShiftRightS32);
  124. Add(Instruction.ShiftRightU32, GenerateShiftRightU32);
  125. Add(Instruction.Shuffle, GenerateShuffle);
  126. Add(Instruction.ShuffleDown, GenerateShuffleDown);
  127. Add(Instruction.ShuffleUp, GenerateShuffleUp);
  128. Add(Instruction.ShuffleXor, GenerateShuffleXor);
  129. Add(Instruction.Sine, GenerateSine);
  130. Add(Instruction.SquareRoot, GenerateSquareRoot);
  131. Add(Instruction.StoreAttribute, GenerateStoreAttribute);
  132. Add(Instruction.StoreLocal, GenerateStoreLocal);
  133. Add(Instruction.StoreShared, GenerateStoreShared);
  134. Add(Instruction.StoreShared16, GenerateStoreShared16);
  135. Add(Instruction.StoreShared8, GenerateStoreShared8);
  136. Add(Instruction.StoreStorage, GenerateStoreStorage);
  137. Add(Instruction.StoreStorage16, GenerateStoreStorage16);
  138. Add(Instruction.StoreStorage8, GenerateStoreStorage8);
  139. Add(Instruction.Subtract, GenerateSubtract);
  140. Add(Instruction.SwizzleAdd, GenerateSwizzleAdd);
  141. Add(Instruction.TextureSample, GenerateTextureSample);
  142. Add(Instruction.TextureSize, GenerateTextureSize);
  143. Add(Instruction.Truncate, GenerateTruncate);
  144. Add(Instruction.UnpackDouble2x32, GenerateUnpackDouble2x32);
  145. Add(Instruction.UnpackHalf2x16, GenerateUnpackHalf2x16);
  146. Add(Instruction.VectorExtract, GenerateVectorExtract);
  147. Add(Instruction.VoteAll, GenerateVoteAll);
  148. Add(Instruction.VoteAllEqual, GenerateVoteAllEqual);
  149. Add(Instruction.VoteAny, GenerateVoteAny);
  150. }
  151. private static void Add(Instruction inst, Func<CodeGenContext, AstOperation, OperationResult> handler)
  152. {
  153. InstTable[(int)(inst & Instruction.Mask)] = handler;
  154. }
  155. public static OperationResult Generate(CodeGenContext context, AstOperation operation)
  156. {
  157. var handler = InstTable[(int)(operation.Inst & Instruction.Mask)];
  158. if (handler != null)
  159. {
  160. return handler(context, operation);
  161. }
  162. else
  163. {
  164. throw new NotImplementedException(operation.Inst.ToString());
  165. }
  166. }
  167. private static OperationResult GenerateAbsolute(CodeGenContext context, AstOperation operation)
  168. {
  169. return GenerateUnary(context, operation, context.Delegates.GlslFAbs, context.Delegates.GlslSAbs);
  170. }
  171. private static OperationResult GenerateAdd(CodeGenContext context, AstOperation operation)
  172. {
  173. return GenerateBinary(context, operation, context.Delegates.FAdd, context.Delegates.IAdd);
  174. }
  175. private static OperationResult GenerateAtomicAdd(CodeGenContext context, AstOperation operation)
  176. {
  177. return GenerateAtomicMemoryBinary(context, operation, context.Delegates.AtomicIAdd);
  178. }
  179. private static OperationResult GenerateAtomicAnd(CodeGenContext context, AstOperation operation)
  180. {
  181. return GenerateAtomicMemoryBinary(context, operation, context.Delegates.AtomicAnd);
  182. }
  183. private static OperationResult GenerateAtomicCompareAndSwap(CodeGenContext context, AstOperation operation)
  184. {
  185. return GenerateAtomicMemoryCas(context, operation);
  186. }
  187. private static OperationResult GenerateAtomicMinS32(CodeGenContext context, AstOperation operation)
  188. {
  189. return GenerateAtomicMemoryBinary(context, operation, context.Delegates.AtomicSMin);
  190. }
  191. private static OperationResult GenerateAtomicMinU32(CodeGenContext context, AstOperation operation)
  192. {
  193. return GenerateAtomicMemoryBinary(context, operation, context.Delegates.AtomicUMin);
  194. }
  195. private static OperationResult GenerateAtomicMaxS32(CodeGenContext context, AstOperation operation)
  196. {
  197. return GenerateAtomicMemoryBinary(context, operation, context.Delegates.AtomicSMax);
  198. }
  199. private static OperationResult GenerateAtomicMaxU32(CodeGenContext context, AstOperation operation)
  200. {
  201. return GenerateAtomicMemoryBinary(context, operation, context.Delegates.AtomicUMax);
  202. }
  203. private static OperationResult GenerateAtomicOr(CodeGenContext context, AstOperation operation)
  204. {
  205. return GenerateAtomicMemoryBinary(context, operation, context.Delegates.AtomicOr);
  206. }
  207. private static OperationResult GenerateAtomicSwap(CodeGenContext context, AstOperation operation)
  208. {
  209. return GenerateAtomicMemoryBinary(context, operation, context.Delegates.AtomicExchange);
  210. }
  211. private static OperationResult GenerateAtomicXor(CodeGenContext context, AstOperation operation)
  212. {
  213. return GenerateAtomicMemoryBinary(context, operation, context.Delegates.AtomicXor);
  214. }
  215. private static OperationResult GenerateBallot(CodeGenContext context, AstOperation operation)
  216. {
  217. var source = operation.GetSource(0);
  218. var uvec4Type = context.TypeVector(context.TypeU32(), 4);
  219. var execution = context.Constant(context.TypeU32(), Scope.Subgroup);
  220. var maskVector = context.GroupNonUniformBallot(uvec4Type, execution, context.Get(AggregateType.Bool, source));
  221. var mask = context.CompositeExtract(context.TypeU32(), maskVector, (SpvLiteralInteger)0);
  222. return new OperationResult(AggregateType.U32, mask);
  223. }
  224. private static OperationResult GenerateBarrier(CodeGenContext context, AstOperation operation)
  225. {
  226. context.ControlBarrier(
  227. context.Constant(context.TypeU32(), Scope.Workgroup),
  228. context.Constant(context.TypeU32(), Scope.Workgroup),
  229. context.Constant(context.TypeU32(), MemorySemanticsMask.WorkgroupMemory | MemorySemanticsMask.AcquireRelease));
  230. return OperationResult.Invalid;
  231. }
  232. private static OperationResult GenerateBitCount(CodeGenContext context, AstOperation operation)
  233. {
  234. return GenerateUnaryS32(context, operation, context.Delegates.BitCount);
  235. }
  236. private static OperationResult GenerateBitfieldExtractS32(CodeGenContext context, AstOperation operation)
  237. {
  238. return GenerateBitfieldExtractS32(context, operation, context.Delegates.BitFieldSExtract);
  239. }
  240. private static OperationResult GenerateBitfieldExtractU32(CodeGenContext context, AstOperation operation)
  241. {
  242. return GenerateTernaryU32(context, operation, context.Delegates.BitFieldUExtract);
  243. }
  244. private static OperationResult GenerateBitfieldInsert(CodeGenContext context, AstOperation operation)
  245. {
  246. return GenerateBitfieldInsert(context, operation, context.Delegates.BitFieldInsert);
  247. }
  248. private static OperationResult GenerateBitfieldReverse(CodeGenContext context, AstOperation operation)
  249. {
  250. return GenerateUnaryS32(context, operation, context.Delegates.BitReverse);
  251. }
  252. private static OperationResult GenerateBitwiseAnd(CodeGenContext context, AstOperation operation)
  253. {
  254. return GenerateBinaryS32(context, operation, context.Delegates.BitwiseAnd);
  255. }
  256. private static OperationResult GenerateBitwiseExclusiveOr(CodeGenContext context, AstOperation operation)
  257. {
  258. return GenerateBinaryS32(context, operation, context.Delegates.BitwiseXor);
  259. }
  260. private static OperationResult GenerateBitwiseNot(CodeGenContext context, AstOperation operation)
  261. {
  262. return GenerateUnaryS32(context, operation, context.Delegates.Not);
  263. }
  264. private static OperationResult GenerateBitwiseOr(CodeGenContext context, AstOperation operation)
  265. {
  266. return GenerateBinaryS32(context, operation, context.Delegates.BitwiseOr);
  267. }
  268. private static OperationResult GenerateCall(CodeGenContext context, AstOperation operation)
  269. {
  270. AstOperand funcId = (AstOperand)operation.GetSource(0);
  271. Debug.Assert(funcId.Type == OperandType.Constant);
  272. (var function, var spvFunc) = context.GetFunction(funcId.Value);
  273. var args = new SpvInstruction[operation.SourcesCount - 1];
  274. var spvLocals = context.GetLocalForArgsPointers(funcId.Value);
  275. for (int i = 0; i < args.Length; i++)
  276. {
  277. var operand = (AstOperand)operation.GetSource(i + 1);
  278. if (i >= function.InArguments.Length)
  279. {
  280. args[i] = context.GetLocalPointer(operand);
  281. }
  282. else
  283. {
  284. var type = function.GetArgumentType(i);
  285. var value = context.Get(type, operand);
  286. var spvLocal = spvLocals[i];
  287. context.Store(spvLocal, value);
  288. args[i] = spvLocal;
  289. }
  290. }
  291. var retType = function.ReturnType;
  292. var result = context.FunctionCall(context.GetType(retType), spvFunc, args);
  293. return new OperationResult(retType, result);
  294. }
  295. private static OperationResult GenerateCeiling(CodeGenContext context, AstOperation operation)
  296. {
  297. return GenerateUnary(context, operation, context.Delegates.GlslCeil, null);
  298. }
  299. private static OperationResult GenerateClamp(CodeGenContext context, AstOperation operation)
  300. {
  301. return GenerateTernary(context, operation, context.Delegates.GlslFClamp, context.Delegates.GlslSClamp);
  302. }
  303. private static OperationResult GenerateClampU32(CodeGenContext context, AstOperation operation)
  304. {
  305. return GenerateTernaryU32(context, operation, context.Delegates.GlslUClamp);
  306. }
  307. private static OperationResult GenerateComment(CodeGenContext context, AstOperation operation)
  308. {
  309. return OperationResult.Invalid;
  310. }
  311. private static OperationResult GenerateCompareEqual(CodeGenContext context, AstOperation operation)
  312. {
  313. return GenerateCompare(context, operation, context.Delegates.FOrdEqual, context.Delegates.IEqual);
  314. }
  315. private static OperationResult GenerateCompareGreater(CodeGenContext context, AstOperation operation)
  316. {
  317. return GenerateCompare(context, operation, context.Delegates.FOrdGreaterThan, context.Delegates.SGreaterThan);
  318. }
  319. private static OperationResult GenerateCompareGreaterOrEqual(CodeGenContext context, AstOperation operation)
  320. {
  321. return GenerateCompare(context, operation, context.Delegates.FOrdGreaterThanEqual, context.Delegates.SGreaterThanEqual);
  322. }
  323. private static OperationResult GenerateCompareGreaterOrEqualU32(CodeGenContext context, AstOperation operation)
  324. {
  325. return GenerateCompareU32(context, operation, context.Delegates.UGreaterThanEqual);
  326. }
  327. private static OperationResult GenerateCompareGreaterU32(CodeGenContext context, AstOperation operation)
  328. {
  329. return GenerateCompareU32(context, operation, context.Delegates.UGreaterThan);
  330. }
  331. private static OperationResult GenerateCompareLess(CodeGenContext context, AstOperation operation)
  332. {
  333. return GenerateCompare(context, operation, context.Delegates.FOrdLessThan, context.Delegates.SLessThan);
  334. }
  335. private static OperationResult GenerateCompareLessOrEqual(CodeGenContext context, AstOperation operation)
  336. {
  337. return GenerateCompare(context, operation, context.Delegates.FOrdLessThanEqual, context.Delegates.SLessThanEqual);
  338. }
  339. private static OperationResult GenerateCompareLessOrEqualU32(CodeGenContext context, AstOperation operation)
  340. {
  341. return GenerateCompareU32(context, operation, context.Delegates.ULessThanEqual);
  342. }
  343. private static OperationResult GenerateCompareLessU32(CodeGenContext context, AstOperation operation)
  344. {
  345. return GenerateCompareU32(context, operation, context.Delegates.ULessThan);
  346. }
  347. private static OperationResult GenerateCompareNotEqual(CodeGenContext context, AstOperation operation)
  348. {
  349. return GenerateCompare(context, operation, context.Delegates.FOrdNotEqual, context.Delegates.INotEqual);
  350. }
  351. private static OperationResult GenerateConditionalSelect(CodeGenContext context, AstOperation operation)
  352. {
  353. var src1 = operation.GetSource(0);
  354. var src2 = operation.GetSource(1);
  355. var src3 = operation.GetSource(2);
  356. var cond = context.Get(AggregateType.Bool, src1);
  357. if (operation.Inst.HasFlag(Instruction.FP64))
  358. {
  359. return new OperationResult(AggregateType.FP64, context.Select(context.TypeFP64(), cond, context.GetFP64(src2), context.GetFP64(src3)));
  360. }
  361. else if (operation.Inst.HasFlag(Instruction.FP32))
  362. {
  363. return new OperationResult(AggregateType.FP32, context.Select(context.TypeFP32(), cond, context.GetFP32(src2), context.GetFP32(src3)));
  364. }
  365. else
  366. {
  367. return new OperationResult(AggregateType.S32, context.Select(context.TypeS32(), cond, context.GetS32(src2), context.GetS32(src3)));
  368. }
  369. }
  370. private static OperationResult GenerateConvertFP32ToFP64(CodeGenContext context, AstOperation operation)
  371. {
  372. var source = operation.GetSource(0);
  373. return new OperationResult(AggregateType.FP64, context.FConvert(context.TypeFP64(), context.GetFP32(source)));
  374. }
  375. private static OperationResult GenerateConvertFP32ToS32(CodeGenContext context, AstOperation operation)
  376. {
  377. var source = operation.GetSource(0);
  378. return new OperationResult(AggregateType.S32, context.ConvertFToS(context.TypeS32(), context.GetFP32(source)));
  379. }
  380. private static OperationResult GenerateConvertFP32ToU32(CodeGenContext context, AstOperation operation)
  381. {
  382. var source = operation.GetSource(0);
  383. return new OperationResult(AggregateType.U32, context.ConvertFToU(context.TypeU32(), context.GetFP32(source)));
  384. }
  385. private static OperationResult GenerateConvertFP64ToFP32(CodeGenContext context, AstOperation operation)
  386. {
  387. var source = operation.GetSource(0);
  388. return new OperationResult(AggregateType.FP32, context.FConvert(context.TypeFP32(), context.GetFP64(source)));
  389. }
  390. private static OperationResult GenerateConvertFP64ToS32(CodeGenContext context, AstOperation operation)
  391. {
  392. var source = operation.GetSource(0);
  393. return new OperationResult(AggregateType.S32, context.ConvertFToS(context.TypeS32(), context.GetFP64(source)));
  394. }
  395. private static OperationResult GenerateConvertFP64ToU32(CodeGenContext context, AstOperation operation)
  396. {
  397. var source = operation.GetSource(0);
  398. return new OperationResult(AggregateType.U32, context.ConvertFToU(context.TypeU32(), context.GetFP64(source)));
  399. }
  400. private static OperationResult GenerateConvertS32ToFP32(CodeGenContext context, AstOperation operation)
  401. {
  402. var source = operation.GetSource(0);
  403. return new OperationResult(AggregateType.FP32, context.ConvertSToF(context.TypeFP32(), context.GetS32(source)));
  404. }
  405. private static OperationResult GenerateConvertS32ToFP64(CodeGenContext context, AstOperation operation)
  406. {
  407. var source = operation.GetSource(0);
  408. return new OperationResult(AggregateType.FP64, context.ConvertSToF(context.TypeFP64(), context.GetS32(source)));
  409. }
  410. private static OperationResult GenerateConvertU32ToFP32(CodeGenContext context, AstOperation operation)
  411. {
  412. var source = operation.GetSource(0);
  413. return new OperationResult(AggregateType.FP32, context.ConvertUToF(context.TypeFP32(), context.GetU32(source)));
  414. }
  415. private static OperationResult GenerateConvertU32ToFP64(CodeGenContext context, AstOperation operation)
  416. {
  417. var source = operation.GetSource(0);
  418. return new OperationResult(AggregateType.FP64, context.ConvertUToF(context.TypeFP64(), context.GetU32(source)));
  419. }
  420. private static OperationResult GenerateCosine(CodeGenContext context, AstOperation operation)
  421. {
  422. return GenerateUnary(context, operation, context.Delegates.GlslCos, null);
  423. }
  424. private static OperationResult GenerateDdx(CodeGenContext context, AstOperation operation)
  425. {
  426. return GenerateUnaryFP32(context, operation, context.Delegates.DPdx);
  427. }
  428. private static OperationResult GenerateDdy(CodeGenContext context, AstOperation operation)
  429. {
  430. return GenerateUnaryFP32(context, operation, context.Delegates.DPdy);
  431. }
  432. private static OperationResult GenerateDiscard(CodeGenContext context, AstOperation operation)
  433. {
  434. context.Kill();
  435. return OperationResult.Invalid;
  436. }
  437. private static OperationResult GenerateDivide(CodeGenContext context, AstOperation operation)
  438. {
  439. return GenerateBinary(context, operation, context.Delegates.FDiv, context.Delegates.SDiv);
  440. }
  441. private static OperationResult GenerateEmitVertex(CodeGenContext context, AstOperation operation)
  442. {
  443. context.EmitVertex();
  444. return OperationResult.Invalid;
  445. }
  446. private static OperationResult GenerateEndPrimitive(CodeGenContext context, AstOperation operation)
  447. {
  448. context.EndPrimitive();
  449. return OperationResult.Invalid;
  450. }
  451. private static OperationResult GenerateExponentB2(CodeGenContext context, AstOperation operation)
  452. {
  453. return GenerateUnary(context, operation, context.Delegates.GlslExp2, null);
  454. }
  455. private static OperationResult GenerateFSIBegin(CodeGenContext context, AstOperation operation)
  456. {
  457. if (context.Config.GpuAccessor.QueryHostSupportsFragmentShaderInterlock())
  458. {
  459. context.BeginInvocationInterlockEXT();
  460. }
  461. return OperationResult.Invalid;
  462. }
  463. private static OperationResult GenerateFSIEnd(CodeGenContext context, AstOperation operation)
  464. {
  465. if (context.Config.GpuAccessor.QueryHostSupportsFragmentShaderInterlock())
  466. {
  467. context.EndInvocationInterlockEXT();
  468. }
  469. return OperationResult.Invalid;
  470. }
  471. private static OperationResult GenerateFindLSB(CodeGenContext context, AstOperation operation)
  472. {
  473. var source = context.GetU32(operation.GetSource(0));
  474. return new OperationResult(AggregateType.U32, context.GlslFindILsb(context.TypeU32(), source));
  475. }
  476. private static OperationResult GenerateFindMSBS32(CodeGenContext context, AstOperation operation)
  477. {
  478. var source = context.GetS32(operation.GetSource(0));
  479. return new OperationResult(AggregateType.U32, context.GlslFindSMsb(context.TypeU32(), source));
  480. }
  481. private static OperationResult GenerateFindMSBU32(CodeGenContext context, AstOperation operation)
  482. {
  483. var source = context.GetU32(operation.GetSource(0));
  484. return new OperationResult(AggregateType.U32, context.GlslFindUMsb(context.TypeU32(), source));
  485. }
  486. private static OperationResult GenerateFloor(CodeGenContext context, AstOperation operation)
  487. {
  488. return GenerateUnary(context, operation, context.Delegates.GlslFloor, null);
  489. }
  490. private static OperationResult GenerateFusedMultiplyAdd(CodeGenContext context, AstOperation operation)
  491. {
  492. return GenerateTernary(context, operation, context.Delegates.GlslFma, null);
  493. }
  494. private static OperationResult GenerateGroupMemoryBarrier(CodeGenContext context, AstOperation operation)
  495. {
  496. context.MemoryBarrier(context.Constant(context.TypeU32(), Scope.Workgroup), context.Constant(context.TypeU32(), DefaultMemorySemantics));
  497. return OperationResult.Invalid;
  498. }
  499. private static OperationResult GenerateImageAtomic(CodeGenContext context, AstOperation operation)
  500. {
  501. AstTextureOperation texOp = (AstTextureOperation)operation;
  502. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  503. var componentType = texOp.Format.GetComponentType();
  504. // TODO: Bindless texture support. For now we just return 0/do nothing.
  505. if (isBindless)
  506. {
  507. return new OperationResult(componentType, componentType switch
  508. {
  509. AggregateType.S32 => context.Constant(context.TypeS32(), 0),
  510. AggregateType.U32 => context.Constant(context.TypeU32(), 0u),
  511. _ => context.Constant(context.TypeFP32(), 0f),
  512. });
  513. }
  514. bool isArray = (texOp.Type & SamplerType.Array) != 0;
  515. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  516. int srcIndex = isBindless ? 1 : 0;
  517. SpvInstruction Src(AggregateType type)
  518. {
  519. return context.Get(type, texOp.GetSource(srcIndex++));
  520. }
  521. SpvInstruction index = null;
  522. if (isIndexed)
  523. {
  524. index = Src(AggregateType.S32);
  525. }
  526. int coordsCount = texOp.Type.GetDimensions();
  527. int pCount = coordsCount + (isArray ? 1 : 0);
  528. SpvInstruction pCoords;
  529. if (pCount > 1)
  530. {
  531. SpvInstruction[] elems = new SpvInstruction[pCount];
  532. for (int i = 0; i < pCount; i++)
  533. {
  534. elems[i] = Src(AggregateType.S32);
  535. }
  536. var vectorType = context.TypeVector(context.TypeS32(), pCount);
  537. pCoords = context.CompositeConstruct(vectorType, elems);
  538. }
  539. else
  540. {
  541. pCoords = Src(AggregateType.S32);
  542. }
  543. SpvInstruction value = Src(componentType);
  544. (var imageType, var imageVariable) = context.Images[new TextureMeta(texOp.CbufSlot, texOp.Handle, texOp.Format)];
  545. var image = context.Load(imageType, imageVariable);
  546. SpvInstruction resultType = context.GetType(componentType);
  547. SpvInstruction imagePointerType = context.TypePointer(StorageClass.Image, resultType);
  548. var pointer = context.ImageTexelPointer(imagePointerType, imageVariable, pCoords, context.Constant(context.TypeU32(), 0));
  549. var one = context.Constant(context.TypeU32(), 1);
  550. var zero = context.Constant(context.TypeU32(), 0);
  551. var result = (texOp.Flags & TextureFlags.AtomicMask) switch
  552. {
  553. TextureFlags.Add => context.AtomicIAdd(resultType, pointer, one, zero, value),
  554. TextureFlags.Minimum => componentType == AggregateType.S32
  555. ? context.AtomicSMin(resultType, pointer, one, zero, value)
  556. : context.AtomicUMin(resultType, pointer, one, zero, value),
  557. TextureFlags.Maximum => componentType == AggregateType.S32
  558. ? context.AtomicSMax(resultType, pointer, one, zero, value)
  559. : context.AtomicUMax(resultType, pointer, one, zero, value),
  560. TextureFlags.Increment => context.AtomicIIncrement(resultType, pointer, one, zero),
  561. TextureFlags.Decrement => context.AtomicIDecrement(resultType, pointer, one, zero),
  562. TextureFlags.BitwiseAnd => context.AtomicAnd(resultType, pointer, one, zero, value),
  563. TextureFlags.BitwiseOr => context.AtomicOr(resultType, pointer, one, zero, value),
  564. TextureFlags.BitwiseXor => context.AtomicXor(resultType, pointer, one, zero, value),
  565. TextureFlags.Swap => context.AtomicExchange(resultType, pointer, one, zero, value),
  566. TextureFlags.CAS => context.AtomicCompareExchange(resultType, pointer, one, zero, zero, Src(componentType), value),
  567. _ => context.AtomicIAdd(resultType, pointer, one, zero, value),
  568. };
  569. return new OperationResult(componentType, result);
  570. }
  571. private static OperationResult GenerateImageLoad(CodeGenContext context, AstOperation operation)
  572. {
  573. AstTextureOperation texOp = (AstTextureOperation)operation;
  574. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  575. var componentType = texOp.Format.GetComponentType();
  576. // TODO: Bindless texture support. For now we just return 0/do nothing.
  577. if (isBindless)
  578. {
  579. return GetZeroOperationResult(context, texOp, componentType, isVector: true);
  580. }
  581. bool isArray = (texOp.Type & SamplerType.Array) != 0;
  582. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  583. int srcIndex = isBindless ? 1 : 0;
  584. SpvInstruction Src(AggregateType type)
  585. {
  586. return context.Get(type, texOp.GetSource(srcIndex++));
  587. }
  588. SpvInstruction index = null;
  589. if (isIndexed)
  590. {
  591. index = Src(AggregateType.S32);
  592. }
  593. int coordsCount = texOp.Type.GetDimensions();
  594. int pCount = coordsCount + (isArray ? 1 : 0);
  595. SpvInstruction pCoords;
  596. if (pCount > 1)
  597. {
  598. SpvInstruction[] elems = new SpvInstruction[pCount];
  599. for (int i = 0; i < pCount; i++)
  600. {
  601. elems[i] = Src(AggregateType.S32);
  602. }
  603. var vectorType = context.TypeVector(context.TypeS32(), pCount);
  604. pCoords = context.CompositeConstruct(vectorType, elems);
  605. }
  606. else
  607. {
  608. pCoords = Src(AggregateType.S32);
  609. }
  610. pCoords = ScalingHelpers.ApplyScaling(context, texOp, pCoords, intCoords: true, isBindless, isIndexed, isArray, pCount);
  611. (var imageType, var imageVariable) = context.Images[new TextureMeta(texOp.CbufSlot, texOp.Handle, texOp.Format)];
  612. var image = context.Load(imageType, imageVariable);
  613. var imageComponentType = context.GetType(componentType);
  614. var swizzledResultType = texOp.GetVectorType(componentType);
  615. var texel = context.ImageRead(context.TypeVector(imageComponentType, 4), image, pCoords, ImageOperandsMask.MaskNone);
  616. var result = GetSwizzledResult(context, texel, swizzledResultType, texOp.Index);
  617. return new OperationResult(componentType, result);
  618. }
  619. private static OperationResult GenerateImageStore(CodeGenContext context, AstOperation operation)
  620. {
  621. AstTextureOperation texOp = (AstTextureOperation)operation;
  622. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  623. // TODO: Bindless texture support. For now we just return 0/do nothing.
  624. if (isBindless)
  625. {
  626. return OperationResult.Invalid;
  627. }
  628. bool isArray = (texOp.Type & SamplerType.Array) != 0;
  629. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  630. int srcIndex = isBindless ? 1 : 0;
  631. SpvInstruction Src(AggregateType type)
  632. {
  633. return context.Get(type, texOp.GetSource(srcIndex++));
  634. }
  635. SpvInstruction index = null;
  636. if (isIndexed)
  637. {
  638. index = Src(AggregateType.S32);
  639. }
  640. int coordsCount = texOp.Type.GetDimensions();
  641. int pCount = coordsCount + (isArray ? 1 : 0);
  642. SpvInstruction pCoords;
  643. if (pCount > 1)
  644. {
  645. SpvInstruction[] elems = new SpvInstruction[pCount];
  646. for (int i = 0; i < pCount; i++)
  647. {
  648. elems[i] = Src(AggregateType.S32);
  649. }
  650. var vectorType = context.TypeVector(context.TypeS32(), pCount);
  651. pCoords = context.CompositeConstruct(vectorType, elems);
  652. }
  653. else
  654. {
  655. pCoords = Src(AggregateType.S32);
  656. }
  657. var componentType = texOp.Format.GetComponentType();
  658. const int ComponentsCount = 4;
  659. SpvInstruction[] cElems = new SpvInstruction[ComponentsCount];
  660. for (int i = 0; i < ComponentsCount; i++)
  661. {
  662. if (srcIndex < texOp.SourcesCount)
  663. {
  664. cElems[i] = Src(componentType);
  665. }
  666. else
  667. {
  668. cElems[i] = componentType switch
  669. {
  670. AggregateType.S32 => context.Constant(context.TypeS32(), 0),
  671. AggregateType.U32 => context.Constant(context.TypeU32(), 0u),
  672. _ => context.Constant(context.TypeFP32(), 0f),
  673. };
  674. }
  675. }
  676. var texel = context.CompositeConstruct(context.TypeVector(context.GetType(componentType), ComponentsCount), cElems);
  677. (var imageType, var imageVariable) = context.Images[new TextureMeta(texOp.CbufSlot, texOp.Handle, texOp.Format)];
  678. var image = context.Load(imageType, imageVariable);
  679. context.ImageWrite(image, pCoords, texel, ImageOperandsMask.MaskNone);
  680. return OperationResult.Invalid;
  681. }
  682. private static OperationResult GenerateIsNan(CodeGenContext context, AstOperation operation)
  683. {
  684. var source = operation.GetSource(0);
  685. SpvInstruction result;
  686. if (operation.Inst.HasFlag(Instruction.FP64))
  687. {
  688. result = context.IsNan(context.TypeBool(), context.GetFP64(source));
  689. }
  690. else
  691. {
  692. result = context.IsNan(context.TypeBool(), context.GetFP32(source));
  693. }
  694. return new OperationResult(AggregateType.Bool, result);
  695. }
  696. private static OperationResult GenerateLoadAttribute(CodeGenContext context, AstOperation operation)
  697. {
  698. var src1 = operation.GetSource(0);
  699. var src2 = operation.GetSource(1);
  700. var src3 = operation.GetSource(2);
  701. if (!(src1 is AstOperand baseAttr) || baseAttr.Type != OperandType.Constant)
  702. {
  703. throw new InvalidOperationException($"First input of {nameof(Instruction.LoadAttribute)} must be a constant operand.");
  704. }
  705. var index = context.Get(AggregateType.S32, src3);
  706. var resultType = AggregateType.FP32;
  707. if (src2 is AstOperand operand && operand.Type == OperandType.Constant)
  708. {
  709. int attrOffset = (baseAttr.Value & AttributeConsts.Mask) + (operand.Value << 2);
  710. bool isOutAttr = (baseAttr.Value & AttributeConsts.LoadOutputMask) != 0;
  711. return new OperationResult(resultType, context.GetAttribute(resultType, attrOffset, isOutAttr, index));
  712. }
  713. else
  714. {
  715. var attr = context.Get(AggregateType.S32, src2);
  716. return new OperationResult(resultType, context.GetAttribute(resultType, attr, isOutAttr: false, index));
  717. }
  718. }
  719. private static OperationResult GenerateLoadConstant(CodeGenContext context, AstOperation operation)
  720. {
  721. var src1 = operation.GetSource(0);
  722. var src2 = context.Get(AggregateType.S32, operation.GetSource(1));
  723. var i1 = context.Constant(context.TypeS32(), 0);
  724. var i2 = context.ShiftRightArithmetic(context.TypeS32(), src2, context.Constant(context.TypeS32(), 2));
  725. var i3 = context.BitwiseAnd(context.TypeS32(), src2, context.Constant(context.TypeS32(), 3));
  726. SpvInstruction value = null;
  727. if (context.Config.GpuAccessor.QueryHostHasVectorIndexingBug())
  728. {
  729. // Test for each component individually.
  730. for (int i = 0; i < 4; i++)
  731. {
  732. var component = context.Constant(context.TypeS32(), i);
  733. SpvInstruction elemPointer;
  734. if (context.UniformBuffersArray != null)
  735. {
  736. var ubVariable = context.UniformBuffersArray;
  737. var i0 = context.Get(AggregateType.S32, src1);
  738. elemPointer = context.AccessChain(context.TypePointer(StorageClass.Uniform, context.TypeFP32()), ubVariable, i0, i1, i2, component);
  739. }
  740. else
  741. {
  742. var ubVariable = context.UniformBuffers[((AstOperand)src1).Value];
  743. elemPointer = context.AccessChain(context.TypePointer(StorageClass.Uniform, context.TypeFP32()), ubVariable, i1, i2, component);
  744. }
  745. SpvInstruction newValue = context.Load(context.TypeFP32(), elemPointer);
  746. value = value != null ? context.Select(context.TypeFP32(), context.IEqual(context.TypeBool(), i3, component), newValue, value) : newValue;
  747. }
  748. }
  749. else
  750. {
  751. SpvInstruction elemPointer;
  752. if (context.UniformBuffersArray != null)
  753. {
  754. var ubVariable = context.UniformBuffersArray;
  755. var i0 = context.Get(AggregateType.S32, src1);
  756. elemPointer = context.AccessChain(context.TypePointer(StorageClass.Uniform, context.TypeFP32()), ubVariable, i0, i1, i2, i3);
  757. }
  758. else
  759. {
  760. var ubVariable = context.UniformBuffers[((AstOperand)src1).Value];
  761. elemPointer = context.AccessChain(context.TypePointer(StorageClass.Uniform, context.TypeFP32()), ubVariable, i1, i2, i3);
  762. }
  763. value = context.Load(context.TypeFP32(), elemPointer);
  764. }
  765. return new OperationResult(AggregateType.FP32, value);
  766. }
  767. private static OperationResult GenerateLoadLocal(CodeGenContext context, AstOperation operation)
  768. {
  769. return GenerateLoadLocalOrShared(context, operation, StorageClass.Private, context.LocalMemory);
  770. }
  771. private static OperationResult GenerateLoadShared(CodeGenContext context, AstOperation operation)
  772. {
  773. return GenerateLoadLocalOrShared(context, operation, StorageClass.Workgroup, context.SharedMemory);
  774. }
  775. private static OperationResult GenerateLoadLocalOrShared(
  776. CodeGenContext context,
  777. AstOperation operation,
  778. StorageClass storageClass,
  779. SpvInstruction memory)
  780. {
  781. var offset = context.Get(AggregateType.S32, operation.GetSource(0));
  782. var elemPointer = context.AccessChain(context.TypePointer(storageClass, context.TypeU32()), memory, offset);
  783. var value = context.Load(context.TypeU32(), elemPointer);
  784. return new OperationResult(AggregateType.U32, value);
  785. }
  786. private static OperationResult GenerateLoadStorage(CodeGenContext context, AstOperation operation)
  787. {
  788. var elemPointer = GetStorageElemPointer(context, operation);
  789. var value = context.Load(context.TypeU32(), elemPointer);
  790. return new OperationResult(AggregateType.U32, value);
  791. }
  792. private static OperationResult GenerateLod(CodeGenContext context, AstOperation operation)
  793. {
  794. AstTextureOperation texOp = (AstTextureOperation)operation;
  795. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  796. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  797. // TODO: Bindless texture support. For now we just return 0.
  798. if (isBindless)
  799. {
  800. return new OperationResult(AggregateType.S32, context.Constant(context.TypeS32(), 0));
  801. }
  802. int srcIndex = 0;
  803. SpvInstruction Src(AggregateType type)
  804. {
  805. return context.Get(type, texOp.GetSource(srcIndex++));
  806. }
  807. SpvInstruction index = null;
  808. if (isIndexed)
  809. {
  810. index = Src(AggregateType.S32);
  811. }
  812. int pCount = texOp.Type.GetDimensions();
  813. SpvInstruction pCoords;
  814. if (pCount > 1)
  815. {
  816. SpvInstruction[] elems = new SpvInstruction[pCount];
  817. for (int i = 0; i < pCount; i++)
  818. {
  819. elems[i] = Src(AggregateType.FP32);
  820. }
  821. var vectorType = context.TypeVector(context.TypeFP32(), pCount);
  822. pCoords = context.CompositeConstruct(vectorType, elems);
  823. }
  824. else
  825. {
  826. pCoords = Src(AggregateType.FP32);
  827. }
  828. var meta = new TextureMeta(texOp.CbufSlot, texOp.Handle, texOp.Format);
  829. (_, var sampledImageType, var sampledImageVariable) = context.Samplers[meta];
  830. var image = context.Load(sampledImageType, sampledImageVariable);
  831. var resultType = context.TypeVector(context.TypeFP32(), 2);
  832. var packed = context.ImageQueryLod(resultType, image, pCoords);
  833. var result = context.CompositeExtract(context.TypeFP32(), packed, (SpvLiteralInteger)texOp.Index);
  834. return new OperationResult(AggregateType.FP32, result);
  835. }
  836. private static OperationResult GenerateLogarithmB2(CodeGenContext context, AstOperation operation)
  837. {
  838. return GenerateUnary(context, operation, context.Delegates.GlslLog2, null);
  839. }
  840. private static OperationResult GenerateLogicalAnd(CodeGenContext context, AstOperation operation)
  841. {
  842. return GenerateBinaryBool(context, operation, context.Delegates.LogicalAnd);
  843. }
  844. private static OperationResult GenerateLogicalExclusiveOr(CodeGenContext context, AstOperation operation)
  845. {
  846. return GenerateBinaryBool(context, operation, context.Delegates.LogicalNotEqual);
  847. }
  848. private static OperationResult GenerateLogicalNot(CodeGenContext context, AstOperation operation)
  849. {
  850. return GenerateUnaryBool(context, operation, context.Delegates.LogicalNot);
  851. }
  852. private static OperationResult GenerateLogicalOr(CodeGenContext context, AstOperation operation)
  853. {
  854. return GenerateBinaryBool(context, operation, context.Delegates.LogicalOr);
  855. }
  856. private static OperationResult GenerateLoopBreak(CodeGenContext context, AstOperation operation)
  857. {
  858. AstBlock loopBlock = context.CurrentBlock;
  859. while (loopBlock.Type != AstBlockType.DoWhile)
  860. {
  861. loopBlock = loopBlock.Parent;
  862. }
  863. context.Branch(context.GetNextLabel(loopBlock.Parent));
  864. return OperationResult.Invalid;
  865. }
  866. private static OperationResult GenerateLoopContinue(CodeGenContext context, AstOperation operation)
  867. {
  868. AstBlock loopBlock = context.CurrentBlock;
  869. while (loopBlock.Type != AstBlockType.DoWhile)
  870. {
  871. loopBlock = loopBlock.Parent;
  872. }
  873. (var loopTarget, var continueTarget) = context.LoopTargets[loopBlock];
  874. context.Branch(continueTarget);
  875. return OperationResult.Invalid;
  876. }
  877. private static OperationResult GenerateMaximum(CodeGenContext context, AstOperation operation)
  878. {
  879. return GenerateBinary(context, operation, context.Delegates.GlslFMax, context.Delegates.GlslSMax);
  880. }
  881. private static OperationResult GenerateMaximumU32(CodeGenContext context, AstOperation operation)
  882. {
  883. return GenerateBinaryU32(context, operation, context.Delegates.GlslUMax);
  884. }
  885. private static OperationResult GenerateMemoryBarrier(CodeGenContext context, AstOperation operation)
  886. {
  887. context.MemoryBarrier(context.Constant(context.TypeU32(), Scope.Device), context.Constant(context.TypeU32(), DefaultMemorySemantics));
  888. return OperationResult.Invalid;
  889. }
  890. private static OperationResult GenerateMinimum(CodeGenContext context, AstOperation operation)
  891. {
  892. return GenerateBinary(context, operation, context.Delegates.GlslFMin, context.Delegates.GlslSMin);
  893. }
  894. private static OperationResult GenerateMinimumU32(CodeGenContext context, AstOperation operation)
  895. {
  896. return GenerateBinaryU32(context, operation, context.Delegates.GlslUMin);
  897. }
  898. private static OperationResult GenerateMultiply(CodeGenContext context, AstOperation operation)
  899. {
  900. return GenerateBinary(context, operation, context.Delegates.FMul, context.Delegates.IMul);
  901. }
  902. private static OperationResult GenerateMultiplyHighS32(CodeGenContext context, AstOperation operation)
  903. {
  904. var src1 = operation.GetSource(0);
  905. var src2 = operation.GetSource(1);
  906. var resultType = context.TypeStruct(false, context.TypeS32(), context.TypeS32());
  907. var result = context.SMulExtended(resultType, context.GetS32(src1), context.GetS32(src2));
  908. result = context.CompositeExtract(context.TypeS32(), result, 1);
  909. return new OperationResult(AggregateType.S32, result);
  910. }
  911. private static OperationResult GenerateMultiplyHighU32(CodeGenContext context, AstOperation operation)
  912. {
  913. var src1 = operation.GetSource(0);
  914. var src2 = operation.GetSource(1);
  915. var resultType = context.TypeStruct(false, context.TypeU32(), context.TypeU32());
  916. var result = context.UMulExtended(resultType, context.GetU32(src1), context.GetU32(src2));
  917. result = context.CompositeExtract(context.TypeU32(), result, 1);
  918. return new OperationResult(AggregateType.U32, result);
  919. }
  920. private static OperationResult GenerateNegate(CodeGenContext context, AstOperation operation)
  921. {
  922. return GenerateUnary(context, operation, context.Delegates.FNegate, context.Delegates.SNegate);
  923. }
  924. private static OperationResult GeneratePackDouble2x32(CodeGenContext context, AstOperation operation)
  925. {
  926. var value0 = context.GetU32(operation.GetSource(0));
  927. var value1 = context.GetU32(operation.GetSource(1));
  928. var vector = context.CompositeConstruct(context.TypeVector(context.TypeU32(), 2), value0, value1);
  929. var result = context.GlslPackDouble2x32(context.TypeFP64(), vector);
  930. return new OperationResult(AggregateType.FP64, result);
  931. }
  932. private static OperationResult GeneratePackHalf2x16(CodeGenContext context, AstOperation operation)
  933. {
  934. var value0 = context.GetFP32(operation.GetSource(0));
  935. var value1 = context.GetFP32(operation.GetSource(1));
  936. var vector = context.CompositeConstruct(context.TypeVector(context.TypeFP32(), 2), value0, value1);
  937. var result = context.GlslPackHalf2x16(context.TypeU32(), vector);
  938. return new OperationResult(AggregateType.U32, result);
  939. }
  940. private static OperationResult GenerateReciprocalSquareRoot(CodeGenContext context, AstOperation operation)
  941. {
  942. return GenerateUnary(context, operation, context.Delegates.GlslInverseSqrt, null);
  943. }
  944. private static OperationResult GenerateReturn(CodeGenContext context, AstOperation operation)
  945. {
  946. context.Return();
  947. return OperationResult.Invalid;
  948. }
  949. private static OperationResult GenerateRound(CodeGenContext context, AstOperation operation)
  950. {
  951. return GenerateUnary(context, operation, context.Delegates.GlslRoundEven, null);
  952. }
  953. private static OperationResult GenerateShiftLeft(CodeGenContext context, AstOperation operation)
  954. {
  955. return GenerateBinaryS32(context, operation, context.Delegates.ShiftLeftLogical);
  956. }
  957. private static OperationResult GenerateShiftRightS32(CodeGenContext context, AstOperation operation)
  958. {
  959. return GenerateBinaryS32(context, operation, context.Delegates.ShiftRightArithmetic);
  960. }
  961. private static OperationResult GenerateShiftRightU32(CodeGenContext context, AstOperation operation)
  962. {
  963. return GenerateBinaryS32(context, operation, context.Delegates.ShiftRightLogical);
  964. }
  965. private static OperationResult GenerateShuffle(CodeGenContext context, AstOperation operation)
  966. {
  967. var x = context.GetFP32(operation.GetSource(0));
  968. var index = context.GetU32(operation.GetSource(1));
  969. var mask = context.GetU32(operation.GetSource(2));
  970. var const31 = context.Constant(context.TypeU32(), 31);
  971. var const8 = context.Constant(context.TypeU32(), 8);
  972. var clamp = context.BitwiseAnd(context.TypeU32(), mask, const31);
  973. var segMask = context.BitwiseAnd(context.TypeU32(), context.ShiftRightLogical(context.TypeU32(), mask, const8), const31);
  974. var notSegMask = context.Not(context.TypeU32(), segMask);
  975. var clampNotSegMask = context.BitwiseAnd(context.TypeU32(), clamp, notSegMask);
  976. var indexNotSegMask = context.BitwiseAnd(context.TypeU32(), index, notSegMask);
  977. var threadId = context.GetAttribute(AggregateType.U32, AttributeConsts.LaneId, false);
  978. var minThreadId = context.BitwiseAnd(context.TypeU32(), threadId, segMask);
  979. var maxThreadId = context.BitwiseOr(context.TypeU32(), minThreadId, clampNotSegMask);
  980. var srcThreadId = context.BitwiseOr(context.TypeU32(), indexNotSegMask, minThreadId);
  981. var valid = context.ULessThanEqual(context.TypeBool(), srcThreadId, maxThreadId);
  982. var value = context.GroupNonUniformShuffle(context.TypeFP32(), context.Constant(context.TypeU32(), (int)Scope.Subgroup), x, srcThreadId);
  983. var result = context.Select(context.TypeFP32(), valid, value, x);
  984. var validLocal = (AstOperand)operation.GetSource(3);
  985. context.Store(context.GetLocalPointer(validLocal), context.BitcastIfNeeded(validLocal.VarType, AggregateType.Bool, valid));
  986. return new OperationResult(AggregateType.FP32, result);
  987. }
  988. private static OperationResult GenerateShuffleDown(CodeGenContext context, AstOperation operation)
  989. {
  990. var x = context.GetFP32(operation.GetSource(0));
  991. var index = context.GetU32(operation.GetSource(1));
  992. var mask = context.GetU32(operation.GetSource(2));
  993. var const31 = context.Constant(context.TypeU32(), 31);
  994. var const8 = context.Constant(context.TypeU32(), 8);
  995. var clamp = context.BitwiseAnd(context.TypeU32(), mask, const31);
  996. var segMask = context.BitwiseAnd(context.TypeU32(), context.ShiftRightLogical(context.TypeU32(), mask, const8), const31);
  997. var notSegMask = context.Not(context.TypeU32(), segMask);
  998. var clampNotSegMask = context.BitwiseAnd(context.TypeU32(), clamp, notSegMask);
  999. var threadId = context.GetAttribute(AggregateType.U32, AttributeConsts.LaneId, false);
  1000. var minThreadId = context.BitwiseAnd(context.TypeU32(), threadId, segMask);
  1001. var maxThreadId = context.BitwiseOr(context.TypeU32(), minThreadId, clampNotSegMask);
  1002. var srcThreadId = context.IAdd(context.TypeU32(), threadId, index);
  1003. var valid = context.ULessThanEqual(context.TypeBool(), srcThreadId, maxThreadId);
  1004. var value = context.GroupNonUniformShuffle(context.TypeFP32(), context.Constant(context.TypeU32(), (int)Scope.Subgroup), x, srcThreadId);
  1005. var result = context.Select(context.TypeFP32(), valid, value, x);
  1006. var validLocal = (AstOperand)operation.GetSource(3);
  1007. context.Store(context.GetLocalPointer(validLocal), context.BitcastIfNeeded(validLocal.VarType, AggregateType.Bool, valid));
  1008. return new OperationResult(AggregateType.FP32, result);
  1009. }
  1010. private static OperationResult GenerateShuffleUp(CodeGenContext context, AstOperation operation)
  1011. {
  1012. var x = context.GetFP32(operation.GetSource(0));
  1013. var index = context.GetU32(operation.GetSource(1));
  1014. var mask = context.GetU32(operation.GetSource(2));
  1015. var const31 = context.Constant(context.TypeU32(), 31);
  1016. var const8 = context.Constant(context.TypeU32(), 8);
  1017. var segMask = context.BitwiseAnd(context.TypeU32(), context.ShiftRightLogical(context.TypeU32(), mask, const8), const31);
  1018. var threadId = context.GetAttribute(AggregateType.U32, AttributeConsts.LaneId, false);
  1019. var minThreadId = context.BitwiseAnd(context.TypeU32(), threadId, segMask);
  1020. var srcThreadId = context.ISub(context.TypeU32(), threadId, index);
  1021. var valid = context.SGreaterThanEqual(context.TypeBool(), srcThreadId, minThreadId);
  1022. var value = context.GroupNonUniformShuffle(context.TypeFP32(), context.Constant(context.TypeU32(), (int)Scope.Subgroup), x, srcThreadId);
  1023. var result = context.Select(context.TypeFP32(), valid, value, x);
  1024. var validLocal = (AstOperand)operation.GetSource(3);
  1025. context.Store(context.GetLocalPointer(validLocal), context.BitcastIfNeeded(validLocal.VarType, AggregateType.Bool, valid));
  1026. return new OperationResult(AggregateType.FP32, result);
  1027. }
  1028. private static OperationResult GenerateShuffleXor(CodeGenContext context, AstOperation operation)
  1029. {
  1030. var x = context.GetFP32(operation.GetSource(0));
  1031. var index = context.GetU32(operation.GetSource(1));
  1032. var mask = context.GetU32(operation.GetSource(2));
  1033. var const31 = context.Constant(context.TypeU32(), 31);
  1034. var const8 = context.Constant(context.TypeU32(), 8);
  1035. var clamp = context.BitwiseAnd(context.TypeU32(), mask, const31);
  1036. var segMask = context.BitwiseAnd(context.TypeU32(), context.ShiftRightLogical(context.TypeU32(), mask, const8), const31);
  1037. var notSegMask = context.Not(context.TypeU32(), segMask);
  1038. var clampNotSegMask = context.BitwiseAnd(context.TypeU32(), clamp, notSegMask);
  1039. var threadId = context.GetAttribute(AggregateType.U32, AttributeConsts.LaneId, false);
  1040. var minThreadId = context.BitwiseAnd(context.TypeU32(), threadId, segMask);
  1041. var maxThreadId = context.BitwiseOr(context.TypeU32(), minThreadId, clampNotSegMask);
  1042. var srcThreadId = context.BitwiseXor(context.TypeU32(), threadId, index);
  1043. var valid = context.ULessThanEqual(context.TypeBool(), srcThreadId, maxThreadId);
  1044. var value = context.GroupNonUniformShuffle(context.TypeFP32(), context.Constant(context.TypeU32(), (int)Scope.Subgroup), x, srcThreadId);
  1045. var result = context.Select(context.TypeFP32(), valid, value, x);
  1046. var validLocal = (AstOperand)operation.GetSource(3);
  1047. context.Store(context.GetLocalPointer(validLocal), context.BitcastIfNeeded(validLocal.VarType, AggregateType.Bool, valid));
  1048. return new OperationResult(AggregateType.FP32, result);
  1049. }
  1050. private static OperationResult GenerateSine(CodeGenContext context, AstOperation operation)
  1051. {
  1052. return GenerateUnary(context, operation, context.Delegates.GlslSin, null);
  1053. }
  1054. private static OperationResult GenerateSquareRoot(CodeGenContext context, AstOperation operation)
  1055. {
  1056. return GenerateUnary(context, operation, context.Delegates.GlslSqrt, null);
  1057. }
  1058. private static OperationResult GenerateStoreAttribute(CodeGenContext context, AstOperation operation)
  1059. {
  1060. var src1 = operation.GetSource(0);
  1061. var src2 = operation.GetSource(1);
  1062. var src3 = operation.GetSource(2);
  1063. if (!(src1 is AstOperand baseAttr) || baseAttr.Type != OperandType.Constant)
  1064. {
  1065. throw new InvalidOperationException($"First input of {nameof(Instruction.StoreAttribute)} must be a constant operand.");
  1066. }
  1067. SpvInstruction elemPointer;
  1068. AggregateType elemType;
  1069. if (src2 is AstOperand operand && operand.Type == OperandType.Constant)
  1070. {
  1071. int attrOffset = (baseAttr.Value & AttributeConsts.Mask) + (operand.Value << 2);
  1072. elemPointer = context.GetAttributeElemPointer(attrOffset, isOutAttr: true, index: null, out elemType);
  1073. }
  1074. else
  1075. {
  1076. var attr = context.Get(AggregateType.S32, src2);
  1077. elemPointer = context.GetAttributeElemPointer(attr, isOutAttr: true, index: null, out elemType);
  1078. }
  1079. var value = context.Get(elemType, src3);
  1080. context.Store(elemPointer, value);
  1081. return OperationResult.Invalid;
  1082. }
  1083. private static OperationResult GenerateStoreLocal(CodeGenContext context, AstOperation operation)
  1084. {
  1085. return GenerateStoreLocalOrShared(context, operation, StorageClass.Private, context.LocalMemory);
  1086. }
  1087. private static OperationResult GenerateStoreShared(CodeGenContext context, AstOperation operation)
  1088. {
  1089. return GenerateStoreLocalOrShared(context, operation, StorageClass.Workgroup, context.SharedMemory);
  1090. }
  1091. private static OperationResult GenerateStoreLocalOrShared(
  1092. CodeGenContext context,
  1093. AstOperation operation,
  1094. StorageClass storageClass,
  1095. SpvInstruction memory)
  1096. {
  1097. var offset = context.Get(AggregateType.S32, operation.GetSource(0));
  1098. var value = context.Get(AggregateType.U32, operation.GetSource(1));
  1099. var elemPointer = context.AccessChain(context.TypePointer(storageClass, context.TypeU32()), memory, offset);
  1100. context.Store(elemPointer, value);
  1101. return OperationResult.Invalid;
  1102. }
  1103. private static OperationResult GenerateStoreShared16(CodeGenContext context, AstOperation operation)
  1104. {
  1105. GenerateStoreSharedSmallInt(context, operation, 16);
  1106. return OperationResult.Invalid;
  1107. }
  1108. private static OperationResult GenerateStoreShared8(CodeGenContext context, AstOperation operation)
  1109. {
  1110. GenerateStoreSharedSmallInt(context, operation, 8);
  1111. return OperationResult.Invalid;
  1112. }
  1113. private static OperationResult GenerateStoreStorage(CodeGenContext context, AstOperation operation)
  1114. {
  1115. var elemPointer = GetStorageElemPointer(context, operation);
  1116. context.Store(elemPointer, context.Get(AggregateType.U32, operation.GetSource(2)));
  1117. return OperationResult.Invalid;
  1118. }
  1119. private static OperationResult GenerateStoreStorage16(CodeGenContext context, AstOperation operation)
  1120. {
  1121. GenerateStoreStorageSmallInt(context, operation, 16);
  1122. return OperationResult.Invalid;
  1123. }
  1124. private static OperationResult GenerateStoreStorage8(CodeGenContext context, AstOperation operation)
  1125. {
  1126. GenerateStoreStorageSmallInt(context, operation, 8);
  1127. return OperationResult.Invalid;
  1128. }
  1129. private static OperationResult GenerateSubtract(CodeGenContext context, AstOperation operation)
  1130. {
  1131. return GenerateBinary(context, operation, context.Delegates.FSub, context.Delegates.ISub);
  1132. }
  1133. private static OperationResult GenerateSwizzleAdd(CodeGenContext context, AstOperation operation)
  1134. {
  1135. var x = context.Get(AggregateType.FP32, operation.GetSource(0));
  1136. var y = context.Get(AggregateType.FP32, operation.GetSource(1));
  1137. var mask = context.Get(AggregateType.U32, operation.GetSource(2));
  1138. var v4float = context.TypeVector(context.TypeFP32(), 4);
  1139. var one = context.Constant(context.TypeFP32(), 1.0f);
  1140. var minusOne = context.Constant(context.TypeFP32(), -1.0f);
  1141. var zero = context.Constant(context.TypeFP32(), 0.0f);
  1142. var xLut = context.ConstantComposite(v4float, one, minusOne, one, zero);
  1143. var yLut = context.ConstantComposite(v4float, one, one, minusOne, one);
  1144. var three = context.Constant(context.TypeU32(), 3);
  1145. var threadId = context.GetAttribute(AggregateType.U32, AttributeConsts.LaneId, false);
  1146. var shift = context.BitwiseAnd(context.TypeU32(), threadId, three);
  1147. shift = context.ShiftLeftLogical(context.TypeU32(), shift, context.Constant(context.TypeU32(), 1));
  1148. var lutIdx = context.ShiftRightLogical(context.TypeU32(), mask, shift);
  1149. lutIdx = context.BitwiseAnd(context.TypeU32(), lutIdx, three);
  1150. var xLutValue = context.VectorExtractDynamic(context.TypeFP32(), xLut, lutIdx);
  1151. var yLutValue = context.VectorExtractDynamic(context.TypeFP32(), yLut, lutIdx);
  1152. var xResult = context.FMul(context.TypeFP32(), x, xLutValue);
  1153. var yResult = context.FMul(context.TypeFP32(), y, yLutValue);
  1154. var result = context.FAdd(context.TypeFP32(), xResult, yResult);
  1155. return new OperationResult(AggregateType.FP32, result);
  1156. }
  1157. private static OperationResult GenerateTextureSample(CodeGenContext context, AstOperation operation)
  1158. {
  1159. AstTextureOperation texOp = (AstTextureOperation)operation;
  1160. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  1161. bool isGather = (texOp.Flags & TextureFlags.Gather) != 0;
  1162. bool hasDerivatives = (texOp.Flags & TextureFlags.Derivatives) != 0;
  1163. bool intCoords = (texOp.Flags & TextureFlags.IntCoords) != 0;
  1164. bool hasLodBias = (texOp.Flags & TextureFlags.LodBias) != 0;
  1165. bool hasLodLevel = (texOp.Flags & TextureFlags.LodLevel) != 0;
  1166. bool hasOffset = (texOp.Flags & TextureFlags.Offset) != 0;
  1167. bool hasOffsets = (texOp.Flags & TextureFlags.Offsets) != 0;
  1168. bool isArray = (texOp.Type & SamplerType.Array) != 0;
  1169. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  1170. bool isMultisample = (texOp.Type & SamplerType.Multisample) != 0;
  1171. bool isShadow = (texOp.Type & SamplerType.Shadow) != 0;
  1172. bool colorIsVector = isGather || !isShadow;
  1173. // TODO: Bindless texture support. For now we just return 0.
  1174. if (isBindless)
  1175. {
  1176. return GetZeroOperationResult(context, texOp, AggregateType.FP32, colorIsVector);
  1177. }
  1178. // This combination is valid, but not available on GLSL.
  1179. // For now, ignore the LOD level and do a normal sample.
  1180. // TODO: How to implement it properly?
  1181. if (hasLodLevel && isArray && isShadow)
  1182. {
  1183. hasLodLevel = false;
  1184. }
  1185. int srcIndex = isBindless ? 1 : 0;
  1186. SpvInstruction Src(AggregateType type)
  1187. {
  1188. return context.Get(type, texOp.GetSource(srcIndex++));
  1189. }
  1190. SpvInstruction index = null;
  1191. if (isIndexed)
  1192. {
  1193. index = Src(AggregateType.S32);
  1194. }
  1195. int coordsCount = texOp.Type.GetDimensions();
  1196. int pCount = coordsCount;
  1197. int arrayIndexElem = -1;
  1198. if (isArray)
  1199. {
  1200. arrayIndexElem = pCount++;
  1201. }
  1202. AggregateType coordType = intCoords ? AggregateType.S32 : AggregateType.FP32;
  1203. SpvInstruction AssemblePVector(int count)
  1204. {
  1205. if (count > 1)
  1206. {
  1207. SpvInstruction[] elems = new SpvInstruction[count];
  1208. for (int index = 0; index < count; index++)
  1209. {
  1210. if (arrayIndexElem == index)
  1211. {
  1212. elems[index] = Src(AggregateType.S32);
  1213. if (!intCoords)
  1214. {
  1215. elems[index] = context.ConvertSToF(context.TypeFP32(), elems[index]);
  1216. }
  1217. }
  1218. else
  1219. {
  1220. elems[index] = Src(coordType);
  1221. }
  1222. }
  1223. var vectorType = context.TypeVector(intCoords ? context.TypeS32() : context.TypeFP32(), count);
  1224. return context.CompositeConstruct(vectorType, elems);
  1225. }
  1226. else
  1227. {
  1228. return Src(coordType);
  1229. }
  1230. }
  1231. SpvInstruction ApplyBias(SpvInstruction vector, SpvInstruction image)
  1232. {
  1233. int gatherBiasPrecision = context.Config.GpuAccessor.QueryHostGatherBiasPrecision();
  1234. if (isGather && gatherBiasPrecision != 0)
  1235. {
  1236. // GPU requires texture gather to be slightly offset to match NVIDIA behaviour when point is exactly between two texels.
  1237. // Offset by the gather precision divided by 2 to correct for rounding.
  1238. var sizeType = pCount == 1 ? context.TypeS32() : context.TypeVector(context.TypeS32(), pCount);
  1239. var pVectorType = pCount == 1 ? context.TypeFP32() : context.TypeVector(context.TypeFP32(), pCount);
  1240. var bias = context.Constant(context.TypeFP32(), (float)(1 << (gatherBiasPrecision + 1)));
  1241. var biasVector = context.CompositeConstruct(pVectorType, Enumerable.Repeat(bias, pCount).ToArray());
  1242. var one = context.Constant(context.TypeFP32(), 1f);
  1243. var oneVector = context.CompositeConstruct(pVectorType, Enumerable.Repeat(one, pCount).ToArray());
  1244. var divisor = context.FMul(
  1245. pVectorType,
  1246. context.ConvertSToF(pVectorType, context.ImageQuerySize(sizeType, image)),
  1247. biasVector);
  1248. vector = context.FAdd(pVectorType, vector, context.FDiv(pVectorType, oneVector, divisor));
  1249. }
  1250. return vector;
  1251. }
  1252. SpvInstruction pCoords = AssemblePVector(pCount);
  1253. pCoords = ScalingHelpers.ApplyScaling(context, texOp, pCoords, intCoords, isBindless, isIndexed, isArray, pCount);
  1254. SpvInstruction AssembleDerivativesVector(int count)
  1255. {
  1256. if (count > 1)
  1257. {
  1258. SpvInstruction[] elems = new SpvInstruction[count];
  1259. for (int index = 0; index < count; index++)
  1260. {
  1261. elems[index] = Src(AggregateType.FP32);
  1262. }
  1263. var vectorType = context.TypeVector(context.TypeFP32(), count);
  1264. return context.CompositeConstruct(vectorType, elems);
  1265. }
  1266. else
  1267. {
  1268. return Src(AggregateType.FP32);
  1269. }
  1270. }
  1271. SpvInstruction dRef = null;
  1272. if (isShadow)
  1273. {
  1274. dRef = Src(AggregateType.FP32);
  1275. }
  1276. SpvInstruction[] derivatives = null;
  1277. if (hasDerivatives)
  1278. {
  1279. derivatives = new[]
  1280. {
  1281. AssembleDerivativesVector(coordsCount), // dPdx
  1282. AssembleDerivativesVector(coordsCount) // dPdy
  1283. };
  1284. }
  1285. SpvInstruction sample = null;
  1286. SpvInstruction lod = null;
  1287. if (isMultisample)
  1288. {
  1289. sample = Src(AggregateType.S32);
  1290. }
  1291. else if (hasLodLevel)
  1292. {
  1293. lod = Src(coordType);
  1294. }
  1295. SpvInstruction AssembleOffsetVector(int count)
  1296. {
  1297. if (count > 1)
  1298. {
  1299. SpvInstruction[] elems = new SpvInstruction[count];
  1300. for (int index = 0; index < count; index++)
  1301. {
  1302. elems[index] = Src(AggregateType.S32);
  1303. }
  1304. var vectorType = context.TypeVector(context.TypeS32(), count);
  1305. return context.ConstantComposite(vectorType, elems);
  1306. }
  1307. else
  1308. {
  1309. return Src(AggregateType.S32);
  1310. }
  1311. }
  1312. SpvInstruction[] offsets = null;
  1313. if (hasOffset)
  1314. {
  1315. offsets = new[] { AssembleOffsetVector(coordsCount) };
  1316. }
  1317. else if (hasOffsets)
  1318. {
  1319. offsets = new[]
  1320. {
  1321. AssembleOffsetVector(coordsCount),
  1322. AssembleOffsetVector(coordsCount),
  1323. AssembleOffsetVector(coordsCount),
  1324. AssembleOffsetVector(coordsCount)
  1325. };
  1326. }
  1327. SpvInstruction lodBias = null;
  1328. if (hasLodBias)
  1329. {
  1330. lodBias = Src(AggregateType.FP32);
  1331. }
  1332. SpvInstruction compIdx = null;
  1333. // textureGather* optional extra component index,
  1334. // not needed for shadow samplers.
  1335. if (isGather && !isShadow)
  1336. {
  1337. compIdx = Src(AggregateType.S32);
  1338. }
  1339. var operandsList = new List<SpvInstruction>();
  1340. var operandsMask = ImageOperandsMask.MaskNone;
  1341. if (hasLodBias)
  1342. {
  1343. operandsMask |= ImageOperandsMask.Bias;
  1344. operandsList.Add(lodBias);
  1345. }
  1346. if (!isMultisample && hasLodLevel)
  1347. {
  1348. operandsMask |= ImageOperandsMask.Lod;
  1349. operandsList.Add(lod);
  1350. }
  1351. if (hasDerivatives)
  1352. {
  1353. operandsMask |= ImageOperandsMask.Grad;
  1354. operandsList.Add(derivatives[0]);
  1355. operandsList.Add(derivatives[1]);
  1356. }
  1357. if (hasOffset)
  1358. {
  1359. operandsMask |= ImageOperandsMask.ConstOffset;
  1360. operandsList.Add(offsets[0]);
  1361. }
  1362. else if (hasOffsets)
  1363. {
  1364. operandsMask |= ImageOperandsMask.ConstOffsets;
  1365. SpvInstruction arrayv2 = context.TypeArray(context.TypeVector(context.TypeS32(), 2), context.Constant(context.TypeU32(), 4));
  1366. operandsList.Add(context.ConstantComposite(arrayv2, offsets[0], offsets[1], offsets[2], offsets[3]));
  1367. }
  1368. if (isMultisample)
  1369. {
  1370. operandsMask |= ImageOperandsMask.Sample;
  1371. operandsList.Add(sample);
  1372. }
  1373. var resultType = colorIsVector ? context.TypeVector(context.TypeFP32(), 4) : context.TypeFP32();
  1374. var meta = new TextureMeta(texOp.CbufSlot, texOp.Handle, texOp.Format);
  1375. (var imageType, var sampledImageType, var sampledImageVariable) = context.Samplers[meta];
  1376. var image = context.Load(sampledImageType, sampledImageVariable);
  1377. if (intCoords)
  1378. {
  1379. image = context.Image(imageType, image);
  1380. }
  1381. pCoords = ApplyBias(pCoords, image);
  1382. var operands = operandsList.ToArray();
  1383. SpvInstruction result;
  1384. if (intCoords)
  1385. {
  1386. result = context.ImageFetch(resultType, image, pCoords, operandsMask, operands);
  1387. }
  1388. else if (isGather)
  1389. {
  1390. if (isShadow)
  1391. {
  1392. result = context.ImageDrefGather(resultType, image, pCoords, dRef, operandsMask, operands);
  1393. }
  1394. else
  1395. {
  1396. result = context.ImageGather(resultType, image, pCoords, compIdx, operandsMask, operands);
  1397. }
  1398. }
  1399. else if (isShadow)
  1400. {
  1401. if (hasLodLevel)
  1402. {
  1403. result = context.ImageSampleDrefExplicitLod(resultType, image, pCoords, dRef, operandsMask, operands);
  1404. }
  1405. else
  1406. {
  1407. result = context.ImageSampleDrefImplicitLod(resultType, image, pCoords, dRef, operandsMask, operands);
  1408. }
  1409. }
  1410. else if (hasDerivatives || hasLodLevel)
  1411. {
  1412. result = context.ImageSampleExplicitLod(resultType, image, pCoords, operandsMask, operands);
  1413. }
  1414. else
  1415. {
  1416. result = context.ImageSampleImplicitLod(resultType, image, pCoords, operandsMask, operands);
  1417. }
  1418. var swizzledResultType = AggregateType.FP32;
  1419. if (colorIsVector)
  1420. {
  1421. swizzledResultType = texOp.GetVectorType(swizzledResultType);
  1422. result = GetSwizzledResult(context, result, swizzledResultType, texOp.Index);
  1423. }
  1424. return new OperationResult(swizzledResultType, result);
  1425. }
  1426. private static OperationResult GenerateTextureSize(CodeGenContext context, AstOperation operation)
  1427. {
  1428. AstTextureOperation texOp = (AstTextureOperation)operation;
  1429. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  1430. // TODO: Bindless texture support. For now we just return 0.
  1431. if (isBindless)
  1432. {
  1433. return new OperationResult(AggregateType.S32, context.Constant(context.TypeS32(), 0));
  1434. }
  1435. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  1436. SpvInstruction index = null;
  1437. if (isIndexed)
  1438. {
  1439. index = context.GetS32(texOp.GetSource(0));
  1440. }
  1441. var meta = new TextureMeta(texOp.CbufSlot, texOp.Handle, texOp.Format);
  1442. (var imageType, var sampledImageType, var sampledImageVariable) = context.Samplers[meta];
  1443. var image = context.Load(sampledImageType, sampledImageVariable);
  1444. image = context.Image(imageType, image);
  1445. if (texOp.Index == 3)
  1446. {
  1447. return new OperationResult(AggregateType.S32, context.ImageQueryLevels(context.TypeS32(), image));
  1448. }
  1449. else
  1450. {
  1451. var type = context.SamplersTypes[meta];
  1452. bool hasLod = !type.HasFlag(SamplerType.Multisample) && type != SamplerType.TextureBuffer;
  1453. int dimensions = (type & SamplerType.Mask) == SamplerType.TextureCube ? 2 : type.GetDimensions();
  1454. if (type.HasFlag(SamplerType.Array))
  1455. {
  1456. dimensions++;
  1457. }
  1458. var resultType = dimensions == 1 ? context.TypeS32() : context.TypeVector(context.TypeS32(), dimensions);
  1459. SpvInstruction result;
  1460. if (hasLod)
  1461. {
  1462. int lodSrcIndex = isBindless || isIndexed ? 1 : 0;
  1463. var lod = context.GetS32(operation.GetSource(lodSrcIndex));
  1464. result = context.ImageQuerySizeLod(resultType, image, lod);
  1465. }
  1466. else
  1467. {
  1468. result = context.ImageQuerySize(resultType, image);
  1469. }
  1470. if (dimensions != 1)
  1471. {
  1472. result = context.CompositeExtract(context.TypeS32(), result, (SpvLiteralInteger)texOp.Index);
  1473. }
  1474. if (texOp.Index < 2 || (type & SamplerType.Mask) == SamplerType.Texture3D)
  1475. {
  1476. result = ScalingHelpers.ApplyUnscaling(context, texOp.WithType(type), result, isBindless, isIndexed);
  1477. }
  1478. return new OperationResult(AggregateType.S32, result);
  1479. }
  1480. }
  1481. private static OperationResult GenerateTruncate(CodeGenContext context, AstOperation operation)
  1482. {
  1483. return GenerateUnary(context, operation, context.Delegates.GlslTrunc, null);
  1484. }
  1485. private static OperationResult GenerateUnpackDouble2x32(CodeGenContext context, AstOperation operation)
  1486. {
  1487. var value = context.GetFP64(operation.GetSource(0));
  1488. var vector = context.GlslUnpackDouble2x32(context.TypeVector(context.TypeU32(), 2), value);
  1489. var result = context.CompositeExtract(context.TypeU32(), vector, operation.Index);
  1490. return new OperationResult(AggregateType.U32, result);
  1491. }
  1492. private static OperationResult GenerateUnpackHalf2x16(CodeGenContext context, AstOperation operation)
  1493. {
  1494. var value = context.GetU32(operation.GetSource(0));
  1495. var vector = context.GlslUnpackHalf2x16(context.TypeVector(context.TypeFP32(), 2), value);
  1496. var result = context.CompositeExtract(context.TypeFP32(), vector, operation.Index);
  1497. return new OperationResult(AggregateType.FP32, result);
  1498. }
  1499. private static OperationResult GenerateVectorExtract(CodeGenContext context, AstOperation operation)
  1500. {
  1501. var vector = context.GetWithType(operation.GetSource(0), out AggregateType vectorType);
  1502. var scalarType = vectorType & ~AggregateType.ElementCountMask;
  1503. var resultType = context.GetType(scalarType);
  1504. SpvInstruction result;
  1505. if (operation.GetSource(1) is AstOperand indexOperand && indexOperand.Type == OperandType.Constant)
  1506. {
  1507. result = context.CompositeExtract(resultType, vector, (SpvLiteralInteger)indexOperand.Value);
  1508. }
  1509. else
  1510. {
  1511. var index = context.Get(AggregateType.S32, operation.GetSource(1));
  1512. result = context.VectorExtractDynamic(resultType, vector, index);
  1513. }
  1514. return new OperationResult(scalarType, result);
  1515. }
  1516. private static OperationResult GenerateVoteAll(CodeGenContext context, AstOperation operation)
  1517. {
  1518. var execution = context.Constant(context.TypeU32(), Scope.Subgroup);
  1519. var result = context.GroupNonUniformAll(context.TypeBool(), execution, context.Get(AggregateType.Bool, operation.GetSource(0)));
  1520. return new OperationResult(AggregateType.Bool, result);
  1521. }
  1522. private static OperationResult GenerateVoteAllEqual(CodeGenContext context, AstOperation operation)
  1523. {
  1524. var execution = context.Constant(context.TypeU32(), Scope.Subgroup);
  1525. var result = context.GroupNonUniformAllEqual(context.TypeBool(), execution, context.Get(AggregateType.Bool, operation.GetSource(0)));
  1526. return new OperationResult(AggregateType.Bool, result);
  1527. }
  1528. private static OperationResult GenerateVoteAny(CodeGenContext context, AstOperation operation)
  1529. {
  1530. var execution = context.Constant(context.TypeU32(), Scope.Subgroup);
  1531. var result = context.GroupNonUniformAny(context.TypeBool(), execution, context.Get(AggregateType.Bool, operation.GetSource(0)));
  1532. return new OperationResult(AggregateType.Bool, result);
  1533. }
  1534. private static OperationResult GenerateCompare(
  1535. CodeGenContext context,
  1536. AstOperation operation,
  1537. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitF,
  1538. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitI)
  1539. {
  1540. var src1 = operation.GetSource(0);
  1541. var src2 = operation.GetSource(1);
  1542. SpvInstruction result;
  1543. if (operation.Inst.HasFlag(Instruction.FP64))
  1544. {
  1545. result = emitF(context.TypeBool(), context.GetFP64(src1), context.GetFP64(src2));
  1546. }
  1547. else if (operation.Inst.HasFlag(Instruction.FP32))
  1548. {
  1549. result = emitF(context.TypeBool(), context.GetFP32(src1), context.GetFP32(src2));
  1550. }
  1551. else
  1552. {
  1553. result = emitI(context.TypeBool(), context.GetS32(src1), context.GetS32(src2));
  1554. }
  1555. return new OperationResult(AggregateType.Bool, result);
  1556. }
  1557. private static OperationResult GenerateCompareU32(
  1558. CodeGenContext context,
  1559. AstOperation operation,
  1560. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitU)
  1561. {
  1562. var src1 = operation.GetSource(0);
  1563. var src2 = operation.GetSource(1);
  1564. var result = emitU(context.TypeBool(), context.GetU32(src1), context.GetU32(src2));
  1565. return new OperationResult(AggregateType.Bool, result);
  1566. }
  1567. private static OperationResult GenerateAtomicMemoryBinary(
  1568. CodeGenContext context,
  1569. AstOperation operation,
  1570. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitU)
  1571. {
  1572. var value = context.GetU32(operation.GetSource(2));
  1573. SpvInstruction elemPointer;
  1574. Instruction mr = operation.Inst & Instruction.MrMask;
  1575. if (mr == Instruction.MrStorage)
  1576. {
  1577. elemPointer = GetStorageElemPointer(context, operation);
  1578. }
  1579. else if (mr == Instruction.MrShared)
  1580. {
  1581. var offset = context.GetU32(operation.GetSource(0));
  1582. elemPointer = context.AccessChain(context.TypePointer(StorageClass.Workgroup, context.TypeU32()), context.SharedMemory, offset);
  1583. }
  1584. else
  1585. {
  1586. throw new InvalidOperationException($"Invalid storage class \"{mr}\".");
  1587. }
  1588. var one = context.Constant(context.TypeU32(), 1);
  1589. var zero = context.Constant(context.TypeU32(), 0);
  1590. return new OperationResult(AggregateType.U32, emitU(context.TypeU32(), elemPointer, one, zero, value));
  1591. }
  1592. private static OperationResult GenerateAtomicMemoryCas(CodeGenContext context, AstOperation operation)
  1593. {
  1594. var value0 = context.GetU32(operation.GetSource(2));
  1595. var value1 = context.GetU32(operation.GetSource(3));
  1596. SpvInstruction elemPointer;
  1597. Instruction mr = operation.Inst & Instruction.MrMask;
  1598. if (mr == Instruction.MrStorage)
  1599. {
  1600. elemPointer = GetStorageElemPointer(context, operation);
  1601. }
  1602. else if (mr == Instruction.MrShared)
  1603. {
  1604. var offset = context.GetU32(operation.GetSource(0));
  1605. elemPointer = context.AccessChain(context.TypePointer(StorageClass.Workgroup, context.TypeU32()), context.SharedMemory, offset);
  1606. }
  1607. else
  1608. {
  1609. throw new InvalidOperationException($"Invalid storage class \"{mr}\".");
  1610. }
  1611. var one = context.Constant(context.TypeU32(), 1);
  1612. var zero = context.Constant(context.TypeU32(), 0);
  1613. return new OperationResult(AggregateType.U32, context.AtomicCompareExchange(context.TypeU32(), elemPointer, one, zero, zero, value1, value0));
  1614. }
  1615. private static void GenerateStoreSharedSmallInt(CodeGenContext context, AstOperation operation, int bitSize)
  1616. {
  1617. var offset = context.Get(AggregateType.U32, operation.GetSource(0));
  1618. var value = context.Get(AggregateType.U32, operation.GetSource(1));
  1619. var wordOffset = context.ShiftRightLogical(context.TypeU32(), offset, context.Constant(context.TypeU32(), 2));
  1620. var bitOffset = context.BitwiseAnd(context.TypeU32(), offset, context.Constant(context.TypeU32(), 3));
  1621. bitOffset = context.ShiftLeftLogical(context.TypeU32(), bitOffset, context.Constant(context.TypeU32(), 3));
  1622. var memory = context.SharedMemory;
  1623. var elemPointer = context.AccessChain(context.TypePointer(StorageClass.Workgroup, context.TypeU32()), memory, wordOffset);
  1624. GenerateStoreSmallInt(context, elemPointer, bitOffset, value, bitSize);
  1625. }
  1626. private static void GenerateStoreStorageSmallInt(CodeGenContext context, AstOperation operation, int bitSize)
  1627. {
  1628. var i0 = context.Get(AggregateType.S32, operation.GetSource(0));
  1629. var offset = context.Get(AggregateType.U32, operation.GetSource(1));
  1630. var value = context.Get(AggregateType.U32, operation.GetSource(2));
  1631. var wordOffset = context.ShiftRightLogical(context.TypeU32(), offset, context.Constant(context.TypeU32(), 2));
  1632. var bitOffset = context.BitwiseAnd(context.TypeU32(), offset, context.Constant(context.TypeU32(), 3));
  1633. bitOffset = context.ShiftLeftLogical(context.TypeU32(), bitOffset, context.Constant(context.TypeU32(), 3));
  1634. var sbVariable = context.StorageBuffersArray;
  1635. var i1 = context.Constant(context.TypeS32(), 0);
  1636. var elemPointer = context.AccessChain(context.TypePointer(StorageClass.Uniform, context.TypeU32()), sbVariable, i0, i1, wordOffset);
  1637. GenerateStoreSmallInt(context, elemPointer, bitOffset, value, bitSize);
  1638. }
  1639. private static void GenerateStoreSmallInt(
  1640. CodeGenContext context,
  1641. SpvInstruction elemPointer,
  1642. SpvInstruction bitOffset,
  1643. SpvInstruction value,
  1644. int bitSize)
  1645. {
  1646. var loopStart = context.Label();
  1647. var loopEnd = context.Label();
  1648. context.Branch(loopStart);
  1649. context.AddLabel(loopStart);
  1650. var oldValue = context.Load(context.TypeU32(), elemPointer);
  1651. var newValue = context.BitFieldInsert(context.TypeU32(), oldValue, value, bitOffset, context.Constant(context.TypeU32(), bitSize));
  1652. var one = context.Constant(context.TypeU32(), 1);
  1653. var zero = context.Constant(context.TypeU32(), 0);
  1654. var result = context.AtomicCompareExchange(context.TypeU32(), elemPointer, one, zero, zero, newValue, oldValue);
  1655. var failed = context.INotEqual(context.TypeBool(), result, oldValue);
  1656. context.LoopMerge(loopEnd, loopStart, LoopControlMask.MaskNone);
  1657. context.BranchConditional(failed, loopStart, loopEnd);
  1658. context.AddLabel(loopEnd);
  1659. }
  1660. private static OperationResult GetZeroOperationResult(
  1661. CodeGenContext context,
  1662. AstTextureOperation texOp,
  1663. AggregateType scalarType,
  1664. bool isVector)
  1665. {
  1666. var zero = scalarType switch
  1667. {
  1668. AggregateType.S32 => context.Constant(context.TypeS32(), 0),
  1669. AggregateType.U32 => context.Constant(context.TypeU32(), 0u),
  1670. _ => context.Constant(context.TypeFP32(), 0f),
  1671. };
  1672. if (isVector)
  1673. {
  1674. AggregateType outputType = texOp.GetVectorType(scalarType);
  1675. if ((outputType & AggregateType.ElementCountMask) != 0)
  1676. {
  1677. int componentsCount = BitOperations.PopCount((uint)texOp.Index);
  1678. SpvInstruction[] values = new SpvInstruction[componentsCount];
  1679. values.AsSpan().Fill(zero);
  1680. return new OperationResult(outputType, context.ConstantComposite(context.GetType(outputType), values));
  1681. }
  1682. }
  1683. return new OperationResult(scalarType, zero);
  1684. }
  1685. private static SpvInstruction GetSwizzledResult(CodeGenContext context, SpvInstruction vector, AggregateType swizzledResultType, int mask)
  1686. {
  1687. if ((swizzledResultType & AggregateType.ElementCountMask) != 0)
  1688. {
  1689. SpvLiteralInteger[] components = new SpvLiteralInteger[BitOperations.PopCount((uint)mask)];
  1690. int componentIndex = 0;
  1691. for (int i = 0; i < 4; i++)
  1692. {
  1693. if ((mask & (1 << i)) != 0)
  1694. {
  1695. components[componentIndex++] = i;
  1696. }
  1697. }
  1698. return context.VectorShuffle(context.GetType(swizzledResultType), vector, vector, components);
  1699. }
  1700. else
  1701. {
  1702. int componentIndex = (int)BitOperations.TrailingZeroCount(mask);
  1703. return context.CompositeExtract(context.GetType(swizzledResultType), vector, (SpvLiteralInteger)componentIndex);
  1704. }
  1705. }
  1706. private static SpvInstruction GetStorageElemPointer(CodeGenContext context, AstOperation operation)
  1707. {
  1708. var sbVariable = context.StorageBuffersArray;
  1709. var i0 = context.Get(AggregateType.S32, operation.GetSource(0));
  1710. var i1 = context.Constant(context.TypeS32(), 0);
  1711. var i2 = context.Get(AggregateType.S32, operation.GetSource(1));
  1712. return context.AccessChain(context.TypePointer(StorageClass.Uniform, context.TypeU32()), sbVariable, i0, i1, i2);
  1713. }
  1714. private static OperationResult GenerateUnary(
  1715. CodeGenContext context,
  1716. AstOperation operation,
  1717. Func<SpvInstruction, SpvInstruction, SpvInstruction> emitF,
  1718. Func<SpvInstruction, SpvInstruction, SpvInstruction> emitI)
  1719. {
  1720. var source = operation.GetSource(0);
  1721. if (operation.Inst.HasFlag(Instruction.FP64))
  1722. {
  1723. return new OperationResult(AggregateType.FP64, emitF(context.TypeFP64(), context.GetFP64(source)));
  1724. }
  1725. else if (operation.Inst.HasFlag(Instruction.FP32))
  1726. {
  1727. return new OperationResult(AggregateType.FP32, emitF(context.TypeFP32(), context.GetFP32(source)));
  1728. }
  1729. else
  1730. {
  1731. return new OperationResult(AggregateType.S32, emitI(context.TypeS32(), context.GetS32(source)));
  1732. }
  1733. }
  1734. private static OperationResult GenerateUnaryBool(
  1735. CodeGenContext context,
  1736. AstOperation operation,
  1737. Func<SpvInstruction, SpvInstruction, SpvInstruction> emitB)
  1738. {
  1739. var source = operation.GetSource(0);
  1740. return new OperationResult(AggregateType.Bool, emitB(context.TypeBool(), context.Get(AggregateType.Bool, source)));
  1741. }
  1742. private static OperationResult GenerateUnaryFP32(
  1743. CodeGenContext context,
  1744. AstOperation operation,
  1745. Func<SpvInstruction, SpvInstruction, SpvInstruction> emit)
  1746. {
  1747. var source = operation.GetSource(0);
  1748. return new OperationResult(AggregateType.FP32, emit(context.TypeFP32(), context.GetFP32(source)));
  1749. }
  1750. private static OperationResult GenerateUnaryS32(
  1751. CodeGenContext context,
  1752. AstOperation operation,
  1753. Func<SpvInstruction, SpvInstruction, SpvInstruction> emitS)
  1754. {
  1755. var source = operation.GetSource(0);
  1756. return new OperationResult(AggregateType.S32, emitS(context.TypeS32(), context.GetS32(source)));
  1757. }
  1758. private static OperationResult GenerateBinary(
  1759. CodeGenContext context,
  1760. AstOperation operation,
  1761. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitF,
  1762. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitI)
  1763. {
  1764. var src1 = operation.GetSource(0);
  1765. var src2 = operation.GetSource(1);
  1766. if (operation.Inst.HasFlag(Instruction.FP64))
  1767. {
  1768. var result = emitF(context.TypeFP64(), context.GetFP64(src1), context.GetFP64(src2));
  1769. if (!context.Config.GpuAccessor.QueryHostReducedPrecision())
  1770. {
  1771. context.Decorate(result, Decoration.NoContraction);
  1772. }
  1773. return new OperationResult(AggregateType.FP64, result);
  1774. }
  1775. else if (operation.Inst.HasFlag(Instruction.FP32))
  1776. {
  1777. var result = emitF(context.TypeFP32(), context.GetFP32(src1), context.GetFP32(src2));
  1778. if (!context.Config.GpuAccessor.QueryHostReducedPrecision())
  1779. {
  1780. context.Decorate(result, Decoration.NoContraction);
  1781. }
  1782. return new OperationResult(AggregateType.FP32, result);
  1783. }
  1784. else
  1785. {
  1786. return new OperationResult(AggregateType.S32, emitI(context.TypeS32(), context.GetS32(src1), context.GetS32(src2)));
  1787. }
  1788. }
  1789. private static OperationResult GenerateBinaryBool(
  1790. CodeGenContext context,
  1791. AstOperation operation,
  1792. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitB)
  1793. {
  1794. var src1 = operation.GetSource(0);
  1795. var src2 = operation.GetSource(1);
  1796. return new OperationResult(AggregateType.Bool, emitB(context.TypeBool(), context.Get(AggregateType.Bool, src1), context.Get(AggregateType.Bool, src2)));
  1797. }
  1798. private static OperationResult GenerateBinaryS32(
  1799. CodeGenContext context,
  1800. AstOperation operation,
  1801. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitS)
  1802. {
  1803. var src1 = operation.GetSource(0);
  1804. var src2 = operation.GetSource(1);
  1805. return new OperationResult(AggregateType.S32, emitS(context.TypeS32(), context.GetS32(src1), context.GetS32(src2)));
  1806. }
  1807. private static OperationResult GenerateBinaryU32(
  1808. CodeGenContext context,
  1809. AstOperation operation,
  1810. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitU)
  1811. {
  1812. var src1 = operation.GetSource(0);
  1813. var src2 = operation.GetSource(1);
  1814. return new OperationResult(AggregateType.U32, emitU(context.TypeU32(), context.GetU32(src1), context.GetU32(src2)));
  1815. }
  1816. private static OperationResult GenerateTernary(
  1817. CodeGenContext context,
  1818. AstOperation operation,
  1819. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitF,
  1820. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitI)
  1821. {
  1822. var src1 = operation.GetSource(0);
  1823. var src2 = operation.GetSource(1);
  1824. var src3 = operation.GetSource(2);
  1825. if (operation.Inst.HasFlag(Instruction.FP64))
  1826. {
  1827. var result = emitF(context.TypeFP64(), context.GetFP64(src1), context.GetFP64(src2), context.GetFP64(src3));
  1828. if (!context.Config.GpuAccessor.QueryHostReducedPrecision())
  1829. {
  1830. context.Decorate(result, Decoration.NoContraction);
  1831. }
  1832. return new OperationResult(AggregateType.FP64, result);
  1833. }
  1834. else if (operation.Inst.HasFlag(Instruction.FP32))
  1835. {
  1836. var result = emitF(context.TypeFP32(), context.GetFP32(src1), context.GetFP32(src2), context.GetFP32(src3));
  1837. if (!context.Config.GpuAccessor.QueryHostReducedPrecision())
  1838. {
  1839. context.Decorate(result, Decoration.NoContraction);
  1840. }
  1841. return new OperationResult(AggregateType.FP32, result);
  1842. }
  1843. else
  1844. {
  1845. return new OperationResult(AggregateType.S32, emitI(context.TypeS32(), context.GetS32(src1), context.GetS32(src2), context.GetS32(src3)));
  1846. }
  1847. }
  1848. private static OperationResult GenerateTernaryU32(
  1849. CodeGenContext context,
  1850. AstOperation operation,
  1851. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitU)
  1852. {
  1853. var src1 = operation.GetSource(0);
  1854. var src2 = operation.GetSource(1);
  1855. var src3 = operation.GetSource(2);
  1856. return new OperationResult(AggregateType.U32, emitU(
  1857. context.TypeU32(),
  1858. context.GetU32(src1),
  1859. context.GetU32(src2),
  1860. context.GetU32(src3)));
  1861. }
  1862. private static OperationResult GenerateBitfieldExtractS32(
  1863. CodeGenContext context,
  1864. AstOperation operation,
  1865. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitS)
  1866. {
  1867. var src1 = operation.GetSource(0);
  1868. var src2 = operation.GetSource(1);
  1869. var src3 = operation.GetSource(2);
  1870. return new OperationResult(AggregateType.S32, emitS(
  1871. context.TypeS32(),
  1872. context.GetS32(src1),
  1873. context.GetU32(src2),
  1874. context.GetU32(src3)));
  1875. }
  1876. private static OperationResult GenerateBitfieldInsert(
  1877. CodeGenContext context,
  1878. AstOperation operation,
  1879. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitS)
  1880. {
  1881. var src1 = operation.GetSource(0);
  1882. var src2 = operation.GetSource(1);
  1883. var src3 = operation.GetSource(2);
  1884. var src4 = operation.GetSource(3);
  1885. return new OperationResult(AggregateType.U32, emitS(
  1886. context.TypeU32(),
  1887. context.GetU32(src1),
  1888. context.GetU32(src2),
  1889. context.GetU32(src3),
  1890. context.GetU32(src4)));
  1891. }
  1892. }
  1893. }