Formats/coalesce linear texture copies #3477

Open
github-bot wants to merge 3 commits from github-bot/gh-3477 into main AGit
Owner

@⁠nothanger on GitHub · 2026-11-27 06:33 UTC · original
nothanger:formats/coalesce-linear-texture-copies → main · +315 −0 · 2 files · 3 commits · open

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.

**@⁠nothanger** on GitHub · 2026-11-27 06:33 UTC · [original](https://github.com/Marvisak/VIctory/pull/3477) `nothanger:formats/coalesce-linear-texture-copies` → `main` · +315 −0 · 2 files · 3 commits · open 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.
Copy dense linear rows as planes and merge adjacent volume planes only when both memory layouts permit it. Preserve padding, mip-chain gaps and validation before writing.

Add per-element differential checks and an asset-free CPU benchmark. Linux CPU tests pass; Windows and GPU execution remain untested.
Exercise 7680 independent configurations, exact and short buffers, block-compressed and 1D layouts, and independently padded rows and depth slices. Check tile and detile against different source data.

Warm benchmark buffers explicitly and include padded-depth, block-compressed, 1D and tiled controls. Retain the original coalescing implementation: dispatch variants did not show a reliable overall improvement in isolated CPU measurements.
Keep the same contiguity guards while changing their evaluation order. Isolated x86-64 CPU comparisons retain dense-copy gains and avoid the tiled-control slowdown observed with the original order.

The expanded 7680-configuration oracle, format suites, Clippy, asset policy, and Windows/MSVC cross-compilation pass. No native Windows or GPU execution is claimed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 06:33:58 +00:00
Author
Owner

@⁠oni-swr on GitHub · 2026-11-27 07:03 UTC · original

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.

**@⁠oni-swr** on GitHub · 2026-11-27 07:03 UTC · [original](https://github.com/Marvisak/VIctory/pull/3477#issuecomment-7212102337) 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.
Author
Owner

@⁠AurumCarlos on GitHub · 2026-11-27 11:06 UTC · original · requested changes

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);
    }
}
**@⁠AurumCarlos** on GitHub · 2026-11-27 11:06 UTC · [original](https://github.com/Marvisak/VIctory/pull/3477#pullrequestreview-6087203514) · **requested changes** 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); } } ```
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3477/head:github-bot/gh-3477'
git switch 'github-bot/gh-3477'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3477'
git switch 'github-bot/gh-3477'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3477'
git switch 'github-bot/gh-3477'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3477'
git switch 'main'
git merge --squash 'github-bot/gh-3477'
git switch 'main'
git merge --ff-only 'github-bot/gh-3477'
git switch 'main'
git merge 'github-bot/gh-3477'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3477
No description provided.