Skip to content

Runtime: use the Vulkan pipeline cache produced by the build (D3D-34) - #367

Merged
ItsNotRexed merged 1 commit into
ProjectLeonida:mainfrom
mrb-eel:runtime/seed-vulkan-pipeline-cache
Nov 23, 2026
Merged

ItsNotRexed merged 1 commit into
ProjectLeonida:mainfrom
mrb-eel:runtime/seed-vulkan-pipeline-cache

Conversation

@mrb-eel

@mrb-eel mrb-eel commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

What changes

D3D-34 adds Vulkan runtime cache seeding from the offline build. Coordination claim.

Validation

  • Rebasing on f847748c and building with MSVC 19.44 and Windows SDK 10.0.26100.0 succeeded. vk_pipeline_check_cli, d3d12_compute, d3d12_draw, and d3d12_set0_compute passed. GPU tests ran with Vulkan validation and synchronization validation enabled on an RTX 4070 SUPER (616.92).
  • Generated a 9,915-byte seed from the repository's synthetic compute shader. A local test interceptor requested Vulkan pipeline creation feedback for the first runtime compute PSO from a different working directory. With the valid seed, feedback flags were 0x3 (valid application-cache hit). With a missing file, a truncated header, or a changed UUID, flags were 0x1 (valid, no application-cache hit). All four runs matched 64 elements across each of three dispatches.

AI-assisted implementation and testing. No game files were used.

@VictorJulianiR VictorJulianiR left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The runtime now consumes the cache emitted by vk_pipeline_check. Its existing save_cache writer (lines 594–602) uses a temporary ofstream without checking write or close, then removes the previous cache and ignores the rename result. For example, a disk-full short write after the header can publish a truncated cache and still print the expected size as saved. The runtime’s device/header check cannot distinguish that prefix from a complete cache. Could the producer check vkGetPipelineCacheData, write and close successfully, then replace the previous file without removing it first? An injected write failure should leave the previous cache readable.

AI-assisted static review of 4e4b338. No project code was executed.

@YaBulochka

Copy link
Copy Markdown
Contributor

Cold-cache numbers for the corpus, since the cache you are adding is exactly what this decides. Measured with vk_pipeline_check over the 26,503-entry index from main (the #223 corpus), --jobs 6 --slow 1, MESA_SHADER_CACHE_DISABLE=1 RADV_DEBUG=nocache.

Hardware: RX 7600 (gfx1102, RDNA3), RADV, Mesa mesa-git 27.0.0, Vulkan 1.4.363, Arch Linux.

stage pipelines total compile mean slowest
cs 2,689 4,809.0 s 1.788 s 122.7 s
ps 12,101 2,636.6 s 0.218 s 4.75 s
ms 9,282 789.1 s 0.085 s 1.91 s
vs 2,236 76.0 s 0.034 s 0.99 s
gs 195 0.6 s 0.003 s 0.00 s

Wall time 1,393.7 s on 6 threads for the whole set, i.e. about 8,311 s of compile work.

Tail: 1,440 pipelines take more than 1 s, 479 more than 5 s, 11 more than 10 s, 6 more than 20 s, 2 more than 50 s. The two worst are the indirect_diffuse_lighting ScreenProbes ActivateProbes pair at 122.7 s and 119.8 s, both wave64 compute; then three RTShadowTrace32TemporalHQ variants at 30-50 s, and the RaytracingResolve material family fills the 3-9 s band. Compute is where the cost is: 823 of the 1,440 slow ones and 4,513 s of the 5,374 s spent above 1 s. Pixel shaders over 1 s are mostly wave64 but never exceed 4.75 s, and mesh never exceeds 1.91 s.

Warm cache is a different world: the same set re-runs in 32.5 s with 13 pipelines over 1 s. So the cache changes first-run cost by two orders of magnitude, and the tail is a small, known set that could be precompiled into the seed.

Two implications for the key: the cache UUID already covers the driver, but on this machine Mesa moved from 27.0.0-devel builds between runs and invalidated everything, which is the expected cost. And a missing seed is not a small regression: one 122 s pipeline is a visible stall at level load even if everything else is cached.

I can post the full list of the >1 s shaders (names only) if that helps pick what to precompile.

@YaBulochka

Copy link
Copy Markdown
Contributor

Clean rerun, replaces the numbers I posted earlier in this thread: those were taken while another job was using the CPU, and the wall times were inflated.

Setup: RX 7600 (gfx1102), RADV, Mesa mesa-git 27.0.0, Vulkan 1.4.363, 6 threads, --slow 0, full graphics and compute pipelines with stubs, no pipeline_cache.bin passed. Dedicated MESA_SHADER_CACHE_DIR: cold on an empty dir with 16G, then warm with the default limit, then warm with 16G, then a control with MESA_SHADER_CACHE_DISABLE=1.

run wall gfx hits/misses compute hits/misses
cold, empty cache dir 1,311.2 s 9,779 / 14,035 124 / 2,565
warm, default 1 GB 28.7 s 23,814 / 0 2,689 / 0
warm, 16 GB 23.8 s 23,814 / 0 2,689 / 0
control, cache disabled 1,253.1 s 9,776 / 14,038 124 / 2,565

So the earlier 32.5 s was pure hits, not a mix: the warm runs have zero misses. The cold and control runs still show ~9.8k gfx hits, and the control has the disk cache disabled, so those hits are duplicates inside the corpus (the same graphics pipeline rebuilt for another variant), not a warm disk cache.

Cache after the cold run: 436 MiB, 30,493 files, median entry 341 B. The default 1 GB limit is enough for this corpus; 16G changes nothing (23.8 vs 28.7 s is noise), so no MESA_SHADER_CACHE_MAX_SIZE change is needed here.

Cold percentiles, seconds:

stage n sum p50 p90 p99 max >1 s
CS 2,689 4,625 0.2 5.7 7.8 124.0 785
PS 12,101 2,403 0.0 0.8 1.7 5.0 535
MS 9,282 644 0.0 0.2 0.7 2.0 29
VS 2,236 62 0.0 0.1 0.5 1.1 2
all 26,503 7,734 0.0 0.6 5.7 124.0 1,351

Compute is 10% of the pipelines and 60% of the compile time. The clean tail is two ScreenProbes ActivateProbes at 124.0 s and 108.2 s, ActivateRays at 47.9 s, then RTShadowTrace32 at 29–31 s. That tail is module size, not cache: the two largest are 47 MB modules, which is #881.

For a seed: cold work is 7,734 s of compile time, warm is 28.7 s, so a cache that covers the tail is worth a lot. The cache file itself is 436 MiB for the whole corpus, so a full seed is above the 200 MB target; a tail-only seed would be far smaller, and the two ScreenProbes plus the RTShadow family are the place to start.

@YaBulochka

Copy link
Copy Markdown
Contributor

Seed measurement, local, nothing committed. The short version: a top-N seed does not meet a 200 MB / 50% budget; 50% needs about 260 MB, and the cost curve is broad rather than a handful of shaders.

Setup: RX 7600 (gfx1102), RADV, Mesa mesa-git 27.0.0, 6 threads. vk_pipeline_check <full corpus> <seed.bin> with a fresh MESA_SHADER_CACHE_DIR per run, so only the VkPipelineCache file carries state. Baseline with no seed: 1,163.8 s (cold with an empty disk cache was 1,311 s; the baseline is the like-for-like reference for the seed runs).

Seeds were built from a directory holding only the shaders whose cold time covers 25/50/75% of the 7,734 s total, then loaded into a full-corpus run:

coverage shaders seed size full run with seed reduction compute hits
25% 240 118.6 MiB 855.0 s 26.5% 364
50% 614 259.4 MiB 605.8 s (rerun 632.8) 47.9% (45.6%) 730
75% 2,060 433.5 MiB 328.1 s 71.8% 952

So:

  • under 200 MB you get roughly a quarter of the time, not half;
  • 50% costs about 260 MB, and the rerun says 45.6–47.9%, right at the target but above the size;
  • 75% is 433 MB.

The curve is broad: 25% of the cost is 240 shaders, 50% is 614, 75% is 2,060 out of 26,503. This is not a "precompile the ten worst" problem.

If a hard 200 MB budget matters, select by time per byte, not by top-N time. The two ScreenProbes ActivateProbes are 124.0 and 108.2 s but are 47 MB modules each (#881), so they are likely the worst value per cache byte and should be candidates for exclusion; medium shaders should fill the seed instead. I can measure that variant if useful, but the expectation is roughly 35–40% at 200 MB, not 50%.

Key stays (GPU + Mesa build + enabled features), and the seed is dead after a driver update.

@ItsNotRexed

Copy link
Copy Markdown
Member

#291's stack is on main now (through #1083, at 99f9989): the GPU address arena, shader_library, PSOs and draws, and the M0/M1 tests. This PR still carries the old #291 commits, so it conflicts in d3d12_draw_test, d3d12_present_test, shader_library_test, vk_pipeline_check and vk_test_device. Could you rebase only the pipeline-cache part onto main? It's what lets a first boot reuse the build's pipeline_cache.bin, and I'll merge it once ctest and the GPU suites pass.

@AlxcBTW

AlxcBTW commented Nov 22, 2026

Copy link
Copy Markdown
Member

@mrb-eel, a heads-up so our PRs don't collide: #1865 (M5) adds one VkPipelineCache per device for graphics and compute PSOs (it was VK_NULL_HANDLE before), plus VkPipelineCreationFeedback hit counting (rt::d3d12::pipeline_cache_stats). It creates an empty cache: no seeding, nothing written to disk.

Your PR does the seeding, i.e. maps pipeline_cache.bin and checks the vendor, device and UUID. That fits on top of #1865's cache: create it from your seed instead of empty. Whichever lands first, I'm happy to do the merge, or to move my creation into your lazily initialised helper if you'd rather keep that.

AI-assisted (Claude Code).

@mrb-eel
mrb-eel force-pushed the runtime/seed-vulkan-pipeline-cache branch from 4e4b338 to 58226b4 Compare November 22, 2026 20:28
dev-ewann pushed a commit to dev-ewann/Leonida that referenced this pull request Nov 22, 2026
The README said the pipelines stage saves a pipeline cache "so the first
boot doesn't compile them". Nothing in runtime/src or runtime/shims reads
pipeline_cache.bin yet (only vk_pipeline_check in runtime/test writes it;
D3D-34 / ProjectLeonida#367 is meant to load it), so the sentence promised something the
runtime does not do.

Reword it to what make_game.py does: create every translated shader on the
GPU driver, check that they compile, and save the cache to
pipeline_cache.bin in --out, and say the runtime does not load it yet.

Claimed in ProjectLeonida#2716. Written with AI assistance (Claude, via OpenBot).
Tested: python -I tests/test_docs_{design_rows,links,list_items,repo_paths}.py,
tests/test_i18n_index.py and tests/test_text_write_line_endings.py pass
(Windows 11, docs only; no GPU involved).

Co-authored-by: OpenBot <335604065+openbotgit[bot]@users.noreply.github.com>
@ItsNotRexed
ItsNotRexed merged commit 42e20d6 into ProjectLeonida:main Nov 23, 2026
7 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants