Reuse one CPU read buffer for watched textures #215
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!215
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/215/head"
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?
@tcurtsinger on GitHub · 2026-11-24 01:29 UTC · original
tcurtsinger:perf/reuse-watched-texture-scratch→main· +147 −15 · 1 files · 3 commits · closed without mergingA watched texture kept a second CPU copy of its tiled pixels in
Last::readfor 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.
d2a4bae)0f7052e)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
d2a4baewere 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 withDXGI_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
440738ethe 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 bufferUNWATCHED_SCRATCH. Both paths can use one allocation by keepingSCRATCHand using that name in the unwatched path when the changes are combined. Related PR #143@jeremygold02 on GitHub · 2026-11-24 05:06 UTC · original
Thanks @tcurtsinger for covering both reuse of the watched-texture read buffer and stale data from unmapped pages.
I tested commit
0f7052e38ac18d58612619c3f612f9050b9769e1with:Both tests passed (1 passed, 0 failed; 45 filtered for each):
watched_texture_reads_reuse_space_and_expose_only_current_bytescopies large, small, then large input through the helper, checking the bytes each time and that the same allocation is reused.unmapped_texture_pages_do_not_keep_previous_pixelsseeds prior data, commits only the first of two reserved pages, and verifies the copied mapped page has its new bytes while the unmapped page is zeroed instead of retaining old pixels.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 on GitHub · 2026-11-24 06:00 UTC · original
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(..)inwith_tiled_copyaborts 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, socrashes.loggets nothing.#495 reserved with
try_reserve_exactinstead. 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 versionlast.asked = nowis set before the copy, so a skipped upload would leave the texture marked as up to date. In #495 the reservation happens first andaskedis only set after it succeeds, so the texture asks to be detiled again next time: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 on GitHub · 2026-11-24 15:39 UTC · original
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 in440738eI 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.
Pull request closed