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

Closed
coderanonymus wants to merge 1 commit from upstream-pr/658 into main
coderanonymus commented 2026-11-24 06:24:22 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by coderanonymus on 2026-11-24T06:24:22Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:issue:6575992547 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/658). Originally posted by [coderanonymus](https://github.com/coderanonymus) on 2026-11-24T06:24:22Z. --- ## 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)
LAKSHYA-cmd-tech commented 2026-11-24 06:54:15 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by LAKSHYA-cmd-tech on 2026-11-24T06:26:07Z.

Review: APPROVED


LGTM! Tested on Windows 11 MSVC. cargo test -p victory-formats passed (21 passed, 0 failed), including c7_dictionary_indices_line_up. Table layout alignment correctly offsets the dictionary indices.

<!-- greyfinch-sync:Marvisak/VIctory:review:6034647633 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/658#pullrequestreview-6034647633). Originally posted by [LAKSHYA-cmd-tech](https://github.com/LAKSHYA-cmd-tech) on 2026-11-24T06:26:07Z. **Review:** APPROVED --- LGTM! Tested on Windows 11 MSVC. cargo test -p victory-formats passed (21 passed, 0 failed), including c7_dictionary_indices_line_up. Table layout alignment correctly offsets the dictionary indices.
pickles976 commented 2026-11-24 06:56:22 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by pickles976 on 2026-11-24T06:26:12Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7123930417 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/658#issuecomment-7123930417). Originally posted by [pickles976](https://github.com/pickles976) on 2026-11-24T06:26:12Z. --- 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.
catminou4 commented 2026-11-24 10:03:28 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T09:37:32Z.

Review: APPROVED


Merges cleanly with main; cargo test -p victory-formats --locked on macOS arm64 (merged into main 8e79c80) → 24 passed / 0 failed / 4 ignored.

The BC7 dictionary layout correction is consistent with the doc comment (16 + 15 i16 instead of 32 i16): the reader now skips the repeated widths u16 and reads 31 index words, consuming the same total bytes with the right split. The new bc7_dictionary_indices_line_up test checks the structural invariants (anchor pixel within the index range for set 0, set 1 unused when it has 0 bits) against the builtin tables, which would catch a misaligned parse. Small, focused, tested.

<!-- greyfinch-sync:Marvisak/VIctory:review:6036818069 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/658#pullrequestreview-6036818069). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T09:37:32Z. **Review:** APPROVED --- Merges cleanly with main; `cargo test -p victory-formats --locked` on macOS arm64 (merged into main `8e79c80`) → **24 passed / 0 failed / 4 ignored**. The BC7 dictionary layout correction is consistent with the doc comment (`16 + 15 i16` instead of `32 i16`): the reader now skips the repeated widths u16 and reads 31 index words, consuming the same total bytes with the right split. The new `bc7_dictionary_indices_line_up` test checks the structural invariants (anchor pixel within the index range for set 0, set 1 unused when it has 0 bits) against the builtin tables, which would catch a misaligned parse. Small, focused, tested.
mercuryVM commented 2026-11-24 11:03:49 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T10:56:46Z.

Review: APPROVED


Tested 0ecaa8c on Windows 11, RTX 5070, MSVC: cargo test -p victory-formats 21 passed, including bc7_dictionary_indices_line_up. cargo clippy -p victory-formats --all-targets is clean.

  • Independent check: I ran the same property (set-0 pixel 0 inside the anchor range, set 1 empty when bits[1] == 0) against the current parse on mercuryVM/VIctory main. It fails for 233 of the 255 BC7 dictionary entries, and passes for all of them with this parse. So the off-by-one is real, and it affects every BC7 block that references the dictionary.
  • TABLES_SIZE is unchanged: 2 skipped bytes + 31 × i16 replace 32 × i16, so the entry stays 116 bytes and the shipped file still parses.
  • Heads-up for ordering: #703 (BCPack streaming rewrite) leaves Tables alone, so this fix still applies after it. The two only collide textually where both append tests at the end of mod tests.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6037951843 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/658#pullrequestreview-6037951843). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T10:56:46Z. **Review:** APPROVED --- Tested 0ecaa8c on Windows 11, RTX 5070, MSVC: `cargo test -p victory-formats` 21 passed, including `bc7_dictionary_indices_line_up`. `cargo clippy -p victory-formats --all-targets` is clean. - Independent check: I ran the same property (set-0 pixel 0 inside the anchor range, set 1 empty when `bits[1] == 0`) against the current parse on mercuryVM/VIctory main. It fails for 233 of the 255 BC7 dictionary entries, and passes for all of them with this parse. So the off-by-one is real, and it affects every BC7 block that references the dictionary. - `TABLES_SIZE` is unchanged: 2 skipped bytes + 31 × i16 replace 32 × i16, so the entry stays 116 bytes and the shipped file still parses. - Heads-up for ordering: #703 (BCPack streaming rewrite) leaves `Tables` alone, so this fix still applies after it. The two only collide textually where both append tests at the end of `mod tests`. AI-assisted (Claude Code).
molodayamama commented 2026-11-24 14:05:01 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by molodayamama on 2026-11-24T14:03:31Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7133770082 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/658#issuecomment-7133770082). Originally posted by [molodayamama](https://github.com/molodayamama) on 2026-11-24T14:03:31Z. --- 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.
upstream-sync closed this pull request 2026-11-24 23:04:19 +00:00

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!658
No description provided.