Foundations
Shader warm-up
A pipeline needs compiled shaders, and a shader compiler can take 100 ms for one. If the first frame that needs a pipeline also compiles it, that frame takes 100 ms: a hitch the user sees. This page shows how to compile every shader a frame can need on worker threads, before a frame asks for it, and what to do when a frame asks first. The last part shows how RASM does it.
In short
- A shader compiled in the frame that first needs it stalls that frame for the whole compile. A cache on disk helps only the next run.
- The set of shaders an engine can need is finite: its passes, times their variants (alpha modes, kinds of target). List it, and queue it for worker threads at startup, in the order the frames will ask.
- A frame that asks first: ready, it draws; being compiled, it waits for that one shader; still queued, it takes it off the queue and compiles it itself. An optional draw (a debug view) can skip frames instead.
- A compiler that is not thread-safe needs one session per thread. Each costs memory, so free them when the queue is empty.
- Several entry points of one source share one parse: compile them together on one thread.
1A frame that compiles
Frames take 1 ms. Then a new kind of object arrives, its pipeline is made in that frame, and the compile takes 95 ms.
2The set is finite
An engine does not compile arbitrary shaders. It compiles its own passes, each in a few variants. Count them, and the list fits in a table.
Passes
Each pass has one shader, or one per stage: compute, or vertex and fragment.
Variants by material
A #define per alpha mode: a few copies of the same source.
Variants by target
Each kind of target the scene draws into needs its own output code: one shader per kind.
Rare variants
Debug views, picking: known, but a run may never need them. Queue them last, or only when asked.
3A queue for worker threads
At startup the program puts the whole list in a queue, in the order the frames will ask: first what the first frame draws, then what a scene adds later, rare variants last. Worker threads take shaders from the front. The main thread starts its frames at once.
- startup: list every shader, in the order of need
- workers take the front of the queue and compile
- each result goes to memory and to the disk cache
- a frame makes its pipeline from the result: no compile
4When a frame asks first
A frame can ask for a shader before a worker is done with it, often in the first second. What it does depends on the shader's state, and on whether the draw may wait.
Ready
The frame takes the code from memory and makes the pipeline: well under a millisecond.
Being compiled
The frame waits for that one shader. The others go on in parallel, so the wait is at most one compile.
Still queued
The main thread takes it off the queue and compiles it, so no worker compiles it a second time.
Optional: skip a frame
A debug view or an overlay keeps its old picture until its shaders are ready. A scene draw never does: an object would be missing.
5One compiler session per thread
Many shader compilers keep their state in a session that only one thread may use at a time. Workers cannot share it. Each worker makes its own, the first time it compiles. A session holds the compiler's built-in modules, which are large.
6One parse per source
Most of a compile is reading the source: parse it and check its types. The vertex and fragment stages of one pipeline, or the entry points of one compute file, share that source. Compiled apart, each stage parses it again.
Each entry point alone
Grouped by source
So a worker that takes a shader also takes the other queued shaders of the same source and compiles them in its session one after the other. The code it makes is the same as from a lone compile.
7Pitfalls
- A warmed shader is compiled again at first use
- The warm-up asked for it under another key: other
#definelines, another order, another name. Build the source in one function that both sides call. - The first frames still wait
- The queue is not in the order of need, or a new kind of target appears. Put what the first frame draws first, and move a new target's shaders to the front when it appears.
- Memory jumps at the first start after an update
- One compiler session per worker. Fewer workers cap it; free the sessions once the queue is empty.
- The main thread stalls while waking workers
- A woken worker at the main thread's priority can take its core (Job systems). Run the workers one level below.
- Closing the program during the warm-up hangs or crashes
- Drop the queued compiles, let the running ones end, join the threads, then free the sessions they used.
8In RASM
The ShaderCompiler has a pool of shader workers (WorkerPool, named RASM shaders). At startup Renderer::warmPipelines queues every shader a frame can need; the pipelines are still made where a pass first needs them, and find their code ready.
| This page | RASM |
|---|---|
| The list of shaders | warmPipelines and warmScenePipelines in src/renderer/pipelineWarmup.cpp: the computes, the shaded variants of the configured anti-aliasing, shadows, tone map, environment bake, blend sort, animation, labels, bloom, points; last the debug display, Inspect, the picks and the Export pass. A view on a new kind of target (TAA, MSAA) queues its variants first, from createSceneView. |
| Same key on both sides | getShaderVariant builds a variant's source for the warm-up and for createScenePipeline, createMeshScenePipeline and the picks. A shader compiled on the main thread is counted in the shutdown line (Shaders: ... (N on the main thread)) and logged at debug level. |
| When a frame asks first | ShaderCompiler::compileFromString: ready, waited for, or taken off the queue and compiled on the main thread. With wait false it returns pending: the debug views' pipelines do, and the view draws shaded until all of them are ready. setDebugView queues their shaders first (warmDebugPipelines). Renderer::waitForLoads also waits for the queue (waitForPrefetches). |
| Session per thread | SlangContext: a Slang global session and session per worker (slang.h: a global session is not thread-safe), made on the first cache miss, freed once no prefetch is open. The main thread has its own. |
| One parse per source | A context keeps each module it loaded, named by the source's hash; runPrefetch takes the queued shaders of the same source with the one it takes. The SPIR-V is byte for byte the same. |
| Worker count | RendererConfig::shaderThreads (and EngineConfig, shader_threads in the C API): 0 takes half the hardware threads, at most 8. |
| Thread priority | WorkerPool threads call lowerThreadPriority (Job systems). |
What the warm-up costs and saves, on the first start after a shader change (24_map_tiles --no-cache, Release, RTX 3060, 24 hardware threads; the memory falls back once the queue is empty, after about 1 s):
| Shader threads | First frame | Longest later frame | Peak working set |
|---|---|---|---|
| none (before the warm-up: compiled at first use) | 759 ms | 451 ms, and more at each first use | 510 MB |
| 2 | 323 ms | 380 ms | 808 MB |
| 4 | 302 ms | 226 ms | 1004 MB |
| 8 (the default here) | 280 ms | 84 ms | 1381 MB |
A warm run makes no session: same frames and startup as without the warm-up, and about 10 MB more memory.
warmPipelines or warmScenePipelines, with the name, entry points and #define lines its pass uses. A pipeline whose draw is optional may pass pending and draw without it; a scene draw waits. Only the shader workers and compileFromString's thread use a SlangContext, never two at once. Pipelines, the pipeline cache and the descriptor layouts stay on the main thread.Further reading
- Shader and pipeline caches: the disk cache the workers read and write.
- Job systems and worker pools: the pool and its thread priority.
- Uber-shaders: where the variants come from.
- Debug views: the view that draws shaded until its shaders are ready.