Skip to content

D3D12-GPU-CHANGED-RECORDS-AVX2: add AVX2 fast path for 32-byte descriptor changed_records - #2689

Merged
MoonEyes merged 1 commit into
ProjectLeonida:mainfrom
bobcatchris:feat/d3d12-gpu-changed-records-avx2
Dec 1, 2026
Merged

MoonEyes merged 1 commit into
ProjectLeonida:mainfrom
bobcatchris:feat/d3d12-gpu-changed-records-avx2

Conversation

@bobcatchris

Copy link
Copy Markdown
Contributor

Claimed in #2570 as D3D12-GPU-CHANGED-RECORDS-AVX2 (#issuecomment-7267296583).

What and why

In d3d12/crates/leonida-gpu/src/descriptor/host.rs, changed_records() scans ~400,000 console descriptor slots (32 bytes each) and mirrors ~40,000 changed slots per frame (d3d12/docs/findings.md). Previously, whenever any 1 of 16 records in a 512-byte chunk differed, changed_records() ran memcmp on the 512-byte chunk and then re-scanned all 16 records with 16 individual memcmp + memcpy calls.

This change adds a runtime-detected AVX2 fast path (std::arch::is_x86_feature_detected!("avx2")) for step == 32:

  • Loads 4 × 32-byte descriptor records (128 bytes) into ymm registers.
  • Combines all 4 vpcmpeqb results with vpand into a single vpmovmskb == -1 fast-path branch.
  • On mismatch, stores each differing, already-loaded new ymm register directly with _mm256_storeu_si256 (zero second load, zero memcmp/memcpy calls).
  • Falls back cleanly to changed_records_scalar() on non-AVX2 CPUs or non-32-byte record strides.

Verification & Measured --release A/B Benchmark

Hardware / OS: native Windows 11 Pro (10.0.26200), AMD Ryzen 7 7800X3D, AMD Radeon RX 7900 XTX (gfx1100), pinned toolchain rustc 1.98.1.

  • cargo test --release -p leonida-gpu --lib changed_records_bench -- --ignored --nocapture (400,000 × 32 B slots, 50 rounds, bit-exact parity asserted every round):
    • 0% dirty: 325.9 µs → 309.8 µs/frame (1.05×)
    • 10% dirty (40k/frame): 432.5 µs → 325.9 µs/frame (1.33×)
    • 100% dirty: 611.3 µs → 485.1 µs/frame (1.26×)
  • cargo test -p leonida-gpu --lib descriptor::host -- --test-threads=4 — 53 passed; 0 failed; 2 ignored (0.48s).
  • python -I scripts/check_no_game_data.py d3d12/crates/leonida-gpu/src/descriptor/host.rs — PASS.

AI-assisted: Antigravity (Gemini 4 Neon)

@lixonik

lixonik commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Independent Windows 10 AVX2 check on ebaa9ec8377dbad29146d2977a920df4ac3b921a merged locally into 1d22590b9fb5c0f51a6a7c0405f1f136e0696c0d. Ryzen 9 5950X, Windows 10 22H2 build 19045, Rust 1.98.1 MSVC.

  • The crate's changed_records parity test passed under --profile diag --features diagnostics. The checked-in ignored benchmark also ran successfully, with 1.22×, 1.70× and 1.65× speedups at 0%, 10% and 100% changed records.
  • In a separate CPU harness I compiled the exact scalar and dispatch functions from the merged source at opt-level=3. It alternated execution order each round, used the median of 60 rounds per pattern, and verified both output indices and resulting buffers every round. At about 10% changed, the speedup ranges across three fresh processes were 1.17–1.20× periodic, 1.06–1.07× pseudorandom, and 1.21–1.28× for runs of 32 adjacent records. All runs used 400,000 synthetic 32-byte records.
  • The original benchmark's regular layout gives a useful comparison point. The spread across layouts is worth keeping in mind when estimating frame-time savings.

CPU microbenchmarks and synthetic data only.

AI-assisted.

…s (D3D12-GPU-CHANGED-RECORDS-AVX2)

changed_records() scans ~400,000 32-byte descriptor slots per frame
(mirroring ~40,000, d3d12/docs/findings.md). Previously, any dirty
512-byte chunk ran memcmp on the 16-record block and then ran 16
individual memcmp + memcpy calls.

Add a runtime-detected AVX2 fast path for step == 32 that loads four
32-byte records (128 bytes) into ymm registers, checks all four with a
single combined vpcmpeqb + vpand + vpmovmskb branch, and on mismatch
stores the already-loaded new ymm vectors directly with
_mm256_storeu_si256 (avoiding the double-scan and memcpy calls).

Measured in --release on Ryzen 7 7800X3D (400k x 32-byte slots, 50
rounds, bit-exact parity verified):
- 0% dirty:   325.9 -> 309.8 us/frame (1.05x)
- 10% dirty:  432.5 -> 325.9 us/frame (1.33x)
- 100% dirty: 611.3 -> 485.1 us/frame (1.26x)

AI-assisted: Antigravity (Gemini 4 Neon)
@bobcatchris
bobcatchris force-pushed the feat/d3d12-gpu-changed-records-avx2 branch from ebaa9ec to 08a1cc4 Compare November 30, 2026 11:21
@KelboLive

Copy link
Copy Markdown

Independent check on Windows (not the author), at 08a1cc4a0.

  • Under concurrent load (another cargo test suite running): 562 passed, 1 failed — descriptor::tests::records_copied_as_bytes_are_made_at_the_next_sync, descriptor.rs:2260: assertion failed: host.made(5).
  • Rerun alone, no other cargo test: 563 passed, 0 failed, 53 ignored (12.9 s).
  • main b17b22d33 alone: 570 passed, 0 failed.

The same failure appeared for #2701 (which does not touch descriptors) under the same concurrent-load condition, so it looks like a load-sensitive write-watch test rather than this PR's AVX2 path. I reported that separately as a suite finding.

Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711, Rust 1.98.1 pinned.

AI-assisted: yes (opencode).

@merzleckeier

Copy link
Copy Markdown
Contributor

Bit-exact CPU composition with the shadow AVX2 paths in #2699

MEASURED. I tested #2689 08a1cc4a0 and #2699 45d09e2d9, separately and together, on main 1d4949d58 (combined tree 43bb79955). The two tested source files are identical in the corresponding clean merges on ad25611b5. Rust 1.98.1, Ryzen 7 7800X3D, optimized native Linux and Windows x64 (Wine 11.22) builds.

The standalone fixture extracts merge_piece, back_piece, changed_runs, changed_records, and their helpers from each tree. It feeds synthetic merge and readback outputs into the record detector and compares against a bytewise reference. The 3,600 cases check 16- and 32-byte records, incomplete trailing records, unaligned slices, overlapping CPU/GPU changes, exact changed ranges and slot IDs, guard bytes, and a repeated publication with no new changes.

All 30 runs produced the same 6,955,200 output bytes. That is three runs per platform for main, each PR alone, both PRs with default dispatch, and both PRs forced to scalar. The combined default runs exercised all four AVX2 paths. The test-only gate recorded zero AVX2 selections in the forced-scalar runs.

Scope: synthetic CPU function composition and output parity. AI-assisted: Codex (GPT-6.2 Sol).

@MoonEyes

MoonEyes commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

RUN - VERIFY-2689-RTX4060 - by MoonEyes (another person than the author) on head 08a1cc4a0, 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 08a1cc4a0 merges cleanly with main 3393ceec9 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) and merzleckeier (bit-exact with #2699). Merged by MoonEyes.

AI-assisted (Claude Code).

@MoonEyes
MoonEyes merged commit da56079 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