Skip to content

d3d12: the Vulkan pipeline cache is also saved while the session runs (LEONIDA_PIPELINE_CACHE_SAVE_SECONDS) - #9467

Closed
thedarkcr0w wants to merge 5 commits into
ProjectLeonida:mainfrom
thedarkcr0w:pipeline-cache-periodic-save
Closed

thedarkcr0w wants to merge 5 commits into
ProjectLeonida:mainfrom
thedarkcr0w:pipeline-cache-periodic-save

Conversation

@thedarkcr0w

Copy link
Copy Markdown

Claimed in #9401: #9401 (comment)

Depends on: #7396. Review only the last commit, 291b10c9a. Below it is #7396, merged cleanly.

What and why

#7396 saves the runtime's VkPipelineCache at device teardown. A retail title never gets there: it is killed, or leaves through TerminateProcess, so nothing is saved exactly where it matters. On a retail 1.0.2.1 boot the title creates its 2,693 pipeline-library compute pipelines one after another while loading. Cold, that is about one every 0.85 s on an RTX 4090 (700 done at 570 s), and every launch started from nothing.

Design

Tested

Windows 11, MSVC 2022 Build Tools RelWithDebInfo (Vulkan headers fetched), NVIDIA GeForce RTX 4090 (driver 32.0.16.1062).

  • New ctest d3d12_pipeline_cache_persist_periodic, in its own process with a 1 s interval:
    • while the device is alive, the file exists and is a whole frame;
    • a later pipeline that hits the cache does not rewrite it.
  • Negative control: with the two save_pipeline_cache_if_due() calls removed, the periodic test fails (no session file while the device is alive); restored, it passes.
  • d3d12_pipeline_cache_persist and pipeline_cache_store_test still pass.
  • scripts/check_no_game_data.py on the changed files: clean. tests/test_docs_repeated_lines.py fails on main too (docs/boot.md), not from this change.
  • Retail boot (own copy): 313,203,142 bytes saved at 120 s, in 6,689 ms. The next launch loaded the file and created 713 library compute pipelines in 150 s, where the cold launch needed 570 s for 700.

Caveat

A save is synchronous on the creating thread: 2.7 to 13.9 s for 310 to 380 MB here. A title that measures its own frame time during loading sees one long creation every interval. Moving the write to a background thread is a possible follow-up; it needs care with #7396's merge-across-sessions step.

AI-assisted (Claude Code).

tedin7 and others added 5 commits November 23, 2026 17:08
…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.
…DA_PIPELINE_CACHE_SAVE_SECONDS), for a title that never tears its device down
@VibeClauding

Copy link
Copy Markdown

Review of 291b10c9a (code read, not run), with one concern about which thread does the save.

save_pipeline_cache_if_due() runs save_pipeline_cache() synchronously on whichever thread just created the pipeline: the full vkGetPipelineCacheData copy plus the staged file write. The try_lock keeps other threads from queuing behind it, but the saving thread itself is held for the whole save. That thread belongs to the title:

  • Compute path: the title's job thread that called slot 62/67.
  • Graphics path: build() is also what variant() calls, and that is "built on first use ... (replay may call this from any queue)". So a due save can also land in a queue's replay of a list, while that PSO's variants_mutex_ is held.

This has been measured on the retail boot. #9438's description, finding 7: "Saving from the creating thread trips the title's hang watchdog, because a 1.5 GB save takes ~2 s. Saving periodically from a separate thread works." The cache reaches that size in exactly the cold-boot case this PR targets: about 2,700 library compute pipelines, plus tens of thousands of graphics PSOs.

Suggestion: keep the policy (interval, the missed-since-last-save check, try_lock), but hand the save to a background thread. A creation that finds a save due only signals a condition variable, and one saver thread owned by DeviceState (joined before the cache is destroyed at teardown) runs save_pipeline_cache(). That's valid because the cache is created with flags 0, not EXTERNALLY_SYNCHRONIZED, so the implementation synchronizes vkGetPipelineCacheData against concurrent vkCreate*Pipelines on the same cache.

A test that would catch the regression: time one CreatePipelineState that triggers a due save against a large cache, and assert it returns without waiting for the file write. Or count that the save ran on a thread other than the caller.

AI-assisted (Claude Code). No game files.

@JonaxScript

Copy link
Copy Markdown

Tested 291b10c9a (as pushed, with #7396 below it) with AMD's Windows driver on two GPUs: pass. The run in the description was on NVIDIA, and the cache contents come from the driver (vkGetPipelineCacheData), so this adds a second vendor.

  • RX 7900 XTX (RDNA3), AMD 26.11.2 (32.0.32029.2010)
  • Ryzen 7 9800X3D iGPU (RDNA2, gfx1036), driver 32.0.21045.29003
  • Windows 11 Pro 26300, MSVC 19.44 (scripts/build_runtime.ps1, RelWithDebInfo), Vulkan SDK 1.4.363.0

ctest -R pipeline_cache: 5/5 on each GPU, twice. The five tests are pipeline_cache_file, d3d12_pipeline_cache_persist, d3d12_pipeline_cache_persist_periodic, pipeline_cache_snapshot and pipeline_cache_store.

The new periodic test:

GPU session file written while the device is alive
RX 7900 XTX periodic: the cache was saved while the session ran (37844 bytes)
iGPU periodic: the cache was saved while the session ran (34324 bytes)

The #7396 persistence test, RX 7900 XTX, so the driver's data really is reused:

  • 37,812 bytes are saved and loaded back.
  • The second device then creates 16 pipelines ... 16 of them from the pipeline cache, where the cold run had 0.
  • A damaged file is ignored: session_cache.bin is damaged or not a session cache: ignored, and that run gets 0 from the cache.
  • The non-ASCII path (漢字Ж.bin) round-trips.
  • A second session's file is merged.

I didn't measure the threading concern above. These caches are tens of KB and each save takes about 1 ms here, so the test sizes can't show the 2.7–13.9 s saves from the description.

No game files. AI-assisted (Claude Code).

@tidyhf

tidyhf commented Nov 23, 2026

Copy link
Copy Markdown

Tested 291b10c on NVIDIA GeForce RTX 4080, driver 617.42, Windows 11 Pro (26300), MSVC 14.41, Vulkan SDK 1.4.363.0: pass.

  • ctest -R '^(d3d12_pipeline_cache_persist|d3d12_pipeline_cache_persist_periodic|pipeline_cache_file|pipeline_cache_snapshot|pipeline_cache_store)$': 5/5 passed.
  • A data point for the thread question in the review above: in d3d12_pipeline_cache_persist_test --periodic the synchronous save took 1-2 ms ("saved 94881 bytes to session_cache.bin in 2 ms", then "in 1 ms"; 17 pipelines created in 6.94 ms). That's a ~95 KB test cache on an NVMe drive; a game-sized cache of tens of MB would cost proportionally more on the pipeline-creating thread, so the concern still stands for real sessions.

AI-assisted (Claude Code) test run.

@sodove

sodove commented Nov 23, 2026

Copy link
Copy Markdown

Independent native Windows/Vulkan check of 291b10c9 in merge 4f11cfd9 with 5d74c272. Windows 11 x64 build 26100, RTX 5070 Ti, NVIDIA 610.74, Vulkan loader 1.4.341, MSVC 19.44 and SDK 10.0.26100.

  • Stock d3d12_pipeline_cache_persist, d3d12_pipeline_cache_persist_periodic, and pipeline_cache_store passed 3/3.
  • A local variant let a compute pipeline trigger a one-second checkpoint, verified the complete frame with the device live, then terminated its own process through TerminateProcess. Each fresh process loaded the checkpoint and reported 16/16 application pipeline cache hits. This passed across 3/3 independent process pairs. The checkpoint bytes stayed identical after each warm run.
  • Interval-zero control used the same pipeline sequence and abrupt exit. It left no checkpoint file.

Synthetic compute pipelines and Vulkan validation off. AI-assisted.

@kristofbarany

Copy link
Copy Markdown

Tested 291b10c merged onto main 5d74c27 (clean merge) on NVIDIA GeForce RTX 4070, driver 621.24, native Windows 11 Pro 26300, MSVC 14.44.35207, Ninja RelWithDebInfo: pass.

  • pipeline_cache_file_test: 3 of 3 runs exit 0 (about 12 s each).
  • pipeline_cache_snapshot_test: 3 of 3 runs exit 0.
  • No compiler warnings in these builds.

No game data used. AI-assisted (Claude Code).

@comtek21516

Copy link
Copy Markdown

Tested 291b10c9a on main at 867d922f0, in one tree with #8213, #9480, #9486, #9492, #9328 (six clean merges): pass.

For this change. pipeline_cache_store passes natively. d3d12_pipeline_cache_persist and d3d12_pipeline_cache_persist_periodic are skipped on the Windows machine (no Vulkan driver), so the GPU run is the one that counts: under Wine on the RTX 4000 both pass, and pipeline_cache_store passes there too.

Setup. Built with MSVC 14.52 (Visual Studio 18), Windows SDK 10.0.26100, Vulkan SDK 1.4.363.1, Ninja RelWithDebInfo on Windows Server 2025 (no Vulkan driver there), then the same binaries run through scripts/run_wine_tests.py under wine-11.0 (WineHQ stable, xvfb-run) on a Quadro RTX 4000 (Turing), NVIDIA 615.71.09, Ubuntu 22.04.5, where the layer tests report device: Quadro RTX 4000.

passed failed skipped
native Windows, this tree 149 4 129
Wine on the GPU, main 867d922f0 210 12 54
Wine on the GPU, this tree 214 12 57

Against main under Wine the only differences are the seven tests this tree adds (four pass, three are skipped because the Wine runner does not start cmake-driven tests); every test that exists on main has the status it has there. The four native failures are gameinput_host, d3d12_sparse_queue_family, vk_bindless_ps_tex and vk_bindless_ps_imgatomic, which fail the same way on main on that machine (#7452). The 12 under Wine are the same 12 as on main (host comparisons Wine cannot answer, and d3d12_xdxr_rt, d3d12_indirect_rt, d3d12_indirect_dispatch_count).

This was a combined run: the six PRs were built into one tree, so it is weaker evidence than a run of this PR alone, and there is no per-PR negative control. A regression would have shown as a difference from main, and there is none. No game files used. AI-assisted (Claude Code).

@RAINighty

Copy link
Copy Markdown

Tested 291b10c9a merged with main 5d74c2723 (clean merge) on Windows 11 (10.0.26300), Intel Core i7-14650HX, hybrid-GPU laptop (RTX 5060 + Intel UHD, no adapter forced), MSVC 19.44.35228, RelWithDebInfo (Ninja): pass.

AI-assisted (Claude Code).

FJKiXfaR pushed a commit to FJKiXfaR/Leonida that referenced this pull request Nov 23, 2026
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.

10 participants