.SPV
SPIR-V shader module
Introduction
SPIR-V is the Khronos Group’s binary language for shaders and compute kernels. Vulkan
requires it, OpenGL 4.6 and OpenCL accept it, and compilers produce it from GLSL, HLSL
and other shading languages. A .spv file holds one compiled module, which
the graphics driver turns into code for the particular GPU.
Software
- glslang, which compiles GLSL and HLSL to SPIR-V (Windows, macOS, Linux)
- SPIRV-Tools, with the
spirv-disdisassembler andspirv-valvalidator (Windows, macOS, Linux) - SPIRV-Cross, which turns SPIR-V back into GLSL, HLSL or Metal (Windows, macOS, Linux)
- Vulkan SDK, which bundles these tools (Windows, macOS, Linux)
- RenderDoc (Windows, Linux)
Structure
A SPIR-V module is a sequence of 32-bit words. It starts with a five-word header: the
magic number 0x07230203, the version (for example 0x00010600
for 1.6), a number identifying the tool that generated it, the bound (every ID in the
module is less than it), and a reserved word that is 0. Reading the magic number also
tells a reader the byte order; files are normally little-endian, so they start with the
bytes 03 02 23 07.
Instructions follow. The first word of each holds the instruction’s length in words in its upper 16 bits and its opcode in the lower 16, so a reader can step over any instruction it does not know. The instructions come in a fixed order of sections: capabilities, extensions, imports, the memory model, entry points, debug names, decorations, types and constants, and then the functions.
0magic0x072302031 word4version, generator, bound, reserved4 words20instructions, one after anothervaries+0word count (upper 16 bits) and opcode (lower 16 bits)1 word+4operands: result type, result ID and argumentsword count − 1 words