Instructions.cs 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323
  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 GenerateTernaryS32(context, operation, context.Delegates.BitFieldSExtract);
  238. }
  239. private static OperationResult GenerateBitfieldExtractU32(CodeGenContext context, AstOperation operation)
  240. {
  241. return GenerateTernaryS32(context, operation, context.Delegates.BitFieldUExtract);
  242. }
  243. private static OperationResult GenerateBitfieldInsert(CodeGenContext context, AstOperation operation)
  244. {
  245. return GenerateQuaternaryS32(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. context.Decorate(result, Decoration.NoContraction);
  1747. return new OperationResult(AggregateType.FP64, result);
  1748. }
  1749. else if (operation.Inst.HasFlag(Instruction.FP32))
  1750. {
  1751. var result = emitF(context.TypeFP32(), context.GetFP32(src1), context.GetFP32(src2));
  1752. context.Decorate(result, Decoration.NoContraction);
  1753. return new OperationResult(AggregateType.FP32, result);
  1754. }
  1755. else
  1756. {
  1757. return new OperationResult(AggregateType.S32, emitI(context.TypeS32(), context.GetS32(src1), context.GetS32(src2)));
  1758. }
  1759. }
  1760. private static OperationResult GenerateBinaryBool(
  1761. CodeGenContext context,
  1762. AstOperation operation,
  1763. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitB)
  1764. {
  1765. var src1 = operation.GetSource(0);
  1766. var src2 = operation.GetSource(1);
  1767. return new OperationResult(AggregateType.Bool, emitB(context.TypeBool(), context.Get(AggregateType.Bool, src1), context.Get(AggregateType.Bool, src2)));
  1768. }
  1769. private static OperationResult GenerateBinaryS32(
  1770. CodeGenContext context,
  1771. AstOperation operation,
  1772. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitS)
  1773. {
  1774. var src1 = operation.GetSource(0);
  1775. var src2 = operation.GetSource(1);
  1776. return new OperationResult(AggregateType.S32, emitS(context.TypeS32(), context.GetS32(src1), context.GetS32(src2)));
  1777. }
  1778. private static OperationResult GenerateBinaryU32(
  1779. CodeGenContext context,
  1780. AstOperation operation,
  1781. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitU)
  1782. {
  1783. var src1 = operation.GetSource(0);
  1784. var src2 = operation.GetSource(1);
  1785. return new OperationResult(AggregateType.U32, emitU(context.TypeU32(), context.GetU32(src1), context.GetU32(src2)));
  1786. }
  1787. private static OperationResult GenerateTernary(
  1788. CodeGenContext context,
  1789. AstOperation operation,
  1790. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitF,
  1791. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitI)
  1792. {
  1793. var src1 = operation.GetSource(0);
  1794. var src2 = operation.GetSource(1);
  1795. var src3 = operation.GetSource(2);
  1796. if (operation.Inst.HasFlag(Instruction.FP64))
  1797. {
  1798. var result = emitF(context.TypeFP64(), context.GetFP64(src1), context.GetFP64(src2), context.GetFP64(src3));
  1799. context.Decorate(result, Decoration.NoContraction);
  1800. return new OperationResult(AggregateType.FP64, result);
  1801. }
  1802. else if (operation.Inst.HasFlag(Instruction.FP32))
  1803. {
  1804. var result = emitF(context.TypeFP32(), context.GetFP32(src1), context.GetFP32(src2), context.GetFP32(src3));
  1805. context.Decorate(result, Decoration.NoContraction);
  1806. return new OperationResult(AggregateType.FP32, result);
  1807. }
  1808. else
  1809. {
  1810. return new OperationResult(AggregateType.S32, emitI(context.TypeS32(), context.GetS32(src1), context.GetS32(src2), context.GetS32(src3)));
  1811. }
  1812. }
  1813. private static OperationResult GenerateTernaryS32(
  1814. CodeGenContext context,
  1815. AstOperation operation,
  1816. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitS)
  1817. {
  1818. var src1 = operation.GetSource(0);
  1819. var src2 = operation.GetSource(1);
  1820. var src3 = operation.GetSource(2);
  1821. return new OperationResult(AggregateType.S32, emitS(
  1822. context.TypeS32(),
  1823. context.GetS32(src1),
  1824. context.GetS32(src2),
  1825. context.GetS32(src3)));
  1826. }
  1827. private static OperationResult GenerateTernaryU32(
  1828. CodeGenContext context,
  1829. AstOperation operation,
  1830. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitU)
  1831. {
  1832. var src1 = operation.GetSource(0);
  1833. var src2 = operation.GetSource(1);
  1834. var src3 = operation.GetSource(2);
  1835. return new OperationResult(AggregateType.U32, emitU(
  1836. context.TypeU32(),
  1837. context.GetU32(src1),
  1838. context.GetU32(src2),
  1839. context.GetU32(src3)));
  1840. }
  1841. private static OperationResult GenerateQuaternaryS32(
  1842. CodeGenContext context,
  1843. AstOperation operation,
  1844. Func<SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction, SpvInstruction> emitS)
  1845. {
  1846. var src1 = operation.GetSource(0);
  1847. var src2 = operation.GetSource(1);
  1848. var src3 = operation.GetSource(2);
  1849. var src4 = operation.GetSource(3);
  1850. return new OperationResult(AggregateType.S32, emitS(
  1851. context.TypeS32(),
  1852. context.GetS32(src1),
  1853. context.GetS32(src2),
  1854. context.GetS32(src3),
  1855. context.GetS32(src4)));
  1856. }
  1857. }
  1858. }