D3D12-FORMATS-BCPACK-FAST-LANES: branchless AVX2 model lookup for BCPack rANS lanes #2690

Merged
bobcatchris merged 1 commit from upstream-pr/2690 into main 2026-12-01 11:11:32 +00:00
bobcatchris commented 2026-11-30 02:54:35 +00:00 (Migrated from github.com)

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


Claimed in #2570 as D3D12-FORMATS-BCPACK-FAST-LANES (#issuecomment-7267372755).

What and why

DirectStorage texture streaming decompresses BC1–BC7 textures through d3d12/crates/leonida-formats/src/bcpack.rs (d3d12/docs/findings.md notes DirectStorage threads on the frame's idle chain). Previously, every rANS symbol decoded in Lane::step() performed four separate 2-D table lookups (freq, start, 32 KiB bucket[256] window table, recip), a branchy while start[b + 1] <= slot loop, an if b == 0 branch, and an infallible Result<(), Error> unwrap.

This change:

  • Packs per-model decode parameters into a 64-byte-aligned Model struct (start_1_8: [i32; 8], adj_start, adj_freq, base_sym, adj_recip) with pre-adjusted b == 0 entries (adj_start[0] = 1, adj_freq[0] = freq[0] - 1, adj_recip[0] = 0, base_sym[0] = 0), eliminating the if b == 0 branch.
  • Dispatches once at lanes() entry to an AVX2 path (#[target_feature(enable = "avx2")]) that resolves bucket_of_avx2() in 3 instructions (_mm256_cmpgt_epi32 + _mm256_movemask_ps + count_ones) with zero branches, falling back cleanly to bucket_of on non-AVX2 hosts.
  • Removes the unused Result<(), Error> wrapper from Lane::step().

Verification & Measured --release A/B Benchmark

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

  • Exhaustive AVX2 parity: bucket_lookup_matches_a_scan_of_the_starts verified bucket_of_avx2 against the reference scan across all 3 × 128 × 65,536 = 25,165,824 (table, model, slot) pairs — 0 mismatches.
  • Built-in --release benchmark (cargo test --release -p leonida-formats bcpack_throughput -- --ignored --nocapture, 16,384 blocks × 40 runs):
    • BC1: 86.3 → 94.7 MB out/s (92.7 → 84.4 ns/block, +9.7%)
    • BC4: 83.0 → 93.8 MB out/s (96.4 → 85.3 ns/block, +13.0%)
    • BC7: 49.7 → 53.3 MB out/s (322.0 → 300.3 ns/block, +7.2%)
  • cargo test -p leonida-formats --lib — 41 passed; 0 failed (every_layout_round_trips_through_the_lanes, payload_step_matches_the_reference_decode, bc7_dictionary_indices_line_up).
  • cargo clippy -p leonida-formats --no-deps --lib -- -D warnings — 0 warnings.
  • python -I scripts/check_no_game_data.py d3d12/crates/leonida-formats/src/bcpack.rs — PASS.

AI-assisted: Antigravity (Gemini 4 Neon)

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5558131619 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2690). Originally posted by [bobcatchris](https://github.com/bobcatchris) on 2026-11-30T02:54:35Z. --- Claimed in #2570 as `D3D12-FORMATS-BCPACK-FAST-LANES` (`#issuecomment-7267372755`). ### What and why DirectStorage texture streaming decompresses BC1–BC7 textures through `d3d12/crates/leonida-formats/src/bcpack.rs` (`d3d12/docs/findings.md` notes DirectStorage threads on the frame's idle chain). Previously, every rANS symbol decoded in `Lane::step()` performed four separate 2-D table lookups (`freq`, `start`, 32 KiB `bucket[256]` window table, `recip`), a branchy `while start[b + 1] <= slot` loop, an `if b == 0` branch, and an infallible `Result<(), Error>` unwrap. This change: - Packs per-model decode parameters into a 64-byte-aligned `Model` struct (`start_1_8: [i32; 8]`, `adj_start`, `adj_freq`, `base_sym`, `adj_recip`) with pre-adjusted `b == 0` entries (`adj_start[0] = 1`, `adj_freq[0] = freq[0] - 1`, `adj_recip[0] = 0`, `base_sym[0] = 0`), eliminating the `if b == 0` branch. - Dispatches once at `lanes()` entry to an AVX2 path (`#[target_feature(enable = "avx2")]`) that resolves `bucket_of_avx2()` in 3 instructions (`_mm256_cmpgt_epi32` + `_mm256_movemask_ps` + `count_ones`) with zero branches, falling back cleanly to `bucket_of` on non-AVX2 hosts. - Removes the unused `Result<(), Error>` wrapper from `Lane::step()`. ### Verification & Measured `--release` A/B Benchmark Hardware / OS: native Windows 11 Pro (`10.0.26200`), AMD Ryzen 7 7800X3D, pinned toolchain `rustc 1.98.1`. - **Exhaustive AVX2 parity:** `bucket_lookup_matches_a_scan_of_the_starts` verified `bucket_of_avx2` against the reference scan across all `3 × 128 × 65,536 = 25,165,824` `(table, model, slot)` pairs — **0 mismatches**. - **Built-in `--release` benchmark** (`cargo test --release -p leonida-formats bcpack_throughput -- --ignored --nocapture`, 16,384 blocks × 40 runs): - **BC1:** `86.3 → 94.7 MB out/s` (`92.7 → 84.4 ns/block`, **+9.7%**) - **BC4:** `83.0 → 93.8 MB out/s` (`96.4 → 85.3 ns/block`, **+13.0%**) - **BC7:** `49.7 → 53.3 MB out/s` (`322.0 → 300.3 ns/block`, **+7.2%**) - `cargo test -p leonida-formats --lib` — **`41 passed; 0 failed`** (`every_layout_round_trips_through_the_lanes`, `payload_step_matches_the_reference_decode`, `bc7_dictionary_indices_line_up`). - `cargo clippy -p leonida-formats --no-deps --lib -- -D warnings` — **0 warnings**. - `python -I scripts/check_no_game_data.py d3d12/crates/leonida-formats/src/bcpack.rs` — **PASS**. AI-assisted: Antigravity (Gemini 4 Neon)
OreoFR commented 2026-11-30 02:56:45 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-30T02:56:45Z.


Checked by OreoFR on macOS 27.2.1, Apple M5 (arm64, no AVX2), Rust 1.98.1, cargo test -p leonida-formats, main 95bc33f6e, PR head 121f6a0e2. MEASURED, partial:

  • main: 41 passed / 5 ignored; main + this PR: 41 passed / 5 ignored. So the crate still builds and passes on a non-x86 host: the AVX2 code is correctly kept out of the build there, and the portable path is unchanged in behaviour as far as these tests go.
  • Not covered: the AVX2 path itself (needs an x86-64 host with AVX2) and its speed-up. This PR adds no test of its own.
  • Merges cleanly with main and with #2566.

AI-assisted: Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7267452554 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2690#issuecomment-7267452554). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-30T02:56:45Z. --- Checked by OreoFR on macOS 27.2.1, **Apple M5 (arm64, no AVX2)**, Rust 1.98.1, `cargo test -p leonida-formats`, main 95bc33f6e, PR head 121f6a0e2. MEASURED, partial: - main: 41 passed / 5 ignored; main + this PR: **41 passed / 5 ignored**. So the crate still builds and passes on a non-x86 host: the AVX2 code is correctly kept out of the build there, and the portable path is unchanged in behaviour as far as these tests go. - Not covered: the AVX2 path itself (needs an x86-64 host with AVX2) and its speed-up. This PR adds no test of its own. - Merges cleanly with main and with #2566. AI-assisted: Claude Code
merzleckeier commented 2026-11-30 10:57:10 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-30T10:57:10Z.


Independent Linux AVX2 check. I merged 121f6a0e2 onto 0568cd62e (471e296e1 tree). CachyOS x86-64, Ryzen 7 7800X3D, Rust 1.98.1. The merge onto d367675c7 is also clean and the formats crate and Cargo inputs are the same.

  • cargo test --locked --offline -p leonida-formats --lib gave 42 passed and 5 ignored on each revision. The AVX2 bucket comparison covered the 25,165,824 stock (table, model, slot) combinations.

  • A separate test called the scalar and AVX2 lane loops directly for 72 synthetic streams spanning all six lane layouts, with random symbols and alternating alphabet extremes. Both paths decoded 489,600 symbols exactly and requested bytes in the same lane order.

  • bcpack_throughput in separate release builds pinned to CPU 6, two ABBA blocks after one warm-up per build. Median ns/block from main to PR was BC1 93.10 → 81.85 (+13.7% throughput), BC4 93.25 → 83.65 (+11.5%) and BC7 318.70 → 294.00 (+8.4%). One BC1 PR sample was 98.4 ns/block and is included. The other three were 81.6–82.0.

CPU-only synthetic tests. AI-assisted with OpenAI Codex.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7276314144 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2690#issuecomment-7276314144). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-11-30T10:57:10Z. --- **Independent Linux AVX2 check.** I merged `121f6a0e2` onto `0568cd62e` (`471e296e1` tree). CachyOS x86-64, Ryzen 7 7800X3D, Rust 1.98.1. The merge onto `d367675c7` is also clean and the formats crate and Cargo inputs are the same. - `cargo test --locked --offline -p leonida-formats --lib` gave 42 passed and 5 ignored on each revision. The AVX2 bucket comparison covered the 25,165,824 stock (table, model, slot) combinations. - A separate test called the scalar and AVX2 lane loops directly for 72 synthetic streams spanning all six lane layouts, with random symbols and alternating alphabet extremes. Both paths decoded 489,600 symbols exactly and requested bytes in the same lane order. - `bcpack_throughput` in separate release builds pinned to CPU 6, two ABBA blocks after one warm-up per build. Median ns/block from `main` to PR was BC1 **93.10 → 81.85** (+13.7% throughput), BC4 **93.25 → 83.65** (+11.5%) and BC7 **318.70 → 294.00** (+8.4%). One BC1 PR sample was 98.4 ns/block and is included. The other three were 81.6–82.0. CPU-only synthetic tests. AI-assisted with OpenAI Codex.
aslan2142 commented 2026-12-01 11:11:29 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T11:11:29Z.

Review: APPROVED


Verified bucket_of_avx2 in d3d12/crates/leonida-formats/src/bcpack.rs: slot + 1 is in 1..=65536 and start_1_8[k] is in 0..=65536, so signed 32-bit _mm256_cmpgt_epi32 is exact across the entire range; the b == 0 path (adj_start[0] = 1, adj_recip[0] = 0, base_sym[0] = 0) produces sym = 0 and rem = slot.wrapping_sub(1), bit-identical to the scalar path. Exhaustive 25.1M-entry AVX2/scalar parity tests and non-x86 fallback builds are verified, and all CI checks are green. Merging.

(AI-assisted review.)

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6130075350 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2690#pullrequestreview-6130075350). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T11:11:29Z. **Review:** APPROVED --- Verified `bucket_of_avx2` in `d3d12/crates/leonida-formats/src/bcpack.rs`: `slot + 1` is in `1..=65536` and `start_1_8[k]` is in `0..=65536`, so signed 32-bit `_mm256_cmpgt_epi32` is exact across the entire range; the `b == 0` path (`adj_start[0] = 1`, `adj_recip[0] = 0`, `base_sym[0] = 0`) produces `sym = 0` and `rem = slot.wrapping_sub(1)`, bit-identical to the scalar path. Exhaustive 25.1M-entry AVX2/scalar parity tests and non-x86 fallback builds are verified, and all CI checks are green. Merging. *(AI-assisted review.)*
enlight3d commented 2026-12-01 11:18:06 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by enlight3d on 2026-12-01T11:18:06Z.


Posted after the merge: the head tested below is the one that was merged.

RUN (independent run on Windows, #2690 head 782c93512, merged locally into main e56aff1ce with git merge --no-ff, no conflicts. CI on the head: change-scope, check, linux-python-and-viewer, no-game-data and semantic-tests all pass.)

This CPU (i9-13900K) has AVX2, so lanes takes the new AVX2 path here.

Commands, in d3d12/, target x86_64-pc-windows-msvc, toolchain 1.98.1, all with --locked, at low priority:

  • cargo test -p leonida-formats --no-fail-fast: main 47 passed / 0 failed / 7 ignored; merge 47 / 0 / 7. The pull request adds an AVX2 check inside bucket_lookup_matches_a_scan_of_the_starts, not a new test. The crate has no diagnostics feature.
  • Red: on main the new check cannot compile (there is no bucket_of_avx2), so the red is by mutation, with the tests kept:
    • bucket_of_avx2 off by one (slot instead of slot + 1 in the compare): bucket_lookup_matches_a_scan_of_the_starts fails on "avx2 model 0 slot 32768" (left 0, right 1). Four decode tests that go through lanes fail too (every_layout_round_trips_through_the_lanes, bc4_blocks_with_and_without_references, bc7_mode6_block, malformed_input_is_an_error), so the decode tests do run the AVX2 path on this CPU.
    • Bucket 0 wrong in the new per-model table (adj_freq[0] = freq[m][0] instead of freq[m][0] - 1): 6 tests fail, among them payload_step_matches_the_reference_decode ("model 0 x 0x1000001") and the round trips. So the existing tests pin the new tables.
    • Green: the file restored (git status clean): 47 / 0 / 7.
  • cargo clippy -p leonida-formats --all-targets, main and merge: 1 warning on each side (texture.rs, unrelated), none new.
  • Bench: the release test binary of main and of the merge, bcpack_throughput --ignored --nocapture --test-threads=1, run alternately 7 times each, at low priority with other jobs on the PC. Median ns/block, main -> merge: BC1 83.5 -> 68.5 (-18 %), BC4 80.9 -> 70.2 (-13 %), BC7 242.0 -> 200.8 (-17 %). Best of 7: BC1 76.9 -> 66.5, BC4 76.2 -> 63.8, BC7 221.5 -> 197.5. The body gives -7 to -12 % on a Ryzen 7 7800X3D; the gain is there, a bit larger on this CPU.
  • Not covered: the scalar fallback of lanes (a CPU without AVX2). On an AVX2 machine lanes never calls lanes_impl with bucket_of; only payload_step_matches_the_reference_decode drives Lane::step with the scalar bucket_of. A test that calls lanes_impl with bucket_of would cover that path on every machine. The game (texture streaming) is not covered either.
  • Small note: the pull request deletes the // shortcut: comment above lanes, but the decoder still steps one lane at a time, which is what that note was about.

Machine: Windows 11 Pro 26300, i9-13900K, 64 GB, RTX 3090 24 GB, NVIDIA 621.40, Rust 1.98.1.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7309450657 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2690#issuecomment-7309450657). Originally posted by [enlight3d](https://github.com/enlight3d) on 2026-12-01T11:18:06Z. --- Posted after the merge: the head tested below is the one that was merged. RUN (independent run on Windows, #2690 head 782c93512, merged locally into main e56aff1ce with `git merge --no-ff`, no conflicts. CI on the head: change-scope, check, linux-python-and-viewer, no-game-data and semantic-tests all pass.) This CPU (i9-13900K) has AVX2, so `lanes` takes the new AVX2 path here. Commands, in `d3d12/`, target x86_64-pc-windows-msvc, toolchain 1.98.1, all with `--locked`, at low priority: - `cargo test -p leonida-formats --no-fail-fast`: main 47 passed / 0 failed / 7 ignored; merge 47 / 0 / 7. The pull request adds an AVX2 check inside `bucket_lookup_matches_a_scan_of_the_starts`, not a new test. The crate has no `diagnostics` feature. - Red: on main the new check cannot compile (there is no `bucket_of_avx2`), so the red is by mutation, with the tests kept: - `bucket_of_avx2` off by one (`slot` instead of `slot + 1` in the compare): `bucket_lookup_matches_a_scan_of_the_starts` fails on "avx2 model 0 slot 32768" (left 0, right 1). Four decode tests that go through `lanes` fail too (`every_layout_round_trips_through_the_lanes`, `bc4_blocks_with_and_without_references`, `bc7_mode6_block`, `malformed_input_is_an_error`), so the decode tests do run the AVX2 path on this CPU. - Bucket 0 wrong in the new per-model table (`adj_freq[0] = freq[m][0]` instead of `freq[m][0] - 1`): 6 tests fail, among them `payload_step_matches_the_reference_decode` ("model 0 x 0x1000001") and the round trips. So the existing tests pin the new tables. - Green: the file restored (`git status` clean): 47 / 0 / 7. - `cargo clippy -p leonida-formats --all-targets`, main and merge: 1 warning on each side (`texture.rs`, unrelated), none new. - Bench: the release test binary of main and of the merge, `bcpack_throughput --ignored --nocapture --test-threads=1`, run alternately 7 times each, at low priority with other jobs on the PC. Median ns/block, main -> merge: BC1 83.5 -> 68.5 (-18 %), BC4 80.9 -> 70.2 (-13 %), BC7 242.0 -> 200.8 (-17 %). Best of 7: BC1 76.9 -> 66.5, BC4 76.2 -> 63.8, BC7 221.5 -> 197.5. The body gives -7 to -12 % on a Ryzen 7 7800X3D; the gain is there, a bit larger on this CPU. - Not covered: the scalar fallback of `lanes` (a CPU without AVX2). On an AVX2 machine `lanes` never calls `lanes_impl` with `bucket_of`; only `payload_step_matches_the_reference_decode` drives `Lane::step` with the scalar `bucket_of`. A test that calls `lanes_impl` with `bucket_of` would cover that path on every machine. The game (texture streaming) is not covered either. - Small note: the pull request deletes the `// shortcut:` comment above `lanes`, but the decoder still steps one lane at a time, which is what that note was about. Machine: Windows 11 Pro 26300, i9-13900K, 64 GB, RTX 3090 24 GB, NVIDIA 621.40, Rust 1.98.1.
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!2690
No description provided.