RT-BCPACK-DECODER: a runtime BCPack decoder, BC1/BC3/BC4/BC5, checked against tools/bcpack.py #976

Closed
leonida/mandy
iFeyz wants to merge 0 commits from feat/bcpack-decoder into main

Pull request closed

This pull request cannot be reopened because the branch was deleted.

iFeyz commented 2026-11-24 16:09:51 +00:00

Claimed in the coordination issue as: RT-BCPACK-DECODER (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7136759575)

What and why

The DirectStorage shim refuses every BCPack request with E_NOTIMPL (execute_request, runtime/shims/dstorage.cpp),
and the retail title asks for BCPack modes 1, 5 and 7 (@DesertGamer's count of refused requests, #14 comment
7135719755). This adds the decoder that path will call, as a portable library (gta6rt_bcpack, no windows.h), so
it builds and tests on every OS. It does not touch execute_request: every BCPack request measured also carries the
destination-pages bit, so the wiring waits for destination pages (@DadaCap), and @DesertGamer offered to do it.

  • runtime/src/bcpack.{h,cpp}: the lane layer is a statement-by-statement port of
    tools/bcpack_calibration/src/model.rs (itself a port of _decode_lanes_raw), with a 64-bit coder state instead
    of model.rs's i128: a state below RANS_L is shifted a byte at a time until it reaches RANS_L, so it stays below
    2^32, and a negative one (only a corrupt stream makes one) is refused before any shift, as model.rs does. The
    BC1, BC3, BC4 and BC5 rebuilds follow blocks.rs / _bc1_blocks .. _bc5_blocks, strict like bcpack.decode:
    a block that needs an unconfirmed built-in history entry is refused (Status::unconfirmed_history), never
    guessed. Where the Rust port hands a stream back to Python, this returns a Status. An optional History takes
    the user's calibrated entries, which win over the built-in ones (_resolve_bc_history's merge).
  • runtime/src/bcpack_tables.h: written by tools/bcpack_header.py from tools/bcpack_tables.py (rANS models,
    symbol-rank tables, confirmed history only). tests/test_bcpack_header.py fails if it goes stale or if a
    provisional entry gets in.
  • runtime/test/bcpack_test.cpp with runtime/test/bcpack_vectors.{py,h}: 120 lane cases over all six modes
    and 90 whole-chunk cases over modes 1, 3, 4 and 5, from tests/test_bcpack.py's synthetic encoder (valid,
    corrupted, cut short, a trailing byte, lane 1 set, history distances to no block, an earlier block or a built-in
    entry), each with the pure-Python decoder's answer (LEONIDA_BCPACK_NATIVE=0). No Python is needed at test time.
  • docs/directstorage.md: the decoder exists; what is left is the wiring.

Not in this PR: BC7 (mode 7, which the title uses) and BC6H; both return unsupported_codec for now and come next
under the same claim. Swizzle (no request measured uses it) and physical pages are not touched.

Tested

  • bcpack_test: all 210 cases agree with tools/bcpack.py (79 lane streams accepted and 41 refused exactly as
    Python does, with the same symbols; 71 chunks decoded to the same bytes, 19 refused with the same class of error),
    plus a chunk refused for its history decoding with a complete external table. Built with MSVC /W4, no warning.
  • tests/test_bcpack_header.py, tests/test_bcpack.py, tests/test_bcpack_tables.py, tests/test_tool_startup.py,
    tests/test_docs_links.py, tests/test_text_write_line_endings.py, scripts/check_no_game_data.py on every new
    file. tests/test_tool_stdout_encoding.py lists only tools/native_pso_hashes.py, which fails on main too.
  • Runtime (Windows 11, MSVC 2022 Build Tools, RelWithDebInfo, full build): ctest 302 of 306 pass, bcpack
    included. The 4 failures are the same as on main here (d3d12_clear, d3d12_execute_indirect_validation,
    d3d12_texture_tiles, d3d12_pipeline_library_host, all already reported in #14).

GPUs tested: n/a (CPU only)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.
Claimed in the coordination issue as: RT-BCPACK-DECODER (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7136759575) ### What and why The DirectStorage shim refuses every BCPack request with `E_NOTIMPL` (`execute_request`, `runtime/shims/dstorage.cpp`), and the retail title asks for BCPack modes 1, 5 and 7 (@DesertGamer's count of refused requests, #14 comment 7135719755). This adds the decoder that path will call, as a portable library (`gta6rt_bcpack`, no `windows.h`), so it builds and tests on every OS. It does not touch `execute_request`: every BCPack request measured also carries the destination-pages bit, so the wiring waits for destination pages (@DadaCap), and @DesertGamer offered to do it. - **`runtime/src/bcpack.{h,cpp}`**: the lane layer is a statement-by-statement port of `tools/bcpack_calibration/src/model.rs` (itself a port of `_decode_lanes_raw`), with a 64-bit coder state instead of model.rs's i128: a state below `RANS_L` is shifted a byte at a time until it reaches `RANS_L`, so it stays below 2^32, and a negative one (only a corrupt stream makes one) is refused before any shift, as model.rs does. The BC1, BC3, BC4 and BC5 rebuilds follow `blocks.rs` / `_bc1_blocks` .. `_bc5_blocks`, strict like `bcpack.decode`: a block that needs an unconfirmed built-in history entry is refused (`Status::unconfirmed_history`), never guessed. Where the Rust port hands a stream back to Python, this returns a `Status`. An optional `History` takes the user's calibrated entries, which win over the built-in ones (`_resolve_bc_history`'s merge). - **`runtime/src/bcpack_tables.h`**: written by `tools/bcpack_header.py` from `tools/bcpack_tables.py` (rANS models, symbol-rank tables, confirmed history only). `tests/test_bcpack_header.py` fails if it goes stale or if a provisional entry gets in. - **`runtime/test/bcpack_test.cpp`** with **`runtime/test/bcpack_vectors.{py,h}`**: 120 lane cases over all six modes and 90 whole-chunk cases over modes 1, 3, 4 and 5, from `tests/test_bcpack.py`'s synthetic encoder (valid, corrupted, cut short, a trailing byte, lane 1 set, history distances to no block, an earlier block or a built-in entry), each with the pure-Python decoder's answer (`LEONIDA_BCPACK_NATIVE=0`). No Python is needed at test time. - **`docs/directstorage.md`**: the decoder exists; what is left is the wiring. Not in this PR: BC7 (mode 7, which the title uses) and BC6H; both return `unsupported_codec` for now and come next under the same claim. Swizzle (no request measured uses it) and physical pages are not touched. ### Tested - `bcpack_test`: all 210 cases agree with `tools/bcpack.py` (79 lane streams accepted and 41 refused exactly as Python does, with the same symbols; 71 chunks decoded to the same bytes, 19 refused with the same class of error), plus a chunk refused for its history decoding with a complete external table. Built with MSVC `/W4`, no warning. - `tests/test_bcpack_header.py`, `tests/test_bcpack.py`, `tests/test_bcpack_tables.py`, `tests/test_tool_startup.py`, `tests/test_docs_links.py`, `tests/test_text_write_line_endings.py`, `scripts/check_no_game_data.py` on every new file. `tests/test_tool_stdout_encoding.py` lists only `tools/native_pso_hashes.py`, which fails on `main` too. - Runtime (Windows 11, MSVC 2022 Build Tools, RelWithDebInfo, full build): `ctest` 302 of 306 pass, `bcpack` included. The 4 failures are the same as on `main` here (`d3d12_clear`, `d3d12_execute_indirect_validation`, `d3d12_texture_tiles`, `d3d12_pipeline_library_host`, all already reported in #14). GPUs tested: n/a (CPU only) AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed.
KernelSmoker approved these changes 2026-11-24 16:27:49 +00:00
KernelSmoker left a comment

Reviewed exact head a371a739c3aeda38642429d4fed01eb9605689fb. Built the Linux CPU decoder target with GCC 16 in Fedora 45 and ran CTest: bcpack 1/1 passed. The generated-table test passed 3/3, and the no-game-data scan plus diff check passed. This is CPU decoder validation; no game data or GPU behavior is claimed. Hosted label status was queued when checked.

Reviewed exact head a371a739c3aeda38642429d4fed01eb9605689fb. Built the Linux CPU decoder target with GCC 16 in Fedora 45 and ran CTest: bcpack 1/1 passed. The generated-table test passed 3/3, and the no-game-data scan plus diff check passed. This is CPU decoder validation; no game data or GPU behavior is claimed. Hosted label status was queued when checked.
aslan2142 commented 2026-11-25 08:40:17 +00:00

Thanks for this stack. main already does what it does: #984 brought in a BCPack decoder and the DirectStorage wiring, so I'm closing this one as carried.

  • runtime/src/bcpack.{h,cpp} on main ports the strict rebuilds of tools/bcpack.py for BC1, BC3, BC4, BC5, BC6H and BC7.
  • dstorage.cpp's decode_stages decodes BCPack requests with zlib, destination and source pages, and swizzle. BC6H/BC7 history comes from a profile built locally (history.bcph). Only BC2 is still E_NOTIMPL.
  • Your 250 synthetic chunk vectors, run through main's decoder: every strict case agrees with tools/bcpack.py (140 identical, 110 refused). 29 of the 98 provisional cases are refused, because main doesn't decode unknown BC6H/BC7 history from zeros; it uses the locally fitted profile instead.
  • The add/add conflict on bcpack.{h,cpp} can't be resolved without picking one decoder, and main's is the one that's wired in and boots.
  • Your claim came first, and #984 had said your decoder would take precedence. The integration kept #984's because it was already wired in and was tested end to end. Sorry for the duplicated work.
  • A pull request porting bcpack_test and its vectors to main's leonida::bcpack::decode would be very welcome. It would be the decoder's first stand-alone test, with refusal cases.

AI-assisted (Claude Code, Claude Opus 5.5).

Thanks for this stack. `main` already does what it does: #984 brought in a BCPack decoder and the DirectStorage wiring, so I'm closing this one as carried. - `runtime/src/bcpack.{h,cpp}` on `main` ports the strict rebuilds of `tools/bcpack.py` for BC1, BC3, BC4, BC5, BC6H and BC7. - `dstorage.cpp`'s `decode_stages` decodes BCPack requests with zlib, destination and source pages, and swizzle. BC6H/BC7 history comes from a profile built locally (`history.bcph`). Only BC2 is still `E_NOTIMPL`. - Your 250 synthetic chunk vectors, run through `main`'s decoder: every strict case agrees with `tools/bcpack.py` (140 identical, 110 refused). 29 of the 98 provisional cases are refused, because `main` doesn't decode unknown BC6H/BC7 history from zeros; it uses the locally fitted profile instead. - The add/add conflict on `bcpack.{h,cpp}` can't be resolved without picking one decoder, and `main`'s is the one that's wired in and boots. - Your claim came first, and #984 had said your decoder would take precedence. The integration kept #984's because it was already wired in and was tested end to end. Sorry for the duplicated work. - A pull request porting `bcpack_test` and its vectors to `main`'s `leonida::bcpack::decode` would be very welcome. It would be the decoder's first stand-alone test, with refusal cases. AI-assisted (Claude Code, Claude Opus 5.5).
Sign in to join this conversation.
No description provided.