RI::cmdDispatch¶
What it does¶
RI::cmdDispatch(cmd, x, y, z) records a command to run the current compute
pipeline on a 3D grid of x * y * z thread groups. Each group has the
thread count the shader declares with [numthreads(...)], so the total number of threads is
x * y * z multiplied by that group size.
To cover n items with 64-thread groups, round up to (n + 63) / 64 groups. Because rounding
up can launch extra threads past n, the shader checks its thread index against the item count
and returns early:
[numthreads(64, 1, 1)]
void main(uint3 id : SV_DispatchThreadID) {
if (id.x >= root.count) {
return;
}
// Bindless: fetch each buffer from the heap by the slot passed in root data.
StructuredBuffer<float> input = ResourceDescriptorHeap[root.inputSlot];
RWStructuredBuffer<float> output = ResourceDescriptorHeap[root.outputSlot];
ConstantBuffer<Params> params = ResourceDescriptorHeap[root.paramsSlot];
output[id.x] = input[id.x] * root.scale + params.offset;
}
Call cmdDispatch outside rendering sections, after setting the
descriptor heap, the compute pipeline, and the root data (if the shader declares any). If the
shader reads data that an earlier command wrote, place a barrier before the
dispatch.
Signature¶
From the header: Outside rendering sections.
Example¶
The same in native code¶
Rules and errors¶
- Each dimension (
x,y, andz) is at mostRI::kMaxGroupsPerDimension(65,535) groups on every backend. That matches D3D12's per-dimension limit, which Vulkan guarantees as a minimum. Although Vulkan GPUs often accept largerxcounts, RI still rejects any dimension above 65,535 in validation builds so a program tested on Vulkan does not break on D3D12. Exceeding this limit reports a programmer error:cmdDispatch: 65536 x 1 x 1 groups; each dimension is at most 65535 on every backend (kMaxGroupsPerDimension); fold the count into a grid (Helpers::groupGrid). To dispatch more than 65,535 groups, fold the count into rows withRI::Helpers::groupGrid(groupGrid). - Indirect dispatches (
cmdExecuteIndirect) must follow the samekMaxGroupsPerDimensionlimit; because their arguments live in a GPU buffer, RI cannot check them on the CPU. - Call
cmdDispatchon the device thread (the thread that calledcreateDevice); validation builds report calls from other threads as a programmer error. - Beyond the group count and thread checks, RI records the native dispatch command directly; the native validation layers report mistakes such as a missing pipeline or dispatching inside a rendering section.