Reject overlapping depth slices in volume texture copies #21

Open
Yoshi-233 wants to merge 1 commit from upstream-pr/21 into main
Yoshi-233 commented 2026-11-25 12:26:31 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by Yoshi-233 on 2026-11-25T12:26:31Z.


A 2×2×2 volume with two active bytes per row and a four-byte row pitch needs six bytes for each slice. On main, a depth pitch of zero or five is accepted. Tiling then reads the same bytes into successive slices, and detiling can overwrite bytes from the previous slice.

Compute the occupied extent through the end of the last active row and check it before either copy starts. A depth pitch of six is valid in this example, and a single-slice mip can still use zero.

Verification

The regression test failed against main and now passes for both linear and 4 KiB swizzled textures. It checks both copy directions, destination contents on error, a round trip at the minimum valid pitch, and the one-slice mip.

The full victory-formats library suite passed on both WSL and native Windows with 25 passed and 4 ignored on each. The staged asset-policy check passed. The repository-wide check still flags the existing 630,652-byte crates/victory-shader/src/emit/lower.rs against its 512 KiB limit.

Windows XG interface verification

On Windows build 26200 I also exercised the HLE's exported XGCreateTextureComputer and called both copy methods through its vtable. The synthetic R8_UNORM volume was 2×2×2 with two mips and a four-byte row pitch. With row-major and 4 KiB swizzled layouts and depth pitches of 0 and 5, main (71652a2) returned S_OK and modified the destination in all eight calls. At 28b2879, all eight returned E_INVALIDARG and the entire destination buffer retained its sentinel bytes.

Across both layouts, six valid round trips also preserved the active texels and output padding. These cover the minimum pitch of 6, a padded pitch of 8, and a single-slice mip with a pitch of 0.

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:issue:6604446212 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/21). Originally posted by [Yoshi-233](https://github.com/Yoshi-233) on 2026-11-25T12:26:31Z. --- A 2×2×2 volume with two active bytes per row and a four-byte row pitch needs six bytes for each slice. On main, a depth pitch of zero or five is accepted. Tiling then reads the same bytes into successive slices, and detiling can overwrite bytes from the previous slice. Compute the occupied extent through the end of the last active row and check it before either copy starts. A depth pitch of six is valid in this example, and a single-slice mip can still use zero. ## Verification The regression test failed against main and now passes for both linear and 4 KiB swizzled textures. It checks both copy directions, destination contents on error, a round trip at the minimum valid pitch, and the one-slice mip. The full `victory-formats` library suite passed on both WSL and native Windows with 25 passed and 4 ignored on each. The staged asset-policy check passed. The repository-wide check still flags the existing 630,652-byte `crates/victory-shader/src/emit/lower.rs` against its 512 KiB limit. ## Windows XG interface verification On Windows build 26200 I also exercised the HLE's exported `XGCreateTextureComputer` and called both copy methods through its vtable. The synthetic R8_UNORM volume was 2×2×2 with two mips and a four-byte row pitch. With row-major and 4 KiB swizzled layouts and depth pitches of 0 and 5, main (`71652a2`) returned `S_OK` and modified the destination in all eight calls. At `28b2879`, all eight returned `E_INVALIDARG` and the entire destination buffer retained its sentinel bytes. Across both layouts, six valid round trips also preserved the active texels and output padding. These cover the minimum pitch of 6, a padded pitch of 8, and a single-slice mip with a pitch of 0.
Ekabusto commented 2026-11-25 12:47:36 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by Ekabusto on 2026-11-25T12:47:36Z.


Checked locally: cargo test -p victory-formats --lib passes (25 passed, 4 ignored), clippy clean, no conflicts.

The new bound is right: a depth slice must reach past the last active row. One point to be aware of: xg.rs:401/438 passes the title's own depth pitch. A title that passed a too-small pitch for a volume with more than one slice used to get an overlapping copy, and now gets an error. The callers inside the emulator (texture.rs:430, pitch * rows; texture.rs:873, linear.len()) always pass enough. Looks good to me.

Reviewed on Windows 11 (build 26300), Rust 1.98.1, each PR in its own worktree; no game data, not run in the game. AI-assisted (Claude Code).

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:comment:7162864417 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/21#issuecomment-7162864417). Originally posted by [Ekabusto](https://github.com/Ekabusto) on 2026-11-25T12:47:36Z. --- Checked locally: `cargo test -p victory-formats --lib` passes (25 passed, 4 ignored), clippy clean, no conflicts. The new bound is right: a depth slice must reach past the last active row. One point to be aware of: `xg.rs:401/438` passes the title's own depth pitch. A title that passed a too-small pitch for a volume with more than one slice used to get an overlapping copy, and now gets an error. The callers inside the emulator (`texture.rs:430`, `pitch * rows`; `texture.rs:873`, `linear.len()`) always pass enough. Looks good to me. Reviewed on Windows 11 (build 26300), Rust 1.98.1, each PR in its own worktree; no game data, not run in the game. AI-assisted (Claude Code).
lcg007xd-c commented 2026-11-25 21:18:49 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by lcg007xd-c on 2026-11-25T21:18:49Z.

Review: APPROVED


Verified on Linux (Rust 1.98.1)

  • cargo test -p victory-formats --lib on 28b2879: 25 passed, 0 failed, 4 ignored. Merged into current main with --no-commit: 25 passed, 0 failed.
  • Fail-without-fix: I put main's need computation back into Layout::walk and kept the new test. volume_depth_pitch_must_cover_the_last_active_row fails at texture.rs:1078 (left: Ok(()), right: Err(TooSmall)), so the pitch-0/5 case was accepted before. git status was clean afterwards.
  • cargo clippy -p victory-formats --target x86_64-pc-windows-msvc: 0 warnings, same as main.
  • git merge --no-commit --no-ff into main: merges cleanly.

Review notes

  • The bound is right. With depth_pitch >= slice_len = (height - 1) * row_pitch + bytes, slice z ends at z * dp + slice_len <= (z + 1) * dp, which is where slice z + 1 starts. Slices cannot overlap, in either copy direction, because tile and detile share walk.
  • m.height as usize - 1 can't underflow, because mip heights go through .max(1) (texture.rs:125/132).
  • For the record: the condition is sufficient but not necessary. An interleaved layout such as row_pitch = 4, bytes = 2, height = 2, depth_pitch = 2 puts slice 1's rows in slice 0's row padding without overlapping, and it is now refused as well. That seems fine to me, since no caller produces such a layout. I mention it only because the PR title says "overlapping". The doc comment (texture.rs:601, "must span the last active row") already states the exact rule.

Not verified

  • Behaviour through XGCreateTextureComputer on Windows (the HLE path in xg.rs), and any title that passes its own depth pitch.

Reviewed with Claude Code.

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:review:6065124156 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/21#pullrequestreview-6065124156). Originally posted by [lcg007xd-c](https://github.com/lcg007xd-c) on 2026-11-25T21:18:49Z. **Review:** APPROVED --- ### Verified on Linux (Rust 1.98.1) - `cargo test -p victory-formats --lib` on 28b2879: 25 passed, 0 failed, 4 ignored. Merged into current main with `--no-commit`: 25 passed, 0 failed. - Fail-without-fix: I put main's `need` computation back into `Layout::walk` and kept the new test. `volume_depth_pitch_must_cover_the_last_active_row` fails at `texture.rs:1078` (`left: Ok(())`, `right: Err(TooSmall)`), so the pitch-0/5 case was accepted before. `git status` was clean afterwards. - `cargo clippy -p victory-formats --target x86_64-pc-windows-msvc`: 0 warnings, same as main. - `git merge --no-commit --no-ff` into main: merges cleanly. ### Review notes - The bound is right. With `depth_pitch >= slice_len = (height - 1) * row_pitch + bytes`, slice `z` ends at `z * dp + slice_len <= (z + 1) * dp`, which is where slice `z + 1` starts. Slices cannot overlap, in either copy direction, because `tile` and `detile` share `walk`. - `m.height as usize - 1` can't underflow, because mip heights go through `.max(1)` (`texture.rs:125/132`). - For the record: the condition is sufficient but not necessary. An interleaved layout such as `row_pitch = 4`, `bytes = 2`, `height = 2`, `depth_pitch = 2` puts slice 1's rows in slice 0's row padding without overlapping, and it is now refused as well. That seems fine to me, since no caller produces such a layout. I mention it only because the PR title says "overlapping". The doc comment (`texture.rs:601`, "must span the last active row") already states the exact rule. ### Not verified - Behaviour through `XGCreateTextureComputer` on Windows (the HLE path in `xg.rs`), and any title that passes its own depth pitch. Reviewed with Claude Code.
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/21:upstream-pr/21'
git switch 'upstream-pr/21'

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/21'
git switch 'upstream-pr/21'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/21'
git switch 'upstream-pr/21'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/21'
git switch 'main'
git merge --squash 'upstream-pr/21'
git switch 'main'
git merge --ff-only 'upstream-pr/21'
git switch 'main'
git merge 'upstream-pr/21'
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
Belobug/VIctory-Belobug-AMD!21
No description provided.