Foundations
Shader and pipeline caches
Before the first frame, every shader is compiled twice: once from its source language to a GPU-neutral code, and once more by the driver to the GPU's own machine code. Both results depend only on their inputs, so a program can keep them on disk and skip the work on the next start. This page explains what such a cache must get right: a key that sees every input, files that cannot be half written, and data from a driver that must be checked before use. The last part shows how RASM does it.
In short
- Two compiles: the shader compiler turns source into SPIR-V (or DXIL, or Metal IR), and the driver turns that into GPU code when a pipeline is made. Each has its own cache.
- A cache entry is found by its key, a hash of every input of the result: the source, every included file, every define, the entry point, the compiler's version and options. One input left out, and an edit returns old code.
- The key is the only thing that tells two inputs apart, so it must be wide (128 bits) and stored in the file, to be compared on read.
- Whatever else the program needs from the compiler (reflection: struct layouts, offsets) goes into the same entry.
- The driver's pipeline cache is opaque bytes for one GPU and one driver. Check its header before you hand it back, and start empty when anything differs.
- Write through a temporary file and a rename, so a crash or a second program never sees half a file. Keep the folder under a size limit.
1Two compiles before the first frame
Follow one shader from its text file to the GPU. The program's compiler runs first; the driver runs when the program makes a pipeline with it.
Both steps are pure: the same inputs always give the same output. That is what makes a cache safe. The whole difficulty is to know every input.
2A key made of every input
A shader cache looks up a result by a key. The key is a hash of the inputs. If an input is not in the key, a change to it gives the same key, and the cache returns the code of the old input.
The top file only
An edit of an included file, a new define for a variant or a new compiler version gives the same key. The program runs with stale code and nobody sees why.
Every input
Anything that can change the output is hashed: each included file's bytes, each define, the entry point, the compiler's version, target and options.
Two ways to know the included files. A manifest stores, with each entry, the list of files the compiler read and their hashes, and checks them on the next start. Or hash the whole include folder once at startup: simpler and always right, but an edit of any shared file misses every entry. Since most shaders include the same core file, the two usually miss the same entries anyway.
Hash each input with its length first. Without it, "ab" then "c" and "a" then "bc" give the same bytes, and so the same key.
Try it. Each row below is one cache entry: a shader, its entry point and its defines. Change one input and see which entries the next start compiles, and which it reads. With the top file as the key, some entries come back stale.
3How wide the key must be
Two different inputs can hash to the same key by chance. Then the cache returns another shader's code, with no error. The chance grows with the square of the number of entries ever made:
p ≈ n² / 2b + 1
- n
- how many different inputs were ever hashed (every edit makes new ones)
- b
- the key's width in bits
- p
- the chance that any two of them share a key
Name the file after the key, and also write the full key into the file's header. A read compares it. Then a short file name, a copied file or a bug in the naming can only cause a miss, never a wrong hit. The hash does not need to be cryptographic: nobody attacks a local cache, it only has to spread accidental inputs well.
4What an entry holds
The compiled code is not always all the program needs. An engine that checks its CPU structs against the shader's structs, or shows a GPU buffer's fields in a debug tool, reads the compiler's reflection. On a cache hit the compiler does not run, so the reflection must be in the entry too.
Checking the CPU structs against the stored reflection on every start, hit or miss, keeps that check as strong as without a cache. A CPU struct that changed is caught, and rebuilding the program does not throw the cache away.
5The driver's pipeline cache
The second compile belongs to the driver. Vulkan gives the program a VkPipelineCache: pass it when making pipelines, read its bytes before exit with vkGetPipelineCacheData, and pass them back on the next start. D3D12 has pipeline libraries and Metal binary archives for the same job. The bytes are the driver's own format, valid only for that GPU and that driver.
One file per GPU
Name the file after the vendor and device IDs, so a machine with two GPUs does not throw one cache away each time it switches.
Merge on save
Two programs started together each loaded the old file. Before writing, read the file again and merge what the other one saved (vkMergePipelineCaches).
Write only changes
Compare a checksum of the data at exit with the one loaded. A start that made no new pipeline writes nothing.
6Writing a file that is never half written
A program can crash while it writes, and another program can read the file at the same moment. Writing in place lets both of them see half a file.
In place
The next start reads a damaged file. With checks it is only a miss; without them, the driver may crash on it.
Temporary file, then rename
A rename replaces the file in one step. A name of its own per write lets two programs write the same entry at once.
7Keeping the folder small
Every edit makes new keys, and the old entries stay. Two limits keep the disk use bounded:
- Shader entriespast the folder's limit, delete the files used longest ago. A hit updates the file's time, so the files in use stay.
- Pipeline cacheone file that only grows: a driver cannot drop the pipelines no run makes any more. Past its limit, delete it and start a new one.
8Pitfalls
- An edit does not show up
- An input is missing from the key: an included file, a define, the compiler version. Hash every input, or the whole include folder.
- A shader from another file is used
- A key collision, or a file name that is a short form of the key. Use a 128-bit key and compare the full key on read.
- A crash at startup after a driver update
- The pipeline cache was handed over without a check. Compare vendor, device, driver version and UUID first.
- Tools break on a cache hit
- They read reflection, which only a compile produces. Store it in the entry.
- Tests give different results from run to run
- A cache from an earlier run changes the path a test takes. Turn the cache off in tests that must be repeatable.
- An installed program cannot write its cache
- The program's own folder is often read-only. Fall back to the user's local cache folder (%LOCALAPPDATA%, ~/Library/Caches, ~/.cache), not a roaming one.
9In RASM
EngineConfig::cacheDir is the folder of both caches: cache beside the executable by default, an absolute path as it is, and "" for none. resolveCacheDir (in src/platform/paths.cpp) makes the folder and writes a test file; if that fails for a relative path, the caches go to the user's local cache folder plus rasm/<executable name>, with one log line; if that fails too, the engine runs without them.
The reads, compiles and writes of this figure mostly run on the shader workers: at startup warmPipelines queues every shader a frame can need, and a pipeline then finds its SPIR-V in memory (Shader warm-up). Two workers never write one entry: each shader is queued once.
| This page | RASM |
|---|---|
| Included files | The whole folder: every file under shaders/lib (relative path and bytes) is in the base key. A module that read a file outside it (IModule::getDependencyFilePath) is compiled but not cached. |
| Compiler and options | getBuildTagString of the Slang global session (the release, such as 2026.2), and the Slang library file's name, size and write time (getModulePath), since a patched build of one release keeps the tag; the target's format and profile, and every entry of the session's and the target's option arrays, hashed from the arrays the session is made with, so a new option changes the key by itself. |
| Defines and entry point | The variants' defines are lines at the top of the source (Renderer::createScenePipeline), so the source string holds them. The module name and the entry point are hashed too. |
| Key | CacheKey, FNV-1a 128 (hashBytes, hashString with the length first). The file is shaders/<32 hex digits>.shader, and its header holds the key again. |
| File format | readCacheFile / writeCacheFile in src/core/cacheFile.cpp: magic, version, key, size, FNV-1a 64 checksum, then the payload. Both caches use it. CacheFileStatus says MISSING, STALE or CORRUPT. |
| Reflection | Each entry holds the push constants' struct name and every struct pc reaches (StructLayout: size, members, flattened fields). validateRasmStructs checks them on every compile and every hit; the inspector's GPU tables section reads them through ShaderCompiler::findStructLayout. |
| Pipeline cache | VulkanPipelineCache: checks the file's identity (vendor, device, driver version, UUID) and Vulkan's own header, merges what another run saved, writes only when the data changed. Over 256 MiB it is deleted. |
| Size limit | pruneCacheFolder: past MAX_SHADER_CACHE_BYTES (256 MiB), the files used longest ago go, down to 3/4. A hit sets the file's time. |
| Off | cacheDir = "". The image tests and the unit tests turn it off; every example takes --no-cache. |
Release, headless, RTX 3060, --frames 1 --wait-for-loads, median of 5 starts. Cold is the first start with an empty cache folder; warm is every start after it.
On this NVIDIA driver the pipeline cache saves little (about 35 ms in 09), because the driver keeps its own cache of compiled shaders too. Drivers without one, such as lavapipe, MoltenVK and many mobile drivers, gain more.
Where it lives in code
| What | Where |
|---|---|
| Key, cache files, pruning | src/core/cacheFile.h, src/core/cacheFile.cpp |
| Shader entries | readCachedShader, writeCachedShader in src/renderer/shaderCache.cpp |
| Base key, hit or compile, struct check | ShaderCompiler::initialize, compileFromString, compileWithSlang in src/renderer/shaderCompiler.cpp |
| Pipeline cache | src/rhi/vulkan/vulkanPipelineCache.cpp; loaded in VulkanDevice::initialize, saved in ~VulkanDevice |
| The folder | resolveCacheDir in src/platform/paths.cpp, called from Engine::Engine |
| Tests | tests/cacheFileTests.cpp, tests/shaderCacheTests.cpp, tests/pathTests.cpp |
Anything new that changes the SPIR-V goes into the key: a new compiler option goes into the option arrays (they are hashed), a new search path's files go into the base key, and a new way to pass defines must reach the source string. Change SHADER_CACHE_VERSION when the entry's payload changes, and SHADER_CACHE_KEY_VERSION when the key's inputs change. Never use a cached result without its stored key compared.
Further reading
- Khronos, the Vulkan specification, "Pipeline Cache" (
VkPipelineCacheHeaderVersionOne,vkMergePipelineCaches). - Arseny Kapoulkine, "Robust pipeline cache serialization" (the header checks some drivers need).
- Uber shaders and shader permutations, Struct layout rules, The Vulkan loader.