Skip to content

runtime: a pipeline cache from the runtime's own device (RT-RUNTIME-PIPELINE-CACHE) - #1308

Open
Daolyap wants to merge 1 commit into
ProjectLeonida:mainfrom
Daolyap:rt-runtime-pipeline-cache
Open

Daolyap wants to merge 1 commit into
ProjectLeonida:mainfrom
Daolyap:rt-runtime-pipeline-cache

Conversation

@Daolyap

@Daolyap Daolyap commented Nov 24, 2026

Copy link
Copy Markdown

Item: RT-RUNTIME-PIPELINE-CACHE (claimed in #14). Complements #218, which persists the cache at device teardown.

Why

The pipelines stage's pipeline_cache.bin passes the runtime's header check, but it misses for the runtime's
pipelines
. A driver keys its cache on the device's enabled extensions and features, and vk_pipeline_check
creates a different device. The runtime enables 17 extensions the checker doesn't (mesh shader,
maintenance5/8, shader FMA and clock, fragment barycentrics and interlock among them), and the two build their
feature lists differently: the checker sets every core feature through Features2 and the 1.1/1.2/1.3 structs,
the runtime a subset through per-extension structs. I diffed both VkDeviceCreateInfos with a local dump.

Measured on an RTX 3070, driver 32.0.16.1692, with the retail 1.0.2.1 BVH builder's 52 compute programs:

Largest program (LoadInstances) All 52
vk_pipeline_check, cold 28.5 s 63 s total compile
vk_pipeline_check, re-run on its own cache 0.01 s (hit)
runtime, the checker's cache loaded ~28 s (miss)
pso_warmup (runtime device), cold 27.8 s 63 s
pso_warmup, re-run on its own cache under 1 s (hit) 5.7 s

This matters for booting. The title creates the BVH builder's pipelines on its graphics-init thread, and its own
watchdog ends the process after about 26 s, so the cold 28 s compile kills every launch the same way, with nothing
in the log. With a pso_warmup cache beside gta6rt.exe, the #432 stack on native Windows gets past graphics init
and into its frame loop (details in #14).

Change

  • DeviceState::save_pipeline_cache(path): vkGetPipelineCacheData written through a temporary file and renamed.
    rt::d3d12::save_pipeline_cache(ID3D12Device*, path) exposes it for a device from create_vulkan_device*.
    vkGetPipelineCacheData is added to the backend's function table.
  • pso_warmup <dir of .dxbc compute programs> <out file> (runtime/test/pso_warmup.cpp, built with the other
    Windows tools): builds every program through CreateComputePipelineState on create_vulkan_device_standalone,
    then writes that device's cache. It needs LEONIDA_SHADERS and the user's carved programs; nothing of the game is
    in the source.
  • LEONIDA_PIPELINE_CACHE_OUT=<file>: the runtime saves after every compute compile of 250 ms or more, so a run
    that ends without a teardown (a watchdog, a crash) keeps what it compiled.
  • A compute compile of 1 s or more is logged with the shader's name.
  • docs/d3d12_vulkan.md section 4: replaces the sentence implying the checker's cache serves the runtime with the
    measurement and both new paths.

Not in this PR: wiring pso_warmup into make_game.py's pipelines stage, and the graphics PSO path. Both are
follow-ups once this shape is agreed.

Tested

  • Windows 11, VS 2022 RelWithDebInfo, NVIDIA RTX 3070, driver 32.0.16.1692.
  • The measurements above, on the user's own 1.0.2.1 copy.
  • gta6rt and pso_warmup build without new warnings. tests/test_docs_env_vars.py and
    tests/test_text_write_line_endings.py pass. check_no_game_data.py on every changed file passes.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

…IPELINE-CACHE)

A driver keys its pipeline cache on the device's enabled extensions and features. vk_pipeline_check
enables a different set from the runtime (17 device extensions the runtime has and it doesn't), so the
pipelines stage's pipeline_cache.bin, though accepted by its header, misses for the runtime's pipelines.
Measured on an RTX 3070 (driver 32.0.16.1692) with the retail 1.0.2.1 BVH builder: the checker's own re-run
of its largest program hits in 0.01 s, while the runtime with that seed loaded still compiles it cold in
about 28 s. The title compiles it on its graphics-init thread, and its own watchdog ends the process after
about 26 s.

- DeviceState::save_pipeline_cache writes the device's vkGetPipelineCacheData through a temporary file;
  rt::d3d12::save_pipeline_cache exposes it for a device from create_vulkan_device*.
- pso_warmup (runtime/test/pso_warmup.cpp) builds a folder of compute programs through
  create_vulkan_device_standalone and writes that device's cache. For the BVH builder's 52 programs: 63 s
  cold, 5.7 s with the result seeded.
- LEONIDA_PIPELINE_CACHE_OUT=<file> saves after every compute compile of 250 ms or more, so a run that ends
  without a teardown keeps what it compiled.
- A compute compile of 1 s or more is logged with the shader's name.
- docs/d3d12_vulkan.md: the checker's device differs, with the measurement.

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

Snooozs commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Read the whole diff and tested 8fb2bdea4 merged on main 4a50cafc7 (clean): Windows 10 Pro 19045, VS 2022 RelWithDebInfo, RTX 5060 Ti (NVIDIA 621.01). No game files here, so nothing below says anything about your BVH timings.

The cause in the docs doesn't reproduce here. pipeline_cache_seed writes a cache with vk_pipeline_check and creates the same pipeline on the runtime's device:

pipeline_cache_seed_test result
main, checker copy under the test's exe name application cache hit
main, locally patched so that the checker copy has another exe name application cache hit
with this PR application cache hit

So on this driver the checker's device gives the runtime the same key, at least for that empty shader with no bindings, and the exe name isn't part of it. That is after the changes docs/pipeline_check.md ("The cache and the runtime's device") describes: VK_EXT_mesh_shader and VK_KHR_maintenance8 are already enabled in the checker because NVIDIA keys on them. Your miss must come from something this test doesn't cover. Could you:

  • run pipeline_cache_seed on the 3070? If it passes there too, "its device does not match the runtime's" isn't the reason.
  • check whether the 52 programs have bindings? The sentence you extend already limits the match to compute shaders with no bindings and the 144-byte range; another layout would miss for that reason.

The same section measured that on an RX 9070 XT the executable's file name is part of the key. There a cache from pso_warmup.exe would miss in gta6rt.exe. Not measured with this tool, but the docs could say so (running a copy named gta6rt.exe avoids it).

In the code:

  • save_pipeline_cache: the stream's close isn't checked, only write. On a full disk the failure can show up at the flush, and the short file is then renamed over a good one. out.close(); if (!out) return false; before the rename. The .partial file also stays behind on each failure.
  • The second vkGetPipelineCacheData returns VK_INCOMPLETE when another thread's compile grows the cache between the two calls. That save is then dropped without a message, and the title compiles on several threads. Retrying would do.
  • %ls with a non-ASCII path: with an output folder named w東 the file is written correctly, but the log line is cut off at that character, newline included.
  • With an empty folder the tool writes a 36-byte cache and exits 0. Pointed at the wrong folder and an existing cache as output, that replaces a 63 s result unless the cache was also the seed. Failing at "0 built" would be safer.

What works here: pso_warmup builds, a missing output folder gives exit 2 and no file, d3d12_compute_test passes with 0 VUID lines. The save path has no automated test; pipeline_cache_seed_test already makes a device and a pipeline, so it could take one.

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

@OreoFR

OreoFR commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Independent retail check of 8fb2bdea4 merged on main 6fe83805a (clean merge), answering @Snooozs's two questions with the game's own programs. Windows 11 (26200), VS 2022 Build Tools (MSVC 14.44) RelWithDebInfo, NVIDIA GeForce RTX 4070 Laptop GPU, driver 32.0.15.9159, Core Ultra 7 155H, own 1.0.2.1 copy. Counts and timings only.

Every run below got its own empty NVIDIA shader disk cache (__GL_SHADER_DISK_CACHE_PATH pointed at a fresh folder per run; the driver did write into it), so no run was warmed by the one before. The CPU was partly busy with an unrelated shader translation (6 of 22 threads), so absolute times may be a little high; the cold/seeded ratios are what matter.

Q2, the 52 programs' layout: all 52 XboxBvhBuilder.dll programs in <work>/shaders/spirv/bvh/index.json have no bindings and push_constant_bytes 144. So a layout difference does not explain a miss for them. 42 are wave32 with a required subgroup size, 10 are wave64 translated with --wave64-emulate (this GPU is 32-wide).

The checker's cold compile (vk_pipeline_check <spirv>/bvh <cache> --stage cs --jobs 1 --slow 1 --cache-feedback): 52 ok, 55.0 s total, LoadInstances 26.4 s, the next ones 6.9 s and below. That is the checker's pipeline_cache.bin used as the seed in run B.

The runtime's device (pso_warmup <work>/shaders/bvh <out>, LEONIDA_SHADERS=<work>/shaders/spirv, the seed copied beside the exe as pipeline_cache.bin):

run seed 52 programs slowest log
A none 61.1 s LoadInstances 27.1 s –
B the checker's cache 5.2 s 2.6 s loaded 5830614 bytes from pipeline_cache.bin
C pso_warmup's own cache from A 5.0 s 2.5 s loaded 5830588 bytes
D as C, the exe copied under another name 5.1 s 2.6 s

So on this driver the checker's cache does hit for the runtime's pipelines: LoadInstances goes from 27 s to under 1 s with either seed. And the executable's name is not part of the key here (D), unlike the RX 9070 XT measurement in docs/pipeline_check.md.

Q1: pipeline_cache_seed_test <vk_pipeline_check.exe> passes: ok: vk_pipeline_check cache hits on the runtime's device (NVIDIA GeForce RTX 4070 Laptop GPU).

The 2.5 s left in B, C and D is not a miss. In every seeded run pso_warmup still reported BitonicInnerSort64CS at 2.5-2.6 s, but:

  • the PR's own compute pipeline ... took ... ms line (which times vkCreateComputePipelines only) was not printed for it,
  • the cache written after B is byte-for-byte the size of the one loaded (5,830,614 bytes), so the driver compiled nothing new,
  • the checker re-seeded with its own cache reports 52 hits and 0.49 s for all 52, this program included.

It is the first file pso_warmup opens, and DeviceState::library() loads the translated-shader library on first use: here a 54.5 MB index.json with 29,187 entries. Renaming that file so it sorts last moved the 2.5-2.9 s to the new first file (BitonicInnerSortCS), and the renamed one dropped under 1 s. So pso_warmup's per-file times include that one-time load in the first line, and it would be clearer to load the library before the timed loop (a dummy lookup, or a small library() call through the device).

It also matters for the boot, a little: in the title, the first pipeline creation is on the graphics-init thread the watchdog watches, so those ~2.5 s are paid inside the same ~26 s budget as the BVH compiles. Loading the library when the device is created would move them out of it. Not measured in the title itself.

What this means for the PR: the "the checker's device does not match" cause does not reproduce on this driver with the current checker, which already enables VK_EXT_mesh_shader and VK_KHR_maintenance8 as Snooozs pointed out. Perhaps the 3070 measurement used an older checker? The useful parts stand on their own either way: LEONIDA_PIPELINE_CACHE_OUT (a watchdog-killed run keeps what it compiled), the slow-compile log line, and pso_warmup as a way to seed without running the whole pipelines stage, which was on course for over 3 h on this machine for the full corpus (I stopped it; estimated from its progress). The first sentence of the docs change would need rewording.

Measured with Claude Code.

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 25, 2026
@akimfomin

Copy link
Copy Markdown
Contributor

OreoFR's first-use index measurement led PortBridge to try retaining one top-level JSON entry at a time. This changes the main shader-catalog load and keeps the existing optional-index paths. Work is recorded in #14.

On a 29,187-entry source-generated catalog (57.4 MB), median ShaderLibrary::load was 808.83 → 559.31 ms on pinned main, with peak process commit 458.76 → 154.72 MB. With #1108 and #1218 applied first, the separate comparison was 419.52 → 317.39 ms and 499.34 → 103.84 MB. Both exact pairs were measured in eight balanced fresh-process rounds. The independent check verified every expected row; the native shader-library tests passed. A malformed tail retains the previous catalog, and duplicate lookup order is unchanged.

The standalone patch, generator, test and raw timings use only public source and synthetic metadata. This measures the loader you identified inside the first pipeline, without making a title-startup timing claim. The two touched source files also matched the latest main at verification.

AI assistance: OpenAI Codex (GPT-6 family) assisted with the implementation, verification and measurements.

@akimfomin

Copy link
Copy Markdown
Contributor

One substantial follow-up to the main index result above. PortBridge kept the one-entry parser and replaced its complete input string with a 16 KiB cursor. It reuses the same JSON grammar, keeps optional indexing on the existing path, and incorporates the late read-error check motivated by sodove's #1218.

On a generated 57,357,214-byte index of 29,187 rows, the exact stock / bounded reads / bounded plus an optional short-decimal conversion measured 802.8 / 470.4 / 442.5 ms. Peak process commit was 459.2 / 34.6 / 34.7 MB. We used all six process orders and checked every row. The independent reproduction was in the same range. Rollback after malformed input and a Windows-locked byte passed, and all 111,211 numeric controls agreed. The three source headers used by the probe are unchanged on main f7838564.

Separate patches, source-only reproduction and raw data. These are loader measurements for synthetic inputs on Windows. The bounded input patch can be reviewed on its own; the decimal conversion is a separate follow-on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants