Skip to content

D3D12.X: base pipelines keep their own copy of their shaders, shared by content - #3292

Merged
throny merged 1 commit into
xcl-dev:mainfrom
Foniass:d3d12x-retained-pso-shaders
Nov 29, 2026
Merged

throny merged 1 commit into
xcl-dev:mainfrom
Foniass:d3d12x-retained-pso-shaders

Conversation

@Foniass

@Foniass Foniass commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

RSSetDepthBiasX's patched-pipeline fallback and CreateDerivedPipelineStateX
derive a pipeline from a base one recorded at its creation. The base kept
the title's shader pointers and checked the bytecode by hash when deriving:
once the title freed or reused its shader buffers (common after pipeline
creation), deriving failed ("the shaders of base pipeline ... are gone").

The base now owns its stage bytecode. Tens of thousands of pipelines share
a few thousand shaders, so the copies are interned in a pool keyed by
(fnv64, length) and compared byte for byte (Arc<[u8]>, weak entries swept
as the pool grows): one copy per distinct shader, freed with the last
pipeline that holds it.

Ported from amd-dev 48cf713 (its device_x part) and 60345c9 (the shared
pool), raylitebull. The other parts of 48cf713 are separate PRs.

What is in it

  • crates/xcl/src/hle/d3d12x/device_x.rs: Base::shader_blobs (Arc<[u8]> per stage), the interning pool BASE_SHADERS, desc_with_owned_shaders for RSSetDepthBiasX and CreateDerivedPipelineStateX; test base_pso_retains_shader_bytecode_after_title_buffer_changes (bytecode survives the title freeing its buffers, identical shaders share one copy, the copy goes with the last pipeline)

Tested

  • NVIDIA GeForce RTX 5070 Ti (driver 32.0.16.2114), AMD Ryzen 7 7800X3D, Windows 11 build 26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), debug tests, release smoke
  • cargo test --workspace --no-fail-fast -- --test-threads=4 on 7e24237: 2605 passed, 4 failed, 49 ignored. The 4 failures are gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_0..3 (E_OUTOFMEMORY from CreateComputePipelineState, seeds 70 / 545 / 1019 / 1507), the same as on main on this machine; they pass on another RTX 5070 Ti. SKIPPED GPU tests: 44 notices, almost all no wave64.
  • Retail title: 60 s smoke run below (no device removal or crash, frames and peak commit within the limits).

Regression run

main:

xcl-smoke v1
commit: b10cc4c6766adc947c2054ada4a2af98be15108d
gpu: NVIDIA GeForce RTX 5070 Ti
title: 3cb1d8e0
seconds: 60
exit: 0x0
frames shown: 1565 (PresentX 1565)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16026 MB

this PR:

xcl-smoke v1
commit: 7e2423798475fb4bbd01f5ab382c463eaeb550b0
gpu: NVIDIA GeForce RTX 5070 Ti
title: 3cb1d8e0
seconds: 60
exit: 0x0
frames shown: 1667 (PresentX 1667)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16768 MB

Not tested

  • a title that derives pipelines after freeing its shader buffers: the smoke title's runs never logged the old "shaders ... are gone" warning, so this path is covered by the unit test only
  • memory of the pool on a title with many pipelines (not measured)
  • AMD GPUs

AI use

  • Claude Code with Claude Opus 5.5: ported this part of the amd-dev commit named above to main, wrote the commit message and this description, ran the tests and the smoke run.

🤖 Generated with Claude Code

…by content

RSSetDepthBiasX's patched-pipeline fallback and CreateDerivedPipelineStateX
derive a pipeline from a base one recorded at its creation. The base kept
the title's shader pointers and checked the bytecode by hash when deriving:
once the title freed or reused its shader buffers (common after pipeline
creation), deriving failed ("the shaders of base pipeline ... are gone").

The base now owns its stage bytecode. Tens of thousands of pipelines share
a few thousand shaders, so the copies are interned in a pool keyed by
(fnv64, length) and compared byte for byte (Arc<[u8]>, weak entries swept
as the pool grows): one copy per distinct shader, freed with the last
pipeline that holds it.

Ported from amd-dev 48cf713 (its device_x part) and 60345c9 (the shared
pool), raylitebull. The other parts of 48cf713 are separate PRs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added area:d3d12 D3D12.X runtime, command lists, resources, sparse memory gpu:amd Code is specific to AMD / RDNA / wave64 GPUs (scope tag; see needs:amd-test) labels Nov 29, 2026
@andrewDoing

Copy link
Copy Markdown
Contributor

Overlap with #3091 matters for the stated last-owner guarantee. Weak<[u8]> keeps the Arc allocation, including its inline bytes, allocated after the final strong owner drops. Weak::upgrade returning None proves the shader is no longer accessible through the index; it does not prove its allocation was freed.

I compiled the exact interning helpers from this head (7e24237) and #3091 (1dad60e) in a Windows allocator-instrumented CPU harness. Eight synchronized threads retained 512 references to 16 distinct immutable 256 KiB shader payloads. Both helpers shared identical bytes by pointer across all threads and preserved all contents. After joining, I dropped every strong reference, leaving each production weak index intact. Net live allocations of the payload sizes (input buffers excluded) were:

Helper With strong owners After last owner After forcing the next sweep
This PR, Arc<[u8]> 4,194,560 B 4,194,560 B 0 B
#3091, Arc<Vec> 4,194,304 B 0 B already released

The 256-byte difference while live is the inline Arc headers on this toolchain; the material difference is the 4 MiB of outstanding payload after release. If no later insertion triggers the sweep, that allocation remains. Both implementations copy on a cold miss outside the lock and recheck before publishing, so concurrent cold misses can transiently allocate duplicates. This measures allocator requests and releases, not RSS or peak commit.

I suggest coordinating the derived-base path with #3091, whose bounded weak index also covers pending async recipes. If taking this PR alone, splitting payload storage from the weakly retained allocation and asserting deallocation rather than failed upgrade would match its stated guarantee. OpenAI Codex prepared and ran the CPU controls and drafted this comment.

@Kalmarv

Kalmarv commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

merge-check
base: fa20459
head: 7e24237
verdict: safe to merge

Merged cleanly onto origin/main (fa204595) in a 5-PR batch (#3288, #3290, #3292, #3294, #3300) and verified on NVIDIA GeForce RTX 5070 Ti with DXC 1.9.2609.5:

  • Base pipeline shader retention + Arc sharing verified across hle::d3d12x:: and e2e debug-layer runs.
  • gpu::: 853 passed, 0 failed, 24 ignored (10.45 s, including fuzz_validate_and_gpu_0..3).
  • hle::d3d12x:: (--test-threads=1, skipping 2 known main #3187 uma_driver mixed-window tests): 1144 passed, 0 failed, 3 ignored (30.21 s).
  • Remaining xcl --lib (--test-threads=1): 0 failed.
  • -p xcl --test e2e (after rebuilding xcl-testtitle): 5 passed, 0 failed (including D3D12 debug layer + DRED).
  • GitHub CI (Build and test (Windows, no GPU) and PR description): green.

@github-actions github-actions Bot added needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) needs:amd-test Needs a test report or merge-check on an AMD GPU labels Nov 29, 2026
@Aniseto

Aniseto commented Nov 29, 2026

Copy link
Copy Markdown

Ran the base-pipeline shader retention test at 7e242379 on an NVIDIA Blackwell desktop part with an updated driver:

Environment: Windows 11 Pro build 26200 x64, NVIDIA GeForce RTX 5070 Ti (Blackwell, wave32 only, 16 GB) driver 32.0.16.2140 (621.40), Intel Core i5-12400F, 64 GB RAM, rustc 1.100.0 (0ef1bc386 2026-11-09), MSVC 14.44.35207, DXC 1.9.2609.5, debug build.

XCL_ADAPTER=NVIDIA cargo test -p xcl --lib base_pso_retains_shader_bytecode_after_title_buffer_changes -- --test-threads=1
1 passed; 0 failed; 0 ignored; 0 measured; 2498 filtered out      (exit 0)

The test passes: base pipelines keep their shader bytecode after the title frees its buffers and identical shaders share one copy. The thread already has an RTX 5070 Ti report on driver 32.0.16.2114; this adds the newer driver 32.0.16.2140. It is a synthetic retention test, not the title path that derives pipelines after freeing buffers.

Not tested: a retail title, a paired smoke run, the full workspace suite, AMD and Intel GPUs, native wave64 hosts, Wine, release profile, game data.

AI-assisted (OpenCode), model: DeepSeek V4.1 Flash Free - checked out the PR's head commit in a detached worktree, built it, ran the tests, wrote this report; the worktree was not modified; no game data was used.

@throny

throny commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

merge-check claim
head: 7e24237

@throny

throny commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

merge-check
base: d8bd9a3
head: 7e24237
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2619 passed, 0 failed, 49 ignored (27 SKIPPED lines). Base alone at d8bd9a3: 2618 passed, 0 failed, 49 ignored.
review: no finding. A base pipeline now holds its own copies of its shaders (shared between pipelines through a pool of weak references) instead of hashes, the stored description no longer holds the title's shader pointers, and they are pointed at the copies when a pipeline is derived. Lifetimes are sound from reading: the base owns the bytes, the description with the copies is used only while the base is alive, and the background build path copies the shaders itself. Deriving a pipeline after the title freed its shaders now works where main returned E_FAIL, which is the intent. Cost: one copy of each distinct shader stays in memory as long as a graphics pipeline using it lives. The test checks the sharing, the bytes and the release after the last pipeline; it does not drive the two derive functions themselves.
performance: not measured (memory: see the cost above).
verdict: safe to merge
hardware / tool and model: RTX 4080 16 GB (621.40), i9-12900K, Windows 11 26300, Rust 1.100.0, debug. Claude Code; merge, build and suite run by Claude Fable 5.2, diff review by Claude Opus 5.5 (every hunk read; nothing run on the GPU beyond the suite).

@github-actions github-actions Bot added ready-to-merge Has 2 independent safe merge-checks on current head (#3041) and removed needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) needs:amd-test Needs a test report or merge-check on an AMD GPU labels Nov 29, 2026
@throny
throny merged commit d2ec83d into xcl-dev:main Nov 29, 2026
3 checks passed
@crosettiremi

Copy link
Copy Markdown

A negative data point: the error this targets ("shaders of base pipeline ... are gone") does not occur in any of our 19 retail-title logs on a Radeon RX 7900 XT 20 GB (driver 32.0.32029.2010), bases fa204595 and d8bd9a38, 4 and 16 UMA windows, cold and warm caches, runs that reach the first world frame. So this card and title give no before/after for it. I have not run this PR's head. Since our runs end at a job commit limit, I would be interested in what the kept copies cost in commit on a full retail load if anyone measured it.

AI use: Claude Code (Claude Fable 5.2) searched the logs and wrote this.

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

Labels

area:d3d12 D3D12.X runtime, command lists, resources, sparse memory gpu:amd Code is specific to AMD / RDNA / wave64 GPUs (scope tag; see needs:amd-test) ready-to-merge Has 2 independent safe merge-checks on current head (#3041)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants