skip to content

Why do per-draw state changes and resource binding cost CPU time in a renderer, and what does pre-built pipeline state remove?

level: middleimportance: should knowfreq 45%

answer

  1. the driver learns state at draw time
  2. validate, translate, maybe recompile
  3. bake shaders and fixed state together
  4. group bindings by update frequency

basics

~20 s

Driver-managed APIs validated and translated each state change at draw time, sometimes recompiling shaders. A pre-built pipeline state object bakes shaders and fixed-function state into one validated object at load, so a draw just switches to it.

solid answer

~40 s

When state is set piecemeal, the driver cannot know the final combination until the draw, so at draw time it validates the combination, translates it into hardware state and, on GPUs that implement some fixed-function state in shader code, may compile a new shader variant: CPU time per draw, and a hitch on first use. A **pre-built pipeline state object** bakes the shaders, vertex layout, rasterizer, depth/stencil and blend state and render-target formats into one object compiled at creation, so switching pipelines at draw time is nearly free. Resource binding remains, so a **binding layout** declares the slots up front and renderers group bindings by update frequency (per frame, per material, per draw). Sorting draws by pipeline, then material, keeps the remaining switches cheap.

go deeper

for a junior

Recall that a pipeline state object bundles shaders with blend, depth and raster state, and that switching it is a draw-time cost worth reducing.

for a middle

Explain what the driver did at draw time when state was set piecemeal, why pre-built state moves that work to creation, and how bindings grouped by frequency keep per-draw work small.

for a senior

Design the draw sort key and binding groups for a scene, and explain why pipeline creation must happen at load or in the background to avoid first-use hitches.

for a principal

Weigh classic slot binding against bindless across the team's platforms: CPU savings against shader complexity, residency pressure and the tooling needed to debug it.

## Where the CPU time goes Every draw needs the GPU configured: which shaders run, how vertices are fetched, how depth and stencil are tested, how colour is blended, which render-target formats are written, and which buffers and textures the shaders read. In driver-managed APIs each of those was set by its own call, and any of them could change between two draws. The driver could not know the final combination until the draw was issued, so at **draw time** it had to: 1. validate the combination of everything currently bound; 2. translate it into the GPU's hardware state; 3. on hardware where some "fixed-function" state is really implemented in shader code (vertex fetch, blending or output-format conversion on some GPUs), patch or **compile a shader variant** for the new combination; 4. track which resources were bound so it could manage hazards and memory behind the application's back. Most of this is CPU work, repeated for every draw whose state differs from the previous one. Step 3 can take milliseconds the first time a combination appears, which shows up as a hitch. ## What pre-built pipeline state holds A **pipeline state object** bakes the combination in advance. Created once, typically during loading, it holds: - the shader program for every stage; - the vertex input layout; - rasterizer state such as cull mode, fill mode and depth bias; - depth/stencil test and write state; - blend state for each render target; - the render-target and depth formats it writes; - the binding layout its shaders expect. The driver validates and compiles the whole object at creation. At draw time, switching pipelines is close to setting a pointer: nothing is left to discover. The cost has moved, not vanished. Creating a pipeline object compiles shaders, so creating one on the render thread the first time an effect appears still hitches; renderers create the pipelines they will need during loading or on background threads, while when and where the underlying shader variants are compiled and cached is decided in the asset pipeline. A few values stay dynamic in most APIs because they change often and need no compilation: viewport, scissor, stencil reference and blend constants. ## Resource binding and binding layouts A pipeline fixes *how* its shaders read resources, not *which* resources they read. A **binding layout** declares up front which slots the shaders use and what kind each is (constant data, textures, buffers, samplers), so binding needs no per-draw rediscovery. Renderers group bindings by how often they change: | Group | Changes | Examples | |---|---|---| | Per frame | once per frame | camera matrices, time, exposure | | Per pass | once per pass | shadow maps, inputs from earlier passes | | Per material | when the material changes | albedo, normal and roughness textures | | Per draw | every draw | object transform, instance offset | Binding the per-frame group once and changing only the small per-draw group keeps a draw's CPU cost to a few small writes. **Bindless** designs go further: textures and buffers sit in one large table, shaders index it with an integer, and a draw passes only indices. That trades CPU binding work for indexing rules in the shader, harder debugging, and every referenced resource having to stay resident. ## Ordering draws to cut switches A pipeline switch is still the costliest change; besides CPU work it can make the GPU drain or reconfigure parts of its pipeline. Renderers therefore sort with a key, most expensive change first: 1. pass or render target; 2. pipeline state; 3. material bindings; 4. per-draw data, or depth front to back for opaque geometry where early depth rejection saves more than a state switch costs. Two materials that share shaders and fixed-function state need **one** pipeline; they differ only in bindings. Creating a pipeline per material multiplies compile time and switches for no gain. ## What remains costly - Pipeline creation, now paid up front and multiplied by every combination of shaders, formats and fixed state the game uses. - Very many tiny draws: even a cheap draw carries fixed per-draw overhead, which is why instanced and indirect draws exist. - Changing render targets, which ends a pass and can force extra memory traffic on tile-based GPUs. So pre-built pipeline state removes per-draw validation and surprise compiles, binding layouts remove per-draw rediscovery of resources, and sorting removes most of what is left; what remains is a known, up-front cost.

  • Why does a pipeline state object include the render-target formats and the vertex layout?
    On some GPUs, fetching vertices and converting or blending output to a target's format are done partly by compiled shader code. Knowing the vertex layout and target formats at creation lets the driver compile that code once. If they could change at draw time, the driver would be back to patching shaders per draw.
  • What does a bindless design trade for its lower binding cost?
    Shaders index one large table of resources by integer, so the CPU binds the table once and a draw passes only indices. In exchange, shaders must handle indices that differ across lanes, debugging an out-of-range index is harder, and every resource the table can reference must stay resident in GPU memory.

  • Pre-built pipeline state makes shader compilation free.
  • Every material needs its own pipeline state object, even with identical shaders and state.
  • A draw is expensive because the CPU must wait for the GPU to finish the previous draw.
  • Changing a texture costs as much as changing the pipeline, so draw order does not matter.
  • Binding layouts are a naming convention with no effect on per-draw cost.