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.

source pass.slang + included files + defines, entry point shader compiler parse, check, optimize SPIR-V GPU-neutral driver + blend, formats, samples → GPU machine code shader cache a file per key pipeline cache the driver's bytes, one file tens to hundreds of ms per big shader a few ms per pipeline, more on some drivers
The program owns the first cache and decides its key. The driver owns what is in the second one; the program only stores its bytes and hands them back.

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.

pass.slang lighting.slang edited, not in the key same key old code returned

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.

pass.slang lighting.slang #define MASK 1 fragMain compiler version target, options new key compiled again

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.

Pick a change, then switch the key between "top file only" and "every input". A stale row is a cache hit that returns old code: the bug a missing input causes.

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
3 × 10−864-bit key, a million inputs1012 / 265: small, but over many users and years not zero
1.5 × 10−27128-bit key, a million inputs1012 / 2129: never in practice
Store the key, compare it on read

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.

magic 4 B version 4 B key 16 B size 8 B checksum 8 B SPIR-V 10 to 300 KiB reflection struct sizes, offsets header, 40 bytes: checked before the payload is used payload: its size and checksum are in the header one file per key
A wrong magic, a short file or a wrong checksum is a damaged file. A good file with another version or key is stale. Both are a miss: compile, and write the entry again.

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.

the program's header vendor, device, driver version, UUID, size, checksum Vulkan's header size, version, vendor, device, UUID driver data opaque all equal to this GPU and driver? → hand the data to vkCreatePipelineCache anything else → log one line, start with an empty cache
The driver checks its own header too, but some drivers crash on data from another device instead of refusing it. A new driver version usually brings a new UUID; checking the version as well costs nothing.
GPU A GPU B a.pipelines b.pipelines

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.

this run other run merged file

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).

loaded = at exit no write

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.

new bytes missing crash here: a damaged 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.

x.1a2b.tmp rename x old file or whole new file

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:

  1. 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.
  2. 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.

Renderer::create*Pipeline once per stage compileFromString key = baseKey + name + entry point + source shaders/<key>.shader readCachedShader hit miss SPIR-V + struct layouts compileWithSlang then writeCachedShader validateRasmStructs C++ structs vs the layouts RenderContext::createPipeline the set layout's pipeline layout vkCreate*Pipelines with VulkanPipelineCache::get() vulkan-<vendor>- <device>.pipelines load / save base key, once at startup: cache version, Slang build tag, target, profile, every compiler option, every file of shaders/lib (path and bytes). The pipeline cache loads when the device starts and saves when it ends.
A hit skips Slang only. The struct check, the shader module and the pipeline are made as without a cache, and the pipeline cache lets the driver skip its own compile.

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 pageRASM
Included filesThe 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 optionsgetBuildTagString 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 pointThe 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.
KeyCacheKey, 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 formatreadCacheFile / 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.
ReflectionEach 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 cacheVulkanPipelineCache: 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 limitpruneCacheFolder: past MAX_SHADER_CACHE_BYTES (256 MiB), the files used longest ago go, down to 3/4. A hit sets the file's time.
OffcacheDir = "". 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.

1.02 s09_gltf_materials, warm2.43 s with no cache; 2.49 s cold (writing the entries)
0.98 s15_debug_ui, warm1.71 s with no cache; 1.72 s cold
1.54 s02_gltf, warm2.30 s with no cache; the rest is mesh import
14 msmaking 09's 24 pipelines, warm1449 ms with no cache, 1389 ms of it in Slang (Tracy)

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

WhatWhere
Key, cache files, pruningsrc/core/cacheFile.h, src/core/cacheFile.cpp
Shader entriesreadCachedShader, writeCachedShader in src/renderer/shaderCache.cpp
Base key, hit or compile, struct checkShaderCompiler::initialize, compileFromString, compileWithSlang in src/renderer/shaderCompiler.cpp
Pipeline cachesrc/rhi/vulkan/vulkanPipelineCache.cpp; loaded in VulkanDevice::initialize, saved in ~VulkanDevice
The folderresolveCacheDir in src/platform/paths.cpp, called from Engine::Engine
Teststests/cacheFileTests.cpp, tests/shaderCacheTests.cpp, tests/pathTests.cpp
Rules to keep

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