D3D12-GPU-SHADOW-AVX2-MERGE-AND-RUNS: AVX2 fast paths for shadow merge, back_piece, and changed_runs #2699
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2699"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ubiquitarianism/leonida. Originally posted by bobcatchris on 2026-11-30T05:49:20Z.
Problem
Under
LEONIDA_RELAY=1andLEONIDA_LAND=1, per-frame read-back landing (land.rs->Shadow::card_merge_relay->merge_piece,changed_runs, andback_pieceind3d12/crates/leonida-gpu/src/shadow.rs) spent ~5.7 ms of CPU time across worker threads per frame scanning and merging up to 64 KiB piece stretches byte-by-byte, cloningwas(title.clone()) per piece incard_merge_relay, and building an intermediatemaskbuffer inback_piecebefore doing a second byte-by-byte pass to buildruns.Fix
In
d3d12/crates/leonida-gpu/src/shadow.rs(disjoint from#2566and open#2570claims):merge_piece_avx2(+merge_piece_scalartail): compares 32 bytes at a time, skips unchanged blocks wherenow == heldwith zero stores toheldortitle, blends changed bytes via_mm256_blendv_epi8, and updatestitleonly whenmerged != title.changed_runs_avx2(+changed_runs_scalartail): compares 64 bytes per iteration with_mm256_cmpeq_epi8and extracts diff runs directly viapush_diff_mask().back_piece_avx2(+back_piece_scalartail): eliminatesmaskallocation and its second pass by extracting diff runs straight from the AVX2 compare mask throughpush_diff_mask().MERGE_WASscratch buffer insidecard_merge_relayinstead of heap-allocatingtitle.clone()on every piece (and skip copying when!mapped).Verification
10.0.26200), Ryzen 7 7800X3D, pinnedrustc 1.98.1.cargo test -p leonida-gpu --lib shadow::: 59 passed, 0 failed, 2 ignored (includes newavx2_merge_back_and_runs_match_scalar_across_lengths_and_patterns).cargo test --release -p leonida-gpu --lib shadow_avx2_bench -- --ignored --nocapture(64 KiB sparse 4 B writes every 272 B; apples-to-applesback_piecewrapper matchingmain):merge_piece: 16.46 us -> 3.08 us (5.35x)changed_runs: 8.34 us -> 1.59 us (5.24x)back_piece: 33.56 us -> 4.80 us (6.99x)cargo test --release -p leonida-gpu --lib runs_of_equal_pages -- --ignored --nocapture:3.4 us by eights, 0.8 us now.python -I scripts/check_no_game_data.py d3d12/crates/leonida-gpu/src/shadow.rs: PASS.Claimed in #2570 as
D3D12-GPU-SHADOW-AVX2-MERGE-AND-RUNS.AI-assisted: Antigravity (Gemini 4 Neon)
Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-30T06:00:40Z.
Independent Windows A/B of
da4cdbbbf4696c62d0eb192eabe091f52267bf66merged onto0ae217ceb136401793bf00f6fd7d9055eb0eabc7. Ryzen 9 5950X with AVX2, Windows 10 22H2 build 19045, Rust 1.98.1 MSVC.cargo test --locked --offline --profile diag -p leonida-gpu --features diagnostics --lib shadow:: -- --test-threads=1reported 59 passed and 2 ignored. I ran the ignoredshadow_avx2_benchseparately.One benchmark detail: the checked-in scalar arm calls
back_piece_scalarfor the full buffer. The AVX2 arm callsback_piece, which first narrows the diff span. I compiled the exact main and PRback_piecewrappers side by side in a standalone optimized CPU harness, with the same synthetic reader, retained buffers and alternating A/B order. Each case took the median of 224 calls per process, repeated in three processes. On a 64 KiB piece:Main and PR returned identical spans, changed runs, merged data, and staging bytes in every comparison. Synthetic CPU measurements only. No frame-time estimate from this test.
AI-assisted.
Imported from ubiquitarianism/leonida. Originally posted by bobcatchris on 2026-11-30T06:16:14Z.
Updated
shadow_avx2_benchso the scalarback_piecearm matchesmain's span-narrowing +maskallocation flow apples-to-apples (6.99xon 64 KiB sparse:33.56 us -> 4.80 us;59/59shadow::unit tests pass). AI-assisted: Antigravity (Gemini 4 Neon).Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-30T07:41:12Z.
Independent Wine check of the AVX2 update
01cb26ba8on the GitHub mergeb56f509c9(basebbd45f5cc). CachyOS, Ryzen 7 7800X3D, Rust 1.98.1 MSVC target, Wine 11.22, VKD3D-Proton 3.1.0 (0bb64c56c8f0dcc). On both RTX 4070 SUPER (620.73.04) and Radeon 610M/Raphael (RADV 26.3.1), theshadow::suite passed 58 on base and 59 on the PR, with zero failures. Both runs initialized the selected adapter and passed the landing/merge tests. A separate CPU probe matched a bytewise reference for merge, exact runs and readback in 540 synthetic cases per arm, including unaligned slices, SIMD boundaries, overlapping CPU writes and failed reads.The same 64 KiB
changed_runsinputs on the production base and PR functions give a workload-dependent result. Optimized Linux build, seven fresh process pairs with alternating order, one logical CPU, median µs/call:The equal page was about 0.22 µs slower against the actual base implementation, which skips equal 512-byte blocks. The ignored benchmark’s 8-byte loop measures a different baseline. Spread-out and dense writes improved substantially. These are synthetic tests and CPU timings.
AI-assisted with OpenAI Codex.
Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-11-30T15:41:11Z.
Independent check on Windows (not the author), at
45d09e2d9.cargo test --locked --jobs 8 -p leonida-gpu --lib -- --test-threads=8: 564 passed, 0 failed, 53 ignored in 15.3 s.The diff is
shadow.rsonly (AVX2 fast paths), so this is compile + regression coverage; I did not independently measure the claimed frame-time gain, which needs a game run.Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711, NVIDIA RTX 4070 Ti SUPER (driver 621.01), Rust 1.98.1 pinned.
AI-assisted: yes (opencode).
Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-12-01T03:25:34Z.
Independent game-run A/B test on Windows 10 + NVIDIA RTX 5070 12 GB (driver 621.40) + Ryzen 9 5950X with
LEONIDA_RELAY=1andLEONIDA_LAND=1(MEASURED,PROVISIONAL (n=2 balanced pairs: B1/A1, A2/B2), same save restored before every run,d3d12/target/diagbuilt withdiagnostics,dlss,mimalloc).Verdict:
PROVISIONAL (n=2 pairs): better / parity-- B was faster in all 3 compared draw buckets (0 slower), mean world median frame time improved40.28 ms -> 34.45 ms,merging what was fetchedimproved in pair 1 (2.80 -> 2.30 mswall /6.23 -> 5.73 msall threads) and was flat in pair 2 (3.06 -> 3.07 mswall /7.56 -> 7.40 msall threads), with 0 errors and 0 regressions across all runs. (Note: draw counts drifted between pair 1 runs due to world streaming timing; 4+ balanced pairs would be needed for a formal 95% t-interval viad3d12_ab_compare.py.)Per-bucket summary (
tools/d3d12_run_fps.py --compare):1000-2999: A29.9 (9)vs B39.9 (9)(+33%)1000-2999: A21.4 (8)vs B23.7 (7)(+11%);3000-4999: A21.6 (6)vs B21.8 (6)(+0%, 21.6 vs 21.8)tools/d3d12_ab_compare.py): mean A40.28 msvs mean B34.45 ms(PROVISIONAL, n=2 pairs; would need 2 more pairs for a formal 95% CI).AI-assisted: Antigravity.
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-01T08:33:48Z.
RUN - VERIFY-2699-RTX4060 - by MoonEyes (another person than the author) on head
45d09e2d9, with #2836, #2835, #2695, #2696, #2689, #2699 and #2705 merged together intomain9372a4d4f(clean).main9372a4d4fand thenmain+ the seven, one run each, same save: 0 device lost, 0 panics, 0 exceptions in both, about 490 s in the world each, alive at the time limit. Frame rate median 17.8 with the seven and 15.0 onmain, not a comparison: other work ran on the PC during themainrun.main9372a4d4f+ the seven (Windows): leonida-hle 243, leonida-gpu 602, leonida-formats 47, leonida 47, leonida-shader 403 of 405: twosource_hash_testsfailed on a stale build script of mine (a target folder shared with an older branch) and pass after a rebuild of it (3 of 3).Limits: one card, one run per arm, two arms only, the seven together. Values only, no game data.
AI-assisted (Claude Code).
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-01T10:18:37Z.
Merging. Head
45d09e2d9merges cleanly withmainda56079c9and does not force a rebase of #2566. CI green on this head and on main. In-game run on the RTX 4060 posted above (this head, with the six others, about 490 s in the world, 0 device lost, pictures compared with main). Tests on main + the seven: leonida-hle, leonida-gpu, leonida-formats, leonida, leonida-shader, 0 failed. Verified on it by KelboLive (Windows), merzleckeier, and lixonik (game run A/B, RTX 5070). Merged by MoonEyes.AI-assisted (Claude Code).