Skip to content

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

// <RI/RI.hpp>, namespace RI
void cmdDispatch(CommandList &cmd, uint32 x, uint32 y, uint32 z);

From the header: Outside rendering sections.

Example

RI::cmdDispatch(cmd, (kCount + 63) / 64, 1, 1); // 64 threads per group, see [numthreads]

The same in native code

RI::cmdDispatch(cmd, (kCount + 63) / 64, 1, 1); // 64 threads per group, see [numthreads]
vkCmdDispatch(cmd, (kCount + 63) / 64, 1, 1);
cmd->Dispatch((kCount + 63) / 64, 1, 1);

Rules and errors

  • Each dimension (x, y, and z) is at most RI::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 larger x counts, 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 with RI::Helpers::groupGrid (groupGrid).
  • Indirect dispatches (cmdExecuteIndirect) must follow the same kMaxGroupsPerDimension limit; because their arguments live in a GPU buffer, RI cannot check them on the CPU.
  • Call cmdDispatch on the device thread (the thread that called createDevice); 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.