Formats: fix BC7 dictionary layout and mode 4/5 rotation guards #3386

Open
legayclightning22 wants to merge 1 commit from upstream-pr/3386 into main
legayclightning22 commented 2026-11-26 21:19:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by legayclightning22 on 2026-11-26T21:19:45Z.


Summary

Fixes BC7 dictionary layout parsing and restricts channel rotation to BC7 modes 4 and 5 in crates/victory-formats/src/bcpack.rs (817313d, +125/-5).

Changes

  • Fix Tables::parse, rotation, bc7_precision, and bc7_pack so BC7 dictionary indices align and channel rotation is applied only to modes 4 and 5.
  • Add unit tests bc7_rotation_applies_only_to_modes_4_and_5 and bc7_dictionary_indices_line_up.

Verification

  • cargo test -p victory-formats --lib bcpack::
  • cargo clippy --locked -p victory-formats --all-targets -- -D warnings
  • cargo xtask policy
<!-- greyfinch-sync:Marvisak/VIctory:issue:5517282504 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3386). Originally posted by [legayclightning22](https://github.com/legayclightning22) on 2026-11-26T21:19:45Z. --- ## Summary Fixes BC7 dictionary layout parsing and restricts channel rotation to BC7 modes 4 and 5 in `crates/victory-formats/src/bcpack.rs` (`817313d`, `+125/-5`). ## Changes - Fix `Tables::parse`, `rotation`, `bc7_precision`, and `bc7_pack` so BC7 dictionary indices align and channel rotation is applied only to modes 4 and 5. - Add unit tests `bc7_rotation_applies_only_to_modes_4_and_5` and `bc7_dictionary_indices_line_up`. ## Verification - `cargo test -p victory-formats --lib bcpack::` - `cargo clippy --locked -p victory-formats --all-targets -- -D warnings` - `cargo xtask policy`
Dukestar69 commented 2026-11-26 21:28:22 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T21:28:22Z.

Review: APPROVED


LGTM — verified on Windows against origin/main (63bcca4).

  • Fixing the BC7 dictionary layout alignment and restricting BC7 rotation handling to modes 4 and 5 is cleanly verified by unit tests.
  • cargo test -p victory-formats --lib bcpack:: -- --test-threads=1: 12 passed, 0 failed (2 ignored; including bc7_dictionary_indices_line_up and bc7_rotation_applies_only_to_modes_4_and_5).
  • cargo clippy -p victory-formats --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6080640304 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3386#pullrequestreview-6080640304). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T21:28:22Z. **Review:** APPROVED --- LGTM — verified on Windows against `origin/main` (`63bcca4`). - Fixing the BC7 dictionary layout alignment and restricting BC7 rotation handling to modes 4 and 5 is cleanly verified by unit tests. - `cargo test -p victory-formats --lib bcpack:: -- --test-threads=1`: 12 passed, 0 failed (2 ignored; including `bc7_dictionary_indices_line_up` and `bc7_rotation_applies_only_to_modes_4_and_5`). - `cargo clippy -p victory-formats --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
fuzeofficlal commented 2026-11-26 21:30:41 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T21:30:41Z.

Review: CHANGES_REQUESTED


Requesting a fix on the BC7 dictionary parser (817313d vs 63bcca43) — the Mode 4/5 rotation handling matches Microsoft's BC7 layout, but entry 0 of the BC7 dictionary is misread:

Blocking issue

  • [P1] Entry 0 does not have the repeated width word before its indices:
    • crates/victory-formats/src/bcpack.rs:225 notes // Unused: the widths again in all entries but the first., yet Tables::parse calls r.u16() unconditionally for all 0..DICTIONARY (255) entries.
    • Inspecting crates/victory-formats/src/bcpack-tables.bin at the start of the BC7 dictionary (0xBD12):
      • Entry 0 (+50..+63): 04 00 (bits = [4, 0]), followed immediately at +52 by 02 00 01 00 00 00 ... ([2, 1, 0, ...]).
      • Entry 1 (+50..+63): 03 00 (bits = [3, 0]), repeated 03 00 at +52, then indices 01 00 00 00 ... at +54.
    • Unconditionally discarding a u16 shifts entry 0's ix.ix[0] to [1, 0, 0, ...] (which still happens to satisfy bc7_dictionary_indices_line_up's < 1 << (bits[0] - 1) check). Reading 32 u16s from +52 for entry 0 and skipping the repeated u16 (reading 31) only for entries 1..255 fixes entry 0 while keeping the 116-byte stride intact.

Additional notes

  • Rotation (bcpack.rs:748-754, 859-882): Restricting rotation(ctrl) to BC7 modes 4 and 5 cleanly fixes the channel-precision / non-rotating-mode shift overflow and matches the BC7 Mode 4 (rotation + idxMode) and Mode 5 (rotation) bit layout (0..=3).
  • Tests: Adding an exact check on t.bc7[0].1.ix[0] (and optionally a nonzero-index Mode 4 idxMode = 1 case) would pin entry 0 directly.
  • Checks:
    • cargo test --locked -p victory-formats -- --test-threads=1: 48 passed, 0 failed, 5 ignored.
    • cargo clippy -p victory-formats --all-targets --locked -- -D warnings (default and --all-features): clean.
    • cargo xtask policy: 191 files ok; rustfmt --edition 2024: clean.
<!-- greyfinch-sync:Marvisak/VIctory:review:6080662509 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3386#pullrequestreview-6080662509). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T21:30:41Z. **Review:** CHANGES_REQUESTED --- Requesting a fix on the BC7 dictionary parser (`817313d` vs `63bcca43`) — the Mode 4/5 rotation handling matches Microsoft's BC7 layout, but entry 0 of the BC7 dictionary is misread: ### Blocking issue - **[P1] Entry 0 does not have the repeated width word before its indices**: - `crates/victory-formats/src/bcpack.rs:225` notes `// Unused: the widths again in all entries but the first.`, yet `Tables::parse` calls `r.u16()` unconditionally for all `0..DICTIONARY` (`255`) entries. - Inspecting `crates/victory-formats/src/bcpack-tables.bin` at the start of the BC7 dictionary (`0xBD12`): - Entry 0 (`+50..+63`): `04 00` (`bits = [4, 0]`), followed immediately at `+52` by `02 00 01 00 00 00 ...` (`[2, 1, 0, ...]`). - Entry 1 (`+50..+63`): `03 00` (`bits = [3, 0]`), repeated `03 00` at `+52`, then indices `01 00 00 00 ...` at `+54`. - Unconditionally discarding a `u16` shifts entry 0's `ix.ix[0]` to `[1, 0, 0, ...]` (which still happens to satisfy `bc7_dictionary_indices_line_up`'s `< 1 << (bits[0] - 1)` check). Reading 32 `u16`s from `+52` for entry 0 and skipping the repeated `u16` (reading 31) only for entries `1..255` fixes entry 0 while keeping the 116-byte stride intact. ### Additional notes - **Rotation (`bcpack.rs:748-754`, `859-882`)**: Restricting `rotation(ctrl)` to BC7 modes 4 and 5 cleanly fixes the channel-precision / non-rotating-mode shift overflow and matches the BC7 Mode 4 (`rotation` + `idxMode`) and Mode 5 (`rotation`) bit layout (`0..=3`). - **Tests**: Adding an exact check on `t.bc7[0].1.ix[0]` (and optionally a nonzero-index Mode 4 `idxMode = 1` case) would pin entry 0 directly. - **Checks**: - `cargo test --locked -p victory-formats -- --test-threads=1`: 48 passed, 0 failed, 5 ignored. - `cargo clippy -p victory-formats --all-targets --locked -- -D warnings` (default and `--all-features`): clean. - `cargo xtask policy`: 191 files ok; `rustfmt --edition 2024`: clean.
legayclightning22 commented 2026-11-26 21:53:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by legayclightning22 on 2026-11-26T21:53:19Z.


Good catch—pushed 7520d2f fixing Tables::parse so entry 0 reads 32 u16s directly without discarding its first index, while entries 1..255 skip the repeated width u16 and read 31 u16s to preserve the 116-byte stride. Also added an exact check on t.bc7[0].1.ix[0] and a Mode 4 idxMode = 1 test; re-verified victory-formats tests, clippy --all-features -- -D warnings, rustfmt, and cargo xtask policy.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7204158443 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3386#issuecomment-7204158443). Originally posted by [legayclightning22](https://github.com/legayclightning22) on 2026-11-26T21:53:19Z. --- Good catch—pushed `7520d2f` fixing `Tables::parse` so entry `0` reads 32 `u16`s directly without discarding its first index, while entries `1..255` skip the repeated width `u16` and read 31 `u16`s to preserve the 116-byte stride. Also added an exact check on `t.bc7[0].1.ix[0]` and a Mode 4 `idxMode = 1` test; re-verified `victory-formats` tests, `clippy --all-features -- -D warnings`, `rustfmt`, and `cargo xtask policy`.
fuzeofficlal commented 2026-11-26 22:08:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T22:08:16Z.

Review: APPROVED


Re-verified on 7520d2f8:

  • rustfmt --edition 2024 --check crates/victory-formats/src/bcpack.rs: clean.
  • cargo xtask policy: 191 files ok.
  • cargo clippy --locked -p victory-formats --all-targets -- -D warnings (default and --all-features): clean.
  • cargo test --locked -p victory-formats: 48 passed, 0 failed, 5 ignored (the_shipped_tables_parse, bc7_dictionary_indices_line_up, and bc7_rotation_applies_only_to_modes_4_and_5 all pass).

Verified directly against crates/victory-formats/src/bcpack-tables.bin (77,982 bytes): entry 0 at 0xBD12 reads bits = [4, 0], ix[0] = [2, 1, 0, ...], and ix[1] = [0; 16] (116 bytes total), and entries 1..254 (0xBD86..0x1309E) all have bits[1] == 0, matching duplicate width words, valid pixel-0 anchors, and exact 116-byte stride alignment to EOF. The mode 4/5 rotation guard in rotation(ctrl) is also consistent across bc7_precision and bc7_pack. Previous feedback is resolved — LGTM.

<!-- greyfinch-sync:Marvisak/VIctory:review:6080999153 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3386#pullrequestreview-6080999153). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T22:08:16Z. **Review:** APPROVED --- Re-verified on `7520d2f8`: - `rustfmt --edition 2024 --check crates/victory-formats/src/bcpack.rs`: clean. - `cargo xtask policy`: `191 files ok`. - `cargo clippy --locked -p victory-formats --all-targets -- -D warnings` (default and `--all-features`): clean. - `cargo test --locked -p victory-formats`: **48 passed, 0 failed, 5 ignored** (`the_shipped_tables_parse`, `bc7_dictionary_indices_line_up`, and `bc7_rotation_applies_only_to_modes_4_and_5` all pass). Verified directly against `crates/victory-formats/src/bcpack-tables.bin` (`77,982` bytes): entry 0 at `0xBD12` reads `bits = [4, 0]`, `ix[0] = [2, 1, 0, ...]`, and `ix[1] = [0; 16]` (116 bytes total), and entries `1..254` (`0xBD86..0x1309E`) all have `bits[1] == 0`, matching duplicate width words, valid pixel-0 anchors, and exact 116-byte stride alignment to EOF. The mode 4/5 rotation guard in `rotation(ctrl)` is also consistent across `bc7_precision` and `bc7_pack`. Previous feedback is resolved — LGTM.
passerbyyhx commented 2026-11-26 22:33:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by passerbyyhx on 2026-11-26T22:33:00Z.


I verified 7520d2f87548fc05c79a354632159e7c8f0accd7 with synthetic rANS BCPack streams through bcpack::decode. Pillow 11.2.1 independently decoded the emitted BC7, and all 480 RGBA texels across 30 blocks matched the expected pixels. The matrix covers modes 4 and 5 at all four rotations, both mode-4 index selectors, and two distinct endpoint and spatial index patterns.

The same fixture on parent 63bcca43c6a00a758510398fa43096ce7e4f7df0 disagreed on 63 texels across four blocks. The three mode-6 reserved-rotation-bit cases differed at all 16 texels each, and the second synthetic dictionary entry differed at 15. The zero-bit mode-6 control and first dictionary entry matched. Swapping the two mode-4 index streams in the revision caused 248 texel mismatches across all 16 mode-4 cases. The victory-formats suite passed 49 tests, with five ignored.

Synthetic fixture and Pillow comparison script

AI-assisted with Codex.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7204777897 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3386#issuecomment-7204777897). Originally posted by [passerbyyhx](https://github.com/passerbyyhx) on 2026-11-26T22:33:00Z. --- I verified `7520d2f87548fc05c79a354632159e7c8f0accd7` with synthetic rANS BCPack streams through `bcpack::decode`. Pillow 11.2.1 independently decoded the emitted BC7, and all **480 RGBA texels across 30 blocks** matched the expected pixels. The matrix covers modes 4 and 5 at all four rotations, both mode-4 index selectors, and two distinct endpoint and spatial index patterns. The same fixture on parent `63bcca43c6a00a758510398fa43096ce7e4f7df0` disagreed on 63 texels across four blocks. The three mode-6 reserved-rotation-bit cases differed at all 16 texels each, and the second synthetic dictionary entry differed at 15. The zero-bit mode-6 control and first dictionary entry matched. Swapping the two mode-4 index streams in the revision caused 248 texel mismatches across all 16 mode-4 cases. The `victory-formats` suite passed 49 tests, with five ignored. [Synthetic fixture and Pillow comparison script](https://github.com/passerbyyhx/VIctory/commit/a76b0eae1834fbce02b1530cf97e19fd6a59754c) AI-assisted with Codex.
upipibi commented 2026-11-27 23:15:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-27T23:15:55Z.


Overlap with #3111 (read from both diffs, not run): the rotation half of this PR is the same change. Both add fn rotation(ctrl) after pbits and use it in bc7_precision and bc7_pack, and both add a test named bc7_rotation_applies_only_to_modes_4_and_5. #3111 is older (26 Nov 08:26), triage marked it a merge candidate, and gabrielvbraga reproduced its panic case with the bcpack_tables fuzz target.

So whichever lands second conflicts on those hunks. Rebased onto #3111, this PR would come down to the Tables::parse dictionary-layout fix and bc7_dictionary_indices_line_up, the part #3111 doesn't have.

On the rotation half's effect: with the shipped tables, no BC7 control word sets bits 3-4 outside modes 4 and 5 (0 of 512 control words and 0 of 255 dictionary entries in xcl's copy of the same tables, counted for xcl#3081), so it changes no decoded block there.

AI-assisted (Claude Code, Claude Opus 5.5 read both diffs).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7230004837 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3386#issuecomment-7230004837). Originally posted by [upipibi](https://github.com/upipibi) on 2026-11-27T23:15:55Z. --- Overlap with #3111 (read from both diffs, not run): the rotation half of this PR is the same change. Both add `fn rotation(ctrl)` after `pbits` and use it in `bc7_precision` and `bc7_pack`, and both add a test named `bc7_rotation_applies_only_to_modes_4_and_5`. #3111 is older (26 Nov 08:26), triage marked it a merge candidate, and gabrielvbraga reproduced its panic case with the `bcpack_tables` fuzz target. So whichever lands second conflicts on those hunks. Rebased onto #3111, this PR would come down to the `Tables::parse` dictionary-layout fix and `bc7_dictionary_indices_line_up`, the part #3111 doesn't have. On the rotation half's effect: with the shipped tables, no BC7 control word sets bits 3-4 outside modes 4 and 5 (0 of 512 control words and 0 of 255 dictionary entries in xcl's copy of the same tables, counted for xcl#3081), so it changes no decoded block there. AI-assisted (Claude Code, Claude Opus 5.5 read both diffs).
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3386:upstream-pr/3386'
git switch 'upstream-pr/3386'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3386'
git switch 'upstream-pr/3386'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3386'
git switch 'upstream-pr/3386'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3386'
git switch 'main'
git merge --squash 'upstream-pr/3386'
git switch 'main'
git merge --ff-only 'upstream-pr/3386'
git switch 'main'
git merge 'upstream-pr/3386'
git push origin 'main'
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!3386
No description provided.