Skip to content

Reuse one CPU read buffer for watched textures - #215

Closed
tcurtsinger wants to merge 3 commits into
Marvisak:mainfrom
tcurtsinger:perf/reuse-watched-texture-scratch
Closed

tcurtsinger wants to merge 3 commits into
Marvisak:mainfrom
tcurtsinger:perf/reuse-watched-texture-scratch

Conversation

@tcurtsinger

@tcurtsinger tcurtsinger commented Nov 24, 2026 •

Copy link
Copy Markdown

A watched texture kept a second CPU copy of its tiled pixels in Last::read for as long as its staging resource lived. The upload only needs those bytes while it detiles. This change reuses one buffer per submitting thread. Each texture keeps its own upload buffer and write-watch timestamp. If the scratch buffer cannot grow, the refresh keeps the previous staging contents and retries on a later submission.

Memory measurement

I refreshed 32 synthetic 1024 × 512 RGBA8 textures on one thread using Windows 11 and D3D12 WARP. Each texture occupied 2 MiB in write-watched memory. I pre-touched the source and mapped upload buffers, kept all 32 textures alive, and measured the increase in process private bytes across their first refresh. Each revision ran in three fresh processes.

Revision Median increase Range
Main (d2a4bae) 64.31 MiB 64.31–64.32 MiB
Original scratch-buffer revision (0f7052e) 2.08 MiB 2.06–2.08 MiB

All 32 uploads had their expected pixel values. A second refresh read zero bytes for each clean texture. After I rewrote one texture, it refreshed and the other 31 kept their pixels. The measurement covers synthetic uploads.

The native Windows 11 run on our RTX 4080 reached the first mission at 1–2 FPS. The sampled peaks on d2a4bae were 22.74 GiB of private commit and 13.87 GiB resident. With this change alone they were 22.44 GiB and 13.37 GiB. Both runs ended with DXGI_ERROR_DEVICE_HUNG. Scene timing and sampling intervals differed, so the size of any whole-game memory or frame-time improvement remains uncertain. Baseline result · Subsequent combined test.

Checks

  • The regression tests cover scratch reuse across texture sizes, zero-filled bytes for unmapped pages, and retrying the original dirty pages after a failed reservation. The latter covers both the first refresh and a later change. Retry regression
  • On Windows D3D12 WARP, a synthetic test allocator refused a 2 MiB scratch allocation during the first refresh and again on a later dirty refresh. Both times the staging pixels and write-watch number stayed intact. The next refresh of the same 2 MiB texture produced the expected pixels without another write to its source.
  • At 440738e the Windows workspace passed 345 tests with default features and 349 with diagnostics. Six were ignored in each run. The two workspace Clippy configurations passed with warnings denied. The asset policy, diff check and rustfmt on the changed file passed.

At the current head of #143 (525a713), a test merge finds one conflict at the thread-local buffer declaration. Its unwatched path calls the buffer UNWATCHED_SCRATCH. Both paths can use one allocation by keeping SCRATCH and using that name in the unwatched path when the changes are combined. Related PR #143

AntoninMichelq pushed a commit to AntoninMichelq/VIctory that referenced this pull request Nov 24, 2026
…s) into the integration

Folds the branch's own READ thread-local into the PR's with_tiled_copy
(SCRATCH), keeping the branch's drop of one-off reads over 64 MiB and
the staging budget/eviction.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AZ9pSB4ZoXYUifuXg8gm66
@upipibi upipibi mentioned this pull request Nov 24, 2026
@jeremygold02

Copy link
Copy Markdown
Contributor

Thanks @tcurtsinger for covering both reuse of the watched-texture read buffer and stale data from unmapped pages.

I tested commit 0f7052e38ac18d58612619c3f612f9050b9769e1 with:

cargo test --locked -p victory-gpu --lib watched_texture_reads_reuse_space_and_expose_only_current_bytes -- --test-threads=1
cargo test --locked -p victory-gpu --lib unmapped_texture_pages_do_not_keep_previous_pixels -- --test-threads=1

Both tests passed (1 passed, 0 failed; 45 filtered for each):

These tests cover the CPU-side tiled-copy buffer and unmapped-page handling. They do not exercise texture upload through a rendered title or GPU sampling.

@ghost

ghost commented Nov 24, 2026

Copy link
Copy Markdown

I opened #495 with the same change before seeing this one; it is a duplicate of #215 and I am closing it in favour of this one. One thing from it may be worth taking, since this PR has the tests.

When the host runs out of Windows commit, read.reserve_exact(..) / resize(..) in with_tiled_copy aborts the whole process. I saw exactly that on an RTX 3070 Ti while in the world with the debug layer on (commit about 52 GB of a 48 to 54 GB limit): memory allocation of 7372800 bytes failed, then again at 12582912 bytes. Rust prints that line and aborts, so crashes.log gets nothing.

#495 reserved with try_reserve_exact instead. If that fails it logs once and skips that texture's upload (return 0), which keeps the process alive. One detail that matters if you adopt it: in this version last.asked = now is set before the copy, so a skipped upload would leave the texture marked as up to date. In #495 the reservation happens first and asked is only set after it succeeds, so the texture asks to be detiled again next time:

let mut read = READ.take();
read.clear();
if read.try_reserve_exact(size).is_err() {
    once!(size, "no room to read a texture's memory; not uploaded");
    READ.set(read);
    return 0;
}
last.asked = now;

I did not test that failure branch under real memory pressure. With the equivalent change here (and a larger pagefile at the same time), process private memory stayed flat at about 21 to 22 GB over a 20-minute run.

AI-assisted (Claude Code).

@tcurtsinger

Copy link
Copy Markdown
Author

Thanks for the report in #495. The scratch buffer now grows with try_reserve_exact. A failed reservation logs once, leaves the staging pixels and write-watch number intact, and returns zero bytes refreshed. The dirty texture stays eligible for the next submission. Change in 440738e

I verified this on Windows D3D12 WARP with a test allocator that refused a 2 MiB request on the first refresh and again on a later update. After each refusal, the same-size refresh succeeded and produced the expected pixels with no new title writes. The committed regression also covers a clean refresh after recovery. This measures the allocation failure path. The effect during a low-memory game session remains unmeasured. Regression test

AI-assisted implementation and testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants