Instructions.cs 100 KB

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