Skip to content

bcpack: strict BC3 and BC5 block rebuild in Rust (~45x-47x end to end) - #6308

Merged
shayderrr merged 4 commits into
ItsNotRexed:mainfrom
TG0000:perf/bcpack-bc3-bc5
Nov 23, 2026
Merged

shayderrr merged 4 commits into
ItsNotRexed:mainfrom
TG0000:perf/bcpack-bc3-bc5

Conversation

@TG0000

@TG0000 TG0000 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Extends the Rust BCPack strict block rebuilder in tools/bcpack_calibration to cover BC3 and BC5 in the same native call as the lane decode, delivering ~45×–47× end-to-end speedup over pure Python (~6.4×–6.6× over lanes-only) with bit-exact parity.

Depends on: #6188

What changed

  • tools/bcpack_calibration/src/blocks.rs: adds bit-exact strict bc3 and bc5 block rebuilders plus dist8 and pair_bits helpers.
  • tools/bcpack_calibration/src/lib.rs: wires codecs 3 (BC3) and 5 (BC5) into decode_blocks, and adds a lanes != 12 C-ABI guard.
  • tools/bcpack.py: registers BC3 and BC5 in _NATIVE_BLOCKS; widens _decode_blocks_native history-loader exception handling to (ValueError, TypeError, OSError) so invalid history arguments fall back to Python and preserve exact error ordering (after lane decode).
  • tests/test_bcpack_native.py: adds 7 tests in NativeBlockRebuildTest covering BC3/BC5 randomized equivalence, exact synthetic encoder round-trip, refusals/fallbacks (lane 1, size, corrupt/truncated chunks), confirmed/unconfirmed/unknown built-in history, external history, invalid-history error ordering, and negative controls (swapped LUT rows + LEONIDA_BCPACK_NATIVE=0).
  • tools/bcpack_calibration/README.md: documents BC3/BC5 support and measured synthetic speedups.

Python remains the reference and fallback: any non-strict mode, non-zero lane 1, unconfirmed/unknown history entry, out-of-range symbol, or refused lane stream falls back to pure Python unchanged (LEONIDA_BCPACK_NATIVE=0 disables the native path). third_party/bcpack-rs is untouched.

Verification (Windows 11, Ryzen 9 5900X, CPython 3.12.10, Rust release build, CPU only, synthetic inputs, no game files)

  • Unit tests:
    • python -I tests/test_bcpack_native.py: 12/12 OK (all 12 skip with LEONIDA_BCPACK_NATIVE=0)
    • python -I tests/test_bcpack.py: 17/17 OK (both default native and LEONIDA_BCPACK_NATIVE=0)
    • test_bcpack_tables.py (34/34), test_bcpack_history_calib.py (19/19), test_ttpf_mips.py (77/77), test_ttpf_assets.py (28/28, 3 skipped), test_calibrated_texture_extra.py (24/24)
  • Equivalence:
    • 750 synthetic streams (3 seeds × 250, strict + provisional, bit-flips, truncation, trailing bytes, external histories): 0 mismatches, 0 accepted-by-only-one-side (255 accepted, 495 refused).
    • Independent 6,000-stream randomized differential review (strict + provisional, random external histories): 0 mismatches (134 BC3 + 168 BC5 native, rest fallback).
  • End-to-end synthetic benchmark (16,384 blocks, median of 5 alternating runs, SHA-256 identical across all three modes):
    • BC3: 935 ms pure Python → 126.5 ms lanes-only → 19.8 ms native (47.2× vs Python, 6.4× vs lanes-only)
    • BC5: 908 ms pure Python → 130.8 ms lanes-only → 19.9 ms native (45.5× vs Python, 6.6× vs lanes-only)

AI-assisted (Claude Sonnet 5.5 implementation, Claude Opus 5.5 review). Claimed in #6214.

TG0000 and others added 4 commits November 23, 2026 13:50
tools/bcpack_calibration already exports `unpack` (third_party/bcpack-rs).
decode_lanes now tries it first and keeps the Python model for the streams
it refuses, so strict output is unchanged. About 60x faster on the entropy
layer and 12x end to end for BC1 on synthetic chunks; LEONIDA_BCPACK_NATIVE=0
turns it off. tests/test_bcpack_native.py checks the two agree (skipped when
the library is not built).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
third_party/bcpack-rs follows the hardware RTL and has no virtual zero
bytes, so it refused every q = 0 fragment whose omitted trailing zero comes
before a symbol, and those chunks fell back to Python. tools/bcpack_calibration
now exports unpack_model, a line-by-line port of _decode_lanes_raw fed
bcpack.py's own probability tables: it accepts exactly the streams Python
accepts, with the same symbols (8,000 synthetic chunks, sound and corrupt,
single and multi-fragment: 0 disagreements). About 30x on the lane layer and
10x end to end for BC1, q = 0 included.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… the lanes

decode_blocks (tools/bcpack_calibration/src/blocks.rs) ports _bc1_blocks and
_bc4_blocks and runs them on the lanes of unpack_model without building
Python lists. It takes only the strict path: a non-zero lane 1 or a history
entry that is not confirmed hands the whole chunk back to Python, so errors
and provisional entries do not change. Synthetic 16,384-block chunks: BC1
420 -> 23 ms, BC4 540 -> 14 ms against Python, same SHA-256.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… the lanes

decode_blocks now also takes codecs 3 and 5, with the same contract as BC1 and
BC4: only the strict path is native. A non-zero lane 1, a history entry that is
not confirmed, an out-of-range symbol or a stream the lane model refuses returns
-1, and Python decodes the chunk itself, so output, errors and provisional
entries do not change. LEONIDA_BCPACK_NATIVE=0 still turns it off.

_decode_blocks_native also returns None when the history argument is rejected
by the loader, so Python raises that error after the lanes as it always did.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@Kev-Ball

Copy link
Copy Markdown

Checked strict decoding at 7e4a5a64 on all 368 BC3/BC5 chunks across the 57 texture pools in Xbox 1.0.2.1. Compared the release Rust library through bcpack.decode_with_info with the same revision’s pure-Python path, using built-in histories and default strict mode. I counted actual native returns separately from fallbacks.

Codec Package chunks Decoded directly in Rust Fell back and raised the same strict-mode error Decoded bytes compared
BC3 35 8 27 966,656
BC5 333 8 325 10,551,296

Every accepted chunk matched byte-for-byte, including the returned provisional-label set. All 352 refusals matched the Python exception type and message (BcpackProvisionalError), and neither side accepted a chunk the other refused. On this package the new native path covers all 16 chunks the built-in strict reference accepts. CachyOS x86-64, CPU only, AI-assisted.

@TG0000

TG0000 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @Kev-Ball for verifying all 368 retail BC3/BC5 chunks — 100% byte-identical, with all 16 built-in strict chunks covered. Much appreciated.

@shayderrr shayderrr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

Reviewed change in PR #6308: bcpack: strict BC3 and BC5 block rebuild in Rust (~45x-47x end to end).

Scope: tests/test_bcpack_native.py, tools/bcpack.py, tools/bcpack_calibration/README.md, +3 more
Diff: +1182 / -1 across 6 file(s).

The change stays inside the stated scope and the touched files line up with the description. No unrelated production paths are pulled in, and the stated behavior is directly exercised by the diff. I'm approving this one.

Reviewed by shayderrr via automated batch review.

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Head 7e4a5a64b, main 561b65a83 (0 behind); git diff --check clean. Stacked on #6188 (open): this diff includes its BC1/BC4 and Rust lane-model commits, so merge order matters.

  • cargo build --release --locked --offline --manifest-path tools/bcpack_calibration/Cargo.toml -> OK (2.1s)
  • before the build: 12 skipped; after: python -I -m pytest tests/test_bcpack_native.py -q -> 12 passed, 754 subtests

BC3/BC5 rebuild is bit-exact against the Python strict path across the randomized and encoder-round-trip cases, and the lane(1)==0 / confirmed-history gating falls back to Python as designed. First-frame: none (offline texture export tool, no runtime path). Merge after #6188.

@shayderrr

Copy link
Copy Markdown
Collaborator

Thanks @TG0000 - I went through this change and the diff around tests/test_bcpack_native.py. The change is tight, the rationale matches the code, and nothing here needs another pass from my side. Approved and squash-merging it into main now. Nice work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants