RI::createComputePipeline, RI::destroyPipeline¶
What it does¶
A pipeline is a shader compiled by the driver for the device's GPU, paired with the
resource layout it needs to run. RI::createComputePipeline(device, desc) takes the compute
shader's bytecode (SPIR-V on Vulkan, DXIL on D3D12; see
Compiling shaders) and returns a ready RI::Pipeline. Graphics pipelines are created with
createGraphicsPipeline and destroyed with the same destroyPipeline.
Step by step:
- RI reads the shader: on Vulkan it checks that the entry point is
mainand that the stage is compute; on both backends it checks that resources are accessed through the descriptor heap rather than declared at explicit registers or bindings, and reads the size of the shader's root data. - RI builds the layout from that information: on Vulkan, a
VkPipelineLayoutwith the device's resource and sampler descriptor set layouts plus a push-constant range for the root data; on D3D12, anID3D12RootSignaturewith root constants atb0, space999and direct heap-indexing flags. You never write a pipeline layout or a root signature yourself. - The driver compiles the pipeline during this call. Compiling a pipeline can take milliseconds, so create pipelines up front or on a loading thread rather than while recording commands.
pipeline.rootDataSize holds the root data size read from the shader, in bytes (0 if it
declares none).
Signature¶
// <RI/RI.hpp>, namespace RI
Pipeline createComputePipeline(Device &device, const ComputePipelineDesc &desc);
// <RI/RI.hpp>, namespace RI
struct Pipeline {
uint32 rootDataSize = 0; // bytes, from the shader (P44, P45)
// internal: backend-private state (not part of the API)
};
| Field | Type | Default | Description |
|---|---|---|---|
rootDataSize |
uint32 |
0 |
bytes, from the shader (P44, P45) |
// <RI/RI.hpp>, namespace RI
struct ComputePipelineDesc {
Bytecode shader; // entry point "main"
std::string_view name; // empty: unnamed (P106)
};
| Field | Type | Default | Description |
|---|---|---|---|
shader |
Bytecode |
entry point "main" | |
name |
std::string_view |
empty: unnamed (P106) |
From the header: Shader bytecode: SPIR-V for Vulkan, DXIL for D3D12 (P29).
Example¶
// 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;
}
const std::vector<std::byte> bytecode = loadShader("doc_compute");
RI::Pipeline pipeline = RI::createComputePipeline(device, {.shader = bytecode});
// pipeline.rootDataSize is 20: the size of Root, read from the shader.
The bytecode is only read during the call; you may free it afterwards.
The same in native code¶
// 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
What to watch for: the pipeline layout must match the shader's descriptor sets and
push-constant range. RI checks the bindings and reads the root data size from the SPIR-V
using SPIRV-Reflect. The VkShaderModule can be destroyed as soon as
vkCreateComputePipelines returns.
// 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");
What to watch for: bindless shaders require a root signature with the
D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED and
D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED flags. RI finds the root data (at
b0, space999) with its own DXIL reader, which works even on stripped shaders.
Rules and errors¶
Programmer errors, in every build, with a readable message:
- the bytecode is empty, or is not valid SPIR-V (Vulkan) or DXIL (D3D12);
- Vulkan: the shader has no entry point named
main, or is not a compute shader (on D3D12, a non-compute shader fails inCreateComputePipelineState); - the shader declares more than one root data block;
- the shader declares a resource at a register or binding (
declared resources are not supported yet; reach resources through ResourceDescriptorHeap[]); - Vulkan:
SamplerDescriptorHeapis at set 0, binding 1 (DXC's default) instead of set 1, binding 0; compile the shader with-fvk-bind-sampler-heap 0 1(binding 0, set 1).
Unsupported error, in every build: the shader's root data is larger than
device.limits.maxRootDataSize.
Destroy a pipeline only after all GPU work that uses it has finished. destroyPipeline
releases the native pipeline (unless it was imported) and resets the RI::Pipeline struct.