Skip to content

D3D12-GPU-SHADOW-AVX2-MERGE-AND-RUNS: AVX2 fast paths for shadow merge, back_piece, and changed_runs - #2699

Merged
MoonEyes merged 2 commits into
ProjectLeonida:mainfrom
bobcatchris:feat/d3d12-gpu-shadow-avx2-merge
Dec 1, 2026
Merged

MoonEyes merged 2 commits into
ProjectLeonida:mainfrom
bobcatchris:feat/d3d12-gpu-shadow-avx2-merge

Conversation

@bobcatchris

@bobcatchris bobcatchris commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Under LEONIDA_RELAY=1 and LEONIDA_LAND=1, per-frame read-back landing (land.rs -> Shadow::card_merge_relay -> merge_piece, changed_runs, and back_piece in d3d12/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, cloning was (title.clone()) per piece in card_merge_relay, and building an intermediate mask buffer in back_piece before doing a second byte-by-byte pass to build runs.

Fix

In d3d12/crates/leonida-gpu/src/shadow.rs (disjoint from #2566 and open #2570 claims):

  • Add runtime-detected AVX2 merge_piece_avx2 (+ merge_piece_scalar tail): compares 32 bytes at a time, skips unchanged blocks where now == held with zero stores to held or title, blends changed bytes via _mm256_blendv_epi8, and updates title only when merged != title.
  • Add runtime-detected AVX2 changed_runs_avx2 (+ changed_runs_scalar tail): compares 64 bytes per iteration with _mm256_cmpeq_epi8 and extracts diff runs directly via push_diff_mask().
  • Add runtime-detected AVX2 back_piece_avx2 (+ back_piece_scalar tail): eliminates mask allocation and its second pass by extracting diff runs straight from the AVX2 compare mask through push_diff_mask().
  • Reuse a thread-local MERGE_WAS scratch buffer inside card_merge_relay instead of heap-allocating title.clone() on every piece (and skip copying when !mapped).

Verification

  • Hardware/OS: Windows 11 Pro (10.0.26200), Ryzen 7 7800X3D, pinned rustc 1.98.1.
  • cargo test -p leonida-gpu --lib shadow::: 59 passed, 0 failed, 2 ignored (includes new avx2_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-apples back_piece wrapper matching main):
    • 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)

@lixonik

lixonik commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Independent Windows A/B of da4cdbbbf4696c62d0eb192eabe091f52267bf66 merged onto 0ae217ceb136401793bf00f6fd7d9055eb0eabc7. 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=1 reported 59 passed and 2 ignored. I ran the ignored shadow_avx2_bench separately.

One benchmark detail: the checked-in scalar arm calls back_piece_scalar for the full buffer. The AVX2 arm calls back_piece, which first narrows the diff span. I compiled the exact main and PR back_piece wrappers 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:

  • 8 changed bytes every 512 bytes: 6.71–7.88×.
  • One 8-byte cluster in the middle: 1.00–1.08×. Both implementations already narrow the span.
  • Dense changes: 2.52–4.30×.
  • Sparse GPU writes with overlapping CPU edits: 7.79–8.12×.

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.

@bobcatchris

Copy link
Copy Markdown
Contributor Author

Updated shadow_avx2_bench so the scalar back_piece arm matches main's span-narrowing + mask allocation flow apples-to-apples (6.99x on 64 KiB sparse: 33.56 us -> 4.80 us; 59/59 shadow:: unit tests pass). AI-assisted: Antigravity (Gemini 4 Neon).

@merzleckeier

Copy link
Copy Markdown
Contributor

Independent Wine check of the AVX2 update 01cb26ba8 on the GitHub merge b56f509c9 (base bbd45f5cc). 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), the shadow:: 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_runs inputs 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:

Change pattern Base PR
Equal 0.894 1.115
Single 8-byte cluster 1.058 1.150
4 bytes every 271 6.156 1.403
Alternating bytes 124.728 50.569
Every byte 19.348 1.839

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.

…anged_runs

AI-assisted: Antigravity (Gemini 4 Neon)
AI-assisted: Antigravity (Gemini 4 Neon)
@KelboLive

Copy link
Copy Markdown

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.rs only (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).

@lixonik

lixonik commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Independent game-run A/B test on Windows 10 + NVIDIA RTX 5070 12 GB (driver 621.40) + Ryzen 9 5950X with LEONIDA_RELAY=1 and LEONIDA_LAND=1 (MEASURED, PROVISIONAL (n=2 balanced pairs: B1/A1, A2/B2), same save restored before every run, d3d12/target/diag built with diagnostics,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 improved 40.28 ms -> 34.45 ms, merging what was fetched improved in pair 1 (2.80 -> 2.30 ms wall / 6.23 -> 5.73 ms all threads) and was flat in pair 2 (3.06 -> 3.07 ms wall / 7.56 -> 7.40 ms all 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 via d3d12_ab_compare.py.)

RUN 2699-A1 | 2026-12-01 UTC | lixonik | AI-assisted: Antigravity
BASE:        a596adfac (main) | shaders: warm cache (25,687 translated, 202 untranslated)
MACHINE:     NVIDIA GeForce RTX 5070, 12 GB, desktop | 621.40 | Windows 10 Pro 22H2 (build 19045) | 64 GiB RAM | Ryzen 9 5950X
RECIPE:      documented (scripts/play-d3d12.ps1 -SeriesS -NoFrameGen) + DEVIATIONS: LEONIDA_RELAY=1, LEONIDA_LAND=1, LEONIDA_KEYS_FILE (menu enter only) | precompile: yes (warm, 6 s)
OS/DRIVER:   TdrDelay=default (Windows 10 D3D12 Agility SDK)
TARGET:      world frame time and land merge time under LEONIDA_RELAY=1 + LEONIDA_LAND=1 | PREDICTED (written before the run): baseline CPU-bound Series S play with 2.5-3.5 ms land merge
MEASURED:    play (12 world samples, max 3,411 draws): median 29.1 fps (p10 23.1, p90 30.4), 34.4 ms/frame (preparing 25.9 ms, copies 10.6 ms / 34.4 MB, textures 0.4 ms, gpu_wait 1.1 ms, copy_back 7.2 ms, 1763 draws); buckets: 1000-2999 29.9 fps (9), 3000-4999 23.1 fps (3); land merge median 2.80 ms wall (6.23 ms all threads, 34 jobs, n=84); 0 errors | duration 219.4 s (75 s in world)
END:         time limit (75 s in world)
CACHES:      warm: shader cache + NVIDIA DXCache (same save restored before start) | RUN 1 of 2 (pair 1, BA order)
INSTRUMENTS: standard diag log, d3d12_run_fps.py
VERDICT:     prediction held
RUN 2699-B1 | 2026-12-01 UTC | lixonik | AI-assisted: Antigravity
BASE:        a596adfac + #2699 (45d09e2d9) | shaders: warm cache (same)
MACHINE:     NVIDIA GeForce RTX 5070, 12 GB, desktop | 621.40 | Windows 10 Pro 22H2 (build 19045) | 64 GiB RAM | Ryzen 9 5950X
RECIPE:      documented (scripts/play-d3d12.ps1 -SeriesS -NoFrameGen) + DEVIATIONS: LEONIDA_RELAY=1, LEONIDA_LAND=1, LEONIDA_KEYS_FILE (menu enter only) | precompile: yes (warm, 6 s)
OS/DRIVER:   TdrDelay=default (Windows 10 D3D12 Agility SDK)
TARGET:      world frame time and land merge time under LEONIDA_RELAY=1 + LEONIDA_LAND=1 | PREDICTED (written before the run): lower land merge CPU time, no regressions
MEASURED:    play (10 world samples, max 3,072 draws): median 39.5 fps (p10 33.6, p90 42.6), 25.4 ms/frame (preparing 19.8 ms, copies 7.3 ms / 30.2 MB, textures 0.3 ms, gpu_wait 0.0 ms, copy_back 6.0 ms, 1062 draws); buckets: 1000-2999 39.9 fps (9, +33% vs A1), 3000-4999 33.6 fps (1); land merge median 2.30 ms wall (5.73 ms all threads, 36 jobs, n=84); 0 errors | duration 368.8 s (75 s in world)
END:         time limit (75 s in world)
CACHES:      warm: shader cache + NVIDIA DXCache (same save restored before start) | RUN 1 of 2 (pair 1, BA order)
INSTRUMENTS: standard diag log, d3d12_run_fps.py
VERDICT:     prediction held (provisional; lower draws in B1 contributed to total frame delta, while land merge wall time also dropped 2.80 -> 2.30 ms despite 36 vs 34 jobs)
RUN 2699-A2 | 2026-12-01 UTC | lixonik | AI-assisted: Antigravity
BASE:        a596adfac (main) | shaders: warm cache (same)
MACHINE:     NVIDIA GeForce RTX 5070, 12 GB, desktop | 621.40 | Windows 10 Pro 22H2 (build 19045) | 64 GiB RAM | Ryzen 9 5950X
RECIPE:      documented (scripts/play-d3d12.ps1 -SeriesS -NoFrameGen) + DEVIATIONS: LEONIDA_RELAY=1, LEONIDA_LAND=1, LEONIDA_KEYS_FILE (menu enter only) | precompile: yes (warm, 6 s)
OS/DRIVER:   TdrDelay=default (Windows 10 D3D12 Agility SDK)
TARGET:      world frame time and land merge time (pair 2, AB order) | PREDICTED (written before the run): consistent with A1
MEASURED:    play (14 world samples, max 4,853 draws): median 21.6 fps (p10 19.5, p90 26.1), 46.1 ms/frame (preparing 31.2 ms, copies 13.6 ms / 44.8 MB, textures 1.1 ms, gpu_wait 1.4 ms, copy_back 7.5 ms, 2686 draws); buckets: 1000-2999 21.4 fps (8), 3000-4999 21.6 fps (6); land merge median 3.06 ms wall (7.56 ms all threads, 40 jobs, n=67); 0 errors | duration 198.9 s (60 s in world)
END:         time limit (60 s in world)
CACHES:      warm: shader cache + NVIDIA DXCache (same save restored before start) | RUN 2 of 2 (pair 2, AB order)
INSTRUMENTS: standard diag log, d3d12_run_fps.py
VERDICT:     prediction held
RUN 2699-B2 | 2026-12-01 UTC | lixonik | AI-assisted: Antigravity
BASE:        a596adfac + #2699 (45d09e2d9) | shaders: warm cache (same)
MACHINE:     NVIDIA GeForce RTX 5070, 12 GB, desktop | 621.40 | Windows 10 Pro 22H2 (build 19045) | 64 GiB RAM | Ryzen 9 5950X
RECIPE:      documented (scripts/play-d3d12.ps1 -SeriesS -NoFrameGen) + DEVIATIONS: LEONIDA_RELAY=1, LEONIDA_LAND=1, LEONIDA_KEYS_FILE (menu enter only) | precompile: yes (warm, 6 s)
OS/DRIVER:   TdrDelay=default (Windows 10 D3D12 Agility SDK)
TARGET:      world frame time and land merge time (pair 2, AB order) | PREDICTED (written before the run): faster or equal to A2 with no regressions
MEASURED:    play (14 world samples, max 5,143 draws): median 22.9 fps (p10 14.3, p90 25.8), 43.5 ms/frame (preparing 32.1 ms, copies 14.2 ms / 48.5 MB, textures 1.1 ms, gpu_wait 1.8 ms, copy_back 7.8 ms, 2976 draws); buckets: 1000-2999 23.7 fps (7, +11% vs A2), 3000-4999 21.8 fps (6, +0.9% vs A2), 5000-6999 15.0 fps (1); land merge median 3.07 ms wall (7.40 ms all threads, 41 jobs, n=67); 0 errors | duration 198.8 s (60 s in world)
END:         time limit (60 s in world)
CACHES:      warm: shader cache + NVIDIA DXCache (same save restored before start) | RUN 2 of 2 (pair 2, AB order)
INSTRUMENTS: standard diag log, d3d12_run_fps.py
VERDICT:     prediction held (provisional: B faster in 2/2 compared buckets in pair 2 and 3/3 buckets across both pairs, 0 regressions)

Per-bucket summary (tools/d3d12_run_fps.py --compare):

  • Pair 1 (A1 vs B1): 1000-2999: A 29.9 (9) vs B 39.9 (9) (+33%)
  • Pair 2 (A2 vs B2): 1000-2999: A 21.4 (8) vs B 23.7 (7) (+11%); 3000-4999: A 21.6 (6) vs B 21.8 (6) (+0%, 21.6 vs 21.8)
  • All-run world medians (tools/d3d12_ab_compare.py): mean A 40.28 ms vs mean B 34.45 ms (PROVISIONAL, n=2 pairs; would need 2 more pairs for a formal 95% CI).

AI-assisted: Antigravity.

@MoonEyes

MoonEyes commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

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 into main 9372a4d4f (clean).

  • Machine: RTX 4060 8 GB, Windows 11, diag profile, features diagnostics,mimalloc, Series S, BAR window off, texture budget 1024 MB.
  • In-game, no scripted action, 480 s asked, a session that loads into the world and stays: main 9372a4d4f and then main + 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 on main, not a comparison: other work ran on the PC during the main run.
  • Picture: screenshots every 60 s. The last four show the same scene in both runs (water, sky, the boat, the map) and look the same; the first four are cutscenes at different moments and were only looked at for broken textures (none seen). No screenshot is posted.
  • Tests on main 9372a4d4f + the seven (Windows): leonida-hle 243, leonida-gpu 602, leonida-formats 47, leonida 47, leonida-shader 403 of 405: two source_hash_tests failed 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).

@MoonEyes

MoonEyes commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Merging. Head 45d09e2d9 merges cleanly with main da56079c9 and 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).

@MoonEyes
MoonEyes merged commit 653f60d into ProjectLeonida:main Dec 1, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants