Formats: share immutable texture swizzle tables #993

Merged
TG0000 merged 1 commit from upstream-pr/993 into main 2026-11-25 00:12:14 +00:00
TG0000 commented 2026-11-24 14:45:35 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by TG0000 on 2026-11-24T14:45:35Z.


Summary

Texture::new built the three swizzle tables of its layout on every call, although they depend only on the micro-tile kind, the block size and the element size. This shares one immutable table set per key through OnceLocks (at most 45 keys); extents, mips and array slices still only affect the base address. No layout changes: linear and unsupported modes keep an empty table.

Measured

cargo run --release -p victory-formats --example texture_layout_bench (included), Ryzen 9 5900X, Windows 11. Three processes per side, interleaved, median of the three; other builds were running, so treat these as approximate. ns per layout:

Layout main this PR
BC7 64KB_D 1049 298
BC1 64KB_S 1205 236
RGBA8 4KB_D 950 357
R8 64KB_S 3034 260
RGBA8 volume 64KB_S 980 308
RGBA8 linear (no table; unchanged code path) 339 370

The linear rows are within run-to-run noise (273-427 ns on main, 321-445 here). The bench's checksums are identical on both sides. This is a microbenchmark of layout creation; in-game frame time was not measured.

Verification

Rust 1.98.1 MSVC, base ffe310e:

  • cargo test --workspace --locked --no-fail-fast: all pass, 343 passed, 0 failed. An earlier run on a fully loaded PC failed frame::tests::waits_end_on_ticks_and_count_frames in victory-gpu, which this PR does not touch; #174 and #689 are about that kind of wait.
  • cargo clippy --workspace --all-targets --locked -- -D warnings, with and without --all-features: clean.
  • The new test compares every shared table with a freshly built one for 8 modes and 5 element sizes, and checks that two layouts of the same key share the same table.

Overlap

#603 inlines micro-tile copies when detiling, and #263, #315, #351 and #629 also edit texture.rs. #603 changes the detile loop, not table construction.

AI-assisted: written by GPT 6.2 Sol, benchmarked and checked by Claude Opus 5.5. No game data.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:6584421302 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/993). Originally posted by [TG0000](https://github.com/TG0000) on 2026-11-24T14:45:35Z. --- ## Summary `Texture::new` built the three swizzle tables of its layout on every call, although they depend only on the micro-tile kind, the block size and the element size. This shares one immutable table set per key through `OnceLock`s (at most 45 keys); extents, mips and array slices still only affect the base address. No layout changes: linear and unsupported modes keep an empty table. ## Measured `cargo run --release -p victory-formats --example texture_layout_bench` (included), Ryzen 9 5900X, Windows 11. Three processes per side, interleaved, median of the three; other builds were running, so treat these as approximate. ns per layout: | Layout | main | this PR | |---|---|---| | BC7 64KB_D | 1049 | 298 | | BC1 64KB_S | 1205 | 236 | | RGBA8 4KB_D | 950 | 357 | | R8 64KB_S | 3034 | 260 | | RGBA8 volume 64KB_S | 980 | 308 | | RGBA8 linear (no table; unchanged code path) | 339 | 370 | The linear rows are within run-to-run noise (273-427 ns on main, 321-445 here). The bench's checksums are identical on both sides. This is a microbenchmark of layout creation; **in-game frame time was not measured**. ## Verification Rust 1.98.1 MSVC, base `ffe310e`: - `cargo test --workspace --locked --no-fail-fast`: all pass, 343 passed, 0 failed. An earlier run on a fully loaded PC failed `frame::tests::waits_end_on_ticks_and_count_frames` in `victory-gpu`, which this PR does not touch; #174 and #689 are about that kind of wait. - `cargo clippy --workspace --all-targets --locked -- -D warnings`, with and without `--all-features`: clean. - The new test compares every shared table with a freshly built one for 8 modes and 5 element sizes, and checks that two layouts of the same key share the same table. ## Overlap #603 inlines micro-tile copies when detiling, and #263, #315, #351 and #629 also edit `texture.rs`. #603 changes the detile loop, not table construction. AI-assisted: written by GPT 6.2 Sol, benchmarked and checked by Claude Opus 5.5. No game data. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MichaelSmithAI commented 2026-11-24 15:04:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by MichaelSmithAI on 2026-11-24T15:00:23Z.


We applied the table-sharing change to our staged texture branch and compared the tiled output with its parent. Across ten synthetic 137×75 layouts, all 4,307,968 bytes matched. The cases included BC1 and BC7, mip tails, array slices, 3D volumes, separate depth and stencil planes, and a linear control. Each build also detiled every patterned subresource back to its input, including preservation of row padding.

The victory-formats suite passed 24 tests at the exact PR head (64b80c3) and 27 on the integrated branch, with four ignored on each. This checks byte compatibility on the staged texture code. We did not measure title frame time.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7135303264 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/993#issuecomment-7135303264). Originally posted by [MichaelSmithAI](https://github.com/MichaelSmithAI) on 2026-11-24T15:00:23Z. --- We applied the table-sharing change to our staged texture branch and compared the tiled output with its parent. Across ten synthetic 137×75 layouts, all 4,307,968 bytes matched. The cases included BC1 and BC7, mip tails, array slices, 3D volumes, separate depth and stencil planes, and a linear control. Each build also detiled every patterned subresource back to its input, including preservation of row padding. The victory-formats suite passed 24 tests at the exact PR head (64b80c3) and 27 on the integrated branch, with four ignored on each. This checks byte compatibility on the staged texture code. We did not measure title frame time.
mercuryVM commented 2026-11-24 16:04:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T15:26:44Z.

Review: APPROVED


Tested 64b80c3 on Windows 11, RTX 5070, MSVC: builds, cargo test --workspace --no-fail-fast 557 passed. One run had frame::tests::waits_end_on_ticks_and_count_frames fail under load (the known timing flake, untouched here); it passed 3 of 3 on its own. The new layouts_share_identical_swizzle_tables passes.

  • The table index is in range for every tiled mode. Swizzle::block_log2 only gives 8, 12 or 16 for tiled modes (linear's 0 takes the other branch), so (block_log2 - 8) / 4 is 0..=2. kind is 0..=2, and element sizes go up to 16 bytes, so element_log2 is 0..=4. If a 256 KiB mode is ever added, the array panics rather than silently aliasing, which is acceptable.
  • Layout still derives PartialEq, which now compares through the reference, i.e. by contents, so equality is unchanged.
  • The test checks every key against a freshly built table and checks pointer identity across extents, mips and clones, which covers the claim. I didn't rerun the bench numbers.

Merged into our fork's main.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6042177006 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/993#pullrequestreview-6042177006). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T15:26:44Z. **Review:** APPROVED --- Tested 64b80c3 on Windows 11, RTX 5070, MSVC: builds, `cargo test --workspace --no-fail-fast` 557 passed. One run had `frame::tests::waits_end_on_ticks_and_count_frames` fail under load (the known timing flake, untouched here); it passed 3 of 3 on its own. The new `layouts_share_identical_swizzle_tables` passes. - The table index is in range for every tiled mode. `Swizzle::block_log2` only gives 8, 12 or 16 for tiled modes (linear's 0 takes the other branch), so `(block_log2 - 8) / 4` is 0..=2. `kind` is 0..=2, and element sizes go up to 16 bytes, so `element_log2` is 0..=4. If a 256 KiB mode is ever added, the array panics rather than silently aliasing, which is acceptable. - `Layout` still derives `PartialEq`, which now compares through the reference, i.e. by contents, so equality is unchanged. - The test checks every key against a freshly built table and checks pointer identity across extents, mips and clones, which covers the claim. I didn't rerun the bench numbers. Merged into our fork's main. AI-assisted (Claude Code).
catminou4 commented 2026-11-24 16:04:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T15:41:43Z.

Review: APPROVED


Swizzle patterns are now shared immutable tables keyed by (micro-tile kind, block size, element size) instead of one Vec triple per Layout — the key is sound because extents/mips/array slices only move the base address, and the new test proves two different layouts share the same tables across all 8 kind/block combos and element sizes 0..4. The bench example is a nice touch for the PR description's claims.

Two hardening questions, non-blocking: (1) shared_pattern indexes PATTERNS[kind][block_index][element_log2] directly — kind is safe (0..=2 by construction at the call site), but block_index = (block_log2 - 8) / 4 would underflow if a swizzle ever yielded block_log2 < 8, and element_log2 >= 5 would go out of bounds; today MICRO modes only produce 8/12/16 and texel sizes <= 16 B, so it's latent. A debug_assert on both would make the invariant explicit. (2) OnceLock per cell means first use per combo pays the build; fine.

Verified locally: test-merge with main clean; cargo test -p victory-formats --locked (macOS arm64, rustc 1.98.1) → 24 passed / 0 failed / 4 ignored. Approving.

<!-- greyfinch-sync:Marvisak/VIctory:review:6042461081 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/993#pullrequestreview-6042461081). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T15:41:43Z. **Review:** APPROVED --- Swizzle patterns are now shared immutable tables keyed by (micro-tile kind, block size, element size) instead of one `Vec` triple per `Layout` — the key is sound because extents/mips/array slices only move the base address, and the new test proves two different layouts share the same tables across all 8 kind/block combos and element sizes 0..4. The bench example is a nice touch for the PR description's claims. Two hardening questions, non-blocking: (1) `shared_pattern` indexes `PATTERNS[kind][block_index][element_log2]` directly — `kind` is safe (0..=2 by construction at the call site), but `block_index = (block_log2 - 8) / 4` would underflow if a swizzle ever yielded `block_log2 < 8`, and `element_log2 >= 5` would go out of bounds; today MICRO modes only produce 8/12/16 and texel sizes <= 16 B, so it's latent. A debug_assert on both would make the invariant explicit. (2) `OnceLock` per cell means first use per combo pays the build; fine. Verified locally: test-merge with main clean; `cargo test -p victory-formats --locked` (macOS arm64, rustc 1.98.1) → **24 passed / 0 failed / 4 ignored**. Approving.
DoubleD17 commented 2026-11-24 16:04:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DoubleD17 on 2026-11-24T16:00:36Z.

Review: COMMENTED


I independently enumerated the swizzle table cache on 64b80c3. Of the 45 reserved slots, 40 can be initialized through Layout::new. The five 256 B thick slots are unreachable. For each reachable key, I counted the entries in the three offset vectors and cross-checked the counts by projecting the block offsets onto each axis.

With all 40 keys initialized in one process, the vectors hold 4,474 u32 entries, or 17,896 bytes of payload. The largest key holds 2,052 bytes across its three vectors. This source-derived count uses vector lengths. It excludes spare capacity, slot storage, allocator overhead and temporary allocations. I did not execute the PR or measure RSS, initialization time or frame time.

AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:review:6042799425 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/993#pullrequestreview-6042799425). Originally posted by [DoubleD17](https://github.com/DoubleD17) on 2026-11-24T16:00:36Z. **Review:** COMMENTED --- I independently enumerated the swizzle table cache on `64b80c3`. Of the 45 reserved slots, 40 can be initialized through `Layout::new`. The five 256 B thick slots are unreachable. For each reachable key, I counted the entries in the three offset vectors and cross-checked the counts by projecting the block offsets onto each axis. With all 40 keys initialized in one process, the vectors hold 4,474 `u32` entries, or **17,896 bytes of payload**. The largest key holds **2,052 bytes** across its three vectors. This source-derived count uses vector lengths. It excludes spare capacity, slot storage, allocator overhead and temporary allocations. I did not execute the PR or measure RSS, initialization time or frame time. AI-assisted.
TG0000 commented 2026-11-24 16:05:02 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by TG0000 on 2026-11-24T15:07:52Z.


Thanks for the byte comparison and the integrated run.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7135507999 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/993#issuecomment-7135507999). Originally posted by [TG0000](https://github.com/TG0000) on 2026-11-24T15:07:52Z. --- Thanks for the byte comparison and the integrated run.
Zipettte commented 2026-11-24 16:05:05 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Zipettte on 2026-11-24T15:13:00Z.


Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 64b80c3: cargo test -p victory-formats passes, 24 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7135649787 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/993#issuecomment-7135649787). Originally posted by [Zipettte](https://github.com/Zipettte) on 2026-11-24T15:13:00Z. --- Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 64b80c3: `cargo test -p victory-formats` passes, 24 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed). _Tested with AI assistance (Claude)._
TG0000 commented 2026-11-24 16:05:28 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by TG0000 on 2026-11-24T16:01:00Z.


Thanks for the review. On (1): both are array indexing, so a value out of range panics at the lookup (an underflow of block_index gives an index far out of range) and cannot read another table. I left the head unchanged; I can add the debug_asserts if the maintainer prefers them.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7136984175 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/993#issuecomment-7136984175). Originally posted by [TG0000](https://github.com/TG0000) on 2026-11-24T16:01:00Z. --- Thanks for the review. On (1): both are array indexing, so a value out of range panics at the lookup (an underflow of `block_index` gives an index far out of range) and cannot read another table. I left the head unchanged; I can add the `debug_assert`s if the maintainer prefers them.
upstream-sync closed this pull request 2026-11-25 01:05:10 +00:00
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
Marvisak/VIctory!993
No description provided.