TtpfReader: BC6H/BC7 BCPack block rebuilders and strict-mode lower-mip fallback (ASSET-315) #23

Closed
github-bot wants to merge 1 commit from gh-pr/23/head into gh-pr/23/base
Owner

@⁠tedin7 on GitHub · 2026-11-22 23:16 UTC · original
tedin7:asset-315 → main · +1365 −22 · 13 files · 1 commits · closed without merging

Claimed in the Leonida coordination issue as: ASSET-315 (https://github.com/ItsNotRexed/Leonida/issues/3918#issuecomment-7087083876)

What and why

TtpfReader could not decode BCPack codec 6 (BC6H) and codec 7 (BC7) chunks, which Leonida's pure-Python tools/bcpack.py already reads. This ports the two block rebuilders and keeps the lower-mip fallback (Leonida docs/textures.md, "Next step" 1).

  • Core/BcpackBc6h.cs, Core/BcpackBc7.cs: ports of _bc6h_blocks and _bc7_check/_bc7_rebuild/_bc7_blocks (strict and provisional behaviour, same limits, same provisional labels). Core/BcpackBptcTables.cs holds the public D3D11/Khronos partition and anchor tables and the BC7 lane-15 signature permutation (same values as tools/bcpack_tables_bptc.py).
  • BCPack's built-in 256-entry initial-history tables for BC6H/BC7 are not included (they are not published; this is the history=None case of the Python tool). A block whose delta reaches before the start of the chunk reads that history: strict mode refuses the chunk (BcpackProvisionalException), provisional mode decodes it against an all-zero history while at most max(2, blocks / 1024) blocks depend on it. Chunks whose delta chains stay inside the chunk decode exactly. Per the Leonida notes, real chunks almost all read built-in history, so on real pools strict mode will mostly still refuse BC6H/BC7 until external history tables are wired in (the separate "Next step" 2). I could not check that here: there is no game data.
  • BcpackDecoder: codecs 6 and 7 are enabled; the class is partial and DecodeZigZag/LooseUnknownBlockLimit are internal so the rebuilders can share them.
  • TtpfReader: because BC6H/BC7 chunks now count as decodable at index time, a mip whose chunk strict mode refuses is handled when the texture is read: CreateDds drops that mip and every larger mip and exports from the next smaller one (the DDS header and size follow), and DecodeTexturePreview/DecodeTexturePixels use the next smaller mip. This also covers BC1/BC3/BC4/BC5 chunks that need unconfirmed history, which used to fail the whole export. Provisional mode does not fall back. If every mip is refused, CreateDds throws the BcpackProvisionalException.
  • READMEs and the archive description text no longer say BC6H/BC7 are unsupported.

Behaviour changes to be aware of: BC6H/BC7 textures are now listed as exportable with their full mip count instead of "lower-mip-only", and when the fallback applies the DDS is shorter than the DdsSize listed in the catalog (ArchiveReader shows the listed size but does not check the length). Making the listing exact would need a decode pass at index time, which I did not want to add to Inspect.

Not in this PR: the Patterns[2] fix (Core/TtpfReader.cs:70) and the external --history tables.

Tested

Environment: Linux, .NET SDK 8.0 (/root/.dotnet), no Windows, no GPU, no game data.

  • dotnet build Core/RpfPreview.Core.csproj -c Release -m:2: 0 warnings, 0 errors (TreatWarningsAsErrors is on).
  • dotnet run -c Release --project Tools/BcpackBptcCheck: 776 checks, 0 failed.
    • fixtures.json holds 204 synthetic BCPack BC6H/BC7 chunks built from random lane symbols with the encoder in Leonida's tests/test_bcpack.py (all 14 BC6H modes, all 285 BC7 signatures, in-chunk delta chains up to 300 blocks, history reads, inferred signatures, anchor and p-bit failures). For each chunk and each of strict/provisional mode, the output of tools/bcpack.py (bytes, provisional labels, or failure kind) was recorded and the C# result must match exactly. make_fixtures.py --leonida <checkout> regenerates the file (fixed seed, about 5 minutes).
    • A synthetic RDS5 pool (written by SyntheticPool.cs: one 64x64 BC7 texture, 4 mips, one raw chunk with the small mips and one BCPack chunk with mip 0) checks the fallback end to end: a clean chunk exports 4 mips with the reference blocks; a chunk that reads built-in history exports 3 mips in strict mode (same small mips as the clean export), 4 mips with allowProvisional, and the preview picks the 32x32 mip.
  • Before/after: the same check on the unmodified tree fails (codecs 6/7 throw "not supported" for every chunk).
  • I also broke the new code on purpose in several places (BC6H mode-14 bit order, BC7 rotation bits, p-bit and anchor status, the loose-block limits, the fallback condition) and the check failed each time.
  • Not verified: anything on real textures (no game data here), the WPF App project (Windows only, not built), and Tools/IndexBuilder (unchanged API, not run).

Checklist

  • No game data, keys or decryption: fixtures are random synthetic streams plus their reference decode.
  • Builds without warnings.
  • Check added in Tools/BcpackBptcCheck (the Tests/ folder is git-ignored in this repo).

Prepared with the help of an AI coding agent; reviewed and tested as above.

**@⁠tedin7** on GitHub · 2026-11-22 23:16 UTC · [original](https://github.com/breadbbbb/rpf9-explorer/pull/23) `tedin7:asset-315` → `main` · +1365 −22 · 13 files · 1 commits · closed without merging Claimed in the Leonida coordination issue as: ASSET-315 (https://github.com/ItsNotRexed/Leonida/issues/3918#issuecomment-7087083876) ### What and why `TtpfReader` could not decode BCPack codec 6 (BC6H) and codec 7 (BC7) chunks, which Leonida's pure-Python `tools/bcpack.py` already reads. This ports the two block rebuilders and keeps the lower-mip fallback (Leonida `docs/textures.md`, "Next step" 1). - `Core/BcpackBc6h.cs`, `Core/BcpackBc7.cs`: ports of `_bc6h_blocks` and `_bc7_check`/`_bc7_rebuild`/`_bc7_blocks` (strict and provisional behaviour, same limits, same provisional labels). `Core/BcpackBptcTables.cs` holds the public D3D11/Khronos partition and anchor tables and the BC7 lane-15 signature permutation (same values as `tools/bcpack_tables_bptc.py`). - BCPack's built-in 256-entry initial-history tables for BC6H/BC7 are **not** included (they are not published; this is the `history=None` case of the Python tool). A block whose delta reaches before the start of the chunk reads that history: strict mode refuses the chunk (`BcpackProvisionalException`), provisional mode decodes it against an all-zero history while at most `max(2, blocks / 1024)` blocks depend on it. Chunks whose delta chains stay inside the chunk decode exactly. Per the Leonida notes, real chunks almost all read built-in history, so on real pools strict mode will mostly still refuse BC6H/BC7 until external history tables are wired in (the separate "Next step" 2). I could not check that here: there is no game data. - `BcpackDecoder`: codecs 6 and 7 are enabled; the class is `partial` and `DecodeZigZag`/`LooseUnknownBlockLimit` are `internal` so the rebuilders can share them. - `TtpfReader`: because BC6H/BC7 chunks now count as decodable at index time, a mip whose chunk strict mode refuses is handled when the texture is read: `CreateDds` drops that mip and every larger mip and exports from the next smaller one (the DDS header and size follow), and `DecodeTexturePreview`/`DecodeTexturePixels` use the next smaller mip. This also covers BC1/BC3/BC4/BC5 chunks that need unconfirmed history, which used to fail the whole export. Provisional mode does not fall back. If every mip is refused, `CreateDds` throws the `BcpackProvisionalException`. - READMEs and the archive description text no longer say BC6H/BC7 are unsupported. Behaviour changes to be aware of: BC6H/BC7 textures are now listed as exportable with their full mip count instead of "lower-mip-only", and when the fallback applies the DDS is shorter than the `DdsSize` listed in the catalog (`ArchiveReader` shows the listed size but does not check the length). Making the listing exact would need a decode pass at index time, which I did not want to add to `Inspect`. Not in this PR: the `Patterns[2]` fix (`Core/TtpfReader.cs:70`) and the external `--history` tables. ### Tested Environment: Linux, .NET SDK 8.0 (`/root/.dotnet`), no Windows, no GPU, no game data. - `dotnet build Core/RpfPreview.Core.csproj -c Release -m:2`: 0 warnings, 0 errors (`TreatWarningsAsErrors` is on). - `dotnet run -c Release --project Tools/BcpackBptcCheck`: **776 checks, 0 failed**. - `fixtures.json` holds 204 synthetic BCPack BC6H/BC7 chunks built from random lane symbols with the encoder in Leonida's `tests/test_bcpack.py` (all 14 BC6H modes, all 285 BC7 signatures, in-chunk delta chains up to 300 blocks, history reads, inferred signatures, anchor and p-bit failures). For each chunk and each of strict/provisional mode, the output of `tools/bcpack.py` (bytes, provisional labels, or failure kind) was recorded and the C# result must match exactly. `make_fixtures.py --leonida <checkout>` regenerates the file (fixed seed, about 5 minutes). - A synthetic RDS5 pool (written by `SyntheticPool.cs`: one 64x64 BC7 texture, 4 mips, one raw chunk with the small mips and one BCPack chunk with mip 0) checks the fallback end to end: a clean chunk exports 4 mips with the reference blocks; a chunk that reads built-in history exports 3 mips in strict mode (same small mips as the clean export), 4 mips with `allowProvisional`, and the preview picks the 32x32 mip. - Before/after: the same check on the unmodified tree fails (codecs 6/7 throw "not supported" for every chunk). - I also broke the new code on purpose in several places (BC6H mode-14 bit order, BC7 rotation bits, p-bit and anchor status, the loose-block limits, the fallback condition) and the check failed each time. - Not verified: anything on real textures (no game data here), the WPF `App` project (Windows only, not built), and `Tools/IndexBuilder` (unchanged API, not run). ### Checklist - [x] No game data, keys or decryption: fixtures are random synthetic streams plus their reference decode. - [x] Builds without warnings. - [x] Check added in `Tools/BcpackBptcCheck` (the `Tests/` folder is git-ignored in this repo). Prepared with the help of an AI coding agent; reviewed and tested as above.
BcpackDecoder now rebuilds codec 6 (BC6H) and codec 7 (BC7) chunks, ported from
tools/bcpack.py in Leonida (Core/BcpackBc6h.cs, Core/BcpackBc7.cs and the public
BPTC partition/anchor/signature tables in Core/BcpackBptcTables.cs). BCPack's
built-in initial-history tables are not included, so a chunk whose delta chains
read them is refused in strict mode and decoded against an all-zero history in
provisional mode, exactly like history=None in the Python tool.

TtpfReader keeps the lower-mip fallback: when strict mode refuses a mip, CreateDds
drops it and every larger mip and exports from the next smaller one, and the
preview/pixel readers pick the next smaller mip.

Tools/BcpackBptcCheck compares the C# output with the reference decoder on
synthetic streams (make_fixtures.py generates fixtures.json) and exercises the
fallback on a synthetic RDS5 pool. No game data.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 19:05:45 +00:00
github-bot closed this pull request 2026-11-26 19:05:45 +00:00
@ -120,0 +123,4 @@
try { return WriteDds(input, source, native, first, end, cache, cancellationToken, allowProvisional, ref reading); }
// Strict mode refuses a BCPack chunk that needs unconfirmed or unknown built-in history. The mip it
// belongs to and every larger mip are dropped and the export starts at the next smaller mip.
catch (BcpackProvisionalException) when (!allowProvisional && reading + 1 < end) { first = reading + 1; }
Author
Owner

@⁠breadbbbb on GitHub · 2026-11-23 01:14 UTC · original

[P1] Preserve a readable top mip when smaller mips are refused

The fallback only moves first toward smaller mip levels. A pool with a valid raw 64x64 top mip and an unknown-history BC7 chunk for mips 1 through 3 previously exported a 64x64 one-mip DDS (4,244 bytes). With this PR, Inspect selects all four mips; CreateDds then fails on mip 1, retries mips 2 and 3, and finally throws BcpackProvisionalException, even though mip 0 is still readable. This is a regression from the current top-only export. Select a valid contiguous mip window, including a top-only window when lower levels cannot be decoded, instead of always discarding every larger mip after a refusal.

**@⁠breadbbbb** on GitHub · 2026-11-23 01:14 UTC · [original](https://github.com/breadbbbb/rpf9-explorer/pull/23#discussion_r4660965896) [P1] Preserve a readable top mip when smaller mips are refused The fallback only moves first toward smaller mip levels. A pool with a valid raw 64x64 top mip and an unknown-history BC7 chunk for mips 1 through 3 previously exported a 64x64 one-mip DDS (4,244 bytes). With this PR, Inspect selects all four mips; CreateDds then fails on mip 1, retries mips 2 and 3, and finally throws BcpackProvisionalException, even though mip 0 is still readable. This is a regression from the current top-only export. Select a valid contiguous mip window, including a top-only window when lower levels cannot be decoded, instead of always discarding every larger mip after a refusal.
@ -120,0 +130,4 @@
private static byte[] WriteDds(FileStream input, Index source, Native native, int first, int end, Dictionary<int, byte[]> cache,
CancellationToken cancellationToken, bool allowProvisional, ref int reading)
{
var t = native.Info with { BaseMipLevel = first, Mips = end - first }; var layout = native.Layout!;
Author
Owner

@⁠breadbbbb on GitHub · 2026-11-23 01:14 UTC · original

[P2] Expose the effective mip window to export callers

Changing BaseMipLevel/Mips only on this local copy leaves the catalog and every consumer with the original shape. The supplied history-refusal fixture lists 64x64, four mips, base level 0 and 5,588 DDS bytes, but CreateDds returns 32x32, three mips and 1,492 bytes. Running Leonida rpf9dump against this PR reproduces a manifest with Mips=4 and BaseMipLevel=0 alongside that three-mip DDS; the app also labels the original export dimensions and mip count. Return the resolved export metadata and update consumers, or keep fallback an explicit operation whose changed dimensions are reported, rather than silently changing the existing byte-only export contract.

**@⁠breadbbbb** on GitHub · 2026-11-23 01:14 UTC · [original](https://github.com/breadbbbb/rpf9-explorer/pull/23#discussion_r4660965900) [P2] Expose the effective mip window to export callers Changing BaseMipLevel/Mips only on this local copy leaves the catalog and every consumer with the original shape. The supplied history-refusal fixture lists 64x64, four mips, base level 0 and 5,588 DDS bytes, but CreateDds returns 32x32, three mips and 1,492 bytes. Running Leonida rpf9dump against this PR reproduces a manifest with Mips=4 and BaseMipLevel=0 alongside that three-mip DDS; the app also labels the original export dimensions and mip count. Return the resolved export metadata and update consumers, or keep fallback an explicit operation whose changed dimensions are reported, rather than silently changing the existing byte-only export contract.
Author
Owner

@⁠breadbbbb on GitHub · 2026-11-23 01:14 UTC · original · requested changes

Checked 87acdd5. The 776 supplied checks, 816 additional bounded-input checks, and App/IndexBuilder builds pass, but two export regressions need fixing before merge. A valid top-only export can now be lost when the smaller mips require unknown history, and a successful fallback can leave the catalog and Leonida manifest describing a different mip window from the returned DDS. Both are reproduced; see the inline comments. Native BC6H/BC7 pool parity was not verified because the original game archives are unavailable.

The description explicitly identifies these rebuilders as ports of Leonida functions. Please also document source attribution and the applicable licensing/permission for those ports when revising this; Leonida publishes a GPLv3 license: github.com/ItsNotRexed/Leonida@aa2988cc55/LICENSE . This provenance question is separate from the reproduced technical regressions.

**@⁠breadbbbb** on GitHub · 2026-11-23 01:14 UTC · [original](https://github.com/breadbbbb/rpf9-explorer/pull/23#pullrequestreview-6015566263) · **requested changes** Checked 87acdd5. The 776 supplied checks, 816 additional bounded-input checks, and App/IndexBuilder builds pass, but two export regressions need fixing before merge. A valid top-only export can now be lost when the smaller mips require unknown history, and a successful fallback can leave the catalog and Leonida manifest describing a different mip window from the returned DDS. Both are reproduced; see the inline comments. Native BC6H/BC7 pool parity was not verified because the original game archives are unavailable. The description explicitly identifies these rebuilders as ports of Leonida functions. Please also document source attribution and the applicable licensing/permission for those ports when revising this; Leonida publishes a GPLv3 license: https://github.com/ItsNotRexed/Leonida/blob/aa2988cc555bad7ecd508b48a702adbb32fe7c95/LICENSE . This provenance question is separate from the reproduced technical regressions.
Author
Owner

@⁠tedin7 on GitHub · 2026-11-23 01:26 UTC · original

Thanks for the careful review and the reproductions. Both regressions are real; I will fix them (keep a readable top-only window when lower mips are refused, and return the effective mip window so the catalog, the app and Leonida's manifest describe the DDS that was actually written) once the provenance question below is settled, since it decides whether these files can go in at all.

On provenance, plainly: Core/BcpackBc6h.cs and Core/BcpackBc7.cs are my C# ports of functions in Leonida's tools/bcpack.py (_bc6h_blocks, _bc7_check, _bc7_rebuild, _bc7_blocks), which other Leonida contributors wrote and which are published under GPL-3.0. A port is a derivative work, so these files have to stay under GPL-3.0 with attribution to Leonida; I cannot offer them under other terms. Core/BcpackBptcTables.cs holds the public D3D11/Khronos BPTC partition and anchor tables plus the BC7 lane-15 permutation taken from Leonida's tools/bcpack_tables_bptc.py, so I would treat it the same way.

rpf9-explorer has no license file today, so the question is yours: are you willing to take these files under GPL-3.0 (a header with the Leonida source, commit and license in each file, and a note in the README; it would also make the app as distributed GPL-3.0)? If yes, I will add that together with the two fixes. If not, I will close this PR, and the Leonida side (#4074) will be withdrawn too. Either answer is fine.

**@⁠tedin7** on GitHub · 2026-11-23 01:26 UTC · [original](https://github.com/breadbbbb/rpf9-explorer/pull/23#issuecomment-7089146012) Thanks for the careful review and the reproductions. Both regressions are real; I will fix them (keep a readable top-only window when lower mips are refused, and return the effective mip window so the catalog, the app and Leonida's manifest describe the DDS that was actually written) once the provenance question below is settled, since it decides whether these files can go in at all. On provenance, plainly: `Core/BcpackBc6h.cs` and `Core/BcpackBc7.cs` are my C# ports of functions in Leonida's `tools/bcpack.py` (`_bc6h_blocks`, `_bc7_check`, `_bc7_rebuild`, `_bc7_blocks`), which other Leonida contributors wrote and which are published under GPL-3.0. A port is a derivative work, so these files have to stay under GPL-3.0 with attribution to Leonida; I cannot offer them under other terms. `Core/BcpackBptcTables.cs` holds the public D3D11/Khronos BPTC partition and anchor tables plus the BC7 lane-15 permutation taken from Leonida's `tools/bcpack_tables_bptc.py`, so I would treat it the same way. rpf9-explorer has no license file today, so the question is yours: are you willing to take these files under GPL-3.0 (a header with the Leonida source, commit and license in each file, and a note in the README; it would also make the app as distributed GPL-3.0)? If yes, I will add that together with the two fixes. If not, I will close this PR, and the Leonida side (#4074) will be withdrawn too. Either answer is fine.
Author
Owner

@⁠breadbbbb on GitHub · 2026-11-23 02:51 UTC · original

Thanks for clarifying the provenance and confirming both export regressions.

The decision is to decline these GPL-3.0 ports and keep rpf9-explorer's current licensing position unchanged. We are not adopting GPL-3.0 for the application through this PR. Closing this proposal without merging, as you offered.

A future proposal would need compatible distribution terms and fixes for the two reproduced export regressions. Thank you for the contribution and the clear explanation.

**@⁠breadbbbb** on GitHub · 2026-11-23 02:51 UTC · [original](https://github.com/breadbbbb/rpf9-explorer/pull/23#issuecomment-7090274655) Thanks for clarifying the provenance and confirming both export regressions. The decision is to decline these GPL-3.0 ports and keep rpf9-explorer's current licensing position unchanged. We are not adopting GPL-3.0 for the application through this PR. Closing this proposal without merging, as you offered. A future proposal would need compatible distribution terms and fixes for the two reproduced export regressions. Thank you for the contribution and the clear explanation.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.