shader_spirv.cpp 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. /*
  2. * Copyright 2011-2024 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #include "bgfx_p.h"
  6. #include "shader_spirv.h"
  7. namespace bgfx
  8. {
  9. #define SPV_OPERAND_1(_a0) SpvOperand::_a0
  10. #define SPV_OPERAND_2(_a0, _a1) SPV_OPERAND_1(_a0), SPV_OPERAND_1(_a1)
  11. #define SPV_OPERAND_3(_a0, _a1, _a2) SPV_OPERAND_1(_a0), SPV_OPERAND_2(_a1, _a2)
  12. #define SPV_OPERAND_4(_a0, _a1, _a2, _a3) SPV_OPERAND_1(_a0), SPV_OPERAND_3(_a1, _a2, _a3)
  13. #define SPV_OPERAND_5(_a0, _a1, _a2, _a3, _a4) SPV_OPERAND_1(_a0), SPV_OPERAND_4(_a1, _a2, _a3, _a4)
  14. #define SPV_OPERAND_6(_a0, _a1, _a2, _a3, _a4, _a5) SPV_OPERAND_1(_a0), SPV_OPERAND_5(_a1, _a2, _a3, _a4, _a5)
  15. #define SPV_OPERAND_7(_a0, _a1, _a2, _a3, _a4, _a5, _a6) SPV_OPERAND_1(_a0), SPV_OPERAND_6(_a1, _a2, _a3, _a4, _a5, _a6)
  16. #define SPV_OPERAND_8(_a0, _a1, _a2, _a3, _a4, _a5, _a6, _a7) SPV_OPERAND_1(_a0), SPV_OPERAND_7(_a1, _a2, _a3, _a4, _a5, _a6, _a7)
  17. #define SPV_OPERAND_9(_a0, _a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8) SPV_OPERAND_1(_a0), SPV_OPERAND_8(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8)
  18. #define SPV_OPERAND(...) { BX_MACRO_DISPATCHER(SPV_OPERAND_, __VA_ARGS__)(__VA_ARGS__) }
  19. #define _ Count
  20. bool isDebug(SpvOpcode::Enum _opcode)
  21. {
  22. return (SpvOpcode::SourceContinued <= _opcode && SpvOpcode::Line >= _opcode)
  23. || SpvOpcode::NoLine == _opcode
  24. ;
  25. }
  26. struct SpvOpcodeInfo
  27. {
  28. bool hasType;
  29. bool hasResult;
  30. SpvOperand::Enum operands[8];
  31. };
  32. static const SpvOpcodeInfo s_spvOpcodeInfo[] =
  33. {
  34. { false, false, /* Nop, // 0 */ SPV_OPERAND(_) },
  35. { true, true, /* Undef, // 1 */ SPV_OPERAND(_) },
  36. { false, false, /* SourceContinued, // 2 */ SPV_OPERAND(_) },
  37. { false, false, /* Source, // 3 */ SPV_OPERAND(SourceLanguage, LiteralNumber, Id, LiteralString) },
  38. { false, false, /* SourceExtension, // 4 */ SPV_OPERAND(LiteralString) },
  39. { false, true, /* Name, // 5 */ SPV_OPERAND(LiteralString) },
  40. { false, true, /* MemberName, // 6 */ SPV_OPERAND(LiteralNumber, LiteralString) },
  41. { false, false, /* String, // 7 */ SPV_OPERAND(_) },
  42. { false, false, /* Line, // 8 */ SPV_OPERAND(_) },
  43. { false, false, /* ------------------------------- Invalid9 // 9 */ SPV_OPERAND(_) },
  44. { false, false, /* Extension, // 10 */ SPV_OPERAND(LiteralString) },
  45. { false, true, /* ExtInstImport, // 11 */ SPV_OPERAND(LiteralString) },
  46. { true, true, /* ExtInst, // 12 */ SPV_OPERAND(LiteralNumber) },
  47. { false, false, /* ------------------------------ Invalid13 // 13 */ SPV_OPERAND(_) },
  48. { false, false, /* MemoryModel, // 14 */ SPV_OPERAND(AddressingModel, MemoryModel) },
  49. { false, false, /* EntryPoint, // 15 */ SPV_OPERAND(ExecutionModel, Id, LiteralString) },
  50. { true, true, /* ExecutionMode, // 16 */ SPV_OPERAND(_) },
  51. { false, false, /* Capability, // 17 */ SPV_OPERAND(Capability ) },
  52. { false, false, /* ------------------------------ Invalid18 // 18 */ SPV_OPERAND(_) },
  53. { false, true, /* TypeVoid, // 19 */ SPV_OPERAND(LiteralNumber) },
  54. { false, true, /* TypeBool, // 20 */ SPV_OPERAND(LiteralNumber) },
  55. { false, true, /* TypeInt, // 21 */ SPV_OPERAND(LiteralNumber) },
  56. { false, true, /* TypeFloat, // 22 */ SPV_OPERAND(LiteralNumber) },
  57. { false, true, /* TypeVector, // 23 */ SPV_OPERAND(ComponentType, LiteralNumber) },
  58. { false, true, /* TypeMatrix, // 24 */ SPV_OPERAND(LiteralNumber) },
  59. { false, true, /* TypeImage, // 25 */ SPV_OPERAND(SampledType, Dim, LiteralNumber, LiteralNumber, LiteralNumber, LiteralNumber, ImageFormat, AccessQualifier) },
  60. { false, true, /* TypeSampler, // 26 */ SPV_OPERAND(LiteralNumber) },
  61. { false, true, /* TypeSampledImage, // 27 */ SPV_OPERAND(LiteralNumber) },
  62. { false, true, /* TypeArray, // 28 */ SPV_OPERAND(ElementType, LiteralNumber) },
  63. { false, true, /* TypeRuntimeArray, // 29 */ SPV_OPERAND(ElementType) },
  64. { false, true, /* TypeStruct, // 30 */ SPV_OPERAND(IdRep) },
  65. { false, true, /* TypeOpaque, // 31 */ SPV_OPERAND(LiteralString) },
  66. { false, true, /* TypePointer, // 32 */ SPV_OPERAND(StorageClass, Id) },
  67. { false, true, /* TypeFunction, // 33 */ SPV_OPERAND(Id, Id, Id, Id, Id) },
  68. { false, true, /* TypeEvent, // 34 */ SPV_OPERAND(_) },
  69. { false, true, /* TypeDeviceEvent, // 35 */ SPV_OPERAND(_) },
  70. { false, true, /* TypeReserveId, // 36 */ SPV_OPERAND(_) },
  71. { false, true, /* TypeQueue, // 37 */ SPV_OPERAND(_) },
  72. { false, true, /* TypePipe, // 38 */ SPV_OPERAND(_) },
  73. { false, true, /* TypeForwardPointer, // 39 */ SPV_OPERAND(_) },
  74. { false, false, /* ------------------------------ Invalid40 // 40 */ SPV_OPERAND(_) },
  75. { true, true, /* ConstantTrue, // 41 */ SPV_OPERAND(_) },
  76. { true, true, /* ConstantFalse, // 42 */ SPV_OPERAND(_) },
  77. { true, true, /* Constant, // 43 */ SPV_OPERAND(LiteralRep) },
  78. { true, true, /* ConstantComposite, // 44 */ SPV_OPERAND(LiteralRep) },
  79. { true, true, /* ConstantSampler, // 45 */ SPV_OPERAND(SamplerAddressingMode, LiteralNumber, SamplerFilterMode) },
  80. { true, true, /* ConstantNull, // 46 */ SPV_OPERAND(_) },
  81. { false, false, /* ------------------------------ Invalid47 // 47 */ SPV_OPERAND(_) },
  82. { true, true, /* SpecConstantTrue, // 48 */ SPV_OPERAND(_) },
  83. { true, true, /* SpecConstantFalse, // 49 */ SPV_OPERAND(_) },
  84. { true, true, /* SpecConstant, // 50 */ SPV_OPERAND(_) },
  85. { true, true, /* SpecConstantComposite, // 51 */ SPV_OPERAND(_) },
  86. { true, true, /* SpecConstantOp, // 52 */ SPV_OPERAND(_) },
  87. { false, false, /* ------------------------------ Invalid53 // 53 */ SPV_OPERAND(_) },
  88. { true, true, /* Function, // 54 */ SPV_OPERAND(FunctionControl, Id) },
  89. { true, true, /* FunctionParameter, // 55 */ SPV_OPERAND(_) },
  90. { false, false, /* FunctionEnd, // 56 */ SPV_OPERAND(_) },
  91. { true, true, /* FunctionCall, // 57 */ SPV_OPERAND(Function, IdRep) },
  92. { false, false, /* ------------------------------ Invalid58 // 58 */ SPV_OPERAND(_) },
  93. { true, true, /* Variable, // 59 */ SPV_OPERAND(StorageClass, Id) },
  94. { true, true, /* ImageTexelPointer, // 60 */ SPV_OPERAND(_) },
  95. { true, true, /* Load, // 61 */ SPV_OPERAND(Pointer, MemoryAccess) },
  96. { false, false, /* Store, // 62 */ SPV_OPERAND(Pointer, Object, MemoryAccess) },
  97. { false, false, /* CopyMemory, // 63 */ SPV_OPERAND(_) },
  98. { false, false, /* CopyMemorySized, // 64 */ SPV_OPERAND(_) },
  99. { true, true, /* AccessChain, // 65 */ SPV_OPERAND(Base, IdRep) },
  100. { true, true, /* InBoundsAccessChain, // 66 */ SPV_OPERAND(Base, IdRep) },
  101. { true, true, /* PtrAccessChain, // 67 */ SPV_OPERAND(_) },
  102. { true, true, /* ArrayLength, // 68 */ SPV_OPERAND(_) },
  103. { true, true, /* GenericPtrMemSemantics, // 69 */ SPV_OPERAND(_) },
  104. { true, true, /* InBoundsPtrAccessChain, // 70 */ SPV_OPERAND(_) },
  105. { false, false, /* Decorate, // 71 */ SPV_OPERAND(Id, Decoration, LiteralRep) },
  106. { false, false, /* MemberDecorate, // 72 */ SPV_OPERAND(StructureType, LiteralNumber, Decoration, LiteralRep) },
  107. { false, false, /* DecorationGroup, // 73 */ SPV_OPERAND(_) },
  108. { false, false, /* GroupDecorate, // 74 */ SPV_OPERAND(_) },
  109. { false, false, /* GroupMemberDecorate, // 75 */ SPV_OPERAND(_) },
  110. { false, false, /* ------------------------------ Invalid76 // 76 */ SPV_OPERAND(_) },
  111. { true, true, /* VectorExtractDynamic, // 77 */ SPV_OPERAND(_) },
  112. { true, true, /* VectorInsertDynamic, // 78 */ SPV_OPERAND(_) },
  113. { true, true, /* VectorShuffle, // 79 */ SPV_OPERAND(Id, Id, LiteralRep) },
  114. { true, true, /* CompositeConstruct, // 80 */ SPV_OPERAND(IdRep) },
  115. { true, true, /* CompositeExtract, // 81 */ SPV_OPERAND(Composite, LiteralRep) },
  116. { true, true, /* CompositeInsert, // 82 */ SPV_OPERAND(Id, Composite, LiteralRep) },
  117. { true, true, /* CopyObject, // 83 */ SPV_OPERAND(Id) },
  118. { true, true, /* Transpose, // 84 */ SPV_OPERAND(Matrix) },
  119. { false, false, /* ------------------------------ Invalid85 // 85 */ SPV_OPERAND(_) },
  120. { true, true, /* SampledImage, // 86 */ SPV_OPERAND(_) },
  121. { true, true, /* ImageSampleImplicitLod, // 87 */ SPV_OPERAND(SampledImage, Coordinate, ImageOperands, IdRep) },
  122. { true, true, /* ImageSampleExplicitLod, // 88 */ SPV_OPERAND(SampledImage, Coordinate, ImageOperands, Id, IdRep) },
  123. { true, true, /* ImageSampleDrefImplicitLod, // 89 */ SPV_OPERAND(_) },
  124. { true, true, /* ImageSampleDrefExplicitLod, // 90 */ SPV_OPERAND(_) },
  125. { true, true, /* ImageSampleProjImplicitLod, // 91 */ SPV_OPERAND(_) },
  126. { true, true, /* ImageSampleProjExplicitLod, // 92 */ SPV_OPERAND(_) },
  127. { true, true, /* ImageSampleProjDrefImplicitLod, // 93 */ SPV_OPERAND(_) },
  128. { true, true, /* ImageSampleProjDrefExplicitLod, // 94 */ SPV_OPERAND(_) },
  129. { true, true, /* ImageFetch, // 95 */ SPV_OPERAND(_) },
  130. { true, true, /* ImageGather, // 96 */ SPV_OPERAND(SampledImage, Coordinate, Component, ImageOperands, IdRep) },
  131. { true, true, /* ImageDrefGather, // 97 */ SPV_OPERAND(SampledImage, Coordinate, Dref, ImageOperands, IdRep) },
  132. { true, true, /* ImageRead, // 98 */ SPV_OPERAND(_) },
  133. { false, false, /* ImageWrite, // 99 */ SPV_OPERAND(_) },
  134. { true, true, /* Image, // 100 */ SPV_OPERAND(_) },
  135. { true, true, /* ImageQueryFormat, // 101 */ SPV_OPERAND(_) },
  136. { true, true, /* ImageQueryOrder, // 102 */ SPV_OPERAND(_) },
  137. { true, true, /* ImageQuerySizeLod, // 103 */ SPV_OPERAND(_) },
  138. { true, true, /* ImageQuerySize, // 104 */ SPV_OPERAND(_) },
  139. { true, true, /* ImageQueryLod, // 105 */ SPV_OPERAND(_) },
  140. { true, true, /* ImageQueryLevels, // 106 */ SPV_OPERAND(_) },
  141. { true, true, /* ImageQuerySamples, // 107 */ SPV_OPERAND(_) },
  142. { false, false, /* ----------------------------- Invalid108 // 108 */ SPV_OPERAND(_) },
  143. { true, true, /* ConvertFToU, // 109 */ SPV_OPERAND(_) },
  144. { true, true, /* ConvertFToS, // 110 */ SPV_OPERAND(_) },
  145. { true, true, /* ConvertSToF, // 111 */ SPV_OPERAND(_) },
  146. { true, true, /* ConvertUToF, // 112 */ SPV_OPERAND(_) },
  147. { true, true, /* UConvert, // 113 */ SPV_OPERAND(Id) },
  148. { true, true, /* SConvert, // 114 */ SPV_OPERAND(Id) },
  149. { true, true, /* FConvert, // 115 */ SPV_OPERAND(Id) },
  150. { true, true, /* QuantizeToF16, // 116 */ SPV_OPERAND(_) },
  151. { true, true, /* ConvertPtrToU, // 117 */ SPV_OPERAND(_) },
  152. { true, true, /* SatConvertSToU, // 118 */ SPV_OPERAND(_) },
  153. { true, true, /* SatConvertUToS, // 119 */ SPV_OPERAND(_) },
  154. { true, true, /* ConvertUToPtr, // 120 */ SPV_OPERAND(_) },
  155. { true, true, /* PtrCastToGeneric, // 121 */ SPV_OPERAND(_) },
  156. { true, true, /* GenericCastToPtr, // 122 */ SPV_OPERAND(_) },
  157. { true, true, /* GenericCastToPtrExplicit, // 123 */ SPV_OPERAND(_) },
  158. { true, true, /* Bitcast, // 124 */ SPV_OPERAND(_) },
  159. { false, false, /* ----------------------------- Invalid125 // 125 */ SPV_OPERAND(_) },
  160. { true, true, /* SNegate, // 126 */ SPV_OPERAND(Id) },
  161. { true, true, /* FNegate, // 127 */ SPV_OPERAND(Id) },
  162. { true, true, /* IAdd, // 128 */ SPV_OPERAND(Id, Id) },
  163. { true, true, /* FAdd, // 129 */ SPV_OPERAND(Id, Id) },
  164. { true, true, /* ISub, // 130 */ SPV_OPERAND(Id, Id) },
  165. { true, true, /* FSub, // 131 */ SPV_OPERAND(Id, Id) },
  166. { true, true, /* IMul, // 132 */ SPV_OPERAND(Id, Id) },
  167. { true, true, /* FMul, // 133 */ SPV_OPERAND(Id, Id) },
  168. { true, true, /* UDiv, // 134 */ SPV_OPERAND(Id, Id) },
  169. { true, true, /* SDiv, // 135 */ SPV_OPERAND(Id, Id) },
  170. { true, true, /* FDiv, // 136 */ SPV_OPERAND(Id, Id) },
  171. { true, true, /* UMod, // 137 */ SPV_OPERAND(Id, Id) },
  172. { true, true, /* SRem, // 138 */ SPV_OPERAND(Id, Id) },
  173. { true, true, /* SMod, // 139 */ SPV_OPERAND(Id, Id) },
  174. { true, true, /* FRem, // 140 */ SPV_OPERAND(Id, Id) },
  175. { true, true, /* FMod, // 141 */ SPV_OPERAND(Id, Id) },
  176. { true, true, /* VectorTimesScalar, // 142 */ SPV_OPERAND(Vector, Scalar) },
  177. { true, true, /* MatrixTimesScalar, // 143 */ SPV_OPERAND(Matrix, Scalar) },
  178. { true, true, /* VectorTimesMatrix, // 144 */ SPV_OPERAND(Vector, Matrix) },
  179. { true, true, /* MatrixTimesVector, // 145 */ SPV_OPERAND(Matrix, Vector) },
  180. { true, true, /* MatrixTimesMatrix, // 146 */ SPV_OPERAND(Matrix, Matrix) },
  181. { true, true, /* OuterProduct, // 147 */ SPV_OPERAND(Vector, Vector) },
  182. { true, true, /* Dot, // 148 */ SPV_OPERAND(Vector, Vector) },
  183. { true, true, /* IAddCarry, // 149 */ SPV_OPERAND(Id, Id) },
  184. { true, true, /* ISubBorrow, // 150 */ SPV_OPERAND(Id, Id) },
  185. { true, true, /* UMulExtended, // 151 */ SPV_OPERAND(Id, Id) },
  186. { true, true, /* SMulExtended, // 152 */ SPV_OPERAND(Id, Id) },
  187. { false, false, /* ----------------------------- Invalid153 // 153 */ SPV_OPERAND(_) },
  188. { true, true, /* Any, // 154 */ SPV_OPERAND(Vector) },
  189. { true, true, /* All, // 155 */ SPV_OPERAND(Vector) },
  190. { true, true, /* IsNan, // 156 */ SPV_OPERAND(Id) },
  191. { true, true, /* IsInf, // 157 */ SPV_OPERAND(Id) },
  192. { true, true, /* IsFinite, // 158 */ SPV_OPERAND(Id) },
  193. { true, true, /* IsNormal, // 159 */ SPV_OPERAND(Id) },
  194. { true, true, /* SignBitSet, // 160 */ SPV_OPERAND(Id) },
  195. { true, true, /* LessOrGreater, // 161 */ SPV_OPERAND(Id, Id) },
  196. { true, true, /* Ordered, // 162 */ SPV_OPERAND(Id, Id) },
  197. { true, true, /* Unordered, // 163 */ SPV_OPERAND(_) },
  198. { true, true, /* LogicalEqual, // 164 */ SPV_OPERAND(_) },
  199. { true, true, /* LogicalNotEqual, // 165 */ SPV_OPERAND(_) },
  200. { true, true, /* LogicalOr, // 166 */ SPV_OPERAND(_) },
  201. { true, true, /* LogicalAnd, // 167 */ SPV_OPERAND(_) },
  202. { true, true, /* LogicalNot, // 168 */ SPV_OPERAND(_) },
  203. { true, true, /* Select, // 169 */ SPV_OPERAND(Condition, Id, Id) },
  204. { true, true, /* IEqual, // 170 */ SPV_OPERAND(_) },
  205. { true, true, /* INotEqual, // 171 */ SPV_OPERAND(_) },
  206. { true, true, /* UGreaterThan, // 172 */ SPV_OPERAND(_) },
  207. { true, true, /* SGreaterThan, // 173 */ SPV_OPERAND(_) },
  208. { true, true, /* UGreaterThanEqual, // 174 */ SPV_OPERAND(_) },
  209. { true, true, /* SGreaterThanEqual, // 175 */ SPV_OPERAND(_) },
  210. { true, true, /* ULessThan, // 176 */ SPV_OPERAND(_) },
  211. { true, true, /* SLessThan, // 177 */ SPV_OPERAND(_) },
  212. { true, true, /* ULessThanEqual, // 178 */ SPV_OPERAND(_) },
  213. { true, true, /* SLessThanEqual, // 179 */ SPV_OPERAND(_) },
  214. { true, true, /* FOrdEqual, // 180 */ SPV_OPERAND(_) },
  215. { true, true, /* FUnordEqual, // 181 */ SPV_OPERAND(_) },
  216. { true, true, /* FOrdNotEqual, // 182 */ SPV_OPERAND(_) },
  217. { true, true, /* FUnordNotEqual, // 183 */ SPV_OPERAND(_) },
  218. { true, true, /* FOrdLessThan, // 184 */ SPV_OPERAND(_) },
  219. { true, true, /* FUnordLessThan, // 185 */ SPV_OPERAND(_) },
  220. { true, true, /* FOrdGreaterThan, // 186 */ SPV_OPERAND(_) },
  221. { true, true, /* FUnordGreaterThan, // 187 */ SPV_OPERAND(_) },
  222. { true, true, /* FOrdLessThanEqual, // 188 */ SPV_OPERAND(_) },
  223. { true, true, /* FUnordLessThanEqual, // 189 */ SPV_OPERAND(_) },
  224. { true, true, /* FOrdGreaterThanEqual, // 190 */ SPV_OPERAND(_) },
  225. { true, true, /* FUnordGreaterThanEqual, // 191 */ SPV_OPERAND(_) },
  226. { false, false, /* ----------------------------- Invalid192 // 192 */ SPV_OPERAND(_) },
  227. { false, false, /* ----------------------------- Invalid193 // 193 */ SPV_OPERAND(_) },
  228. { true, true, /* ShiftRightLogical, // 194 */ SPV_OPERAND(_) },
  229. { true, true, /* ShiftRightArithmetic, // 195 */ SPV_OPERAND(_) },
  230. { true, true, /* ShiftLeftLogical, // 196 */ SPV_OPERAND(_) },
  231. { true, true, /* BitwiseOr, // 197 */ SPV_OPERAND(_) },
  232. { true, true, /* BitwiseXor, // 198 */ SPV_OPERAND(_) },
  233. { true, true, /* BitwiseAnd, // 199 */ SPV_OPERAND(_) },
  234. { true, true, /* Not, // 200 */ SPV_OPERAND(_) },
  235. { true, true, /* BitFieldInsert, // 201 */ SPV_OPERAND(_) },
  236. { true, true, /* BitFieldSExtract, // 202 */ SPV_OPERAND(_) },
  237. { true, true, /* BitFieldUExtract, // 203 */ SPV_OPERAND(_) },
  238. { true, true, /* BitReverse, // 204 */ SPV_OPERAND(_) },
  239. { true, true, /* BitCount, // 205 */ SPV_OPERAND(_) },
  240. { false, false, /* ----------------------------- Invalid206 // 206 */ SPV_OPERAND(_) },
  241. { true, true, /* DPdx, // 207 */ SPV_OPERAND(_) },
  242. { true, true, /* DPdy, // 208 */ SPV_OPERAND(_) },
  243. { true, true, /* Fwidth, // 209 */ SPV_OPERAND(_) },
  244. { true, true, /* DPdxFine, // 210 */ SPV_OPERAND(_) },
  245. { true, true, /* DPdyFine, // 211 */ SPV_OPERAND(_) },
  246. { true, true, /* FwidthFine, // 212 */ SPV_OPERAND(_) },
  247. { true, true, /* DPdxCoarse, // 213 */ SPV_OPERAND(_) },
  248. { true, true, /* DPdyCoarse, // 214 */ SPV_OPERAND(_) },
  249. { true, true, /* FwidthCoarse, // 215 */ SPV_OPERAND(_) },
  250. { false, false, /* ----------------------------- Invalid216 // 216 */ SPV_OPERAND(_) },
  251. { false, false, /* ----------------------------- Invalid217 // 217 */ SPV_OPERAND(_) },
  252. { false, false, /* EmitVertex, // 218 */ SPV_OPERAND(_) },
  253. { false, false, /* EndPrimitive, // 219 */ SPV_OPERAND(_) },
  254. { false, false, /* EmitStreamVertex, // 220 */ SPV_OPERAND(_) },
  255. { false, false, /* EndStreamPrimitive, // 221 */ SPV_OPERAND(_) },
  256. { false, false, /* ----------------------------- Invalid222 // 222 */ SPV_OPERAND(_) },
  257. { false, false, /* ----------------------------- Invalid223 // 223 */ SPV_OPERAND(_) },
  258. { false, false, /* ControlBarrier, // 224 */ SPV_OPERAND(_) },
  259. { false, false, /* MemoryBarrier, // 225 */ SPV_OPERAND(_) },
  260. { false, false, /* ----------------------------- Invalid226 // 226 */ SPV_OPERAND(_) },
  261. { true, true, /* AtomicLoad, // 227 */ SPV_OPERAND(_) },
  262. { false, false, /* AtomicStore, // 228 */ SPV_OPERAND(_) },
  263. { true, true, /* AtomicExchange, // 229 */ SPV_OPERAND(_) },
  264. { true, true, /* AtomicCompareExchange, // 230 */ SPV_OPERAND(_) },
  265. { true, true, /* AtomicCompareExchangeWeak, // 231 */ SPV_OPERAND(_) },
  266. { true, true, /* AtomicIIncrement, // 232 */ SPV_OPERAND(_) },
  267. { true, true, /* AtomicIDecrement, // 233 */ SPV_OPERAND(_) },
  268. { true, true, /* AtomicIAdd, // 234 */ SPV_OPERAND(_) },
  269. { true, true, /* AtomicISub, // 235 */ SPV_OPERAND(_) },
  270. { true, true, /* AtomicSMin, // 236 */ SPV_OPERAND(_) },
  271. { true, true, /* AtomicUMin, // 237 */ SPV_OPERAND(_) },
  272. { true, true, /* AtomicSMax, // 238 */ SPV_OPERAND(_) },
  273. { true, true, /* AtomicUMax, // 239 */ SPV_OPERAND(_) },
  274. { true, true, /* AtomicAnd, // 240 */ SPV_OPERAND(_) },
  275. { true, true, /* AtomicOr, // 241 */ SPV_OPERAND(_) },
  276. { true, true, /* AtomicXor, // 242 */ SPV_OPERAND(_) },
  277. { false, false, /* ----------------------------- Invalid243 // 243 */ SPV_OPERAND(_) },
  278. { false, false, /* ----------------------------- Invalid244 // 244 */ SPV_OPERAND(_) },
  279. { true, true, /* Phi, // 245 */ SPV_OPERAND(_) },
  280. { false, false, /* LoopMerge, // 246 */ SPV_OPERAND(_) },
  281. { false, false, /* SelectionMerge, // 247 */ SPV_OPERAND(_) },
  282. { false, true, /* Label, // 248 */ SPV_OPERAND(_) },
  283. { false, false, /* Branch, // 249 */ SPV_OPERAND(Id) },
  284. { false, false, /* BranchConditional, // 250 */ SPV_OPERAND(Condition, Id, Id, LiteralRep) },
  285. { false, false, /* Switch, // 251 */ SPV_OPERAND(_) },
  286. { false, false, /* Kill, // 252 */ SPV_OPERAND(_) },
  287. { false, false, /* Return, // 253 */ SPV_OPERAND(_) },
  288. { false, false, /* ReturnValue, // 254 */ SPV_OPERAND(Id) },
  289. { false, false, /* Unreachable, // 255 */ SPV_OPERAND(_) },
  290. { false, false, /* LifetimeStart, // 256 */ SPV_OPERAND(_) },
  291. { false, false, /* LifetimeStop, // 257 */ SPV_OPERAND(_) },
  292. { false, false, /* ----------------------------- Invalid258 // 258 */ SPV_OPERAND(_) },
  293. { true, true, /* GroupAsyncCopy, // 259 */ SPV_OPERAND(_) },
  294. { false, false, /* GroupWaitEvents, // 260 */ SPV_OPERAND(_) },
  295. { true, true, /* GroupAll, // 261 */ SPV_OPERAND(_) },
  296. { true, true, /* GroupAny, // 262 */ SPV_OPERAND(_) },
  297. { true, true, /* GroupBroadcast, // 263 */ SPV_OPERAND(_) },
  298. { true, true, /* GroupIAdd, // 264 */ SPV_OPERAND(_) },
  299. { true, true, /* GroupFAdd, // 265 */ SPV_OPERAND(_) },
  300. { true, true, /* GroupFMin, // 266 */ SPV_OPERAND(_) },
  301. { true, true, /* GroupUMin, // 267 */ SPV_OPERAND(_) },
  302. { true, true, /* GroupSMin, // 268 */ SPV_OPERAND(_) },
  303. { true, true, /* GroupFMax, // 269 */ SPV_OPERAND(_) },
  304. { true, true, /* GroupUMax, // 270 */ SPV_OPERAND(_) },
  305. { true, true, /* GroupSMax, // 271 */ SPV_OPERAND(_) },
  306. { false, false, /* ----------------------------- Invalid272 // 272 */ SPV_OPERAND(_) },
  307. { false, false, /* ----------------------------- Invalid273 // 273 */ SPV_OPERAND(_) },
  308. { true, true, /* ReadPipe, // 274 */ SPV_OPERAND(_) },
  309. { true, true, /* WritePipe, // 275 */ SPV_OPERAND(_) },
  310. { true, true, /* ReservedReadPipe, // 276 */ SPV_OPERAND(_) },
  311. { true, true, /* ReservedWritePipe, // 277 */ SPV_OPERAND(_) },
  312. { true, true, /* ReserveReadPipePackets, // 278 */ SPV_OPERAND(_) },
  313. { true, true, /* ReserveWritePipePackets, // 279 */ SPV_OPERAND(_) },
  314. { false, false, /* CommitReadPipe, // 280 */ SPV_OPERAND(_) },
  315. { false, false, /* CommitWritePipe, // 281 */ SPV_OPERAND(_) },
  316. { true, true, /* IsValidReserveId, // 282 */ SPV_OPERAND(_) },
  317. { true, true, /* GetNumPipePackets, // 283 */ SPV_OPERAND(_) },
  318. { true, true, /* GetMaxPipePackets, // 284 */ SPV_OPERAND(_) },
  319. { true, true, /* GroupReserveReadPipePackets, // 285 */ SPV_OPERAND(_) },
  320. { true, true, /* GroupReserveWritePipePackets, // 286 */ SPV_OPERAND(_) },
  321. { false, false, /* GroupCommitReadPipe, // 287 */ SPV_OPERAND(_) },
  322. { false, false, /* GroupCommitWritePipe, // 288 */ SPV_OPERAND(_) },
  323. { false, false, /* ----------------------------- Invalid289 // 289 */ SPV_OPERAND(_) },
  324. { false, false, /* ----------------------------- Invalid290 // 290 */ SPV_OPERAND(_) },
  325. { true, true, /* EnqueueMarker, // 291 */ SPV_OPERAND(_) },
  326. { true, true, /* EnqueueKernel, // 292 */ SPV_OPERAND(_) },
  327. { true, true, /* GetKernelNDrangeSubGroupCount, // 293 */ SPV_OPERAND(_) },
  328. { true, true, /* GetKernelNDrangeMaxSubGroupSize, // 294 */ SPV_OPERAND(_) },
  329. { true, true, /* GetKernelWorkGroupSize, // 295 */ SPV_OPERAND(_) },
  330. { true, true, /* GetKernelPreferredWorkGroupSizeMultiple, // 296 */ SPV_OPERAND(_) },
  331. { false, false, /* RetainEvent, // 297 */ SPV_OPERAND(_) },
  332. { false, false, /* ReleaseEvent, // 298 */ SPV_OPERAND(_) },
  333. { true, true, /* CreateUserEvent, // 299 */ SPV_OPERAND(_) },
  334. { true, true, /* IsValidEvent, // 300 */ SPV_OPERAND(_) },
  335. { false, false, /* SetUserEventStatus, // 301 */ SPV_OPERAND(_) },
  336. { false, false, /* CaptureEventProfilingInfo, // 302 */ SPV_OPERAND(_) },
  337. { true, true, /* GetDefaultQueue, // 303 */ SPV_OPERAND(_) },
  338. { true, true, /* BuildNDRange, // 304 */ SPV_OPERAND(_) },
  339. { true, true, /* ImageSparseSampleImplicitLod, // 305 */ SPV_OPERAND(_) },
  340. { true, true, /* ImageSparseSampleExplicitLod, // 306 */ SPV_OPERAND(_) },
  341. { true, true, /* ImageSparseSampleDrefImplicitLod, // 307 */ SPV_OPERAND(_) },
  342. { true, true, /* ImageSparseSampleDrefExplicitLod, // 308 */ SPV_OPERAND(_) },
  343. { true, true, /* ImageSparseSampleProjImplicitLod, // 309 */ SPV_OPERAND(_) },
  344. { true, true, /* ImageSparseSampleProjExplicitLod, // 310 */ SPV_OPERAND(_) },
  345. { true, true, /* ImageSparseSampleProjDrefImplicitLod, // 311 */ SPV_OPERAND(_) },
  346. { true, true, /* ImageSparseSampleProjDrefExplicitLod, // 312 */ SPV_OPERAND(_) },
  347. { true, true, /* ImageSparseFetch, // 313 */ SPV_OPERAND(_) },
  348. { true, true, /* ImageSparseGather, // 314 */ SPV_OPERAND(_) },
  349. { true, true, /* ImageSparseDrefGather, // 315 */ SPV_OPERAND(_) },
  350. { true, true, /* ImageSparseTexelsResident, // 316 */ SPV_OPERAND(_) },
  351. { false, false, /* NoLine, // 317 */ SPV_OPERAND(_) },
  352. { true, true, /* AtomicFlagTestAndSet, // 318 */ SPV_OPERAND(_) },
  353. { false, false, /* AtomicFlagClear, // 319 */ SPV_OPERAND(_) },
  354. { true, true, /* ImageSparseRead, // 320 */ SPV_OPERAND(_) },
  355. };
  356. BX_STATIC_ASSERT(BX_COUNTOF(s_spvOpcodeInfo) == SpvOpcode::Count);
  357. const char* s_spvOpcode[] =
  358. {
  359. "Nop",
  360. "Undef",
  361. "SourceContinued",
  362. "Source",
  363. "SourceExtension",
  364. "Name",
  365. "MemberName",
  366. "String",
  367. "Line",
  368. "Invalid9",
  369. "Extension",
  370. "ExtInstImport",
  371. "ExtInst",
  372. "Invalid13",
  373. "MemoryModel",
  374. "EntryPoint",
  375. "ExecutionMode",
  376. "Capability",
  377. "Invalid18",
  378. "TypeVoid",
  379. "TypeBool",
  380. "TypeInt",
  381. "TypeFloat",
  382. "TypeVector",
  383. "TypeMatrix",
  384. "TypeImage",
  385. "TypeSampler",
  386. "TypeSampledImage",
  387. "TypeArray",
  388. "TypeRuntimeArray",
  389. "TypeStruct",
  390. "TypeOpaque",
  391. "TypePointer",
  392. "TypeFunction",
  393. "TypeEvent",
  394. "TypeDeviceEvent",
  395. "TypeReserveId",
  396. "TypeQueue",
  397. "TypePipe",
  398. "TypeForwardPointer",
  399. "Invalid40",
  400. "ConstantTrue",
  401. "ConstantFalse",
  402. "Constant",
  403. "ConstantComposite",
  404. "ConstantSampler",
  405. "ConstantNull",
  406. "Invalid47",
  407. "SpecConstantTrue",
  408. "SpecConstantFalse",
  409. "SpecConstant",
  410. "SpecConstantComposite",
  411. "SpecConstantOp",
  412. "Invalid53",
  413. "Function",
  414. "FunctionParameter",
  415. "FunctionEnd",
  416. "FunctionCall",
  417. "Invalid58",
  418. "Variable",
  419. "ImageTexelPointer",
  420. "Load",
  421. "Store",
  422. "CopyMemory",
  423. "CopyMemorySized",
  424. "AccessChain",
  425. "InBoundsAccessChain",
  426. "PtrAccessChain",
  427. "ArrayLength",
  428. "GenericPtrMemSemantics",
  429. "InBoundsPtrAccessChain",
  430. "Decorate",
  431. "MemberDecorate",
  432. "DecorationGroup",
  433. "GroupDecorate",
  434. "GroupMemberDecorate",
  435. "Invalid76",
  436. "VectorExtractDynamic",
  437. "VectorInsertDynamic",
  438. "VectorShuffle",
  439. "CompositeConstruct",
  440. "CompositeExtract",
  441. "CompositeInsert",
  442. "CopyObject",
  443. "Transpose",
  444. "Invalid85",
  445. "SampledImage",
  446. "ImageSampleImplicitLod",
  447. "ImageSampleExplicitLod",
  448. "ImageSampleDrefImplicitLod",
  449. "ImageSampleDrefExplicitLod",
  450. "ImageSampleProjImplicitLod",
  451. "ImageSampleProjExplicitLod",
  452. "ImageSampleProjDrefImplicitLod",
  453. "ImageSampleProjDrefExplicitLod",
  454. "ImageFetch",
  455. "ImageGather",
  456. "ImageDrefGather",
  457. "ImageRead",
  458. "ImageWrite",
  459. "Image",
  460. "ImageQueryFormat",
  461. "ImageQueryOrder",
  462. "ImageQuerySizeLod",
  463. "ImageQuerySize",
  464. "ImageQueryLod",
  465. "ImageQueryLevels",
  466. "ImageQuerySamples",
  467. "Invalid108",
  468. "ConvertFToU",
  469. "ConvertFToS",
  470. "ConvertSToF",
  471. "ConvertUToF",
  472. "UConvert",
  473. "SConvert",
  474. "FConvert",
  475. "QuantizeToF16",
  476. "ConvertPtrToU",
  477. "SatConvertSToU",
  478. "SatConvertUToS",
  479. "ConvertUToPtr",
  480. "PtrCastToGeneric",
  481. "GenericCastToPtr",
  482. "GenericCastToPtrExplicit",
  483. "Bitcast",
  484. "Invalid125",
  485. "SNegate",
  486. "FNegate",
  487. "IAdd",
  488. "FAdd",
  489. "ISub",
  490. "FSub",
  491. "IMul",
  492. "FMul",
  493. "UDiv",
  494. "SDiv",
  495. "FDiv",
  496. "UMod",
  497. "SRem",
  498. "SMod",
  499. "FRem",
  500. "FMod",
  501. "VectorTimesScalar",
  502. "MatrixTimesScalar",
  503. "VectorTimesMatrix",
  504. "MatrixTimesVector",
  505. "MatrixTimesMatrix",
  506. "OuterProduct",
  507. "Dot",
  508. "IAddCarry",
  509. "ISubBorrow",
  510. "UMulExtended",
  511. "SMulExtended",
  512. "Invalid153",
  513. "Any",
  514. "All",
  515. "IsNan",
  516. "IsInf",
  517. "IsFinite",
  518. "IsNormal",
  519. "SignBitSet",
  520. "LessOrGreater",
  521. "Ordered",
  522. "Unordered",
  523. "LogicalEqual",
  524. "LogicalNotEqual",
  525. "LogicalOr",
  526. "LogicalAnd",
  527. "LogicalNot",
  528. "Select",
  529. "IEqual",
  530. "INotEqual",
  531. "UGreaterThan",
  532. "SGreaterThan",
  533. "UGreaterThanEqual",
  534. "SGreaterThanEqual",
  535. "ULessThan",
  536. "SLessThan",
  537. "ULessThanEqual",
  538. "SLessThanEqual",
  539. "FOrdEqual",
  540. "FUnordEqual",
  541. "FOrdNotEqual",
  542. "FUnordNotEqual",
  543. "FOrdLessThan",
  544. "FUnordLessThan",
  545. "FOrdGreaterThan",
  546. "FUnordGreaterThan",
  547. "FOrdLessThanEqual",
  548. "FUnordLessThanEqual",
  549. "FOrdGreaterThanEqual",
  550. "FUnordGreaterThanEqual",
  551. "Invalid192",
  552. "Invalid193",
  553. "ShiftRightLogical",
  554. "ShiftRightArithmetic",
  555. "ShiftLeftLogical",
  556. "BitwiseOr",
  557. "BitwiseXor",
  558. "BitwiseAnd",
  559. "Not",
  560. "BitFieldInsert",
  561. "BitFieldSExtract",
  562. "BitFieldUExtract",
  563. "BitReverse",
  564. "BitCount",
  565. "Invalid206",
  566. "DPdx",
  567. "DPdy",
  568. "Fwidth",
  569. "DPdxFine",
  570. "DPdyFine",
  571. "FwidthFine",
  572. "DPdxCoarse",
  573. "DPdyCoarse",
  574. "FwidthCoarse",
  575. "Invalid216",
  576. "Invalid217",
  577. "EmitVertex",
  578. "EndPrimitive",
  579. "EmitStreamVertex",
  580. "Invalid222",
  581. "Invalid223",
  582. "EndStreamPrimitive",
  583. "ControlBarrier",
  584. "MemoryBarrier",
  585. "Invalid226",
  586. "AtomicLoad",
  587. "AtomicStore",
  588. "AtomicExchange",
  589. "AtomicCompareExchange",
  590. "AtomicCompareExchangeWeak",
  591. "AtomicIIncrement",
  592. "AtomicIDecrement",
  593. "AtomicIAdd",
  594. "AtomicISub",
  595. "AtomicSMin",
  596. "AtomicUMin",
  597. "AtomicSMax",
  598. "AtomicUMax",
  599. "AtomicAnd",
  600. "AtomicOr",
  601. "AtomicXor",
  602. "Invalid243",
  603. "Invalid244",
  604. "Phi",
  605. "LoopMerge",
  606. "SelectionMerge",
  607. "Label",
  608. "Branch",
  609. "BranchConditional",
  610. "Switch",
  611. "Kill",
  612. "Return",
  613. "ReturnValue",
  614. "Unreachable",
  615. "LifetimeStart",
  616. "LifetimeStop",
  617. "Invalid258",
  618. "GroupAsyncCopy",
  619. "GroupWaitEvents",
  620. "GroupAll",
  621. "GroupAny",
  622. "GroupBroadcast",
  623. "GroupIAdd",
  624. "GroupFAdd",
  625. "GroupFMin",
  626. "GroupUMin",
  627. "GroupSMin",
  628. "GroupFMax",
  629. "GroupUMax",
  630. "GroupSMax",
  631. "Invalid272",
  632. "Invalid273",
  633. "ReadPipe",
  634. "WritePipe",
  635. "ReservedReadPipe",
  636. "ReservedWritePipe",
  637. "ReserveReadPipePackets",
  638. "ReserveWritePipePackets",
  639. "CommitReadPipe",
  640. "CommitWritePipe",
  641. "IsValidReserveId",
  642. "GetNumPipePackets",
  643. "GetMaxPipePackets",
  644. "GroupReserveReadPipePackets",
  645. "GroupReserveWritePipePackets",
  646. "GroupCommitReadPipe",
  647. "GroupCommitWritePipe",
  648. "Invalid289",
  649. "Invalid290",
  650. "EnqueueMarker",
  651. "EnqueueKernel",
  652. "GetKernelNDrangeSubGroupCount",
  653. "GetKernelNDrangeMaxSubGroupSize",
  654. "GetKernelWorkGroupSize",
  655. "GetKernelPreferredWorkGroupSizeMultiple",
  656. "RetainEvent",
  657. "ReleaseEvent",
  658. "CreateUserEvent",
  659. "IsValidEvent",
  660. "SetUserEventStatus",
  661. "CaptureEventProfilingInfo",
  662. "GetDefaultQueue",
  663. "BuildNDRange",
  664. "ImageSparseSampleImplicitLod",
  665. "ImageSparseSampleExplicitLod",
  666. "ImageSparseSampleDrefImplicitLod",
  667. "ImageSparseSampleDrefExplicitLod",
  668. "ImageSparseSampleProjImplicitLod",
  669. "ImageSparseSampleProjExplicitLod",
  670. "ImageSparseSampleProjDrefImplicitLod",
  671. "ImageSparseSampleProjDrefExplicitLod",
  672. "ImageSparseFetch",
  673. "ImageSparseGather",
  674. "ImageSparseDrefGather",
  675. "ImageSparseTexelsResident",
  676. "NoLine",
  677. "AtomicFlagTestAndSet",
  678. "AtomicFlagClear",
  679. "ImageSparseRead",
  680. "",
  681. };
  682. BX_STATIC_ASSERT(BX_COUNTOF(s_spvOpcode)-1 == SpvOpcode::Count);
  683. const char* getName(SpvOpcode::Enum _opcode)
  684. {
  685. BX_WARN(_opcode <= SpvOpcode::Count, "Unknown opcode id %d.", _opcode);
  686. return _opcode <= SpvOpcode::Count
  687. ? s_spvOpcode[_opcode]
  688. : "?SpvOpcode?"
  689. ;
  690. }
  691. struct SpvDecorationInfo
  692. {
  693. SpvOperand::Enum operands[2];
  694. };
  695. static const SpvDecorationInfo s_spvDecorationInfo[] =
  696. {
  697. { /* RelaxedPrecision */ SPV_OPERAND(_) },
  698. { /* SpecId */ SPV_OPERAND(LiteralNumber) },
  699. { /* Block */ SPV_OPERAND(_) },
  700. { /* BufferBlock */ SPV_OPERAND(_) },
  701. { /* RowMajor */ SPV_OPERAND(_) },
  702. { /* ColMajor */ SPV_OPERAND(_) },
  703. { /* ArrayStride */ SPV_OPERAND(LiteralNumber) },
  704. { /* MatrixStride */ SPV_OPERAND(LiteralNumber) },
  705. { /* GLSLShared */ SPV_OPERAND(_) },
  706. { /* GLSLPacked */ SPV_OPERAND(_) },
  707. { /* CPacked */ SPV_OPERAND(_) },
  708. { /* BuiltIn */ SPV_OPERAND(LiteralNumber) },
  709. { /* Unknown12 */ SPV_OPERAND(_) },
  710. { /* NoPerspective */ SPV_OPERAND(_) },
  711. { /* Flat */ SPV_OPERAND(_) },
  712. { /* Patch */ SPV_OPERAND(_) },
  713. { /* Centroid */ SPV_OPERAND(_) },
  714. { /* Sample */ SPV_OPERAND(_) },
  715. { /* Invariant */ SPV_OPERAND(_) },
  716. { /* Restrict */ SPV_OPERAND(_) },
  717. { /* Aliased */ SPV_OPERAND(_) },
  718. { /* Volatile */ SPV_OPERAND(_) },
  719. { /* Constant */ SPV_OPERAND(_) },
  720. { /* Coherent */ SPV_OPERAND(_) },
  721. { /* NonWritable */ SPV_OPERAND(_) },
  722. { /* NonReadable */ SPV_OPERAND(_) },
  723. { /* Uniform */ SPV_OPERAND(_) },
  724. { /* Unknown27 */ SPV_OPERAND(_) },
  725. { /* SaturatedConversion */ SPV_OPERAND(_) },
  726. { /* Stream */ SPV_OPERAND(LiteralNumber) },
  727. { /* Location */ SPV_OPERAND(LiteralNumber) },
  728. { /* Component */ SPV_OPERAND(LiteralNumber) },
  729. { /* Index */ SPV_OPERAND(LiteralNumber) },
  730. { /* Binding */ SPV_OPERAND(LiteralNumber) },
  731. { /* DescriptorSet */ SPV_OPERAND(LiteralNumber) },
  732. { /* Offset */ SPV_OPERAND(LiteralNumber) },
  733. { /* XfbBuffer */ SPV_OPERAND(LiteralNumber) },
  734. { /* XfbStride */ SPV_OPERAND(LiteralNumber) },
  735. { /* FuncParamAttr */ SPV_OPERAND(_) },
  736. { /* FPRoundingMode */ SPV_OPERAND(_) },
  737. { /* FPFastMathMode */ SPV_OPERAND(_) },
  738. { /* LinkageAttributes */ SPV_OPERAND(LiteralString, LinkageType) },
  739. { /* NoContraction */ SPV_OPERAND(_) },
  740. { /* InputAttachmentIndex */ SPV_OPERAND(LiteralNumber) },
  741. { /* Alignment */ SPV_OPERAND(LiteralNumber) },
  742. };
  743. static const char* s_spvDecoration[] =
  744. {
  745. "RelaxedPrecision",
  746. "SpecId",
  747. "Block",
  748. "BufferBlock",
  749. "RowMajor",
  750. "ColMajor",
  751. "ArrayStride",
  752. "MatrixStride",
  753. "GLSLShared",
  754. "GLSLPacked",
  755. "CPacked",
  756. "BuiltIn",
  757. "Unknown12",
  758. "NoPerspective",
  759. "Flat",
  760. "Patch",
  761. "Centroid",
  762. "Sample",
  763. "Invariant",
  764. "Restrict",
  765. "Aliased",
  766. "Volatile",
  767. "Constant",
  768. "Coherent",
  769. "NonWritable",
  770. "NonReadable",
  771. "Uniform",
  772. "Unknown27",
  773. "SaturatedConversion",
  774. "Stream",
  775. "Location",
  776. "Component",
  777. "Index",
  778. "Binding",
  779. "DescriptorSet",
  780. "Offset",
  781. "XfbBuffer",
  782. "XfbStride",
  783. "FuncParamAttr",
  784. "FPRoundingMode",
  785. "FPFastMathMode",
  786. "LinkageAttributes",
  787. "NoContraction",
  788. "InputAttachmentIndex",
  789. "Alignment",
  790. ""
  791. };
  792. BX_STATIC_ASSERT(BX_COUNTOF(s_spvDecoration)-1 == SpvDecoration::Count);
  793. const char* getName(SpvDecoration::Enum _enum)
  794. {
  795. BX_UNUSED(s_spvDecorationInfo);
  796. BX_ASSERT(_enum <= SpvDecoration::Count, "Unknown decoration id %d.", _enum);
  797. return _enum <= SpvDecoration::Count
  798. ? s_spvDecoration[_enum]
  799. : "?SpvDecoration?"
  800. ;
  801. }
  802. #undef _
  803. #undef SPV_OPERAND
  804. static const char* s_spvStorageClass[] =
  805. {
  806. "UniformConstant",
  807. "Input",
  808. "Uniform",
  809. "Output",
  810. "Workgroup",
  811. "CrossWorkgroup",
  812. "Private",
  813. "Function",
  814. "Generic",
  815. "PushConstant",
  816. "AtomicCounter",
  817. "Image",
  818. ""
  819. };
  820. BX_STATIC_ASSERT(BX_COUNTOF(s_spvStorageClass)-1 == SpvStorageClass::Count);
  821. const char* getName(SpvStorageClass::Enum _enum)
  822. {
  823. BX_ASSERT(_enum <= SpvStorageClass::Count, "Unknown storage class id %d.", _enum);
  824. return _enum <= SpvStorageClass::Count
  825. ? s_spvStorageClass[_enum]
  826. : "?SpvStorageClass?"
  827. ;
  828. }
  829. static const char* s_spvBuiltin[] =
  830. {
  831. "Position",
  832. "PointSize",
  833. "ClipDistance",
  834. "CullDistance",
  835. "VertexId",
  836. "InstanceId",
  837. "PrimitiveId",
  838. "InvocationId",
  839. "Layer",
  840. "ViewportIndex",
  841. "TessLevelOuter",
  842. "TessLevelInner",
  843. "TessCoord",
  844. "PatchVertices",
  845. "FragCoord",
  846. "PointCoord",
  847. "FrontFacing",
  848. "SampleId",
  849. "SamplePosition",
  850. "SampleMask",
  851. "FragDepth",
  852. "HelperInvocation",
  853. "NumWorkgroups",
  854. "WorkgroupSize",
  855. "WorkgroupId",
  856. "LocalInvocationId",
  857. "GlobalInvocationId",
  858. "LocalInvocationIndex",
  859. "WorkDim",
  860. "GlobalSize",
  861. "EnqueuedWorkgroupSize",
  862. "GlobalOffset",
  863. "GlobalLinearId",
  864. "SubgroupSize",
  865. "SubgroupMaxSize",
  866. "NumSubgroups",
  867. "NumEnqueuedSubgroups",
  868. "SubgroupId",
  869. "SubgroupLocalInvocationId",
  870. "VertexIndex",
  871. "InstanceIndex",
  872. "",
  873. };
  874. BX_STATIC_ASSERT(BX_COUNTOF(s_spvBuiltin)-1 == SpvBuiltin::Count);
  875. const char* getName(SpvBuiltin::Enum _enum)
  876. {
  877. BX_ASSERT(_enum <= SpvBuiltin::Count, "Unknown builtin id %d.", _enum);
  878. return _enum <= SpvBuiltin::Count
  879. ? s_spvBuiltin[_enum]
  880. : "?SpvBuiltin?"
  881. ;
  882. }
  883. int32_t read(bx::ReaderI* _reader, SpvOperand& _operand, bx::Error* _err)
  884. {
  885. int32_t size = 0;
  886. uint32_t token;
  887. _operand.literalString = "";
  888. switch (_operand.type)
  889. {
  890. case SpvOperand::LiteralString:
  891. do
  892. {
  893. size += bx::read(_reader, token, _err);
  894. _operand.literalString.append( (char*)&token, (char*)&token + sizeof(token) );
  895. }
  896. while (0 != (token & 0xff000000) && _err->isOk() );
  897. break;
  898. default:
  899. size += bx::read(_reader, _operand.data, _err);
  900. break;
  901. }
  902. return size;
  903. }
  904. int32_t read(bx::ReaderI* _reader, SpvInstruction& _instruction, bx::Error* _err)
  905. {
  906. int32_t size = 0;
  907. uint32_t token;
  908. size += bx::read(_reader, token, _err);
  909. _instruction.opcode = SpvOpcode::Enum( (token & UINT32_C(0x0000ffff) ) );
  910. _instruction.length = uint16_t( (token & UINT32_C(0xffff0000) ) >> 16);
  911. if (_instruction.opcode >= SpvOpcode::Count)
  912. {
  913. BX_ERROR_SET(_err, kShaderInvalidInstruction, "SPIR-V: Invalid instruction.");
  914. return size;
  915. }
  916. if (0 == _instruction.length)
  917. {
  918. return size;
  919. }
  920. const SpvOpcodeInfo& info = s_spvOpcodeInfo[_instruction.opcode];
  921. _instruction.hasType = info.hasType;
  922. _instruction.hasResult = info.hasResult;
  923. if (info.hasType)
  924. {
  925. size += read(_reader, _instruction.type, _err);
  926. }
  927. else
  928. {
  929. _instruction.type = UINT32_MAX;
  930. }
  931. if (info.hasResult)
  932. {
  933. size += read(_reader, _instruction.result, _err);
  934. }
  935. else
  936. {
  937. _instruction.result = UINT32_MAX;
  938. }
  939. uint16_t currOp = 0;
  940. switch (_instruction.opcode)
  941. {
  942. case SpvOpcode::EntryPoint:
  943. _instruction.operand[currOp].type = info.operands[currOp];
  944. size += read(_reader, _instruction.operand[currOp++], _err);
  945. _instruction.operand[currOp].type = info.operands[currOp];
  946. size += read(_reader, _instruction.operand[currOp++], _err);
  947. _instruction.operand[currOp].type = info.operands[currOp];
  948. size += read(_reader, _instruction.operand[currOp++], _err);
  949. _instruction.operand[currOp].type = SpvOperand::Id;
  950. for (uint32_t ii = 0, num = _instruction.length - size/4; ii < num; ++ii)
  951. {
  952. size += read(_reader, _instruction.operand[currOp], _err);
  953. }
  954. break;
  955. default:
  956. for (
  957. ; size/4 != _instruction.length
  958. && _err->isOk()
  959. && currOp < BX_COUNTOF(_instruction.operand)
  960. ; ++currOp)
  961. {
  962. _instruction.operand[currOp].type = info.operands[currOp];
  963. size += read(_reader, _instruction.operand[currOp], _err);
  964. }
  965. break;
  966. }
  967. _instruction.numOperands = currOp;
  968. return size;
  969. }
  970. int32_t write(bx::WriterI* _writer, const SpvInstruction& _instruction, bx::Error* _err)
  971. {
  972. int32_t size = 0;
  973. BX_UNUSED(_writer, _instruction, _err);
  974. return size;
  975. }
  976. int32_t toString(char* _out, int32_t _size, const SpvInstruction& _instruction)
  977. {
  978. int32_t size = 0;
  979. if (_instruction.hasResult)
  980. {
  981. if (_instruction.hasType)
  982. {
  983. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  984. , " r%d.t%d = "
  985. , _instruction.result
  986. , _instruction.type
  987. );
  988. }
  989. else
  990. {
  991. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  992. , " r%d = "
  993. , _instruction.result
  994. );
  995. }
  996. }
  997. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  998. , "%s"
  999. , getName(_instruction.opcode)
  1000. );
  1001. for (uint32_t ii = 0, num = _instruction.numOperands; ii < num; ++ii)
  1002. {
  1003. const SpvOperand& operand = _instruction.operand[ii];
  1004. switch (operand.type)
  1005. {
  1006. case SpvOperand::AddressingModel:
  1007. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1008. , "%sAddressingModel(%d)"
  1009. , 0 == ii ? " " : ", "
  1010. , operand.data
  1011. );
  1012. break;
  1013. case SpvOperand::Decoration:
  1014. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1015. , "%s%s"
  1016. , 0 == ii ? " " : ", "
  1017. , getName(SpvDecoration::Enum(operand.data) )
  1018. );
  1019. break;
  1020. case SpvOperand::FunctionControl:
  1021. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1022. , "%s0x%08x"
  1023. , 0 == ii ? " " : ", "
  1024. , operand.data
  1025. );
  1026. break;
  1027. case SpvOperand::LiteralNumber:
  1028. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1029. , "%s%d"
  1030. , 0 == ii ? " " : ", "
  1031. , operand.data
  1032. );
  1033. break;
  1034. case SpvOperand::LiteralString:
  1035. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1036. , "%s%s"
  1037. , 0 == ii ? " " : ", "
  1038. , operand.literalString.c_str()
  1039. );
  1040. break;
  1041. case SpvOperand::MemoryModel:
  1042. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1043. , "%sMemoryModel(%d)"
  1044. , 0 == ii ? " " : ", "
  1045. , operand.data
  1046. );
  1047. break;
  1048. case SpvOperand::StorageClass:
  1049. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1050. , "%s%s"
  1051. , 0 == ii ? " " : ", "
  1052. , getName(SpvStorageClass::Enum(operand.data) )
  1053. );
  1054. break;
  1055. case SpvOperand::Count:
  1056. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1057. , "%s__%d__"
  1058. , 0 == ii ? " " : ", "
  1059. , operand.data
  1060. );
  1061. break;
  1062. default:
  1063. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1064. , "%sr%d"
  1065. , 0 == ii ? " " : ", "
  1066. , operand.data
  1067. );
  1068. break;
  1069. }
  1070. }
  1071. return size;
  1072. }
  1073. int32_t read(bx::ReaderSeekerI* _reader, SpvShader& _shader, bx::Error* _err)
  1074. {
  1075. int32_t size = 0;
  1076. uint32_t numBytes = uint32_t(bx::getSize(_reader) - bx::seek(_reader) );
  1077. _shader.byteCode.resize(numBytes);
  1078. size += bx::read(_reader, _shader.byteCode.data(), numBytes, _err);
  1079. return size;
  1080. }
  1081. int32_t write(bx::WriterI* _writer, const SpvShader& _shader, bx::Error* _err)
  1082. {
  1083. int32_t size = 0;
  1084. BX_UNUSED(_writer, _shader, _err);
  1085. return size;
  1086. }
  1087. #define SPIRV_MAGIC 0x07230203
  1088. int32_t read(bx::ReaderSeekerI* _reader, SpirV& _spirv, bx::Error* _err)
  1089. {
  1090. BX_ERROR_SCOPE(_err);
  1091. int32_t size = 0;
  1092. size += bx::read(_reader, _spirv.header, _err);
  1093. if (!_err->isOk()
  1094. || size != sizeof(SpirV::Header)
  1095. || _spirv.header.magic != SPIRV_MAGIC
  1096. )
  1097. {
  1098. BX_ERROR_SET(_err, kShaderInvalidHeader, "SPIR-V: Invalid header.");
  1099. return size;
  1100. }
  1101. size += read(_reader, _spirv.shader, _err);
  1102. return size;
  1103. }
  1104. int32_t write(bx::WriterSeekerI* _writer, const SpirV& _spirv, bx::Error* _err)
  1105. {
  1106. int32_t size = 0;
  1107. BX_UNUSED(_writer, _spirv, _err);
  1108. return size;
  1109. }
  1110. void parse(const SpvShader& _src, SpvParseFn _fn, void* _userData, bx::Error* _err)
  1111. {
  1112. BX_ERROR_SCOPE(_err);
  1113. uint32_t numBytes = uint32_t(_src.byteCode.size() );
  1114. bx::MemoryReader reader(_src.byteCode.data(), numBytes);
  1115. for (uint32_t token = 0, numTokens = uint32_t(_src.byteCode.size() / sizeof(uint32_t) ); token < numTokens;)
  1116. {
  1117. SpvInstruction instruction;
  1118. uint32_t size = read(&reader, instruction, _err);
  1119. if (!_err->isOk() )
  1120. {
  1121. return;
  1122. }
  1123. if (size/4 != instruction.length)
  1124. {
  1125. BX_TRACE("read %d, expected %d, %s"
  1126. , size/4
  1127. , instruction.length
  1128. , getName(instruction.opcode)
  1129. );
  1130. BX_ERROR_SET(_err, kShaderInvalidInstruction, "SPIR-V: Invalid instruction.");
  1131. return;
  1132. }
  1133. bool cont = _fn(token * sizeof(uint32_t), instruction, _userData);
  1134. if (!cont)
  1135. {
  1136. return;
  1137. }
  1138. token += instruction.length;
  1139. }
  1140. }
  1141. } // namespace bgfx