Instructions.cs 97 KB

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