WIP: perf(vk): an evicted GPU-drawn texture's write-back waits for nothing, so an area change no longer stalls the frame for it #2789
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/mods
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!2789
Loading…
Reference in a new issue
No description provided.
Delete branch "HumanBean/Longshot:perf/evict-writeback-no-wait"
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?
Summary
Evicting a texture the GPU drew could freeze the game for a third of a second. To evict such a texture, the renderer first copies its texels back into the game's memory. It recorded that copy, submitted it, and waited for it (
readback_submit_wait) inside the present job. That drained the whole GPU queue, and the game's main thread waited the whole time on the present's fence. On an RX 580 (8 GB, RADV, at 6.6 of 7.4 GB of VRAM), one such frame took 320 ms:tex_frame_ms 318.7, main_blocked_ms 317.8. A comment atreadback_submit_waitalready noted a 317 ms wait at an area change.Now the eviction write-back works the way the small drawn textures' write-backs already did (
drawn_writeback_frame): nothing waits.retire_recordputs the texture's copies into this present's uploads, and the texture stays in the cache, retiring.retire_finishdecides each texture's fate (evict_retire::step):evict_retire::pending), so it doesn't evict again for the same excess.RX_VK_EVICT_WRITEBACK_WAIT=1restores the old wait unchanged (a kill switch, off by default).Read first:
gpu/vk/evict_retire.h, thenretire_record/retire_finishand the eviction pass ingpu/vk/vk_texture.cpp.Testing
Tested:
--mode build:dev.py vk: the renderer builds.RX_TARGET=windowswith llvm-mingw:longshot_vk.dlllinks. Its exports check was skipped, becausedev.py vklooks for a.soafterwards.run_all.py --strict evict_ readback_timeout target_retention drawn_writeback: 6 of 6 pass. That includes the newtests/test_evict_retire.py(the rules and the source) and the updated stand-ins intest_readback_timeout_reuse.py(which now drives the switch-on path) andtest_target_retention.cpp.run_all.pyhad 15 failures outside this change (launcher switch table, guest holes, lifter, input, …). Whether each also fails onmainisn't checked yet.894683cfwith this branch (and #2790) merged. 2560x1440 output, 1920x1080 internal, no upscaler, uncapped. Each run is 3 laps of: warp to the Market, walk back into Hyrule Field, forward for 30 s (about 6 min). Runs went new, old, new, old, with the old side set byRX_VK_EVICT_WRITEBACK_WAIT=1.vk: 7 evicted textures the GPU drew retired after their copies, 7 written into their memory in 2.5 ms, 1 frames after. Each run had 7–9 such textures, 3–15 ms in all, with 0 failed and 0 gone meanwhile.tex_frame_msstayed ≤ 0.6 ms in every worst frame, so this route can't show the gain. The stall was seen in free play, at about 6.6 of 7.4 GB of VRAM.Still to test:
tex_frame_msframes, againstRX_VK_EVICT_WRITEBACK_WAIT=1(by hand).Measurements
Verdict: no change on this route, inside the run-to-run spread both ways. The stall it removes didn't happen in these runs. It is a correctness-neutral removal of a wait that was seen at 320 ms in free play.
Setup: as in Tested; 2 runs each side, interleaved; frames.csv (
RX_VK_FRAME_TRACE=1) and the log.RX_VK_EVICT_WRITEBACK_WAIT=1(main's path)f6f32217tex_frame_msThe ≥250 ms frames on both sides are area-change texture uploads (about 100–200 MB in one frame) and the game's own loading, not evictions.
Risks
memory_written_runs, at page granularity.RX_VK_EVICT_WRITEBACK_WAIT=1, or revert.Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named). (15 failures outside this change, not yet checked againstmain.)main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
mainand passes here. (No game-free test can show a GPU fence wait; the rules have their own test.)gpu/vk/switches.def. (A kill switch, off.)tests/that reads changed source is updated, not weakened.Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.🤖 Generated with Claude Code
Possibly related, for context:
@review
Recommendation: Request changes. A retiring write-back can outlive a guest-memory remap and write the old texture into the new mapping. The claimed reduction in the eviction stall also remains unproven on the measured route.
The change moves eviction copies into the present submission and defers their write-back and destruction, avoiding an explicit GPU wait in the eviction pass. It keeps images and staging memory alive longer and adds a kill switch; the delayed write-back needs to account for memory remapping.
Blocking (1): must change before merging.
gpu/vk/vk_texture.cpp:5989: A pending retirement is not cancelled or completed when the texture's guest pages are remapped. (more on the line) Do: Resolve or cancel overlapping retirement entries before the mapping changes, and add a test covering a remap between recording and retirement completion.Should fix (1): not blocking, but worth doing before merging.
tests/test_evict_retire.py:8: The new check hard-codes g++ instead of using the compiler selected by the test runner. (more on the line) Do: Use the runner's {cxx} placeholder and make the standalone default respect the configured compiler.Testing before merging (2) (runs on the systems it can change; it doesn't hold the recommendation)
gpu/vk/vk_texture.cpp:6833: The measured route did not exercise the stall this change is intended to remove. (more on the line) Do: Run interleaved base and branch measurements on a reproducible eviction-heavy route, including frame-time tails and RAM and VRAM usage.gpu/vk/vk_texture.cpp:5950: The deferred write-back and image lifetime have not been checked on the supported Tier 1 systems. (more on the line) Do: Check the recorded screenshots and run eviction-heavy area changes on the affected Tier 1 Windows and Linux hardware and Steam Deck, checking texture correctness and memory pressure.@fixcan do L1 and L2.Checks: Failing on this head, which is behind its base, where they pass:
CI / Third-party parts and guide screenshots (pull_request),CI / Rust (rustfmt, clippy, cargo test, end to end) (pull_request),CI / Game-free checks (run_all.py, part 1/2) (pull_request),CI / Game-free checks (run_all.py, part 2/2) (pull_request).@rebase(or@fix) brings it up to date and runs them again; not held against it.More: does it help, other systems, switches, what was read
Does it help: Unproven. The PR reports a 320 ms eviction stall in free play, but it did not recur on either side of the two interleaved runs. Median fps was 40.7–41.1 before and 39.9–40.9 after; the reported 1% lows were 11.4–13.3 before and 9.9–11.7 after.
Other systems: The default path affects texture eviction, guest-memory write-back, GPU submissions and memory pressure on both Windows and Linux and across GPU vendors. The reported game run covers Linux RADV on an RX 580; screenshots and Tier 1 Windows, Linux and Steam Deck coverage remain outstanding.
Switches: RX_VK_EVICT_WRITEBACK_WAIT is added, default off; setting it to 1 restores the synchronous path. It has a switches.def row, but measurements have not yet established the default's benefit or costs across supported systems.
Reviewed:
f6f3221773against3ab4db6e91: Read the repository rules, PR template, standards, diff, commit, new retirement header and tests, and the changed eviction and read-back code with its relevant callers. Skimmed the remaining portions of the large changed files and existing test fixtures; ../Tooling/AGENTS.md and docs/review-checklist.md were absent.; nothing run (a code review).Checked and fine:
Testing: No commands from the project were run, as requested. The PR reports successful Linux and Windows renderer builds and six targeted checks; its full test run had 15 failures not yet compared with the base.
Overlaps and merge order: Not checked; network access is closed.
Automated code review by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.
@ -5895,0 +5947,4 @@}/// The retiring textures the GPU is done copying: their texels into their memory where the texture is still as it was chosen,/// then their end; the ones used since live againvoid retire_finish() {L4 (should fix): The deferred write-back and image lifetime have not been checked on the supported Tier 1 systems.
When: The only reported game run uses Linux RADV on an RX 580, and its screenshots have not yet been checked; the changed GPU copy and memory-pressure behavior also runs on Windows, other drivers and Steam Deck.
Do: Check the recorded screenshots and run eviction-heavy area changes on the affected Tier 1 Windows and Linux hardware and Steam Deck, checking texture correctness and memory pressure.
@ -5895,0 +5986,4 @@}bool ok = false;if (s == evict_retire::Step::Write) {ok = flush_finish(r.f, true); // (frees its staging buffer)L1 (blocking): A pending retirement is not cancelled or completed when the texture's guest pages are remapped.
When: After retire_record captures a texture, vmap_changing can call texture_flush_to_guest for an overlapping range before the GPU copy completes. That writes the old mapping before the remap, but leaves the retirement entry alive; retire_finish later writes its captured texels through the same GPU address into the replacement mapping, corrupting its contents.
Do: Resolve or cancel overlapping retirement entries before the mapping changes, and add a test covering a remap between recording and retirement completion.
@ -6691,2 +6831,3 @@// which go once the GPU is past their copies: retire_finish)std::unordered_set<const Texture*> flush_later;{ // (the write-backs: batches of up to 64 textures or ~512 MB of staging memory, each one submission and one wait)if (!evict_writeback_wait()) { // (the write-backs: their copies with this present's uploads, finished later: retire_finish)L3 (should fix): The measured route did not exercise the stall this change is intended to remove.
When: Both paths stayed at or below 0.6 ms of texture-frame time in their worst frames, so the reported runs cannot show whether the asynchronous path removes the observed 320 ms eviction stall.
Do: Run interleaved base and branch measurements on a reproducible eviction-heavy route, including frame-time tails and RAM and VRAM usage.
@ -0,0 +5,4 @@import tempfilefrom pathlib import PathRUN_ALL_ARGS = ["--cxx", "g++"]L2 (should fix): The new check hard-codes g++ instead of using the compiler selected by the test runner.
When: On a supported Windows or Linux test machine with clang++ selected and no g++ installed, run_all skips this check, or fails it in strict mode, even though a supported C++ compiler is available.
Do: Use the runner's {cxx} placeholder and make the standalone default respect the configured compiler.
f6f32217732d07f9f257Review addressed in
2d07f9f2(amended into the one commit).L1 (remap between recording and retirement): fixed.
texture_flush_to_guest(whatvmap_changingcalls) now first callsretire_cancel_remapped(lo, hi): every retirement whose storage overlaps the range is markedremapped, and its texture, if still in the cache under the same id, is live again (retiring = false), so the loop below writes it into the old mapping right then.evict_retire::steptakesremappedand returns a newStep::Remappedonce the GPU is past the copies: only the staging buffer is freed, nothing goes through the GPU address into the new mapping. The staging buffer still waits for the copies (Waitcomes first).retire_finishalso skips a remapped entry's texture lookup, so a texture chosen again after the remap gets a fresh entry and the old one can't act on it.tests/test_evict_retire.cppgets the remap cases (remapped and copies still running → Wait; remapped → Remapped, also when gone meanwhile or drawn over), and the Python source check pins the call intexture_flush_to_guest. The architecture page says what a remap does to a retiring texture.L2 (hard-coded g++): fixed.
RUN_ALL_ARGS = ["--cxx", "{cxx}"]; standalone, the default is$CXX, elsec++. Passes with g++ and clang++.L3 / L4 (eviction-heavy A/B, Tier 1 hardware): not done in this push. They need game runs, and none were run for this update. The open measurement and the Windows, Linux and Steam Deck coverage still stand; the PR stays WIP until they're done.
Checks:
test_evict_retire.py(g++ and clang++),test_readback_timeout_reuse.pyandtest_target_retention.pypass; the renderer compiles for Linux (a pack on the shared base).View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.