Formats: speed up BC7 BCPack decoding by 27% (and BC1/BC4 by 14-15%) #2735

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

@⁠brainbudt on GitHub · 2026-11-25 19:22 UTC · original
brainbudt:perf/bcpack-bc7 → main · +304 −151 · 1 files · 1 commits · open

BC7 decodes 32 symbols per block (vs. 12 for BC1/BC4) and previously ran ~3x slower per block (70.2 MB/s, 228.1 ns/block), packing each pixel index through a separate 128-bit writer operation.

What changed

  • Precompute the rANS symbol for single-symbol 256-slot windows, and use a boundary bitmask + popcount for mixed windows (lookup table grows from ~51 KiB to ~439 KiB, built once per process).
  • Rebuild BC7 endpoints row by row, pack each index set into one word, and strip omitted anchor bits before writing.
  • Share the paired index residual lookup between BC6H and BC7.
  • Pure safe Rust, no new dependencies, public API unchanged.

Benchmark (bcpack_throughput, median of 7 release runs on Apple M4, 16,384 blocks × 40)

Format Before (MB/s) After (MB/s) Before (ns/block) After (ns/block) Gain
BC1 111.6 126.8 71.7 63.1 +13.6%
BC4 115.9 133.8 69.0 59.8 +15.4%
BC7 70.2 89.2 228.1 179.3 +27.1%

Verification

  • Byte-for-byte equivalence verified against the original decoder across all 285 BC7 control codes, all 14 BC6H modes, multi-fragment streams, dictionary references, history wrap-around, and 2,304 truncated/mutated error cases.
  • cargo test --release -p victory-formats passed, cargo clippy -p victory-formats --all-targets -- -D warnings passed, and cargo check for x86_64-pc-windows-msvc passed.

Created with gpt-6.2-astra in Codex (orchestrated with Gemini 4 Neon in T3 Code).

**@⁠brainbudt** on GitHub · 2026-11-25 19:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/2735) `brainbudt:perf/bcpack-bc7` → `main` · +304 −151 · 1 files · 1 commits · open BC7 decodes 32 symbols per block (vs. 12 for BC1/BC4) and previously ran ~3x slower per block (`70.2 MB/s`, `228.1 ns/block`), packing each pixel index through a separate 128-bit writer operation. ### What changed - Precompute the rANS symbol for single-symbol 256-slot windows, and use a boundary bitmask + popcount for mixed windows (lookup table grows from ~51 KiB to ~439 KiB, built once per process). - Rebuild BC7 endpoints row by row, pack each index set into one word, and strip omitted anchor bits before writing. - Share the paired index residual lookup between BC6H and BC7. - Pure safe Rust, no new dependencies, public API unchanged. ### Benchmark (`bcpack_throughput`, median of 7 release runs on Apple M4, 16,384 blocks × 40) | Format | Before (MB/s) | After (MB/s) | Before (ns/block) | After (ns/block) | Gain | |---|---:|---:|---:|---:|---:| | BC1 | 111.6 | 126.8 | 71.7 | 63.1 | +13.6% | | BC4 | 115.9 | 133.8 | 69.0 | 59.8 | +15.4% | | BC7 | 70.2 | 89.2 | 228.1 | 179.3 | +27.1% | ### Verification - Byte-for-byte equivalence verified against the original decoder across all 285 BC7 control codes, all 14 BC6H modes, multi-fragment streams, dictionary references, history wrap-around, and 2,304 truncated/mutated error cases. - `cargo test --release -p victory-formats` passed, `cargo clippy -p victory-formats --all-targets -- -D warnings` passed, and `cargo check` for `x86_64-pc-windows-msvc` passed. Created with gpt-6.2-astra in Codex (orchestrated with Gemini 4 Neon in T3 Code).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:13:03 +00:00
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 20:08 UTC · original

Read the Model/MixedWindow construction: the transition-bitmask + rank approach replaces the per-symbol window scan, and the empty-bucket / does-not-tile-65536 validation happens up front before any symbols are emitted - good. What I can't verify by reading is bit-exactness vs the old decoder across all BC7 modes; is there a differential test or corpus run backing the 27% figure? If the existing bcpack tests decode the same fixtures through both paths, that would cover it.

**@⁠kotekompotek** on GitHub · 2026-11-25 20:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/2735#issuecomment-7173247801) Read the Model/MixedWindow construction: the transition-bitmask + rank approach replaces the per-symbol window scan, and the empty-bucket / does-not-tile-65536 validation happens up front before any symbols are emitted - good. What I can't verify by reading is bit-exactness vs the old decoder across all BC7 modes; is there a differential test or corpus run backing the 27% figure? If the existing bcpack tests decode the same fixtures through both paths, that would cover it.
Author
Owner

@⁠rezioliver-collab on GitHub · 2026-11-25 20:08 UTC · original

On an Apple M1, this PR reduced median warm BC7 decode time at all four sizes. Including Tables::builtin() in the first request, the PR was faster only at 16,384 blocks among the sizes tested. At that size, first-request time fell from 12.17 ms to 10.22 ms, and the PR was faster in all 40 pairs.

Medians in microseconds (µs). Each cell shows main (13f91ca) → this PR (62911d14).

BC7 blocks Table construction First decode Table construction + first decode Warm decode
1 99.21 → 364.23 6.79 → 6.83 107.17 → 373.02 1.60 → 1.53
16 99.88 → 367.23 14.90 → 12.31 115.00 → 380.38 6.48 → 5.12
256 99.85 → 368.29 114.06 → 98.71 214.48 → 467.79 89.61 → 71.72
16,384 99.21 → 363.02 12,073 → 9,847 12,172 → 10,218 12,027 → 9,743

We built both revisions in release mode with Rust 1.98.1 on macOS 14.7. For each size, both builds used the same pre-generated synthetic BC7 input. We ran 40 pairs, alternating which build ran first. Each process built the shipped tables once and timed its first decode, then ran three warm-ups before timing repeated decodes with those tables. The combined column is the median of each process’s total. Every timed output matched the main reference byte for byte, with comparisons outside the timed region.

**@⁠rezioliver-collab** on GitHub · 2026-11-25 20:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/2735#issuecomment-7173255355) On an Apple M1, this PR reduced median warm BC7 decode time at all four sizes. Including `Tables::builtin()` in the first request, the PR was faster only at **16,384 blocks** among the sizes tested. At that size, first-request time fell from **12.17 ms to 10.22 ms**, and the PR was faster in all 40 pairs. Medians in microseconds (µs). Each cell shows `main` (`13f91ca`) → this PR (`62911d14`). | BC7 blocks | Table construction | First decode | Table construction + first decode | Warm decode | |---:|---:|---:|---:|---:| | 1 | 99.21 → 364.23 | 6.79 → 6.83 | 107.17 → 373.02 | 1.60 → 1.53 | | 16 | 99.88 → 367.23 | 14.90 → 12.31 | 115.00 → 380.38 | 6.48 → 5.12 | | 256 | 99.85 → 368.29 | 114.06 → 98.71 | 214.48 → 467.79 | 89.61 → 71.72 | | 16,384 | 99.21 → 363.02 | 12,073 → 9,847 | 12,172 → 10,218 | 12,027 → 9,743 | We built both revisions in release mode with Rust 1.98.1 on macOS 14.7. For each size, both builds used the same pre-generated synthetic BC7 input. We ran 40 pairs, alternating which build ran first. Each process built the shipped tables once and timed its first decode, then ran three warm-ups before timing repeated decodes with those tables. The combined column is the median of each process’s total. **Every timed output matched the `main` reference byte for byte**, with comparisons outside the timed region.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 20:09 UTC · original

Reviewed and benchmarked 62911d14 on Windows 11 against main (13f91ca).

Code review

  • Zero unsafe, clean bit-exact equivalence across all fast paths:
    • Model rANS 256×256-slot windows (Window::Single / Window::Mixed(u8) + [u64; 4] transition bitmap): at most 256 symbols means at most 255 transitions, so mixed.len() as u8 and value: u8 cannot overflow; 63 - (slot & 63) stays in 0..=63, and wrapping_add/sub preserves the exact 32-bit modular behavior even on slot == 0 malformed inputs.
    • Lane::step: (x >> 16) * u32::from(sym.freq) stays below u32::MAX, matching the prior modular step.
    • nibbles 256-entry PAIRS table, bc7 shifted endpoint accumulation, and bc7_pack's pack_indices::<BITS> + descending-order anchor shorten (max(a,b) -> min(a,b) -> 0) preserve exact bit layout.

Verification & same-machine --release benchmark (rustc 1.98.1 MSVC)

  • cargo test -p victory-formats --locked: 39 passed, 0 failed, 5 ignored (including fragmented_streams_match_original_decoder FNV-1a golden hash + truncation safety and invalid_modes_stop_after_the_last_valid_block).
  • cargo clippy -p victory-formats --all-targets --locked -- -D warnings: 0 warnings; cargo xtask policy: 184 files ok; clean git merge-tree against 13f91ca and #2736.
  • bcpack_throughput (--release, same machine vs 13f91ca):
    • BC1: 110.5 -> 136.0 MB out/s (72.4 -> 58.8 ns/block, +23.1% / 1.23x)
    • BC4: 114.6 -> 133.2 MB out/s (69.8 -> 60.0 ns/block, +16.2% / 1.16x)
    • BC7: 67.4 -> 88.0 MB out/s (237.6 -> 181.9 ns/block, +30.6% / 1.31x)
**@⁠fuzeofficlal** on GitHub · 2026-11-25 20:09 UTC · [original](https://github.com/Marvisak/VIctory/pull/2735#issuecomment-7173278763) Reviewed and benchmarked `62911d14` on Windows 11 against `main` (`13f91ca`). ### Code review - Zero `unsafe`, clean bit-exact equivalence across all fast paths: - `Model` rANS 256×256-slot windows (`Window::Single` / `Window::Mixed(u8)` + `[u64; 4]` transition bitmap): at most 256 symbols means at most 255 transitions, so `mixed.len() as u8` and `value: u8` cannot overflow; `63 - (slot & 63)` stays in `0..=63`, and `wrapping_add/sub` preserves the exact 32-bit modular behavior even on `slot == 0` malformed inputs. - `Lane::step`: `(x >> 16) * u32::from(sym.freq)` stays below `u32::MAX`, matching the prior modular step. - `nibbles` 256-entry `PAIRS` table, `bc7` shifted endpoint accumulation, and `bc7_pack`'s `pack_indices::<BITS>` + descending-order anchor `shorten` (`max(a,b) -> min(a,b) -> 0`) preserve exact bit layout. ### Verification & same-machine `--release` benchmark (`rustc 1.98.1` MSVC) - `cargo test -p victory-formats --locked`: **39 passed, 0 failed, 5 ignored** (including `fragmented_streams_match_original_decoder` FNV-1a golden hash + truncation safety and `invalid_modes_stop_after_the_last_valid_block`). - `cargo clippy -p victory-formats --all-targets --locked -- -D warnings`: **0 warnings**; `cargo xtask policy`: **184 files ok**; clean `git merge-tree` against `13f91ca` and `#2736`. - `bcpack_throughput` (`--release`, same machine vs `13f91ca`): - **BC1**: `110.5` -> **`136.0 MB out/s`** (`72.4` -> `58.8 ns/block`, **+23.1% / 1.23x**) - **BC4**: `114.6` -> **`133.2 MB out/s`** (`69.8` -> `60.0 ns/block`, **+16.2% / 1.16x**) - **BC7**: `67.4` -> **`88.0 MB out/s`** (`237.6` -> `181.9 ns/block`, **+30.6% / 1.31x**)
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 23:22 UTC · original

Automated triage for the maintainers.

This replaces the bucket scan plus reciprocal division in the BCPack rANS step (Lane::step, crates/victory-formats/src/bcpack.rs) with a per-model Model table (precomputed symbol for single-symbol 256-slot windows, transition bitmask + popcount for mixed ones), and speeds up BC7 endpoint rebuilding and index packing in bc7 / bc7_pack. By reading I checked that the new symbol ranges give the same next state as the old (sym, f, rem) computation (including symbol 0's slot-0 wrap), that (base >> shift) + residual) << shift equals the old masked add, and that pack_indices + shorten drops exactly the anchor high bits the old per-index put dropped (Writer::put is LSB-first and masks); payload_step_matches_the_reference_decode still checks every slot of every model against the reference decode, now including narrow tables that force mixed windows. Table construction becomes slower (one-time, about 0.36 ms per a commenter's measurement). CI (check) passes.

This is a recommendation for a maintainer to look, not an approval.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 23:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/2735#issuecomment-7177462215) <!-- victory-triage v1 head=62911d14bbeabf35ead3550f3ad2359abc65b9e6 verdict=merge-candidate at=2026-11-25T23:21:53Z --> Automated triage for the maintainers. This replaces the bucket scan plus reciprocal division in the BCPack rANS step (`Lane::step`, `crates/victory-formats/src/bcpack.rs`) with a per-model `Model` table (precomputed symbol for single-symbol 256-slot windows, transition bitmask + popcount for mixed ones), and speeds up BC7 endpoint rebuilding and index packing in `bc7` / `bc7_pack`. By reading I checked that the new symbol ranges give the same next state as the old `(sym, f, rem)` computation (including symbol 0's slot-0 wrap), that `(base >> shift) + residual) << shift` equals the old masked add, and that `pack_indices` + `shorten` drops exactly the anchor high bits the old per-index `put` dropped (Writer::put is LSB-first and masks); `payload_step_matches_the_reference_decode` still checks every slot of every model against the reference decode, now including narrow tables that force mixed windows. Table construction becomes slower (one-time, about 0.36 ms per a commenter's measurement). CI (`check`) passes. This is a recommendation for a maintainer to look, not an approval. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 04:19 UTC · original · approved these changes

Tested merged onto origin/main (63bcca4) on Windows 11 x64:

  • cargo test -p victory-formats --lib bcpack::tests: 11 passed, 0 failed, 2 ignored (including fragmented_streams_match_original_decoder, payload_step_matches_the_reference_decode, and noise_never_panics).
  • cargo clippy -p victory-formats --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 04:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/2735#pullrequestreview-6068493782) · **approved these changes** Tested merged onto `origin/main` (`63bcca4`) on Windows 11 x64: - `cargo test -p victory-formats --lib bcpack::tests`: **11 passed, 0 failed, 2 ignored** (including `fragmented_streams_match_original_decoder`, `payload_step_matches_the_reference_decode`, and `noise_never_panics`). - `cargo clippy -p victory-formats --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠futurepro62 on GitHub · 2026-11-26 12:35 UTC · original

NVIDIA full-suite run, to go with the reports above by Dukestar69, fuzeofficlal, since none of them names a GPU run. Tested exact head 62911d1 (on its base 13f91ca).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), dxil.dll from Windows SDK 10.0.26100 (the loader's default), debug.
  • cargo build --workspace --locked, then cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware: 774 passed, 0 failed, 9 ignored.
  • The new tests ran and passed: fragmented_streams_match_original_decoder, invalid_modes_stop_after_the_last_valid_block.
  • --skip leaves out tests that fail on main 63bcca4 on this card too, not because of this PR: heap_direct_texture_reads_mark_their_heap_index ends the victory-shader test process with 0xc0000005 (#2571), so without the skip the rest of that crate never reports; dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views and the two stride_tests hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); gathers_at_a_level_match_the_hardware fails on lane 0 (#1744).

Not tested

  • AMD (the tests take the default adapter), a release build, the title (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
**@⁠futurepro62** on GitHub · 2026-11-26 12:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/2735#issuecomment-7192203524) NVIDIA full-suite run, to go with the reports above by Dukestar69, fuzeofficlal, since none of them names a GPU run. Tested exact head `62911d1` (on its base `13f91ca`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), `dxil.dll` from Windows SDK 10.0.26100 (the loader's default), debug. - `cargo build --workspace --locked`, then `cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware`: **774 passed, 0 failed, 9 ignored**. - The new tests ran and passed: `fragmented_streams_match_original_decoder`, `invalid_modes_stop_after_the_last_valid_block`. - `--skip` leaves out tests that fail on main `63bcca4` on this card too, not because of this PR: `heap_direct_texture_reads_mark_their_heap_index` ends the `victory-shader` test process with `0xc0000005` (#2571), so without the skip the rest of that crate never reports; `dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views` and the two `stride_tests` hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); `gathers_at_a_level_match_the_hardware` fails on lane 0 (#1744). **Not tested** - AMD (the tests take the default adapter), a release build, the title (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 12:58 UTC · original · approved these changes

Reviewed and verified 62911d14bbea against origin/main (63bcca43; clean git merge-tree):

  • The fast paths preserve exact bitstream semantics: the largest model has at most 256 symbols and 255 transitions (u8 indices cannot overflow), the transition rank includes the current slot across 64-slot word and 256-slot page boundaries, and the updated rANS step (crates/victory-formats/src/bcpack.rs:416-422) preserves original modular wrapping at slot == 0 and f[0] == 1.
  • Paired endpoint fields are disjoint, and stripping anchor MSBs in descending order (bcpack.rs:749-786, 934-980) matches the original per-index writer (verified all 64 three-subset partitions keep distinct non-zero anchors). Independent differential checks across 3,080,192 slot lookups, 12,320,721 rANS updates, 261,120 endpoints, and 19,200 index packs matched bit-for-bit.

Local verification on Windows 11 (Ryzen 9 9950X):

  • rustfmt --edition 2024 --check crates/victory-formats/src/bcpack.rs, xtask policy (184 files ok), cargo clippy -p victory-formats --all-targets (default & --all-features), and cargo test -p victory-formats in both Debug and Release (39 passed, 0 failed, 5 ignored) all pass.
  • Same-machine --release bcpack_throughput (16,384 blocks vs 63bcca43):
    • BC1: 108.0 -> 134.1 MB out/s (74.1 -> 59.7 ns/block, +24.2%)
    • BC4: 113.4 -> 138.7 MB out/s (70.5 -> 57.7 ns/block, +22.3%)
    • BC7 (mode 6): 66.1 -> 88.9 MB out/s (241.9 -> 180.1 ns/block, +34.5%)

LGTM.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 12:58 UTC · [original](https://github.com/Marvisak/VIctory/pull/2735#pullrequestreview-6074518768) · **approved these changes** Reviewed and verified `62911d14bbea` against `origin/main` (`63bcca43`; clean `git merge-tree`): - The fast paths preserve exact bitstream semantics: the largest model has at most 256 symbols and 255 transitions (`u8` indices cannot overflow), the transition rank includes the current slot across 64-slot word and 256-slot page boundaries, and the updated rANS step (`crates/victory-formats/src/bcpack.rs:416-422`) preserves original modular wrapping at `slot == 0` and `f[0] == 1`. - Paired endpoint fields are disjoint, and stripping anchor MSBs in descending order (`bcpack.rs:749-786, 934-980`) matches the original per-index writer (verified all 64 three-subset partitions keep distinct non-zero anchors). Independent differential checks across 3,080,192 slot lookups, 12,320,721 rANS updates, 261,120 endpoints, and 19,200 index packs matched bit-for-bit. Local verification on Windows 11 (Ryzen 9 9950X): - `rustfmt --edition 2024 --check crates/victory-formats/src/bcpack.rs`, `xtask policy` (`184 files ok`), `cargo clippy -p victory-formats --all-targets` (default & `--all-features`), and `cargo test -p victory-formats` in both Debug and Release (`39 passed, 0 failed, 5 ignored`) all pass. - Same-machine `--release bcpack_throughput` (16,384 blocks vs `63bcca43`): - BC1: `108.0 -> 134.1 MB out/s` (`74.1 -> 59.7 ns/block`, **+24.2%**) - BC4: `113.4 -> 138.7 MB out/s` (`70.5 -> 57.7 ns/block`, **+22.3%**) - BC7 (mode 6): `66.1 -> 88.9 MB out/s` (`241.9 -> 180.1 ns/block`, **+34.5%**) LGTM.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/2735/head:github-bot/gh-2735'
git switch 'github-bot/gh-2735'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2735'
git switch 'github-bot/gh-2735'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2735'
git switch 'github-bot/gh-2735'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2735'
git switch 'main'
git merge --squash 'github-bot/gh-2735'
git switch 'main'
git merge --ff-only 'github-bot/gh-2735'
git switch 'main'
git merge 'github-bot/gh-2735'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2735
No description provided.