Skip to content

RI::cmdExecuteIndirect, RI::cmdExecuteIndirectWithRootData

What it does

Indirect execution runs draws or dispatches whose arguments were written into a buffer by the GPU—for example, a culling pass deciding what to draw without a round trip to the CPU.

  • Level 1, available on every GPU: RI::cmdExecuteIndirect reads records of kind from args starting at offset. When count is nullptr, it executes maxCount records; when count is provided, the GPU reads a uint32 count from count at countOffset and executes at most maxCount records.
  • Level 2, available when device.limits.indirectRootData is true: RI::cmdExecuteIndirectWithRootData lets the GPU update root data between draws or dispatches (for example, a per-draw material or object index). Each record starts with the bound pipeline's root data, followed by the arguments. Level 2 may require scratch memory for the driver: RI::indirectScratchRequirements(device, pipeline, kind, maxCount) returns the required size, alignment, and allowed memory kinds, which you supply as a range in a buffer created with BufferUsage::eIndirectScratch. When the returned size is 0 (which is always the case on D3D12), scratch may be nullptr.

IndirectKind and the argument structs (which have the same layout on both backends):

Kind Record arguments Requires
eDraw DrawIndirectArgs (vertex count, instance count, first vertex, first instance) a graphics pipeline, inside a rendering section
eDrawIndexed DrawIndexedIndirectArgs (index count, instance count, first index, vertex offset, first instance) a graphics pipeline and an index buffer, inside a rendering section
eDrawMesh DispatchIndirectArgs (task or mesh groups) a mesh pipeline, inside a rendering section
eDispatch DispatchIndirectArgs (groups) a compute pipeline, outside rendering sections

RI::indirectStride(kind, rootDataSize) returns the byte stride from one record to the next: the size of the arguments for kind, preceded at Level 2 by rootDataSize rounded up to a multiple of 4 bytes.

Signature

// <RI/RI.hpp>, namespace RI
void cmdExecuteIndirect(CommandList &cmd, IndirectKind kind, const Buffer &args, uint64 offset, uint32 maxCount,
                        const Buffer *count = nullptr, uint64 countOffset = 0);

From the header: Indirect execution (P48). Level 1: maxCount records of indirectStride(kind) bytes at offset in args, with the count read from count at countOffset when given (at most maxCount). Dispatches take one record and no count. Buffers need BufferUsage::eIndirect; the GPU reads them at Stage::eIndirect.

// <RI/RI.hpp>, namespace RI
void cmdExecuteIndirectWithRootData(CommandList &cmd, IndirectKind kind, const Buffer &records, uint64 offset,
                                    uint32 maxCount, const Buffer *scratch, uint64 scratchOffset,
                                    const Buffer *count = nullptr, uint64 countOffset = 0);
// <RI/RI.hpp>, namespace RI
MemoryRequirements indirectScratchRequirements(Device &device, const Pipeline &pipeline, IndirectKind kind,
                                               uint32 maxCount);
  • D3D12 header: Level 2 (DeviceLimits::indirectRootData): each record starts with the root data of the pipeline set (its rootDataSize, rounded up to 4), followed by the arguments; stride indirectStride(kind, rootDataSize). Set root data again before later direct draws. scratch (BufferUsage::eIndirectScratch) holds indirectScratchRequirements(...).size bytes at scratchOffset; it may be null when that size is 0 (always, on D3D12). Calls not separated by a barrier on Stage::eIndirect must use separate scratch ranges.
  • Vulkan header: Level 2 (DeviceLimits::indirectRootData): each record starts with the root data of the pipeline set (its rootDataSize, rounded up to 4), followed by the arguments; stride indirectStride(kind, rootDataSize). Set root data again before later direct draws. scratch (BufferUsage::eIndirectScratch) holds indirectScratchRequirements(...).size bytes at scratchOffset; it may be null when that size is 0. Calls not separated by a barrier on Stage::eIndirect must use separate scratch ranges.
// <RI/RI.hpp>, namespace RI
enum class IndirectKind : uint8 { eDraw, eDrawIndexed, eDrawMesh, eDispatch };

From the header: Indirect execution (P48): what the GPU-written arguments describe. Each argument struct has the same layout on both backends.

Value Meaning
eDraw
eDrawIndexed
eDrawMesh
eDispatch
// <RI/RI.hpp>, namespace RI
constexpr uint32 indirectStride(IndirectKind kind, uint32 rootDataSize = 0);

From the header: Bytes from one indirect record to the next (P48): the arguments of kind, preceded at Level 2 by rootDataSize bytes of root data (rounded up to 4).

Example

A Level 2 record for an indexed draw whose shader declares DrawRoot as its root data:

struct DrawRecord {
    DrawRoot root;                     // the pipeline's root data
    RI::DrawIndexedIndirectArgs args;
};
static_assert(sizeof(DrawRecord) == RI::indirectStride(RI::IndirectKind::eDrawIndexed, sizeof(DrawRoot)));

// Once, when the pipeline and maxDraws are known: the driver's scratch, if any.
const RI::MemoryRequirements need =
    RI::indirectScratchRequirements(device, pipeline, RI::IndirectKind::eDrawIndexed, maxDraws);
RI::Memory scratchMemory;
RI::Buffer scratch;
if (need.size > 0) {
    // Memory that suits the scratch (some drivers allow only some GPU memory types).
    scratchMemory = RI::allocateMemory(device, RI::MemoryKind::eGPU, need.size, &need);
    scratch = RI::createBuffer(device, scratchMemory, 0,
                               {.size = need.size, .usage = RI::BufferUsage::eIndirectScratch});
}

// While recording:
RI::cmdSetPipeline(cmd, pipeline);
RI::cmdSetIndexBuffer(cmd, indices, 0, RI::IndexFormat::eUint32);
RI::cmdExecuteIndirectWithRootData(cmd, RI::IndirectKind::eDrawIndexed, records, 0, maxDraws,
                                   need.size > 0 ? &scratch : nullptr, 0, &drawCount, 0);

How RI does it

  • Vulkan: Level 1 calls vkCmdDraw*Indirect, vkCmdDraw*IndirectCount, vkCmdDrawMeshTasksIndirect(Count)EXT, and vkCmdDispatchIndirect. Level 2 uses VK_EXT_device_generated_commands (enabled on Vulkan 1.4 devices that support it, except on NVIDIA in validation builds with GPU-assisted validation): RI builds an indirect commands layout with a push-constant token followed by the draw or dispatch token, and executes it with vkCmdExecuteGeneratedCommandsEXT for the bound pipeline. The driver's preprocess buffer is your scratch range: its size comes from vkGetGeneratedCommandsMemoryRequirementsEXT for the pipeline, kind, and maxCount, and BufferUsage::eIndirectScratch adds the buffer's preprocess usage bit. RI allocates nothing itself.
  • D3D12: Both levels call ExecuteIndirect with command signatures that RI creates and caches: the draw or dispatch alone (Level 1), or root constants (root parameter 0) followed by the draw or dispatch (Level 2). Level 2 is always available on D3D12.

Rules and errors

  • The argument buffer and any count buffer must have BufferUsage::eIndirect, offset and countOffset must be multiples of 4, and all maxCount records (plus the 4-byte uint32 count, if used) must fit within their buffers (programmer error in validation builds).
  • Level 1 dispatches (IndirectKind::eDispatch with cmdExecuteIndirect) must have maxCount == 1 and count == nullptr, because Vulkan has no indirect dispatch count command (programmer error in validation builds). Level 2 dispatches (cmdExecuteIndirectWithRootData) may use both maxCount > 1 and a count buffer.
  • Draw calls (eDraw, eDrawIndexed, and eDrawMesh) must be recorded inside a rendering section (cmdBeginRendering / cmdEndRendering), while dispatches (eDispatch) must be recorded outside any rendering section (programmer error in validation builds).
  • Indirect dispatches (eDispatch) and mesh draws (eDrawMesh) must still respect the portable group limits: at most RI::kMaxGroupsPerDimension (65,535) groups per dimension, and at most RI::kMaxMeshGroups (2^22) total groups per mesh draw. Because the arguments live in a GPU buffer, RI cannot check them on the CPU.
  • The GPU reads the argument, record, and count buffers during Stage::eIndirect. Place a barrier from the stage that wrote them to Stage::eIndirect before calling an indirect command—for example, cmdBarrier(cmd, Stage::eCompute, Stage::eIndirect).
  • After calling cmdExecuteIndirectWithRootData, set root data again before any subsequent direct draws or dispatches, because Vulkan leaves push constants undefined after vkCmdExecuteGeneratedCommandsEXT.
  • Calling indirectScratchRequirements or cmdExecuteIndirectWithRootData, or creating a buffer with BufferUsage::eIndirectScratch, on a device where device.limits.indirectRootData is false is reported as unsupported.
  • When indirectScratchRequirements returns a non-zero size, scratch must be non-null and created with BufferUsage::eIndirectScratch, scratchOffset plus that size must fit in the buffer, and the buffer address at scratchOffset must be aligned to the returned alignment (programmer error in validation builds otherwise). Two cmdExecuteIndirectWithRootData calls that are not separated by a barrier on Stage::eIndirect must not share a scratch range, because the GPU may execute them concurrently; calls in order on the same queue with a Stage::eIndirect barrier between them, or in different frames, may reuse the same range.
  • Level 1 has no portable per-draw identifier: Vulkan has DrawIndex, whereas D3D12 has no draw-ID built-in. To pass a per-draw index portably, use Level 2 root data or store the index in firstInstance.