formats: a multisampled linear layout said it was addressable #125

Open
xiaotu030202 wants to merge 1 commit from upstream-pr/125 into main
xiaotu030202 commented 2026-11-25 14:10:59 +00:00 (Migrated from github.com)
First-time contributor

Imported from Zettrox/ztrx. Originally posted by xiaotu030202 on 2026-11-25T13:13:10Z.


The bug

Layout::new decides whether to build a tile pattern with one test:

let addressable = !d.swizzle.xor() && d.samples == 1;

addressable(), the method callers ask instead, tested something else:

self.desc.swizzle == Swizzle::LINEAR || !self.pattern[0].is_empty()

For a linear layout with more than one sample those disagree. Layout::new clears the pattern (samples != 1), but the method's first clause is true for LINEAR alone, so it answers yes — and offset then takes its linear branch:

if self.desc.swizzle == Swizzle::LINEAR {
    let row = u64::from(y) * u64::from(m.pitch) + u64::from(x);
    return Ok(base + row * u64::from(self.desc.element_bytes));
}

There is no sample dimension anywhere in that expression. A multisampled texture asked for an address got a wrong one instead of the Unsupported the shortcut: note right above promises ("no addressing for the XOR modes and MSAA"). A copy built on that address reads the wrong bytes — silently.

The fix

One line, so the method uses the test Layout::new uses:

!self.desc.swizzle.xor() && self.desc.samples == 1

The tests, and how they were checked

only_the_modes_without_pipe_xor_are_addressable — all 15 swizzle modes GFX10 has, at one sample: the 7 without pipe XOR addressable, the 8 _T/_X ones not. Swizzle::xor is self.0 >= 16 and every constant at or above 16 is named _T or _X, so this pins both the predicate and the naming against each other.

multisampling_is_unaddressable_even_without_pipe_xor — the case that found the bug: LINEAR and SW_64KB_S at 4 samples, both of which used to answer yes.

a_swizzle_gfx10_does_not_have_is_refused — the other end: numbers block_log2 does not know (3, 4, 7, 8, 11, 16, 20, 28, 100) have no layout at all, so addressable is never reached with an accidentally empty pattern.

Checked by reverting the fix:

old addressable():  FAILED   "Swizzle(0) with 4 samples reported addressable"

Before this PR, addressable() had one assertion in the whole crate — inside an unrelated depth-stencil test, on SW_64KB_Z_X.

Verification

Windows 11, Rust 1.98.1 (pinned), MSVC 14.44.35207. No game files, no GPU — this crate is byte arithmetic.

$ cargo test -p victory-formats
test result: ok. 30 passed; 0 failed; 4 ignored     (was 24 passed)

$ cargo test -p victory-gpu --lib
test result: ok. 114 passed; 0 failed; 1 ignored     (the crate that consumes this)

$ cargo clippy -p victory-formats --all-targets
clean

$ cargo xtask policy
asset policy: 172 files ok

Two things I looked at and left alone

The 4 ignored tests. I checked whether any could be made self-contained. matches_a_reference_decoder compares SNR against a real decoder's PCM output — no synthetic stream substitutes for that. walks_saved_streams and reference_chunks need real captures and reference chunks. They stay ignored.

The neighbouring shortcut: notes — XOR tile modes (_X, _T) and render-target metadata planes — both still say "add them when a title copies into or out of one", and there is no evidence one has. A patch there would be speculation.

Does this change behaviour for the title?

Only where it was already wrong: a multisampled linear layout, asked for an address, now returns Unsupported instead of a wrong offset. Multisampled textures are render targets the host draws, and the note in the code says titles do not copy into or out of them — which is exactly why this went unnoticed, and also why the fix is safe.

<!-- greyfinch-sync:Zettrox/ztrx:issue:6605291788 --> Imported from [Zettrox/ztrx](https://github.com/Zettrox/ztrx/pull/125). Originally posted by [xiaotu030202](https://github.com/xiaotu030202) on 2026-11-25T13:13:10Z. --- ## The bug `Layout::new` decides whether to build a tile pattern with one test: ```rust let addressable = !d.swizzle.xor() && d.samples == 1; ``` `addressable()`, the method callers ask instead, tested something else: ```rust self.desc.swizzle == Swizzle::LINEAR || !self.pattern[0].is_empty() ``` For **a linear layout with more than one sample** those disagree. `Layout::new` clears the pattern (`samples != 1`), but the method's first clause is true for `LINEAR` alone, so it answers **yes** — and `offset` then takes its linear branch: ```rust if self.desc.swizzle == Swizzle::LINEAR { let row = u64::from(y) * u64::from(m.pitch) + u64::from(x); return Ok(base + row * u64::from(self.desc.element_bytes)); } ``` There is no sample dimension anywhere in that expression. A multisampled texture asked for an address got **a wrong one** instead of the `Unsupported` the `shortcut:` note right above promises ("no addressing for the XOR modes and MSAA"). A copy built on that address reads the wrong bytes — silently. ## The fix One line, so the method uses the test `Layout::new` uses: ```rust !self.desc.swizzle.xor() && self.desc.samples == 1 ``` ## The tests, and how they were checked **`only_the_modes_without_pipe_xor_are_addressable`** — all 15 swizzle modes GFX10 has, at one sample: the 7 without pipe XOR addressable, the 8 `_T`/`_X` ones not. `Swizzle::xor` is `self.0 >= 16` and every constant at or above 16 is named `_T` or `_X`, so this pins both the predicate and the naming against each other. **`multisampling_is_unaddressable_even_without_pipe_xor`** — the case that found the bug: `LINEAR` and `SW_64KB_S` at 4 samples, both of which used to answer yes. **`a_swizzle_gfx10_does_not_have_is_refused`** — the other end: numbers `block_log2` does not know (3, 4, 7, 8, 11, 16, 20, 28, 100) have no layout at all, so `addressable` is never reached with an accidentally empty pattern. Checked by reverting the fix: ``` old addressable(): FAILED "Swizzle(0) with 4 samples reported addressable" ``` Before this PR, `addressable()` had one assertion in the whole crate — inside an unrelated depth-stencil test, on `SW_64KB_Z_X`. ## Verification Windows 11, Rust 1.98.1 (pinned), MSVC 14.44.35207. **No game files, no GPU** — this crate is byte arithmetic. ``` $ cargo test -p victory-formats test result: ok. 30 passed; 0 failed; 4 ignored (was 24 passed) $ cargo test -p victory-gpu --lib test result: ok. 114 passed; 0 failed; 1 ignored (the crate that consumes this) $ cargo clippy -p victory-formats --all-targets clean $ cargo xtask policy asset policy: 172 files ok ``` ## Two things I looked at and left alone **The 4 ignored tests.** I checked whether any could be made self-contained. `matches_a_reference_decoder` compares SNR against a real decoder's PCM output — no synthetic stream substitutes for that. `walks_saved_streams` and `reference_chunks` need real captures and reference chunks. They stay ignored. **The neighbouring `shortcut:` notes** — XOR tile modes (`_X`, `_T`) and render-target metadata planes — both still say "add them when a title copies into or out of one", and there is no evidence one has. A patch there would be speculation. ## Does this change behaviour for the title? Only where it was already wrong: a multisampled **linear** layout, asked for an address, now returns `Unsupported` instead of a wrong offset. Multisampled textures are render targets the host draws, and the note in the code says titles do not copy into or out of them — which is exactly why this went unnoticed, and also why the fix is safe.
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/125:upstream-pr/125'
git switch 'upstream-pr/125'

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