Skip to content

D3D12.X: pipelines that share a shader share one copy of its bytes - #827

Open
Cattn wants to merge 1 commit into
xcl-dev:mainfrom
Cattn:intern-shader-blobs
Open

Cattn wants to merge 1 commit into
xcl-dev:mainfrom
Cattn:intern-shader-blobs

Conversation

@Cattn

@Cattn Cattn commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

An async pipeline keeps its description, with copies of its shader bytecode, until it is built. A title that creates tens of thousands of pipelines from a few thousand shaders holds one copy per pipeline. A retail title had about 84,000 pending pipelines; with the copies shared, its commit at 80 s was about 1 GB lower (27,016 MB against 27,972 MB, in a longer run on a branch with other changes).

What is in it

  • crates/xcl/src/hle/d3d12x/real.rs: intern_shader_blob keeps a weak map from (hash, length) to Arc<[u8]>, compares the bytes in full, and own_shader / own_stream store the shared Arc instead of a Vec<u8> copy. shaders_key takes any byte slices. A unit test checks that equal shaders share one copy, different ones do not, and that a copy is made again once every pipeline released it.

Tested

  • NVIDIA GeForce RTX 4070 (driver 32.0.16.1742), AMD Ryzen 7 7800X3D, Windows 11 10.0.26200, rustc 1.93.1, debug tests and release smoke builds
  • cargo test --workspace --no-fail-fast -- --test-threads=4 on 44d6921: 702 passed, 0 failed, 41 ignored
  • SKIPPED: gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware (no wave64)

Regression run

The first smoke run of this commit came right after two others with a warm shader cache, got further into loading in its 60 s and peaked at 22,423 MB. I ran main and this PR again back to back; these are the second pair.

main:

xcl-smoke v1
commit: 64b3f35599bb7102f55d640e1a791e7d6dd3b175
gpu: NVIDIA GeForce RTX 4070
title: 7f4b2c91
seconds: 60
exit: 0x0
frames shown: 1610 (PresentX 1610)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16997 MB

this PR:

xcl-smoke v1
commit: 44d69219872ae75420b953a8c9853aa6b57a29a3
gpu: NVIDIA GeForce RTX 4070
title: 7f4b2c91
seconds: 60
exit: 0x0
frames shown: 1628 (PresentX 1628)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 17107 MB

Not tested

  • The saving on its own: the 1 GB figure comes from a longer run on a branch with other changes, and the 60 s smoke run ends before most pipelines are created.
  • AMD and Intel GPUs, wave64 hardware.

AI use

  • Claude Code with Claude Opus 5.5: wrote the change, the test and this description. It also ran the tests and smoke runs on the hardware above. A person has not yet reviewed the diff line by line.

🤖 Generated with Claude Code

An async pipeline keeps its description with copies of its shader
bytecode until it is built. A title that creates tens of thousands of
pipelines from a few thousand shaders held one copy per pipeline (about
84,000 pending pipelines on a retail title); with the copies shared, its
commit at 80 s was about 1 GB lower (27,016 MB against 27,972 MB).

own_shader and own_stream now intern the bytes (a weak map keyed by
hash and length, compared in full), so every pipeline with the same
shader holds the same Arc<[u8]>. shaders_key takes any byte slices.
@Cattn
Cattn marked this pull request as ready for review November 24, 2026 06:13
@crosettiremi

Copy link
Copy Markdown

Checked 44d6921 on Windows with an RX 7900 XT. The title run adds this PR to the #782 residency comparison and uses the same saved Series X profile, 28 GiB job cap, 6 GiB texture budget, and A press at 75 seconds. The disk cache directories started from the same snapshot. The AMD driver cache persisted.

At 150.26 s, queued or active descriptors referenced 588 MiB of shader bytes counting duplicates. The interner held 185 MiB of live payload and 2 MiB of payload behind expired weak entries, about 401 MiB less than retaining each reference separately. The process committed 28,231 MB at that sample, versus 28,117 MB without #827 at 150.07 s. Texture and driver allocation patterns varied. The run had an explicit native compute creation E_OUTOFMEMORY (0x8007000e) at 151.450 s, peaked at 28,440 MB sampled process commit, and exited with 0xc0000409. No device removal was logged.

The weak-cache entry has a lifetime detail. Arc<[u8]> allocates the bytes inline, so Weak<[u8]> retains the backing allocation after strong_count reaches zero. An 8 MiB standalone allocator probe on the installed MSVC Rust toolchain measured 8,388,624 bytes retained after the final owner dropped, versus 40 bytes for Arc<Vec> with Weak<Vec>. A local change to Arc<Vec> preserves pointer sharing between descriptors and releases the payload with the last strong owner. Both shader-key and sharing tests pass with that representation. The retail sample retained only 2 MiB in expired entries, so this specific issue does not account for the memory peak.

@thehollowedone

thehollowedone commented Nov 24, 2026 •

Copy link
Copy Markdown

Shader allocation lifetime on 44d6921. A synthetic fixture called intern_shader_blob with 32 distinct 2 MiB buffers and confirmed identical inputs shared a live allocation. On Windows 11 build 26300, Ryzen 7 9800X3D, Rust 1.99.1 MSVC, private commit rose 64.26 MiB and stayed there after the last strong owners were dropped.

A separate Arc<Box<[u8]>> control rose 64.25 MiB and returned to its starting commit after the strong owners dropped, with all weak entries still present. This matches Rust’s Weak allocation lifetime. With Weak<[u8]>, the shader bytes are in the allocation the map still retains.

cargo test --offline --locked -p xcl --lib scratch_shader_blob_commit_lifetime -- --nocapture --test-threads=1 passed 1/1 in two fresh debug processes with identical measurements and zero skips. Synthetic CPU memory only. AI-assisted. Codex (GPT-6, exact variant unavailable) wrote and ran the fixture and drafted the report.

Current-main integration recheck. 44d6921 still conflicts with 06c9f55. I resolved the two conflicts locally, preserving the public shader key and main’s empty shader stream handling, and tried the separate byte allocation in the actual pipeline ownership code.

A second synthetic probe counted live allocator bytes after both owners of each of 24 distinct 2 MiB buffers were dropped. The PR interner retained 50,333,104 bytes. The local version retained 1,584 bytes of cache metadata. Both shared the live allocation for equal inputs.

The local integration passed cargo build --offline --locked --workspace -j 6 and cargo test --offline --locked --workspace --no-fail-fast -j 6 -- --test-threads=4 --nocapture. Results were 1,266 passed, 0 failed, 46 ignored, including 26 runtime skips. Shader-key, shader-sharing and stream tests passed. The full suite used an RTX 3080 Ti / 621.35 and DXC 1.8.2502.11. Synthetic evidence only. Codex (GPT-6, exact variant unavailable) resolved the local integration, wrote the allocator probe, ran the tests and drafted this update.

@MesshM

MesshM commented Nov 24, 2026

Copy link
Copy Markdown

Test run of 44d69219872ae75420b953a8c9853aa6b57a29a3 on AMD RDNA4. I merged it onto current main beaa245 locally (clean merge) and tested that tree.

Tested

  • AMD Radeon RX 9060 XT 16 GB (RDNA4, VRS supported), driver 32.0.32029.2010; Ryzen 7 5700X, 64 GB RAM, Windows 11 Pro build 26200, Rust 1.100.0 stable-x86_64-pc-windows-msvc, debug
  • cargo build --workspace + cargo test --workspace -- --test-threads=4 on the merge: 714 passed, 0 failed, 41 ignored, exit code 0; xcl lib tests 673 passed; 0 SKIPPED.

Review: Sharing the shader bytes of pending pipelines as Arc<[u8]> keeps one copy per shader instead of one per pipeline; the build path reads the same bytes. I found nothing to change.

Not tested: no retail title or regression run (I have no unpacked game), release build.

Run and review by Claude Code (Claude Opus 5.5) on this machine.

@dcastell1

Copy link
Copy Markdown

Verified on Windows 11 10.0.26300, NVIDIA GeForce RTX 5090 (driver 621.24), Rust 1.100.0 MSVC 14.44.35207, debug build, applied on main@beaa245:

patch -p1 < this PR            # clean, no fuzz
cargo test --workspace -- --test-threads=4
test result: 738 passed; 0 failed; 41 ignored   (20 blocks, exit 0)

That is one more passing test than main on the same box (737), so shader blobs shared between pipelines runs green here and nothing else in the workspace changes verdict. No game data, hardware adapter, no Wine.

AI-assisted (opencode): applied the patch to a built tree, ran the suite, restored the two files.

@ryosen0

ryosen0 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Paired 150 s runs of 44d6921 vs current main (beaa245) on RTX 3060 Ti 8 GB (Ryzen 7 9800X3D, driver 32.0.16.1742, Windows 11, release), same title, same scripted presses (25/35/45/55/65/80/95/110/125/140 s), same session — reaching the loading screens where the pending-pipeline population is large:

main beaa245 this PR 44d6921
frames at 150 s 4149 4151
peak commit 16381 MB 16260 MB (−121 MB)
commit at t≈80 s 16373 MB 16255 MB (−118 MB)
removals / crashes 0 / 0 0 / 0

So the interning saving is real and measurable on a second host in a plain 150 s window: ~120 MB here, consistent in both the 80 s sample and the peak. Smaller than the 1 GB figure from your longer stacked run, as expected — this window creates far fewer pending pipelines than a full world load, and the saving should scale with the pending population. Frame pacing identical (60 presents / 2 s throughout both).

The thehollowedone lifetime point above stands independent of this measurement.

AI use: GPT-6.2 Astra (API model selector) in DSH built both commits, ran the paired runs, and drafted this comment; the human contributor reviewed the numbers before posting.

@SlopTart

Copy link
Copy Markdown

Linux / AMD RDNA3 datapoint.

Merged state: head 44d6921 onto main a1672ef, clean merge.

Full suite, cargo test --workspace --release --no-fail-fast --target x86_64-pc-windows-gnu -- --test-threads=4: 706 passed, 10 failed, 41 ignored. Failures are the known Wine-lane set from my #42 rows plus #718 trio members; nothing here fails because of your change. Shared shader byte tests green; no new failures.

Not tested: retail titles (none here), Windows, RDNA4.
Counts only, no game data.

Machine: RX 7900 XT (RADV NAVI31), Mesa 26.2.5, kernel 7.2 (Arch), Proton Experimental (wine-11.0), patched vkd3d-proton v3.1, Rust cross x86_64-pc-windows-gnu, XCL_NETWORK=0, release profile, --test-threads=4. Linux rows: #42 / #718.

AI use: Hermes agent (local Qwen-class) ran the suite and drafted this; I checked it before posting.

@raylitebull

Copy link
Copy Markdown
Member

Code/CI review only (not independently runtime-tested by me): clean merge with both checks green (Build and test + PR description). Small, clean +40/-11 change deduplicating shader bytes across pending PSOs. Recommend merging, @ivall.

kacper-serewis pushed a commit to kacper-serewis/xcl that referenced this pull request Nov 24, 2026
An async pipeline keeps its description with copies of its shader
bytecode until it is built. A title that creates tens of thousands of
pipelines from a few thousand shaders held one copy per pipeline.

own_shader and own_stream now intern the bytes in a weak map keyed by
(fnv64, length) with full-byte comparison, so pending pipelines with the
same shader share one allocation while different bytes stay separate.

Adapted from PR xcl-dev#827 by Cattn (44d6921):
- Store Arc<Box<[u8]>> instead of Arc<[u8]> so Weak entries do not pin
  the inline shader byte payload in heap memory after the last strong
  OwnedDesc drops (measured 32 x 1 MiB own_compute drop: +33,755,136 B
  retained with Arc<[u8]> vs +8,192 B with Arc<Box<[u8]>>).
- Prune expired weak entries in amortized O(1) once the map reaches its
  growth threshold so control blocks and buckets stay bounded.
- Add unit tests exercising own_compute, own_graphics, and own_stream
  with equal and different shader bytes.

Co-Authored-By: Gemini 4 Neon <noreply@google.com>
(cherry picked from commit dfbb0e5f9db742c967095e08c538dcadd16b7462)
@throny throny added the goal:world-load Bears on the retail title getting past world loading label Nov 25, 2026
IvyNotFound added a commit to IvyNotFound/xcl that referenced this pull request Nov 25, 2026
# Conflicts:
#	crates/xcl/src/hle/d3d12x/real.rs
IvyNotFound added a commit to IvyNotFound/xcl that referenced this pull request Nov 25, 2026
…blob by its bytes

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

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11): merge conflict with current main — needs a rebase before it can be tested.

@S1m0ne1997

S1m0ne1997 commented Nov 25, 2026 •

Copy link
Copy Markdown

Test report for 44d69219872ae75420b953a8c9853aa6b57a29a3 (this PR's head), for #1607.

Tested

  • NVIDIA GeForce RTX 4090 24 GB (driver 621.40), AMD Ryzen 9 9950X3D, 47 GB RAM, Windows 11 Home 10.0.26300, rustc 1.100.0 (0ef1bc386 2026-11-09) x86_64-pc-windows-msvc, debug.
  • cargo build --workspace, then cargo test --workspace -- --test-threads=4 at the head: 702 passed, 0 failed, 41 ignored.
  • SKIPPED (2): gpu_geometry_wave32_prefix_count_over_a_mask (no wave64), gpu_wave32_program_in_wave64_hardware (no wave64).

Regression run (retail title, scripts/smoke.ps1 -Seconds 60, release; main built from the commit in its block, this PR from its head, no uncommitted changes)

main:

xcl-smoke v1
commit: 06c9f5505f63f10c5ec49ef932d75b2c28177324
gpu: NVIDIA GeForce RTX 4090
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1599 (PresentX 1599)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16384 MB

this PR:

xcl-smoke v1
commit: 44d69219872ae75420b953a8c9853aa6b57a29a3
gpu: NVIDIA GeForce RTX 4090
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1601 (PresentX 1601)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 17795 MB

Not tested

  • AMD, other NVIDIA cards and drivers, Linux. I did not review the diff; this report is the build, the suite and the regression run only.

AI use: Claude Code with Claude Opus 5.5 ran the build, the suite and the regression run and wrote this report at my request.

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

Labels

goal:world-load Bears on the retail title getting past world loading

Projects

None yet

Development

Successfully merging this pull request may close these issues.