Repository navigation
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
Conversation
…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.
|
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:
|
|
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). |
|
Side-by-side check of #7396 ( Tests: #7396 Damaged file after a valid header (the case #7360 passes to the driver and #7396's Differences that matter more:
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. |
|
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: Synthetic tests only, no game files. AI-assisted (Claude Code). |
|
Independent native Windows GPU validation at exact head 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 ( One run of the repository's synthetic persistence fixture:
The fixture reached Limitations: the retargeted-symbolic-link block explicitly skipped ( AI-assisted with Codex. |
|
Tested efd81f7, Windows 11, MSVC 14.50 (VS Build Tools 18), RelWithDebInfo, Vulkan SDK 1.4.363.0, NVIDIA RTX 3060 Ti (610.88):
AI-assisted. No game files. |
|
Tested efd81f7 on NVIDIA GeForce RTX 4070 SUPER (621.14, Vulkan 1.4.357), Windows 11, MSVC 14.51: pass
AI-assisted. |
|
Tested
On main alone, on this GPU, the full ctest fails these: Ran the full |
@ItsNotRexed: an exe PR, as you asked.
Claimed in #7049 (comment)
AI-assisted (Claude).
What
The runtime's
VkPipelineCachenow persists across launches:pipeline_cache.user.binbesidegta6rt.exe(or toLEONIDA_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.VkPipelineCacheHeaderVersionOnematches the device (vendor, device ID,pipelineCacheUUID) and it is at least as new as the build'spipeline_cache.bin; otherwise from the seed, as before. The file is read withReadFile, not a mapping, so a read error on a share that went away is a failed load, not an exception.pipeline_cache.binis 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. ALEONIDA_PIPELINE_CACHEthat 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.vkMergePipelineCaches) before this one saves, so the last writer does not drop them.LEONIDA_PIPELINE_CACHE_MAX_MBchanges 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=offloads and saves no session file; CTest sets it for every test exceptd3d12_pipeline_cache_persist, so tests leave nopipeline_cache.user.bin.VK_EXT_pipeline_creation_feedback(core on the Vulkan 1.3 path;VulkanDeviceInfo::pipeline_creation_feedback). At teardown the log saysN pipelines created in X ms, H of them from the pipeline cache;rt::d3d12::pipeline_cache_statsreturns the same numbers for tests.pipeline_cache_file.handpipeline_cache_snapshot.hmove fromruntime/test/toruntime/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 withLEONIDA_PIPELINE_CACHE=off, which leaves no file),pipeline_cache_file_testandpipeline_cache_snapshot_testpass andvk_pipeline_checkbuilds.Pipeline creation time, summed from the driver's feedback for 16 PSOs, median of 5 runs (min to max):
CreateComputePipelineStatecall (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 msEach 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'sd3d12_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_bytesnot 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漢字Ж.binthe first commit terminates the process (exit 3:path::string()throws under this Wine's code page); now it passes.LEONIDA_PIPELINE_CACHE=offis checked in the working directory, the scratch folder and the executable's folder, and fails whenis_offis made to return false.What is not covered, honestly:
EXCEPTION_IN_PAGE_ERRORfrom 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).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.Notes for review:
not saving the pipeline cache: N bytes is over the 1024 MB limit); setLEONIDA_PIPELINE_CACHE_MAX_MBto raise it.pipeline_cache_snapshot.h; whichever lands second follows the rename.