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.

0 16 ms first frame: 6 compiles first see-through draw: 70 ms first animated model: 95 ms time →
The cost comes at first use, which can be minutes into a run. The disk cache removes it on the next run only.

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.

cull opaque sky tone

Passes

Each pass has one shader, or one per stage: compute, or vertex and fragment.

opaque cut-out see-through

Variants by material

A #define per alpha mode: a few copies of the same source.

1 sample MSAA + motionTAA

Variants by target

Each kind of target the scene draws into needs its own output code: one shader per kind.

debug pick

Rare variants

Debug views, picking: known, but a run may never need them. Queue them last, or only when asked.

Formats do not count. A pipeline depends on the target's formats, but its shader code does not. Warm the shaders; the pipeline objects themselves are then quick to make from the driver's cache.

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.

  1. startup: list every shader, in the order of need
  2. workers take the front of the queue and compile
  3. each result goes to memory and to the disk cache
  4. a frame makes its pipeline from the result: no compile
Set the workers to 0 to see compiles at first use. Then add workers, and reverse the queue to see why its order matters. Purple bars are compiles, amber ones a worker making its compiler session; red marks are frames that waited.

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 draw

Ready

The frame takes the code from memory and makes the pipeline: well under a millisecond.

worker: compiling frame waits draw

Being compiled

The frame waits for that one shader. The others go on in parallel, so the wait is at most one compile.

main compiles it

Still queued

The main thread takes it off the queue and compiles it, so no worker compiles it a second time.

shaded debug

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.

Never drop a scene draw. A missing object for one frame is a visible pop. A frame that cannot draw an object without a shader waits for it.

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.

While the queue has work worker 1 session ~100 MB worker 2 session ~100 MB worker 3 session ~100 MB worker 4 session ~100 MB Queue empty sessions freed: the workers keep only their threads
More workers finish sooner and cost more memory for that time. A warm cache needs no compile, so it makes no session at all.

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

parse + check vertex parse + check fragment

Grouped by source

parse + check vertex fragment

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 #define lines, 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.

Renderer constructorwarmPipelines() a view on a new kind of targetwarmScenePipelines(first) queue front: needed first RASM shaders workers a Slang session each,made on a cache missdisk cache → memory a pass: createScenePipeline → compileFromStringready: take it · running: wait for it · queued: compile it here
Nothing new happens inside a frame on a warm run: every shader comes from the disk cache, read on a worker.
This pageRASM
The list of shaderswarmPipelines 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 sidesgetShaderVariant 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 firstShaderCompiler::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 threadSlangContext: 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 sourceA 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 countRendererConfig::shaderThreads (and EngineConfig, shader_threads in the C API): 0 takes half the hardware threads, at most 8.
Thread priorityWorkerPool 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 threadsFirst frameLongest later framePeak working set
none (before the warm-up: compiled at first use)759 ms451 ms, and more at each first use510 MB
2323 ms380 ms808 MB
4302 ms226 ms1004 MB
8 (the default here)280 ms84 ms1381 MB

A warm run makes no session: same frames and startup as without the warm-up, and about 10 MB more memory.

Rules to keep. A new pipeline gets its shaders in 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