Skip to content

RI::cmdDraw, RI::cmdDrawIndexed, RI::cmdDrawMesh

What it does

RI::cmdDraw(cmd, vertexCount, instanceCount, firstVertex, firstInstance) draws vertexCount vertices, repeated instanceCount times (1 by default), starting at firstVertex and firstInstance (both 0 by default). It uses the graphics pipeline set by cmdSetPipeline and the root data set by cmdSetRootData, and renders into the open rendering section.

RI has no vertex buffers. Instead, the vertex shader receives SV_VertexID (from 0 to vertexCount - 1) and SV_InstanceID, and reads vertex data directly from buffers in the descriptor heap—a pattern called vertex pulling. A full-screen triangle needs no buffer data at all:

#include <equine/root.hlsli>

struct DrawRoot {
    float4 color; // read by the pixel shader
    float depth;  // the triangle's depth
};
EQ_ROOT_DATA(DrawRoot, root);

// Vertices 0, 1, 2 give (-1, 1), (3, 1), (-1, -3) in clip space: a triangle
// that covers the target. Clip-space y points up on both backends.
float4 main(uint vertex : SV_VertexID) : SV_Position {
    const float2 uv = float2((vertex << 1) & 2, vertex & 2);
    return float4(uv * float2(2, -2) + float2(-1, 1), root.depth, 1);
}

Indexed draws: RI::cmdSetIndexBuffer(cmd, buffer, offset, format) binds a buffer of 16-bit (IndexFormat::eUint16) or 32-bit (IndexFormat::eUint32) indices. The buffer must have BufferUsage::eIndex, and offset must be a multiple of the index size (2 or 4 bytes) and less than the buffer size. RI::cmdDrawIndexed(cmd, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance) then draws indexCount indices starting at firstIndex, repeated instanceCount times. Each vertex shader invocation receives SV_VertexID equal to the index value: the shader still pulls its own vertex data, while the index buffer defines which vertices form each primitive and lets the GPU reuse shaded vertices.

firstVertex and vertexOffset are not part of SV_VertexID on any backend. This is D3D12's rule, and RI's SPIR-V compile rule (-fvk-support-nonzero-base-vertex) gives Vulkan the same behavior. SV_InstanceID likewise leaves out firstInstance. A shader that needs an offset can take it from root data, or read SV_StartVertexLocation and SV_StartInstanceLocation (shader model 6.8).

Mesh draws (requires device.limits.meshShaders): with a mesh pipeline set, RI::cmdDrawMesh(cmd, x, y, z) launches x * y * z thread groups of the task shader, or of the mesh shader if the pipeline has no task shader. A task shader launches mesh shader groups using DispatchMesh, and mesh shaders output vertices and primitives directly using SetMeshOutputCounts. When a pipeline has both shaders, the mesh shader's input payload must match the task shader's output payload exactly (D3D12 rejects the pipeline otherwise, whereas Vulkan does not check). A mesh shader without a task shader takes no payload.

Signature

// <RI/RI.hpp>, namespace RI
void cmdDraw(CommandList &cmd, uint32 vertexCount, uint32 instanceCount = 1, uint32 firstVertex = 0,
             uint32 firstInstance = 0);
// <RI/RI.hpp>, namespace RI
void cmdSetIndexBuffer(CommandList &cmd, const Buffer &buffer, uint64 offset, IndexFormat format);

From the header: Indexed draws (P39): indices come from an index buffer; vertices are still pulled by the shader. SV_VertexID is the index value alone, and counts from 0 in cmdDraw: firstVertex and vertexOffset are left out on every backend (P117); shader model 6.8 reads them as SV_StartVertexLocation.

// <RI/RI.hpp>, namespace RI
void cmdDrawIndexed(CommandList &cmd, uint32 indexCount, uint32 instanceCount = 1, uint32 firstIndex = 0,
                    int32 vertexOffset = 0, uint32 firstInstance = 0);
// <RI/RI.hpp>, namespace RI
void cmdDrawMesh(CommandList &cmd, uint32 x, uint32 y, uint32 z);

From the header: Mesh pipelines (P39): launches x * y * z task shader groups, or mesh shader groups when the pipeline has no task shader.

Example

RI::cmdSetPipeline(cmd, pipeline);
RI::Helpers::cmdSetRootData(cmd, DrawRoot{.color = {1.0f, 0.5f, 0.0f, 1.0f}, .depth = 0.5f});
RI::cmdDraw(cmd, 3); // three vertices, one instance

The same in native code

RI::cmdSetPipeline(cmd, pipeline);
RI::Helpers::cmdSetRootData(cmd, DrawRoot{.color = {1.0f, 0.5f, 0.0f, 1.0f}, .depth = 0.5f});
RI::cmdDraw(cmd, 3); // three vertices, one instance
vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
const DrawRoot root{.color = {1.0f, 0.5f, 0.0f, 1.0f}, .depth = 0.5f};
vkCmdPushConstants(cmd, pipelineLayout, VK_SHADER_STAGE_ALL, 0, sizeof(root), &root);
vkCmdDraw(cmd, 3, 1, 0, 0);

What to watch for: the graphics bind point has its own pipeline and descriptor sets, separate from compute. Without vertex input, the pipeline's vertex input state is empty.

ID3D12DescriptorHeap *heaps[] = {viewHeap.Get()};
cmd->SetDescriptorHeaps(1, heaps); // before the root signature that indexes it
cmd->SetGraphicsRootSignature(rootSignature.Get());
cmd->SetPipelineState(pipeline.Get());
cmd->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
const DrawRoot root{.color = {1.0f, 0.5f, 0.0f, 1.0f}, .depth = 0.5f};
cmd->SetGraphicsRoot32BitConstants(0, sizeof(root) / 4, &root, 0);
cmd->DrawInstanced(3, 1, 0, 0);

What to watch for: the graphics root signature and root constants are separate from compute (SetGraphicsRoot*). The exact topology is command-list state (IASetPrimitiveTopology); RI sets it from the pipeline at cmdSetPipeline.

Rules and errors

  • Call draw commands only inside a rendering section and with a graphics pipeline set. RI does not check for an open rendering section so that native rendering sections can contain RI draws; on Vulkan, the validation layer reports a draw recorded outside any section.
  • The pipeline's attachment formats and sample count must match those of the open rendering section.
  • In validation builds, cmdSetIndexBuffer reports a programmer error if the buffer lacks BufferUsage::eIndex, or if offset is not a multiple of the index size or is greater than or equal to buffer.size.
  • For indirect draws, see cmdExecuteIndirect.
  • cmdDrawMesh launches at most kMaxGroupsPerDimension (65,535) groups per dimension and kMaxMeshGroups (2^22) groups in total on every backend; exceeding either limit is a programmer error in validation builds. Fold large counts with groupGrid.