Skip to content

d3d12: discard stale pipeline libraries across driver updates and self-heal refused entries (D3D12-PIPELINE-LIBRARY-DRIVER-STAMP) - #2789

Open
zmasi wants to merge 9 commits into
ProjectLeonida:mainfrom
zmasi:fix/d3d12-pipeline-library-driver-stamp
Open

zmasi wants to merge 9 commits into
ProjectLeonida:mainfrom
zmasi:fix/d3d12-pipeline-library-driver-stamp

Conversation

@zmasi

@zmasi zmasi commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #2781 (D3D12-PIPELINE-LIBRARY-DRIVER-STAMP).

Across NVIDIA driver updates (616.64 -> 621.01), ID3D12Device1::CreatePipelineLibrary can accept the outer host-pipelines.bin container at startup while LoadGraphicsPipeline / LoadComputePipeline / LoadPipeline subsequently reject every stored blob with E_INVALIDARG (0x80070057). When pipeline() recompiled each refused PSO (410–5,228 ms per hitch) and called StorePipeline(name, &pso), StorePipeline also failed with E_INVALIDARG because an ID3D12PipelineLibrary cannot overwrite an existing name — leaving added == 0 (or re-serializing the stale blobs back to disk) and keeping the mismatched driver's library alive until device removal (0x887A0005).

This change adds two defense-in-depth layers inside d3d12/crates/leonida-gpu/src/pipeline/library.rs, controlled by LEONIDA_LIBRARY_DRIVER_CHECK (on by default; LEONIDA_LIBRARY_DRIVER_CHECK=0 restores the previous behavior):

  1. Proactive adapter & driver stamp sidecar (host-pipelines.driver):

    • Records a single-line adapter + UMD driver stamp (vendor, device, subsys, rev from DXGI_ADAPTER_DESC1 plus Host::driver() when driver != 0) alongside host-pipelines.bin.
    • At open_in(), if host-pipelines.driver names a different adapter or driver, host-pipelines.next, host-pipelines.bin, and host-pipelines.driver are discarded upfront before CreatePipelineLibrary is called, and an empty library is started cleanly.
    • Pre-existing unstamped caches load normally without an upfront wipe and backfill <stamp> older=1 on save until replaced, preserving Origin::doubtful() for Layer 2.
  2. Reactive self-healing swap (Library::keep / stale / begin_anew):

    • Places active library state behind RwLock<Active> (keeping saving separate so Serialize never blocks loads).
    • When a file-backed library without this driver's own plain stamp refuses Load*Pipeline on generation g, create() succeeds, StorePipeline(name, &pso) fails with E_INVALIDARG on the same generation g without name having been stored in this run (Seen::stored, guarded under same_name(name)), and a fresh empty library accepts pso, begin_anew() atomically swaps in the fresh library, replays non-title static-sampler probe pipelines (Seen::beside for pipeline::places), retains any mapped Blob in Library::retired (releasing its working-set pages via VirtualUnlock so trim() and removal_note() never see an unmapped view), and marks the library dirty (anew) so save replaces host-pipelines.bin and writes this driver's plain stamp.
    • Also passes a real null pointer (std::ptr::null()) to CreatePipelineLibrary when bytes.is_empty() (avoiding Rust's dangling 0x1 empty-slice pointer on VKD3D-Proton), uses unique staging filenames on save, and removes any stale host-pipelines.next when an in-place rename succeeds.

Verification

Tested on Windows 11 (10.0.26200), NVIDIA GeForce RTX 3090 (driver 610.88, UMD 32.0.16.1088), and D3D12 WARP (0x1414):

  • Red -> Green & concurrency unit/GPU tests (16 tests in pipeline::library::tests, 47 passed across pipeline::):
    • with_the_check_off_a_refused_pipeline_is_made_in_every_run (reproduces repeated 0x80070057 on both LoadComputePipeline and StorePipeline, added == 0, unchanged file across restarts when LEONIDA_LIBRARY_DRIVER_CHECK=0).
    • a_pipeline_neither_loaded_nor_replaced_begins_the_library_anew (verifies swap + 0.005–0.015 ms subsequent loads across both mapped and held blobs).
    • a_library_of_another_driver_is_not_handed_to_the_host and a_library_of_another_driver_that_cannot_be_deleted_is_not_called_this_drivers.
    • a_name_stored_in_this_run_does_not_begin_the_library_anew (4-thread barrier benign-race test), threads_that_find_it_out_together_begin_the_library_anew_once (8-thread single-swap race test), and pipelines_kept_beside_the_titles_go_into_the_library_begun_anew (places probe carry-over).
  • cargo clippy -p leonida-gpu --all-targets --locked and --features diagnostics --all-targets --locked: 0 warnings in library.rs; single touched file (host.rs and make.rs untouched).
  • python -I tests/test_d3d12_switches.py (19/19 passed) and python -I scripts/check_no_game_data.py (rc=0).

AI-assisted: authored with Claude Fable 5.2, independently reviewed with Gemini 4 Neon (XHigh), and verified with Gemini 4 Carbon (High).

…ne anew (D3D12-PIPELINE-LIBRARY-DRIVER-STAMP)

After a driver update the host can make the pipeline library from the old
driver's file and then refuse each of its pipelines when it is loaded
(`E_INVALIDARG`, 0x80070057). `StorePipeline` answers the same for a name
that is already in the library, so each such pipeline was compiled again in
every run and every save wrote the old driver's pipelines back.

Note the adapter and driver beside the library (`host-pipelines.driver`:
vendor, device, subsystem and revision IDs and `Host::driver`). A library
whose line names another adapter or driver is not handed to
`CreatePipelineLibrary`: its files are deleted and the run starts an empty
one.

A file saved before the line was kept is loaded as before and noted with
`older=1`. When one of its pipelines neither loads nor can be stored under
its name (and no store of this run took the name), the run swaps to an
empty library, stores the pipeline there, and the next save writes it over
the file. The pipelines `places` keeps in the library go into the new one,
and a mapped old file stays mapped for `trim` and `removal_note`. A library
its line calls this driver's own is not swapped: a refused pipeline there
is not another driver's.

The library and its generation sit behind `RwLock<Active>`; `saving` stays
beside it, so loads still go on while a save serialises.

`LEONIDA_LIBRARY_DRIVER_CHECK=0` turns both off (on by default).

Fixes ProjectLeonida#2781

AI-assisted: yes

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

Verified here on the machine that has the problem: NVIDIA 0x2705 with the driver line captured, cargo test -p leonida-gpu --lib pipeline:: -> 47 passed, 7 ignored (16 new library tests, and the GPU ones really ran — one prints "loaded from the library begun anew in 0.009 ms"), tests/test_d3d12_switches.py 19 OK with the switch read as "on, 0 turns it off".

The mechanism does what it says for a pre-sidecar cache: stamp_of (library.rs:928) writes vendor/device/subsys/rev plus Host::driver() (host.rs:1373), open_in (:990) classifies it, a mismatch discards the pair through discard (:948), and on a refusal that also loses the store, stale() + begin_anew (:749, :767) swap in an empty library and save writes a clean file under this driver's stamp. Nothing refused is ever written back: serialisation only ever uses active.library.

Two notes.

  1. Say in the body that the first run after a poisoned cache still looks poisoned. A missing name is also E_INVALIDARG on this device (the tests assert that after a swap), so the first run still logs made … did not load it: 0x80070057 for nearly every pipeline and still compiles all of them; the second run is the healed one. Otherwise testers (this machine included) will read the first run as the fix not working.
  2. A real bug: replace_library treats a failed removal of host-pipelines.next as success (library.rs:1302-1308). If NEXT is held by something else — a case discard already documents — the rename succeeds, the removal fails silently, and note(dir, &active, true) (:1246) writes this driver's plain stamp (:877-884). The next start then trusts the line, Blob::of (:263) renames the surviving other-driver NEXT over the library, the file loads as Origin::Noted, and stale() never heals it. Reproduced with NEXT held share_mode(0): after the save the line names this driver while NEXT still exists, and the next run stays (0, Noted) after a refusal. Only write the plain stamp when NEXT is actually gone (or adopt NEXT before comparing the sidecar).

@Kal0s-design

Copy link
Copy Markdown
Contributor

Independent Windows check of e38238383 on current main 640b0d0df (no conflict, merge 5ec44016e). Native Windows 11, MSVC x64, Rust 1.98.1, with a hardware D3D12 device, so the library tests that make real pipelines through Card (host().device → ID3D12Device1) ran rather than skipped.

  • cargo test -p leonida-gpu --lib: 609 passed, 0 failed, 54 ignored. main lists 653 tests, the merge 663, so the 10 new pipeline::library tests are the only addition.
  • The 16 pipeline::library tests, the new ones included, run 200 times on their own in the release build with 16 test threads: 0 failures. That covers threads_that_find_it_out_together_begin_the_library_anew_once, a_library_of_another_driver_is_not_handed_to_the_host and concurrent_write_atomically_never_fails_or_truncates.
  • Not covered: an actual driver update between two runs (the stamp change is simulated by the tests).

AI-assisted: yes

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Dec 1, 2026
…brary-driver-stamp

# Conflicts:
#	d3d12/crates/leonida-gpu/src/pipeline/library.rs
@github-actions github-actions Bot removed the needs rebase Conflicts with the base branch: rebase it on main label Dec 1, 2026
@Neutron-0

Copy link
Copy Markdown

A heads-up for this pull request: the page of D3D12 switches (d3d12/docs/switches.md) is written by tools/d3d12_switches.py from the sources of d3d12/, and 1 name this pull request reads is not on the page yet: LEONIDA_LIBRARY_DRIVER_CHECK.

Nothing checks that today, so the page has drifted before (it was a name behind when this was measured). A test that checks it is waiting to be opened (DOCS-SWITCHES-PAGE-CHECK), and it would make this pull request's CI red until the page is written again. One command does that:

python -I tools/d3d12_switches.py --out d3d12/docs/switches.md

It writes the page with the names in it and nothing else; the diff is the new rows.

@zmasi

zmasi commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the heads-up @Neutron-0 — regenerated d3d12/docs/switches.md (LEONIDA_LIBRARY_DRIVER_CHECK, --check rc 0) in 573f7f216.

AI-assisted: Gemini 4 Carbon.

@merzleckeier

Copy link
Copy Markdown
Contributor

Independent check of 573f7f216 on c60ce461a. All 16 pipeline-library tests completed on both GPUs, using the Windows MSVC test binary under Wine and vkd3d-proton 3.1.0 with VKD3D_CONFIG=pipeline_library_app_cache. GPUs were AMD Raphael (RADV 26.3.1) and RTX 4070 SUPER (NVIDIA 620.73.4).

The test cases cover a differing driver note, refusal of a cached compute pipeline with the check off, saving and then reloading a rebuilt entry with the check on, and concurrent detection rebuilding the library only once. The revisions to the library state were synthetic. A native Windows driver upgrade was outside the run.

AI-assisted with OpenAI Codex.

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Dec 1, 2026
…brary-driver-stamp

# Conflicts:
#	d3d12/docs/switches.md
@github-actions github-actions Bot added needs rebase Conflicts with the base branch: rebase it on main and removed needs rebase Conflicts with the base branch: rebase it on main labels Dec 1, 2026
…brary-driver-stamp

# Conflicts:
#	d3d12/docs/switches.md
@github-actions github-actions Bot added needs rebase Conflicts with the base branch: rebase it on main and removed needs rebase Conflicts with the base branch: rebase it on main labels Dec 1, 2026
…brary-driver-stamp

# Conflicts:
#	d3d12/docs/switches.md
@github-actions github-actions Bot removed the needs rebase Conflicts with the base branch: rebase it on main label Dec 1, 2026
@lixonik

lixonik commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Independent check of c44b20fec merged cleanly on main (1e3e316c5), on native Windows 10 Pro 22H2 (build 19045) + NVIDIA GeForce RTX 5070 12 GB (driver 621.40, Ryzen 9 5950X):

  • cargo test --locked -p leonida-gpu --lib pipeline::library -- --nocapture: 16 passed, 0 failed (live GPU self-heal tests ran on RTX 5070 / 621.40, printing loaded from the library begun anew in 0.009 ms and 0.008 ms).
  • cargo test --locked -p leonida-gpu --lib pipeline:: -- --skip depth_bounds_pipeline_loads_and_filters_draws: 47 passed, 0 failed, 8 ignored (depth_bounds_pipeline_loads_and_filters_draws skipped for the separate Win10 inbox d3d12.dll SM 6.6 flag issue tracked in d3d12-gpu: drop unused SM 6.6 root signature flags in depth_bounds test #2855).
  • python -I tools/d3d12_switches.py --check d3d12/docs/switches.md: exit 0; test_d3d12_switches.py: 19 passed, 0 failed.
  • Zero new compiler warnings in pipeline/library.rs.

AI-assisted: Antigravity

@fiestorra

Copy link
Copy Markdown
Contributor

Independent verification on an RTX 4090, driver 621.24 (32.0.16.2124), Windows 11 - by the method in AGENTS.md (PR ref, merge-tree, apply it on main, run what it touches).

Applied on main 1fff69129:

git merge-tree --write-tree origin/main refs/remotes/pr/2789     no conflicts
git merge --squash refs/remotes/pr/2789                          clean, 2 files

One thing worth knowing: a commit-by-commit cherry-pick of the range (3dd1bf42..c44b20fec, 8 commits) does conflict in pipeline/library.rs - main moved under it. The squash-merge is clean, so merging is fine; a rebase would be needed only if the branch is to be kept linear.

What it does here:

cargo test -p leonida-gpu --lib -- --test-threads=1 pipeline::library
    ok. 16 passed; 0 failed

cargo test -p leonida-gpu --lib -- --test-threads=1
    653 passed; 1 failed; 59 ignored
    the one that fails is descriptor::host::tests::views_are_made_beside_the_heap_and_copied_in,
    which fails the same way on main without this change (reported in #2570; it is the byte
    comparison, not the copy)

cargo clippy -p leonida-gpu --all-targets
    1 warning: unused mut at host.rs:7628 - also there on main, not yours (#2877 carries the fix)

python -I tests/test_d3d12_switches.py
    passed (exit 0), so LEONIDA_LIBRARY_DRIVER_CHECK is documented as the code reads it

What I could not test, and would not claim: the case the issue is about - a library written under one driver and opened under another. Downgrading this machine's driver is not something I will do to a working setup, so the 616.64 -> 621.01 transition is verified only through your own tests (a_library_of_another_driver_is_not_handed_to_the_host and the two that begin the library anew), which is a fake stamp, not a driver that changed. And the title was not run with it.

What that leaves as risk: the stamp is read from DXGI_ADAPTER_DESC1, and the case that matters is a driver update that keeps the same device id and changes only the UMD version - if the driver applies its version elsewhere than the fields you read, the stamp would not move and the library would still be handed over. Worth one line in the PR saying which fields change on an update, since the person who can check it is someone who upgraded and kept the old file.

AI-assisted: yes - DeepSeek v4.1 (flash).

Written with an AI assistant.

…brary-driver-stamp

# Conflicts:
#	d3d12/docs/switches.md
@andrew-ai

andrew-ai commented Dec 2, 2026 •

Copy link
Copy Markdown

At 4cec236 I queried the production stamp(host) in three fresh Windows processes using one diagnostics build. The RTX 5070 returned vendor 0x10de, device 0x2f04, driver 9007199255791260. WARP returned vendor 0x1414, device 0x008c, driver 2814751477606228. Returning to the 5070 reproduced the complete first stamp byte for byte.

I then exercised both on-disk mismatch tests on the 5070, substituting that measured WARP stamp in the test sidecar. With the check enabled, the ordinary case discarded the stale library, saved a rebuilt pipeline under the current stamp and loaded it on reopen. With an exclusive handle on the stale library file, the save could not replace it. The file was not relabeled as current, and reopening started with an empty library. Both tests reached the readback assertions; I added temporary test-only output to distinguish that from an early return.

These are real D3D12 pipeline files in temporary directories, with deliberately mismatched metadata. No driver installation was changed. Codex-assisted.

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

Labels

None yet

Projects

None yet

8 participants