formats: bcpack: AVX2 lane schedule and SSE4.1/SSSE3 block packing #363
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/lobomfz--VIctory-xmem!363
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-363"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@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 UTCSummary
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
pmaddubsw/pmaddwd.Verification & Benchmarks (Ryzen 7 5700X / RX 9070 XT, Linux RADV)
bcpackunit 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.x86_64-pc-windows-msvcrelease check +test --no-run+clippy -D warningsclean.bcpack_throughput, median of 3, Ryzen 7 5700X):(Block packing alone improved 1.8×–2.6×: BC7 115→44 ns, BC6 121→52 ns, BC4 19→8 ns.)
VICTORY_BENCH=rt, 4K FSR 4 Quality, scripted moving Prison3m x 4, RX 9070 XT): 0 crashes, +2.39% avg FPS (25.75vs25.15 fps, 1% low11.85vs12.10).@Kal0s-design on GitHub · 2026-12-03 20:59 UTC · original
Tested
e51f77fnatively 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
@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 inavx2_blocks_match_the_scalar_blocksand addedif !accelerated() { return; }guards to the threemod x86unit tests), re-verified tests +clippy -D warnings.@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_schedulecompares 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_stepwalks 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_withis 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.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, whilebc7hasVICTORY_BC7_DICTIONARYfor exactly this. AVICTORY_BCPACK_SCALAR=1is 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.rsis untouched onmainsince your base, so the rebase is mechanical.749c53a87bto06cd405206@lucamzanon on GitHub · 2026-12-04 12:57 UTC · original
Thanks @Luxuride! Rebased cleanly onto
main(0fd7614) and addedVICTORY_BCPACK_SCALAR=1in06cd405to force the scalar path at runtime (with unit testbcpack_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/ssse3active). Re-verifiedcargo test --locked --lib -p victory-formats bcpackin debug & release (20/20 pass),clippy --all-targets -- -D warnings, andrustfmt --edition 2024 --check.