Formats/BCPack: restrict BC7 channel rotation to modes 4 and 5 #3111

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

@⁠RVZO6 on GitHub · 2026-11-26 08:26 UTC · original
RVZO6:rvzo/bcpack-bc7-rotation → main · +107 −2 · 1 files · 1 commits · open

What

Splits out item (1) requested in the #315 triage note: restrict BC7 channel rotation in crates/victory-formats/src/bcpack.rs to BC7 modes 4 and 5.

Root cause

Only BC7 modes 4 and 5 define a 2-bit channel rotation field. Previously, both bc7_precision and bc7_pack unconditionally extracted ctrl >> 3 & 3 for all modes:

  • For modes 0, 1, and 3 (pbits(mode) == true, alpha width 0) with rot == 1, bc7_precision swapped 0 into p[0] and skipped else if pbits(mode) { p[0] += 1; }, leaving prec[0] == 0. bc7_pack then evaluated ep.rows[...][...] >> (16 - prec[0]), panicking with attempt to shift right with overflow.
  • For modes 0–3 with rot == 2 | 3, bc7_precision swapped alpha's 0 width into G or B, dropping a color channel.
  • For modes 6 and 7 with rot != 0, bc7_precision skipped red's p-bit increment and bc7_pack reordered endpoint channels and misaligned the packed block.

Fix

  • Added fn rotation(ctrl: u16) -> usize, returning usize::from(ctrl >> 3 & 3) only when matches!(ctrl & 7, 4 | 5) and 0 otherwise.
  • Used rotation(ctrl) in both bc7_precision and bc7_pack.
  • Added unit regression test bc7_rotation_applies_only_to_modes_4_and_5 covering:
    • Modes 0, 1, 2, 3, 6, and 7 with rot = 1..=3 matching rot = 0 bit-for-bit through public decode(7, ...) (verified panicking at bcpack.rs:866 before the fix).
    • Modes 4 and 5 with rot = 1..=3 continuing to rotate alpha with channel rot - 1 (positive control).

Verification

  • cargo test -p victory-formats --locked (Linux): 47 passed, 0 failed.
  • cargo clippy -p victory-formats --all-targets --locked -- -D warnings (default + --all-features): clean.
  • Official windows-latest MSVC + WARP SM 6.6 CI (xtask policy, clippy default + --all-features, full workspace tests): https://github.com/RVZO6/VIctory/actions/runs/45782627843.

Not run: the title; any hardware GPU.
AI-assisted: T3 Code (Antigravity, gemini-4-neon).

**@⁠RVZO6** on GitHub · 2026-11-26 08:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/3111) `RVZO6:rvzo/bcpack-bc7-rotation` → `main` · +107 −2 · 1 files · 1 commits · open ## What Splits out item (1) requested in the `#315` triage note: restrict BC7 channel rotation in `crates/victory-formats/src/bcpack.rs` to BC7 modes 4 and 5. ## Root cause Only BC7 modes 4 and 5 define a 2-bit channel rotation field. Previously, both `bc7_precision` and `bc7_pack` unconditionally extracted `ctrl >> 3 & 3` for all modes: - For modes 0, 1, and 3 (`pbits(mode) == true`, alpha width `0`) with `rot == 1`, `bc7_precision` swapped `0` into `p[0]` and skipped `else if pbits(mode) { p[0] += 1; }`, leaving `prec[0] == 0`. `bc7_pack` then evaluated `ep.rows[...][...] >> (16 - prec[0])`, panicking with `attempt to shift right with overflow`. - For modes 0–3 with `rot == 2 | 3`, `bc7_precision` swapped alpha's `0` width into G or B, dropping a color channel. - For modes 6 and 7 with `rot != 0`, `bc7_precision` skipped red's p-bit increment and `bc7_pack` reordered endpoint channels and misaligned the packed block. ## Fix - Added `fn rotation(ctrl: u16) -> usize`, returning `usize::from(ctrl >> 3 & 3)` only when `matches!(ctrl & 7, 4 | 5)` and `0` otherwise. - Used `rotation(ctrl)` in both `bc7_precision` and `bc7_pack`. - Added unit regression test `bc7_rotation_applies_only_to_modes_4_and_5` covering: - Modes 0, 1, 2, 3, 6, and 7 with `rot = 1..=3` matching `rot = 0` bit-for-bit through public `decode(7, ...)` (verified panicking at `bcpack.rs:866` before the fix). - Modes 4 and 5 with `rot = 1..=3` continuing to rotate alpha with channel `rot - 1` (positive control). ## Verification - `cargo test -p victory-formats --locked` (Linux): 47 passed, 0 failed. - `cargo clippy -p victory-formats --all-targets --locked -- -D warnings` (default + `--all-features`): clean. - Official `windows-latest` MSVC + WARP SM 6.6 CI (`xtask policy`, clippy default + `--all-features`, full workspace tests): https://github.com/RVZO6/VIctory/actions/runs/45782627843. **Not run:** the title; any hardware GPU. AI-assisted: T3 Code (Antigravity, gemini-4-neon).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 08:34:06 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 08:32 UTC · original

Automated triage for the maintainers.

Verdict: merge candidate. CI check passes.

This PR adds rotation(ctrl) in crates/victory-formats/src/bcpack.rs. It uses it in bc7_precision and bc7_pack, so the 2-bit rotation field is honoured only for BC7 modes 4 and 5, which matches the public BC7 format. I checked it against main. With rot == 1 on modes 0, 1 and 3, bc7_precision zeroes red's precision (alpha width 0), and bc7_pack's >> (16 - p) then overflows. On other modes, rotation reorders the endpoints. So a malformed or unexpected control word from the stream tables could panic or produce a corrupt block. The change is minimal, and the new test bc7_rotation_applies_only_to_modes_4_and_5 checks that non-rotating modes decode the same for every rot. It also checks the rotated packing for modes 4 and 5.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-26 08:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/3111#issuecomment-7186717170) <!-- victory-triage v1 head=796c55ec776da567d9c1566867ac3dc13e5d78f2 verdict=merge-candidate at=2026-11-26T08:31:06Z --> Automated triage for the maintainers. **Verdict: merge candidate.** CI `check` passes. This PR adds `rotation(ctrl)` in `crates/victory-formats/src/bcpack.rs`. It uses it in `bc7_precision` and `bc7_pack`, so the 2-bit rotation field is honoured only for BC7 modes 4 and 5, which matches the public BC7 format. I checked it against main. With `rot == 1` on modes 0, 1 and 3, `bc7_precision` zeroes red's precision (alpha width 0), and `bc7_pack`'s `>> (16 - p)` then overflows. On other modes, rotation reorders the endpoints. So a malformed or unexpected control word from the stream tables could panic or produce a corrupt block. The change is minimal, and the new test `bc7_rotation_applies_only_to_modes_4_and_5` checks that non-rotating modes decode the same for every `rot`. It also checks the rotated packing for modes 4 and 5. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠AurumCarlos on GitHub · 2026-11-26 08:43 UTC · original · reviewed

Reviewed head 796c55e (on main 63bcca4). The change looks correct to me; nothing blocking, three small notes.

Checked

  • rotation() honours bits 4:3 only for modes 4 and 5, as in BC7, and bc7_precision and bc7_pack now share it, so precision and endpoint order cannot disagree.
  • The new test catches the bug: with the old usize::from(ctrl >> 3 & 3) it fails with attempt to shift right with overflow at the >> (16 - p) in bc7_pack; with the PR, the 11 bcpack tests pass and cargo clippy -p victory-formats --lib --tests -- -D warnings is clean (Linux).

Notes

  1. No effect with the shipped tables. The control code comes from t.ctrl7, not from the stream, and the runtime only uses Tables::builtin() (dstorage.rs, load_bcpack_tables). Scanning its 285 used codes: bits 4:3 are non-zero only on modes 4 (2/2/2/2 per rotation) and 5 (1/1/1/1); modes 0-3, 6 and 7 all have rotation 0. So this is hardening for tables given to Tables::parse (tests, VICTORY_BCPACK_TABLES), and in-game decoding is byte-for-byte the same. It may be worth saying so in the description, since the triage note reads as if a title stream could trigger the panic.
  2. Reject instead of ignore (optional). A table that sets rotation bits on a mode without a rotation field is now accepted and the bits dropped. Tables::parse could refuse such ctrl7 entries instead, as bc7 already refuses ctrl == 0 ("unused BC7 control code"), so a wrong table file fails loudly. The built-in tables pass that check.
  3. The modes 4/5 half of the test mirrors the packer. Its expected block is built with the same swap of endpoint order 3 and rot - 1 that bc7_pack uses, so it pins the current behaviour but would not notice a wrong rotation convention. The half for the other modes is an independent check. Optional: compare one rotated mode-4 block with an independent BC7 decode, or note in the comment that this part is a regression pin.

AI-assisted review (Claude Code).

**@⁠AurumCarlos** on GitHub · 2026-11-26 08:43 UTC · [original](https://github.com/Marvisak/VIctory/pull/3111#pullrequestreview-6070887040) · **reviewed** Reviewed head `796c55e` (on `main` `63bcca4`). The change looks correct to me; nothing blocking, three small notes. ### Checked - `rotation()` honours bits 4:3 only for modes 4 and 5, as in BC7, and `bc7_precision` and `bc7_pack` now share it, so precision and endpoint order cannot disagree. - The new test catches the bug: with the old `usize::from(ctrl >> 3 & 3)` it fails with `attempt to shift right with overflow` at the `>> (16 - p)` in `bc7_pack`; with the PR, the 11 `bcpack` tests pass and `cargo clippy -p victory-formats --lib --tests -- -D warnings` is clean (Linux). ### Notes 1. **No effect with the shipped tables.** The control code comes from `t.ctrl7`, not from the stream, and the runtime only uses `Tables::builtin()` (`dstorage.rs`, `load_bcpack_tables`). Scanning its 285 used codes: bits 4:3 are non-zero only on modes 4 (2/2/2/2 per rotation) and 5 (1/1/1/1); modes 0-3, 6 and 7 all have rotation 0. So this is hardening for tables given to `Tables::parse` (tests, `VICTORY_BCPACK_TABLES`), and in-game decoding is byte-for-byte the same. It may be worth saying so in the description, since the triage note reads as if a title stream could trigger the panic. 2. **Reject instead of ignore (optional).** A table that sets rotation bits on a mode without a rotation field is now accepted and the bits dropped. `Tables::parse` could refuse such ctrl7 entries instead, as `bc7` already refuses `ctrl == 0` ("unused BC7 control code"), so a wrong table file fails loudly. The built-in tables pass that check. 3. **The modes 4/5 half of the test mirrors the packer.** Its expected block is built with the same swap of endpoint order 3 and `rot - 1` that `bc7_pack` uses, so it pins the current behaviour but would not notice a wrong rotation convention. The half for the other modes is an independent check. Optional: compare one rotated mode-4 block with an independent BC7 decode, or note in the comment that this part is a regression pin. AI-assisted review (Claude Code).
Author
Owner

@⁠oni-swr on GitHub · 2026-11-26 08:44 UTC · original

Independent CPU test report for PR #3111 (claim: #1633). This is a test report, not a source review.

Tested: head 796c55ec776da567d9c1566867ac3dc13e5d78f2, exact parent 63bcca43c6a00a758510398fa43096ce7e4f7df0.
Environment: Windows 11, rustc/cargo 1.98.1, CPU only (no GPU, no D3D12). cargo test --locked -j 4 -p victory-formats, with a private CARGO_TARGET_DIR.

Results

  • PR head, with the scratch oracle below added: 48 passed; 0 failed; 5 ignored (the 5 are benchmarks or tests that need files outside the repository, which were not used). This includes the PR's own bc7_rotation_applies_only_to_modes_4_and_5.
  • Exact parent, unmodified: 46 passed; 0 failed; 5 ignored (the PR's test is absent there, as expected). With the scratch oracle added: 47 passed; 0 failed; 5 ignored.

Independent oracle (scratch only, not part of the PR). I wrote a separate test that builds valid single-subset synthetic stream records for BC7 modes 4 and 5. It decodes each record, then parses the output block bit by bit. It checks the mode bit, the 2-bit rotation field and, for mode 4, the index-selection bit. For every rotation 0..3 it then checks that each wire channel slot holds the expected logical channel: slot j holds the logical channel that rotation swaps into it, with R/G/B/A swapped as in the public BC7 rule, and the alpha-width slot is the 4th. Mode 4 uses 5/5/5/6 bits and mode 5 uses 7/7/7/8. Endpoint values are distinct per channel and endpoint, and a second value set puts wide values in the alpha-width slot. Everything after the endpoints must be zero, since the indices are zero.

  • 24 oracle cases (mode 4: 2 index-selection values × 4 rotations × 2 value sets = 16; mode 5: 4 rotations × 2 value sets = 8): 24/24 agree on the PR head.
  • The same oracle on the exact parent: 24/24 agree. Rotation 0 controls and mode 4/5 rotation 1..3 behave identically before and after the PR, so the change does not alter valid mode 4/5 output.

Not covered: modes 0-3/6/7 with nonzero rotation bits, which are the PR's other behavior change, are exercised only by the PR's own test and not by my oracle. No multi-subset or non-zero index data was checked. All data is synthetic.

Cleanup: the scratch test was removed (working tree clean), and the unique target directory was deleted by literal path and confirmed gone.

AI use: Claude Code (Claude Sonnet 5.5) ran this verification; coordinated with Codex (gpt-6.2-sol) per the claim.

**@⁠oni-swr** on GitHub · 2026-11-26 08:44 UTC · [original](https://github.com/Marvisak/VIctory/pull/3111#issuecomment-7186967050) **Independent CPU test report for PR #3111** (claim: https://git.gtahub.st/github/Marvisak--VIctory/issues/1633). This is a test report, not a source review. **Tested:** head `796c55ec776da567d9c1566867ac3dc13e5d78f2`, exact parent `63bcca43c6a00a758510398fa43096ce7e4f7df0`. **Environment:** Windows 11, rustc/cargo 1.98.1, CPU only (no GPU, no D3D12). `cargo test --locked -j 4 -p victory-formats`, with a private `CARGO_TARGET_DIR`. **Results** - PR head, with the scratch oracle below added: `48 passed; 0 failed; 5 ignored` (the 5 are benchmarks or tests that need files outside the repository, which were not used). This includes the PR's own `bc7_rotation_applies_only_to_modes_4_and_5`. - Exact parent, unmodified: `46 passed; 0 failed; 5 ignored` (the PR's test is absent there, as expected). With the scratch oracle added: `47 passed; 0 failed; 5 ignored`. **Independent oracle (scratch only, not part of the PR).** I wrote a separate test that builds valid single-subset synthetic stream records for BC7 modes 4 and 5. It decodes each record, then parses the output block bit by bit. It checks the mode bit, the 2-bit rotation field and, for mode 4, the index-selection bit. For every rotation 0..3 it then checks that each wire channel slot holds the expected logical channel: slot `j` holds the logical channel that rotation swaps into it, with R/G/B/A swapped as in the public BC7 rule, and the alpha-width slot is the 4th. Mode 4 uses 5/5/5/6 bits and mode 5 uses 7/7/7/8. Endpoint values are distinct per channel and endpoint, and a second value set puts wide values in the alpha-width slot. Everything after the endpoints must be zero, since the indices are zero. - 24 oracle cases (mode 4: 2 index-selection values × 4 rotations × 2 value sets = 16; mode 5: 4 rotations × 2 value sets = 8): **24/24 agree on the PR head.** - The same oracle on the exact parent: **24/24 agree**. Rotation 0 controls and mode 4/5 rotation 1..3 behave identically before and after the PR, so the change does not alter valid mode 4/5 output. **Not covered:** modes 0-3/6/7 with nonzero rotation bits, which are the PR's other behavior change, are exercised only by the PR's own test and not by my oracle. No multi-subset or non-zero index data was checked. All data is synthetic. **Cleanup:** the scratch test was removed (working tree clean), and the unique target directory was deleted by literal path and confirmed gone. AI use: Claude Code (Claude Sonnet 5.5) ran this verification; coordinated with Codex (gpt-6.2-sol) per the claim.
Author
Owner

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

Verified against origin/main (63bcca4):

  • cargo test -p victory-formats -- --test-threads=1: 47/47 passed (5 ignored benches/external; including 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.

Cleanly restricts BC7 channel rotation to modes 4 and 5 per spec.

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

**@⁠Dukestar69** on GitHub · 2026-11-26 08:56 UTC · [original](https://github.com/Marvisak/VIctory/pull/3111#pullrequestreview-6071053327) · **approved these changes** Verified against `origin/main` (`63bcca4`): - `cargo test -p victory-formats -- --test-threads=1`: 47/47 passed (5 ignored benches/external; including `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. Cleanly restricts BC7 channel rotation to modes 4 and 5 per spec. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
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/3111/head:github-bot/gh-3111'
git switch 'github-bot/gh-3111'

Merge

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