shader_spirv.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. /*
  2. * Copyright 2011-2019 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #ifndef BGFX_SHADER_SPIRV_H
  6. #define BGFX_SHADER_SPIRV_H
  7. #include <bx/readerwriter.h>
  8. BX_ERROR_RESULT(BGFX_SHADER_SPIRV_INVALID_HEADER, BX_MAKEFOURCC('S', 'H', 0, 1) );
  9. BX_ERROR_RESULT(BGFX_SHADER_SPIRV_INVALID_INSTRUCTION, BX_MAKEFOURCC('S', 'H', 0, 2) );
  10. #define SPV_CHUNK_HEADER BX_MAKEFOURCC(0x03, 0x02, 0x23, 0x07)
  11. namespace bgfx
  12. {
  13. // Reference(s):
  14. // - https://web.archive.org/web/20181126035927/https://www.khronos.org/registry/spir-v/specs/1.0/SPIRV.html
  15. //
  16. struct SpvOpcode
  17. {
  18. enum Enum
  19. {
  20. Nop,
  21. Undef,
  22. SourceContinued,
  23. Source,
  24. SourceExtension,
  25. Name,
  26. MemberName,
  27. String,
  28. Line,
  29. Invalid9,
  30. Extension,
  31. ExtInstImport,
  32. ExtInst,
  33. Invalid13,
  34. MemoryModel,
  35. EntryPoint,
  36. ExecutionMode,
  37. Capability,
  38. Invalid18,
  39. TypeVoid,
  40. TypeBool,
  41. TypeInt,
  42. TypeFloat,
  43. TypeVector,
  44. TypeMatrix,
  45. TypeImage,
  46. TypeSampler,
  47. TypeSampledImage,
  48. TypeArray,
  49. TypeRuntimeArray,
  50. TypeStruct,
  51. TypeOpaque,
  52. TypePointer,
  53. TypeFunction,
  54. TypeEvent,
  55. TypeDeviceEvent,
  56. TypeReserveId,
  57. TypeQueue,
  58. TypePipe,
  59. TypeForwardPointer,
  60. Invalid40,
  61. ConstantTrue,
  62. ConstantFalse,
  63. Constant,
  64. ConstantComposite,
  65. ConstantSampler,
  66. ConstantNull,
  67. Invalid47,
  68. SpecConstantTrue,
  69. SpecConstantFalse,
  70. SpecConstant,
  71. SpecConstantComposite,
  72. SpecConstantOp,
  73. Invalid53,
  74. Function,
  75. FunctionParameter,
  76. FunctionEnd,
  77. FunctionCall,
  78. Invalid58,
  79. Variable,
  80. ImageTexelPointer,
  81. Load,
  82. Store,
  83. CopyMemory,
  84. CopyMemorySized,
  85. AccessChain,
  86. InBoundsAccessChain,
  87. PtrAccessChain,
  88. ArrayLength,
  89. GenericPtrMemSemantics,
  90. InBoundsPtrAccessChain,
  91. Decorate,
  92. MemberDecorate,
  93. DecorationGroup,
  94. GroupDecorate,
  95. GroupMemberDecorate,
  96. Invalid76,
  97. VectorExtractDynamic,
  98. VectorInsertDynamic,
  99. VectorShuffle,
  100. CompositeConstruct,
  101. CompositeExtract,
  102. CompositeInsert,
  103. CopyObject,
  104. Transpose,
  105. Invalid85,
  106. SampledImage,
  107. ImageSampleImplicitLod,
  108. ImageSampleExplicitLod,
  109. ImageSampleDrefImplicitLod,
  110. ImageSampleDrefExplicitLod,
  111. ImageSampleProjImplicitLod,
  112. ImageSampleProjExplicitLod,
  113. ImageSampleProjDrefImplicitLod,
  114. ImageSampleProjDrefExplicitLod,
  115. ImageFetch,
  116. ImageGather,
  117. ImageDrefGather,
  118. ImageRead,
  119. ImageWrite,
  120. Image,
  121. ImageQueryFormat,
  122. ImageQueryOrder,
  123. ImageQuerySizeLod,
  124. ImageQuerySize,
  125. ImageQueryLod,
  126. ImageQueryLevels,
  127. ImageQuerySamples,
  128. Invalid108,
  129. ConvertFToU,
  130. ConvertFToS,
  131. ConvertSToF,
  132. ConvertUToF,
  133. UConvert,
  134. SConvert,
  135. FConvert,
  136. QuantizeToF16,
  137. ConvertPtrToU,
  138. SatConvertSToU,
  139. SatConvertUToS,
  140. ConvertUToPtr,
  141. PtrCastToGeneric,
  142. GenericCastToPtr,
  143. GenericCastToPtrExplicit,
  144. Bitcast,
  145. Invalid125,
  146. SNegate,
  147. FNegate,
  148. IAdd,
  149. FAdd,
  150. ISub,
  151. FSub,
  152. IMul,
  153. FMul,
  154. UDiv,
  155. SDiv,
  156. FDiv,
  157. UMod,
  158. SRem,
  159. SMod,
  160. FRem,
  161. FMod,
  162. VectorTimesScalar,
  163. MatrixTimesScalar,
  164. VectorTimesMatrix,
  165. MatrixTimesVector,
  166. MatrixTimesMatrix,
  167. OuterProduct,
  168. Dot,
  169. IAddCarry,
  170. ISubBorrow,
  171. UMulExtended,
  172. SMulExtended,
  173. Invalid153,
  174. Any,
  175. All,
  176. IsNan,
  177. IsInf,
  178. IsFinite,
  179. IsNormal,
  180. SignBitSet,
  181. LessOrGreater,
  182. Ordered,
  183. Unordered,
  184. LogicalEqual,
  185. LogicalNotEqual,
  186. LogicalOr,
  187. LogicalAnd,
  188. LogicalNot,
  189. Select,
  190. IEqual,
  191. INotEqual,
  192. UGreaterThan,
  193. SGreaterThan,
  194. UGreaterThanEqual,
  195. SGreaterThanEqual,
  196. ULessThan,
  197. SLessThan,
  198. ULessThanEqual,
  199. SLessThanEqual,
  200. FOrdEqual,
  201. FUnordEqual,
  202. FOrdNotEqual,
  203. FUnordNotEqual,
  204. FOrdLessThan,
  205. FUnordLessThan,
  206. FOrdGreaterThan,
  207. FUnordGreaterThan,
  208. FOrdLessThanEqual,
  209. FUnordLessThanEqual,
  210. FOrdGreaterThanEqual,
  211. FUnordGreaterThanEqual,
  212. Invalid192,
  213. Invalid193,
  214. ShiftRightLogical,
  215. ShiftRightArithmetic,
  216. ShiftLeftLogical,
  217. BitwiseOr,
  218. BitwiseXor,
  219. BitwiseAnd,
  220. Not,
  221. BitFieldInsert,
  222. BitFieldSExtract,
  223. BitFieldUExtract,
  224. BitReverse,
  225. BitCount,
  226. Invalid206,
  227. DPdx,
  228. DPdy,
  229. Fwidth,
  230. DPdxFine,
  231. DPdyFine,
  232. FwidthFine,
  233. DPdxCoarse,
  234. DPdyCoarse,
  235. FwidthCoarse,
  236. Invalid216,
  237. Invalid217,
  238. EmitVertex,
  239. EndPrimitive,
  240. EmitStreamVertex,
  241. EndStreamPrimitive,
  242. Invalid222,
  243. Invalid223,
  244. ControlBarrier,
  245. MemoryBarrier,
  246. Invalid226,
  247. AtomicLoad,
  248. AtomicStore,
  249. AtomicExchange,
  250. AtomicCompareExchange,
  251. AtomicCompareExchangeWeak,
  252. AtomicIIncrement,
  253. AtomicIDecrement,
  254. AtomicIAdd,
  255. AtomicISub,
  256. AtomicSMin,
  257. AtomicUMin,
  258. AtomicSMax,
  259. AtomicUMax,
  260. AtomicAnd,
  261. AtomicOr,
  262. AtomicXor,
  263. Invalid243,
  264. Invalid244,
  265. Phi,
  266. LoopMerge,
  267. SelectionMerge,
  268. Label,
  269. Branch,
  270. BranchConditional,
  271. Switch,
  272. Kill,
  273. Return,
  274. ReturnValue,
  275. Unreachable,
  276. LifetimeStart,
  277. LifetimeStop,
  278. Invalid258,
  279. GroupAsyncCopy,
  280. GroupWaitEvents,
  281. GroupAll,
  282. GroupAny,
  283. GroupBroadcast,
  284. GroupIAdd,
  285. GroupFAdd,
  286. GroupFMin,
  287. GroupUMin,
  288. GroupSMin,
  289. GroupFMax,
  290. GroupUMax,
  291. GroupSMax,
  292. Invalid272,
  293. Invalid273,
  294. ReadPipe,
  295. WritePipe,
  296. ReservedReadPipe,
  297. ReservedWritePipe,
  298. ReserveReadPipePackets,
  299. ReserveWritePipePackets,
  300. CommitReadPipe,
  301. CommitWritePipe,
  302. IsValidReserveId,
  303. GetNumPipePackets,
  304. GetMaxPipePackets,
  305. GroupReserveReadPipePackets,
  306. GroupReserveWritePipePackets,
  307. GroupCommitReadPipe,
  308. GroupCommitWritePipe,
  309. Invalid289,
  310. Invalid290,
  311. EnqueueMarker,
  312. EnqueueKernel,
  313. GetKernelNDrangeSubGroupCount,
  314. GetKernelNDrangeMaxSubGroupSize,
  315. GetKernelWorkGroupSize,
  316. GetKernelPreferredWorkGroupSizeMultiple,
  317. RetainEvent,
  318. ReleaseEvent,
  319. CreateUserEvent,
  320. IsValidEvent,
  321. SetUserEventStatus,
  322. CaptureEventProfilingInfo,
  323. GetDefaultQueue,
  324. BuildNDRange,
  325. ImageSparseSampleImplicitLod,
  326. ImageSparseSampleExplicitLod,
  327. ImageSparseSampleDrefImplicitLod,
  328. ImageSparseSampleDrefExplicitLod,
  329. ImageSparseSampleProjImplicitLod,
  330. ImageSparseSampleProjExplicitLod,
  331. ImageSparseSampleProjDrefImplicitLod,
  332. ImageSparseSampleProjDrefExplicitLod,
  333. ImageSparseFetch,
  334. ImageSparseGather,
  335. ImageSparseDrefGather,
  336. ImageSparseTexelsResident,
  337. NoLine,
  338. AtomicFlagTestAndSet,
  339. AtomicFlagClear,
  340. ImageSparseRead,
  341. Count
  342. };
  343. };
  344. const char* getName(SpvOpcode::Enum _opcode);
  345. struct SpvBuiltin
  346. {
  347. enum Enum
  348. {
  349. Position,
  350. PointSize,
  351. ClipDistance,
  352. CullDistance,
  353. VertexId,
  354. InstanceId,
  355. PrimitiveId,
  356. InvocationId,
  357. Layer,
  358. ViewportIndex,
  359. TessLevelOuter,
  360. TessLevelInner,
  361. TessCoord,
  362. PatchVertices,
  363. FragCoord,
  364. PointCoord,
  365. FrontFacing,
  366. SampleId,
  367. SamplePosition,
  368. SampleMask,
  369. FragDepth,
  370. HelperInvocation,
  371. NumWorkgroups,
  372. WorkgroupSize,
  373. WorkgroupId,
  374. LocalInvocationId,
  375. GlobalInvocationId,
  376. LocalInvocationIndex,
  377. WorkDim,
  378. GlobalSize,
  379. EnqueuedWorkgroupSize,
  380. GlobalOffset,
  381. GlobalLinearId,
  382. SubgroupSize,
  383. SubgroupMaxSize,
  384. NumSubgroups,
  385. NumEnqueuedSubgroups,
  386. SubgroupId,
  387. SubgroupLocalInvocationId,
  388. VertexIndex,
  389. InstanceIndex,
  390. Count
  391. };
  392. };
  393. const char* getName(SpvBuiltin::Enum _enum);
  394. struct SpvExecutionModel
  395. {
  396. enum Enum
  397. {
  398. Vertex,
  399. TessellationControl,
  400. TessellationEvaluation,
  401. Geometry,
  402. Fragment,
  403. GLCompute,
  404. Kernel,
  405. Count
  406. };
  407. };
  408. struct SpvAddressingModel
  409. {
  410. enum Enum
  411. {
  412. Logical,
  413. Physical32,
  414. Physical64,
  415. Count
  416. };
  417. };
  418. struct SpvMemoryModel
  419. {
  420. enum Enum
  421. {
  422. Simple,
  423. GLSL450,
  424. OpenCL,
  425. Count
  426. };
  427. };
  428. struct SpvStorageClass
  429. {
  430. enum Enum
  431. {
  432. UniformConstant,
  433. Input,
  434. Uniform,
  435. Output,
  436. Workgroup,
  437. CrossWorkgroup,
  438. Private,
  439. Function,
  440. Generic,
  441. PushConstant,
  442. AtomicCounter,
  443. Image,
  444. Count
  445. };
  446. };
  447. const char* getName(SpvStorageClass::Enum _enum);
  448. struct SpvResourceDim
  449. {
  450. enum Enum
  451. {
  452. Texture1D,
  453. Texture2D,
  454. Texture3D,
  455. TextureCube,
  456. TextureRect,
  457. Buffer,
  458. SubpassData,
  459. };
  460. };
  461. struct SpvDecoration
  462. {
  463. enum Enum
  464. {
  465. RelaxedPrecision,
  466. SpecId,
  467. Block,
  468. BufferBlock,
  469. RowMajor,
  470. ColMajor,
  471. ArrayStride,
  472. MatrixStride,
  473. GLSLShared,
  474. GLSLPacked,
  475. CPacked,
  476. BuiltIn,
  477. Unknown12,
  478. NoPerspective,
  479. Flat,
  480. Patch,
  481. Centroid,
  482. Sample,
  483. Invariant,
  484. Restrict,
  485. Aliased,
  486. Volatile,
  487. Constant,
  488. Coherent,
  489. NonWritable,
  490. NonReadable,
  491. Uniform,
  492. Unknown27,
  493. SaturatedConversion,
  494. Stream,
  495. Location,
  496. Component,
  497. Index,
  498. Binding,
  499. DescriptorSet,
  500. Offset,
  501. XfbBuffer,
  502. XfbStride,
  503. FuncParamAttr,
  504. FPRoundingMode,
  505. FPFastMathMode,
  506. LinkageAttributes,
  507. NoContraction,
  508. InputAttachmentIndex,
  509. Alignment,
  510. Count
  511. };
  512. };
  513. const char* getName(SpvDecoration::Enum _enum);
  514. struct SpvOperand
  515. {
  516. SpvOperand() { /* not pod */ }
  517. enum Enum
  518. {
  519. AccessQualifier,
  520. AddressingModel,
  521. Base,
  522. Capability,
  523. Component,
  524. ComponentType,
  525. Composite,
  526. Condition,
  527. Coordinate,
  528. Decoration,
  529. Dim,
  530. Dref,
  531. ElementType,
  532. ExecutionModel,
  533. Function,
  534. FunctionControl,
  535. Id,
  536. IdRep,
  537. ImageFormat,
  538. ImageOperands,
  539. LinkageType,
  540. LiteralNumber,
  541. LiteralRep,
  542. LiteralString,
  543. Matrix,
  544. MemoryAccess,
  545. MemoryModel,
  546. Object,
  547. Pointer,
  548. SampledType,
  549. SampledImage,
  550. SamplerAddressingMode,
  551. SamplerFilterMode,
  552. Scalar,
  553. SourceLanguage,
  554. StorageClass,
  555. StructureType,
  556. Vector,
  557. Count
  558. };
  559. Enum type;
  560. uint32_t data;
  561. stl::string literalString;
  562. };
  563. struct SpvInstruction
  564. {
  565. SpvInstruction() { /* not pod */ }
  566. SpvOpcode::Enum opcode;
  567. uint16_t length;
  568. uint16_t numOperands;
  569. uint32_t type;
  570. uint32_t result;
  571. bool hasType;
  572. bool hasResult;
  573. SpvOperand operand[32];
  574. };
  575. int32_t read(bx::ReaderI* _reader, SpvInstruction& _instruction, bx::Error* _err);
  576. int32_t write(bx::WriterI* _writer, const SpvInstruction& _instruction, bx::Error* _err);
  577. int32_t toString(char* _out, int32_t _size, const SpvInstruction& _instruction);
  578. struct SpvShader
  579. {
  580. SpvShader() { /* not pod */ }
  581. stl::vector<uint8_t> byteCode;
  582. };
  583. int32_t read(bx::ReaderSeekerI* _reader, SpvShader& _shader, bx::Error* _err);
  584. int32_t write(bx::WriterI* _writer, const SpvShader& _shader, bx::Error* _err);
  585. typedef bool (*SpvParseFn)(uint32_t _offset, const SpvInstruction& _instruction, void* _userData);
  586. void parse(const SpvShader& _src, SpvParseFn _fn, void* _userData, bx::Error* _err = NULL);
  587. typedef void (*SpvFilterFn)(SpvInstruction& _instruction, void* _userData);
  588. void filter(SpvShader& _dst, const SpvShader& _src, SpvFilterFn _fn, void* _userData, bx::Error* _err = NULL);
  589. struct SpirV
  590. {
  591. SpirV() { /* not pod */ }
  592. struct Header
  593. {
  594. uint32_t magic;
  595. uint32_t version;
  596. uint32_t generator;
  597. uint32_t bound;
  598. uint32_t schema;
  599. };
  600. Header header;
  601. SpvShader shader;
  602. };
  603. int32_t read(bx::ReaderSeekerI* _reader, SpirV& _spirv, bx::Error* _err);
  604. int32_t write(bx::WriterSeekerI* _writer, const SpirV& _spirv, bx::Error* _err);
  605. } // namespace bgfx
  606. #endif // BGFX_SHADER_SPIRV_H