BCPack: read BC7 dictionary indices where each entry has them #2161

Closed
0xkn0wh3r3 wants to merge 2 commits from upstream-pr/2163 into main
0xkn0wh3r3 commented 2026-11-25 06:42:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by 0xkn0wh3r3 on 2026-11-25T06:42:17Z.


Problem

Tables::parse reads 32 indices straight after each BC7 dictionary entry's two index-width bytes. The built-in table file (bcpack-tables.bin) has another layout: entries 1 to 254 repeat their two width bytes before 31 indices. So every one of those entries' indices is read two bytes early, and BCPack BC7 blocks that come from the dictionary decode with the wrong indices.

Entry 0 is the exception: its 32 indices start right after its widths. Checked on the built-in file: the u16 after the widths equals the width bytes in 254 entries. In entry 0 (control 0, widths [4, 0]) it is 2, the first of its indices 2, 1, 0, ...

Fix

Two commits:

  • 5f0936d is #658 by coderanonymus, unchanged (cherry-picked onto current main): skip the repeated widths and read 31 indices (set 1's 16th slot is unused; no entry gives set 1 index bits). #658 was closed on 2026-11-24 without being merged and without a stated reason; it had an independent Windows check. If it was closed for a reason this misses, please say.
  • cd4402f adds the exception: entry 0 reads its 32 indices directly. With #658 alone, entry 0 lost its leading 2 and its other indices shifted. XCL's notes report the same layout (their tables were generated from this file; their #2007).

Testing

  • bc7_dictionary_indices_line_up (from #658) checks every entry: pixel 0 is an anchor (top bit clear), and a set with 0 index bits holds no indices. It now also checks entry 0: control 0, widths [4, 0], indices beginning 2, 1, 0.
  • The test catches both misreads: with main's parser it fails at entry 1 (its first index is the repeated width bytes); with #658 alone it fails on entry 0 ([1, 0, 0] where [2, 1, 0] is wanted); with both commits it passes.
  • cargo test -p victory-formats: 39 passed; cargo test -p victory-hle (DirectStorage's BCPack path): all pass. cargo clippy --workspace --all-targets, with and without --features diagnostics: clean.
  • Not checked against game textures here (no game data).
<!-- greyfinch-sync:Marvisak/VIctory:issue:6598846749 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2163). Originally posted by [0xkn0wh3r3](https://github.com/0xkn0wh3r3) on 2026-11-25T06:42:17Z. --- ### Problem `Tables::parse` reads 32 indices straight after each BC7 dictionary entry's two index-width bytes. The built-in table file (`bcpack-tables.bin`) has another layout: entries 1 to 254 repeat their two width bytes before 31 indices. So every one of those entries' indices is read two bytes early, and BCPack BC7 blocks that come from the dictionary decode with the wrong indices. Entry 0 is the exception: its 32 indices start right after its widths. Checked on the built-in file: the u16 after the widths equals the width bytes in 254 entries. In entry 0 (control 0, widths [4, 0]) it is 2, the first of its indices 2, 1, 0, ... ### Fix Two commits: - `5f0936d` is #658 by coderanonymus, unchanged (cherry-picked onto current `main`): skip the repeated widths and read 31 indices (set 1's 16th slot is unused; no entry gives set 1 index bits). #658 was closed on 2026-11-24 without being merged and without a stated reason; it had an independent Windows check. If it was closed for a reason this misses, please say. - `cd4402f` adds the exception: entry 0 reads its 32 indices directly. With #658 alone, entry 0 lost its leading 2 and its other indices shifted. XCL's notes report the same layout (their tables were generated from this file; their #2007). ### Testing - `bc7_dictionary_indices_line_up` (from #658) checks every entry: pixel 0 is an anchor (top bit clear), and a set with 0 index bits holds no indices. It now also checks entry 0: control 0, widths [4, 0], indices beginning 2, 1, 0. - The test catches both misreads: with `main`'s parser it fails at entry 1 (its first index is the repeated width bytes); with #658 alone it fails on entry 0 (`[1, 0, 0]` where `[2, 1, 0]` is wanted); with both commits it passes. - `cargo test -p victory-formats`: 39 passed; `cargo test -p victory-hle` (DirectStorage's BCPack path): all pass. `cargo clippy --workspace --all-targets`, with and without `--features diagnostics`: clean. - Not checked against game textures here (no game data).
Dathinel commented 2026-11-25 07:03:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dathinel on 2026-11-25T06:53:16Z.


I checked the effect on the final 16-byte BC7 block using synthetic data through decode(7, ...). The two one-block streams reference dictionary entries 0 and 1 respectively. I used mode 6 with distinct endpoints and pixel indices (3 * pixel + entry + 1) % 16, and compared all 128 bits against the expected endpoint fields, anchor and remaining indices.

Decoder Entry 0 Entry 1
Parent 940d32f matches differs
First commit 5f0936d differs matches
PR head cd4402f matches matches

The synthetic table intentionally repeats the widths for entry 1 only. This verifies why both changes matter for the two stated layouts; it does not independently establish which layout an external encoder uses. No game assets. AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7155262261 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2163#issuecomment-7155262261). Originally posted by [Dathinel](https://github.com/Dathinel) on 2026-11-25T06:53:16Z. --- I checked the effect on the **final 16-byte BC7 block** using synthetic data through `decode(7, ...)`. The two one-block streams reference dictionary entries 0 and 1 respectively. I used mode 6 with distinct endpoints and pixel indices `(3 * pixel + entry + 1) % 16`, and compared all 128 bits against the expected endpoint fields, anchor and remaining indices. | Decoder | Entry 0 | Entry 1 | |---|---|---| | Parent `940d32f` | matches | differs | | First commit `5f0936d` | differs | matches | | PR head `cd4402f` | matches | matches | The synthetic table intentionally repeats the widths for entry 1 only. This verifies why both changes matter for the two stated layouts; it does not independently establish which layout an external encoder uses. No game assets. AI-assisted.
DenRakEiw commented 2026-11-25 08:06:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T07:24:37Z.

Review: APPROVED


Verified locally merged onto origin/main (940d32f, Windows 11 x64, Rust 1.98.1 MSVC):

  • cargo test -p victory-formats --lib bcpack:: — 10 passed, 0 failed (2 ignored), including bc7_dictionary_indices_line_up
  • cargo clippy -p victory-formats --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean fix reading BC7 dictionary indices at each entry's actual location with targeted regression test. Looks good.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6053419111 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2163#pullrequestreview-6053419111). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T07:24:37Z. **Review:** APPROVED --- Verified locally merged onto `origin/main` (`940d32f`, Windows 11 x64, Rust 1.98.1 MSVC): - `cargo test -p victory-formats --lib bcpack::` — 10 passed, 0 failed (2 ignored), including `bc7_dictionary_indices_line_up` - `cargo clippy -p victory-formats --all-targets` — 0 warnings - `cargo xtask policy` — ok (`183 files ok`) Clean fix reading BC7 dictionary indices at each entry's actual location with targeted regression test. Looks good. AI-assisted (Gemini 4 Neon).
catminou4 commented 2026-11-25 08:06:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T07:24:39Z.

Review: APPROVED


Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b, cargo test -p victory-formats --locked.

39 passed / 0 failed / 5 ignored. BCPack reads BC7 dictionary indices where each entry has its own bit width — BC7 index decoding completeness; relates to #1557's BC7 subsets.

<!-- greyfinch-sync:Marvisak/VIctory:review:6053419380 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2163#pullrequestreview-6053419380). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T07:24:39Z. **Review:** APPROVED --- Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b, `cargo test -p victory-formats --locked`. 39 passed / 0 failed / 5 ignored. BCPack reads BC7 dictionary indices where each entry has its own bit width — BC7 index decoding completeness; relates to #1557's BC7 subsets.
Marvisak commented 2026-11-25 13:02:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T12:35:52Z.


Automated triage for the maintainers.

Merge candidate. Tables::parse in crates/victory-formats/src/bcpack.rs now reads the BC7 dictionary the way the built-in bcpack-tables.bin lays it out. In entries 1–254, the two index-width bytes are repeated (as a u16) before 31 indices. Entry 0 has its 32 indices straight after the widths. Each entry still takes the same number of bytes, so the table size and everything after it are unchanged.

The evidence comes from the table file itself, which is the right source here. bc7_dictionary_indices_line_up checks every entry for a structural invariant: pixel 0 is an anchor (top index bit clear), and a set with 0 index bits holds no indices. Per the description, that test fails on main at entry 1, fails on #658's version at entry 0, and passes with both commits. Dathinel's synthetic decode above shows the same thing at the level of the final 16-byte BC7 block.

CI: not run on this head. It merges cleanly with main.

For the maintainers: the first commit is #658 (coderanonymus), which was closed unmerged on 2026-11-24 with no stated reason. If that close was deliberate, this PR needs the same decision.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7162593340 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2163#issuecomment-7162593340). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T12:35:52Z. --- <!-- victory-triage v1 head=cd4402f405ec4f2c1d12fcf061221d8644121358 verdict=merge-candidate at=2026-11-25T13:37:00Z --> Automated triage for the maintainers. **Merge candidate.** `Tables::parse` in `crates/victory-formats/src/bcpack.rs` now reads the BC7 dictionary the way the built-in `bcpack-tables.bin` lays it out. In entries 1–254, the two index-width bytes are repeated (as a u16) before 31 indices. Entry 0 has its 32 indices straight after the widths. Each entry still takes the same number of bytes, so the table size and everything after it are unchanged. The evidence comes from the table file itself, which is the right source here. `bc7_dictionary_indices_line_up` checks every entry for a structural invariant: pixel 0 is an anchor (top index bit clear), and a set with 0 index bits holds no indices. Per the description, that test fails on `main` at entry 1, fails on #658's version at entry 0, and passes with both commits. Dathinel's synthetic decode above shows the same thing at the level of the final 16-byte BC7 block. CI: not run on this head. It merges cleanly with `main`. For the maintainers: the first commit is #658 (coderanonymus), which was closed unmerged on 2026-11-24 with no stated reason. If that close was deliberate, this PR needs the same decision. --- _Generated by [Claude Code](https://claude.ai/code)_
upstream-sync closed this pull request 2026-11-25 23:03:56 +00:00
Marvisak commented 2026-11-25 23:06:01 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T22:12:09Z.


Automated triage for the maintainers, second look (a review against current main, with builds and tests where they could change the verdict).

Closing because main's reading of the BC7 dictionary is the verified one. The decoder was checked byte for byte against the reference decoder on all 108,605 BCPack chunks of the title, and every BC7 chunk there references the dictionary. The observation here is real (the first primary value equals the first width in 254 entries), but it describes the table's own data, which the reference reads at those same offsets; shifting the read would make every BC7 block that takes its indices from dictionary entries 1 to 254 differ from the reference. This re-lands #658, which was closed for the same reason, and the new test asserts a rule that the dictionary's working values do not follow.

If this is mistaken, reply here with what was missed and it will be looked at again. Thank you for the contribution.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7176065731 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2163#issuecomment-7176065731). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T22:12:09Z. --- Automated triage for the maintainers, second look (a review against current `main`, with builds and tests where they could change the verdict). Closing because `main`'s reading of the BC7 dictionary is the verified one. The decoder was checked byte for byte against the reference decoder on all 108,605 BCPack chunks of the title, and every BC7 chunk there references the dictionary. The observation here is real (the first primary value equals the first width in 254 entries), but it describes the table's own data, which the reference reads at those same offsets; shifting the read would make every BC7 block that takes its indices from dictionary entries 1 to 254 differ from the reference. This re-lands #658, which was closed for the same reason, and the new test asserts a rule that the dictionary's working values do not follow. If this is mistaken, reply here with what was missed and it will be looked at again. Thank you for the contribution.

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
Marvisak/VIctory!2161
No description provided.