Skip to content

d3d12: persist the runtime's Vulkan pipeline cache across launches (framed and checked file, merged across sessions, never the build seed; 26x faster PSO creation on the second launch) - #7396

Open
tedin7 wants to merge 3 commits into
ItsNotRexed:mainfrom
tedin7:fast-psocache

Conversation

@tedin7

@tedin7 tedin7 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

@ItsNotRexed: an exe PR, as you asked.

Claimed in #7049 (comment)

AI-assisted (Claude).

What

The runtime's VkPipelineCache now persists across launches:

  • At device teardown it saves the cache, with every pipeline the session added, to pipeline_cache.user.bin beside gta6rt.exe (or to LEONIDA_PIPELINE_CACHE). The file is the driver's data behind a 32-byte frame (magic, byte count, hash). It is written to a staging file in the same folder and renamed over the old one, so a crash or a full disk leaves the previous file; the rename waits up to 5 s for another process that holds the old file.
  • At first use the runtime starts from that file when it is whole (frame, byte count, hash), its VkPipelineCacheHeaderVersionOne matches the device (vendor, device ID, pipelineCacheUUID) and it is at least as new as the build's pipeline_cache.bin; otherwise from the seed, as before. The file is read with ReadFile, not a mapping, so a read error on a share that went away is a failed load, not an exception.
  • The build's pipeline_cache.bin is never written. The seed and session paths are resolved to absolute paths once, when the device is made (so a later change of working directory or of a link cannot move the save), and at the save the session file is compared with the seed by file identity again. A LEONIDA_PIPELINE_CACHE that names the seed is refused. If the seed changed while the session ran, the session does not save (its file would outrank the rebuilt seed); the next start uses the new seed.
  • If another session saved the file while this one ran, its pipelines are merged in (vkMergePipelineCaches) before this one saves, so the last writer does not drop them.
  • Never fails the run: every save error (read-only folder, full disk, a directory in the way, a file name the code page cannot hold) is logged, in UTF-8, and ignored; nothing in the teardown path can throw.
  • It saves only if the session created at least one pipeline that missed the cache (creation feedback; a pipeline whose feedback is missing or not valid counts as a miss; without feedback, a cache that grew). A cache over 1 GiB (LEONIDA_PIPELINE_CACHE_MAX_MB changes the limit) is not saved (one log line says so) and a session file over it is not loaded; the build's seed is not limited.
  • LEONIDA_PIPELINE_CACHE=off loads and saves no session file; CTest sets it for every test except d3d12_pipeline_cache_persist, so tests leave no pipeline_cache.user.bin.
  • Pipeline creation asks for VK_EXT_pipeline_creation_feedback (core on the Vulkan 1.3 path; VulkanDeviceInfo::pipeline_creation_feedback). At teardown the log says N pipelines created in X ms, H of them from the pipeline cache; rt::d3d12::pipeline_cache_stats returns the same numbers for tests.
  • pipeline_cache_file.h and pipeline_cache_snapshot.h move from runtime/test/ to runtime/src/ (the runtime now uses them; the pipeline checker and their tests include them from there).

Why

#367 seeds the cache from the build, but the runtime destroyed it at teardown, so every pipeline it created while playing (anything the build's corpus did not have, or a seed that does not match the driver) was compiled again next launch. Finding by @cherryinbubbles-ops (#7049 (comment)), who measured 64 synthetic PSOs going from 16.67 ms to 1.37 ms with the cache saved and reloaded. The second commit answers an automated review of the first (truncated files, a seed that could be overwritten, lost parallel sessions and more).

Tested

d3d12_pipeline_cache_persist_test (new): D3D12 devices on a Vulkan device, again and again in one process, compute PSOs made from hand-written SPIR-V no-ops that differ in workgroup size (each PSO is a distinct pipeline; no game data). pipeline_cache_store_test (new, no GPU): the frame, the save gate and file identity. Under Wine on an RTX 4090 (fast_test_exe.sh) both pass; d3d12_compute_test (run with LEONIDA_PIPELINE_CACHE=off, which leaves no file), pipeline_cache_file_test and pipeline_cache_snapshot_test pass and vk_pipeline_check builds.

Pipeline creation time, summed from the driver's feedback for 16 PSOs, median of 5 runs (min to max):

  • session 1, no cache file: 0 of 16 from the cache, 11.83 ms (11.72 to 12.51)
  • session 2, the file session 1 saved (89,370 bytes of driver data): 16 of 16 from the cache, 0.45 ms (0.44 to 0.55), about 26 times faster
  • session 3, junk file (an empty application cache in a warmed-up process): 0 of 16, 3.72 ms (3.52 to 4.25)
  • through the whole CreateComputePipelineState call (shader lookup, SPIR-V parsing and the rest included): 18.9 ms in session 1 and 6.6 ms in session 2; the save takes 1 to 2 ms
  • tiny shaders on a driver whose own disk cache was warm; the real corpus shaders take seconds each (docs/design.md), so the saving there is the compile time itself. Not measured with game files.

Each fix of the review has a check that fails on the first commit (c2ee65668) and passes now: the new test and unit test built against that commit's d3d12_vulkan_device.cpp, with the Wine-only file name swapped for an ASCII one, fail at: a truncated or changed file reaching the driver (loaded_bytes not 0), a session file held open for writing and mapped (not loaded; not replaced), two sessions that learned in parallel (the third does not find all 24), the seed rebuilt during a session (a file saved anyway), the seed overwritten after the working directory changed, a name made another name for the seed after the check. With the file name 漢字Ж.bin the first commit terminates the process (exit 3: path::string() throws under this Wine's code page); now it passes. LEONIDA_PIPELINE_CACHE=off is checked in the working directory, the scratch folder and the executable's folder, and fails when is_off is made to return false.

What is not covered, honestly:

  • A link retargeted between the check and the save: Wine leaves a symbolic link to a DOS path dangling, so that block skips itself here (it runs where links work). The same hole is closed by resolving once and by the identity check, both tested with the working directory and a hard link.
  • An EXCEPTION_IN_PAGE_ERROR from a lost share cannot be produced here; the check is the file held open for writing, which the old read-only-sharing mapping refused. The seed is still mapped, as in Runtime: use the Vulkan pipeline cache produced by the build (D3D-34) #367 (reading a 3.8 GB seed into memory instead would cost more than it saves).
  • A driver that clears the valid feedback bit on a successful creation cannot be made to; the gate is tested as a function (learned_something), and the device test no longer exits early: it checks the file, the load, the unchanged file, the junk, the merge and the seed first and only then skips (77) if the counters showed no valid feedback; a device without feedback (the Vulkan 1.2 path) is run on purpose and must save.
  • Two sessions that save at the same moment can still lose one of the two (the merge reads the file once, then renames).

Notes for review:

  • Saving writes the whole cache, not a delta (Vulkan has none), so it is capped: the full-corpus seed is 3.8 GB, over the 1 GiB limit, so with that seed the runtime saves nothing and logs one line (not saving the pipeline cache: N bytes is over the 1024 MB limit); set LEONIDA_PIPELINE_CACHE_MAX_MB to raise it.
  • runtime(pipeline_cache_snapshot): guard headroom capacity addition against size_t overflow #6238 moves/edits pipeline_cache_snapshot.h; whichever lands second follows the rename.

…the next session from it

The device destroyed its VkPipelineCache without keeping the pipelines the session created. It now saves the cache to pipeline_cache.user.bin beside the executable (or LEONIDA_PIPELINE_CACHE) through a staging file and a rename, loads it first when its header matches the device and it is not older than the build's pipeline_cache.bin, never writes the seed, and never fails the run on an I/O error. Pipeline creation reports VK_EXT_pipeline_creation_feedback, logged at teardown and read by d3d12_pipeline_cache_persist_test.
…under 1 GiB; LEONIDA_PIPELINE_CACHE=off for tests

A session whose pipelines all came from the cache writes nothing, and a cache over LEONIDA_PIPELINE_CACHE_MAX_MB (default 1024) is skipped with one log line instead of writing the full-corpus 3.8 GB at exit. LEONIDA_PIPELINE_CACHE=off loads and saves no session file; CTest sets it for every test but d3d12_pipeline_cache_persist.
…red between sessions

A session file is the driver's data behind a frame with its byte count and hash, read with ReadFile (a cut or changed file never reaches the driver). Seed and session paths are resolved once at device creation and compared by file identity at the save; a seed rebuilt during the session stops the save; pipelines another session saved are merged in first; the replacement waits for a reader holding the file; names are logged in UTF-8 so nothing throws at teardown; a creation without valid feedback counts as a miss. Tests cover each, and LEONIDA_PIPELINE_CACHE=off is checked in the working directory too.
@tedin7

tedin7 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Note on overlap: #7360 (@aslan2142) and #7384 (@ddaikodaiko) do the same item, tracked as D3D-704. Our claim came first, at 15:56 UTC in #7049 (#7049 (comment), item 2); the D3D-704 listing and both claims came later, at 16:17 and 16:29. Whichever lands, what this one adds:

  • a framed, hashed session file, so a truncated or changed file never reaches the driver;
  • a merge of another session's save before ours (vkMergePipelineCaches);
  • the build seed protected by file identity (a hard link or a changed working directory doesn't overwrite it);
  • a skipped save when the seed was rebuilt during the session;
  • a 1 GiB cap;
  • LEONIDA_PIPELINE_CACHE=off set for every other CTest test, so tests leave no files.
    Maintainers, pick one. I'm happy to close this in favour of either if you prefer, or to port these checks there. Known limits: two sessions saving at the same instant can still lose one's additions, and a seed rebuilt between the check and the rename is a narrow race. Both are written in the body. AI-assisted.

@skorex

skorex commented Nov 23, 2026

Copy link
Copy Markdown

This looks like a duplicate of #7360 (D3D-704). #7360 seems the one to keep: it was claimed under the D3D-704 ID in #7231, it is mergeable, and it has been checked independently on native Windows + MSVC (RTX 3080, RTX 5070 Ti, AMD iGPU).

Thanks for offering to port your checks: the framed/hashed session file, the save gated on creation feedback and the merge with another session's save would all be welcome on #7360. Note that all three PRs save the full cache, seed included. With the 3.8 GB corpus seed that means either a huge rewrite (#7360) or no save at all (the caps here and in #7384).

@hiddenblur

Copy link
Copy Markdown

Side-by-side check of #7396 (efd81f79d) and #7360 (32232957a), since both serve D3D-704 (and #7384 too, not reviewed here). Windows 11, MSVC (VS 18) via CMake + Ninja, NVIDIA RTX 3080, driver 32.0.16.1088, pinned Vulkan-Headers 6802bb47; no game files.

Tests: #7396 pipeline_cache_store_test, pipeline_cache_file_test and d3d12_pipeline_cache_persist_test pass; #7360 pipeline_cache_persist_test and pipeline_cache_file_test pass. #7360's GPU test isn't built here (no glslangValidator). In #7396's GPU test: 16 PSOs, session 1 with no file 212 ms of creation and 0 hits, session 2 from the saved file 0.69 ms and 16/16 hits, and after two sessions saved in parallel the next one gets 24/24.

Damaged file after a valid header (the case #7360 passes to the driver and #7396's LPCS frame rejects first): a probe made a real 90,684-byte cache from the same 16 no-op SPIR-V shaders, then gave damaged copies with the 32-byte header intact to vkCreatePipelineCache and created the 16 pipelines again. NVIDIA refused 7 of 8 outright, with no crash: truncated to half, last byte cut, only 16 payload bytes, 64 bytes inverted mid-payload or at the start, last quarter zeroed, random payload. It accepted the eighth, one bit flipped near the end, and served 15/16 from it (the damaged entry was recompiled). Same result on two runs. So on NVIDIA the header-only check in #7360 is safe, and #7396's hash is defence in depth; other drivers are untested.

Differences that matter more:

#7396 #7360
integrity frame with byte count and hash, read with ReadFile Vulkan header only, then the driver
two sessions at once merges the other session's save first last writer wins
full ~3.8 GB seed over the 1 GiB cap: saves nothing (logged, LEONIDA_PIPELINE_CACHE_MAX_MB) no cap: rewrites ~3.8 GB after any session that adds a pipeline
build seed paths resolved once, file identity checked, no save if the seed changed separate per-GPU file, never the seed
other tests LEONIDA_PIPELINE_CACHE=off set in CTest leave pipeline_cache_runtime_*.bin in the build folder
headers moves pipeline_cache_file.h / pipeline_cache_snapshot.h to runtime/src includes them from runtime/test

The last-but-one row in both is the open problem: because the cache is saved whole, the seed's entries go into every session file, so with a full-corpus seed one PR saves nothing and the other rewrites gigabytes. Keeping the session's own misses in a small cache of their own (and merging it with the seed at load) would give a file the size of what the session learned.

AI-assisted. Synthetic shaders only, no game files.

@justrealyme

Copy link
Copy Markdown

Tested efd81f7 on NVIDIA GeForce RTX 5070 Ti, driver 617.42, Windows 11 build 26300, VS 2022 Build Tools (MSVC 19.44), Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0, RelWithDebInfo: pass.

Built the four pipeline-cache test targets and ran them through ctest: pipeline_cache_file (12.4 s), d3d12_pipeline_cache_persist (8.2 s), pipeline_cache_snapshot and pipeline_cache_store all Passed.

Synthetic tests only, no game files. AI-assisted (Claude Code).

@likecrank-dotcom

Copy link
Copy Markdown

Independent native Windows GPU validation at exact head efd81f79dc1ca2768de3dac9433b867cda44da08.

Windows 11 Pro 10.0.26300, NVIDIA GeForce RTX 5060 Ti, driver 621.14 / 32.0.16.2114; MSVC 19.44.35230.0, Windows SDK 10.0.26100.0, CMake 3.31.6-msvc6, VS 2022 x64 RelWithDebInfo. No local source edits.

Focused CTest: 4/4 PASS (pipeline_cache_file 13.16 s, d3d12_pipeline_cache_persist 8.74 s, pipeline_cache_snapshot 0.60 s, pipeline_cache_store 0.59 s).

One run of the repository's synthetic persistence fixture:

Session Hits Driver creation Whole PSO API calls Loaded driver bytes
1, no application-cache file 0/16 184.27 ms 197.38 ms 0
2, file saved by session 1 16/16 1.06 ms 8.69 ms 95,551
3, junk file 0/16 6.97 ms 10.64 ms 0
after two sessions learned different pipelines and saved sequentially 24/24 1.04 ms 9.00 ms 143,214

The fixture reached pipeline cache persistence: ok. Its rejection of a seed rebuilt during a session, late hard-link alias to the seed, 0 MB save limit, and directory-as-output path were exercised; the expected diagnostics appeared. It also exercised the damaged/truncated cache, no-feedback, Unicode-name, held-file, changing-working-directory, and off-mode checks in the test.

Limitations: the retargeted-symbolic-link block explicitly skipped (no usable symbolic links here); Khronos validation is not installed, so LEONIDA_VK_VALIDATION=0; no game files, full-corpus seed, retail performance or full runtime suite. These are single-run synthetic timings, not a game-speed estimate or a controlled benchmark of the driver's own disk cache. The full-seed/default-cap limitation already discussed here remains.

AI-assisted with Codex.

@DavidM1911

Copy link
Copy Markdown

Tested efd81f7, Windows 11, MSVC 14.50 (VS Build Tools 18), RelWithDebInfo, Vulkan SDK 1.4.363.0, NVIDIA RTX 3060 Ti (610.88):

  • Pipeline-cache tests: pipeline_cache_file PASS (12.4 s), d3d12_pipeline_cache_persist PASS (8.6 s), pipeline_cache_snapshot PASS, pipeline_cache_store PASS.
  • Full ctest: 258/261 pass. The 3 failures — d3d12_vulkan_device, d3d12_indirect_dispatch_count, d3d12_indirect_rt — reproduce identically on clean main dde82f27e on this machine (reported in the coordination issue), so they are pre-existing and not from this change.

AI-assisted. No game files.

@CourtyardGames

Copy link
Copy Markdown

Tested efd81f7 on NVIDIA GeForce RTX 4070 SUPER (621.14, Vulkan 1.4.357), Windows 11, MSVC 14.51: pass

  • Read the full diff first. File writes are the session cache (pipeline_cache.user.bin beside the exe, or LEONIDA_PIPELINE_CACHE), a staging file renamed over it, and the new tests' scratch folders. No network, registry, clipboard or process spawning. The CMake change at the end only sets LEONIDA_PIPELINE_CACHE=off on the other tests.
  • Fresh worktree build. ctest -R "pipeline_cache|d3d12_pipeline": 5/5 passed (pipeline_cache_file, pipeline_cache_snapshot, pipeline_cache_store, d3d12_pipeline_library_host, d3d12_pipeline_cache_persist).
  • Second launch reuses the cache (d3d12_pipeline_cache_persist_test.exe run directly): session 1, no file: 16 pipelines, 0 from cache, 4.26 ms; session 2: 16 of 16 from the cache, 0.44 ms, 96096 bytes loaded; session 3 with junk in the file: ignored (0 bytes loaded, 0 from cache, no crash); a session after two parallel ones: 24 of 24 from the cache, with the other session's pipelines merged. The logs also show the "not saving" cases (seed changed, over the size limit) and an unwritable destination handled without failing.
  • Files stay where they should: after the run the build dir has no pipeline_cache*.bin or .user.bin, and the test's scratch folder in %TEMP% is removed.
  • Full ctest -j6: 235/236; the only failure is d3d12_vulkan_device (known on main, d3d12_vulkan_device: check PC CreateReservedResource2 behind the Xbox proxy, not through slot 78 #7125). No cache file appeared in the build dir after the full run.
  • Not run: a real title launch (no game files); other GPUs or drivers.

AI-assisted.

@caiodstx

Copy link
Copy Markdown

Tested efd81f79d merged alone on main dde82f27e on AMD Radeon RX 6700 XT (RDNA2, gfx1031), AMD 26.9.1 (Vulkan 1.4.315), Windows 11 IoT Enterprise LTSC 2024 (26100), MSVC 14.51 + Ninja, RelWithDebInfo, Vulkan headers via -DLEONIDA_VULKAN_HEADERS_FETCH=ON, no game files: pass, no regressions.

  • The PR's tests pass: d3d12_pipeline_cache_persist, pipeline_cache_file, pipeline_cache_snapshot, pipeline_cache_store
  • Full ctest: no test fails with this PR that doesn't also fail on main alone.

On main alone, on this GPU, the full ctest fails these: d3d12_vulkan_device, d3d12_texture_tiles and d3d12_pipeline_library_host (each has an open fix PR), d3d12_depth_resolve (from #6610, fixed by #7905), and vk_queue_query_sync, which is flaky on main here (line 468: progress_loss_ok, 5 of 10 serial runs fail; also seen in #8426).

Ran the full ctest --test-dir runtime/build -j 6 with the PR merged alone and compared the failures with main alone. AI-assisted (Claude Code).

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.

8 participants