GPU: share a scratch buffer for texture detiling instead of retaining one per Staging #1871
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1873"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by 0xKaji on 2026-11-24T23:58:08Z.
Summary
Staging::lastincrates/victory-gpu/src/texture.rskept aread: Vec<u8>buffer inside everyStaginginstance. On watched title textures,refresh_now()resizedlast.readtolayout.sizeand kept that allocation alive for the lifetime of theStagingobject — even thoughStaging::detileimmediately copies the detiled texels into the mapped D3D12 upload buffer andlast.readis never read again until the next refresh.Across hundreds/thousands of uploaded textures, each
Stagingpermanently 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
read: Vec<u8>from per-StagingLast.static READ_SCRATCH: Mutex<Vec<u8>> = Mutex::new(Vec::new())inStaging::refresh_now()so detiling scratch memory isO(max_texture_size)rather thanO(sum of all uploaded texture sizes).Note:
Staging::refreshruns on the submission thread (serializingBatch::texture_duerefreshes), so a singleMutex<Vec<u8>>adds no contention on the hot path.Verified
cargo xtask policy:asset policy: 174 files okcargo xwin clippy -p victory-gpu --tests --target x86_64-pc-windows-msvc --offline -- -D warnings: 0 warningsresource::tests::buffer_data_reaches_the_host_texture --test-threads=1on Linux + Wine11.22+vkd3d-proton 3.2.0+ NVIDIA RTX 3060): 1 passed, 0 failed.3,953 MiBVRAM,0Xids).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.
Imported from Marvisak/VIctory. Originally posted by sawetco on 2026-11-25T00:33:12Z.
Review: COMMENTED
No description provided.
Imported from Marvisak/VIctory. Originally posted by sawetco on 2026-11-25T00:33:12Z.
Code review location:
crates/victory-gpu/src/texture.rs; line 318A smaller texture shrinks the vector’s length while keeping its capacity. When the next upload is larger,
resize(size, 0)zeroes the tail immediately beforecopy_mappedfills 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.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 viaReadProcessMemoryand runs this commit’sLayout::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
4eea047and ranresource::tests::buffer_data_reaches_the_host_texturewith a D3D12 device required. It passed.AI-assisted benchmark and review (Codex).
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 ontomain940d32f. It removes the per-textureLast::readbuffer incrates/victory-gpu/src/texture.rs. Before, every watchedStagingkept a CPU copy of its whole tiled texture for as long as it lived.Staging::refresh_nownow reads into one sharedREAD_SCRATCHbuffer, anddetilecopies out of it straight away, so nothing read the old per-texture copy afterwards. The scratch lock is always taken after the staging's ownlastlock, and nothing insidedetiletakes 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
checknow passes on81ebd7c, so the earlier unrelatedvictory-shaderfailures are gone.CI: passing (
check).Generated by Claude Code
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. Verifiedcargo xtask policy(182 files ok) andcargo xwin clippy -p victory-gpu --tests --target x86_64-pc-windows-msvc --offline -- -D warnings(0 warnings).Imported from Marvisak/VIctory. Originally posted by 0xKaji on 2026-11-25T15:50:42Z.
Superseded by
8f2a6f1on main, which already removes per-Staging Last::read and reuses a thread-local Scratch buffer with a 32 MiB retention cap.Pull request closed