formats: bcpack: AVX2 lane schedule and SSE4.1/SSSE3 block packing #363

Manually merged
github merged 3 commits from github-bot/gh-363 into main 2026-12-05 17:31:23 +00:00 AGit
Owner

@⁠lucamzanon on GitHub · 2026-12-03 20:13 UTC · original
lucamzanon:xmem/1-bcpack-avx2 → main · +2152 −43 · 1 files · 3 commits · merged 2026-12-05 17:27 UTC

Summary

Accelerates CPU BCPack texture decompression (crates/victory-formats/src/bcpack.rs) with an AVX2 branchless lane scheduler and SSE4.1/SSSE3 block packing for BC1–BC7, with automatic runtime/target fallback to the original scalar implementation.

Changes

  • AVX2 lane schedule (70–80% of decode time): processes two lane vectors per round branchlessly (step, byte order, stalls, delivery) with identical semantics to the scalar schedule (including truncated/garbage streams).
  • SSE4.1/SSSE3 block packing:
    • BC1–BC5: 8-symbol residual unpack and 2/3-bit index packing via pmaddubsw/pmaddwd.
    • BC6H/BC7: vectorized index residuals + layout-driven block writes replacing per-field bit-writer calls.
  • Scalar fallback: original scalar path retained for non-x86_64 or CPUs without AVX2/SSE4.1/SSSE3.

Verification & Benchmarks (Ryzen 7 5700X / RX 9070 XT, Linux RADV)

  • Bit-exactness: 19/19 bcpack unit tests pass, including 7 new scalar-vs-SIMD differential tests covering all layouts, truncated/garbage streams, rANS bucket/quotient boundaries, all BC6H modes, and all BC7 control-code shapes.
  • Cross-compile / lint: x86_64-pc-windows-msvc release check + test --no-run + clippy -D warnings clean.
  • Release micro-benchmark (bcpack_throughput, median of 3, Ryzen 7 5700X):
Format Scalar AVX2 Speedup
BC1 105 ns/blk (76 MB/s) 82 ns/blk (98 MB/s) 1.28×
BC3 206 ns/blk (79 MB/s) 160 ns/blk (100 MB/s) 1.28×
BC4 107 ns/blk (75 MB/s) 85 ns/blk (94 MB/s) 1.26×
BC5 204 ns/blk (78 MB/s) 164 ns/blk (98 MB/s) 1.25×
BC6 366 ns/blk (44 MB/s) 262 ns/blk (61 MB/s) 1.39×
BC7 369 ns/blk (43 MB/s) 271 ns/blk (59 MB/s) 1.36×

(Block packing alone improved 1.8×–2.6×: BC7 115→44 ns, BC6 121→52 ns, BC4 19→8 ns.)

  • In-game A/B/B/A check (VICTORY_BENCH=rt, 4K FSR 4 Quality, scripted moving Prison 3m x 4, RX 9070 XT): 0 crashes, +2.39% avg FPS (25.75 vs 25.15 fps, 1% low 11.85 vs 12.10).
**@⁠lucamzanon** on GitHub · 2026-12-03 20:13 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/363) `lucamzanon:xmem/1-bcpack-avx2` → `main` · +2152 −43 · 1 files · 3 commits · merged 2026-12-05 17:27 UTC ## Summary Accelerates CPU BCPack texture decompression (`crates/victory-formats/src/bcpack.rs`) with an AVX2 branchless lane scheduler and SSE4.1/SSSE3 block packing for BC1–BC7, with automatic runtime/target fallback to the original scalar implementation. ### Changes - **AVX2 lane schedule (70–80% of decode time):** processes two lane vectors per round branchlessly (step, byte order, stalls, delivery) with identical semantics to the scalar schedule (including truncated/garbage streams). - **SSE4.1/SSSE3 block packing:** - BC1–BC5: 8-symbol residual unpack and 2/3-bit index packing via `pmaddubsw`/`pmaddwd`. - BC6H/BC7: vectorized index residuals + layout-driven block writes replacing per-field bit-writer calls. - **Scalar fallback:** original scalar path retained for non-x86_64 or CPUs without AVX2/SSE4.1/SSSE3. ## Verification & Benchmarks (Ryzen 7 5700X / RX 9070 XT, Linux RADV) - **Bit-exactness:** 19/19 `bcpack` unit tests pass, including 7 new scalar-vs-SIMD differential tests covering all layouts, truncated/garbage streams, rANS bucket/quotient boundaries, all BC6H modes, and all BC7 control-code shapes. - **Cross-compile / lint:** `x86_64-pc-windows-msvc` release check + `test --no-run` + `clippy -D warnings` clean. - **Release micro-benchmark (`bcpack_throughput`, median of 3, Ryzen 7 5700X):** | Format | Scalar | AVX2 | Speedup | |---|---:|---:|---:| | BC1 | 105 ns/blk (76 MB/s) | **82 ns/blk (98 MB/s)** | **1.28×** | | BC3 | 206 ns/blk (79 MB/s) | **160 ns/blk (100 MB/s)** | **1.28×** | | BC4 | 107 ns/blk (75 MB/s) | **85 ns/blk (94 MB/s)** | **1.26×** | | BC5 | 204 ns/blk (78 MB/s) | **164 ns/blk (98 MB/s)** | **1.25×** | | BC6 | 366 ns/blk (44 MB/s) | **262 ns/blk (61 MB/s)** | **1.39×** | | BC7 | 369 ns/blk (43 MB/s) | **271 ns/blk (59 MB/s)** | **1.36×** | *(Block packing alone improved 1.8×–2.6×: BC7 115→44 ns, BC6 121→52 ns, BC4 19→8 ns.)* - **In-game A/B/B/A check** (`VICTORY_BENCH=rt`, 4K FSR 4 Quality, scripted moving Prison `3m x 4`, RX 9070 XT): **0 crashes**, **+2.39% avg FPS** (`25.75` vs `25.15 fps`, 1% low `11.85` vs `12.10`).
The lane schedule was one lane step at a time, with a data-dependent branch
on every lane and every byte; it is most of the decode. It now runs on two
vectors of lanes (eight, and eight or four more): each round every lane's
step, the schedule's byte order and its stalls and deliveries are computed
for a whole vector at once, branchlessly, with the same semantics as the
scalar schedule. The bucket of a slot is the greatest start not above it,
found with a max over the model's buckets per lane, and the quotient with a
64-bit product of a 32-bit reciprocal, exactly as the scalar step does. The
lanes in a header phase step one by one as before (once a fragment).

The block packing follows the same route: BC1-BC5 add the residuals eight
symbols at a time and pack the 2- and 3-bit codes with multiply-adds; BC6H
and BC7 take their index residuals in a vector and write the blocks from a
per-control-code (per mode) layout instead of a field-by-field bit writer.

Every path has the scalar one beside it, chosen when the CPU has no AVX2,
and the tests compare the two on every layout, cut and garbage streams,
every model at its bucket and quotient edges, every BC7 control code shape
and every BC6H mode. A release benchmark (`bcpack_throughput`, ignored)
prints both throughputs: on a Ryzen 7 5700X, 1.25-1.40x per format (the
packing 1.8-2.6x, the schedule 1.2x).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-03 20:18:33 +00:00
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-03 20:59 UTC · original

Tested e51f77f natively on Windows 11 with the x86_64-pc-windows-msvc target and Rust 1.98.1. Runtime detection found avx2, sse4.1 and ssse3, so the fast paths really ran here.

cargo test -p victory-formats passes with 59 passed and 7 ignored, in debug and release (main gives 52 and 7). The 7 new tests passed 20 runs out of 20. With accelerated() forced to false locally the non-avx2 tests also pass on the scalar path.

I also ran a local differential harness that I didn't commit. It calls decode_with with fast false and true on the same input and compares the whole dst, prefilled with a sentinel, plus the exact Error value, with panics caught. I used the shipped tables, the synthetic test tables, and a mix of the synthetic models with the shipped ranks, control words and dictionaries, so stream() can build valid streams that use real BC7 control codes (picked at random from the 285 nonzero ones) and real dictionary entries. Random and biased byte streams for bc 0 to 8, with many src sizes from 0 to about 20000 and dst sizes including 0, 1, 7, 12 and 17, gave 12.6M cases. Valid streams for BC1/3/4/5/6H/7 decoded whole, with more or fewer blocks, cut at every length, padded and with flipped bits gave another 10.2M. index_residuals against the scalar loop with full range i16 references and ref_bits 0 to 16 gave 81.6M. Everything matched bit for bit and neither path panicked. A smaller debug run with overflow checks, about 2.6M cases, matched too.

Reading the code I didn't find a problem. Detection is cached in a OnceLock and the unsafe calls only happen behind fast. The 8 byte input load near the end of src goes through a zero padded copy (bcpack.rs:1218), and the other wide loads stay inside the 16 lane state arrays and the record stride.

Two small things. cargo clippy -p victory-formats --all-targets gives two needless_borrow warnings at bcpack.rs:3633-3634 (&t where t is already &Tables), main is clean. And the three tests inside mod x86 (bcpack.rs:1997, 2037, 2092) call the SSE functions without the accelerated() check the avx2_* tests have. That won't matter on any real x86_64 CI box, it's just inconsistent.

bcpack_throughput in release, scalar vs SIMD, gave BC1 1.43x, BC3 1.45x, BC4 1.40x, BC5 1.36x, BC6H 1.75x and BC7 1.74x. That's a bit higher than your Linux numbers, though it's a single run so I wouldn't read much into the exact figures.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-03 20:59 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/363#issuecomment-7392113672) Tested e51f77f natively on Windows 11 with the x86_64-pc-windows-msvc target and Rust 1.98.1. Runtime detection found avx2, sse4.1 and ssse3, so the fast paths really ran here. cargo test -p victory-formats passes with 59 passed and 7 ignored, in debug and release (main gives 52 and 7). The 7 new tests passed 20 runs out of 20. With accelerated() forced to false locally the non-avx2 tests also pass on the scalar path. I also ran a local differential harness that I didn't commit. It calls decode_with with fast false and true on the same input and compares the whole dst, prefilled with a sentinel, plus the exact Error value, with panics caught. I used the shipped tables, the synthetic test tables, and a mix of the synthetic models with the shipped ranks, control words and dictionaries, so stream() can build valid streams that use real BC7 control codes (picked at random from the 285 nonzero ones) and real dictionary entries. Random and biased byte streams for bc 0 to 8, with many src sizes from 0 to about 20000 and dst sizes including 0, 1, 7, 12 and 17, gave 12.6M cases. Valid streams for BC1/3/4/5/6H/7 decoded whole, with more or fewer blocks, cut at every length, padded and with flipped bits gave another 10.2M. index_residuals against the scalar loop with full range i16 references and ref_bits 0 to 16 gave 81.6M. Everything matched bit for bit and neither path panicked. A smaller debug run with overflow checks, about 2.6M cases, matched too. Reading the code I didn't find a problem. Detection is cached in a OnceLock and the unsafe calls only happen behind fast. The 8 byte input load near the end of src goes through a zero padded copy (bcpack.rs:1218), and the other wide loads stay inside the 16 lane state arrays and the record stride. Two small things. cargo clippy -p victory-formats --all-targets gives two needless_borrow warnings at bcpack.rs:3633-3634 (&t where t is already &Tables), main is clean. And the three tests inside mod x86 (bcpack.rs:1997, 2037, 2092) call the SSE functions without the accelerated() check the avx2_* tests have. That won't matter on any real x86_64 CI box, it's just inconsistent. bcpack_throughput in release, scalar vs SIMD, gave BC1 1.43x, BC3 1.45x, BC4 1.40x, BC5 1.36x, BC6H 1.75x and BC7 1.74x. That's a bit higher than your Linux numbers, though it's a single run so I wouldn't read much into the exact figures. AI-assisted: yes
Author
Owner

@⁠lucamzanon on GitHub · 2026-12-04 03:44 UTC · original

Thanks @⁠Kal0s-design for the thorough 104M-case differential check! Fixed both nits (removed the two needless_borrows in avx2_blocks_match_the_scalar_blocks and added if !accelerated() { return; } guards to the three mod x86 unit tests), re-verified tests + clippy -D warnings.

**@⁠lucamzanon** on GitHub · 2026-12-04 03:44 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/363#issuecomment-7400393999) Thanks @⁠Kal0s-design for the thorough 104M-case differential check! Fixed both nits (removed the two `needless_borrow`s in `avx2_blocks_match_the_scalar_blocks` and added `if !accelerated() { return; }` guards to the three `mod x86` unit tests), re-verified tests + `clippy -D warnings`.
Author
Owner

@⁠Luxuride on GitHub · 2026-12-04 12:20 UTC · original

Strongest test suite in this whole backlog pass, and the change is contained. Worth taking, with one addition.

What I verified:

  • avx2_lanes_match_the_scalar_schedule compares decoded symbols and error parity against the scalar schedule across all 6 layouts × block counts 1/2/17/300/1000, whole and cut at 0 / 1 / half / len−1, plus 150 garbage streams; avx2_decode_step_matches_the_scalar_step walks all 128 models of the builtin and test tables at every bucket/quotient edge. That is the shape of test that catches a lane schedule bug.
  • decode_with is the parse path for both the accelerated and scalar variants, so a bad SIMD stream fails exactly as a bad scalar one does. No defect found.
  • One thing no CI will catch: both tests start with if !accelerated() { return; }, so on a box without AVX2 they pass without testing anything. They need one manual run on an AVX2 machine — worth saying in the PR body that you did that.

The addition: there is no way to force the scalar path. accelerated() is the only gate, while bc7 has VICTORY_BC7_DICTIONARY for exactly this. A VICTORY_BCPACK_SCALAR=1 is cheap insurance and matches how the rest of the crate gets debugged — a misreported CPU feature bit otherwise becomes undebuggable without a rebuild.

Small honesty note: your +2.39% average came with the 1% low slightly worse (11.85 vs 12.10) in that one 4K scripted run. One more run would let you quote both numbers with a straight face.

173 commits behind, but crates/victory-formats/src/bcpack.rs is untouched on main since your base, so the rebase is mechanical.

**@⁠Luxuride** on GitHub · 2026-12-04 12:20 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/363#issuecomment-7411293539) Strongest test suite in this whole backlog pass, and the change is contained. Worth taking, with one addition. What I verified: - `avx2_lanes_match_the_scalar_schedule` compares decoded symbols and error parity against the scalar schedule across all 6 layouts × block counts 1/2/17/300/1000, whole and cut at 0 / 1 / half / len−1, plus 150 garbage streams; `avx2_decode_step_matches_the_scalar_step` walks all 128 models of the builtin and test tables at every bucket/quotient edge. That is the shape of test that catches a lane schedule bug. - `decode_with` is the parse path for both the accelerated and scalar variants, so a bad SIMD stream fails exactly as a bad scalar one does. No defect found. - One thing no CI will catch: both tests start with `if !accelerated() { return; }`, so on a box without AVX2 they pass without testing anything. They need one manual run on an AVX2 machine — worth saying in the PR body that you did that. The addition: there is no way to force the scalar path. `accelerated()` is the only gate, while `bc7` has `VICTORY_BC7_DICTIONARY` for exactly this. A `VICTORY_BCPACK_SCALAR=1` is cheap insurance and matches how the rest of the crate gets debugged — a misreported CPU feature bit otherwise becomes undebuggable without a rebuild. Small honesty note: your +2.39% average came with the 1% low slightly worse (11.85 vs 12.10) in that one 4K scripted run. One more run would let you quote both numbers with a straight face. 173 commits behind, but `crates/victory-formats/src/bcpack.rs` is untouched on `main` since your base, so the rebase is mechanical.
github-bot force-pushed github-bot/gh-363 from 749c53a87b to 06cd405206 2026-12-04 13:03:31 +00:00 Compare
Author
Owner

@⁠lucamzanon on GitHub · 2026-12-04 12:57 UTC · original

Thanks @⁠Luxuride! Rebased cleanly onto main (0fd7614) and added VICTORY_BCPACK_SCALAR=1 in 06cd405 to force the scalar path at runtime (with unit test bcpack_scalar_switch_parses_1_only).

Also confirming that the AVX2 + SSE4.1/SSSE3 test suite was executed on an AVX2-capable CPU (Ryzen 7 5700X, plus @⁠Kal0s-design's 104.4M-case Win11 MSVC run where runtime detection confirmed avx2/sse4.1/ssse3 active). Re-verified cargo test --locked --lib -p victory-formats bcpack in debug & release (20/20 pass), clippy --all-targets -- -D warnings, and rustfmt --edition 2024 --check.

**@⁠lucamzanon** on GitHub · 2026-12-04 12:57 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/363#issuecomment-7412152216) Thanks @⁠Luxuride! Rebased cleanly onto `main` (`0fd7614`) and added `VICTORY_BCPACK_SCALAR=1` in `06cd405` to force the scalar path at runtime (with unit test `bcpack_scalar_switch_parses_1_only`). Also confirming that the AVX2 + SSE4.1/SSSE3 test suite was executed on an AVX2-capable CPU (Ryzen 7 5700X, plus @⁠Kal0s-design's 104.4M-case Win11 MSVC run where runtime detection confirmed `avx2`/`sse4.1`/`ssse3` active). Re-verified `cargo test --locked --lib -p victory-formats bcpack` in debug & release (20/20 pass), `clippy --all-targets -- -D warnings`, and `rustfmt --edition 2024 --check`.
github manually merged commit f1343781ca into main 2026-12-05 17:31:23 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.