BCPack: replace bit-packing loops with compile-time LUTs and precomputed masks in victory #2702

Open
github-bot wants to merge 1 commit from github-bot/gh-2702 into main AGit
Owner

@⁠Decodere on GitHub · 2026-11-26 07:14 UTC · original
Decodere:fix-bcpack-victory-lut-and-pack-fastpaths → main · +316 −59 · 1 files · 1 commits · open

What is in it

Replaces per-block bit-spread/deinterleave loops, dynamic mask shifts, slice anchor scans, and bounds-checked slice indexing in crates/xcl/src/hle/bcpack/victory.rs with compile-time lookup tables, precomputed masks, u16 anchor bitmasks, and fixed-size array packing helpers on the CPU BCPack decompression path.

  • Compile-time bit-spread and nibble LUTs: Adds SPREAD4 ([u8; 16]), SPREAD8 ([u16; 256]), and NIBBLES ([[i16; 2]; 256]) with #[inline(always)] interleave, wide_interleave (used in bc6), and nibbles replacing the runtime bit-spread and deinterleave folds.
  • Precomputed Widths masks and active loops in plain: Replaces bit-width arrays on Widths with precomputed ep_mask, ep_len, ix_mask, and ix_len. plain loops only over active endpoint/index counts (0..w.ep_len and 0..w.ix_len, with a fixed 16-byte SIMD fast path when w.ix_len == 16 for BC1/BC4), removing dynamic shifts from every element.
  • Fixed-size block-packing helpers: Narrows quads (&[u8; 4], &mut [u8; 16]), bc1_color (&[u8; 6], &[u8; 16], &mut [u8; 8], writing a single u64::to_le_bytes() store), and bc4_alpha (&[u8; 16], &mut [u8; 8]), uses as_chunks_mut::<2>() in pairs, and marks zz, quads, pairs, bc1_color, bc4_alpha, add_index, and Bits::put #[inline(always)] (with a 64-bit mask before widening to u128 in Bits::put).
  • Bitmask anchor checks in BC7 and BC6H: Replaces anchors.contains(...) slice scans in bc7_pack and per-pixel anchor branches in bc6 with a precomputed u16 anchor_mask.
  • Unit tests: Adds two pure-CPU unit tests (lut_helpers_match_scalar_reference_exhaustively and all_bc_fixtures_and_packers_match_baseline) exhaustively verifying interleave, wide_interleave, nibbles, Widths masks/lengths, BC7/BC6H anchor_mask, Bits::put, all 12 valid shipped tests/data/bcpack fixtures against unmodified origin/main FNV-1a hashes (plus bc7-700-2 error parity), bc1_color/bc4_alpha, and all 14 BC6H modes + all 8 BC7 modes.

Part of #1833

Tested

  • Baseline capture on origin/main (b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7): Recorded exact FNV-1a hashes for all 12 valid shipped tests/data/bcpack fixtures (and bc7-700-2 error parity) plus baseline end-to-end release bcpack::decode timings (bc1-8192-3: 701.3 us, bc7-8192-3: 1470.5 us, bc1-700-2: 185.5 us).
  • Post-fix end-to-end release bcpack::decode timings (best of 5, including untouched rANS stage):
    • bc1-8192-3 (64 KiB, 8192 blocks): 701.3 -> 650.9 us (1.08x)
    • bc7-8192-3 (64 KiB, 4096 blocks): 1470.5 -> 1147.2 us (1.28x)
    • bc1-700-2 (700 blocks): 185.5 -> 103.5 us (1.79x)
  • Standalone rustc -O per-primitive micro-benchmarks (100% exhaustive scalar parity verified):
    • interleave: 3.89 -> 3.14 ns/op (1.24x)
    • nibbles: 5.45 -> 2.78 ns/op (1.96x)
    • wide_interleave: 12.34 -> 3.25 ns/op (3.79x)
    • bc7_indices (anchor_mask): 84.95 -> 55.70 ns/op (1.53x)
    • bc1_color: 18.00 -> 12.16 ns/op (1.48x)
    • bc4_alpha: 14.64 -> 11.47 ns/op (1.28x)
    • plain residual mask loop: BC1 10.46 -> 4.22 ns/op (2.48x), BC3 12.49 -> 6.69 ns/op (1.87x)
  • Debug WARP (XCL_ADAPTER=warp, XCL_WARP=1): cargo test -p xcl --lib -- hle::bcpack:: --nocapture -> 8 passed; 0 failed; 0 ignored (0 skipped, 0.04s).
  • Release WARP (XCL_ADAPTER=warp, XCL_WARP=1): cargo test --release -p xcl --lib -- hle::bcpack:: --nocapture -> 8 passed; 0 failed; 0 ignored (0 skipped, 0.01s).
  • Clippy: cargo clippy --all-targets -> 0 new warnings in crates/xcl/src/hle/bcpack/victory.rs.
  • Release RTX 4060: cargo test --release -p xcl --lib -- hle::bcpack:: --nocapture -> 8 passed; 0 failed; 0 ignored (0 skipped, 0.01s).
  • Full release workspace on RTX 4060: cargo test --release --workspace --no-fail-fast -- --test-threads=4 --nocapture -> 37 test binaries, 1437 passed; 0 failed; 47 ignored (29 pre-existing host-gated SKIPPED lines: 21 wave64-only tests on a 32-lane GPU, 3 per-lane wave32 tests refused by the driver, 3 D3D12 debug layer tests without d3d12SDKLayers.dll, 1 no heap windows, 1 cannot disable the driver cache).

Regression run

Baseline (origin/main):

xcl-smoke v1
commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1571 (PresentX 1571)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 13975 MB

Branch (HEAD):

xcl-smoke v1
commit: d4baa281541c058cda6cd4553d5c0f9f26249918
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1443 (PresentX 1443)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14015 MB

Not tested

  • External XCL_SAMPLE_BCPACK sample corpus (tests/bcpack_parity.rs skips when the env var is unset).
  • Wave64-only GPU tests and D3D12 debug-layer tests (unavailable on this 32-lane RTX 4060 host without d3d12SDKLayers.dll).

AI use

Implemented, unit-tested, benchmarked, and verified with assistance from Google Antigravity (Gemini 4 Neon); every change and test result was reviewed and executed locally.

**@⁠Decodere** on GitHub · 2026-11-26 07:14 UTC · [original](https://github.com/xcl-dev/xcl/pull/2702) `Decodere:fix-bcpack-victory-lut-and-pack-fastpaths` → `main` · +316 −59 · 1 files · 1 commits · open **What is in it** Replaces per-block bit-spread/deinterleave loops, dynamic mask shifts, slice anchor scans, and bounds-checked slice indexing in `crates/xcl/src/hle/bcpack/victory.rs` with compile-time lookup tables, precomputed masks, `u16` anchor bitmasks, and fixed-size array packing helpers on the CPU BCPack decompression path. - **Compile-time bit-spread and nibble LUTs:** Adds `SPREAD4` (`[u8; 16]`), `SPREAD8` (`[u16; 256]`), and `NIBBLES` (`[[i16; 2]; 256]`) with `#[inline(always)]` `interleave`, `wide_interleave` (used in `bc6`), and `nibbles` replacing the runtime bit-spread and deinterleave folds. - **Precomputed `Widths` masks and active loops in `plain`:** Replaces bit-width arrays on `Widths` with precomputed `ep_mask`, `ep_len`, `ix_mask`, and `ix_len`. `plain` loops only over active endpoint/index counts (`0..w.ep_len` and `0..w.ix_len`, with a fixed 16-byte SIMD fast path when `w.ix_len == 16` for BC1/BC4), removing dynamic shifts from every element. - **Fixed-size block-packing helpers:** Narrows `quads` (`&[u8; 4]`, `&mut [u8; 16]`), `bc1_color` (`&[u8; 6]`, `&[u8; 16]`, `&mut [u8; 8]`, writing a single `u64::to_le_bytes()` store), and `bc4_alpha` (`&[u8; 16]`, `&mut [u8; 8]`), uses `as_chunks_mut::<2>()` in `pairs`, and marks `zz`, `quads`, `pairs`, `bc1_color`, `bc4_alpha`, `add_index`, and `Bits::put` `#[inline(always)]` (with a 64-bit mask before widening to `u128` in `Bits::put`). - **Bitmask anchor checks in BC7 and BC6H:** Replaces `anchors.contains(...)` slice scans in `bc7_pack` and per-pixel anchor branches in `bc6` with a precomputed `u16` `anchor_mask`. - **Unit tests:** Adds two pure-CPU unit tests (`lut_helpers_match_scalar_reference_exhaustively` and `all_bc_fixtures_and_packers_match_baseline`) exhaustively verifying `interleave`, `wide_interleave`, `nibbles`, `Widths` masks/lengths, BC7/BC6H `anchor_mask`, `Bits::put`, all 12 valid shipped `tests/data/bcpack` fixtures against unmodified `origin/main` FNV-1a hashes (plus `bc7-700-2` error parity), `bc1_color`/`bc4_alpha`, and all 14 BC6H modes + all 8 BC7 modes. Part of #1833 **Tested** - **Baseline capture on `origin/main` (`b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7`):** Recorded exact FNV-1a hashes for all 12 valid shipped `tests/data/bcpack` fixtures (and `bc7-700-2` error parity) plus baseline end-to-end release `bcpack::decode` timings (`bc1-8192-3`: `701.3 us`, `bc7-8192-3`: `1470.5 us`, `bc1-700-2`: `185.5 us`). - **Post-fix end-to-end release `bcpack::decode` timings (best of 5, including untouched rANS stage):** - `bc1-8192-3` (64 KiB, 8192 blocks): `701.3 -> 650.9 us` (`1.08x`) - `bc7-8192-3` (64 KiB, 4096 blocks): `1470.5 -> 1147.2 us` (`1.28x`) - `bc1-700-2` (700 blocks): `185.5 -> 103.5 us` (`1.79x`) - **Standalone `rustc -O` per-primitive micro-benchmarks (100% exhaustive scalar parity verified):** - `interleave`: `3.89 -> 3.14 ns/op` (`1.24x`) - `nibbles`: `5.45 -> 2.78 ns/op` (`1.96x`) - `wide_interleave`: `12.34 -> 3.25 ns/op` (`3.79x`) - `bc7_indices` (`anchor_mask`): `84.95 -> 55.70 ns/op` (`1.53x`) - `bc1_color`: `18.00 -> 12.16 ns/op` (`1.48x`) - `bc4_alpha`: `14.64 -> 11.47 ns/op` (`1.28x`) - `plain` residual mask loop: BC1 `10.46 -> 4.22 ns/op` (`2.48x`), BC3 `12.49 -> 6.69 ns/op` (`1.87x`) - **Debug WARP (`XCL_ADAPTER=warp`, `XCL_WARP=1`):** `cargo test -p xcl --lib -- hle::bcpack:: --nocapture` -> `8 passed; 0 failed; 0 ignored` (`0` skipped, `0.04s`). - **Release WARP (`XCL_ADAPTER=warp`, `XCL_WARP=1`):** `cargo test --release -p xcl --lib -- hle::bcpack:: --nocapture` -> `8 passed; 0 failed; 0 ignored` (`0` skipped, `0.01s`). - **Clippy:** `cargo clippy --all-targets` -> `0` new warnings in `crates/xcl/src/hle/bcpack/victory.rs`. - **Release RTX 4060:** `cargo test --release -p xcl --lib -- hle::bcpack:: --nocapture` -> `8 passed; 0 failed; 0 ignored` (`0` skipped, `0.01s`). - **Full release workspace on RTX 4060:** `cargo test --release --workspace --no-fail-fast -- --test-threads=4 --nocapture` -> `37` test binaries, `1437 passed; 0 failed; 47 ignored` (`29` pre-existing host-gated `SKIPPED` lines: `21` wave64-only tests on a 32-lane GPU, `3` per-lane wave32 tests refused by the driver, `3` D3D12 debug layer tests without `d3d12SDKLayers.dll`, `1` `no heap windows`, `1` `cannot disable the driver cache`). **Regression run** Baseline (`origin/main`): ``` xcl-smoke v1 commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7 gpu: NVIDIA GeForce RTX 4060 title: 29640639 seconds: 60 exit: 0x0 frames shown: 1571 (PresentX 1571) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 13975 MB ``` Branch (`HEAD`): ``` xcl-smoke v1 commit: d4baa281541c058cda6cd4553d5c0f9f26249918 gpu: NVIDIA GeForce RTX 4060 title: 29640639 seconds: 60 exit: 0x0 frames shown: 1443 (PresentX 1443) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14015 MB ``` **Not tested** - External `XCL_SAMPLE_BCPACK` sample corpus (`tests/bcpack_parity.rs` skips when the env var is unset). - Wave64-only GPU tests and D3D12 debug-layer tests (unavailable on this 32-lane RTX 4060 host without `d3d12SDKLayers.dll`). **AI use** Implemented, unit-tested, benchmarked, and verified with assistance from Google Antigravity (Gemini 4 Neon); every change and test result was reviewed and executed locally.
Per-block BCPack decoding on the DirectStorage texture decompression path repeated inner bit-spread/deinterleave loops (`interleave`, `nibbles`, `bc6` `wide`), recomputed dynamic bit masks in `plain`, scanned anchor slices on every pixel in `bc7_pack`, and indexed unsized slices inside the 16-pixel `bc1_color` and `bc4_alpha` packing loops.

- Add compile-time `SPREAD4`, `SPREAD8`, and `NIBBLES` lookup tables with `#[inline(always)]` `interleave`, `wide_interleave`, and `nibbles` helpers replacing the per-call bit-spread and deinterleave loops.
- Precompute `ep_mask`, `ep_len`, `ix_mask`, and `ix_len` in `Widths` and use exact active endpoint/index masking loops (with a fixed 16-byte SIMD fast path for BC1/BC4) in `plain`, removing 40 dynamic shifts per block.
- Narrow `quads`, `bc1_color`, and `bc4_alpha` to fixed-size array references (`[u8; 4]`, `[u8; 6]`, `[u8; 16]`, `[u8; 8]`), use `as_chunks_mut::<2>()` in `pairs`, write `bc1_color` in a single `u64::to_le_bytes()` store, and inline `zz`, `add_index`, and `Bits::put` (using a 64-bit mask before widening to `u128`).
- Replace `anchors.contains(...)` slice scans in `bc7_pack` and per-pixel anchor branches in `bc6` with a precomputed `u16` `anchor_mask` bitmask.
- Add pure-CPU unit tests exhaustively verifying `interleave`, `wide_interleave`, `nibbles`, `Widths` masks/lengths, BC7/BC6H `anchor_mask`, `Bits::put`, all 12 valid shipped `tests/data/bcpack` fixtures against unmodified `origin/main` hashes, `bc1_color`/`bc4_alpha`, and all 14 BC6H modes + all 8 BC7 modes.

Part of #1833
Assisted-by: Google Antigravity (Gemini 4 Neon)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 07:16:09 +00:00
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2702/head:github-bot/gh-2702'
git switch 'github-bot/gh-2702'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2702'
git switch 'github-bot/gh-2702'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2702'
git switch 'github-bot/gh-2702'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2702'
git switch 'main'
git merge --squash 'github-bot/gh-2702'
git switch 'main'
git merge --ff-only 'github-bot/gh-2702'
git switch 'main'
git merge 'github-bot/gh-2702'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.