Skip to content

Compiling shaders for RI

RI takes bytecode

RI does not compile shaders: you give it bytecode, SPIR-V on Vulkan and DXIL on D3D12. Shaders are written in HLSL and compiled for both backends with one compiler, DXC (DirectX Shader Compiler). To compile at run time, use the shader compiler helper.

// RI takes bytecode only (P29): SPIR-V on Vulkan, DXIL on D3D12.
std::vector<std::byte> loadShader(const std::string &name) {
#if EQUINE_BACKEND_VULKAN
    const std::string path = std::string(EQUINE_SHADER_DIR) + "/" + name + ".spv";
#else
    const std::string path = std::string(EQUINE_SHADER_DIR) + "/" + name + ".dxil";
#endif
    std::ifstream file(path, std::ios::binary);
    const std::vector<char> bytes{std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()};
    std::vector<std::byte> out(bytes.size());
    std::memcpy(out.data(), bytes.data(), bytes.size());
    return out;
}

The compile rule

Shaders for RI must be compiled with these DXC options:

Option Target Why
-T cs_6_6 (or newer) both shader profile (cs_6_6, vs_6_6, ps_6_6, ...): shader model 6.6 or newer, for ResourceDescriptorHeap[] (bindless)
-E main both RI's entry point is main
-HV 2021 both compile as HLSL 2021
-I <repository>/shaders both finds equine/root.hlsli (root data)
-spirv -fspv-target-env=vulkan1.3 SPIR-V emit Vulkan 1.3 SPIR-V
-fvk-use-dx-layout SPIR-V use D3D12's buffer layout so one C++ struct fits both backends
-fvk-bind-sampler-heap 0 1 SPIR-V place SamplerDescriptorHeap in set 1, binding 0, where RI's heap binds it
-fvk-support-nonzero-base-vertex SPIR-V make SV_VertexID leave out the first vertex and vertex offset, as on D3D12

Nothing is needed for DXIL beyond the common options. RI reads what it needs from DXIL with its own reader, even with reflection and debug information stripped (-Qstrip_reflect -Qstrip_debug).

By hand:

dxc -T cs_6_6 -E main -HV 2021 -I shaders -spirv -fspv-target-env=vulkan1.3 \
    -fvk-use-dx-layout -fvk-bind-sampler-heap 0 1 -fvk-support-nonzero-base-vertex \
    -Fo my_shader.spv my_shader.hlsl
dxc -T cs_6_6 -E main -HV 2021 -I shaders -Fo my_shader.dxil my_shader.hlsl

In CMake

The build downloads one pinned DXC release (verified by SHA-256) at configure time. That release includes the DXIL signer (dxil.dll / libdxil.so), so DXIL compiled on Linux loads on D3D12 on Windows. equine_add_shaders compiles each file to both .spv and .dxil, with the rule above:

equine_add_shaders(my_app PROFILE cs_6_6 SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/my_shader.hlsl")

The compiled files are written to <build>/shaders/, and EQUINE_SHADER_DIR is defined on the target with that path. equine_add_shaders is always available when RI is configured (including from a parent CMake project).

HLSL that works on both backends

Three HLSL features compile on both targets but behave differently unless you follow these rules:

  • Pass values from the vertex to the pixel shader in one struct: see below.
  • Matrices in buffers are column-major. A float4x4 in a constant or structured buffer is read column by column unless declared row_major, on both backends (because -fvk-use-dx-layout gives SPIR-V D3D12's layout). With mul(matrix, vector), store the matrix column by column from C++ (GLM's layout), or declare it row_major float4x4 if your C++ matrices are stored row by row. A wrong choice is silent: the image is just wrong.
  • Clip space matches D3D12 on both backends: y points up, and depth runs from 0 (near) to 1 (far). Build projection matrices for a 0-to-1 depth range (with GLM, define GLM_FORCE_DEPTH_ZERO_TO_ONE), not OpenGL's -1-to-1 range (rendering).

Values passed to the pixel shader

The vertex shader (or mesh shader) outputs a position and per-vertex attributes such as a color, texture coordinates, or a normal. With tessellation or a geometry shader, the last of those stages (geometry, else domain) plays this part: "the vertex shader" below means the stage just before the pixel shader. The GPU interpolates them across each triangle and gives them to the pixel shader as its inputs. Each backend pairs outputs and inputs its own way:

  • Vulkan numbers the values (their location) in the order they are declared, leaving out SV_Position, and pairs equal numbers.
  • D3D12 places the values in registers, in the order they are declared, SV_Position included, and pairs equal registers.

Neither pairs them by name. So the rules are:

  1. Use one struct. Return it from the vertex shader and take the same struct in the pixel shader. This always works.
  2. A pixel shader that declares its own inputs lists the same values in the same order, SV_Position included, and may leave out only values at the end.
  3. Leaving out a value in the middle breaks both backends. If the vertex shader outputs uv then normal and the pixel shader takes only normal, Vulkan gives it the uv (silently: the image is just wrong) and D3D12 refuses the pipeline.

Values the GPU generates for the pixel shader (such as SV_IsFrontFace or SV_SampleIndex) are not outputs of the preceding stage and may be declared in any order.

In validation builds, createGraphicsPipeline checks rules 2 and 3 on both backends, matching values by semantic:

programmer error: createGraphicsPipeline: the pixel shader reads NORMAL0 at location 0, but the
vertex shader writes it at location 1: ...; pass them in one struct (P101)

One case is visible only on D3D12: a pixel shader that leaves out SV_Position at the start while taking the values after it. Vulkan has no location for SV_Position, so it works there, and D3D12 refuses it. The check relies on the names DXC gives the values in SPIR-V; shaders from another compiler are not checked on Vulkan.

What RI checks at pipeline creation

createComputePipeline and createGraphicsPipeline inspect each shader's bytecode and report a readable programmer error when:

  • the bytecode is not valid SPIR-V (Vulkan backend) or not valid DXIL (D3D12 backend);
  • on Vulkan, there is no entry point named main, or the shader stage does not match the pipeline stage (on D3D12, the D3D12 runtime checks stage compatibility when creating the pipeline state);
  • a shader declares more than one root data block (or, on D3D12, declares anything other than one b0 constant buffer in the reserved space999);
  • a shader declares a resource at a register or binding instead of using the descriptor heap (declared resources are not supported yet; reach resources through ResourceDescriptorHeap[]);
  • on Vulkan, SamplerDescriptorHeap is at set 0, binding 1 (DXC's default): the message tells you to add -fvk-bind-sampler-heap 0 1;
  • on Vulkan, a shader reads SV_VertexID and was compiled without -fvk-support-nonzero-base-vertex;
  • in createGraphicsPipeline, two stages declare different root data sizes.

Root data larger than the device allows (device.limits.maxRootDataSize) is an unsupported error (Root data).