Skip to content

Root data

What it is

Root data is a small block of values that you pass from C++ directly in the command list for each dispatch or draw, without creating a buffer—for example, descriptor heap slots, a scale factor, or an element count. It is the fastest way to pass a few values to a shader. Natively, it maps to Vulkan's push constants and D3D12's root constants.

Declaring it in the shader

Mark the shader's root data once, with the macro EQ_ROOT_DATA(Type, name) from shaders/equine/root.hlsli:

#include <equine/root.hlsli>

// Root data: a few values set per dispatch with cmdSetRootData (P45).
// The C++ side declares the same struct.
struct Root {
    uint inputSlot;  // descriptor heap slots of the three buffers
    uint outputSlot;
    uint paramsSlot;
    float scale;
    uint count;
};
EQ_ROOT_DATA(Root, root);

The macro expands differently for each target:

Target EQ_ROOT_DATA(Root, root) becomes
SPIR-V (Vulkan) [[vk::push_constant]] ConstantBuffer<Root> root
DXIL (D3D12) ConstantBuffer<Root> root : register(b0, space999)

Register space 999 is reserved for root data; no other resource in the shader may use it. RI locates b0, space999 in the DXIL bytecode (even when reflection is stripped) and turns it into D3D12 root constants.

You do not write a byte size in C++, add a [RootSignature] attribute in HLSL, or declare root data in the pipeline description: the shader is the source of truth for the size and layout of root data. A shader may declare at most one root data block.

Setting it from C++

Write a C++ struct with the same members in the same order:

// The C++ mirror of `Root` in the shader: same members, same order.
struct Root {
    uint32 inputSlot;
    uint32 outputSlot;
    uint32 paramsSlot;
    float scale;
    uint32 count;
};

and set it after binding the pipeline and before dispatching or drawing:

RI::cmdSetHeap(cmd, heap);
RI::cmdSetPipeline(cmd, pipeline);
RI::Helpers::cmdSetRootData(cmd, Root{.inputSlot = 0, .outputSlot = 1, .paramsSlot = 2, .scale = 3.0f, .count = kCount});

RI::Helpers::cmdSetRootData(cmd, value) forwards sizeof(value) bytes to the core RI::cmdSetRootData(cmd, bytes) (which takes a std::span<const std::byte>). At compile time, the helper enforces that the struct is trivially copyable (and not a pointer or std::span), that its size is a multiple of 4 bytes, and that it does not exceed kMaxRootDataSize. The bytes are copied into the command list immediately, so you can modify or destroy the struct right after the call.

One C++ struct for both backends

SPIR-V and DXIL lay out constant buffers differently by default. RI's compile rule passes -fvk-use-dx-layout to DXC when compiling to SPIR-V, so both use D3D12's layout and one C++ struct matches both. With that layout, a struct of 32-bit scalars, as above, maps one to one. Be careful with vectors: a float3 followed by a float packs into 16 bytes, but a float3 does not start in the middle of a 16-byte row.

Under D3D12 constant-buffer packing rules, a vector never crosses a 16-byte boundary. For example, a float2 that would start at byte offset 12 is pushed to offset 16, so uint, uint, uint, float2 takes 24 bytes in HLSL but 20 bytes in C++ (if written as five 32-bit fields). Because the C++ compiler does not insert that padding automatically, either add an explicit padding member in C++ or stick to 32-bit scalars (uint, float, int), which match one to one. The size check in validation builds catches a total-size mismatch, but it cannot detect fields that are misaligned inside a struct of the same total size, so adding static_assert(sizeof(MyRoot) == ...) next to the C++ struct helps.

If the shader never reads the root data block, the compiler removes it and RI treats the shader as having no root data (rootDataSize is 0). Setting root data for such a pipeline is an error, so call cmdSetRootData only for pipelines whose shaders actually read their root data.

Rules and errors

  • Size check (validation builds): the size you pass must equal the size the shader declares (pipeline.rootDataSize). Otherwise: programmer error: root data is 8 bytes, but the pipeline's shader declares 20 bytes. Setting root data for a pipeline whose shader declares none is also an error.
  • Size limit: a shader may declare up to device.limits.maxRootDataSize bytes: 256 on D3D12 (64 root constants), and the GPU's push-constant limit on Vulkan (at least 128), capped at kMaxRootDataSize (256) on every backend. A shader that declares more than device.limits.maxRootDataSize bytes fails pipeline or work-graph creation (createComputePipeline, createGraphicsPipeline, or createWorkGraph) with an unsupported error.
  • Set the pipeline first: cmdSetRootData validates the byte count against the currently bound pipeline (pipeline.rootDataSize) and records the data for that pipeline's type (compute, graphics, or work graph).

The same in native code

RI::cmdSetHeap(cmd, heap);
RI::cmdSetPipeline(cmd, pipeline);
RI::Helpers::cmdSetRootData(cmd, Root{.inputSlot = 0, .outputSlot = 1, .paramsSlot = 2, .scale = 3.0f, .count = kCount});

The pipeline layout declares a push-constant range of exactly the root data's size:

// The layout: the heap set, plus push constants of exactly the root data's size.
const VkPushConstantRange pushRange{.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT, .offset = 0, .size = sizeof(Root)};
const VkPipelineLayoutCreateInfo pipelineLayoutInfo{.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
                                                    .setLayoutCount = 1,
                                                    .pSetLayouts = &setLayout,
                                                    .pushConstantRangeCount = 1,
                                                    .pPushConstantRanges = &pushRange};
VkPipelineLayout pipelineLayout = VK_NULL_HANDLE;
check(vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &pipelineLayout), "vkCreatePipelineLayout");

const std::vector<uint32_t> spirv = loadSpirv(shaderPath);
const VkShaderModuleCreateInfo moduleInfo{.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
                                          .codeSize = spirv.size() * 4,
                                          .pCode = spirv.data()};
VkShaderModule module = VK_NULL_HANDLE;
check(vkCreateShaderModule(device, &moduleInfo, nullptr, &module), "vkCreateShaderModule");
const VkComputePipelineCreateInfo pipelineInfo{
    .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
    .stage = {.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
              .stage = VK_SHADER_STAGE_COMPUTE_BIT,
              .module = module,
              .pName = "main"},
    .layout = pipelineLayout,
};
VkPipeline pipeline = VK_NULL_HANDLE;
check(vkCreateComputePipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &pipeline),
      "vkCreateComputePipelines");
vkDestroyShaderModule(device, module, nullptr); // not needed once the pipeline exists
const Root root{.inputSlot = 0, .outputSlot = 1, .paramsSlot = 2, .scale = 3.0f, .count = kCount};
vkCmdPushConstants(cmd, pipelineLayout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(root), &root);

The root signature declares 32-bit root constants at b0, space999:

// The root signature: root constants at b0, space999 (where EQ_ROOT_DATA puts
// the root data), and the flags that let shaders index the heaps directly.
const D3D12_ROOT_PARAMETER1 rootConstants{
    .ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS,
    .Constants = {.ShaderRegister = 0, .RegisterSpace = 999, .Num32BitValues = sizeof(Root) / 4},
    .ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL,
};
const D3D12_VERSIONED_ROOT_SIGNATURE_DESC rootDesc{
    .Version = D3D_ROOT_SIGNATURE_VERSION_1_1,
    .Desc_1_1 = {.NumParameters = 1,
                 .pParameters = &rootConstants,
                 .Flags = D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED |
                          D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED},
};
ComPtr<ID3DBlob> blob;
ComPtr<ID3DBlob> errors;
check(D3D12SerializeVersionedRootSignature(&rootDesc, &blob, &errors), "D3D12SerializeVersionedRootSignature");
ComPtr<ID3D12RootSignature> rootSignature;
check(device->CreateRootSignature(0, blob->GetBufferPointer(), blob->GetBufferSize(), IID_PPV_ARGS(&rootSignature)),
      "CreateRootSignature");

const std::vector<char> dxil = loadDxil(shaderPath);
const D3D12_COMPUTE_PIPELINE_STATE_DESC psoDesc{
    .pRootSignature = rootSignature.Get(),
    .CS = {.pShaderBytecode = dxil.data(), .BytecodeLength = dxil.size()},
};
ComPtr<ID3D12PipelineState> pipeline;
check(device->CreateComputePipelineState(&psoDesc, IID_PPV_ARGS(&pipeline)), "CreateComputePipelineState");
const Root root{.inputSlot = 0, .outputSlot = 1, .paramsSlot = 2, .scale = 3.0f, .count = kCount};
cmd->SetComputeRoot32BitConstants(0, sizeof(root) / 4, &root, 0);