fix(vk): no more game stalls waiting for GPU work it does not need (culling-table reads wait only for their own bytes) #2366
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
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
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!2366
Loading…
Reference in a new issue
No description provided.
Delete branch "Veix/Longshot:fix/readback-shared-page"
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
At 4K the game's job threads stood still in reads of OoT's culling tables, waiting for GPU work that never touches the bytes they read: the table's other slot shares a 4 KB page with them.
OoT keeps four culling tables double-buffered: two slots back to back, of 0x9b040, 0x44b040, 0x8b040 and 0x1b580 bytes, none a whole number of 4 KB pages, so the two slots meet inside one page. Each frame the GPU writes one slot and the game's CPU reads the other, last frame's, long finished. The write-back notes in
vk_memory.cppare kept per 4 KB page, and a read waits for the newest part that wrote any of its pages: the GPU's write of this frame's slot put the shared page on the current part, and the read of the other slot waited for it. Where the GPU runs behind (4K), such a wait took 1 to 8 ms, and the job threads waited with it.Measured on
mainwith a diagnostic (not in this PR) on the Castle Town route at 4K: about 10,700 reads of each table waited (5,400 of 0x1b580), every one for a part that wrote the neighbouring slot on the shared page, and not one byte of the read range changed during the wait. The 588 waits over 1 ms in that run (2 s in all) were all of this kind.This keeps, for a page that a write-back covers only in part, which bytes it covered and what the page held before it: two more page tables beside
wb_pages/wb_parts, which stay as they are. A read that covers its first or last page only in part waits there for the last write if it touches the read's bytes and for the earlier state if it touches others; every page between is whole, as before. Nothing compares which write is newer (writes in one part or with one value need no case of their own), nothing is evicted, and a read never waits longer than before. Page copies and refreshes stay page-based. With it no read of the four tables waits on the route, and the frames that waited are gone: below.To see it on
main: the Castle Town market at 3840x2160 with DLAA, the frame trace on (RX_VK_FRAME_TRACE=1): frames over 20 ms come with the game's own time (game_ms) at 14 to 20 ms against 8 normally.Read first: the comment above
wb_spans_onandwb_edge_note/wb_edge_takeingpu/vk/vk_memory.cpp, thenmemory_writeback_value's first and last page, thentests/test_wb_spans.py.This gives what players got from
RX_VK_READBACK_LATE=3980,9b040,8b040,44b040,1b580, exactly instead of late. That list (passed around among players, and in #1725's discussion) read the four tables without their wait, which removed these stalls but would also have handed the game stale bytes whenever a table's own write was still pending. Here the reads skip only the wait for the other slot, and still wait whenever their own bytes are being written: the same result without reading anything late. On the same PC and route at 4K the list and this change measured the same, 74 frames over 20 ms each against 170 onmain(one run each, the Market's second stay; the interleaved runs below are againstmainonly).RX_VK_READBACK_LATE's default (0x3980, the culling camera block) is unchanged: that block is a genuine wait, its bytes are written by the part the read waits for. The four table sizes are no longer needed in it.Testing
Please no automatic fixes on this branch (
@fix, bot pushes): review comments are welcome, I make the changes myself.Tested: Windows 11, RTX 4070 SUPER 12 GB, driver 621.24, Ryzen 9 9900X. The Castle Town route (Market, castle, Market) at 3840x2160 with DLAA, main and this branch interleaved, two runs each (Measurements). Several free-play sessions by hand in Hyrule Field and Castle Town on builds carrying this commit, fire and water looked at on purpose: no wrong picture seen.
Checks:
tests/test_wb_spans.py(11 cases: the two slots on one page, the read's own bytes, the page's earlier write, a whole-page write also in the same part, writes over or within a recorded page, many partial writes, the switch off, and 2,000 random write-backs and reads against a per-byte model of which write each byte last had: no read waits for less than its bytes' newest write, and none for a later submission than the page rule) fails onmain(at its source check:mainkeeps no bytes; its rule is the switch's=0, under which theoffcase shows the read of one slot waiting for the other slot's part) and passes here; it also fails on three deliberately broken versions of the change (the earlier state left out, the last write left out, whole-page writes not clearing). Two checks that compile pieces ofvk_memory.cppfollow the change and pass:tests/test_readback_part.pygets a stub forenv_on_or(the switch at its default, so it runs the change as it ships), andtests/test_late_fix_build.pycutswb_pages_notetogether with the helpers it now calls;tests/test_invalidate_quick.py,tests/test_wb_pages.py,tests/test_switch_table.pyand the othertest_wb_*checks pass.tests/run_all.pyon the head (Windows, llvm-mingw): 817 passed, 14 failed, 127 skipped (Vulkan headers, spirv-val and Linux-only checks); the 14 fail onmain0e32bc10on this PC the same way: test_auto_handheld, test_crash_dialog, test_dev_ab, test_dev_build, test_dev_migrate, test_dev_packs, test_game_menu_mouse_buttons, test_input_movie, test_linux_preflight, test_pads_after_menu, test_perf_report, test_pipeline_state_computes, test_saveload_warm_cache, test_sdiv_overflow.Still to test:
The steps for each (Test Farm), 2 runs a side against
main, no switch set, Render resolution 100 % with DLAA or the game's own upscaler, uncapped, V-Sync off: from the savemarket-entrance, teleport toMarket_Child, wait 8 s, run around the Market for 30 s and measure; teleport toHyruleField_Child, wait 8 s, run around for 30 s and measure.frametime_p99_msandframetime_1pct_low_ms(the slowest 1 % of frames, in ms) at or belowmain's and the same picture; pages written in place take the same path (@Beanstalk)frametime_p99_msandframetime_1pct_low_msthanmain(here at 4K: 23.3 to 21.6 ms and 26.3 to 24.7 ms) (@Beanstalk)Measurements
Verdict: at 4K, where the GPU runs behind, the game's own p99 falls by 22 % (17.6 to 13.7 ms), frames are 16 % more even from one to the next, the p99 frame falls by 7 %, the 1 % low rises by 7 % and fps by 1.5 %. Systems whose GPU runs behind at lower resolutions (lower-end cards at 720p or 1080p) are expected to gain the same way; that is still to be tested (Testing).
Setup: Windows 11, RTX 4070 SUPER 12 GB, driver 621.24, Ryzen 9 9900X; the Castle Town route, DLAA, uncapped, V-Sync Auto (mailbox), the frame trace on, no other switch on either side; main and this PR interleaved (this, main, this, main), two runs each, the mean of the two (the median of two). Market: from 5 s after the second entry to the end of the stay. 1 % low: 1000 over the mean of the slowest 1 % of frame times. Frame-to-frame change: the mean of |a frame time - the frame time before it|.
0e32bc10The cost, a microbenchmark of a frame's worth of write-back notes and reads (244 notes, 25 reads): 22.4 to 22.8 us with buffers at fixed places, 22.0 to 23.8 us with new places every frame, so 0.4 to 1.8 us a frame (about 0.01 % of a 16.7 ms frame). It compiles
vk_memory.cpp's section as the renderer is built (-O2 -march=native) and timeswb_pages_noteandmemory_writeback_valuefor 7 s on each side.Risks
memory_end_submission's runs, the in-place writes' ranges); no page copy writes beyond its noted range. A write noted wider than it was makes the read wait as before.memory_rb_refresh, the mirror syncs, the late pages' fix) stay per page;RX_VK_READBACK_EARLYkeeps its own per-page writer lists, unchanged: it now starts only when the read's own bytes are pending, and then may still wait for a write next to them, as before; the sizes inRX_VK_READBACK_LATEare still read without a wait.wb_pages, made only for the 256 MB chunks where a partial write lands (512 KB a table a chunk), never freed, aswb_pages.RX_VK_WB_SPANS=0restores the page rule; a revert is enough. A player notices nothing after updating but fewer slow frames where the GPU was behind.Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named).main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
mainand passes here.gpu/vk/switches.def.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.Written with AI assistance (Claude).
Tested in game: Castle Town route at 3840x2160 and 1280x720 with DLAA, main and this branch interleaved, two runs each; several free-play sessions in Hyrule Field and Castle Town, fire and water looked at on purpose: no wrong picture seen
Possibly related, for context:
@review
Recommendation: Approve. The code is ready: only testing is left. The reported 4K results suggest fewer stalls, but the gain and default need confirmation against the PR's merge base and on the other supported hardware affected by the write-back path. Recommend approval; not approved on the forge.
The change tracks partial-page write-backs so a CPU read can avoid waiting for a neighboring range. It adds three persistent page tables and more work under the write-back lock in exchange for potentially better frame pacing.
Testing before merging (3) (runs on the systems it can change; it doesn't hold the recommendation)
gpu/vk/vk_memory.cpp:2113: Confirm the frame-pacing gain against this PR's merge base and report the individual runs or their spread. (more on the line) Do: Run interleaved comparisons againstf9fb7449on the same route and settings, reporting each run's frame times and 1% lows.gpu/vk/vk_memory.cpp:2114: Check the default-on path on the remaining affected Tier 1 systems. (more on the line) Do: Complete the described Test Farm runs on those systems, checking frame pacing and the picture against the base.gpu/vk/vk_memory.cpp:2074: Measure the actual RAM cost during play. (more on the line) Do: Compare process RAM on base and head over the stated route and a longer session, including Steam Deck or another memory-constrained system. (@fixdoes its part first: Use a compact or sparse representation for partial-page records to reduce the allocation per touched chunk.)@fixcan do L3.Checks: Still running: 9 checks.
More: does it help, other systems, switches, what was read
Does it help: Unproven against this PR's base: the reported two-run means on one Windows NVIDIA PC show p99 frame time falling from 23.3 to 21.6 ms and 1% low rising from 38.0 to 40.6 fps, but the reported main commit is
0e32bc10rather than the merge basef9fb7449, and individual run spreads are not given.Other systems: The changed wait selection also applies to Linux, AMD, Steam Deck and in-place writes. The author reports game sessions on Windows with an RTX 4070 SUPER; the PR lists the other hardware runs as outstanding. Page-based refresh and early-read paths remain in use.
Switches: RX_VK_WB_SPANS is a new kill switch, default on, with a switches.def row. The default helps in the reported Windows NVIDIA runs, but its effect on other supported systems remains to be measured.
Reviewed:
399abbc23aagainstf9fb744968: Read the repository rules, PR template, commit and diff; inspected the changed write-back code, its callers, page marks, and the new test. Skimmed the existing test fixtures and switch table rather than reading those files in full. The full vk_memory.cpp and switches.def were not read in full. docs/review-checklist.md and ../Tooling/AGENTS.md were absent.; nothing run (a code review).Checked and fine:
Testing: No code or tests were run during this read-only review. The author reports game testing and game-free checks; CI is still running.
Overlaps and merge order: Unknown; network access was unavailable.
Automated code review by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.
@ -2053,3 +2110,4 @@}void wb_pages_note(uint64_t guest_lo, uint64_t guest_hi, uint64_t value, uint64_t part) {if (!value || guest_hi <= guest_lo) return;std::lock_guard lk(wb_pages_mu);L1 (should fix): Confirm the frame-pacing gain against this PR's merge base and report the individual runs or their spread.
When: The PR compares against main
0e32bc10, while this checkout's merge base isf9fb7449; only the means of two runs are reported, so the size of run-to-run variation is unknown.Do: Run interleaved comparisons against
f9fb7449on the same route and settings, reporting each run's frame times and 1% lows.@ -2053,0 +2071,4 @@return on;}WbPages wb_ext; // (lo | hi << 32 of the page's last note when it covered the page in part; 0: the whole page)WbPages wb_rest_v, wb_rest_p; // (the page's value and part before that note: its other bytes' newest at most)L3 (should fix): Measure the actual RAM cost during play.
When: Each guest-memory chunk touched by a partial write can allocate three persistent 512 KB tables; the PR states the allocation size but does not report how many chunks a game session uses.
Do: Compare process RAM on base and head over the stated route and a longer session, including Steam Deck or another memory-constrained system.
@ -2053,3 +2111,4 @@void wb_pages_note(uint64_t guest_lo, uint64_t guest_hi, uint64_t value, uint64_t part) {if (!value || guest_hi <= guest_lo) return;std::lock_guard lk(wb_pages_mu);if (wb_spans_on()) { // (before the pages' values: what a page held is its rest)L2 (should fix): Check the default-on path on the remaining affected Tier 1 systems.
When: Linux NVIDIA, AMD on Windows and Linux, and Steam Deck use the changed wait selection; the supplied results cover only Windows on an RTX 4070 SUPER.
Do: Complete the described Test Farm runs on those systems, checking frame pacing and the picture against the base.
Jiro113 referenced this pull request2026-11-23 19:08:32 +00:00