Instructions.cs 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237
  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(), 3); // 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. return new OperationResult(resultType, context.GetAttribute(resultType, attrOffset, isOutAttr: false, index));
  713. }
  714. else
  715. {
  716. var attr = context.Get(AggregateType.S32, src2);
  717. return new OperationResult(resultType, context.GetAttribute(resultType, attr, isOutAttr: false, index));
  718. }
  719. }
  720. private static OperationResult GenerateLoadConstant(CodeGenContext context, AstOperation operation)
  721. {
  722. var src1 = operation.GetSource(0);
  723. var src2 = context.Get(AggregateType.S32, operation.GetSource(1));
  724. var i1 = context.Constant(context.TypeS32(), 0);
  725. var i2 = context.ShiftRightArithmetic(context.TypeS32(), src2, context.Constant(context.TypeS32(), 2));
  726. var i3 = context.BitwiseAnd(context.TypeS32(), src2, context.Constant(context.TypeS32(), 3));
  727. SpvInstruction value = null;
  728. if (context.Config.GpuAccessor.QueryHostHasVectorIndexingBug())
  729. {
  730. // Test for each component individually.
  731. for (int i = 0; i < 4; i++)
  732. {
  733. var component = context.Constant(context.TypeS32(), i);
  734. SpvInstruction elemPointer;
  735. if (context.UniformBuffersArray != null)
  736. {
  737. var ubVariable = context.UniformBuffersArray;
  738. var i0 = context.Get(AggregateType.S32, src1);
  739. elemPointer = context.AccessChain(context.TypePointer(StorageClass.Uniform, context.TypeFP32()), ubVariable, i0, i1, i2, component);
  740. }
  741. else
  742. {
  743. var ubVariable = context.UniformBuffers[((AstOperand)src1).Value];
  744. elemPointer = context.AccessChain(context.TypePointer(StorageClass.Uniform, context.TypeFP32()), ubVariable, i1, i2, component);
  745. }
  746. SpvInstruction newValue = context.Load(context.TypeFP32(), elemPointer);
  747. value = value != null ? context.Select(context.TypeFP32(), context.IEqual(context.TypeBool(), i3, component), newValue, value) : newValue;
  748. }
  749. }
  750. else
  751. {
  752. SpvInstruction elemPointer;
  753. if (context.UniformBuffersArray != null)
  754. {
  755. var ubVariable = context.UniformBuffersArray;
  756. var i0 = context.Get(AggregateType.S32, src1);
  757. elemPointer = context.AccessChain(context.TypePointer(StorageClass.Uniform, context.TypeFP32()), ubVariable, i0, i1, i2, i3);
  758. }
  759. else
  760. {
  761. var ubVariable = context.UniformBuffers[((AstOperand)src1).Value];
  762. elemPointer = context.AccessChain(context.TypePointer(StorageClass.Uniform, context.TypeFP32()), ubVariable, i1, i2, i3);
  763. }
  764. value = context.Load(context.TypeFP32(), elemPointer);
  765. }
  766. return new OperationResult(AggregateType.FP32, value);
  767. }
  768. private static OperationResult GenerateLoadLocal(CodeGenContext context, AstOperation operation)
  769. {
  770. return GenerateLoadLocalOrShared(context, operation, StorageClass.Private, context.LocalMemory);
  771. }
  772. private static OperationResult GenerateLoadShared(CodeGenContext context, AstOperation operation)
  773. {
  774. return GenerateLoadLocalOrShared(context, operation, StorageClass.Workgroup, context.SharedMemory);
  775. }
  776. private static OperationResult GenerateLoadLocalOrShared(
  777. CodeGenContext context,
  778. AstOperation operation,
  779. StorageClass storageClass,
  780. SpvInstruction memory)
  781. {
  782. var offset = context.Get(AggregateType.S32, operation.GetSource(0));
  783. var elemPointer = context.AccessChain(context.TypePointer(storageClass, context.TypeU32()), memory, offset);
  784. var value = context.Load(context.TypeU32(), elemPointer);
  785. return new OperationResult(AggregateType.U32, value);
  786. }
  787. private static OperationResult GenerateLoadStorage(CodeGenContext context, AstOperation operation)
  788. {
  789. var elemPointer = GetStorageElemPointer(context, operation);
  790. var value = context.Load(context.TypeU32(), elemPointer);
  791. return new OperationResult(AggregateType.U32, value);
  792. }
  793. private static OperationResult GenerateLod(CodeGenContext context, AstOperation operation)
  794. {
  795. AstTextureOperation texOp = (AstTextureOperation)operation;
  796. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  797. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  798. // TODO: Bindless texture support. For now we just return 0.
  799. if (isBindless)
  800. {
  801. return new OperationResult(AggregateType.S32, context.Constant(context.TypeS32(), 0));
  802. }
  803. int srcIndex = 0;
  804. SpvInstruction Src(AggregateType type)
  805. {
  806. return context.Get(type, texOp.GetSource(srcIndex++));
  807. }
  808. SpvInstruction index = null;
  809. if (isIndexed)
  810. {
  811. index = Src(AggregateType.S32);
  812. }
  813. int pCount = texOp.Type.GetDimensions();
  814. SpvInstruction pCoords;
  815. if (pCount > 1)
  816. {
  817. SpvInstruction[] elems = new SpvInstruction[pCount];
  818. for (int i = 0; i < pCount; i++)
  819. {
  820. elems[i] = Src(AggregateType.FP32);
  821. }
  822. var vectorType = context.TypeVector(context.TypeFP32(), pCount);
  823. pCoords = context.CompositeConstruct(vectorType, elems);
  824. }
  825. else
  826. {
  827. pCoords = Src(AggregateType.FP32);
  828. }
  829. var meta = new TextureMeta(texOp.CbufSlot, texOp.Handle, texOp.Format);
  830. (_, var sampledImageType, var sampledImageVariable) = context.Samplers[meta];
  831. var image = context.Load(sampledImageType, sampledImageVariable);
  832. var resultType = context.TypeVector(context.TypeFP32(), 2);
  833. var packed = context.ImageQueryLod(resultType, image, pCoords);
  834. var result = context.CompositeExtract(context.TypeFP32(), packed, (SpvLiteralInteger)texOp.Index);
  835. return new OperationResult(AggregateType.FP32, result);
  836. }
  837. private static OperationResult GenerateLogarithmB2(CodeGenContext context, AstOperation operation)
  838. {
  839. return GenerateUnary(context, operation, context.Delegates.GlslLog2, null);
  840. }
  841. private static OperationResult GenerateLogicalAnd(CodeGenContext context, AstOperation operation)
  842. {
  843. return GenerateBinaryBool(context, operation, context.Delegates.LogicalAnd);
  844. }
  845. private static OperationResult GenerateLogicalExclusiveOr(CodeGenContext context, AstOperation operation)
  846. {
  847. return GenerateBinaryBool(context, operation, context.Delegates.LogicalNotEqual);
  848. }
  849. private static OperationResult GenerateLogicalNot(CodeGenContext context, AstOperation operation)
  850. {
  851. return GenerateUnaryBool(context, operation, context.Delegates.LogicalNot);
  852. }
  853. private static OperationResult GenerateLogicalOr(CodeGenContext context, AstOperation operation)
  854. {
  855. return GenerateBinaryBool(context, operation, context.Delegates.LogicalOr);
  856. }
  857. private static OperationResult GenerateLoopBreak(CodeGenContext context, AstOperation operation)
  858. {
  859. AstBlock loopBlock = context.CurrentBlock;
  860. while (loopBlock.Type != AstBlockType.DoWhile)
  861. {
  862. loopBlock = loopBlock.Parent;
  863. }
  864. context.Branch(context.GetNextLabel(loopBlock.Parent));
  865. return OperationResult.Invalid;
  866. }
  867. private static OperationResult GenerateLoopContinue(CodeGenContext context, AstOperation operation)
  868. {
  869. AstBlock loopBlock = context.CurrentBlock;
  870. while (loopBlock.Type != AstBlockType.DoWhile)
  871. {
  872. loopBlock = loopBlock.Parent;
  873. }
  874. (var loopTarget, var continueTarget) = context.LoopTargets[loopBlock];
  875. context.Branch(continueTarget);
  876. return OperationResult.Invalid;
  877. }
  878. private static OperationResult GenerateMaximum(CodeGenContext context, AstOperation operation)
  879. {
  880. return GenerateBinary(context, operation, context.Delegates.GlslFMax, context.Delegates.GlslSMax);
  881. }
  882. private static OperationResult GenerateMaximumU32(CodeGenContext context, AstOperation operation)
  883. {
  884. return GenerateBinaryU32(context, operation, context.Delegates.GlslUMax);
  885. }
  886. private static OperationResult GenerateMemoryBarrier(CodeGenContext context, AstOperation operation)
  887. {
  888. context.MemoryBarrier(context.Constant(context.TypeU32(), Scope.Device), context.Constant(context.TypeU32(), DefaultMemorySemantics));
  889. return OperationResult.Invalid;
  890. }
  891. private static OperationResult GenerateMinimum(CodeGenContext context, AstOperation operation)
  892. {
  893. return GenerateBinary(context, operation, context.Delegates.GlslFMin, context.Delegates.GlslSMin);
  894. }
  895. private static OperationResult GenerateMinimumU32(CodeGenContext context, AstOperation operation)
  896. {
  897. return GenerateBinaryU32(context, operation, context.Delegates.GlslUMin);
  898. }
  899. private static OperationResult GenerateMultiply(CodeGenContext context, AstOperation operation)
  900. {
  901. return GenerateBinary(context, operation, context.Delegates.FMul, context.Delegates.IMul);
  902. }
  903. private static OperationResult GenerateMultiplyHighS32(CodeGenContext context, AstOperation operation)
  904. {
  905. var src1 = operation.GetSource(0);
  906. var src2 = operation.GetSource(1);
  907. var resultType = context.TypeStruct(false, context.TypeS32(), context.TypeS32());
  908. var result = context.SMulExtended(resultType, context.GetS32(src1), context.GetS32(src2));
  909. result = context.CompositeExtract(context.TypeS32(), result, 1);
  910. return new OperationResult(AggregateType.S32, result);
  911. }
  912. private static OperationResult GenerateMultiplyHighU32(CodeGenContext context, AstOperation operation)
  913. {
  914. var src1 = operation.GetSource(0);
  915. var src2 = operation.GetSource(1);
  916. var resultType = context.TypeStruct(false, context.TypeU32(), context.TypeU32());
  917. var result = context.UMulExtended(resultType, context.GetU32(src1), context.GetU32(src2));
  918. result = context.CompositeExtract(context.TypeU32(), result, 1);
  919. return new OperationResult(AggregateType.U32, result);
  920. }
  921. private static OperationResult GenerateNegate(CodeGenContext context, AstOperation operation)
  922. {
  923. return GenerateUnary(context, operation, context.Delegates.FNegate, context.Delegates.SNegate);
  924. }
  925. private static OperationResult GeneratePackDouble2x32(CodeGenContext context, AstOperation operation)
  926. {
  927. var value0 = context.GetU32(operation.GetSource(0));
  928. var value1 = context.GetU32(operation.GetSource(1));
  929. var vector = context.CompositeConstruct(context.TypeVector(context.TypeU32(), 2), value0, value1);
  930. var result = context.GlslPackDouble2x32(context.TypeFP64(), vector);
  931. return new OperationResult(AggregateType.FP64, result);
  932. }
  933. private static OperationResult GeneratePackHalf2x16(CodeGenContext context, AstOperation operation)
  934. {
  935. var value0 = context.GetFP32(operation.GetSource(0));
  936. var value1 = context.GetFP32(operation.GetSource(1));
  937. var vector = context.CompositeConstruct(context.TypeVector(context.TypeFP32(), 2), value0, value1);
  938. var result = context.GlslPackHalf2x16(context.TypeU32(), vector);
  939. return new OperationResult(AggregateType.U32, result);
  940. }
  941. private static OperationResult GenerateReciprocalSquareRoot(CodeGenContext context, AstOperation operation)
  942. {
  943. return GenerateUnary(context, operation, context.Delegates.GlslInverseSqrt, null);
  944. }
  945. private static OperationResult GenerateReturn(CodeGenContext context, AstOperation operation)
  946. {
  947. context.Return();
  948. return OperationResult.Invalid;
  949. }
  950. private static OperationResult GenerateRound(CodeGenContext context, AstOperation operation)
  951. {
  952. return GenerateUnary(context, operation, context.Delegates.GlslRoundEven, null);
  953. }
  954. private static OperationResult GenerateShiftLeft(CodeGenContext context, AstOperation operation)
  955. {
  956. return GenerateBinaryS32(context, operation, context.Delegates.ShiftLeftLogical);
  957. }
  958. private static OperationResult GenerateShiftRightS32(CodeGenContext context, AstOperation operation)
  959. {
  960. return GenerateBinaryS32(context, operation, context.Delegates.ShiftRightArithmetic);
  961. }
  962. private static OperationResult GenerateShiftRightU32(CodeGenContext context, AstOperation operation)
  963. {
  964. return GenerateBinaryS32(context, operation, context.Delegates.ShiftRightLogical);
  965. }
  966. private static OperationResult GenerateShuffle(CodeGenContext context, AstOperation operation)
  967. {
  968. var x = context.GetFP32(operation.GetSource(0));
  969. var index = context.GetU32(operation.GetSource(1));
  970. var mask = context.GetU32(operation.GetSource(2));
  971. var const31 = context.Constant(context.TypeU32(), 31);
  972. var const8 = context.Constant(context.TypeU32(), 8);
  973. var clamp = context.BitwiseAnd(context.TypeU32(), mask, const31);
  974. var segMask = context.BitwiseAnd(context.TypeU32(), context.ShiftRightLogical(context.TypeU32(), mask, const8), const31);
  975. var notSegMask = context.Not(context.TypeU32(), segMask);
  976. var clampNotSegMask = context.BitwiseAnd(context.TypeU32(), clamp, notSegMask);
  977. var indexNotSegMask = context.BitwiseAnd(context.TypeU32(), index, notSegMask);
  978. var threadId = context.GetAttribute(AggregateType.U32, AttributeConsts.LaneId, false);
  979. var minThreadId = context.BitwiseAnd(context.TypeU32(), threadId, segMask);
  980. var maxThreadId = context.BitwiseOr(context.TypeU32(), minThreadId, clampNotSegMask);
  981. var srcThreadId = context.BitwiseOr(context.TypeU32(), indexNotSegMask, minThreadId);
  982. var valid = context.ULessThanEqual(context.TypeBool(), srcThreadId, maxThreadId);
  983. var value = context.SubgroupReadInvocationKHR(context.TypeFP32(), x, srcThreadId);
  984. var result = context.Select(context.TypeFP32(), valid, value, x);
  985. var validLocal = (AstOperand)operation.GetSource(3);
  986. context.Store(context.GetLocalPointer(validLocal), context.BitcastIfNeeded(validLocal.VarType.Convert(), AggregateType.Bool, valid));
  987. return new OperationResult(AggregateType.FP32, result);
  988. }
  989. private static OperationResult GenerateShuffleDown(CodeGenContext context, AstOperation operation)
  990. {
  991. var x = context.GetFP32(operation.GetSource(0));
  992. var index = context.GetU32(operation.GetSource(1));
  993. var mask = context.GetU32(operation.GetSource(2));
  994. var const31 = context.Constant(context.TypeU32(), 31);
  995. var const8 = context.Constant(context.TypeU32(), 8);
  996. var clamp = context.BitwiseAnd(context.TypeU32(), mask, const31);
  997. var segMask = context.BitwiseAnd(context.TypeU32(), context.ShiftRightLogical(context.TypeU32(), mask, const8), const31);
  998. var notSegMask = context.Not(context.TypeU32(), segMask);
  999. var clampNotSegMask = context.BitwiseAnd(context.TypeU32(), clamp, notSegMask);
  1000. var threadId = context.GetAttribute(AggregateType.U32, AttributeConsts.LaneId, false);
  1001. var minThreadId = context.BitwiseAnd(context.TypeU32(), threadId, segMask);
  1002. var maxThreadId = context.BitwiseOr(context.TypeU32(), minThreadId, clampNotSegMask);
  1003. var srcThreadId = context.IAdd(context.TypeU32(), threadId, index);
  1004. var valid = context.ULessThanEqual(context.TypeBool(), srcThreadId, maxThreadId);
  1005. var value = context.SubgroupReadInvocationKHR(context.TypeFP32(), x, srcThreadId);
  1006. var result = context.Select(context.TypeFP32(), valid, value, x);
  1007. var validLocal = (AstOperand)operation.GetSource(3);
  1008. context.Store(context.GetLocalPointer(validLocal), context.BitcastIfNeeded(validLocal.VarType.Convert(), AggregateType.Bool, valid));
  1009. return new OperationResult(AggregateType.FP32, result);
  1010. }
  1011. private static OperationResult GenerateShuffleUp(CodeGenContext context, AstOperation operation)
  1012. {
  1013. var x = context.GetFP32(operation.GetSource(0));
  1014. var index = context.GetU32(operation.GetSource(1));
  1015. var mask = context.GetU32(operation.GetSource(2));
  1016. var const31 = context.Constant(context.TypeU32(), 31);
  1017. var const8 = context.Constant(context.TypeU32(), 8);
  1018. var segMask = context.BitwiseAnd(context.TypeU32(), context.ShiftRightLogical(context.TypeU32(), mask, const8), const31);
  1019. var threadId = context.GetAttribute(AggregateType.U32, AttributeConsts.LaneId, false);
  1020. var minThreadId = context.BitwiseAnd(context.TypeU32(), threadId, segMask);
  1021. var srcThreadId = context.ISub(context.TypeU32(), threadId, index);
  1022. var valid = context.SGreaterThanEqual(context.TypeBool(), srcThreadId, minThreadId);
  1023. var value = context.SubgroupReadInvocationKHR(context.TypeFP32(), x, srcThreadId);
  1024. var result = context.Select(context.TypeFP32(), valid, value, x);
  1025. var validLocal = (AstOperand)operation.GetSource(3);
  1026. context.Store(context.GetLocalPointer(validLocal), context.BitcastIfNeeded(validLocal.VarType.Convert(), AggregateType.Bool, valid));
  1027. return new OperationResult(AggregateType.FP32, result);
  1028. }
  1029. private static OperationResult GenerateShuffleXor(CodeGenContext context, AstOperation operation)
  1030. {
  1031. var x = context.GetFP32(operation.GetSource(0));
  1032. var index = context.GetU32(operation.GetSource(1));
  1033. var mask = context.GetU32(operation.GetSource(2));
  1034. var const31 = context.Constant(context.TypeU32(), 31);
  1035. var const8 = context.Constant(context.TypeU32(), 8);
  1036. var clamp = context.BitwiseAnd(context.TypeU32(), mask, const31);
  1037. var segMask = context.BitwiseAnd(context.TypeU32(), context.ShiftRightLogical(context.TypeU32(), mask, const8), const31);
  1038. var notSegMask = context.Not(context.TypeU32(), segMask);
  1039. var clampNotSegMask = context.BitwiseAnd(context.TypeU32(), clamp, notSegMask);
  1040. var threadId = context.GetAttribute(AggregateType.U32, AttributeConsts.LaneId, false);
  1041. var minThreadId = context.BitwiseAnd(context.TypeU32(), threadId, segMask);
  1042. var maxThreadId = context.BitwiseOr(context.TypeU32(), minThreadId, clampNotSegMask);
  1043. var srcThreadId = context.BitwiseXor(context.TypeU32(), threadId, index);
  1044. var valid = context.ULessThanEqual(context.TypeBool(), srcThreadId, maxThreadId);
  1045. var value = context.SubgroupReadInvocationKHR(context.TypeFP32(), x, srcThreadId);
  1046. var result = context.Select(context.TypeFP32(), valid, value, x);
  1047. var validLocal = (AstOperand)operation.GetSource(3);
  1048. context.Store(context.GetLocalPointer(validLocal), context.BitcastIfNeeded(validLocal.VarType.Convert(), AggregateType.Bool, valid));
  1049. return new OperationResult(AggregateType.FP32, result);
  1050. }
  1051. private static OperationResult GenerateSine(CodeGenContext context, AstOperation operation)
  1052. {
  1053. return GenerateUnary(context, operation, context.Delegates.GlslSin, null);
  1054. }
  1055. private static OperationResult GenerateSquareRoot(CodeGenContext context, AstOperation operation)
  1056. {
  1057. return GenerateUnary(context, operation, context.Delegates.GlslSqrt, null);
  1058. }
  1059. private static OperationResult GenerateStoreAttribute(CodeGenContext context, AstOperation operation)
  1060. {
  1061. var src1 = operation.GetSource(0);
  1062. var src2 = operation.GetSource(1);
  1063. var src3 = operation.GetSource(2);
  1064. if (!(src1 is AstOperand baseAttr) || baseAttr.Type != OperandType.Constant)
  1065. {
  1066. throw new InvalidOperationException($"First input of {nameof(Instruction.StoreAttribute)} must be a constant operand.");
  1067. }
  1068. SpvInstruction elemPointer;
  1069. AggregateType elemType;
  1070. if (src2 is AstOperand operand && operand.Type == OperandType.Constant)
  1071. {
  1072. int attrOffset = (baseAttr.Value & AttributeConsts.Mask) + (operand.Value << 2);
  1073. elemPointer = context.GetAttributeElemPointer(attrOffset, isOutAttr: true, index: null, out elemType);
  1074. }
  1075. else
  1076. {
  1077. var attr = context.Get(AggregateType.S32, src2);
  1078. elemPointer = context.GetAttributeElemPointer(attr, isOutAttr: true, index: null, out elemType);
  1079. }
  1080. var value = context.Get(elemType, src3);
  1081. context.Store(elemPointer, value);
  1082. return OperationResult.Invalid;
  1083. }
  1084. private static OperationResult GenerateStoreLocal(CodeGenContext context, AstOperation operation)
  1085. {
  1086. return GenerateStoreLocalOrShared(context, operation, StorageClass.Private, context.LocalMemory);
  1087. }
  1088. private static OperationResult GenerateStoreShared(CodeGenContext context, AstOperation operation)
  1089. {
  1090. return GenerateStoreLocalOrShared(context, operation, StorageClass.Workgroup, context.SharedMemory);
  1091. }
  1092. private static OperationResult GenerateStoreLocalOrShared(
  1093. CodeGenContext context,
  1094. AstOperation operation,
  1095. StorageClass storageClass,
  1096. SpvInstruction memory)
  1097. {
  1098. var offset = context.Get(AggregateType.S32, operation.GetSource(0));
  1099. var value = context.Get(AggregateType.U32, operation.GetSource(1));
  1100. var elemPointer = context.AccessChain(context.TypePointer(storageClass, context.TypeU32()), memory, offset);
  1101. context.Store(elemPointer, value);
  1102. return OperationResult.Invalid;
  1103. }
  1104. private static OperationResult GenerateStoreShared16(CodeGenContext context, AstOperation operation)
  1105. {
  1106. GenerateStoreSharedSmallInt(context, operation, 16);
  1107. return OperationResult.Invalid;
  1108. }
  1109. private static OperationResult GenerateStoreShared8(CodeGenContext context, AstOperation operation)
  1110. {
  1111. GenerateStoreSharedSmallInt(context, operation, 8);
  1112. return OperationResult.Invalid;
  1113. }
  1114. private static OperationResult GenerateStoreStorage(CodeGenContext context, AstOperation operation)
  1115. {
  1116. var elemPointer = GetStorageElemPointer(context, operation);
  1117. context.Store(elemPointer, context.Get(AggregateType.U32, operation.GetSource(2)));
  1118. return OperationResult.Invalid;
  1119. }
  1120. private static OperationResult GenerateStoreStorage16(CodeGenContext context, AstOperation operation)
  1121. {
  1122. GenerateStoreStorageSmallInt(context, operation, 16);
  1123. return OperationResult.Invalid;
  1124. }
  1125. private static OperationResult GenerateStoreStorage8(CodeGenContext context, AstOperation operation)
  1126. {
  1127. GenerateStoreStorageSmallInt(context, operation, 8);
  1128. return OperationResult.Invalid;
  1129. }
  1130. private static OperationResult GenerateSubtract(CodeGenContext context, AstOperation operation)
  1131. {
  1132. return GenerateBinary(context, operation, context.Delegates.FSub, context.Delegates.ISub);
  1133. }
  1134. private static OperationResult GenerateSwizzleAdd(CodeGenContext context, AstOperation operation)
  1135. {
  1136. var x = context.Get(AggregateType.FP32, operation.GetSource(0));
  1137. var y = context.Get(AggregateType.FP32, operation.GetSource(1));
  1138. var mask = context.Get(AggregateType.U32, operation.GetSource(2));
  1139. var v4float = context.TypeVector(context.TypeFP32(), 4);
  1140. var one = context.Constant(context.TypeFP32(), 1.0f);
  1141. var minusOne = context.Constant(context.TypeFP32(), -1.0f);
  1142. var zero = context.Constant(context.TypeFP32(), 0.0f);
  1143. var xLut = context.ConstantComposite(v4float, one, minusOne, one, zero);
  1144. var yLut = context.ConstantComposite(v4float, one, one, minusOne, one);
  1145. var threadId = context.GetAttribute(AggregateType.U32, AttributeConsts.LaneId, false);
  1146. var shift = context.BitwiseAnd(context.TypeU32(), threadId, context.Constant(context.TypeU32(), 3));
  1147. shift = context.ShiftLeftLogical(context.TypeU32(), shift, context.Constant(context.TypeU32(), 1));
  1148. var lutIdx = context.ShiftRightLogical(context.TypeU32(), mask, shift);
  1149. var xLutValue = context.VectorExtractDynamic(context.TypeFP32(), xLut, lutIdx);
  1150. var yLutValue = context.VectorExtractDynamic(context.TypeFP32(), yLut, lutIdx);
  1151. var xResult = context.FMul(context.TypeFP32(), x, xLutValue);
  1152. var yResult = context.FMul(context.TypeFP32(), y, yLutValue);
  1153. var result = context.FAdd(context.TypeFP32(), xResult, yResult);
  1154. return new OperationResult(AggregateType.FP32, result);
  1155. }
  1156. private static OperationResult GenerateTextureSample(CodeGenContext context, AstOperation operation)
  1157. {
  1158. AstTextureOperation texOp = (AstTextureOperation)operation;
  1159. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  1160. bool isGather = (texOp.Flags & TextureFlags.Gather) != 0;
  1161. bool hasDerivatives = (texOp.Flags & TextureFlags.Derivatives) != 0;
  1162. bool intCoords = (texOp.Flags & TextureFlags.IntCoords) != 0;
  1163. bool hasLodBias = (texOp.Flags & TextureFlags.LodBias) != 0;
  1164. bool hasLodLevel = (texOp.Flags & TextureFlags.LodLevel) != 0;
  1165. bool hasOffset = (texOp.Flags & TextureFlags.Offset) != 0;
  1166. bool hasOffsets = (texOp.Flags & TextureFlags.Offsets) != 0;
  1167. bool isArray = (texOp.Type & SamplerType.Array) != 0;
  1168. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  1169. bool isMultisample = (texOp.Type & SamplerType.Multisample) != 0;
  1170. bool isShadow = (texOp.Type & SamplerType.Shadow) != 0;
  1171. // TODO: Bindless texture support. For now we just return 0.
  1172. if (isBindless)
  1173. {
  1174. return new OperationResult(AggregateType.FP32, context.Constant(context.TypeFP32(), 0f));
  1175. }
  1176. // This combination is valid, but not available on GLSL.
  1177. // For now, ignore the LOD level and do a normal sample.
  1178. // TODO: How to implement it properly?
  1179. if (hasLodLevel && isArray && isShadow)
  1180. {
  1181. hasLodLevel = false;
  1182. }
  1183. int srcIndex = isBindless ? 1 : 0;
  1184. SpvInstruction Src(AggregateType type)
  1185. {
  1186. return context.Get(type, texOp.GetSource(srcIndex++));
  1187. }
  1188. SpvInstruction index = null;
  1189. if (isIndexed)
  1190. {
  1191. index = Src(AggregateType.S32);
  1192. }
  1193. int coordsCount = texOp.Type.GetDimensions();
  1194. int pCount = coordsCount;
  1195. int arrayIndexElem = -1;
  1196. if (isArray)
  1197. {
  1198. arrayIndexElem = pCount++;
  1199. }
  1200. AggregateType coordType = intCoords ? AggregateType.S32 : AggregateType.FP32;
  1201. SpvInstruction AssemblePVector(int count)
  1202. {
  1203. if (count > 1)
  1204. {
  1205. SpvInstruction[] elems = new SpvInstruction[count];
  1206. for (int index = 0; index < count; index++)
  1207. {
  1208. if (arrayIndexElem == index)
  1209. {
  1210. elems[index] = Src(AggregateType.S32);
  1211. if (!intCoords)
  1212. {
  1213. elems[index] = context.ConvertSToF(context.TypeFP32(), elems[index]);
  1214. }
  1215. }
  1216. else
  1217. {
  1218. elems[index] = Src(coordType);
  1219. }
  1220. }
  1221. var vectorType = context.TypeVector(intCoords ? context.TypeS32() : context.TypeFP32(), count);
  1222. return context.CompositeConstruct(vectorType, elems);
  1223. }
  1224. else
  1225. {
  1226. return Src(coordType);
  1227. }
  1228. }
  1229. SpvInstruction pCoords = AssemblePVector(pCount);
  1230. pCoords = ScalingHelpers.ApplyScaling(context, texOp, pCoords, intCoords, isBindless, isIndexed, isArray, pCount);
  1231. SpvInstruction AssembleDerivativesVector(int count)
  1232. {
  1233. if (count > 1)
  1234. {
  1235. SpvInstruction[] elems = new SpvInstruction[count];
  1236. for (int index = 0; index < count; index++)
  1237. {
  1238. elems[index] = Src(AggregateType.FP32);
  1239. }
  1240. var vectorType = context.TypeVector(context.TypeFP32(), count);
  1241. return context.CompositeConstruct(vectorType, elems);
  1242. }
  1243. else
  1244. {
  1245. return Src(AggregateType.FP32);
  1246. }
  1247. }
  1248. SpvInstruction dRef = null;
  1249. if (isShadow)
  1250. {
  1251. dRef = Src(AggregateType.FP32);
  1252. }
  1253. SpvInstruction[] derivatives = null;
  1254. if (hasDerivatives)
  1255. {
  1256. derivatives = new[]
  1257. {
  1258. AssembleDerivativesVector(coordsCount), // dPdx
  1259. AssembleDerivativesVector(coordsCount) // dPdy
  1260. };
  1261. }
  1262. SpvInstruction sample = null;
  1263. SpvInstruction lod = null;
  1264. if (isMultisample)
  1265. {
  1266. sample = Src(AggregateType.S32);
  1267. }
  1268. else if (hasLodLevel)
  1269. {
  1270. lod = Src(coordType);
  1271. }
  1272. SpvInstruction AssembleOffsetVector(int count)
  1273. {
  1274. if (count > 1)
  1275. {
  1276. SpvInstruction[] elems = new SpvInstruction[count];
  1277. for (int index = 0; index < count; index++)
  1278. {
  1279. elems[index] = Src(AggregateType.S32);
  1280. }
  1281. var vectorType = context.TypeVector(context.TypeS32(), count);
  1282. return context.ConstantComposite(vectorType, elems);
  1283. }
  1284. else
  1285. {
  1286. return Src(AggregateType.S32);
  1287. }
  1288. }
  1289. SpvInstruction[] offsets = null;
  1290. if (hasOffset)
  1291. {
  1292. offsets = new[] { AssembleOffsetVector(coordsCount) };
  1293. }
  1294. else if (hasOffsets)
  1295. {
  1296. offsets = new[]
  1297. {
  1298. AssembleOffsetVector(coordsCount),
  1299. AssembleOffsetVector(coordsCount),
  1300. AssembleOffsetVector(coordsCount),
  1301. AssembleOffsetVector(coordsCount)
  1302. };
  1303. }
  1304. SpvInstruction lodBias = null;
  1305. if (hasLodBias)
  1306. {
  1307. lodBias = Src(AggregateType.FP32);
  1308. }
  1309. SpvInstruction compIdx = null;
  1310. // textureGather* optional extra component index,
  1311. // not needed for shadow samplers.
  1312. if (isGather && !isShadow)
  1313. {
  1314. compIdx = Src(AggregateType.S32);
  1315. }
  1316. var operandsList = new List<SpvInstruction>();
  1317. var operandsMask = ImageOperandsMask.MaskNone;
  1318. if (hasLodBias)
  1319. {
  1320. operandsMask |= ImageOperandsMask.Bias;
  1321. operandsList.Add(lodBias);
  1322. }
  1323. if (!isMultisample && hasLodLevel)
  1324. {
  1325. operandsMask |= ImageOperandsMask.Lod;
  1326. operandsList.Add(lod);
  1327. }
  1328. if (hasDerivatives)
  1329. {
  1330. operandsMask |= ImageOperandsMask.Grad;
  1331. operandsList.Add(derivatives[0]);
  1332. operandsList.Add(derivatives[1]);
  1333. }
  1334. if (hasOffset)
  1335. {
  1336. operandsMask |= ImageOperandsMask.ConstOffset;
  1337. operandsList.Add(offsets[0]);
  1338. }
  1339. else if (hasOffsets)
  1340. {
  1341. operandsMask |= ImageOperandsMask.ConstOffsets;
  1342. SpvInstruction arrayv2 = context.TypeArray(context.TypeVector(context.TypeS32(), 2), context.Constant(context.TypeU32(), 4));
  1343. operandsList.Add(context.ConstantComposite(arrayv2, offsets[0], offsets[1], offsets[2], offsets[3]));
  1344. }
  1345. if (isMultisample)
  1346. {
  1347. operandsMask |= ImageOperandsMask.Sample;
  1348. operandsList.Add(sample);
  1349. }
  1350. bool colorIsVector = isGather || !isShadow;
  1351. var resultType = colorIsVector ? context.TypeVector(context.TypeFP32(), 4) : context.TypeFP32();
  1352. var meta = new TextureMeta(texOp.CbufSlot, texOp.Handle, texOp.Format);
  1353. (var imageType, var sampledImageType, var sampledImageVariable) = context.Samplers[meta];
  1354. var image = context.Load(sampledImageType, sampledImageVariable);
  1355. if (intCoords)
  1356. {
  1357. image = context.Image(imageType, image);
  1358. }
  1359. var operands = operandsList.ToArray();
  1360. SpvInstruction result;
  1361. if (intCoords)
  1362. {
  1363. result = context.ImageFetch(resultType, image, pCoords, operandsMask, operands);
  1364. }
  1365. else if (isGather)
  1366. {
  1367. if (isShadow)
  1368. {
  1369. result = context.ImageDrefGather(resultType, image, pCoords, dRef, operandsMask, operands);
  1370. }
  1371. else
  1372. {
  1373. result = context.ImageGather(resultType, image, pCoords, compIdx, operandsMask, operands);
  1374. }
  1375. }
  1376. else if (isShadow)
  1377. {
  1378. if (hasLodLevel)
  1379. {
  1380. result = context.ImageSampleDrefExplicitLod(resultType, image, pCoords, dRef, operandsMask, operands);
  1381. }
  1382. else
  1383. {
  1384. result = context.ImageSampleDrefImplicitLod(resultType, image, pCoords, dRef, operandsMask, operands);
  1385. }
  1386. }
  1387. else if (hasDerivatives || hasLodLevel)
  1388. {
  1389. result = context.ImageSampleExplicitLod(resultType, image, pCoords, operandsMask, operands);
  1390. }
  1391. else
  1392. {
  1393. result = context.ImageSampleImplicitLod(resultType, image, pCoords, operandsMask, operands);
  1394. }
  1395. if (colorIsVector)
  1396. {
  1397. result = context.CompositeExtract(context.TypeFP32(), result, (SpvLiteralInteger)texOp.Index);
  1398. }
  1399. return new OperationResult(AggregateType.FP32, result);
  1400. }
  1401. private static OperationResult GenerateTextureSize(CodeGenContext context, AstOperation operation)
  1402. {
  1403. AstTextureOperation texOp = (AstTextureOperation)operation;
  1404. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  1405. // TODO: Bindless texture support. For now we just return 0.
  1406. if (isBindless)
  1407. {
  1408. return new OperationResult(AggregateType.S32, context.Constant(context.TypeS32(), 0));
  1409. }
  1410. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  1411. SpvInstruction index = null;
  1412. if (isIndexed)
  1413. {
  1414. index = context.GetS32(texOp.GetSource(0));
  1415. }
  1416. var meta = new TextureMeta(texOp.CbufSlot, texOp.Handle, texOp.Format);
  1417. (var imageType, var sampledImageType, var sampledImageVariable) = context.Samplers[meta];
  1418. var image = context.Load(sampledImageType, sampledImageVariable);
  1419. image = context.Image(imageType, image);
  1420. if (texOp.Index == 3)
  1421. {
  1422. return new OperationResult(AggregateType.S32, context.ImageQueryLevels(context.TypeS32(), image));
  1423. }
  1424. else
  1425. {
  1426. var type = context.SamplersTypes[meta];
  1427. bool hasLod = !type.HasFlag(SamplerType.Multisample) && type != SamplerType.TextureBuffer;
  1428. int dimensions = (type & SamplerType.Mask) == SamplerType.TextureCube ? 2 : type.GetDimensions();
  1429. if (type.HasFlag(SamplerType.Array))
  1430. {
  1431. dimensions++;
  1432. }
  1433. var resultType = dimensions == 1 ? context.TypeS32() : context.TypeVector(context.TypeS32(), dimensions);
  1434. SpvInstruction result;
  1435. if (hasLod)
  1436. {
  1437. int lodSrcIndex = isBindless || isIndexed ? 1 : 0;
  1438. var lod = context.GetS32(operation.GetSource(lodSrcIndex));
  1439. result = context.ImageQuerySizeLod(resultType, image, lod);
  1440. }
  1441. else
  1442. {
  1443. result = context.ImageQuerySize(resultType, image);
  1444. }
  1445. if (dimensions != 1)
  1446. {
  1447. result = context.CompositeExtract(context.TypeS32(), result, (SpvLiteralInteger)texOp.Index);
  1448. }
  1449. if (texOp.Index < 2 || (type & SamplerType.Mask) == SamplerType.Texture3D)
  1450. {
  1451. result = ScalingHelpers.ApplyUnscaling(context, texOp, result, isBindless, isIndexed);
  1452. }
  1453. return new OperationResult(AggregateType.S32, result);
  1454. }
  1455. }
  1456. private static OperationResult GenerateTruncate(CodeGenContext context, AstOperation operation)
  1457. {
  1458. return GenerateUnary(context, operation, context.Delegates.GlslTrunc, null);
  1459. }
  1460. private static OperationResult GenerateUnpackDouble2x32(CodeGenContext context, AstOperation operation)
  1461. {
  1462. var value = context.GetFP64(operation.GetSource(0));
  1463. var vector = context.GlslUnpackDouble2x32(context.TypeVector(context.TypeU32(), 2), value);
  1464. var result = context.CompositeExtract(context.TypeU32(), vector, operation.Index);
  1465. return new OperationResult(AggregateType.U32, result);
  1466. }
  1467. private static OperationResult GenerateUnpackHalf2x16(CodeGenContext context, AstOperation operation)
  1468. {
  1469. var value = context.GetU32(operation.GetSource(0));
  1470. var vector = context.GlslUnpackHalf2x16(context.TypeVector(context.TypeFP32(), 2), value);
  1471. var result = context.CompositeExtract(context.TypeFP32(), vector, operation.Index);
  1472. return new OperationResult(AggregateType.FP32, result);
  1473. }
  1474. private static OperationResult GenerateVoteAll(CodeGenContext context, AstOperation operation)
  1475. {
  1476. var result = context.SubgroupAllKHR(context.TypeBool(), context.Get(AggregateType.Bool, operation.GetSource(0)));
  1477. return new OperationResult(AggregateType.Bool, result);
  1478. }
  1479. private static OperationResult GenerateVoteAllEqual(CodeGenContext context, AstOperation operation)
  1480. {
  1481. var result = context.SubgroupAllEqualKHR(context.TypeBool(), context.Get(AggregateType.Bool, operation.GetSource(0)));
  1482. return new OperationResult(AggregateType.Bool, result);
  1483. }
  1484. private static OperationResult GenerateVoteAny(CodeGenContext context, AstOperation operation)
  1485. {
  1486. var result = context.SubgroupAnyKHR(context.TypeBool(), context.Get(AggregateType.Bool, operation.GetSource(0)));
  1487. return new OperationResult(AggregateType.Bool, result);
  1488. }
  1489. private static OperationResult GenerateCompare(
  1490. CodeGenContext context,
  1491. AstOperation operation,
  1492. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitF,
  1493. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitI)
  1494. {
  1495. var src1 = operation.GetSource(0);
  1496. var src2 = operation.GetSource(1);
  1497. SpvInstruction result;
  1498. if (operation.Inst.HasFlag(Instruction.FP64))
  1499. {
  1500. result = emitF(context.TypeBool(), context.GetFP64(src1), context.GetFP64(src2));
  1501. }
  1502. else if (operation.Inst.HasFlag(Instruction.FP32))
  1503. {
  1504. result = emitF(context.TypeBool(), context.GetFP32(src1), context.GetFP32(src2));
  1505. }
  1506. else
  1507. {
  1508. result = emitI(context.TypeBool(), context.GetS32(src1), context.GetS32(src2));
  1509. }
  1510. return new OperationResult(AggregateType.Bool, result);
  1511. }
  1512. private static OperationResult GenerateCompareU32(
  1513. CodeGenContext context,
  1514. AstOperation operation,
  1515. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitU)
  1516. {
  1517. var src1 = operation.GetSource(0);
  1518. var src2 = operation.GetSource(1);
  1519. var result = emitU(context.TypeBool(), context.GetU32(src1), context.GetU32(src2));
  1520. return new OperationResult(AggregateType.Bool, result);
  1521. }
  1522. private static OperationResult GenerateAtomicMemoryBinary(
  1523. CodeGenContext context,
  1524. AstOperation operation,
  1525. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitU)
  1526. {
  1527. var value = context.GetU32(operation.GetSource(2));
  1528. SpvInstruction elemPointer;
  1529. Instruction mr = operation.Inst & Instruction.MrMask;
  1530. if (mr == Instruction.MrStorage)
  1531. {
  1532. elemPointer = GetStorageElemPointer(context, operation);
  1533. }
  1534. else if (mr == Instruction.MrShared)
  1535. {
  1536. var offset = context.GetU32(operation.GetSource(0));
  1537. elemPointer = context.AccessChain(context.TypePointer(StorageClass.Workgroup, context.TypeU32()), context.SharedMemory, offset);
  1538. }
  1539. else
  1540. {
  1541. throw new InvalidOperationException($"Invalid storage class \"{mr}\".");
  1542. }
  1543. var one = context.Constant(context.TypeU32(), 1);
  1544. var zero = context.Constant(context.TypeU32(), 0);
  1545. return new OperationResult(AggregateType.U32, emitU(context.TypeU32(), elemPointer, one, zero, value));
  1546. }
  1547. private static OperationResult GenerateAtomicMemoryCas(CodeGenContext context, AstOperation operation)
  1548. {
  1549. var value0 = context.GetU32(operation.GetSource(2));
  1550. var value1 = context.GetU32(operation.GetSource(3));
  1551. SpvInstruction elemPointer;
  1552. Instruction mr = operation.Inst & Instruction.MrMask;
  1553. if (mr == Instruction.MrStorage)
  1554. {
  1555. elemPointer = GetStorageElemPointer(context, operation);
  1556. }
  1557. else if (mr == Instruction.MrShared)
  1558. {
  1559. var offset = context.GetU32(operation.GetSource(0));
  1560. elemPointer = context.AccessChain(context.TypePointer(StorageClass.Workgroup, context.TypeU32()), context.SharedMemory, offset);
  1561. }
  1562. else
  1563. {
  1564. throw new InvalidOperationException($"Invalid storage class \"{mr}\".");
  1565. }
  1566. var one = context.Constant(context.TypeU32(), 1);
  1567. var zero = context.Constant(context.TypeU32(), 0);
  1568. return new OperationResult(AggregateType.U32, context.AtomicCompareExchange(context.TypeU32(), elemPointer, one, zero, zero, value1, value0));
  1569. }
  1570. private static void GenerateStoreSharedSmallInt(CodeGenContext context, AstOperation operation, int bitSize)
  1571. {
  1572. var offset = context.Get(AggregateType.U32, operation.GetSource(0));
  1573. var value = context.Get(AggregateType.U32, operation.GetSource(1));
  1574. var wordOffset = context.ShiftRightLogical(context.TypeU32(), offset, context.Constant(context.TypeU32(), 2));
  1575. var bitOffset = context.BitwiseAnd(context.TypeU32(), offset, context.Constant(context.TypeU32(), 3));
  1576. bitOffset = context.ShiftLeftLogical(context.TypeU32(), bitOffset, context.Constant(context.TypeU32(), 3));
  1577. var memory = context.SharedMemory;
  1578. var elemPointer = context.AccessChain(context.TypePointer(StorageClass.Workgroup, context.TypeU32()), memory, wordOffset);
  1579. GenerateStoreSmallInt(context, elemPointer, bitOffset, value, bitSize);
  1580. }
  1581. private static void GenerateStoreStorageSmallInt(CodeGenContext context, AstOperation operation, int bitSize)
  1582. {
  1583. var i0 = context.Get(AggregateType.S32, operation.GetSource(0));
  1584. var offset = context.Get(AggregateType.U32, operation.GetSource(1));
  1585. var value = context.Get(AggregateType.U32, operation.GetSource(2));
  1586. var wordOffset = context.ShiftRightLogical(context.TypeU32(), offset, context.Constant(context.TypeU32(), 2));
  1587. var bitOffset = context.BitwiseAnd(context.TypeU32(), offset, context.Constant(context.TypeU32(), 3));
  1588. bitOffset = context.ShiftLeftLogical(context.TypeU32(), bitOffset, context.Constant(context.TypeU32(), 3));
  1589. var sbVariable = context.StorageBuffersArray;
  1590. var i1 = context.Constant(context.TypeS32(), 0);
  1591. var elemPointer = context.AccessChain(context.TypePointer(StorageClass.Uniform, context.TypeU32()), sbVariable, i0, i1, wordOffset);
  1592. GenerateStoreSmallInt(context, elemPointer, bitOffset, value, bitSize);
  1593. }
  1594. private static void GenerateStoreSmallInt(
  1595. CodeGenContext context,
  1596. SpvInstruction elemPointer,
  1597. SpvInstruction bitOffset,
  1598. SpvInstruction value,
  1599. int bitSize)
  1600. {
  1601. var loopStart = context.Label();
  1602. var loopEnd = context.Label();
  1603. context.Branch(loopStart);
  1604. context.AddLabel(loopStart);
  1605. var oldValue = context.Load(context.TypeU32(), elemPointer);
  1606. var newValue = context.BitFieldInsert(context.TypeU32(), oldValue, value, bitOffset, context.Constant(context.TypeU32(), bitSize));
  1607. var one = context.Constant(context.TypeU32(), 1);
  1608. var zero = context.Constant(context.TypeU32(), 0);
  1609. var result = context.AtomicCompareExchange(context.TypeU32(), elemPointer, one, zero, zero, newValue, oldValue);
  1610. var failed = context.INotEqual(context.TypeBool(), result, oldValue);
  1611. context.LoopMerge(loopEnd, loopStart, LoopControlMask.MaskNone);
  1612. context.BranchConditional(failed, loopStart, loopEnd);
  1613. context.AddLabel(loopEnd);
  1614. }
  1615. private static SpvInstruction GetStorageElemPointer(CodeGenContext context, AstOperation operation)
  1616. {
  1617. var sbVariable = context.StorageBuffersArray;
  1618. var i0 = context.Get(AggregateType.S32, operation.GetSource(0));
  1619. var i1 = context.Constant(context.TypeS32(), 0);
  1620. var i2 = context.Get(AggregateType.S32, operation.GetSource(1));
  1621. return context.AccessChain(context.TypePointer(StorageClass.Uniform, context.TypeU32()), sbVariable, i0, i1, i2);
  1622. }
  1623. private static OperationResult GenerateUnary(
  1624. CodeGenContext context,
  1625. AstOperation operation,
  1626. Func<SpvInstruction, SpvInstruction, SpvInstruction> emitF,
  1627. Func<SpvInstruction, SpvInstruction, SpvInstruction> emitI)
  1628. {
  1629. var source = operation.GetSource(0);
  1630. if (operation.Inst.HasFlag(Instruction.FP64))
  1631. {
  1632. return new OperationResult(AggregateType.FP64, emitF(context.TypeFP64(), context.GetFP64(source)));
  1633. }
  1634. else if (operation.Inst.HasFlag(Instruction.FP32))
  1635. {
  1636. return new OperationResult(AggregateType.FP32, emitF(context.TypeFP32(), context.GetFP32(source)));
  1637. }
  1638. else
  1639. {
  1640. return new OperationResult(AggregateType.S32, emitI(context.TypeS32(), context.GetS32(source)));
  1641. }
  1642. }
  1643. private static OperationResult GenerateUnaryBool(
  1644. CodeGenContext context,
  1645. AstOperation operation,
  1646. Func<SpvInstruction, SpvInstruction, SpvInstruction> emitB)
  1647. {
  1648. var source = operation.GetSource(0);
  1649. return new OperationResult(AggregateType.Bool, emitB(context.TypeBool(), context.Get(AggregateType.Bool, source)));
  1650. }
  1651. private static OperationResult GenerateUnaryFP32(
  1652. CodeGenContext context,
  1653. AstOperation operation,
  1654. Func<SpvInstruction, SpvInstruction, SpvInstruction> emit)
  1655. {
  1656. var source = operation.GetSource(0);
  1657. return new OperationResult(AggregateType.FP32, emit(context.TypeFP32(), context.GetFP32(source)));
  1658. }
  1659. private static OperationResult GenerateUnaryS32(
  1660. CodeGenContext context,
  1661. AstOperation operation,
  1662. Func<SpvInstruction, SpvInstruction, SpvInstruction> emitS)
  1663. {
  1664. var source = operation.GetSource(0);
  1665. return new OperationResult(AggregateType.S32, emitS(context.TypeS32(), context.GetS32(source)));
  1666. }
  1667. private static OperationResult GenerateBinary(
  1668. CodeGenContext context,
  1669. AstOperation operation,
  1670. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitF,
  1671. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitI)
  1672. {
  1673. var src1 = operation.GetSource(0);
  1674. var src2 = operation.GetSource(1);
  1675. if (operation.Inst.HasFlag(Instruction.FP64))
  1676. {
  1677. var result = emitF(context.TypeFP64(), context.GetFP64(src1), context.GetFP64(src2));
  1678. context.Decorate(result, Decoration.NoContraction);
  1679. return new OperationResult(AggregateType.FP64, result);
  1680. }
  1681. else if (operation.Inst.HasFlag(Instruction.FP32))
  1682. {
  1683. var result = emitF(context.TypeFP32(), context.GetFP32(src1), context.GetFP32(src2));
  1684. context.Decorate(result, Decoration.NoContraction);
  1685. return new OperationResult(AggregateType.FP32, result);
  1686. }
  1687. else
  1688. {
  1689. return new OperationResult(AggregateType.S32, emitI(context.TypeS32(), context.GetS32(src1), context.GetS32(src2)));
  1690. }
  1691. }
  1692. private static OperationResult GenerateBinaryBool(
  1693. CodeGenContext context,
  1694. AstOperation operation,
  1695. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitB)
  1696. {
  1697. var src1 = operation.GetSource(0);
  1698. var src2 = operation.GetSource(1);
  1699. return new OperationResult(AggregateType.Bool, emitB(context.TypeBool(), context.Get(AggregateType.Bool, src1), context.Get(AggregateType.Bool, src2)));
  1700. }
  1701. private static OperationResult GenerateBinaryS32(
  1702. CodeGenContext context,
  1703. AstOperation operation,
  1704. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitS)
  1705. {
  1706. var src1 = operation.GetSource(0);
  1707. var src2 = operation.GetSource(1);
  1708. return new OperationResult(AggregateType.S32, emitS(context.TypeS32(), context.GetS32(src1), context.GetS32(src2)));
  1709. }
  1710. private static OperationResult GenerateBinaryU32(
  1711. CodeGenContext context,
  1712. AstOperation operation,
  1713. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitU)
  1714. {
  1715. var src1 = operation.GetSource(0);
  1716. var src2 = operation.GetSource(1);
  1717. return new OperationResult(AggregateType.U32, emitU(context.TypeU32(), context.GetU32(src1), context.GetU32(src2)));
  1718. }
  1719. private static OperationResult GenerateTernary(
  1720. CodeGenContext context,
  1721. AstOperation operation,
  1722. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitF,
  1723. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitI)
  1724. {
  1725. var src1 = operation.GetSource(0);
  1726. var src2 = operation.GetSource(1);
  1727. var src3 = operation.GetSource(2);
  1728. if (operation.Inst.HasFlag(Instruction.FP64))
  1729. {
  1730. var result = emitF(context.TypeFP64(), context.GetFP64(src1), context.GetFP64(src2), context.GetFP64(src3));
  1731. context.Decorate(result, Decoration.NoContraction);
  1732. return new OperationResult(AggregateType.FP64, result);
  1733. }
  1734. else if (operation.Inst.HasFlag(Instruction.FP32))
  1735. {
  1736. var result = emitF(context.TypeFP32(), context.GetFP32(src1), context.GetFP32(src2), context.GetFP32(src3));
  1737. context.Decorate(result, Decoration.NoContraction);
  1738. return new OperationResult(AggregateType.FP32, result);
  1739. }
  1740. else
  1741. {
  1742. return new OperationResult(AggregateType.S32, emitI(context.TypeS32(), context.GetS32(src1), context.GetS32(src2), context.GetS32(src3)));
  1743. }
  1744. }
  1745. private static OperationResult GenerateTernaryS32(
  1746. CodeGenContext context,
  1747. AstOperation operation,
  1748. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitS)
  1749. {
  1750. var src1 = operation.GetSource(0);
  1751. var src2 = operation.GetSource(1);
  1752. var src3 = operation.GetSource(2);
  1753. return new OperationResult(AggregateType.S32, emitS(
  1754. context.TypeS32(),
  1755. context.GetS32(src1),
  1756. context.GetS32(src2),
  1757. context.GetS32(src3)));
  1758. }
  1759. private static OperationResult GenerateTernaryU32(
  1760. CodeGenContext context,
  1761. AstOperation operation,
  1762. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitU)
  1763. {
  1764. var src1 = operation.GetSource(0);
  1765. var src2 = operation.GetSource(1);
  1766. var src3 = operation.GetSource(2);
  1767. return new OperationResult(AggregateType.U32, emitU(
  1768. context.TypeU32(),
  1769. context.GetU32(src1),
  1770. context.GetU32(src2),
  1771. context.GetU32(src3)));
  1772. }
  1773. private static OperationResult GenerateQuaternaryS32(
  1774. CodeGenContext context,
  1775. AstOperation operation,
  1776. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitS)
  1777. {
  1778. var src1 = operation.GetSource(0);
  1779. var src2 = operation.GetSource(1);
  1780. var src3 = operation.GetSource(2);
  1781. var src4 = operation.GetSource(3);
  1782. return new OperationResult(AggregateType.S32, emitS(
  1783. context.TypeS32(),
  1784. context.GetS32(src1),
  1785. context.GetS32(src2),
  1786. context.GetS32(src3),
  1787. context.GetS32(src4)));
  1788. }
  1789. }
  1790. }