WIP: perf(vk): a large capture is copied after the submits' order lock, so the game's other thread does not wait for it (RX_VK_CAPTURE_UNLOCKED_MB) #2073
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!2073
Loading…
Reference in a new issue
No description provided.
Delete branch "GummiNaomi/Longshot:perf/capture-off-lock"
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
When the game submits work for the GPU, the emulator first copies every page the CPU wrote since the last submit into a
capture, so that the replay later reads the data as it was at the submit and not as the game has changed it since. In play
a capture is under 1 MB; after an area loads it can be 100-250 MB, a copy of 10-60 ms. That copy ran under
submit_order_mu, the lock that keeps the captures in the same order as their jobs, and the game's other submitting threadneeds the same lock at its next submit and at its CPU-side event signal. So while the Presentation thread copied a large
capture, the main thread stood still at its signal for the whole copy: 20-92 ms, 7-12 times in a six-minute teleport tour.
This PR copies a capture of
RX_VK_CAPTURE_UNLOCKED_MB(16) MB or more after the lock is let go, on the same thread,before the thread goes back to the game:
capture_makenow only takes the pages (the scan, the late pages, the shadow notes: all under the lock as before); thenew
capture_copymakes the buffer, the staging and the copy.memory_capture_submit(CaptureLater*)queues a large capture aspending, in its place and with its job, under the lock;submit_qandevent_signal_queuedcalllater.finish()once the lock's scope ends. Smaller captures are copied beforethey are queued, as before, so steady play does not change.
memory_take_capture(the replay, in order) waits oncap_copiedwhile the capture it takes is pending. The copyingthread waits for nothing of the replay's (its buffer comes from VMA or the spares, its helpers and the flush are its
own), so the wait always ends;
CaptureLatermarks the capture done in a destructor, so a copy that throws cannot leavethe replay waiting.
RX_VK_ASYNC=0(the take on the same thread) andmemory_capture_nowcopy at once.Result: the main thread no longer waits for the other thread's large copies (0-1 waits of 20 ms or more a tour, against
7-10 on main and 12 with the switch at 0). See Measurements for what that is in frames: on this PC, a small gain that is
not proven.
Read first:
gpu/vk/vk_memory.cpp:memory_capture_submit,CaptureLater::finish,capture_copy,memory_take_capture;then
gpu/vk/vk_replay.cpp:submit_q,event_signal_queued. See #1599 (capture buffers reused by size class: thebuffer's own cost; this PR makes whatever the copy costs not block the other thread) and #1667 (a probe of a slow
capture's parts). Both touch
capture_make; whichever lands second rebases.Testing
Tested: Windows 11 Pro 26200, RTX 4070 SUPER (NVIDIA 617.42), Ryzen 9 9950X3D with SMT off, 32 GB, headless test
profile, uncapped, the switches of my own play (
RX_VK_TEX_AHEAD=1 RX_VK_ASTC_BC_ALL=1 RX_VK_IMAGE_BLOCK_MB=32 RX_VK_LOAD=create RX_VK_BC_LAYERS=8). Two walk tours a side (100 s in the Market, 100 s in Kokiri Forest), threesix-minute teleport tours a side (Market, Hyrule Field, Kakariko, Kokiri Forest, Lost Woods, Zora's River, 23 teleports),
one with
RX_VK_CAPTURE_UNLOCKED_MB=0, and one a side withRX_VK_MEM_CHECK=1. Every teleport arrived on both, with noerror lines and the PR's log line ("captures copied after the order lock (32): ...") in every PR run. The memory check
found no take out of order or unpaired (0 of 343,675 on the PR), no sync over the GPU's data, and no page read live after
a write since its capture (0 on both). A temporary probe (not in this PR) logged each wait of 1 ms or more for
submit_order_muand each capture's parts, on both builds.Checks:
tests/test_capture_unlocked.py(new): the realmemory_capture_submit,capture_unlocked_bytes,CaptureLater::finish,memory_take_captureandstruct Capturecut from the source, with a stand-in copy of 400 ms:another thread's submit takes ~0 ms unset against 353 ms at
=0; the take of the large capture waits for its copy; theorder is kept; a copy that throws does not hang the take; no
CaptureLatercopies at once; the callers' order (lock,submit, enqueue, the lock's end, finish) checked as text. It fails on main (
submit_q no longer has: CaptureLater later;) and passes here.tests/test_mem_check.pyandtests/test_vk_range_walk.pyfollow the moved code (the newsignature; the staging's
RangeWalkis now incapture_copy). The three pass on Windows (clang 22.1 and the emsdk's clang 24) and on Linux (WSL Ubuntu 24.04, gcc 13.3), on this head.The tours ran on the branch's first head, 632e13e6 on main
f1f2dc5b. It is rebased since onto main @9dd41326, with noconflicts and the same two patches (
git range-diffshows them identical); the 14 commits in between touch none of thelines it changes. This head (
23a685d8) builds on Windows (tools/build.ps1). Of the 218 checks that read the changedfiles, 172 pass on Windows and the other 46 fail the same way on main @
9dd41326(this PC's compilers and missing tools,most of them: test_audio_volume, test_capture_read_once, test_capture_readback_visibility, test_crash_dialog, test_crash_record, test_dev_ab, test_dev_packs, test_device_lost_exit, test_draw_stream_visibility, test_driver_env_choose, test_flush_nothing_held, test_frame_overlap, test_fsr_image_retirement, test_fsr_uniform_ownership, test_gpu_wait_stats, test_guest_imported, test_hud_layout_row, test_input_background, test_linux_pressure, test_menu_keys, test_mesh_conv, test_mesh_fallback_layout, test_nonuniform_image_arrays, test_oot_fov, test_pending_guest_import, test_perf_report, test_present_aspect_vulkan, test_present_queue_default, test_present_source_crop, test_profiling_mode, test_readback_wait_bounded, test_recorder_own_idle, test_renderer_new_handler, test_replay_write_order, test_slow_frames, test_sparse_mirror, test_target_reuse_memory, test_texel_buffer_ownership, test_text_size, test_thinlto_tls, test_tracy_funcs, test_ub_update_batch, test_udmabuf_limit_unreadable, test_upscaler_rows, test_vertex_bind, test_vk_occlusion_queries).
Still to test:
line (@Beanstalk linux, the teleport tour)
RX_VK_CAPTURE_UNLOCKED_MB=0against unset, expecting no more long frames after thearrivals unset (@Beanstalk steamdeck, the teleport tour with RX_VK_CAPTURE_UNLOCKED_MB=0 against unset)
Measurements
Verdict: the waits it targets are gone; in frames a small gain, inside main's own spread on this PC (no memory pressure).
Setup: as Tested; main @
f1f2dc5bagainst this PR @ d3467a54, both with the probe, interleaved (main, PR, main, PR, thePR at
=0, main, PR), each build's files kept and put back before each run. The teleport tour's figures cover its 23teleports; "after arrivals" is the 3 s after each; the later visits (7-23) are the cached ones. The branch was rebased
since onto main @
9dd41326with no conflicts (its two commits are the measured change and a fix to the new test only). The14 commits in between change none of the capture or order-lock code (
vk_replay.cpp: #1859's output preview, #2008'smesh-draw scan, #1977's sprint toggle), so I expect these numbers to hold; not measured again.
f1f2dc5bRX_VK_CAPTURE_UNLOCKED_MB=0: this PR @ d3467a54The frames after the later arrivals average 47 (2363 ms) a tour on main and 43 (2146 ms) on this PR: about 0.2 s less of
long frames a six-minute tour, half of the 0.44 s less order-lock waiting in those windows. But main's best run (2124 ms) beats this PR's worst (2238 ms), so
on this PC that is not proven. (Whole-run fps from the frame log reads lower on this PR, 147 against 165-179, only
because main's longer first-visit loads add ~10 s of loading-screen frames at ~950 a second. Over the frames that draw it
is the same, as above.)
The one wait of 44 ms left is behind four 8 MB captures in a row, which stay under the lock (3.5-19 ms each, 5-11 ms of it
making their buffers: #1599's part). The default of 16 MB: play's captures are under 1-2 MB and an area load's are
16-250 MB, so 16 leaves play as it was; a lower value would take bursts like that one too, not measured.
The first visit of an area loaded faster in every PR run (the six first arrivals: 17.2, 17.2, 16.5 s, and 19.3 s with the
memory check, against 28.6, 28.4, 26.9 s on main, 29.6 s with the check, and 26.7 s at
=0; the Market 4.1-4.4 against9.2-10.3 s). I do not claim it. The long part is a loading screen with only small captures in it, copied the same way by
both, and older builds show the long Market load in 5 of 23 tours at random. It lines up with the switch in all 13 runs
(both tours), and I could not find why.
Risks
write it makes to a page the copy has not reached yet is in the copy. The lock never kept the game's writes out of a
copy (a thread that is not at a submit writes on during it), it only held a thread that came to one; and such a write is
to data of a submission the GPU has not read, made without waiting for it. The submitting thread's own writes stay out
(it copies before it returns), and the captures' order and their pages' marks are taken under the lock as before.
RX_VK_MEM_CHECK=1every extra one is "capture retired" (2.23 M against 2.02 M): the page has not been written sinceits newest capture, but that capture's buffer has already been let go, so the live page holds the captured bytes.
"Written since their capture" and "never captured" are 0 on both sides, and "not copied" is the same (0.76 M). Why more
buffers are let go before the sync I did not trace.
RX_VK_MEM_CHECK=1only: a game thread'scapture_made()(under the order lock) can wait for the checker's lockwhile the replay's
take_pageswalks a large capture's pages, which the replay now takes sooner. One run had two suchwaits (29 and 107 ms). A diag switch's cost; the check's own results are unaffected.
same jobs to be queued.
RX_VK_CAPTURE_UNLOCKED_MB=0(every copy under the lock, as on main); a revert is enough. Nothingchanges for players after updating (no caches, saves or options touched).
Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named). (The checks that read the changed files, asTesting says: the 46 that fail fail on main too.)
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.tools/build.ps1; Linux: not built here, left to someone with a Linux toolchain)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
@Beanstalk steamdeck and linux: the teleport tour from kokiri-start (Castle Town Market, Hyrule Field, Kakariko, Kokiri Forest, Lost Woods, Zora's River, then the same again, about 20 s in each), main against the PR, with
RX_VK_CAPTURE_UNLOCKED_MBunset and=0(main ignores it) andRX_VK_FRAME_TRACE=1. Measure the frames of 33 ms or more in the 3 s after each arrival, each arrival's longest frame, and each load's time, with first visits and later ones kept apart.Expecting on the Steam Deck fewer and shorter long frames after the arrivals with it unset than at
=0and on main: a large capture's copy is slower there, so the waits this removes should be longer than this PR's Windows numbers show. On Linux, every teleport to arrive and the PR's log to have the line "captures copied after the order lock", with no more long frames than main. The first visits' loads are worth a look too: on Windows they were ~10 s shorter on the PR in every run, for a reason not found yet.Beanstalk test farm · ⛔ can't be tested as asked
Commit
23a685d· approved by GummiNaomi (untrusted) · dashboardNot run: the farm can't test this as asked, so nothing is built or run:
⚠️ Note: The capture summary is logged only after 32 qualifying copies, so its absence is unjudged.
⚠️ Note: Load metrics are aggregated across the tour; first and later loads are not separately judged.
What it could test instead: The alternative runs a two-lap teleport tour from Kokiri on Steam Deck and Linux, comparing the PR’s default behavior with main. It measures five-second frame windows at the first and second Market arrivals, records load times, and compares Market images. Crashes, failed arrivals, major frame-time or load-time regressions, and image differences fail the plan; the capture log is unjudged if the tour does not produce it.
A member of
BigBeanscan comment@Beanstalk <new words>to ask for something else, or@Beanstalk test it anywayto run the plan regardless.Steam Deck result: I ran the switch A/B you asked for on a Steam Deck OLED: the Market, arriving by teleport and then standing, three runs with this PR's default against three with
RX_VK_CAPTURE_UNLOCKED_MB=0. Neither the steady Market nor the arrival showed a difference the Deck can resolve: the standing fps was within the runs' spread, and the arrival's long frames (the worst about 0.4-0.5 s) were the same size on both sides. No errors in any log. The tool doesn't run your teleport tour; each run had one area load (the teleport from Kokiri to the Market), and none of the runs logged thecaptures copied after the order lock (32)line, so fewer than 32 captures of 16 MB or more were taken in a run.Standing in the Market, 60 s after the warm-up, 3 interleaved pairs
RX_VK_CAPTURE_UNLOCKED_MBunset, 16 MB)RX_VK_CAPTURE_UNLOCKED_MB=0(as main)dev.py ab's verdict: no difference shown: +1.8 % fps (median 26.2 against 25.7) for=0, within the runs' spread (A 25.6 to 26.8, B 26.0 to 26.4).The arrival: the first 15 s after the teleport to the Market (from the frame trace)
=01=02=03The arrival's long frames are uploads and the draws waiting on them on this Deck, on both sides, e.g.
vk: slow submit: 67.1 ms: draw 66.9 descriptors 58.8 texture lookups 54.7 uploads 53.5 ...andvk: a frame's uploads took 54 ms: ASTC on the GPU 25 (7.4 MB, 54 ms).The PR's mode line is in every run: default
vk: captures of 16 MB or more copied after the submits' order lock (RX_VK_CAPTURE_UNLOCKED_MB=0: under it, as before), and=0vk: every capture copied under the submits' order lock, as before (RX_VK_CAPTURE_UNLOCKED_MB=0). NoFATAL, device-lost,VK_ERRORor validation line in the 6 logs; every run exited normally.Hardware: Steam Deck OLED, SteamOS 3.9.5, RADV VANGOGH, Mesa 26.2.0-devel, GPU clock manual 1600 MHz, headless.
Settings: Minimum, Render 50 % (640x400), output 1280x800, uncapped, V-Sync and frame generation off.
Game data: complete (the dump, with the three damaged textures replaced).
Built: this PR @
23a685d8(its 2 commits, which sit on main9dd41326), x86-64-v3, shaders prepared on the Deck; both sides are this build, with and without the switch.Measured:
dev.py ab, Market, 3 interleaved pairs (the project's tool, with a local change to compare two prebuilt runtimes, since the Deck does not build); the arrival from each run's frame trace.Limits: one area load a run, three runs a side, one Deck, headless. On this Deck the arrival's freezes come from texture uploads (0.4-0.5 s), which may hide a 20-90 ms capture wait; the teleport tour with several large area loads, where the PR's own counter line would appear, was not run.
Closing in favour of #2244, which takes the capture's copy off the game's thread entirely (this PR only moved large copies outside the submits' order lock). The Steam Deck A/B here showed no difference it could resolve.
Pull request closed