shader_spirv.h 11 KB

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