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

Merged
bobcatchris merged 1 commit from upstream-pr/2689 into main 2026-12-01 10:07:52 +00:00
bobcatchris commented 2026-11-30 02:48:25 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by bobcatchris on 2026-11-30T02:48:25Z.


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)

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5558073346 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2689). Originally posted by [bobcatchris](https://github.com/bobcatchris) on 2026-11-30T02:48:25Z. --- 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 commented 2026-11-30 03:27:40 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-30T03:27:40Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7267911974 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2689#issuecomment-7267911974). Originally posted by [lixonik](https://github.com/lixonik) on 2026-11-30T03:27:40Z. --- 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.
KelboLive commented 2026-11-30 15:58:14 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-11-30T15:58:14Z.


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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7283906752 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2689#issuecomment-7283906752). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-11-30T15:58:14Z. --- 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 commented 2026-11-30 17:20:39 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-30T17:20:39Z.


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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7286121313 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2689#issuecomment-7286121313). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-11-30T17:20:39Z. --- ### 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 commented 2026-12-01 08:33:42 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-01T08:33:42Z.


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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7305480553 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2689#issuecomment-7305480553). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-01T08:33:42Z. --- 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 commented 2026-12-01 10:07:48 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-01T10:07:48Z.


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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7307713848 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2689#issuecomment-7307713848). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-01T10:07:48Z. --- 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).
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!2689
No description provided.