Drawing

GPU-driven rendering and indirect draws

A classic renderer walks over its objects on the CPU and records one draw call for each. A GPU-driven renderer keeps the scene in GPU memory and lets the GPU itself decide what to draw and how. The tool that makes this possible is the indirect draw: a draw call whose arguments live in a GPU buffer. This page explains the idea from the start. The last part shows how RASM does it.

In short

  • A CPU draw loop costs CPU time per object: state changes, bindings and one draw call each. With many objects, the CPU becomes the limit.
  • GPU-driven rendering moves the scene into GPU tables (objects, meshes, materials). The CPU only updates rows that changed.
  • An indirect draw reads its 5 arguments from a buffer. A compute shader can write them, so the CPU does not need to know them.
  • Multi-draw indirect runs many draw records with one call. A draw count buffer lets the GPU also choose how many.
  • firstInstance, or a draw-index built-in, tells the shader which object a draw belongs to.
  • One call uses one pipeline state. So draws are sorted into runs that share their state, with one call per run.

1The CPU draw loop

The simplest renderer draws one object at a time. For each object, the CPU sets up the GPU state the object needs, then asks for a draw:

for each object in the scene:
    if the object is not visible: skip it
    bind the pipeline        (only if it is not the one bound already)
    bind the vertex and index buffers of the object's mesh
    bind the descriptor set of the object's material
    set constants:           the object's world matrix
    drawIndexed(indexCount, 1, firstIndex, vertexOffset, 0)

Each line is a command that the graphics driver must check and turn into GPU commands. This happens on the CPU, every frame, for every object.

CPU: one group of commands per object, every frame bind buffers its mesh bind textures its material set constants its matrix draw one mesh object 0 object 1 object 2 … × N Driver, also on the CPU checks every command, tracks the bound state, writes the GPU's own commands GPU: runs the draws one by one waits waits waits waits
The CPU cost grows with the number of objects, not with the amount of work the GPU does. A scene of 10,000 small objects costs much more CPU time than one big mesh with the same triangles. A small draw can finish before the CPU has sent the next one: then the GPU waits.

This is called a CPU-driven renderer. The CPU decides everything: which objects are visible, in which order they are drawn, and with which arguments. It works well for hundreds of objects. With tens of thousands, recording the commands can take longer than the GPU needs to draw them.

2Not all commands cost the same

Changing some state is much more expensive than changing other state. A new pipeline means new shaders and new fixed-function settings for the GPU. New push constants are only a few bytes. Most engines know this rough order:

costly cheap Render target or pass change Pipeline: shaders, blend, depth test, vertex layout Descriptor sets: textures, buffers Vertex and index buffers Push constants, uniforms The draw
A common rough order, not exact numbers. The real costs depend on the API, the driver and the GPU, so measure on your own targets.

So the first fix for a CPU-driven renderer is to sort the draws. Each draw gets a number, a sort key, built from its state, with the most expensive state in the highest bits. After sorting, draws with the same expensive state are neighbours, and that state changes once per group instead of once per draw.

pass 4 bits pipeline 12 bits material 16 bits mesh 16 bits depth 16 bits high bits: costly state low bits: cheap order
Sort all draws by this 64-bit number. Draws of the same pass and pipeline become neighbours, so the pipeline changes once per group. Depth in the low bits orders the draws inside a group, front to back. This is one example layout. Each engine picks its own fields and sizes.
A B A C B A C B A draws in scene order · the letter is the pipeline 8 pipeline changes

Unsorted

The expensive state changes at almost every draw.

A A A A B B B C C the same draws, sorted by key 2 pipeline changes

Sorted by key

Draws with the same state are neighbours. The state changes once per group.

Sorting reduces the state changes, but the CPU still records one draw per object and still decides what is visible. GPU-driven rendering removes the loop itself.

3The idea: give the scene to the GPU

The GPU is very good at doing the same small job for many items in parallel. Culling and building draw arguments are such jobs. So a GPU-driven renderer moves the scene into GPU buffers and lets a compute shader do the work of the CPU loop.

CPU-driven Scene objects in CPU memory Cull, sort, bind, draw a CPU loop over all objects N draw calls one per object Pixels GPU-driven Changed rows only a few per frame, or none Scene tables objects, meshes, materials Compute: cull one thread per object Draw records and their count A few indirect draws, one per state Vertex shader reads its object's row reads
Blue runs on the CPU, green and purple on the GPU, amber boxes are GPU buffers. In the GPU-driven version, the per-object work never touches the CPU.
WorkCPU-drivenGPU-driven
Where the scene livesCPU memory, copied into constants per drawGPU tables, one row per object, updated when something changes
Visibility testCPU, one object at a timecompute shader, one thread per object (see frustum culling)
Level of detailCPU picks a meshcompute shader changes the draw's index range (see LOD selection)
Draw argumentsCPU passes them in each callcompute shader writes them into a buffer
Calls the CPU recordsone per object, plus state changesone per group of draws that share a state
CPU cost per framegrows with the object countalmost constant
What this needs from the API
  • A draw call that takes its arguments from GPU memory (sections 4, 6 and 7).
  • A way for the shader to find its object's data without per-draw bindings (section 5). This also needs bindless textures and vertex pulling, because there is no chance to bind a mesh's buffers or a material's textures between draws.

4Indirect draw arguments

An indexed draw needs 5 numbers. We call these 5 numbers a draw record. The two kinds of draw call differ in where the record comes from:

the CPU records drawIndexed(36, 1, 4800, 0, 57) the 5 numbers, in the call GPU draws

Direct draw

The 5 numbers are function arguments. The CPU must know them when it records the call.

the CPU records drawIndexedIndirect(buf, offset) only where to look GPU draws 36 1 4800 0 57 buf, in GPU memory

Indirect draw

An indirect call passes only a buffer and an offset. The GPU reads the 5 numbers from that buffer when it runs the call.

One indexed draw record: 5 × 4 bytes = 20 bytes indexCount uint32 instanceCount uint32 firstIndex uint32 vertexOffset int32, signed firstInstance uint32 0 4 8 12 16 indices to read (3 per triangle) copies to draw (often 1) first index to read in the index buffer added to each index value instance index of copy 0
The same five numbers a direct indexed draw takes, in the same order, but stored in a GPU buffer. Grey numbers are byte offsets. A non-indexed draw record has 4 numbers: vertex count, instance count, first vertex, first instance.

Every modern API has this record, with the same fields in the same order. Only the names differ:

APIRecord type, and the draw call that reads it
VulkanVkDrawIndexedIndirectCommand
read by vkCmdDrawIndexedIndirect and vkCmdDrawIndexedIndirectCount
Direct3D 12D3D12_DRAW_INDEXED_ARGUMENTS
read by ExecuteIndirect, with a command signature
MetalMTLDrawIndexedPrimitivesIndirectArguments
read by drawIndexedPrimitives with an indirect buffer
OpenGL 4.3+DrawElementsIndirectCommand
read by glMultiDrawElementsIndirect
WebGPU5 × u32 in a buffer
read by drawIndexedIndirect, one draw per call

Because the record is plain memory, anyone who can write a buffer can write it: the CPU through an upload, or a compute shader on the GPU. That is the whole point. The CPU only says "draw what is written there", and the GPU fills in what "there" means.

5firstInstance as an object id

A draw record says which triangles to draw. It does not say which object they belong to: where it is, which material it has. In a CPU-driven renderer, the CPU binds that data before each draw. In a GPU-driven renderer, nothing is bound between draws, so the shader must find the object's data itself. It needs a number that is different for each draw: an object id.

The usual trick uses firstInstance. Instancing draws the same mesh instanceCount times, and each copy gets an instance index. Copy i gets firstInstance + i. When the draw has one copy, its instance index is just firstInstance. So the renderer writes the object's row number into firstInstance, and the vertex shader uses the instance index to read that row.

Draw record 3 indexCount36 instanceCount1 firstIndex4800 vertexOffset0 firstInstance57 Vertex shader instance index = 57 firstInstance + copy 0 reads row 57 Object table row world matrix mesh id material id 55 56 57 4 × 4123 mesh table materialtable
The draw record carries only a row number. Everything else about the object is in tables, and the shader follows the row number to them. The draw call itself knows nothing about the object. It is only a pointer to a row.

There is a second way. Some APIs give the shader a draw-index built-in: the number of the record inside the current multi-draw call (next section). The shader can read the record itself with it, and any extra per-draw data stored next to it. Both ways are common. Watch out for the API differences:

Built-inVulkan (SPIR-V)Direct3D (HLSL)
Instance indexInstanceIndex includes firstInstance. A non-zero firstInstance in an indirect record needs the drawIndirectFirstInstance feature.SV_InstanceID starts at 0 for every draw: it does not include the start instance. Shader compilers that target Vulkan from HLSL have to choose one meaning.
Draw indexDrawIndex, from Vulkan 1.1 with the shaderDrawParameters feature. 0 for the first record of each call.No built-in. ExecuteIndirect can instead set a root constant per record.

6Multi-draw indirect

One indirect draw that reads one record saves little: the CPU still makes one call per object. Multi-draw indirect fixes that. One call reads N records, one after another, and runs N draws. The records are stride bytes apart, usually exactly the record size.

draw record buffer record 020 B record 120 B record 220 B record 320 B record 420 B record 520 B stride = 20 One call from the CPU drawIndexedIndirect(buf, 0, 6, 20) GPU command processor reads record i, runs draw i
The arguments are: buffer, offset of the first record, number of records, stride. In the shader, the draw-index built-in counts 0, 1, … 5 through the call. The CPU cost is one call, whatever N is. Vulkan needs the multiDrawIndirect feature for N > 1.

The GPU still runs N separate draws. Its command processor, the front part of the GPU that reads commands, walks the records itself. That is cheap, but not free: a draw of a few triangles still has a fixed cost on the GPU. GPU-driven rendering removes the CPU cost of a draw, not the GPU cost.

7Draw count buffers

Multi-draw indirect still has one number from the CPU: N, the record count. But after GPU culling, only the GPU knows how many draws are left. One answer is to keep N fixed and make culled records draw nothing (set their indexCount or instanceCount to 0). That works, but the command processor still reads every empty record.

The better answer is a draw count buffer. The call takes a second buffer, and the GPU reads the record count from it at run time. The CPU passes only an upper limit, maxDrawCount. The GPU draws min(count, maxDrawCount) records.

N = 6, fixed by the CPU · culled records draw nothing d0 0 0 d3 d4 0 GPU reads all 6 records

Fixed count, empty records

It works, but the command processor still reads every empty record.

visible records packed at the start · count from the GPU d0 d3 d4 GPU reads 3 records count = 3

Draw count buffer

The GPU reads how many records to draw, and stops there.

Compute: cull keeps 3 of 6 obj 0 obj 3 obj 4 unused unused unused records count = 3 count buffer Recorded by the CPU before culling ran drawIndexedIndirectCount(buf, 0, cnt, 0, 6, 20) When the GPU runs the call, it reads count = 3 and draws min(3, 6) = 3 records.
The arguments are: record buffer and offset, count buffer and offset, maxDrawCount, stride. The record buffer must have room for maxDrawCount records.

Packing the visible records tightly at the start of the buffer, in a stable order, is a job of its own: stream compaction. In Vulkan the call is vkCmdDrawIndexedIndirectCount, core since Vulkan 1.2 with the drawIndirectCount feature. Direct3D 12 has the same count buffer in ExecuteIndirect.

Order matters for the GPU too

Culling runs in parallel, so the easy way to pack is an atomic counter: each kept record takes the next free slot. Then the order of the records changes from frame to frame. The picture stays the same, but depth-test efficiency and debugging captures do not. A prefix sum keeps the original order. See stream compaction.

8Grouping draws by state: runs

One indirect call uses the state that is bound when it is recorded: one pipeline, one set of bindings, one value of every dynamic state. All its draws share it. So draws that need different state cannot be in the same call.

The fix is the sort from section 2, done once: sort the draw records by their state, so each state's records are one contiguous range, a run. Then record one indirect call per run, each with its own offset, its own maxDrawCount and its own count.

run 0 · state A 0 1 2 3 run 1 · state B 4 5 run 2 · state C 6 7 8 set state A draw: first record 0, max 4, count[0] set state B first 4, max 2, count[1] set state C first 6, max 3, count[2]
3 states, 3 calls, whatever the number of draws. The CPU cost is now one call per state, not per object. The fewer states the scene needs, the fewer calls. Culling packs each run inside its own range, so a run's visible records start at the run's first record, and a run with nothing visible draws nothing.

This is why GPU-driven renderers work hard to reduce the number of states. Each card is one thing that would split the runs, and how engines avoid it:

shader A shader B shader C one shader material data

A pipeline per material

One shader for all materials, with the material as data: uber shaders.

012345 one array, picked by index

Textures bound per material

All textures in one big array, picked by index: bindless resources.

one big buffer mesh 012

Vertex and index buffers per mesh

All meshes in one big buffer: vertex pulling and mega-buffers.

pipeline stays the same cull: back draw run 0 cull: none draw run 1

Fixed-function state, such as face culling

Dynamic state: the value is set in the command buffer, not baked into the pipeline. It still splits runs, but not pipelines.

opaque pass transparent pass sorted, blended its own pipeline, its own pass

Blending (transparent objects)

Cannot be avoided. Transparent objects need their own pipeline, and usually a sorted order, so they get their own pass.

9What the CPU still does

GPU-driven does not mean the CPU does nothing. It means the CPU's work no longer grows with the number of objects drawn each frame.

CPU meshes textures materials

Once, at load

Upload meshes, textures and materials. Create the pipelines.

CPU changed row

When something changes

Write the changed rows of the scene tables: a moved object, a new material. Rebuild the draw list when objects are added or change state.

camera cull run A run B submit the same few commands, any scene size

Every frame

Write the camera and the per-frame constants. Record the culling dispatches, a few binds and one indirect call per run. Submit.

Never

Loop over all objects to test, sort or draw them.

The CPU also loses something: it no longer knows what was drawn. How many objects were visible, or how many triangles, is only in GPU memory. To show it, the CPU must read it back a frame or two later, after the GPU has finished (see GPU timing and frame statistics).

10Worked example

A scene with 8 draws in two states. Click through what happens in one frame.

1. The draw list, sorted by state

run A: records 0 to 4 run B: records 5 to 7 d0 d1 d2 d3 d4 d5 d6 d7

All 8 records are in a GPU buffer. The CPU built this list when the scene changed, and uploaded it. It is the same every frame until the scene changes, so nothing in this step happens per frame. Each record has the full mesh range and the object's row in firstInstance.

2. Culling tests every record

run A run B d0 ✓ d1 ✗ d2 ✓ d3 ✓ d4 ✗ d5 ✗ d6 ✓ d7 ✓

One compute thread per record. Each thread reads its object's row and bounds, and tests them against the camera. It may also pick a coarser level of detail and change the record's index range. 5 of the 8 records are visible: 3 in run A, 2 in run B.

3. Pack each run, write its count

output record buffer: each run keeps its range, its visible records packed at the start d0 d2 d3 d6 d7 0 1 2 3 4 5 6 7 count[A] = 3 count[B] = 2 count buffer

Each run's records start where the run starts, so the CPU already knew the offsets when it recorded the calls. The order inside a run stays the list order: d0, d2, d3. The unused slots keep old data, which no draw reads.

4. Two indirect calls draw them

Call A (state A) records from byte 0 × 20 = 0 count from count[A] = 3, max 5 draw index 0, 1, 2 → slots 0, 1, 2 Call B (state B) records from byte 5 × 20 = 100 count from count[B] = 2, max 3 draw index 0, 1 → slots 5 + 0, 5 + 1

The CPU recorded 2 state changes and 2 calls. It did not know that 5 draws would be visible, nor which ones. The draw-index built-in restarts at 0 in every call. A shader that reads the record by draw index must add the run's first slot, which the CPU passes in, for example as a push constant.

Try it. The picture below is the same draw list. You decide which objects are visible; it packs the records and writes the counts, as steps 2 and 3 do.

visible record culled, or an unused slot (dashed) written by the GPU: packed records, counts recorded by the CPU
Click records to hide and show their objects. The number of calls a CPU loop records follows the visible objects. The two indirect calls never change: only the counts do. Hide all of run B and see that its call stays.

The same idea at scale

10,000 objects, 3 statesCPU-drivenGPU-driven
Commands recorded per frameabout 40,000 (bind mesh, bind material, constants, draw, per object)3 binds once, then 3 per run (state, constants, draw): 12
Draw record buffernone10,000 × 20 B = 200,000 B (about 195 KiB), written only on change
Count buffernone3 × 4 B = 12 B, written by the GPU every frame
Draws the GPU runsthe visible onesthe visible ones

11Limits and trade-offs

LimitWhyUsual answer
Materials must be dataOne call has one pipeline, so per-material shaders split the runs.Uber shaders, bindless textures.
Per-draw GPU cost staysThe command processor still starts every draw. Thousands of tiny draws keep the GPU front end busy.Merge small objects, use instancing (instanceCount > 1), or split meshes into small clusters drawn with mesh shaders (meshlets).
Harder to debugThe draw arguments are made on the GPU. A wrong record is only visible in a frame capture.Capture tools, a CPU fallback path that draws everything, readback of the counts.
The CPU does not know what is visibleThe result is in GPU memory.Read back statistics a frame or two later.
Memory sized for the worst caseA count call needs room for maxDrawCount records, even if few survive.Size buffers to the draw list, grow them when it grows.
API supportCount buffers are core in Vulkan 1.2 and Direct3D 12. Core WebGPU has single indirect draws only.A fallback that issues one indirect draw per record, or draws without culling.

12In RASM

RASM is GPU-driven from the start. Renderer::addScenePasses adds a scene to the render graph as a compute pass that culls, Cull, and a pass that draws, Scene (Renderer::render does this for you). Scene draws the opaque and cut-out draws with two pipelines, a few binds and one drawIndexedIndirectCount per run of one alpha mode and one cull mode: at most 6 calls, or 12 with occlusion culling (6 in Scene 1, 6 in Scene 2). The see-through run gets one more call, sorted, in its own Transparent pass. The tables it reads are in the GPU scene tour.

CPU · when the scene changes rebuildDrawList a DrawInfo per sub-mesh Draw list buffer sorted into runs, per slot GPU · every frame, the Cull pass cull.slang countMain, writeMain Visible draws packed per run Draw counts one uint per run CPU · drawSceneView, in the Scene pass once: bindIndexBuffer (the geometry pool, 32-bit) per opaque or cut-out run (up to 6): bindPipeline, bindDescriptorSet when the pipeline changes setCullMode(run.cullMode) pushConstants {scene, firstDraw = run.first, argument} drawIndexedIndirectCount(max = run.count, stride 20) The run's draws count from the GPU vertMain getDrawIndex() → DrawInfo → firstInstance → instance row
Both passes run in the graph's order: the culling first, then the draws. Cull declares that it writes the visible draws and counts, Scene that it reads them, so the graph records the barrier between them. By default both are in the frame's one command buffer; with EngineConfig::asyncCompute, Cull runs on the compute queue and Scene waits for it instead (async compute).

Where RASM follows the textbook, and where it differs

PartThis pageRASM
Draw record5 numbers, 20 bytesDrawInfo, the same 5 fields in the same order, in uberMaterialTypes.h and core.slang. Stride 20 (sizeof(gfx::DrawInfo)).
InstancinginstanceCount copies per recordAlways instanceCount = 1. Two entities with the same mesh are two draws (P3).
Object idfirstInstance read through the instance indexfirstInstance is the instance row, but vertMain never reads an instance built-in. It reads its own DrawInfo through getDrawIndex and takes firstInstance from it.
Draw indexrestarts at 0 per callgetDrawIndex reads DrawIndex with spirv_asm and adds firstDraw from the push constant: the run's first slot.
Grouping into runsby pipeline and stateBy alpha mode, which picks the pipeline, then by cull mode, which is dynamic state (VK_DYNAMIC_STATE_CULL_MODE). So there are at most 8 runs (MAX_DRAW_RUNS): NONE, FRONT, BACK for opaque draws, the same for cut-outs, the point run (point cloud and glyph chunks), and one see-through run that the Transparent pass draws sorted. See face culling and transparency.
Building the listCPU on change, or GPUCPU, on change (rebuildDrawList): one draw per sub-mesh of each entity whose mesh and textures are loaded, with LOD 0's index range. std::stable_sort keeps the entity order inside a run.
Count bufferone count per callOne uint32_t per run, at the end of the scene view's visible draw buffer (drawCountsPtr), written by writeMain. Order inside a run is kept: compaction uses prefix sums, not an atomic counter (see stream compaction).
LODcompute changes the index rangeYes: writeMain writes the chosen LOD's firstIndex and indexCount into the packed copy. The CPU list keeps LOD 0. See LOD selection.
Two roundsoneWith occlusion culling, the runs are drawn twice, in two passes: the early draws in Scene 1, then the late ones in Scene 2, from the same lists. So up to 12 calls, and the push constant's argument says which phase drew (the culling debug view colors by it).
Fallbacka CPU pathIf cull.slang fails to compile, drawSceneView uses drawIndexedIndirect over each whole run of the draw list: everything drawn, at LOD 0, with no count buffer.
Statisticsread back laterCulling adds up visible draws and triangles into a host-readable buffer, read after the frame slot's fence (see GPU timing).

Where it lives in code

WhatWhere
The draw recordgfx::DrawInfo in src/renderer/uberMaterialTypes.h, DrawInfo in shaders/lib/core.slang
Draw list and runsSceneDrawList, DrawRun in src/renderer/gpuScene.h; Renderer::rebuildDrawList, Renderer::uploadDrawList in src/renderer/gpuScene.cpp
The scene passesRenderer::addScenePasses in src/renderer/gpuScene.cpp
Recording the drawsRenderer::drawSceneView in src/renderer/gpuScene.cpp
Recording the cullingRenderer::recordSceneCulling, Renderer::recordHiz in src/renderer/gpuScene.cpp
Packing and countingcountMain, writeMain in shaders/passes/cull.slang
The indirect callsRenderContext::drawIndexedIndirectCount, RenderContext::drawIndexedIndirect in src/rhi/renderContext.cpp; VulkanCommandBuffer::drawIndexedIndirectCount in src/rhi/vulkan/vulkanCommandBuffer.cpp
The Vulkan featuresmultiDrawIndirect, drawIndirectFirstInstance, drawIndirectCount, shaderDrawParameters in initPhysicalDevice, src/rhi/vulkan/vulkanDevice.cpp
The draw index in the shadergetDrawIndex, PushConstants in shaders/lib/core.slang; vertMain in shaders/passes/rasm.slang
Keep these true
  • DrawInfo has the layout of the Vulkan indexed indirect record, on both sides. A field added to it changes the stride of every indirect call and of the culling buffers.
  • The draw list is sorted by alpha mode and cull mode, each run is contiguous, and culling packs a run's visible draws at the start of that run's range. drawSceneView and cull.slang both rely on runFirst.
  • Every call pushes firstDraw = the run's first slot. Without it, every run would read run 0's draws.
  • A new state that differs between draws must be dynamic state or a new pipeline, and a new run, or it cannot be in this pass. Raising the number of runs means changing MAX_DRAW_RUNS on both sides and the static_assert next to it.
  • drawSceneView sets the cull mode back to NONE after its runs: beginPass sets it for every other pipeline.
Not done yet
  • P3: no real instancing. Each entity's sub-mesh is its own draw, even when the mesh is shared.
  • A destroyed or removed entity leaves the draw lists at their next rebuild, and its instance rows are reused (deferred destruction).
  • D12: getDrawIndex uses spirv_asm, so this path is Vulkan-only today.
  • Big meshes are culled per meshlet where the GPU has mesh shaders (MeshletPath::AUTO, the default): a task shader culls them inside the draw pass. MeshletPath::VERTEX_PULLING does it everywhere, with two more compute passes and one indirect draw per visible meshlet (P28).

Further reading