An effect hitches the first time it appears because its shader variant was never built; where should shader permutations be compiled and cached to prevent it?
answer
- a compile on the render path
- front end offline, back end per device
- the reachable set, recorded
- persistent cache plus asynchronous fallback
basics
~20 sBuild every reachable variant before it is needed: compile it in the cook, finish device-specific compilation at load or first launch where the driver owns that step, cache the results persistently, and compile any remaining miss asynchronously.
solid answer
~50 sThe hitch is a synchronous compile: the first draw that needs a particular combination of material features finds no compiled program and blocks while it is built, which can take far longer than a frame. Compilation has two stages. Source to an intermediate form can always run offline in the **cook**. The final GPU machine code can also be produced in the cook on fixed-hardware platforms, but where the driver finishes compilation for the player's GPU it must happen on the device, so the engine creates those programs behind a loading screen or on first launch and keeps them in a **persistent on-disk cache**. Compiling every theoretical combination is infeasible, so the reachable set is built from what materials use plus a list recorded in playtests and automated runs. A miss that still slips through compiles on a worker thread while a fallback draws, and telemetry feeds it back into the list.
go deeper
Recall that a shader can have many variants and that compiling one at the moment it is first needed causes a visible stall.
Explain the two compilation stages and why fixed-hardware platforms can ship finished binaries while others must finish compiling on the device.
Diagnose a first-use hitch from a capture, then fix it with a recorded reachable set, load-time warm-up, a persistent cache and an asynchronous fallback.
Weigh cook time and package size against the risk of shipped hitches, and decide who owns keeping the recorded variant list current.
## Why the first use hitches A material's shader is rarely one program. Feature switches (fog on or off, skinned or static vertices, number of lights, a dissolve effect) combine into **variants**, also called permutations. Why they multiply and what each costs on the GPU is a rendering topic; the pipeline's question is **when and where each variant gets compiled, and where the result is kept**. The classic bug: an explosion effect uses a combination no earlier frame needed. The renderer asks for that variant, finds nothing compiled, and compiles it on the spot on the thread that is building the frame. A single compile can take from a few milliseconds to far longer than a whole frame's budget, so the player sees a stall exactly when the effect appears, once per session or once per install. ## Two stages, two places Shader compilation usually happens in two stages, and they can run in different places: | Stage | Input to output | Where it can run | What is cached | |---|---|---|---| | Front end | source to intermediate bytecode | always offline, in the cook | bytecode in the packaged build | | Back end on fixed hardware | bytecode to GPU machine code | offline, in the cook | final binaries in the package | | Back end where the driver finishes it | bytecode to code for this GPU and driver | on the device, at load or first launch | an on-disk cache on the player's machine | On fixed-hardware platforms the GPU and its compiler are known when the cook runs, so the cook can ship finished binaries and the device need not compile at run time. On platforms where many GPUs and driver versions exist, the driver translates the intermediate into code for the exact GPU in the machine. That step cannot run in the cook, so the engine must schedule it on the device at a moment the player tolerates: a loading screen, a first-launch preparation step, or a background thread before the content is needed. The driver's output is kept in a persistent cache, and a **driver update usually invalidates it**, which is why the first session after an update may compile again. ## Deciding which variants to build Compiling every theoretical combination of switches is usually impossible in cook time and package size. The practical approach is to build the **reachable set**: 1. Enumerate the switch combinations that each material actually enables, rather than every switch of the shared shader. 2. Add the render-state context each variant is drawn with, such as vertex format and output targets, when the platform's compiled program depends on it. 3. Record which variants are actually requested during playtests and automated runs through every level, and ship that list with the build. 4. Precompile the recorded list in the cook where possible, and create the device-specific programs from it at load where the driver must finish them. 5. Keep **telemetry on misses** in shipped builds and feed newly seen variants back into the list for the next patch. The list goes stale whenever content changes, so recording has to be part of the regular build process rather than a one-off before launch. ## Cache layers Several caches cooperate, each answering a different question: - the **shared derived-data cache** used by cook machines, so the same variant is never compiled twice across the team; - the **packaged binaries or bytecode** in the build, so devices skip what the cook could do; - the **on-device cache** of driver-compiled programs, so a second launch does not repeat the first launch's work. Each cache needs a key that changes when its output would change: the variant's switches, the source and its included files, the compiler version and, on the device, the driver version. ## Making a miss survivable No list is perfect, so the runtime needs a policy for the variant it has never seen: - compile it on a **worker thread**, not on the thread building the frame; - draw with a **fallback** variant (a simpler shader for the same material) or skip that draw for a frame or two until the compile finishes; - log the miss with the variant's switches so the team can add it to the recorded list. ## Diagnosing the hitch A frame-time capture that shows a long stall inside the renderer's request for a program, coinciding with the effect's first appearance and disappearing on the second, points to a variant compile. Confirm it by logging every on-demand compile with its duration, then check whether the variant was missing from the recorded list, rejected by a stale on-device cache, or never cooked for this platform.
- Why can a first launch after a graphics-driver update suddenly take much longer to load?Programs compiled by the driver are cached on disk keyed by driver version, because a new driver may generate different machine code. After an update the cached entries no longer match, so the engine's warm-up step, or the first use of each variant, compiles them again. Games that precompile at load show this as a longer preparation step; games that compile on demand show it as hitches.
- How would you keep the recorded list of shader variants from going stale?Make recording part of the build process: automated runs fly through every level and trigger every effect, logging requested variants, and the build fails or warns when a cooked level requests a variant absent from the list. Shipped builds report misses with their switches, and those reports are merged into the list for the next patch.
- Compile every possible permutation at startup so no miss can ever happen.
- Shader variants are always fully compiled in the cook, on every platform.
- A shader compile on first use is cheap enough to hide inside one frame.
- Once the on-disk shader cache is filled, it stays valid across driver updates.
- Compiling the missing variant synchronously is fine because the frame then renders correctly.