skip to content

How would you set a shader permutation budget, choosing between uber-shaders with dynamic branches and specialised variants as material feature flags multiply?

level: principalimportance: should knowfreq 38%

answer

  1. independent choices multiply
  2. two to the power of features
  3. pipeline states, memory, batching
  4. worst-path registers in one program
  5. a number, an owner, a check

basics

~20 s

Each independent on/off feature doubles the variant count, and every shipped variant costs pipeline states, memory and batching. Specialise features that change inputs, registers or cost a lot; fold cheap toggles into branches on per-draw constants; enforce a numeric budget.

solid answer

~50 s

Permutations multiply because independent choices combine: eight on/off features give `2^8 = 256` variants, a three-way quality tier makes 768, and skinned versus static vertices 1,536, per pass. Every variant that ships costs a pipeline state built and held in memory, a possible creation hitch the first time it is needed, and a distinct state that breaks batching. An **uber-shader** avoids that by branching on per-draw constants; those branches do not diverge, but the program is allocated registers for its heaviest path and loses compile-time folding. So I classify features: ones that change vertex inputs, register use or cost a lot become variants; cheap toggles become constant branches; combinations no material uses are stripped. Then I set a budget per pass and platform, make a build check fail when it is exceeded, and give one owner the exceptions.

go deeper

for a junior

Recall that each independent on/off shader feature doubles the number of program variants, and that an uber-shader is one program that branches instead.

for a middle

Explain what a variant costs at run time, and why branches on per-draw constants do not diverge but still reserve registers for the heaviest path.

for a senior

Classify features by cost, register and input-layout impact, and strip combinations no material uses. Measure occupancy and batching before moving a feature between branch and variant.

for a principal

Own the number: a per-platform variant budget, the rule for which features may be specialised, a build check that enforces it, and an exception process with one owner.

## Why permutations multiply A **shader permutation** (or variant) is one compiled program produced from a shared source by fixing a set of feature choices. Independent choices multiply, they do not add: | Choice | Options | Running total per pass | |---|---|---| | Eight on/off material features | 2 x 2 x ... x 2 = 256 | 256 | | Quality tier | 3 | 768 | | Vertex type (static or skinned) | 2 | 1,536 | | Passes (depth, shadow, colour), if nothing is stripped | 3 | 4,608 in total | Every new independent on/off feature doubles the total unless combinations are stripped. That is why a material system that adds "just one more checkbox" each sprint ends up with variant counts no one planned. ## What each variant costs at run time - **Pipeline state creation.** A variant becomes usable only once a pipeline state is built from it, which takes CPU time; if that happens the first time an effect appears, the frame hitches. Where and when variants are compiled and cached is an asset-pipeline decision, but the number of variants is what makes it hard. - **Memory.** Program binaries and pipeline states stay resident for every variant a level might use. - **Batching and state changes.** Each distinct variant is a state change between draws, and draws using different variants cannot be merged. - **Instruction cache pressure.** Many near-identical programs running in one frame compete for the GPU's instruction cache. - **Test and support surface.** Each variant can break on its own, on one platform, and someone has to find it. ## What an uber-shader costs instead An **uber-shader** keeps features in one program and branches on per-draw constants: - **No divergence** from those branches, because every lane of a draw reads the same constant. - **Worst-path register allocation.** The program reserves registers for its heaviest path, which can lower occupancy for every material, including ones that never take it. - **Lost compile-time folding.** Dead code and constant arithmetic that a specialised variant would remove stay in the program. - **Per-pixel conditions still diverge.** A feature switched by a per-pixel value behaves very differently from one switched per draw. ## Middle grounds - **Constants fixed when the pipeline state is built.** Many graphics programming models let a value be baked in at pipeline creation, so the compiler folds it without a separate source branch; it still creates a distinct pipeline state. - **Strip unused combinations.** Scan the shipped materials and build only the combinations that occur. - **Strip per pass.** A depth-only or shadow pass ignores colour features, so hundreds of colour variants collapse to a few. - **Tiers instead of toggles.** Group features into a few quality tiers rather than independent switches. ## Setting and enforcing the budget 1. **Inventory**: count variants per pass and per platform as they stand, and list the features that drive the count. 2. **Classify each feature** by its cost delta, its register delta, whether it changes vertex inputs or resource layout, and how many materials use it. 3. **Decide the default per class**: input-layout changes and heavy, register-hungry features become variants; cheap, widely used toggles become branches on per-draw constants; rare combinations are rejected or folded into a nearby one. 4. **Set numbers**: a total per platform and a ceiling per material, chosen from what load time, memory and creation hitches allow. 5. **Enforce it**: a build step reports the count and fails when a change pushes it over the budget, so growth is a decision rather than an accident. 6. **Name an owner** for exceptions, so a material that truly needs a new variant can get one with its cost on record. ## Signals that the policy needs revisiting - First-use hitches or load times rising as content grows. - Hot materials losing occupancy to an uber-shader's heaviest path. - Batching falling as the number of distinct states per frame climbs. - Artists working around the budget by duplicating materials. There is no single right answer: a project with a small, fixed set of materials can afford many specialised variants, while one with user-generated or highly varied content leans towards uber-shaders and tight stripping. The lead's job is to choose the split deliberately and keep it measured.

  • Why does a depth-only pass need far fewer variants than the colour pass?
    Most material features only change colour: lighting models, detail maps, tints. A depth-only program needs the position, any vertex deformation such as skinning, and the cutout test for alpha-tested materials. Stripping per pass collapses hundreds of colour variants into a handful of depth variants, cutting build time, memory and creation hitches.
  • When should a feature become a constant fixed at pipeline-build time rather than a separate source variant?
    When it changes control flow or loop counts the compiler could fold away, but not the vertex inputs or resource layout. Fixing the value when the pipeline state is built lets the compiler remove dead paths and size registers for the real path without another source branch to maintain. It still creates a distinct pipeline state, so it counts against the budget.
  • What would make you revisit the variant budget mid-project?
    First-use hitches or load times rising, hot materials losing occupancy to an uber-shader's heaviest path, or batching falling as distinct states grow. Each signal says the split between branches and variants has drifted; re-measure the features responsible before simply raising the number.

  • Shader features add variants, so ten features mean about ten programs.
  • An uber-shader's per-draw branches diverge just like per-pixel ones.
  • Specialised variants cost nothing at run time once they are compiled.
  • An uber-shader costs nothing for features a material does not use.
  • Every combination of features must ship in case a material needs it.