Add clean-room BCPack BC6H/BC7 decoders, external history loading, and strict-mode mip fallback #27

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

@⁠LocranKing on GitHub · 2026-11-23 13:25 UTC · original
LocranKing:feat/bcpack-bc6h-bc7 → main · +1786 −54 · 7 files · 1 commits · open

AI-assisted.

Summary

Adds clean-room Xbox Series BCPack BC6H (codec 6) and BC7 (codec 7) block rebuilders, external bc6h/bc7 history loading in BcpackHistory.Load(path), and strict-mode mip fallback in TtpfReader so mixed-mip textures continue exporting and previewing their decodable mips when upper BCPack mips require external history.

Follow-up to PR #7 (BcpackDecoder BC1–BC5) and PR #22 (BcpackHistory external tables), addressing ItsNotRexed/Leonida ASSET-315 / docs/textures.md Next steps 1–2.

What changed

  • Core/BcpackTablesBptc.cs (new): Public D3D11/Khronos BPTC partition/anchor tables, all 14 BC6H bit-packing plans, BC7 lane-15 signature rank table (286 entries + 10 inferred ranks), de-interleave lookup tables, and empty default Bc6hHistoryTable / Bc7HistoryTable containers. Zero unpublished game constants are embedded.
  • Core/BcpackHistory.cs: Extends BcpackHistory.Load(path) to parse optional bc6h (bc6h.json, "bc6h" key, "codec": "bc6h", or "endpointHistory" marker) and bc7 (bc7.json, "bc7" key, "codec": "bc7", or "historyEndpoints" marker) sections matching upstream Leonida tools/bcpack_tables_bptc.py (endpointConfirmedBits, indexConfirmedBits, flat and nested BC7 arrays, mid-range fill for 0 < d < 8, and historyEndpointLastResort), while keeping BC1–BC5 parsing and error messages unchanged.
  • Core/BcpackDecoder.cs: Enables SupportsRebuild = true for codecs 6 (BC6H) and 7 (BC7) and implements RebuildBc6h and RebuildBc7 using UInt128 packing, fixed 256-entry ring buffers, periodic CancellationToken checks, and identical strict/provisional gating, confirmed-bit checks, anchor/p-bit consistency validation, and distinct unknown-block counting.
  • Core/TtpfReader.cs:
    • CreateDds: catches BcpackProvisionalException on refused upper mips (caching refused chunk keys to avoid redundant rANS decodes), falls back to the highest contiguous decodable mip chain with recomputed DDS header dimensions/pitch/size, and rethrows the original BcpackProvisionalException only if no mip in the texture is decodable.
    • DecodeTexturePreview / DecodeTexturePixels: falls back to coarser decodable mips when the selected preview mip is refused, and walks back upward (within the 16M pixel cap) when only upper mips are decodable (e.g., zlib/raw upper mips with a history-dependent BC6H/BC7 tail).
    • Updates "Remaining layouts" metadata string.
  • Core/ArchiveReader.cs, README.md, README.txt: Updates BCPack BC6H/BC7 capability, external history, and lower-mip fallback notes.

Verification

Only outcomes reported, no game data.

Environment: Windows 11 Build 26200 x64 · NVIDIA RTX 3060 Ti (driver 621.01) · .NET SDK 8.0.428 · Python 3.12.10.

  1. Clean Release builds (TreatWarningsAsErrors=true in Core):
    • dotnet build Core\RpfPreview.Core.csproj -c Release: 0 warnings, 0 errors.
    • dotnet build Tools\IndexBuilder\IndexBuilder.csproj -c Release: 0 warnings, 0 errors.
    • Upstream ItsNotRexed/Leonida tools/rpf9dump (main, including --history / --allow-provisional) compiled against this Core: 0 warnings, 0 errors.
  2. Synthetic C# regression suite (196 checks, zero game data) + negative control:
    • 196/196 PASS on feat/bcpack-bc6h-bc7 (all 14 BC6H modes, all 8 BC7 modes including rotations, index selection, shared/per-endpoint p-bits, mode 0 zero shortcut, fixed docs/textures.md stream, 31 strict/provisional/confirmedBits/anchor/p-bit/distinct-unknown-block cases verified against upstream tools/bcpack.py, 11 malformed JSON validation cases, and a synthetic 5-texture RDS5/ETPF634 pool covering BC7+zlib fallback, BC6H+raw fallback, all-BC7 strict refusal, self-contained BC7 strict export, and reverse-mixed zlib-top + BC7-tail preview/export).
    • Negative control on pre-PR base 16134b0: fails as expected (FAIL: BC6H codec 6 supported).
  3. Read-only 57-pool Xbox Series 1.0.2.1 parity & fallback validation (aggregate counts only):
    • Full C# chunk scan across all 5,435 BC6H+BC7 chunks (268 BC6H, 5,167 BC7; 1,300.6 MiB decoded) in 4 modes (strict/provisional × without/with locally self-calibrated history): 0 unexpected errors (1,556 chunks decoded with local provisional history).
    • End-to-end bit-exact parity vs upstream Python tools/bcpack.py (SHA-256 of decoded bytes + sorted provisional label sets + exact refusal messages across all 4 modes):
      • 100% of all 268 BC6H chunks + all 658 decoded <= 64 KiB BC7 chunks (926 chunks, 102.2 MiB): 3,704 / 3,704 exact matches, 0 mismatches.
      • Stratified E2E set of 456 chunks (106 BC6H + 350 BC7 up to 256 KiB, including 100 refused BC7 chunks): 1,824 / 1,824 exact matches, 0 mismatches.
    • Texture-level TtpfReader.CreateDds strict-mode comparison across all 3,270 BC6H/BC7-touching textures vs pre-PR 16134b0:
      • 0 regressions.
      • 2,397 lower-mip fallbacks preserved bit-exact (identical DDS SHA-256).
      • 121 reverse-mixed textures (zlib/raw upper mips + BC7 tail) upgraded to additional decodable zlib/raw mips; 121/121 passed both DecodeTexturePreview(192) and DecodeTexturePixels(0) with 0 failures.
      • Sample of 200/200 strict-OK mixed-mip textures passed both DecodeTexturePreview(192) and DecodeTexturePixels(0) with 0 failures.
      • With locally self-calibrated provisional history (--limit 200): 2,633 textures exported (648 full-resolution BC6H/BC7).
**@⁠LocranKing** on GitHub · 2026-11-23 13:25 UTC · [original](https://github.com/breadbbbb/rpf9-explorer/pull/27) `LocranKing:feat/bcpack-bc6h-bc7` → `main` · +1786 −54 · 7 files · 1 commits · open AI-assisted. ### Summary Adds clean-room Xbox Series BCPack **BC6H** (codec `6`) and **BC7** (codec `7`) block rebuilders, external `bc6h`/`bc7` history loading in `BcpackHistory.Load(path)`, and strict-mode mip fallback in `TtpfReader` so mixed-mip textures continue exporting and previewing their decodable mips when upper BCPack mips require external history. Follow-up to PR #7 (`BcpackDecoder` BC1–BC5) and PR #22 (`BcpackHistory` external tables), addressing `ItsNotRexed/Leonida` `ASSET-315` / `docs/textures.md` Next steps 1–2. ### What changed - **`Core/BcpackTablesBptc.cs`** *(new)*: Public D3D11/Khronos BPTC partition/anchor tables, all 14 BC6H bit-packing plans, BC7 lane-15 signature rank table (`286` entries + `10` inferred ranks), de-interleave lookup tables, and empty default `Bc6hHistoryTable` / `Bc7HistoryTable` containers. **Zero unpublished game constants are embedded.** - **`Core/BcpackHistory.cs`**: Extends `BcpackHistory.Load(path)` to parse optional `bc6h` (`bc6h.json`, `"bc6h"` key, `"codec": "bc6h"`, or `"endpointHistory"` marker) and `bc7` (`bc7.json`, `"bc7"` key, `"codec": "bc7"`, or `"historyEndpoints"` marker) sections matching upstream `Leonida` `tools/bcpack_tables_bptc.py` (`endpointConfirmedBits`, `indexConfirmedBits`, flat and nested BC7 arrays, mid-range fill for `0 < d < 8`, and `historyEndpointLastResort`), while keeping BC1–BC5 parsing and error messages unchanged. - **`Core/BcpackDecoder.cs`**: Enables `SupportsRebuild = true` for codecs `6` (`BC6H`) and `7` (`BC7`) and implements `RebuildBc6h` and `RebuildBc7` using `UInt128` packing, fixed 256-entry ring buffers, periodic `CancellationToken` checks, and identical strict/provisional gating, confirmed-bit checks, anchor/p-bit consistency validation, and distinct unknown-block counting. - **`Core/TtpfReader.cs`**: - `CreateDds`: catches `BcpackProvisionalException` on refused upper mips (caching refused chunk keys to avoid redundant rANS decodes), falls back to the highest contiguous decodable mip chain with recomputed DDS header dimensions/pitch/size, and rethrows the original `BcpackProvisionalException` only if no mip in the texture is decodable. - `DecodeTexturePreview` / `DecodeTexturePixels`: falls back to coarser decodable mips when the selected preview mip is refused, and walks back upward (within the 16M pixel cap) when only upper mips are decodable (e.g., zlib/raw upper mips with a history-dependent BC6H/BC7 tail). - Updates `"Remaining layouts"` metadata string. - **`Core/ArchiveReader.cs`, `README.md`, `README.txt`**: Updates BCPack BC6H/BC7 capability, external history, and lower-mip fallback notes. ### Verification Only outcomes reported, no game data. **Environment:** Windows 11 Build 26200 x64 · NVIDIA RTX 3060 Ti (driver 621.01) · .NET SDK 8.0.428 · Python 3.12.10. 1. **Clean Release builds (`TreatWarningsAsErrors=true` in `Core`):** - `dotnet build Core\RpfPreview.Core.csproj -c Release`: **0 warnings, 0 errors**. - `dotnet build Tools\IndexBuilder\IndexBuilder.csproj -c Release`: **0 warnings, 0 errors**. - Upstream `ItsNotRexed/Leonida` `tools/rpf9dump` (`main`, including `--history` / `--allow-provisional`) compiled against this `Core`: **0 warnings, 0 errors**. 2. **Synthetic C# regression suite (196 checks, zero game data) + negative control:** - **196/196 PASS** on `feat/bcpack-bc6h-bc7` (all 14 BC6H modes, all 8 BC7 modes including rotations, index selection, shared/per-endpoint p-bits, mode 0 zero shortcut, fixed `docs/textures.md` stream, 31 strict/provisional/confirmedBits/anchor/p-bit/distinct-unknown-block cases verified against upstream `tools/bcpack.py`, 11 malformed JSON validation cases, and a synthetic 5-texture RDS5/ETPF634 pool covering BC7+zlib fallback, BC6H+raw fallback, all-BC7 strict refusal, self-contained BC7 strict export, and reverse-mixed zlib-top + BC7-tail preview/export). - **Negative control** on pre-PR base `16134b0`: fails as expected (`FAIL: BC6H codec 6 supported`). 3. **Read-only 57-pool Xbox Series `1.0.2.1` parity & fallback validation (aggregate counts only):** - Full C# chunk scan across all **5,435** BC6H+BC7 chunks (`268` BC6H, `5,167` BC7; `1,300.6 MiB` decoded) in 4 modes (strict/provisional × without/with locally self-calibrated history): **0 unexpected errors** (`1,556` chunks decoded with local provisional history). - End-to-end bit-exact parity vs upstream Python `tools/bcpack.py` (SHA-256 of decoded bytes + sorted provisional label sets + exact refusal messages across all 4 modes): - **100% of all 268 BC6H chunks** + **all 658 decoded `<= 64 KiB` BC7 chunks** (`926` chunks, `102.2 MiB`): **3,704 / 3,704 exact matches, 0 mismatches**. - Stratified E2E set of **456 chunks** (`106` BC6H + `350` BC7 up to `256 KiB`, including `100` refused BC7 chunks): **1,824 / 1,824 exact matches, 0 mismatches**. - Texture-level `TtpfReader.CreateDds` strict-mode comparison across all **3,270** BC6H/BC7-touching textures vs pre-PR `16134b0`: - **0 regressions**. - **2,397** lower-mip fallbacks preserved **bit-exact** (identical DDS SHA-256). - **121** reverse-mixed textures (zlib/raw upper mips + BC7 tail) upgraded to additional decodable zlib/raw mips; **121/121** passed both `DecodeTexturePreview(192)` and `DecodeTexturePixels(0)` with 0 failures. - Sample of **200/200** strict-OK mixed-mip textures passed both `DecodeTexturePreview(192)` and `DecodeTexturePixels(0)` with 0 failures. - With locally self-calibrated provisional history (`--limit 200`): **2,633** textures exported (**648** full-resolution BC6H/BC7).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 19:06:02 +00:00
Author
Owner

@⁠breadbbbb on GitHub · 2026-11-23 22:28 UTC · original · requested changes

I reproduced an extraction regression on Windows with this head merged into main. In ttdb_graphics.ttpf, slot 75 (BC7, DXGI 98), TtpfReader.CreateDds successfully returned 11,172 bytes in strict mode. ArchiveReader.Open still advertised 43,940 bytes for the entry. Extracting that one entry through ArchiveDocument.ExtractAsync failed with “Decoded member length does not match the archive index.”

Please make the selected mip chain and its effective DDS size agree across catalog metadata, preview, and extraction. Keep the archive reader’s length check. A regression that opens a mixed-mip pool as an ArchiveDocument and compares the extracted DDS byte-for-byte with CreateDds would cover the desktop path.

**@⁠breadbbbb** on GitHub · 2026-11-23 22:28 UTC · [original](https://github.com/breadbbbb/rpf9-explorer/pull/27#pullrequestreview-6030532386) · **requested changes** I reproduced an extraction regression on Windows with this head merged into main. In `ttdb_graphics.ttpf`, slot 75 (BC7, DXGI 98), `TtpfReader.CreateDds` successfully returned 11,172 bytes in strict mode. `ArchiveReader.Open` still advertised 43,940 bytes for the entry. Extracting that one entry through `ArchiveDocument.ExtractAsync` failed with “Decoded member length does not match the archive index.” Please make the selected mip chain and its effective DDS size agree across catalog metadata, preview, and extraction. Keep the archive reader’s length check. A regression that opens a mixed-mip pool as an ArchiveDocument and compares the extracted DDS byte-for-byte with CreateDds would cover the desktop path.
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/27/head:github-bot/gh-27'
git switch 'github-bot/gh-27'

Merge

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