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

Open
github-bot wants to merge 1 commit from github-bot/gh-3386 into main AGit
Owner

@⁠legayclightning22 on GitHub · 2026-11-26 21:19 UTC · original
legayclightning22:fix/bc7-dictionary-and-rotation-guards → main · +149 −8 · 1 files · 1 commits · open

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
**@⁠legayclightning22** on GitHub · 2026-11-26 21:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/3386) `legayclightning22:fix/bc7-dictionary-and-rotation-guards` → `main` · +149 −8 · 1 files · 1 commits · open ## 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`
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 01:06:14 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 21:28 UTC · original · approved these changes

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).

**@⁠Dukestar69** on GitHub · 2026-11-26 21:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/3386#pullrequestreview-6080640304) · **approved these changes** 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).
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 21:30 UTC · original · requested changes

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.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 21:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/3386#pullrequestreview-6080662509) · **requested changes** 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.
Author
Owner

@⁠legayclightning22 on GitHub · 2026-11-26 21:53 UTC · original

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.

**@⁠legayclightning22** on GitHub · 2026-11-26 21:53 UTC · [original](https://github.com/Marvisak/VIctory/pull/3386#issuecomment-7204158443) 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`.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 22:08 UTC · original · approved these changes

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.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 22:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/3386#pullrequestreview-6080999153) · **approved these changes** 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.
Author
Owner

@⁠passerbyyhx on GitHub · 2026-11-26 22:33 UTC · original

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.

**@⁠passerbyyhx** on GitHub · 2026-11-26 22:33 UTC · [original](https://github.com/Marvisak/VIctory/pull/3386#issuecomment-7204777897) 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.
Author
Owner

@⁠upipibi on GitHub · 2026-11-27 23:15 UTC · original

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).

**@⁠upipibi** on GitHub · 2026-11-27 23:15 UTC · [original](https://github.com/Marvisak/VIctory/pull/3386#issuecomment-7230004837) 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 can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3386/head:github-bot/gh-3386'
git switch 'github-bot/gh-3386'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3386'
git switch 'github-bot/gh-3386'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3386'
git switch 'github-bot/gh-3386'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3386'
git switch 'main'
git merge --squash 'github-bot/gh-3386'
git switch 'main'
git merge --ff-only 'github-bot/gh-3386'
git switch 'main'
git merge 'github-bot/gh-3386'
git push origin 'main'
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!3386
No description provided.