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

Closed
0xKaji wants to merge 2 commits from upstream-pr/1873 into main
0xKaji commented 2026-11-24 23:58:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by 0xKaji on 2026-11-24T23:58:08Z.


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).
<!-- greyfinch-sync:Marvisak/VIctory:issue:6593914346 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1873). Originally posted by [0xKaji](https://github.com/0xKaji) on 2026-11-24T23:58:08Z. --- ## 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).
catminou4 commented 2026-11-25 01:07:21 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T00:32:42Z.

Review: COMMENTED


Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; source review of the diff.

Shared scratch buffer for texture detiling instead of realloc: allocation reuse across detiles — check lifetime/reset safety.

<!-- greyfinch-sync:Marvisak/VIctory:review:6049696879 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1873#pullrequestreview-6049696879). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T00:32:42Z. **Review:** COMMENTED --- Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; source review of the diff. Shared scratch buffer for texture detiling instead of realloc: allocation reuse across detiles — check lifetime/reset safety.
sawetco commented 2026-11-25 01:07:21 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by sawetco on 2026-11-25T00:33:12Z.

Review: COMMENTED


No description provided.

<!-- greyfinch-sync:Marvisak/VIctory:review:6049701435 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1873#pullrequestreview-6049701435). Originally posted by [sawetco](https://github.com/sawetco) on 2026-11-25T00:33:12Z. **Review:** COMMENTED --- _No description provided._
sawetco commented 2026-11-25 01:08:23 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by sawetco on 2026-11-25T00:33:12Z.

Code review location: crates/victory-gpu/src/texture.rs; line 318

@@ -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).

<!-- greyfinch-sync:Marvisak/VIctory:review-comment:4688544956 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1873#discussion_r4688544956). Originally posted by [sawetco](https://github.com/sawetco) on 2026-11-25T00:33:12Z. **Code review location:** `crates/victory-gpu/src/texture.rs`; line 318 ~~~~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).
Marvisak commented 2026-11-25 08:06:38 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T07:26:40Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7155866192 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1873#issuecomment-7155866192). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T07:26:40Z. --- <!-- 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)_
0xKaji commented 2026-11-25 14:07:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by 0xKaji on 2026-11-25T13:18:52Z.


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).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7163596995 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1873#issuecomment-7163596995). Originally posted by [0xKaji](https://github.com/0xKaji) on 2026-11-25T13:18:52Z. --- 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).
upstream-sync closed this pull request 2026-11-25 16:06:25 +00:00
0xKaji commented 2026-11-25 16:09:23 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by 0xKaji on 2026-11-25T15:50:42Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7167362611 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1873#issuecomment-7167362611). Originally posted by [0xKaji](https://github.com/0xKaji) on 2026-11-25T15:50:42Z. --- 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

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