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::cmdExecuteIndirectreads records ofkindfromargsstarting atoffset. Whencountisnullptr, it executesmaxCountrecords; whencountis provided, the GPU reads auint32count fromcountatcountOffsetand executes at mostmaxCountrecords. - Level 2, available when
device.limits.indirectRootDataistrue:RI::cmdExecuteIndirectWithRootDatalets 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 withBufferUsage::eIndirectScratch. When the returnedsizeis0(which is always the case on D3D12),scratchmay benullptr.
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 atscratchOffset; 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 atscratchOffset; 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, andvkCmdDispatchIndirect. Level 2 usesVK_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 withvkCmdExecuteGeneratedCommandsEXTfor the bound pipeline. The driver's preprocess buffer is your scratch range: its size comes fromvkGetGeneratedCommandsMemoryRequirementsEXTfor the pipeline,kind, andmaxCount, andBufferUsage::eIndirectScratchadds the buffer's preprocess usage bit. RI allocates nothing itself. - D3D12: Both levels call
ExecuteIndirectwith 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
countbuffer must haveBufferUsage::eIndirect,offsetandcountOffsetmust be multiples of 4, and allmaxCountrecords (plus the 4-byteuint32count, if used) must fit within their buffers (programmer error in validation builds). - Level 1 dispatches (
IndirectKind::eDispatchwithcmdExecuteIndirect) must havemaxCount == 1andcount == nullptr, because Vulkan has no indirect dispatch count command (programmer error in validation builds). Level 2 dispatches (cmdExecuteIndirectWithRootData) may use bothmaxCount > 1and acountbuffer. - Draw calls (
eDraw,eDrawIndexed, andeDrawMesh) 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 mostRI::kMaxGroupsPerDimension(65,535) groups per dimension, and at mostRI::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 toStage::eIndirectbefore 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 aftervkCmdExecuteGeneratedCommandsEXT. - Calling
indirectScratchRequirementsorcmdExecuteIndirectWithRootData, or creating a buffer withBufferUsage::eIndirectScratch, on a device wheredevice.limits.indirectRootDataisfalseis reported as unsupported. - When
indirectScratchRequirementsreturns a non-zerosize,scratchmust be non-null and created withBufferUsage::eIndirectScratch,scratchOffsetplus thatsizemust fit in the buffer, and the buffer address atscratchOffsetmust be aligned to the returnedalignment(programmer error in validation builds otherwise). TwocmdExecuteIndirectWithRootDatacalls that are not separated by a barrier onStage::eIndirectmust not share a scratch range, because the GPU may execute them concurrently; calls in order on the same queue with aStage::eIndirectbarrier 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 infirstInstance.