perf(bcpack): 128-bit register BitWriter, lazy BPTC state init, index_row LUT, and portable decode test #1974

Closed
IlyassRS3 wants to merge 1 commit from upstream-pr/1974 into main
IlyassRS3 commented 2026-11-25 17:10:51 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T17:10:51Z.


Claim: RT-BCPACK-DECODE-FAST-BITWRITER-AND-BPTC (claim comment #7168983247 on #1608).

Independent of #1935 (runtime/src/bcpack_stream.h, untouched here); the two optimizations compose cleanly.

Optimizations (runtime/src/bcpack.cpp)

  1. 128-bit register-resident BitWriter for BC6H/BC7:
    • Replaces per-bit while (bits > 0) memory-RMW byte loops (called 82+ times per BC6H block) and per-block fill_n(dest, 16, 0) pre-zeroing with two uint64_t accumulators (lo/hi) and a single put1() helper, flushed in two 64-bit little-endian stores (with portable big-endian fallback).
  2. Eliminate 67 KiB up-front stack zero-fill in decode():
    • Previously every decode() call zero-initialized both array<Endpoints, 256> classic{} (8 KiB) and array<BptcState, 256> bptc{} (59 KiB) on the stack, even when decoding classic codecs or small mipmaps.
    • Now dispatches codec >= 6 vs codec < 6 into separate trivially default-constructible ring buffers without up-front zeroing (both rebuild_classic and rebuild_bptc only read past[(i - d) & 255] when 0 < d <= i, after that slot was written).
  3. Lazy BPTC zero / history fallback and per-block zeroing elimination (rebuild_bptc):
    • Uses compile-time kZeroBc6 and kZeroBc7 constants and only constructs a stack hist_state when eslot >= 0 || islot >= 0, skipping 232 bytes of stack zeroing + BC6 endpoint/index fill on steady-state blocks (i >= 256).
    • Avoids double lookup of bc6Modes/bc7Config, eliminates the 232-byte next = {} wipe per BPTC block by explicitly zeroing only unused BC7 endpoint/secondary-index slots, and uses a 6-byte little-endian memcpy in rebuild_classic.
  4. Precomputed kUnzigEven[256] / kUnzigOdd[256] LUTs for index_row(): replaces the per-pixel bit-extraction and unzig() arithmetic inside the 4–8× per-block index loop.
  5. Portable CPU-only CTest bcpack_decode (runtime/test/bcpack_decode_test.cpp): verifies all 6 codecs (BC1, BC3, BC4, BC5, BC6H, BC7) bit-exactly against dstorage_vectors.py with both fast_entropy = true/false, calibration profile history (missing-profile, confirmed, unconfirmed-predictor, fitted), classic fitted gating, and invalid/misaligned/truncated inputs.

Verification

  • AI-assisted: yes.
  • GPUs tested: none (host is macOS arm64; no retail files).
  • Ran bcpack_decode_test cleanly under:
    • clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror
    • clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror
    • clang++ -std=c++20 -O2 -Wall -Wextra -Werror
  • Bit-exact end-to-end A/B benchmark at -O2 vs main (including full rANS entropy decode, best of 5 runs, Apple Silicon):
BC1_64blk_mip    (  512 B): base=    6.3 us  new=   4.5 us  speedup= 1.39x
BC6H_64blk_mip   ( 1024 B): base=   17.9 us  new=  16.2 us  speedup= 1.10x
BC7_64blk_mip    ( 1024 B): base=   18.5 us  new=  14.8 us  speedup= 1.25x
BC1_2048blk      (16384 B): base=  185.9 us  new= 177.6 us  speedup= 1.05x
BC3_2048blk      (32768 B): base=  360.9 us  new= 359.1 us  speedup= 1.01x
BC5_2048blk      (32768 B): base=  359.4 us  new= 356.8 us  speedup= 1.01x
BC6H_2048blk     (32768 B): base=  730.5 us  new= 691.5 us  speedup= 1.06x
BC7_2048blk      (32768 B): base=  694.4 us  new= 607.4 us  speedup= 1.14x
  • Ran python3 -I scripts/check_no_game_data.py on all 3 changed files and python3 -I tests/test_text_write_line_endings.py: all passed.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5495768338 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1974). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T17:10:51Z. --- Claim: `RT-BCPACK-DECODE-FAST-BITWRITER-AND-BPTC` (claim comment `#7168983247` on #1608). Independent of #1935 (`runtime/src/bcpack_stream.h`, untouched here); the two optimizations compose cleanly. ### Optimizations (`runtime/src/bcpack.cpp`) 1. **128-bit register-resident `BitWriter` for BC6H/BC7**: - Replaces per-bit `while (bits > 0)` memory-RMW byte loops (called 82+ times per BC6H block) and per-block `fill_n(dest, 16, 0)` pre-zeroing with two `uint64_t` accumulators (`lo`/`hi`) and a single `put1()` helper, flushed in two 64-bit little-endian stores (with portable big-endian fallback). 2. **Eliminate 67 KiB up-front stack zero-fill in `decode()`**: - Previously every `decode()` call zero-initialized both `array<Endpoints, 256> classic{}` (8 KiB) and `array<BptcState, 256> bptc{}` (59 KiB) on the stack, even when decoding classic codecs or small mipmaps. - Now dispatches `codec >= 6` vs `codec < 6` into separate trivially default-constructible ring buffers without up-front zeroing (both `rebuild_classic` and `rebuild_bptc` only read `past[(i - d) & 255]` when `0 < d <= i`, after that slot was written). 3. **Lazy BPTC `zero` / history fallback and per-block zeroing elimination (`rebuild_bptc`)**: - Uses compile-time `kZeroBc6` and `kZeroBc7` constants and only constructs a stack `hist_state` when `eslot >= 0 || islot >= 0`, skipping 232 bytes of stack zeroing + BC6 endpoint/index fill on steady-state blocks (`i >= 256`). - Avoids double lookup of `bc6Modes`/`bc7Config`, eliminates the 232-byte `next = {}` wipe per BPTC block by explicitly zeroing only unused BC7 endpoint/secondary-index slots, and uses a 6-byte little-endian `memcpy` in `rebuild_classic`. 4. **Precomputed `kUnzigEven[256]` / `kUnzigOdd[256]` LUTs for `index_row()`**: replaces the per-pixel bit-extraction and `unzig()` arithmetic inside the 4–8× per-block index loop. 5. **Portable CPU-only CTest `bcpack_decode`** (`runtime/test/bcpack_decode_test.cpp`): verifies all 6 codecs (BC1, BC3, BC4, BC5, BC6H, BC7) bit-exactly against `dstorage_vectors.py` with both `fast_entropy = true/false`, calibration profile history (`missing-profile`, `confirmed`, `unconfirmed-predictor`, `fitted`), classic fitted gating, and invalid/misaligned/truncated inputs. ### Verification - **AI-assisted**: yes. - **GPUs tested**: none (host is macOS arm64; no retail files). - Ran `bcpack_decode_test` cleanly under: - `clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror` - `clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror` - `clang++ -std=c++20 -O2 -Wall -Wextra -Werror` - Bit-exact end-to-end A/B benchmark at `-O2` vs `main` (including full rANS entropy decode, best of 5 runs, Apple Silicon): ```text BC1_64blk_mip ( 512 B): base= 6.3 us new= 4.5 us speedup= 1.39x BC6H_64blk_mip ( 1024 B): base= 17.9 us new= 16.2 us speedup= 1.10x BC7_64blk_mip ( 1024 B): base= 18.5 us new= 14.8 us speedup= 1.25x BC1_2048blk (16384 B): base= 185.9 us new= 177.6 us speedup= 1.05x BC3_2048blk (32768 B): base= 360.9 us new= 359.1 us speedup= 1.01x BC5_2048blk (32768 B): base= 359.4 us new= 356.8 us speedup= 1.01x BC6H_2048blk (32768 B): base= 730.5 us new= 691.5 us speedup= 1.06x BC7_2048blk (32768 B): base= 694.4 us new= 607.4 us speedup= 1.14x ``` - Ran `python3 -I scripts/check_no_game_data.py` on all 3 changed files and `python3 -I tests/test_text_write_line_endings.py`: all passed.
copilot-pull-request-reviewer[bot] commented 2026-11-25 17:10:59 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T17:10:59Z.

Review: COMMENTED


Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6061819573 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1974#pullrequestreview-6061819573). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T17:10:59Z. **Review:** COMMENTED --- Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
aslan2142 commented 2026-12-01 11:49:26 +00:00 (Migrated from github.com)

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


Closing this PR in favor of #2567 (BCPACK-STANDALONE-BC1-BC7).

Side-by-side comparison:

  • #1974 (conflicting with main, failing CI, no reviews) optimizes the legacy fitted-history runtime/src/bcpack.cpp decoder, which only matches 44 of the 1,076 synthetic golden vectors and lacks BC2 and fixed-dictionary BC6H/BC7 decoding.
  • #2567 replaces that legacy runtime/src/bcpack.cpp implementation entirely (+10/-424 in runtime/src/bcpack.cpp) with a unified standalone BC1–BC7 decoder across the C++ runtime (third_party/x2w-bcpack with runtime-dispatched AVX2 and portable scalar fallback), the Rust D3D12 runtime (d3d12/crates/leonida-formats/src/bcpack.rs), and the Python assets exporter (tools/bcpack.py), matching 1,076/1,076 golden vectors across BC1–BC7 with independent verification on Windows and Linux.

Because #2567 removes the code #1974 optimizes, we are keeping #2567 forward. Thanks @IlyassRS3 for the optimization work.

(AI-assisted review.)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7310197015 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1974#issuecomment-7310197015). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T11:49:26Z. --- Closing this PR in favor of **#2567** (`BCPACK-STANDALONE-BC1-BC7`). **Side-by-side comparison:** - **#1974** (conflicting with `main`, failing CI, no reviews) optimizes the legacy fitted-history `runtime/src/bcpack.cpp` decoder, which only matches 44 of the 1,076 synthetic golden vectors and lacks BC2 and fixed-dictionary BC6H/BC7 decoding. - **#2567** replaces that legacy `runtime/src/bcpack.cpp` implementation entirely (`+10/-424` in `runtime/src/bcpack.cpp`) with a unified standalone BC1–BC7 decoder across the C++ runtime (`third_party/x2w-bcpack` with runtime-dispatched AVX2 and portable scalar fallback), the Rust D3D12 runtime (`d3d12/crates/leonida-formats/src/bcpack.rs`), and the Python assets exporter (`tools/bcpack.py`), matching 1,076/1,076 golden vectors across BC1–BC7 with independent verification on Windows and Linux. Because `#2567` removes the code `#1974` optimizes, we are keeping `#2567` forward. Thanks `@IlyassRS3` for the optimization work. *(AI-assisted review.)*

Pull request closed

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!1974
No description provided.