| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649 |
- /*
- * Copyright 2011-2016 Branimir Karadzic. All rights reserved.
- * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
- */
- #ifndef BGFX_SHADER_SPIRV_H
- #define BGFX_SHADER_SPIRV_H
- #include <bx/readerwriter.h>
- BX_ERROR_RESULT(BGFX_SHADER_SPIRV_INVALID_HEADER, BX_MAKEFOURCC('S', 'H', 0, 1) );
- BX_ERROR_RESULT(BGFX_SHADER_SPIRV_INVALID_INSTRUCTION, BX_MAKEFOURCC('S', 'H', 0, 2) );
- #define SPV_CHUNK_HEADER BX_MAKEFOURCC(0x03, 0x02, 0x23, 0x07)
- namespace bgfx
- {
- // Reference: https://www.khronos.org/registry/spir-v/specs/1.0/SPIRV.html
- struct SpvOpcode
- {
- enum Enum
- {
- Nop,
- Undef,
- SourceContinued,
- Source,
- SourceExtension,
- Name,
- MemberName,
- String,
- Line,
- Invalid9,
- Extension,
- ExtInstImport,
- ExtInst,
- Invalid13,
- MemoryModel,
- EntryPoint,
- ExecutionMode,
- Capability,
- Invalid18,
- TypeVoid,
- TypeBool,
- TypeInt,
- TypeFloat,
- TypeVector,
- TypeMatrix,
- TypeImage,
- TypeSampler,
- TypeSampledImage,
- TypeArray,
- TypeRuntimeArray,
- TypeStruct,
- TypeOpaque,
- TypePointer,
- TypeFunction,
- TypeEvent,
- TypeDeviceEvent,
- TypeReserveId,
- TypeQueue,
- TypePipe,
- TypeForwardPointer,
- Invalid40,
- ConstantTrue,
- ConstantFalse,
- Constant,
- ConstantComposite,
- ConstantSampler,
- ConstantNull,
- Invalid47,
- SpecConstantTrue,
- SpecConstantFalse,
- SpecConstant,
- SpecConstantComposite,
- SpecConstantOp,
- Invalid53,
- Function,
- FunctionParameter,
- FunctionEnd,
- FunctionCall,
- Invalid58,
- Variable,
- ImageTexelPointer,
- Load,
- Store,
- CopyMemory,
- CopyMemorySized,
- AccessChain,
- InBoundsAccessChain,
- PtrAccessChain,
- ArrayLength,
- GenericPtrMemSemantics,
- InBoundsPtrAccessChain,
- Decorate,
- MemberDecorate,
- DecorationGroup,
- GroupDecorate,
- GroupMemberDecorate,
- Invalid76,
- VectorExtractDynamic,
- VectorInsertDynamic,
- VectorShuffle,
- CompositeConstruct,
- CompositeExtract,
- CompositeInsert,
- CopyObject,
- Transpose,
- Invalid85,
- SampledImage,
- ImageSampleImplicitLod,
- ImageSampleExplicitLod,
- ImageSampleDrefImplicitLod,
- ImageSampleDrefExplicitLod,
- ImageSampleProjImplicitLod,
- ImageSampleProjExplicitLod,
- ImageSampleProjDrefImplicitLod,
- ImageSampleProjDrefExplicitLod,
- ImageFetch,
- ImageGather,
- ImageDrefGather,
- ImageRead,
- ImageWrite,
- Image,
- ImageQueryFormat,
- ImageQueryOrder,
- ImageQuerySizeLod,
- ImageQuerySize,
- ImageQueryLod,
- ImageQueryLevels,
- ImageQuerySamples,
- Invalid108,
- ConvertFToU,
- ConvertFToS,
- ConvertSToF,
- ConvertUToF,
- UConvert,
- SConvert,
- FConvert,
- QuantizeToF16,
- ConvertPtrToU,
- SatConvertSToU,
- SatConvertUToS,
- ConvertUToPtr,
- PtrCastToGeneric,
- GenericCastToPtr,
- GenericCastToPtrExplicit,
- Bitcast,
- Invalid125,
- SNegate,
- FNegate,
- IAdd,
- FAdd,
- ISub,
- FSub,
- IMul,
- FMul,
- UDiv,
- SDiv,
- FDiv,
- UMod,
- SRem,
- SMod,
- FRem,
- FMod,
- VectorTimesScalar,
- MatrixTimesScalar,
- VectorTimesMatrix,
- MatrixTimesVector,
- MatrixTimesMatrix,
- OuterProduct,
- Dot,
- IAddCarry,
- ISubBorrow,
- UMulExtended,
- SMulExtended,
- Invalid153,
- Any,
- All,
- IsNan,
- IsInf,
- IsFinite,
- IsNormal,
- SignBitSet,
- LessOrGreater,
- Ordered,
- Unordered,
- LogicalEqual,
- LogicalNotEqual,
- LogicalOr,
- LogicalAnd,
- LogicalNot,
- Select,
- IEqual,
- INotEqual,
- UGreaterThan,
- SGreaterThan,
- UGreaterThanEqual,
- SGreaterThanEqual,
- ULessThan,
- SLessThan,
- ULessThanEqual,
- SLessThanEqual,
- FOrdEqual,
- FUnordEqual,
- FOrdNotEqual,
- FUnordNotEqual,
- FOrdLessThan,
- FUnordLessThan,
- FOrdGreaterThan,
- FUnordGreaterThan,
- FOrdLessThanEqual,
- FUnordLessThanEqual,
- FOrdGreaterThanEqual,
- FUnordGreaterThanEqual,
- Invalid192,
- Invalid193,
- ShiftRightLogical,
- ShiftRightArithmetic,
- ShiftLeftLogical,
- BitwiseOr,
- BitwiseXor,
- BitwiseAnd,
- Not,
- BitFieldInsert,
- BitFieldSExtract,
- BitFieldUExtract,
- BitReverse,
- BitCount,
- Invalid206,
- DPdx,
- DPdy,
- Fwidth,
- DPdxFine,
- DPdyFine,
- FwidthFine,
- DPdxCoarse,
- DPdyCoarse,
- FwidthCoarse,
- Invalid216,
- Invalid217,
- EmitVertex,
- EndPrimitive,
- EmitStreamVertex,
- EndStreamPrimitive,
- Invalid222,
- Invalid223,
- ControlBarrier,
- MemoryBarrier,
- Invalid226,
- AtomicLoad,
- AtomicStore,
- AtomicExchange,
- AtomicCompareExchange,
- AtomicCompareExchangeWeak,
- AtomicIIncrement,
- AtomicIDecrement,
- AtomicIAdd,
- AtomicISub,
- AtomicSMin,
- AtomicUMin,
- AtomicSMax,
- AtomicUMax,
- AtomicAnd,
- AtomicOr,
- AtomicXor,
- Invalid243,
- Invalid244,
- Phi,
- LoopMerge,
- SelectionMerge,
- Label,
- Branch,
- BranchConditional,
- Switch,
- Kill,
- Return,
- ReturnValue,
- Unreachable,
- LifetimeStart,
- LifetimeStop,
- Invalid258,
- GroupAsyncCopy,
- GroupWaitEvents,
- GroupAll,
- GroupAny,
- GroupBroadcast,
- GroupIAdd,
- GroupFAdd,
- GroupFMin,
- GroupUMin,
- GroupSMin,
- GroupFMax,
- GroupUMax,
- GroupSMax,
- Invalid272,
- Invalid273,
- ReadPipe,
- WritePipe,
- ReservedReadPipe,
- ReservedWritePipe,
- ReserveReadPipePackets,
- ReserveWritePipePackets,
- CommitReadPipe,
- CommitWritePipe,
- IsValidReserveId,
- GetNumPipePackets,
- GetMaxPipePackets,
- GroupReserveReadPipePackets,
- GroupReserveWritePipePackets,
- GroupCommitReadPipe,
- GroupCommitWritePipe,
- Invalid289,
- Invalid290,
- EnqueueMarker,
- EnqueueKernel,
- GetKernelNDrangeSubGroupCount,
- GetKernelNDrangeMaxSubGroupSize,
- GetKernelWorkGroupSize,
- GetKernelPreferredWorkGroupSizeMultiple,
- RetainEvent,
- ReleaseEvent,
- CreateUserEvent,
- IsValidEvent,
- SetUserEventStatus,
- CaptureEventProfilingInfo,
- GetDefaultQueue,
- BuildNDRange,
- ImageSparseSampleImplicitLod,
- ImageSparseSampleExplicitLod,
- ImageSparseSampleDrefImplicitLod,
- ImageSparseSampleDrefExplicitLod,
- ImageSparseSampleProjImplicitLod,
- ImageSparseSampleProjExplicitLod,
- ImageSparseSampleProjDrefImplicitLod,
- ImageSparseSampleProjDrefExplicitLod,
- ImageSparseFetch,
- ImageSparseGather,
- ImageSparseDrefGather,
- ImageSparseTexelsResident,
- NoLine,
- AtomicFlagTestAndSet,
- AtomicFlagClear,
- ImageSparseRead,
- Count
- };
- };
- const char* getName(SpvOpcode::Enum _opcode);
- struct SpvBuiltin
- {
- enum Enum
- {
- Position,
- PointSize,
- ClipDistance,
- CullDistance,
- VertexId,
- InstanceId,
- PrimitiveId,
- InvocationId,
- Layer,
- ViewportIndex,
- TessLevelOuter,
- TessLevelInner,
- TessCoord,
- PatchVertices,
- FragCoord,
- PointCoord,
- FrontFacing,
- SampleId,
- SamplePosition,
- SampleMask,
- FragDepth,
- HelperInvocation,
- NumWorkgroups,
- WorkgroupSize,
- WorkgroupId,
- LocalInvocationId,
- GlobalInvocationId,
- LocalInvocationIndex,
- WorkDim,
- GlobalSize,
- EnqueuedWorkgroupSize,
- GlobalOffset,
- GlobalLinearId,
- SubgroupSize,
- SubgroupMaxSize,
- NumSubgroups,
- NumEnqueuedSubgroups,
- SubgroupId,
- SubgroupLocalInvocationId,
- VertexIndex,
- InstanceIndex,
- Count
- };
- };
- const char* getName(SpvBuiltin::Enum _enum);
- struct SpvExecutionModel
- {
- enum Enum
- {
- Vertex,
- TessellationControl,
- TessellationEvaluation,
- Geometry,
- Fragment,
- GLCompute,
- Kernel,
- Count
- };
- };
- struct SpvAddressingModel
- {
- enum Enum
- {
- Logical,
- Physical32,
- Physical64,
- Count
- };
- };
- struct SpvMemoryModel
- {
- enum Enum
- {
- Simple,
- GLSL450,
- OpenCL,
- Count
- };
- };
- struct SpvStorageClass
- {
- enum Enum
- {
- UniformConstant,
- Input,
- Uniform,
- Output,
- Workgroup,
- CrossWorkgroup,
- Private,
- Function,
- Generic,
- PushConstant,
- AtomicCounter,
- Image,
- Count
- };
- };
- const char* getName(SpvStorageClass::Enum _enum);
- struct SpvResourceDim
- {
- enum Enum
- {
- Texture1D,
- Texture2D,
- Texture3D,
- TextureCube,
- TextureRect,
- Buffer,
- SubpassData,
- };
- };
- struct SpvDecoration
- {
- enum Enum
- {
- RelaxedPrecision,
- SpecId,
- Block,
- BufferBlock,
- RowMajor,
- ColMajor,
- ArrayStride,
- MatrixStride,
- GLSLShared,
- GLSLPacked,
- CPacked,
- BuiltIn,
- Unknown12,
- NoPerspective,
- Flat,
- Patch,
- Centroid,
- Sample,
- Invariant,
- Restrict,
- Aliased,
- Volatile,
- Constant,
- Coherent,
- NonWritable,
- NonReadable,
- Uniform,
- Unknown27,
- SaturatedConversion,
- Stream,
- Location,
- Component,
- Index,
- Binding,
- DescriptorSet,
- Offset,
- XfbBuffer,
- XfbStride,
- FuncParamAttr,
- FPRoundingMode,
- FPFastMathMode,
- LinkageAttributes,
- NoContraction,
- InputAttachmentIndex,
- Alignment,
- Count
- };
- };
- const char* getName(SpvDecoration::Enum _enum);
- struct SpvOperand
- {
- SpvOperand() { /* not pod */ }
- enum Enum
- {
- AccessQualifier,
- AddressingModel,
- Base,
- Capability,
- Component,
- ComponentType,
- Composite,
- Condition,
- Coordinate,
- Decoration,
- Dim,
- Dref,
- ElementType,
- ExecutionModel,
- Function,
- FunctionControl,
- Id,
- IdRep,
- ImageFormat,
- ImageOperands,
- LinkageType,
- LiteralNumber,
- LiteralRep,
- LiteralString,
- Matrix,
- MemoryAccess,
- MemoryModel,
- Object,
- Pointer,
- SampledType,
- SampledImage,
- SamplerAddressingMode,
- SamplerFilterMode,
- Scalar,
- SourceLanguage,
- StorageClass,
- StructureType,
- Vector,
- Count
- };
- Enum type;
- uint32_t data;
- stl::string literalString;
- };
- struct SpvInstruction
- {
- SpvInstruction() { /* not pod */ }
- SpvOpcode::Enum opcode;
- uint16_t length;
- uint16_t numOperands;
- uint32_t type;
- uint32_t result;
- bool hasType;
- bool hasResult;
- SpvOperand operand[8];
- };
- int32_t read(bx::ReaderI* _reader, SpvInstruction& _instruction, bx::Error* _err);
- int32_t write(bx::WriterI* _writer, const SpvInstruction& _instruction, bx::Error* _err);
- int32_t toString(char* _out, int32_t _size, const SpvInstruction& _instruction);
- struct SpvShader
- {
- SpvShader() { /* not pod */ }
- stl::vector<uint8_t> byteCode;
- };
- int32_t read(bx::ReaderSeekerI* _reader, SpvShader& _shader, bx::Error* _err);
- int32_t write(bx::WriterI* _writer, const SpvShader& _shader, bx::Error* _err);
- typedef bool (*SpvParseFn)(uint32_t _offset, const SpvInstruction& _instruction, void* _userData);
- void parse(const SpvShader& _src, SpvParseFn _fn, void* _userData, bx::Error* _err = NULL);
- typedef void (*SpvFilterFn)(SpvInstruction& _instruction, void* _userData);
- void filter(SpvShader& _dst, const SpvShader& _src, SpvFilterFn _fn, void* _userData, bx::Error* _err = NULL);
- struct SpirV
- {
- SpirV() { /* not pod */ }
- struct Header
- {
- uint32_t magic;
- uint32_t version;
- uint32_t generator;
- uint32_t bound;
- uint32_t schema;
- };
- Header header;
- SpvShader shader;
- };
- int32_t read(bx::ReaderSeekerI* _reader, SpirV& _spirv, bx::Error* _err);
- int32_t write(bx::WriterSeekerI* _writer, const SpirV& _spirv, bx::Error* _err);
- } // namespace bgfx
- #endif // BGFX_SHADER_SPIRV_H
|