Skip to content

d3d12: persist the pipeline cache across launches (D3D-704) - #7384

Closed
ddaikodaiko wants to merge 1 commit into
ItsNotRexed:mainfrom
ddaikodaiko:d3d704-pipeline-cache-session
Closed

ddaikodaiko wants to merge 1 commit into
ItsNotRexed:mainfrom
ddaikodaiko:d3d704-pipeline-cache-session

Conversation

@ddaikodaiko

Copy link
Copy Markdown

Claimed in #7231 (comment).

What

PSOs a session compiled were compiled again by the next one. The layer already read the pipelines build stage's
pipeline_cache.bin beside gta6rt.exe (#367), but never wrote anything back.

D3D-704 closes that: the runtime keeps its own cache in pipeline_cache.session.bin beside the executable.

  • Teardown: after vkDeviceWaitIdle and the resource drain, vkGetPipelineCacheData fills a buffer and
    write_pipeline_cache_file publishes it through a staged file + rename (the helper vk_pipeline_check already
    used, moved from runtime/test to runtime/src).
  • Next launch: the offline seed and the session file cannot be handed over as one VkPipelineCacheCreateInfo
    (Vulkan reads one blob), so the cache is created from the seed as before, the session file becomes a second cache
    and vkMergePipelineCaches folds it in.

The two files are deliberately separate, so a run can never replace the multi-GB corpus cache with the handful of
pipelines it happened to compile.

Limits, deliberately

  • The write is capped at 256 MiB, LEONIDA_PIPELINE_CACHE_BYTES=<bytes> (0 turns the session file off). A cache
    seeded from the corpus file comes back as the whole union (3.8 GB on the measured corpus), and rewriting that at
    every exit costs more than it saves. Past the cap the previous session file is left alone rather than replaced
    with one the next launch would also refuse to write.
  • A session file whose VkPipelineCacheHeaderVersionOne names another vendor, device or pipelineCacheUUID is
    ignored with one line, as the seed already did. A truncated file (< 32 bytes) is "no cache".
  • A write that fails (read-only install directory, full disk) logs the error and leaves the previous file.
  • bad_alloc on the teardown buffer is logged, not thrown out of a destructor.
  • No vkGetPipelineCacheData/vkMergePipelineCaches (not expected: both are Vulkan 1.0 core), no session file.

Tests

New portable test, no Vulkan and no GPU: runtime/test/pipeline_cache_store_test.cpp covers the limit parser
(absent, empty, 0, leading zeros, SIZE_MAX, -1, "1 ", 0x10, overflow) and the 32-byte header comparison
(match, extra payload, short, null, each of headerSize/headerVersion/vendor/device/UUID changed). Both it and the
existing pipeline_cache_file test pass here: 23/23 ctest on Linux (portable subset).

The shim itself has not been compiled here — this machine has no MSVC, no Windows SDK and no Vulkan GPU. The
Windows job of runtime.yml is the compile check, and the end-to-end check is: run d3d12_vulkan_device_test
twice on a Vulkan GPU; the first says wrote N bytes to pipeline_cache.session.bin, the second
merged N bytes from pipeline_cache.session.bin.

Checklist

  • No game data, keys or decryption. Nothing here reads the package or the executable.
  • Does not add a translator rule.
  • docs/d3d12_vulkan.md (§4.5, "Pipeline caches") describes the two files, the merge, the cap and the
    environment variable.

AI-assisted (Claude), and the run planned on Windows 11 + MSVC + a Vulkan GPU (RADV/AMD on Linux is available for
the portable tests, not for the layer).

PSOs a session built were compiled again by the next one. The layer already read the `pipelines` stage's
pipeline_cache.bin beside gta6rt.exe (ItsNotRexed#367), but never wrote anything back.

Now the runtime keeps its own cache in pipeline_cache.session.bin beside it: vkGetPipelineCacheData on device
teardown, then vkMergePipelineCaches into the cache at the next launch. Vulkan takes one blob per create, so the
offline seed and the session file are two caches merged together rather than one file. They are deliberately
separate files, so a run can never replace the multi-GB corpus cache with the handful of pipelines it compiled.

The write is capped at 256 MiB (LEONIDA_PIPELINE_CACHE_BYTES=<bytes>, 0 turns it off): a cache seeded from the
corpus comes back as the whole union, which is not worth rewriting at every exit; past the cap the previous
session file is left alone. A foreign or truncated session file is ignored with one line, and so is a failed
write (a read-only install directory).

The header check moves to runtime/src/pipeline_cache_store.h, shared with the offline seed and tested without
Vulkan (runtime/test/pipeline_cache_store_test.cpp: the limit parser and the 32-byte
VkPipelineCacheHeaderVersionOne comparison). write_pipeline_cache_file moves from runtime/test to runtime/src so
the runtime and vk_pipeline_check use the same staged write.

Not run: this machine has neither MSVC nor the Windows SDK, so the shim itself is compiled by the Windows job.
The portable test passes here (23/23 ctest on Linux). The end-to-end check needs a Vulkan GPU: run
d3d12_vulkan_device_test twice and look for "wrote N bytes to pipeline_cache.session.bin", then "merged N bytes"
on the second run.
@skorex

skorex commented Nov 23, 2026

Copy link
Copy Markdown

This looks like a duplicate of #7360 (D3D-704, pipeline cache persisted across launches), which was claimed first under that ID in #7231, is mergeable and has been checked independently on native Windows + MSVC. Suggest closing in favour of #7360.

@philip-roy-jones

Copy link
Copy Markdown

I checked this PR at d3a1c290 to fill in the Windows behavior omitted from the comparison of #7360 and #7396. Two settings matter when reusing an existing cache.

The Windows helper test holds the destination open without delete sharing, forces replacement to fail, then checks that the previous bytes survive. Overlapping successful writers are a separate case. If A and B load the same session before either saves, this PR merges the file only at first use. B can therefore publish a valid cache that drops entries A saved in the meantime. Held-file test and assertions · First-use merge

For comparison, #7396 checks for an intervening save at teardown. Its two-session test explicitly releases A before B, so the assertion covers B seeing A's completed save. I verified these behaviors against the source and committed assertions. Runtime cap enforcement and simultaneous saves were outside this review. #7396 teardown check · Order in the two-session test

@nullmarsh

Copy link
Copy Markdown

Tested d3a1c290 natively on Windows 11 26200, MSVC 14.44 RelWithDebInfo, RX 9060 XT (AMD 26.11.2): pass. pipeline_cache_file, pipeline_cache_store, pipeline_cache_snapshot, vk_pipeline_check_cli and vk_pipeline_check_stack: 5/5.

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

@DeSunShine

DeSunShine commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Tested d3a1c290b merged into main dde82f27e on NVIDIA GeForce RTX 4090 (driver 621.24), Windows 11 Pro 26200, MSVC (VS Build Tools 18) + Ninja, Vulkan SDK 1.4.363: pass (no regression against main).

  • ctest -R "pipeline_cache|d3d12_compute|d3d12_draw|vulkan_device|vk_pipeline": pipeline_cache_file, pipeline_cache_store, pipeline_cache_snapshot, d3d12_compute, d3d12_draw, vulkan_device_filter, vk_pipeline_check_cli, vk_pipeline_check_stack all passed. d3d12_vulkan_device fails (line 6574: tiles == 1 with this PR merged); it also fails on plain main dde82f27e here (known, fixed by d3d12_vulkan_device: check PC CreateReservedResource2 behind the Xbox proxy, not through slot 78 #7125), but I did not compare the failing line number on main.
  • I did not run a second launch to see the cache file reused by a real title; only the tests above.

AI-assisted (Claude Code).

@KoertLichtendonk

Copy link
Copy Markdown
Contributor

Coordinator note: D3D-704 already has an open PR, #7360 by @aslan2142 (opened 16:37 UTC), so this looks like a duplicate. Please compare the two and either close this one or say here what it adds. The maintainer decides which one is merged.

@caiodstx

Copy link
Copy Markdown

Tested d3a1c290b 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: pipeline_cache_file, 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).

@ddaikodaiko

Copy link
Copy Markdown
Author

Compared the three D3D-704 PRs as asked (#7360, #7396, this one) on main 867d922f0. This one does not add
anything worth a third implementation, so I am closing it.

What the three share: none of them writes the pipelines stage's corpus seed, and all of them load what the last
launch compiled and merge it on the next one.

What #7396 already has that this PR was written for:

  • the session file is separate from the seed and merged at start-up (pipeline_cache.user.bin, or a file named by
    LEONIDA_PIPELINE_CACHE), so a run never rewrites the multi-GB corpus cache;
  • a size cap, LEONIDA_PIPELINE_CACHE_MAX_MB (1 GiB by default), and LEONIDA_PIPELINE_CACHE=off (this PR's only
    differences are a 256 MiB default, LEONIDA_PIPELINE_CACHE_BYTES, and the same refusal past the cap leaving the
    previous file alone);
  • a framed, checked file (this PR only re-checks the VkPipelineCacheHeaderVersionOne fields) and a merge with
    another session's save;
  • a GPU test for the second-launch hit rate, which this PR does not have.

#7360 is the one with independent Windows + MSVC runs (RTX 3080, RTX 5070 Ti, an AMD iGPU) and, per @skorex and
@Envyre above, the one to keep while porting #7396's separate-file and cap pieces into it. If the smaller 256 MiB
default is wanted there, it is one default value rather than a PR.

The branch stays at d3a1c290b (ddaikodaiko/Leonida) if anything here is useful: the stdlib-only
pipeline_cache_store_test (limit parsing and header matching, no Vulkan headers needed) is the only piece with no
counterpart in the other two.

@ddaikodaiko

Copy link
Copy Markdown
Author

Closed as a duplicate of #7396/#7360 after the comparison above; the branch is kept.

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.

7 participants