20161117 - Mechanics of Vulkan SPIR-V Specialization
References
SPIR-V |
Vulkan
SPIR-V Background
- SPIR-V is a single linear stream of variable length instructions.
- Each instruction starts with a 32-bit packed word {MSB: 16-bit WordCount, LSB: 16-bit Opcode}.
- The WordCount enables unknown Opcode to be ignored.
- OpSpecConstant* instructions used to define a specialization constant.
- OpSpecConstantOp builds a new specializaton constant from doing an operation.
- OpSpecConstantOp does not support complex math like sin() and cos().
- OpSpecConstant* instructions have a Result ID and sometimes a default Value.
- OpDecorate with SpecId associates VkSpecializationMapEntry.constantID with a Result ID.
- OpSpecConstant* gets reduced to OpConstant* instructions at PSO creation time.
Human readable SPIR-V example.
%1 = OpTypeInt 32 1 ; declare a signed 32-bit type
OpDecorate %2 SpecId 3 ; set 'constantID = 3'
%2 = OpSpecConstant %1 -8 ; some signed 32-bit integer constant
Vulkan Background
Showing below the related structures which show how to set specialization constants.
typedef struct VkSpecializationMapEntry {
uint32_t constantID; // SpecId in SPIR-V source.
uint32_t offset; // Byte offset in VkSpecializationInfo.pData for value.
size_t size;
} VkSpecializationMapEntry;
// Passed into VkPipelineShaderStageCreateInfo
typedef struct VkSpecializationInfo {
uint32_t mapEntryCount;
const VkSpecializationMapEntry* pMapEntries;
size_t dataSize;
const void* pData;
} VkSpecializationInfo;