Formats/coalesce linear texture copies #3477

Open
nothanger wants to merge 3 commits from upstream-pr/3477 into main
nothanger commented 2026-11-27 06:33:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by nothanger on 2026-11-27T06:33:39Z.


Dense LINEAR textures are currently copied row by row. This change copies contiguous rows as a whole plane, and adjacent volume planes as one span, reducing CPU copy overhead.

Changes

  • Coalesce rows only when both row strides equal the active row byte count.
  • Merge depth planes only when both depth strides match the plane size, preserving padding and gaps between mips.
  • Keep the existing validation before any writes.
  • Add differential tests covering 7,680 configurations and a reproducible CPU benchmark.
  • Reorder the fast-path checks to avoid the tiled-control slowdown observed with the initial implementation.

Validation

Tested on Linux x86-64 with Rust 1.98.1:

  • Debug and release formats suites: 47 passed, 0 failed, 5 ignored.
  • Formats all-targets Clippy with -D warnings: passed.
  • Repository asset-policy check: passed.
  • Windows/MSVC all-targets cross-check and library cross-build: passed.

The five ignored tests are two existing benchmarks and three external-data tests.

Performance

In synthetic CPU measurements, dense-volume detile improved from 28.67 µs to 23.06 µs, approximately 1.24x throughput. Padded rows remained stable; unaligned-width controls were approximately 1–2% slower. Tiled controls remained close to baseline.

No game-FPS improvement is claimed.

This partially overlaps #3450's GPU staging shortcut. This implementation operates in victory-formats, covers both tile and detile, and also benefits XG callers.

Limitations

Native Windows execution, physical GPUs, retail titles and the complete workspace suite were not tested. Linux performance results do not establish Windows performance.

AI assistance

OpenAI Codex (GPT-6 family reported in this session) assisted with investigation, implementation, independent review, tests, measurements and this description. All reported test and benchmark results were actually executed.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5522348863 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3477). Originally posted by [nothanger](https://github.com/nothanger) on 2026-11-27T06:33:39Z. --- Dense LINEAR textures are currently copied row by row. This change copies contiguous rows as a whole plane, and adjacent volume planes as one span, reducing CPU copy overhead. ## Changes - Coalesce rows only when both row strides equal the active row byte count. - Merge depth planes only when both depth strides match the plane size, preserving padding and gaps between mips. - Keep the existing validation before any writes. - Add differential tests covering 7,680 configurations and a reproducible CPU benchmark. - Reorder the fast-path checks to avoid the tiled-control slowdown observed with the initial implementation. ## Validation Tested on Linux x86-64 with Rust 1.98.1: - Debug and release formats suites: 47 passed, 0 failed, 5 ignored. - Formats all-targets Clippy with `-D warnings`: passed. - Repository asset-policy check: passed. - Windows/MSVC all-targets cross-check and library cross-build: passed. The five ignored tests are two existing benchmarks and three external-data tests. ## Performance In synthetic CPU measurements, dense-volume detile improved from 28.67 µs to 23.06 µs, approximately 1.24x throughput. Padded rows remained stable; unaligned-width controls were approximately 1–2% slower. Tiled controls remained close to baseline. No game-FPS improvement is claimed. ## Related work This partially overlaps #3450's GPU staging shortcut. This implementation operates in `victory-formats`, covers both tile and detile, and also benefits XG callers. ## Limitations Native Windows execution, physical GPUs, retail titles and the complete workspace suite were not tested. Linux performance results do not establish Windows performance. ## AI assistance OpenAI Codex (GPT-6 family reported in this session) assisted with investigation, implementation, independent review, tests, measurements and this description. All reported test and benchmark results were actually executed.
oni-swr commented 2026-11-27 07:03:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-27T07:03:37Z.


Independent CPU verification (claimed on #1633). AI-assisted (Claude Sonnet 5.5). CPU only: no GPU, no title, no game data.

Environment: AMD Ryzen 7 5700X3D, Windows 11 build 10.0.26200, rustc 1.98.1, -j 4, own target dir.
Base: origin/main 63bcca43c6a00a758510398fa43096ce7e4f7df0. Head: e65fb626e9c484c58cbaf37dbc98599fd6c889ad.

Tests (cargo test -p victory-formats -j 4)

  • Base: 46 passed, 0 failed, 5 ignored.
  • Head: 47 passed, 0 failed, 5 ignored. The new texture::tests::linear_copies_match_element_addresses (7680 cases against the element-wise oracle) passes.

Benchmark (linear_copy_bench, release, median of 3 runs; each run is itself a median of 9 samples). The example uses only the public API, so I ran the same file against base. Base and head runs were interleaved. Ratio is base/head, so above 1 means head is faster.

case base ns head ns ratio
dense 256x256 detile 7,517 88,766 0.08
dense 256x256 tile 5,856 5,690 1.03
dense 1920x1080 detile 186,509 308,252 0.61
dense 1920x1080 tile 187,321 309,012 0.61
dense volume detile 24,965 39,851 0.63
dense volume tile 25,989 39,426 0.66
padded depth detile 27,061 46,916 0.58
mip-chain volume detile / tile 24,506 / 25,810 22,307 / 22,988 1.10 / 1.12
linear 1D detile / tile 30 / 33 26 / 26 1.14 / 1.25
padded rows, unaligned width, BC7, tiled controls 0.88-1.14 (noise)

The slowdowns reproduced in both measurement sets, with head's dense 256x256 detile at 87.7 and 88.8 us. On this CPU the single large copy is slower than the per-row copies it replaces for the dense cases (up to about 12x for 256x256 detile, about 1.6x for 1080p and the dense volume). I did not find the cause. Possibly the large copy_from_slice hits a slow memcpy path here, for example 4K aliasing between the two same-sized buffers, but that is a guess. It would be worth checking on other CPUs before relying on the speedup claim. Only the mip-chain volume and 1D cases improved.

Review (diff read in full, only crates/victory-formats touched)

  • Condition is right: the fast path needs row_pitch == bytes, m.pitch * n == bytes (tiled row has no padding) and Swizzle::LINEAR. Planes merge only when slice_size == slice_bytes and depth_pitch == slice_bytes. A mip chain correctly falls back to the per-plane loop, because slice_size spans the chain.
  • Overflow: slice_bytes and need are still checked_*, and the length checks run before the fast path. base + z * slice_size is the same arithmetic as the old path. I found no off-by-one and no correctness bug.
  • The new test covers 1D/2D/3D, mips, odd widths, padded rows and depth, exact-length buffers and the error paths, and the checks are meaningful.

No game files, dumps, paths, shader names or hashes were used or are included here.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7212102337 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3477#issuecomment-7212102337). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-27T07:03:37Z. --- Independent CPU verification (claimed on #1633). AI-assisted (Claude Sonnet 5.5). CPU only: no GPU, no title, no game data. **Environment:** AMD Ryzen 7 5700X3D, Windows 11 build 10.0.26200, rustc 1.98.1, `-j 4`, own target dir. **Base:** origin/main `63bcca43c6a00a758510398fa43096ce7e4f7df0`. **Head:** `e65fb626e9c484c58cbaf37dbc98599fd6c889ad`. **Tests** (`cargo test -p victory-formats -j 4`) - Base: 46 passed, 0 failed, 5 ignored. - Head: 47 passed, 0 failed, 5 ignored. The new `texture::tests::linear_copies_match_element_addresses` (7680 cases against the element-wise oracle) passes. **Benchmark** (`linear_copy_bench`, release, median of 3 runs; each run is itself a median of 9 samples). The example uses only the public API, so I ran the same file against base. Base and head runs were interleaved. Ratio is base/head, so above 1 means head is faster. | case | base ns | head ns | ratio | |---|---|---|---| | dense 256x256 detile | 7,517 | 88,766 | 0.08 | | dense 256x256 tile | 5,856 | 5,690 | 1.03 | | dense 1920x1080 detile | 186,509 | 308,252 | 0.61 | | dense 1920x1080 tile | 187,321 | 309,012 | 0.61 | | dense volume detile | 24,965 | 39,851 | 0.63 | | dense volume tile | 25,989 | 39,426 | 0.66 | | padded depth detile | 27,061 | 46,916 | 0.58 | | mip-chain volume detile / tile | 24,506 / 25,810 | 22,307 / 22,988 | 1.10 / 1.12 | | linear 1D detile / tile | 30 / 33 | 26 / 26 | 1.14 / 1.25 | | padded rows, unaligned width, BC7, tiled controls | | | 0.88-1.14 (noise) | The slowdowns reproduced in both measurement sets, with head's dense 256x256 detile at 87.7 and 88.8 us. On this CPU the single large copy is slower than the per-row copies it replaces for the dense cases (up to about 12x for 256x256 detile, about 1.6x for 1080p and the dense volume). I did not find the cause. Possibly the large `copy_from_slice` hits a slow memcpy path here, for example 4K aliasing between the two same-sized buffers, but that is a guess. It would be worth checking on other CPUs before relying on the speedup claim. Only the mip-chain volume and 1D cases improved. **Review** (diff read in full, only `crates/victory-formats` touched) - Condition is right: the fast path needs `row_pitch == bytes`, `m.pitch * n == bytes` (tiled row has no padding) and `Swizzle::LINEAR`. Planes merge only when `slice_size == slice_bytes` and `depth_pitch == slice_bytes`. A mip chain correctly falls back to the per-plane loop, because `slice_size` spans the chain. - Overflow: `slice_bytes` and `need` are still `checked_*`, and the length checks run before the fast path. `base + z * slice_size` is the same arithmetic as the old path. I found no off-by-one and no correctness bug. - The new test covers 1D/2D/3D, mips, odd widths, padded rows and depth, exact-length buffers and the error paths, and the checks are meaningful. No game files, dumps, paths, shader names or hashes were used or are included here.
AurumCarlos commented 2026-11-27 11:06:20 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AurumCarlos on 2026-11-27T11:06:20Z.

Review: CHANGES_REQUESTED


The fast path is correct: for LINEAR, split(z) is (z, 0), so base + z * slice_size matches the general path, and the 47 formats tests pass on e65fb62. But it makes large dense copies much slower with the MSVC CRT that VIctory links against. Requesting changes for that; the fix is small.

Reproduced: one large copy_from_slice hits the MSVC memcpy's large-copy path. I built the PR's own linear_copy_bench with -C target-feature=+crt-static, so the binary carries Microsoft's memcpy from libvcruntime.lib, and ran it under Wine on a Ryzen 5 9600X (base, head and fix interleaved, median of 3 runs each):

case head vs main with the fix below
dense 1920x1080 detile / tile 0.37x / 0.37x 0.88x–1.01x / 0.93x–0.98x
dense 256x256 detile / tile 1.36x / 1.51x 1.33x / 1.49x
BC7 linear detile / tile 1.20x / 1.25x 1.20x / 1.24x
volumes, 1D 1.03x–1.09x 1.02x–1.09x

With Wine's own memcpy the 1080p case doesn't slow down (0.96x–1.11x), which fits the gains measured on Linux. oni-swr's native Windows run on Zen 3 (0.61x at 1080p, 0.08x at 256x256) is the same effect, stronger on that CPU.

Suggested fix: cap each copy at 64 KiB (texture.rs:685). 16 KiB and 4 KiB chunks were no better at 1080p and gave back part of the 256x256 and BC7 gain.

             let base = (u64::from(first) * self.slice_size + m.offset) as usize;
+            // One large copy can be slower than many row copies (the MSVC
+            // CRT's large-copy path): cap each copy at 64 KiB.
+            const CHUNK: usize = 64 << 10;
+            let mut copy = |t: usize, l: usize, len: usize| {
+                let mut at = 0;
+                while at < len {
+                    let n = CHUNK.min(len - at);
+                    copy(t + at, l + at, n);
+                    at += n;
+                }
+            };
             if depth > 1 && self.slice_size == slice_bytes as u64 && depth_pitch == slice_bytes {

On Zen 3, oni-swr's "padded depth" case was slower even with 64 KiB copies, so a native rerun there would be worth it.

No test case exceeds 64 KiB, so the 7,680 cases don't reach the chunking. This test does. It passes with the fix and fails if the chunk's tiled offset is dropped (copy(t, l + at, n)), while linear_copies_match_element_addresses still passes in that case:

/// Dense linear copies larger than one 64 KiB chunk, ending mid-chunk.
#[test]
fn large_linear_copies_cross_chunk_boundaries() {
    for (dimension, width, height, depth) in [
        (Dimension::Tex2d, 256, 256, 1),
        (Dimension::Tex2d, 64, 300, 1),
        (Dimension::Tex3d, 64, 300, 3),
    ] {
        let layout = Layout::new(&Desc {
            dimension,
            width,
            height,
            depth,
            mips: 1,
            element_bytes: 4,
            block: 1,
            samples: 1,
            swizzle: Swizzle::LINEAR,
        })
        .unwrap();
        check_linear_copy(&layout, 0, 0, 0, 0);
    }
}
<!-- greyfinch-sync:Marvisak/VIctory:review:6087203514 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3477#pullrequestreview-6087203514). Originally posted by [AurumCarlos](https://github.com/AurumCarlos) on 2026-11-27T11:06:20Z. **Review:** CHANGES_REQUESTED --- The fast path is correct: for `LINEAR`, `split(z)` is `(z, 0)`, so `base + z * slice_size` matches the general path, and the 47 formats tests pass on `e65fb62`. But it makes large dense copies much slower with the MSVC CRT that VIctory links against. Requesting changes for that; the fix is small. **Reproduced: one large `copy_from_slice` hits the MSVC `memcpy`'s large-copy path.** I built the PR's own `linear_copy_bench` with `-C target-feature=+crt-static`, so the binary carries Microsoft's `memcpy` from `libvcruntime.lib`, and ran it under Wine on a Ryzen 5 9600X (base, head and fix interleaved, median of 3 runs each): | case | head vs `main` | with the fix below | |---|---|---| | dense 1920x1080 detile / tile | **0.37x / 0.37x** | 0.88x–1.01x / 0.93x–0.98x | | dense 256x256 detile / tile | 1.36x / 1.51x | 1.33x / 1.49x | | BC7 linear detile / tile | 1.20x / 1.25x | 1.20x / 1.24x | | volumes, 1D | 1.03x–1.09x | 1.02x–1.09x | With Wine's own `memcpy` the 1080p case doesn't slow down (0.96x–1.11x), which fits the gains measured on Linux. oni-swr's native Windows run on Zen 3 (0.61x at 1080p, 0.08x at 256x256) is the same effect, stronger on that CPU. **Suggested fix:** cap each copy at 64 KiB (`texture.rs:685`). 16 KiB and 4 KiB chunks were no better at 1080p and gave back part of the 256x256 and BC7 gain. ```diff let base = (u64::from(first) * self.slice_size + m.offset) as usize; + // One large copy can be slower than many row copies (the MSVC + // CRT's large-copy path): cap each copy at 64 KiB. + const CHUNK: usize = 64 << 10; + let mut copy = |t: usize, l: usize, len: usize| { + let mut at = 0; + while at < len { + let n = CHUNK.min(len - at); + copy(t + at, l + at, n); + at += n; + } + }; if depth > 1 && self.slice_size == slice_bytes as u64 && depth_pitch == slice_bytes { ``` On Zen 3, oni-swr's "padded depth" case was slower even with 64 KiB copies, so a native rerun there would be worth it. **No test case exceeds 64 KiB**, so the 7,680 cases don't reach the chunking. This test does. It passes with the fix and fails if the chunk's tiled offset is dropped (`copy(t, l + at, n)`), while `linear_copies_match_element_addresses` still passes in that case: ```rust /// Dense linear copies larger than one 64 KiB chunk, ending mid-chunk. #[test] fn large_linear_copies_cross_chunk_boundaries() { for (dimension, width, height, depth) in [ (Dimension::Tex2d, 256, 256, 1), (Dimension::Tex2d, 64, 300, 1), (Dimension::Tex3d, 64, 300, 3), ] { let layout = Layout::new(&Desc { dimension, width, height, depth, mips: 1, element_bytes: 4, block: 1, samples: 1, swizzle: Swizzle::LINEAR, }) .unwrap(); check_linear_copy(&layout, 0, 0, 0, 0); } } ```
andrewpozdnakov7-jpg commented 2026-11-28 09:54:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-28T09:54:52Z.


I combined this PR at e65fb62 with #3706 at 92b2f83 in 59ebeb5. The two exact heads merged cleanly.

The combined Windows build matched all copy offsets and lengths from this head across 3,360 generated subresources with tight and padded pitches, including 1,108 mip tails and 120 volume cases. The release suite passed with 47 passed and five ignored, with your differential linear-copy test among the passes. Strict Clippy and the asset policy passed.

The 256×256 dense linear control stayed around 20 µs. Both benchmark setups showed a gain on the 1,920×1 and 1,920×8 tiled cases. Results for 640×360 and 32×8 varied between the same-binary comparison and separate release builds, so the timings support the narrow cases most clearly.

@nothanger, the combined tree is available for review if both changes are considered together. Please retain the Git authors on cherry-pick or rebase, or add Co-authored-by: andrewpozdnakov7-jpg <[email protected]> if folding the changes into another commit.

AI-assisted with OpenAI Codex.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7237548418 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3477#issuecomment-7237548418). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-28T09:54:52Z. --- I combined this PR at `e65fb62` with #3706 at `92b2f83` in [59ebeb5](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/59ebeb588e3bcd0fb1b56809fca832f4fcdbbd57). The two exact heads merged cleanly. The combined Windows build matched all copy offsets and lengths from this head across 3,360 generated subresources with tight and padded pitches, including 1,108 mip tails and 120 volume cases. The release suite passed with 47 passed and five ignored, with your differential linear-copy test among the passes. Strict Clippy and the asset policy passed. The 256×256 dense linear control stayed around 20 µs. Both benchmark setups showed a gain on the 1,920×1 and 1,920×8 tiled cases. Results for 640×360 and 32×8 varied between the same-binary comparison and separate release builds, so the timings support the narrow cases most clearly. @nothanger, the combined tree is available for review if both changes are considered together. Please retain the Git authors on cherry-pick or rebase, or add `Co-authored-by: andrewpozdnakov7-jpg <[email protected]>` if folding the changes into another commit. AI-assisted with OpenAI Codex.
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/3477:upstream-pr/3477'
git switch 'upstream-pr/3477'

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