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.
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:
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.
Unsorted
The expensive state changes at almost every draw.
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.
| Work | CPU-driven | GPU-driven |
|---|---|---|
| Where the scene lives | CPU memory, copied into constants per draw | GPU tables, one row per object, updated when something changes |
| Visibility test | CPU, one object at a time | compute shader, one thread per object (see frustum culling) |
| Level of detail | CPU picks a mesh | compute shader changes the draw's index range (see LOD selection) |
| Draw arguments | CPU passes them in each call | compute shader writes them into a buffer |
| Calls the CPU records | one per object, plus state changes | one per group of draws that share a state |
| CPU cost per frame | grows with the object count | almost constant |
- 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:
Direct draw
The 5 numbers are function arguments. The CPU must know them when it records the call.
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.
Every modern API has this record, with the same fields in the same order. Only the names differ:
| API | Record type, and the draw call that reads it |
|---|---|
| Vulkan | VkDrawIndexedIndirectCommand read by vkCmdDrawIndexedIndirect and vkCmdDrawIndexedIndirectCount |
| Direct3D 12 | D3D12_DRAW_INDEXED_ARGUMENTS read by ExecuteIndirect, with a command signature |
| Metal | MTLDrawIndexedPrimitivesIndirectArguments read by drawIndexedPrimitives with an indirect buffer |
| OpenGL 4.3+ | DrawElementsIndirectCommand read by glMultiDrawElementsIndirect |
| WebGPU | 5 × 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.
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-in | Vulkan (SPIR-V) | Direct3D (HLSL) |
|---|---|---|
| Instance index | InstanceIndex 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 index | DrawIndex, 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.
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.
Fixed count, empty records
It works, but the command processor still reads every empty record.
Draw count buffer
The GPU reads how many records to draw, and stops there.
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.
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.
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:
A pipeline per material
One shader for all materials, with the material as data: uber shaders.
Textures bound per material
All textures in one big array, picked by index: bindless resources.
Vertex and index buffers per mesh
All meshes in one big buffer: vertex pulling and mega-buffers.
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.
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.
Once, at load
Upload meshes, textures and materials. Create the pipelines.
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.
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
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
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
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
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.
The same idea at scale
| 10,000 objects, 3 states | CPU-driven | GPU-driven |
|---|---|---|
| Commands recorded per frame | about 40,000 (bind mesh, bind material, constants, draw, per object) | 3 binds once, then 3 per run (state, constants, draw): 12 |
| Draw record buffer | none | 10,000 × 20 B = 200,000 B (about 195 KiB), written only on change |
| Count buffer | none | 3 × 4 B = 12 B, written by the GPU every frame |
| Draws the GPU runs | the visible ones | the visible ones |
11Limits and trade-offs
| Limit | Why | Usual answer |
|---|---|---|
| Materials must be data | One call has one pipeline, so per-material shaders split the runs. | Uber shaders, bindless textures. |
| Per-draw GPU cost stays | The 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 debug | The 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 visible | The result is in GPU memory. | Read back statistics a frame or two later. |
| Memory sized for the worst case | A count call needs room for maxDrawCount records, even if few survive. | Size buffers to the draw list, grow them when it grows. |
| API support | Count 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.
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
| Part | This page | RASM |
|---|---|---|
| Draw record | 5 numbers, 20 bytes | DrawInfo, the same 5 fields in the same order, in uberMaterialTypes.h and core.slang. Stride 20 (sizeof(gfx::DrawInfo)). |
| Instancing | instanceCount copies per record | Always instanceCount = 1. Two entities with the same mesh are two draws (P3). |
| Object id | firstInstance read through the instance index | firstInstance 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 index | restarts at 0 per call | getDrawIndex reads DrawIndex with spirv_asm and adds firstDraw from the push constant: the run's first slot. |
| Grouping into runs | by pipeline and state | By 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 list | CPU on change, or GPU | CPU, 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 buffer | one count per call | One 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). |
| LOD | compute changes the index range | Yes: writeMain writes the chosen LOD's firstIndex and indexCount into the packed copy. The CPU list keeps LOD 0. See LOD selection. |
| Two rounds | one | With 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). |
| Fallback | a CPU path | If 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. |
| Statistics | read back later | Culling 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
| What | Where |
|---|---|
| The draw record | gfx::DrawInfo in src/renderer/uberMaterialTypes.h, DrawInfo in shaders/lib/core.slang |
| Draw list and runs | SceneDrawList, DrawRun in src/renderer/gpuScene.h; Renderer::rebuildDrawList, Renderer::uploadDrawList in src/renderer/gpuScene.cpp |
| The scene passes | Renderer::addScenePasses in src/renderer/gpuScene.cpp |
| Recording the draws | Renderer::drawSceneView in src/renderer/gpuScene.cpp |
| Recording the culling | Renderer::recordSceneCulling, Renderer::recordHiz in src/renderer/gpuScene.cpp |
| Packing and counting | countMain, writeMain in shaders/passes/cull.slang |
| The indirect calls | RenderContext::drawIndexedIndirectCount, RenderContext::drawIndexedIndirect in src/rhi/renderContext.cpp; VulkanCommandBuffer::drawIndexedIndirectCount in src/rhi/vulkan/vulkanCommandBuffer.cpp |
| The Vulkan features | multiDrawIndirect, drawIndirectFirstInstance, drawIndirectCount, shaderDrawParameters in initPhysicalDevice, src/rhi/vulkan/vulkanDevice.cpp |
| The draw index in the shader | getDrawIndex, PushConstants in shaders/lib/core.slang; vertMain in shaders/passes/rasm.slang |
DrawInfohas 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.
drawSceneViewandcull.slangboth rely onrunFirst. - 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_RUNSon both sides and thestatic_assertnext to it. drawSceneViewsets the cull mode back toNONEafter its runs:beginPasssets it for every other pipeline.
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:getDrawIndexusesspirv_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_PULLINGdoes it everywhere, with two more compute passes and one indirect draw per visible meshlet (P28).
Further reading
- Ulrich Haar and Sebastian Aaltonen, GPU-Driven Rendering Pipelines, SIGGRAPH 2015, Advances in Real-Time Rendering in Games.
- Graham Wihlidal, Optimizing the Graphics Pipeline with Compute, GDC 2016.
- Cass Everitt, Tim Foley, John McDonald and Graham Sellers, Approaching Zero Driver Overhead in OpenGL, GDC 2014.
- Christer Ericson, Order your graphics draw calls around! (2008): sort keys.
- The Vulkan specification, chapter "Drawing Commands": indirect draws and draw count buffers.