GPU: share a scratch buffer for texture detiling instead of retaining one per Staging #1873

Closed
github-bot wants to merge 2 commits from gh-pr/1873/head into gh-pr/1873/base
Owner

@⁠0xKaji on GitHub · 2026-11-24 23:58 UTC · original
0xKaji:fix/texture-staging-scratch → main · +12 −5 · 1 files · 2 commits · closed without merging

Summary

Staging::last in crates/victory-gpu/src/texture.rs kept a read: Vec<u8> buffer inside every Staging instance. On watched title textures, refresh_now() resized last.read to layout.size and kept that allocation alive for the lifetime of the Staging object — even though Staging::detile immediately copies the detiled texels into the mapped D3D12 upload buffer and last.read is never read again until the next refresh.

Across hundreds/thousands of uploaded textures, each Staging permanently retained a full-size CPU copy of the tiled texture alongside its mapped D3D12 upload buffer, adding unnecessary host RAM / commit pressure (especially noticeable on 16 GiB RAM systems).

Change

  • Remove read: Vec<u8> from per-Staging Last.
  • Use a single shared static READ_SCRATCH: Mutex<Vec<u8>> = Mutex::new(Vec::new()) in Staging::refresh_now() so detiling scratch memory is O(max_texture_size) rather than O(sum of all uploaded texture sizes).

Note: Staging::refresh runs on the submission thread (serializing Batch::texture_due refreshes), so a single Mutex<Vec<u8>> adds no contention on the hot path.

Verified

  • cargo xtask policy: asset policy: 174 files ok
  • cargo xwin clippy -p victory-gpu --tests --target x86_64-pc-windows-msvc --offline -- -D warnings: 0 warnings
  • Wine GPU test (resource::tests::buffer_data_reaches_the_host_texture --test-threads=1 on Linux + Wine 11.22 + vkd3d-proton 3.2.0 + NVIDIA RTX 3060): 1 passed, 0 failed.
  • Live 120 s Series S run on a 15 GiB RAM + RTX 3060 8 GB Linux/Wine host survived the full window (3,953 MiB VRAM, 0 Xids).
**@⁠0xKaji** on GitHub · 2026-11-24 23:58 UTC · [original](https://github.com/Marvisak/VIctory/pull/1873) `0xKaji:fix/texture-staging-scratch` → `main` · +12 −5 · 1 files · 2 commits · closed without merging ## Summary `Staging::last` in `crates/victory-gpu/src/texture.rs` kept a `read: Vec<u8>` buffer inside every `Staging` instance. On watched title textures, `refresh_now()` resized `last.read` to `layout.size` and kept that allocation alive for the lifetime of the `Staging` object — even though `Staging::detile` immediately copies the detiled texels into the mapped D3D12 upload buffer and `last.read` is never read again until the next refresh. Across hundreds/thousands of uploaded textures, each `Staging` permanently retained a full-size CPU copy of the tiled texture alongside its mapped D3D12 upload buffer, adding unnecessary host RAM / commit pressure (especially noticeable on 16 GiB RAM systems). ## Change - Remove `read: Vec<u8>` from per-`Staging` `Last`. - Use a single shared `static READ_SCRATCH: Mutex<Vec<u8>> = Mutex::new(Vec::new())` in `Staging::refresh_now()` so detiling scratch memory is `O(max_texture_size)` rather than `O(sum of all uploaded texture sizes)`. Note: `Staging::refresh` runs on the submission thread (serializing `Batch::texture_due` refreshes), so a single `Mutex<Vec<u8>>` adds no contention on the hot path. ## Verified - `cargo xtask policy`: `asset policy: 174 files ok` - `cargo xwin clippy -p victory-gpu --tests --target x86_64-pc-windows-msvc --offline -- -D warnings`: **0 warnings** - Wine GPU test (`resource::tests::buffer_data_reaches_the_host_texture --test-threads=1` on Linux + Wine `11.22` + `vkd3d-proton 3.2.0` + NVIDIA RTX 3060): **1 passed, 0 failed**. - Live 120 s Series S run on a 15 GiB RAM + RTX 3060 8 GB Linux/Wine host survived the full window (`3,953 MiB` VRAM, `0` Xids).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:47:17 +00:00
github-bot closed this pull request 2026-11-26 04:47:17 +00:00
Author
Owner

@⁠sawetco on GitHub · 2026-11-25 00:33 UTC · original
On crates/victory-gpu/src/texture.rs line 318 (outdated):

@@ -309,9 +312,10 @@ impl Staging {
             }
             // Read through the copy, which stands unmapped pages: memory of
             // big pools is too slow to ask page by page.
-            last.read.resize(size, 0);
-            crate::host::copy_mapped(last.read.as_mut_ptr(), self.memory, size, false);
-            self.detile(&last.read);
+            let mut scratch = READ_SCRATCH.lock().unwrap_or_else(|e| e.into_inner());
+            scratch.resize(size, 0);
+            crate::host::copy_mapped(scratch.as_mut_ptr(), self.memory, size, false);
+            self.detile(&scratch);

A smaller texture shrinks the vector’s length while keeping its capacity. When the next upload is larger, resize(size, 0) zeroes the tail immediately before copy_mapped fills it. Alternating 256² and 2048² RGBA8 textures writes an extra 15.75 MiB of zeros per pair after warm-up. Keeping the largest initialized length still uses one allocation and gives the detiler exactly the current texture’s bytes.

            if scratch.len() < size {
                scratch.resize(size, 0);
            }
            let tiled = &mut scratch[..size];
            crate::host::copy_mapped(tiled.as_mut_ptr(), self.memory, size, false);
            self.detile(tiled);

I tested both paths on a Ryzen 5 7600X, native Windows 11, rustc 1.100.0 MSVC -O. The single-thread harness reads synthetic, fully mapped RGBA8 buffers via ReadProcessMemory and runs this commit’s Layout::detile (swizzle 10). Each fresh process alternated the two implementations, with 3 warm-up rounds and 10 measured rounds of 16 pairs. Across three processes, median time per mixed-size pair fell from 4.93 to 4.51 ms. The change was faster in 29/30 paired rounds. Two 2048² uploads gave 8.59 and 8.60 ms. The complete output buffers matched after large → small → large. These are CPU upload-preparation timings.

I also applied the suggestion to 4eea047 and ran resource::tests::buffer_data_reaches_the_host_texture with a D3D12 device required. It passed.

AI-assisted benchmark and review (Codex).

**@⁠sawetco** on GitHub · 2026-11-25 00:33 UTC · [original](https://github.com/Marvisak/VIctory/pull/1873#discussion_r4688544956) On `crates/victory-gpu/src/texture.rs` line 318 (outdated): ```diff @@ -309,9 +312,10 @@ impl Staging { } // Read through the copy, which stands unmapped pages: memory of // big pools is too slow to ask page by page. - last.read.resize(size, 0); - crate::host::copy_mapped(last.read.as_mut_ptr(), self.memory, size, false); - self.detile(&last.read); + let mut scratch = READ_SCRATCH.lock().unwrap_or_else(|e| e.into_inner()); + scratch.resize(size, 0); + crate::host::copy_mapped(scratch.as_mut_ptr(), self.memory, size, false); + self.detile(&scratch); ``` A smaller texture shrinks the vector’s length while keeping its capacity. When the next upload is larger, `resize(size, 0)` zeroes the tail immediately before `copy_mapped` fills it. Alternating 256² and 2048² RGBA8 textures writes an extra 15.75 MiB of zeros per pair after warm-up. Keeping the largest initialized length still uses one allocation and gives the detiler exactly the current texture’s bytes. ```suggestion if scratch.len() < size { scratch.resize(size, 0); } let tiled = &mut scratch[..size]; crate::host::copy_mapped(tiled.as_mut_ptr(), self.memory, size, false); self.detile(tiled); ``` I tested both paths on a Ryzen 5 7600X, native Windows 11, rustc 1.100.0 MSVC `-O`. The single-thread harness reads synthetic, fully mapped RGBA8 buffers via `ReadProcessMemory` and runs this commit’s `Layout::detile` (swizzle 10). Each fresh process alternated the two implementations, with 3 warm-up rounds and 10 measured rounds of 16 pairs. Across three processes, median time per mixed-size pair fell from **4.93 to 4.51 ms**. The change was faster in 29/30 paired rounds. Two 2048² uploads gave **8.59 and 8.60 ms**. The complete output buffers matched after large → small → large. These are CPU upload-preparation timings. I also applied the suggestion to `4eea047` and ran `resource::tests::buffer_data_reaches_the_host_texture` with a D3D12 device required. It passed. AI-assisted benchmark and review (Codex).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 07:26 UTC · original · edited 2026-11-25 14:50 UTC

Automated triage for the maintainers.

Merge candidate. Re-checked at 81ebd7c, which is the same change rebased onto main 940d32f. It removes the per-texture Last::read buffer in crates/victory-gpu/src/texture.rs. Before, every watched Staging kept a CPU copy of its whole tiled texture for as long as it lived. Staging::refresh_now now reads into one shared READ_SCRATCH buffer, and detile copies out of it straight away, so nothing read the old per-texture copy afterwards. The scratch lock is always taken after the staging's own last lock, and nothing inside detile takes it again, so the lock order is consistent. The cost is that refreshes of different watched textures run one at a time, and the scratch buffer keeps the size of the largest texture read so far.

Reply to @⁠0xKaji: thanks for the rebase. CI check now passes on 81ebd7c, so the earlier unrelated victory-shader failures are gone.

CI: passing (check).


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 07:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/1873#issuecomment-7155866192) · edited 2026-11-25 14:50 UTC <!-- victory-triage v1 head=81ebd7c63f92ca04643c79261f31e1f615bd9241 verdict=merge-candidate at=2026-11-25T14:50:21Z --> Automated triage for the maintainers. **Merge candidate.** Re-checked at `81ebd7c`, which is the same change rebased onto `main` `940d32f`. It removes the per-texture `Last::read` buffer in `crates/victory-gpu/src/texture.rs`. Before, every watched `Staging` kept a CPU copy of its whole tiled texture for as long as it lived. `Staging::refresh_now` now reads into one shared `READ_SCRATCH` buffer, and `detile` copies out of it straight away, so nothing read the old per-texture copy afterwards. The scratch lock is always taken after the staging's own `last` lock, and nothing inside `detile` takes it again, so the lock order is consistent. The cost is that refreshes of different watched textures run one at a time, and the scratch buffer keeps the size of the largest texture read so far. Reply to @⁠0xKaji: thanks for the rebase. CI `check` now passes on `81ebd7c`, so the earlier unrelated `victory-shader` failures are gone. CI: passing (`check`). --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠0xKaji on GitHub · 2026-11-25 13:18 UTC · original

Rebased onto main (940d32f) to pick up the hardware-test skip fixes and re-run CI. Verified cargo xtask policy (182 files ok) and cargo xwin clippy -p victory-gpu --tests --target x86_64-pc-windows-msvc --offline -- -D warnings (0 warnings).

**@⁠0xKaji** on GitHub · 2026-11-25 13:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/1873#issuecomment-7163596995) Rebased onto `main` (`940d32f`) to pick up the hardware-test skip fixes and re-run CI. Verified `cargo xtask policy` (182 files ok) and `cargo xwin clippy -p victory-gpu --tests --target x86_64-pc-windows-msvc --offline -- -D warnings` (0 warnings).
Author
Owner

@⁠0xKaji on GitHub · 2026-11-25 15:50 UTC · original

Superseded by 8f2a6f1 on main, which already removes per-Staging Last::read and reuses a thread-local Scratch buffer with a 32 MiB retention cap.

**@⁠0xKaji** on GitHub · 2026-11-25 15:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/1873#issuecomment-7167362611) Superseded by 8f2a6f1 on main, which already removes per-Staging Last::read and reuses a thread-local Scratch buffer with a 32 MiB retention cap.

Pull request closed

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!1873
No description provided.