WIP: perf(vk): the capture's copies use hand-written x86-64 kernels (lsasm_copy, lsasm_copy_nt; RX_VK_CAPTURE_ASM) #2128
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
4 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!2128
Loading…
Reference in a new issue
No description provided.
Delete branch "siantm/Longshot:perf/asm-capture-copy"
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
The renderer captures the game's memory into its own buffers before each submit (
gpu/vk/vk_memory.cpp): the guardedcopy of a guest range (
guarded_copy), andcopy_part's copies between its own buffers (the texture shadow's pairs,the staged pages). They went through the C runtime's
memcpyand, withRX_VK_CAPTURE_NT=1, the Ccopy_nt.This adds two hand-written x86-64 kernels on the asm kit and makes the capture use them:
gpu/asm/copy.Slsasm_copy: amemcpyfor the capture's copies, an AVX2 and an SSE2 body: up to 256 bytesoverlapping moves, then a vector loop,
rep movsbfrom 16 KB (AVX2) or 4 KB (SSE2), streaming stores over 1.5 MB(as the UCRT's
memcpydoes there);gpu/vk/asm/copy_nt.Slsasm_copy_nt: the Ccopy_nt's strategy (the head plain, the destination's whole 64-bytelines streamed with a prefetch a page ahead, an
sfence, the tail plain).The guarded copy takes
lsasm_copy(lsasm_copy_ntwithRX_VK_CAPTURE_NT=1);copy_part's own copies go throughcapture_copy.guarded_copy's__trystill catches a fault on the source inside a kernel, through the kernel'sSEH unwind data.
copy_fencenow fences after every page-by-page retry, sincelsasm_copy(andmemcpy) stream over1.5 MB. The bytes copied are the same; the result expected was the capture's copies at least as fast as before; the micro-benchmark (Measurements) finds them slower in the AVX2 build at 4-12 KB into cold destinations.
The switch:
RX_VK_CAPTURE_ASM(new, a kill switch, bool, its row ingpu/vk/switches.def), on by default as thebranch shipped it, read once;
=0gives back main's code:memcpyand the Ccopy_nt, both kept, so the two can becompared in one build.
From 971f8074 on
exp/x86-asm-sync. Depends on #1947 (the asm kit, which now carries the GPU-visible ranges' sort,lsasm_sort_ranges): its commits show in this diff until #1947 merges.Not the same change as #797 (dyst's, open): it keeps a thread's last GPU-write notes so a repeat note is skipped; this
one changes how the capture copies bytes, and the two touch none of the same functions.
Read first:
gpu/asm/copy.S,gpu/vk/asm/copy_nt.S, thencapture_copy,guarded_copyandcopy_fenceingpu/vk/vk_memory.cpp.Testing
Tested: Windows 11, i7-9700, RTX 4060, the builder's llvm-mingw clang 23 (
CC/CXX,RX_CPMset); headbb9161d5on #1947's head
cffa2f46(the asm kit, on main9dd41326). Through the PC's job queue, each run in a hidden console,tests/run_all.py --strict --exact --jobs 2 --timeout 1200: the change's own checks (light jobpr-tb-AsmCut5-light,log
t-AsmCut5-asm-capture-copy):test_asm_copy.py,test_capture_copy_helpers.py,test_capture_copy_nt.pypass (3of 3).
--changed cffa2f46(build jobpr-tb-AsmCut5-build, logt-AsmCut5-chg-asm-capture-copy): 97 checks, 84 pass,13 fail, each of which fails on #1947's head @
cffa2f46too (a worktree atcffa2f46in the queue's jobs, logst-AsmCut5-base-kit-changedandt-AsmCut5-base-kit-more):test_capture_read_once.py,test_crash_dialog.py,test_dev_ab.py,test_frame_overlap.py,test_guest_imported.py,test_linux_pressure.py,test_pending_guest_import.py,test_present_queue_default.py,test_replay_write_order.py,test_sparse_mirror.py,test_target_reuse_memory.py,test_texel_buffer_ownership.py,test_vk_occlusion_queries.py.It has run in the game on this PC: the in-play A/B in Measurements (held job
fb-g2128, the Market, 19 runsand a stand-in); one run with the switch off ended with a device loss (In play), no FATAL or Vulkan error in any other. It has also walked issue #2285's crossing in Hyrule Field (held job
reach2285b, three runs: On the crossing of issue #2285, in Measurements); the one withRX_VK_CAPTURE_NT=1crashed in the slow-frame recorder's stack walk (HOS_SLOW_FRAMES, a diagnostic), not in a copy: the crash of issue #1584.Still to test:
uncapped (
HOS_FPS_MAX=240), on #1947's head, on this PR withRX_VK_CAPTURE_ASM=0and on this PR with it unset,each warmed a minute, then at least three interleaved three-minute runs of each (ABC ABC ABC), and one run of each
with
HOS_PROFILE=1(python -I scripts/perf.py reporton its folder: the share ofcopy_partandguarded_copyon their threads), expecting the same picture all three ways, no FATAL, that share lower and fps and frame times of
this PR unset at least those of the other two; a difference beyond the runs' spread is a gain or a loss, one within
it is no difference measured (by hand)
PR with
RX_VK_CAPTURE_ASM=0and on this PR with it unset, warmed and interleaved, expecting the same picture andframe times within the runs' spread or better unset (@Beanstalk linux+nvidia, linux+amd, windows+amd, windows+intel,
steamdeck: Kokiri Forest three minutes, #1947's head, RX_VK_CAPTURE_ASM=0 and unset, warmed and interleaved, frame
times and a screenshot)
python3 -I -B tests/test_asm_copy.py --cxx g++and--cxx clang++, and the same fortests/test_capture_copy_nt.py, expecting PASS (by hand)expecting fewer frames of 100 ms or more at the crossing with this branch, where glibc's
memcpydoes not streamover 1.5 MB and this PR's
lsasm_copydoes (by hand: asked of GrimVisage on this PR; this PC cannot run Linux)Measurements
Verdict: on Windows, level in play: no gain and no loss shown. In the Market at an 800x600 desktop (the output pinned at
2560x1440), one runtime, with
RX_VK_CAPTURE_REP=0on both sides so that the copies are this PR'slsasm_copy, thecapture's copying took 0.095 against 0.094 ms a submit with the switch unset and
=0in steady play (means -0.2 %,p = 0.96), and 0.077 against 0.079 in the load before it (-5.9 %, p = 0.12); nothing is shown in frame time (In play,
below).
The micro-benchmark: no gain in the AVX2 build, which the release ships: there
lsasm_copyis atmemcpy's speed(0.97-1.01×) at most sizes, 1.14× on 64-byte copies, and slower on copies of 4 KB (0.95×) and 12 KB (0.74×) into cold
destinations. In the SSE2 build it is faster on 4 KB copies (1.10× warm, 1.24× cold) and on unaligned 12 KB ones
(1.32×), and slower on 256-byte (0.89×) and 2 MB (0.94×) ones.
lsasm_copy_nt(RX_VK_CAPTURE_NT=1) is at the Ccopy_nt's speed (0.98-1.03×) at every size in both builds. A micro-benchmark on authored input. The Market's copiesare its AVX2 4 KB and 12 KB cold rows' kind (runs of pages into the capture's buffers, which it cycles through; the
sizes are not logged): the 0.74× did not show in play, where a third more copy time would be about 0.03 ms a submit,
six times the run-to-run margin below. What this PR is then worth: the copies' parity in the AVX2 build, in play as in
the micro-benchmark; #2129's
rep movsbfrom 4 KB, which builds on it, is what changes that.On Linux it is open. GrimVisage measured stalls of 100 to 300 ms crossing Hyrule Field (issue #2285) on Linux, where
main's copy ran at about 1.7 GB/s on captures of 50 to 245 MB. On this PC that stall does not happen (On the crossing of
issue #2285, below): every capture copy ran at 8 to 10 GB/s, main's C copy included, because Windows'
memcpyalreadystreams at these sizes. glibc's does not, and this PR's
lsasm_copystreams over 1.5 MB; what that gives on Linux isnot measured by us.
The switch:
RX_VK_CAPTURE_ASM, on by default as this PR proposes; level with=0in play (below).=0gives backmain's
memcpyand Ccopy_ntfor the A/B.Baseline: the C this PR replaces, as #1947's head
cffa2f46has it (the asm kit on main9dd41326, which this PR depends on), not main itself.The micro-benchmark: the capture's copies through a function pointer, as
guarded_memcpyandcapture_copycall them:memcpy(the C runtime's) againstlsasm_copy, andcopy_ntcut fromgpu/vk/vk_memory.cppatcffa2f46againstlsasm_copy_nt; a repetition makes as many copies of one size as come to about 512 KB from an 8 MB source of authored bytes, into the same slots each time (warm) or into slots further on in a 256 MB area each time (cold, as the capture's buffers are). A script on this PC, not in this PR (asm_copy_bench.py, PrTrials'trials/tools/bench), run alone in the PC's job queue on 2026-11-22: 3 rounds of 201 repetitions per case and arm, i7-9700, llvm-mingw clang 23.1.5-O2 -ffp-contract=off, arms SSE2 (-march=x86-64) and AVX2 (-march=x86-64-v3, what the release builds the back end for). The arms take turns at going first; their outputs were compared at every repetition and were equal. Each cell: the median of the three rounds' medians, [lowest–highest round median], in ns per copy. Change: before ÷ after (above 1: this PR faster).cffa2f46bb9161d5cffa2f46bb9161d5In play (held job
fb-g2128, 10:33 to 11:21 UTC on 2026-11-23, one runtime of #2129's67603ce7on this headbb9161d5and #1947'scffa2f46, with #2130's4545cecamerged in, which these runs leave at its default on both sides): theMarket, chosen because a capture copies the most pages there of the scenes on disk (about 210 a submit, 6 submits a
frame). Each run was 60 s, a warm-up a side, then
RX_VK_CAPTURE_ASM=0(memcpy) and unset (lsasm_copy) interleaved(off on on off, four times), every run with
RX_VK_CAPTURE_REP=0. Every run loaded its programs at create(
RX_VK_LOAD=create), the output pinned at 2560x1440 on a desktop at 800x600 at 30 Hz (the PC's only screen then); noload marker or strike file appeared, and no run's log has a commit-limit line. The build is the AVX2 one (the same
runtime's write-marks walk logs its AVX2 32-byte loads), so
lsasm_copyran its 32-byte loop below 16 KB andrep movsbfrom 16 KB. Reach: the capture's own 600-submit line (vk: capture: a submit ... N copied) shows about 213 pagescopied a submit in the unset side's warm-up, each through
lsasm_copy. "Copying" below is that line's capture time lessits lock and its scan: the copies and the capture's own bookkeeping around them. The figures are the runs' 600-submit
lines in the measured stretch (the last 40 lines a run, about 58 s of play).
One
=0run, ca-off4, ended in the load into the Market (frame 8,601) withVK_ERROR_DEVICE_LOSTduring a write-backdiff pass: the driver reset the GPU. It ran with the switch off, the C copies this PR replaces; its memory was as in the
other runs (16.5 GB private, 44.1 of 45.1 GB committed, no commit-limit line), the GPU at 83 °C at most. It is the only
device loss in the 545 game runs logged on this PC, and it is not counted. Its stand-in, ca-off4-again (same runtime,
settings, load mode and desktop), ran at the end, after the kit's reference run, so it is out of the interleave; it is
clean and counts in its place.
RX_VK_CAPTURE_ASM=0(memcpy), 8 runslsasm_copy), 8 runsa frame's replay time (ms): submitBefore the measured stretch (the title, the load into the Market and its first seconds: the 600-submit lines before
those 40 with 5 or more pages copied, about 140-146 pages a submit), the copying took 0.077 against 0.079 ms a submit
(-5.9 %, p = 0.12), 0.055 against 0.055 ms per 100 pages (-2.9 %, p = 0.076): not shown either way.
What a frame is: at 6 submits a frame the copying is about 0.56 ms a frame of the submitting threads' time (the capture
runs at submit, on the game's threads). Twice the standard error of the difference of the two sides' means is 0.0055 ms
a submit (5.7 %) for the copying, and 0.30 ms (2.3 %) of calm game time, so no difference the copy could make shows in
frame time at 8 runs a side, and none is claimed. One run of #1947's kit
cffa2f46(memcpy, no switch), calm game_ms12.55, is a reference only: another build.
On the crossing of issue #2285 (held job
reach2285b, chain 21, ended 07:11 UTC on 2026-11-24): GrimVisage's reporton this PR, Hyrule Field at z about 238. One local runtime of this branch (
bb9161d5with #2129's67603ce7and #2130's4545ceca, as In play), plus a diagnostic line that is local and not pushed: each capture copy of 3 ms or more, with itsMB, its bytes by run length, its time and rate. A script walked child Link from Castle Town's gate into the field and back
and forth over that line, the output at 2560x1440, uncapped; one run per setting, so the settings are not compared with
each other, and no run logged a commit-limit line.
RX_VK_CAPTURE_ASM=0 RX_VK_CAPTURE_REP=0:memcpy)lsasm_copy, #2129'srep movsb)copy_nt(RX_VK_CAPTURE_NT=1,RX_VK_CAPTURE_ASM=0)With main's C copy, 10 of the 12 crossings were walked, and no frame within about 1.5 s of any of them took 100 ms or
more (the worst 68 ms). At the other two an in-game event moved Link from z about 162 to z about 270 with a short load,
whose longest frames (196 and 156 ms) were memory sync, with no capture copy of 3 ms or more at that moment. GrimVisage's
figures, on Linux: main's copy at about 1.7 GB/s on captures of 50 MB or more (a 245 MB one about 220 ms), with 84 / 13 /
2 % of the bytes by run length.
RX_VK_CAPTURE_NTis main's switch (the Ccopy_nt); with this PR's switch on, itselects this PR's
lsasm_copy_ntinstead.The
RX_VK_CAPTURE_NT=1run crashed after about two minutes, in its second leg (0xc0000005, a read inside ntdll): inthe slow-frame recorder (
HOS_SLOW_FRAMES, a diagnostic the script set), insample_all_threadwalking anotherthread's stack through
unwind_stepandRtlVirtualUnwind, not in a copy; the last capture before it was 0.9 MB. Itis the crash of issue #1584, which #1951 addresses; this runtime does not have #1951.
Risks
What could break: a copy that gets bytes wrong would show as wrong textures or geometry in any scene; a fault inside a
kernel that Windows could not unwind would end the game instead of the copy (the test faults each path of both kernels
and checks the unwind). Turn it off with
RX_VK_CAPTURE_ASM=0. Renderer files: the runtime is rebuilt; nothing changesfor players' saves, options or caches.
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
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.Possibly related, for context:
@Beanstalk This makes the renderer's capture copy each submit's ranges with a hand-written x86-64 kernel (gpu/vk), in every scene; RX_VK_CAPTURE_ASM=0 turns it off (the C copy). It sits on #1947 (
cffa2f46, the asm kit), so its diff against its merge base includes that PR's commits; the farm cannot take it as the comparison, so please compare with main. Please start from the kokiri-start save and take the picture right after it loads and settles, before any input, with a main-against-itself picture on the same PC at that point; then, for frame times only, play Kokiri Forest and Hyrule Field, two minutes each; at least two starts a side, interleaved, the PR against main on the same PC, on the PR's side unset (the default, on) and with RX_VK_CAPTURE_ASM=0 (off), on Windows with an RTX 4070, Windows with an Arc B580, Linux with an RX 6600 (RADV) and the Steam Deck. The same picture as main within main's own difference from itself; with it unset, frame time p99 equal to main's or better, within the run-to-run spread, and equal to RX_VK_CAPTURE_ASM=0 or better. The log must have no FATAL, device lost or validation error line.@siantm only members of
BigBeanscan start the farm. One of them can approve this request by reacting 👍 to it or to this comment.Measurements: the micro-benchmark (trials script, run alone on this PC 2026-11-22, 3 rounds, SSE2 and AVX2): figures and Verdict; the Summary's expectation sentence now cites them
@Beanstalk One correction to my request 69732 above, for its picture point: please take each picture 5 s (300 frames at 60 fps) after the first frame whose picture is not a single uniform colour, with no input in between, and take the main-against-itself picture from the same point. A picture taken during the load's fade-in from black is not comparable: run 70269 on #2156 caught the fade in 3 of 4 starts.
@siantm only members of
BigBeanscan start the farm. One of them can approve this request by reacting 👍 to it or to this comment.@Beanstalk This replaces my request 69732 on this head and the corrections under it, in a form the farm can run: it cannot time a picture from the first frame that is not a uniform colour (its answer on #2131), so each picture here follows a fixed wait after the map event. This makes the renderer's capture copy each submit's ranges with a hand-written x86-64 kernel (gpu/vk), in every scene; RX_VK_CAPTURE_ASM=0 turns it off (the C copy). It sits on #1947 (
cffa2f46, the asm kit), so its diff against its merge base includes that PR's commits; the farm cannot take it as the comparison, so please compare with main. Please start from the kokiri-start save and take the picture 10 s (600 frames at 60 fps) after the map event that ends the load, a fixed wait with no input in between (the farm cannot time it from the first frame that is not a uniform colour), with a main-against-itself picture on the same PC from the same point; then, for frame times only, play Kokiri Forest and Hyrule Field, two minutes each; at least two starts a side, interleaved, the PR against main on the same PC, on the PR's side unset (the default, on) and with RX_VK_CAPTURE_ASM=0 (off), on Windows with an RTX 4070, Windows with an Arc B580, Linux with an RX 6600 (RADV) and the Steam Deck. The same picture as main within main's own difference from itself; with it unset, frame time p99 equal to main's or better, within the run-to-run spread, and equal to RX_VK_CAPTURE_ASM=0 or better. The log must have no FATAL, device lost or validation error line.@siantm only members of
BigBeanscan start the farm. One of them can approve this request by reacting 👍 to it or to this comment.Tested on one PC only (RX 9070 XT, RADV Mesa 26.3.1, Ryzen 5 5600X, Fedora 45): other GPUs, drivers, CPUs and Windows are untested, so the numbers below may differ there.
A scene where the capture's copy is most of a stall, in case it's useful for measuring this and #2129 in the game: issue #2285, Hyrule Field.
Child Link walking straight out of the Castle Town gate (
HyruleField_Child; from thekokiri-startsave, teleport to the Market, then out of the gate) and back across one line (z ~238 instatus' position, x ~0; the gate is at z ~32) stalls the game for 100 to 300 ms at every crossing, both directions. Onmain@d02cac0f, RX 9070 XT, RADV Mesa 26.3.1, Ryzen 5 5600X (an AVX2 build), Linux, headless, 60 fps cap: 9 frames over 100 ms in 12 crossings, worst 286 ms; withRX_VK_CAPTURE=0, none in 8 crossings (worst 42 ms). In a sampled stall, 43 % of the game's main thread is innvnQueueSubmitCommands→capture_make→copy_split/copy_part. Likely (not checked) the neighbouring map's preload writing a burst of pages.What I haven't measured: how many pages and MB those captures copy, so whether the copies there are large (your streaming-store range) or many 4-16 KB ones (#2129's). With #2244 the game's thread leaves the copy but then waits for its GPU work behind the capture's take (numbers in #2244), so a faster copy should help either way. Untested with this branch.
@GrimVisage thank you, this is the scene we were missing. In our steady-play runs (the Market on an i7-9700 and RTX 4060, Windows) the capture's copy was small: #2129's
lsasm_copy_coldmade it 1.39 times faster, about 0.16 ms a frame, with nothing shown in frame time; this PR'slsasm_copywas level withmemcpy.We'll try to reproduce your crossing on this PC (from
kokiri-start, the Market, out of the gate, back and forth across the line at z ~238) and, if it stalls here, measure both switches there with the crossings as the unit. We'll also count the pages and MB the stall's captures copy, the figure you say is missing, so it is clear whether those copies are large ones or many 4-16 KB ones. Our runtime is on main9dd41326with the asm kit, Windows and NVIDIA, so if it does not reproduce here we'll say so.@siantm the figures for the crossing, from this PC (same setup as above:
main@d02cac0f, RX 9070 XT, RADV Mesa 26.3.1, Ryzen 5 5600X, Linux, headless, 60 fps cap), so you have them if it doesn't reproduce on yours.Streaming stores remove the stalls here. The same laps across z ~238, 180 s a run, interleaved on one build:
RX_VK_CAPTURE_NT=1RX_VK_CAPTURE_NT=1RX_VK_CAPTURE_NT=1What the captures copy (a local log line in
capture_make, not for merging): crossing the line makes captures of 50-245 MB, and the capture buffers are host-cached memory. Main's copy into them runs at about 1.7 GB/s for the captures of 50 MB or more (two runs) (a 245 MB one: ~220 ms on the game's thread),RX_VK_CAPTURE_NT=1at 11-13 GB/s. Of those captures' bytes, 84 % are in runs of consecutive pages over 1.5 MB (the longest 27-50 MB), 13 % in runs of 64 KB to 1.5 MB, 2 % under 64 KB. Solsasm_copy's streaming path over 1.5 MB should take most of it, where glibc'smemcpydoesn't stream at these sizes; untested with this branch.Two smaller things from the same runs: touching a capture spare's pages ahead, off the game's thread, made main's copy 1.3-1.8x faster (about 0.6 ms a MB of page faults) but added little once streaming stores were on; and captures over 256 MB (up to ~455 MB here) aren't copied at all, as
capture_makeintends.Thanks for the figures. We ran your crossing here: Windows 11, i7-9700 (8 cores), RTX 4060 (driver 610.88), 2560x1440 output, uncapped. The build was a local one of this branch with a log line for each capture copy of 3 ms or more. A script walked Link from Castle Town's gate, one run per setting, so the settings are not compared with each other.
RX_VK_CAPTURE_ASM=0 RX_VK_CAPTURE_REP=0, the C runtime's memcpy), 12 crossings of z ≈ 238:rep movsb, stacked on this one) and withRX_VK_CAPTURE_NT=1(main's Ccopy_nt, asRX_VK_CAPTURE_ASM=0was set in that run), the script made no crossing: one run left the field, the other ended early. Those runs only show the copies made on the way: the largest was 235 MB in 32 ms (median 8.0 GB/s) and 245 MB in 26 ms (median 8.4 GB/s).So on this PC every copy measured runs at 8 to 10 GB/s, the C copy included: Windows' memcpy already streams at these sizes. With the C copy, the 100 to 300 ms stall you measured does not happen at the crossing here. Your 1.7 GB/s is glibc's memcpy, so whatever this pull request's streaming path adds would be on Linux, where we have not measured it; your
RX_VK_CAPTURE_NT=1figures are the nearest measurement there.Two more things.
RX_VK_CAPTURE_NTis main's switch (the Ccopy_nt); this pull request's own switch isRX_VK_CAPTURE_ASM, and with it on (the default)RX_VK_CAPTURE_NT=1copies with this pull request'slsasm_copy_ntin place of main'scopy_nt.RX_VK_CAPTURE_REPis #2129's. Also, theRX_VK_CAPTURE_NT=1run crashed after about two minutes. The crash was in the slow-frame recorder (HOS_SLOW_FRAMES, a diagnostic) while it walked another thread's stack, not in the copy (the crash of issue #1584); the last capture before it was 0.9 MB.If you have the time, the measurement that would settle this pull request's case is your crossing on Linux with this branch at its defaults against main; we cannot measure Linux here.
View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.