formats: the full mip chain and the stencil plane, held at their limits #131

Open
xiaotu030202 wants to merge 2 commits from upstream-pr/131 into main
xiaotu030202 commented 2026-11-25 14:11:00 +00:00 (Migrated from github.com)
First-time contributor

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


Two pairs of values that have to agree, and nothing checked

The chain length against MAX_MIPS

Texture::new computes a mips = 0 description's chain as:

32 - desc.width.max(desc.height).max(depth3d).max(1).leading_zeros()

and Layout::new then requires 1..=16. Two separate expressions of the same limit — raise MAX_EXTENT, or make the chain one longer, and a description that used to lay out becomes Invalid with nothing pointing at the pair.

a_full_mip_chain_fits_the_limit_at_every_extent checks the extents that decide it:

extent chain
1×1 1
2×2 2
1024×1 11
MAX_EXTENT×1 15
1×MAX_EXTENT 15
MAX_EXTENT×MAX_EXTENT 15 (the longest there can be)

It also pins the other end: an extent of zero is refused. The max(1) is for the mip count, not for the size, and it is easy to read the other way — I did, going in, and the test corrected me.

The stencil plane's block against the colour format's

Texture::new builds the stencil Desc as element_bytes: 1, block: f.block — it keeps the colour format's block:

let plane = |bytes| Desc {
    element_bytes: bytes,
    block: f.block,
    ...
};

That is right only because no format in format() is both stencilled and block-compressed. Nothing states it. A BC format gaining a stencil bit would give the stencil plane a 4-texel block and a quarter of the size it needs — silently, since nothing would fail.

a_stencil_plane_is_one_byte_and_never_block_compressed scans every number format() answers for 0..=200, asserts the two sets do not meet, and then checks a real depth-stencil texture's second plane is one byte with block 1.

Both checked by mutation

chain length + 1           ->  FAILED  "1x1: 2 mips, expected 1"
BC1 given a stencil bit    ->  FAILED  "format 70 is both stencilled and
                                        block-compressed (block 4)"

Verification

Windows 11, Rust 1.98.1 (pinned), MSVC 14.44.35207. No game files, no GPU.

$ cargo test -p victory-formats
test result: ok. 34 passed; 0 failed; 4 ignored     (32 on #129, 30 on #125)

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

$ cargo xtask policy
asset policy: 172 files ok

What I found, and did not

No bug. Both pairs currently agree; these tests make them agree on record, so the next change to either side has to notice the other.

The zero-extent case is the one thing I had wrong: I wrote the test expecting 0x0 to lay out one mip because of the max(1), and it is refused — the right answer, and now stated.

Three PRs in this file now, each reviewable alone:

PR what
#123 the DXGI format table, every group's bounds
#125 addressable() answering wrongly for multisampled linear layouts (a real bug)
#129 walk staying inside the buffers it is given
this the chain length and the stencil plane's block

If that is too many for one file, they can be squashed — but they fail independently, which is why I kept them apart.

<!-- greyfinch-sync:Zettrox/ztrx:issue:6605454439 --> Imported from [Zettrox/ztrx](https://github.com/Zettrox/ztrx/pull/131). Originally posted by [xiaotu030202](https://github.com/xiaotu030202) on 2026-11-25T13:21:58Z. --- ## Two pairs of values that have to agree, and nothing checked ### The chain length against `MAX_MIPS` `Texture::new` computes a `mips = 0` description's chain as: ```rust 32 - desc.width.max(desc.height).max(depth3d).max(1).leading_zeros() ``` and `Layout::new` then requires `1..=16`. **Two separate expressions of the same limit** — raise `MAX_EXTENT`, or make the chain one longer, and a description that used to lay out becomes `Invalid` with nothing pointing at the pair. `a_full_mip_chain_fits_the_limit_at_every_extent` checks the extents that decide it: | extent | chain | |---|---| | 1×1 | 1 | | 2×2 | 2 | | 1024×1 | 11 | | `MAX_EXTENT`×1 | 15 | | 1×`MAX_EXTENT` | 15 | | `MAX_EXTENT`×`MAX_EXTENT` | **15** (the longest there can be) | It also pins the other end: **an extent of zero is refused.** The `max(1)` is for the mip count, not for the size, and it is easy to read the other way — I did, going in, and the test corrected me. ### The stencil plane's block against the colour format's `Texture::new` builds the stencil `Desc` as `element_bytes: 1, block: f.block` — **it keeps the colour format's block**: ```rust let plane = |bytes| Desc { element_bytes: bytes, block: f.block, ... }; ``` That is right only because no format in `format()` is both stencilled and block-compressed. Nothing states it. A BC format gaining a stencil bit would give the stencil plane a 4-texel block and **a quarter of the size it needs** — silently, since nothing would fail. `a_stencil_plane_is_one_byte_and_never_block_compressed` scans every number `format()` answers for 0..=200, asserts the two sets do not meet, and then checks a real depth-stencil texture's second plane is one byte with block 1. ## Both checked by mutation ``` chain length + 1 -> FAILED "1x1: 2 mips, expected 1" BC1 given a stencil bit -> FAILED "format 70 is both stencilled and block-compressed (block 4)" ``` ## Verification Windows 11, Rust 1.98.1 (pinned), MSVC 14.44.35207. **No game files, no GPU.** ``` $ cargo test -p victory-formats test result: ok. 34 passed; 0 failed; 4 ignored (32 on #129, 30 on #125) $ cargo clippy -p victory-formats --all-targets clean $ cargo xtask policy asset policy: 172 files ok ``` ## What I found, and did not **No bug.** Both pairs currently agree; these tests make them agree **on record**, so the next change to either side has to notice the other. The zero-extent case is the one thing I had wrong: I wrote the test expecting `0x0` to lay out one mip because of the `max(1)`, and it is refused — the right answer, and now stated. Three PRs in this file now, each reviewable alone: | PR | what | |---|---| | #123 | the DXGI format table, every group's bounds | | #125 | `addressable()` answering wrongly for multisampled linear layouts (a real bug) | | #129 | `walk` staying inside the buffers it is given | | this | the chain length and the stencil plane's block | If that is too many for one file, they can be squashed — but they fail independently, which is why I kept them apart.
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/131:upstream-pr/131'
git switch 'upstream-pr/131'

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