Skip to content

d3d12: a pipeline cache for the offline cache's misses, written while the game runs (RT-HEAVY-PIPELINE-CACHE) - #1914

Merged
aslan2142 merged 5 commits into
ProjectLeonida:mainfrom
kawaii-shakal:heavy-pipeline-cache
Nov 26, 2026
Merged

aslan2142 merged 5 commits into
ProjectLeonida:mainfrom
kawaii-shakal:heavy-pipeline-cache

Conversation

@kawaii-shakal

@kawaii-shakal kawaii-shakal commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Update (a42bffd, merged with main c50ed94): main now persists compute pipelines in its per-pipeline journal (src/pipeline_cache_journal.h), so the heavy cache here covers graphics pipelines only; compute builds use main's build_compute_pipeline unchanged. The text below describes the original version, where it covered both.

Claimed in the coordination issue as RT-HEAVY-PIPELINE-CACHE: #1608 (comment)

Problem

The builds the offline pipeline_cache.bin misses are compiled again on every boot. On an RTX 5070 Ti these are the pipeline library's compute programs, 3 to 7 s each and about 40 per boot before the world starts, and they are what keeps the render thread waiting at the title's hang watchdog. pipeline_cache.session.bin can't keep them for two reasons:

  • It is written at device teardown only, and a boot that ends in a crash never gets there.
  • A cache seeded from the corpus returns the whole corpus (9.2 GB here), so even a clean teardown is over the 256 MiB write limit and writes nothing. LEONIDA_STARTUP_CACHE_CHECKPOINTS has the same problem.

Change

  • Corpus first, without compiling. Every graphics and compute build first asks the corpus cache with VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT (DeviceState::create_pipeline).
  • A miss goes to a cache of its own. It is built against a second cache seeded from pipeline_cache.heavy.bin, next to pipeline_cache.bin. That cache holds only the corpus's misses, so it stays small.
  • Written while the game runs. A build of at least LEONIDA_HEAVY_PIPELINE_CACHE_MS (500 by default) marks the cache dirty. A thread of its own writes it 3 s after the newest slow build and no later than 20 s after the first unwritten one, through the existing staging file and rename (CacheStagingFile), so the file on disk is always complete. A crash loses at most the last 20 s of slow builds. At teardown the thread stops and writes what is left.
  • Off switch. LEONIDA_HEAVY_PIPELINE_CACHE_MS=0 turns it off, and with it pipelineCreationCacheControl unless LEONIDA_PIPELINE_COMPILE=cache-only asks for it. A cache-only create (cache-only) is left exactly as it was.
  • Pure logic, tested on its own. The threshold parsing and the write schedule are in runtime/src/heavy_pipeline_cache.h, with heavy_pipeline_cache_test.
  • Test devices. vk_test_device.h takes the heavy path when LEONIDA_HEAVY_PIPELINE_CACHE_MS is set, and d3d12_mesh_heavy_cache runs the mesh test's draws through it.
  • Docs. docs/d3d12_vulkan.md describes it next to the session cache.

Measured (Windows 11 Pro 26300, RTX 5070 Ti, NVIDIA 617.42, VS 2026 RelWithDebInfo)

  • Does the driver honour cache-only for the heavy programs? Checked with vk_pipeline_check and a local probe that adds the flag (not part of this PR), against an empty cache. Four library compute programs the corpus lacks return VK_PIPELINE_COMPILE_REQUIRED in under 1 ms, so the heavy path is taken for them. The mesh test's small shaders come back VK_SUCCESS from a cache-only create even into an empty cache, because the driver's own shader cache already holds them. That is why the CTest case checks correctness, not the file.
  • CTest: heavy_pipeline_cache, pipeline_cache_store, pipeline_cache_file, d3d12_msaa_draw, d3d12_mesh and d3d12_mesh_heavy_cache all pass, 6 of 6.

Not done here

  • No real boot yet. I haven't run this PR in a real boot, so the next one will measure how much of the inline compile time goes away on the second boot.
  • Graphics variants share the same path. They are only slow when the corpus misses them.

🤖 Generated with Claude Code

… the game runs (RT-HEAVY-PIPELINE-CACHE)

pipeline_cache.session.bin can't keep what a crashed run compiled: it is written at device teardown only, and
a cache seeded from the multi-GiB corpus returns the whole corpus, over its write limit. So the slow builds
the corpus misses (the pipeline library's compute programs, 3 to 7 s each on an RTX 5070 Ti) are compiled
again on every boot.

Each graphics and compute build now first asks the corpus cache with
VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT. A miss is built against a second cache, seeded from
pipeline_cache.heavy.bin next to pipeline_cache.bin, so that cache holds only the misses.

A build of at least LEONIDA_HEAVY_PIPELINE_CACHE_MS (500 by default) schedules a write. A thread of its own
writes the cache 3 s after the newest slow build and no later than 20 s after the first unwritten one,
through the session cache's staging file and rename. At teardown it writes what is left. 0 turns it off,
and with it pipelineCreationCacheControl unless LEONIDA_PIPELINE_COMPILE=cache-only asks for it.

Measured with vk_pipeline_check on the RTX 5070 Ti (617.42): a cache-only create of four library compute
programs the corpus lacks returns VK_PIPELINE_COMPILE_REQUIRED in under 1 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…, counted, not timed; teardown keeps fast builds too

GetTickCount64 moves in 10-16 ms steps, so a slow build noted during a write in the write's own tick was taken
as written. The schedule now counts notes. And without an offline seed every build goes to the heavy cache:
fast ones never schedule a write, so teardown writes the cache whenever any build went into it, which is what
pipeline_cache.session.bin used to keep.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Tyrannys

Copy link
Copy Markdown
Contributor

Data point for this PR and for thegalaxydev's RT-PIPELINE-CACHE-GROWTH-STALL. With a complete offline cache, the session cache can't be saved at all under the current limit. Counts only.

The session snapshot is the whole driver cache, seed included, so once the seed alone is over the limit, every session save is refused. A separate cache for the builds the seed misses, as this PR does, avoids that. So does a limit that is measured against what the run added rather than against the total.

AI-assisted (Claude Code).

@lixonik lixonik 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.

Independent CPU check at e4c413a3a: heavy_pipeline_cache_test passes. A failure-injection harness using the current writer and teardown methods exposed one remaining issue with saving during a run. No GPU or game files.

AI-assisted.

Comment thread runtime/shims/d3d12_vulkan_device.cpp Outdated
save_heavy_pipeline_cache(context->vk, info.device, heavy_cache_handle, heavy_compiles.load(),
heavy_hits.load());
lock.lock();
heavy_schedule.written(generation, now);

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.

[P2] Retry unsuccessful periodic saves while the device is live

The writer ignores the save result and marks the captured generation written. If staging or publication fails briefly and no further slow compile occurs, the writer goes idle with the cache still unsaved. With the exact methods at this head, a mocked first save failure followed by 30 s on the clock produced one attempt while live and a second only at normal teardown. A crash in that interval still loses the cache. Keep the generation dirty on a failed save and retry after a bounded delay so persistent failures do not spin.

@Snooozs

Snooozs commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

One difference to the session saver, from the code at this head (save_heavy_pipeline_cache): it accepts VK_INCOMPLETE, and publish then replaces pipeline_cache.heavy.bin; the session saver (save_session_pipeline_cache) wants VK_SUCCESS and keeps the previous file. A compile that adds to the cache between the size query and the data query gives VK_INCOMPLETE, so the file can be the partial snapshot (valid as a seed, but without the entries that did not fit). Keeping the previous file and letting the next write do it costs nothing: the cache stays dirty.

Also a measurement about size, RTX 5060 Ti 16 GB, NVIDIA 621.01: the file was 1.09 GB, and with about 640 more programs 1.60 GB. The writes are whole, so each of them was that size, one every 10 to 20 s while compiles ran (wrote ... to pipeline_cache.heavy.bin in 0.4-0.7 s). With thegalaxydev's finding about large caches (#1608, 7171729435) several smaller caches, chosen by the module's hash, would keep each small and write only what changed. I have not built that.

AI-assisted (Claude Code, Claude Opus 5.5).

codex and others added 2 commits November 25, 2026 21:56
…LETE snapshot

Review of RT-HEAVY-PIPELINE-CACHE:
- The writer thread marked a generation written even when the save failed, so the
  cache stayed unsaved until teardown. A failed save now keeps the cache dirty and
  is retried after a bounded backoff (5 s, doubling, capped at 2 min), so a write
  that keeps failing does not spin. WriteSchedule::failed() holds that decision and
  heavy_pipeline_cache_test checks it.
- save_heavy_pipeline_cache accepted VK_INCOMPLETE and published the truncated blob
  over a complete file. It now requires VK_SUCCESS, like the session cache, and
  keeps the previous file; the next try queries a fresh size.

Changes made by Claude Code (AI-assisted).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JPqXc6svM4pbBagTsVEYA3
@kawaii-shakal

Copy link
Copy Markdown
Contributor Author

These changes were made by Claude Code (AI-assisted) in aab7653, after the review:

  • @lixonik [P2], retrying failed periodic saves: the writer no longer marks a generation written when the save fails.
    WriteSchedule::failed() keeps the cache dirty and retries 5 s later. The delay doubles with each failure in a row
    and is capped at 2 min, so a save that keeps failing does not spin. Once a save succeeds, the backoff is cleared.
    heavy_pipeline_cache_test has a new test_failed_write that covers the backoff, the cap, a compile during the
    backoff and the reset after a success.
  • @Snooozs, VK_INCOMPLETE: save_heavy_pipeline_cache now requires VK_SUCCESS and written <= size, the same as
    the session saver. A truncated snapshot never replaces the previous file. With the retry above, the next try
    queries a fresh size.
  • Splitting the file into several smaller caches keyed by module hash, to limit the 1.1→1.6 GB rewrites: not done in
    this PR. The file holds only the offline cache's misses. If the size becomes a problem, I'd rather do that as a
    follow-up.

Tested: heavy_pipeline_cache_test, built with g++ 13 on Linux. The shim (d3d12_vulkan_device.cpp) is Windows-only
and was not built here, so it still needs a Windows CI or local run.


Generated by Claude Code

@OreoFR

OreoFR commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Real boots with this PR (earlier head, before aab7653) on an RTX 4070 Laptop 8 GB, NVIDIA 621.40, main + #2077 + the mesh-indirect branch: pipeline_cache.heavy.bin loads at start (~100 MB) and is rewritten during the run in about 0.1 s each time (e.g. "3 slow builds and 364 other builds past the offline cache so far"). Across runs it does its job: the mesh pipeline builds that missed the offline cache went from 135 in the first run to 13 in the next ones. No crash or corrupt cache file in ~15 runs, including runs that ended in a device loss.

main's per-pipeline compute journal (pipeline_cache_journal.h) now persists every compute build and survives a
crash, so the compute build takes main's build_compute_pipeline and the heavy cache keeps graphics pipelines only.
CMake keeps both test sets; docs and the create_pipeline comment say so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kawaii-shakal

Copy link
Copy Markdown
Contributor Author

Merged main (c50ed94) in a42bffd; the PR had a conflict with it.

main now has its own crash-safe store for compute pipelines: the per-pipeline journal (pipeline_cache.d/cs-<hash>.bin, src/pipeline_cache_journal.h), written right after each compile. So in the merge the compute build is main's build_compute_pipeline unchanged, and the heavy cache keeps graphics pipelines only (create_pipeline is used by the graphics variant build). Two effects:

  • No double bookkeeping for compute: one store per pipeline kind.
  • The heavy file gets much smaller, because the largest programs (the library compute shaders) are no longer in it. That covers most of @Snooozs's point about 1.1-1.6 GB whole-file rewrites; splitting the graphics file by module hash stays a possible follow-up.

The docs section and the create_pipeline comment say this now.

Tested (Windows 11, MSVC RelWithDebInfo, RTX 5070 Ti, NVIDIA 617.42), including the aab76535 review fixes on Windows for the first time:

AI-assisted (Claude Code, Claude Opus 5.5).

aslan2142 added a commit that referenced this pull request Nov 26, 2026
Tested on main a73a57e: Wine suite (clang-cl + xwin, RDNA2 iGPU, RADV) 412 pass / 22 fail / 39 skip on main and on main + #1886, 0 changes; Linux portable ctest 108/108; the new over_save_limit cases fail on main and pass here. Review points addressed in 66e96cc. Windows CI cancelled; linux-gpu-tools CI failed at Build (log not available yet; the same job also fails on #1914, and it builds locally). AI-assisted (Claude Code, Claude Opus 5.5 review, Claude Fable 5.2).
@aslan2142
aslan2142 merged commit 32a04b1 into ProjectLeonida:main Nov 26, 2026
5 of 12 checks passed
@aslan2142

Copy link
Copy Markdown
Collaborator

Thank you, @kawaii-shakal. This went into main through #2350 (RT-LINUX-AMD-WORLD), merged at your head a42bffdc3 with your commits and authorship kept. The only conflict was the session-cache paragraph in docs/d3d12_vulkan.md against #1886: main's cap and your heavy-cache text were both kept. GitHub shows this PR as merged for that reason, so nothing needs closing.

AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2)

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