BCPack: read BC7 dictionary indices from the right offset #658

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

@⁠coderanonymus on GitHub · 2026-11-24 06:24 UTC · original
coderanonymus:bcpack-bc7-dictionary-indices → main · +18 −3 · 1 files · 1 commits · closed without merging

What

Tables::parse reads each 116-byte BC7 dictionary entry as u16 ctrl, 24 u16, 2 u8 bits, 32 i16. That layout does not match the shipped bcpack-tables.bin. After the two width bytes at 50..52, every entry has two more bytes: 04 00 in 253 entries, the same as the widths (entry 0 has 02 00). The 16 set-0 indices start at byte 54, not 52.

So the current parse is off by one index:

  • Pixel 0 of set 0 is 4 in 253 of 255 entries. That is the width, not a pixel.
  • Pixel 1 is never negative (always 1..6). Only the anchor (pixel 0) has that property.
  • Set 0's last pixel lands in ix[1][0]. It is nonzero in 233 entries, even though bits[1] is 0 in every entry. Bytes 86..116 are zero in all 255 entries.

bc7() adds residuals to rx.ix[set][p] from the dictionary when the index reference points past the history. So for those blocks, pixel 0 is built on 4 and pixel p on the dictionary's pixel p-1. Each DirectStorage BCPack request is its own stream, so the first 255 blocks of every stream can reach this. Small BC7 mips sit almost entirely in that range.

Change: skip the 2 bytes after the widths, then read 16 i16 for set 0 and 15 for set 1. TABLES_SIZE is unchanged (2 + 2 + 32 + 30 = 66). The layout doc comment now says BC7 (u16 control, 24 u16, 2 u8, 2 B, 16 + 15 i16).

New test bcpack::tests::bc7_dictionary_indices_line_up checks the shipped table: a set with 0 bits holds no indices, and pixel 0 of set 0 fits the anchor range 0..1 << (bits - 1).

Verification

All with CARGO_TARGET_DIR=/home/eric/src/VIctory/target.

  • Before the fix: cargo test --target x86_64-unknown-linux-gnu -p victory-formats --lib bc7_dictionary_indices_line_up fails:
    entry 1: [[3, 1, 0, 0, 0, 0, 0, -1, -1, 0, -1, 0, 0, 0, -1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
    The leading 3 is entry 1's width, and the 1 in set 1 is set 0's last pixel.
  • After the fix: cargo test --target x86_64-unknown-linux-gnu -p victory-formats: 21 passed, 0 failed, 3 ignored (reference_chunks and two xma tests that need outside files).
  • cargo clippy -p victory-formats --tests --target x86_64-pc-windows-msvc -- -D warnings: clean. cargo clippy -p victory-hle --target x86_64-pc-windows-msvc builds, with one warning already on main in xmem.rs.
  • rustfmt --edition 2024 --check crates/victory-formats/src/bcpack.rs: clean.

Not run: reference_chunks. It needs BC7 reference chunks from outside the repo (VICTORY_BCPACK_CASES), so decoded texels were not compared with real output. The fix rests on the table's own structure. No game was run.

No game data used.

🤖 Generated with Claude Code

**@⁠coderanonymus** on GitHub · 2026-11-24 06:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/658) `coderanonymus:bcpack-bc7-dictionary-indices` → `main` · +18 −3 · 1 files · 1 commits · closed without merging ## What `Tables::parse` reads each 116-byte BC7 dictionary entry as `u16 ctrl, 24 u16, 2 u8 bits, 32 i16`. That layout does not match the shipped `bcpack-tables.bin`. After the two width bytes at 50..52, every entry has two more bytes: `04 00` in 253 entries, the same as the widths (entry 0 has `02 00`). The 16 set-0 indices start at byte 54, not 52. So the current parse is off by one index: - Pixel 0 of set 0 is 4 in 253 of 255 entries. That is the width, not a pixel. - Pixel 1 is never negative (always 1..6). Only the anchor (pixel 0) has that property. - Set 0's last pixel lands in `ix[1][0]`. It is nonzero in 233 entries, even though `bits[1]` is 0 in every entry. Bytes 86..116 are zero in all 255 entries. `bc7()` adds residuals to `rx.ix[set][p]` from the dictionary when the index reference points past the history. So for those blocks, pixel 0 is built on 4 and pixel p on the dictionary's pixel p-1. Each DirectStorage BCPack request is its own stream, so the first 255 blocks of every stream can reach this. Small BC7 mips sit almost entirely in that range. Change: skip the 2 bytes after the widths, then read 16 i16 for set 0 and 15 for set 1. `TABLES_SIZE` is unchanged (2 + 2 + 32 + 30 = 66). The layout doc comment now says `BC7 (u16 control, 24 u16, 2 u8, 2 B, 16 + 15 i16)`. New test `bcpack::tests::bc7_dictionary_indices_line_up` checks the shipped table: a set with 0 bits holds no indices, and pixel 0 of set 0 fits the anchor range `0..1 << (bits - 1)`. ## Verification All with `CARGO_TARGET_DIR=/home/eric/src/VIctory/target`. - Before the fix: `cargo test --target x86_64-unknown-linux-gnu -p victory-formats --lib bc7_dictionary_indices_line_up` fails: `entry 1: [[3, 1, 0, 0, 0, 0, 0, -1, -1, 0, -1, 0, 0, 0, -1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]` The leading 3 is entry 1's width, and the 1 in set 1 is set 0's last pixel. - After the fix: `cargo test --target x86_64-unknown-linux-gnu -p victory-formats`: 21 passed, 0 failed, 3 ignored (`reference_chunks` and two xma tests that need outside files). - `cargo clippy -p victory-formats --tests --target x86_64-pc-windows-msvc -- -D warnings`: clean. `cargo clippy -p victory-hle --target x86_64-pc-windows-msvc` builds, with one warning already on main in `xmem.rs`. - `rustfmt --edition 2024 --check crates/victory-formats/src/bcpack.rs`: clean. Not run: `reference_chunks`. It needs BC7 reference chunks from outside the repo (`VICTORY_BCPACK_CASES`), so decoded texels were not compared with real output. The fix rests on the table's own structure. No game was run. No game data used. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Each BC7 dictionary entry has two more bytes after the two index widths.
They repeat the widths in all entries but the first. The parser read
them as pixel 0 of set 0, so every index was off by one: pixel 0 was 4
in 253 of 255 entries, and set 0's last pixel landed in set 1, which no
entry uses. Any BC7 block whose index reference points into the
dictionary was built on the wrong indices.

Skip the two bytes and read 16 indices for set 0 and 15 for set 1. The
table size does not change. The new test checks the shipped table: a
set with 0 bits holds no indices, and pixel 0 is a valid anchor.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:11:48 +00:00
github-bot closed this pull request 2026-11-26 04:11:48 +00:00
Author
Owner

@⁠pickles976 on GitHub · 2026-11-24 06:26 UTC · original

Heads-up across the fence: the format knowledge in this PR is exactly what the sibling runtime (ivall/xcl) still refuses to decode — dictionary-backed BC blocks are its next measured wall. I have filed it on their tracker (ivall/xcl#832) as a format note: layout, the two misalignment traps you caught (redundant width u16; 15-slot set 1), your tiling invariant and anchor semantics — described, no code lifted given the GPL-2/GPL-3 mismatch, with attribution here. Your PR quietly just became the format reference for both projects.

**@⁠pickles976** on GitHub · 2026-11-24 06:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/658#issuecomment-7123930417) Heads-up across the fence: the format knowledge in this PR is exactly what the sibling runtime (ivall/xcl) still refuses to decode — dictionary-backed BC blocks are its next measured wall. I have filed it on their tracker (ivall/xcl#832) as a format note: layout, the two misalignment traps you caught (redundant width u16; 15-slot set 1), your tiling invariant and anchor semantics — described, **no code lifted** given the GPL-2/GPL-3 mismatch, with attribution here. Your PR quietly just became the format reference for both projects.
Author
Owner

@⁠molodayamama on GitHub · 2026-11-24 14:03 UTC · original

Independent Windows check of 0ecaa8cb92ac86e9721f9a0870788fe326b645bd (Windows 10 22H2 19045, Rust 1.98.1, MSVC x64).

  • cargo test -p victory-formats --locked --lib bc7_dictionary_indices_line_up -- --test-threads=1 --nocapture: 1 passed.

The focused synthetic test checks alignment of the BC7 dictionary indices, including the lookup offset used by the decoder. No external reference chunks were required.

Only source-built synthetic fixtures were used. AI-assisted verification.

**@⁠molodayamama** on GitHub · 2026-11-24 14:03 UTC · [original](https://github.com/Marvisak/VIctory/pull/658#issuecomment-7133770082) Independent Windows check of `0ecaa8cb92ac86e9721f9a0870788fe326b645bd` (Windows 10 22H2 19045, Rust 1.98.1, MSVC x64). - `cargo test -p victory-formats --locked --lib bc7_dictionary_indices_line_up -- --test-threads=1 --nocapture`: 1 passed. The focused synthetic test checks alignment of the BC7 dictionary indices, including the lookup offset used by the decoder. No external reference chunks were required. Only source-built synthetic fixtures were used. AI-assisted verification.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!658
No description provided.