WIP: perf(vk): the capture's copies use rep movsb from 4 KB (lsasm_copy_cold; RX_VK_CAPTURE_REP) #2129
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
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!2129
Loading…
Reference in a new issue
No description provided.
Delete branch "siantm/Longshot:perf/asm-capture-rep"
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 capture's buffers cycle through some thirty and are cold when a copy writes them. Below 16 KB,
lsasm_copy'svector loop reads each destination line for ownership before writing it; the string stores of a
rep movsbthat longwrite whole lines without that read.
This adds
lsasm_copy_coldtogpu/asm/copy.S(lsasm_copy's body becomes theCOPY_BODYmacro;lsasm_copyisunchanged): the same kernel with
rep movsbfrom 4 KB, SSE2 and AVX2 bodies, the same SEH and CFI.capture_copy(
copy_part's own copies, and the guarded copy withoutRX_VK_CAPTURE_NT) takes it;guarded_copy's fault handlingis unchanged;
RX_VK_CAPTURE_NT=1still comes first andRX_VK_CAPTURE_ASM=0still givesmemcpy. The bytes copiedare the same; the result expected is less time in the capture's copies of 4 to 16 KB into cold buffers; the micro-benchmark (Measurements) finds it in the AVX2 build at 4-12 KB, and the same as before at 16 KB and in the SSE2 build.
Where it makes a difference: only in a build for a CPU with AVX2 (the kernels pick their body when built), on copies of 4 KB to under 16 KB. In a build without AVX2,
lsasm_copy's SSE2 body already usesrep movsbfrom 4 KB, soRX_VK_CAPTURE_REPon and off run the same code there. This PC's builds are AVX2 builds.The switch:
RX_VK_CAPTURE_REP(new, a kill switch, bool, its row ingpu/vk/switches.def), on by default as thebranch shipped it, read once;
=0giveslsasm_copy, as with the pull request it depends on.From 323a377c on
exp/x86-asm-sync. Depends on #1947 (the asm kit, which now carries the GPU-visible ranges' sort,lsasm_sort_ranges) and on #2128 (the capture's copies inlsasm_copy): their commits show in thisdiff until they merge.
Read first:
COPY_BODYandlsasm_copy_coldingpu/asm/copy.S, thencapture_copyingpu/vk/vk_memory.cpp.Testing
Tested: Windows 11, i7-9700, RTX 4060, the builder's llvm-mingw clang 23 (
CC/CXX,RX_CPMset); head67603ce7on #2128's head
bb9161d5(on #1947's headcffa2f46). Through the PC's job queue, each run in a hiddenconsole,
tests/run_all.py --strict --exact --jobs 2 --timeout 1200: the change's own checks (light jobpr-tb-AsmCut5-light, logt-AsmCut5-asm-capture-rep):test_asm_copy.py,test_capture_copy_nt.pypass (2 of 2).--changed bb9161d5(build jobpr-tb-AsmCut5-build, logt-AsmCut5-chg-asm-capture-rep): 97 checks, 84 pass, 13fail, 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 once: the in-play A/B in Measurements (held job
fb-g2129, the Market, 19 runs), withno FATAL, crash or replay error in any run.
Still to test:
uncapped (
HOS_FPS_MAX=240), on the pull request this one depends on, on this PR withRX_VK_CAPTURE_REP=0and onthis 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 sharelower 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)
one depends on, on this PR with
RX_VK_CAPTURE_REP=0and on this PR with it unset, warmed and interleaved, expectingthe same picture and frame times within the runs' spread or better unset (@Beanstalk linux+nvidia, linux+amd,
windows+amd, windows+intel, steamdeck: Kokiri Forest three minutes, the base pull request, RX_VK_CAPTURE_REP=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)Measurements
Verdict: a gain in the capture's copying, shown in play: in the Market the capture's copies took 0.067 ms a submit
with the switch on against 0.093 ms off (1.39×, p = 0.0002 in a test that swaps runs between the sides), the
micro-benchmark's 1.27-1.36× seen in the game; frame time is not shown to differ (calm game time -1.7 %, p = 0.23,
below what 8 runs a side resolve). It rests on the switch on against off in one runtime. In the micro-benchmark, in the
AVX2 build, the one the release ships:
lsasm_copy_coldis 1.27-1.36×lsasm_copy'sspeed on copies of 4, 8 and 12 KB into cold destinations, which brings the 12 KB copy back to
memcpy's time and putsthe 4 and 8 KB ones ahead of it; 1.08-1.14× with warm destinations; the same (1.00×) at 2, 16 and 64 KB (16 KB and up
were
rep movsbalready). In the SSE2 build no difference (0.99-1.01×): that body already usedrep movsbfrom 4 KB, so a player whose build has no AVX2 runs the same code either way (not measured in play: thisPC's builds are AVX2 builds).
The switch:
RX_VK_CAPTURE_REP, on by default as this PR proposes; the micro-benchmark finds it faster or the same, neverslower, and in play its own stage is faster.
=0gives backlsasm_copyfor the A/B.Baseline:
lsasm_copyat #2128's headbb9161d5, what this PR changes (it depends on #2128 and #1947), withmemcpy(main's copy, as #1947's head
cffa2f46has it) beside it.The micro-benchmark: the capture's copies through a function pointer as
capture_copycalls them:lsasm_copyagainstlsasm_copy_cold(both from this PR'sgpu/asm/copy.S) andmemcpy; a repetition makes as many copies of one size as come to about 512 KB from an 8 MB source of authored bytes, into slots further on in a 256 MB area each time (cold, as the capture's buffers are) or the same slots each time (warm). 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).bb9161d567603ce7cffa2f46In play (held job
fb-g2129, 23:25 UTC on 2026-11-22 to 00:12 UTC on 2026-11-23, one runtime of this head67603ce7on #2128
bb9161d5and #1947cffa2f46, with #2130's4545cecamerged in, which these runs leave at its default on bothsides): the Market, chosen because a capture copies the most pages there of the scenes on disk (about 210 a submit, 6
submits a frame, against 155-170 in Kokiri Forest). Each run was 60 s, a warm-up a side, then
RX_VK_CAPTURE_REP=0andunset interleaved (off on on off, four times). Every run loaded its programs at create (
RX_VK_LOAD=create) at thegame's 2560x1440 output (desktop 2560x1440 at 60 Hz). No run was left out, and no load marker or strike file appeared.
Reach: the capture's own 600-submit line (
vk: capture: a submit ... N copied) shows about 210 pages copied a submit inthe on side's warm-up, each through
capture_copy, which islsasm_copy_coldwith the switch unset. "Copying" below isthat line's capture time less its 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).
RX_VK_CAPTURE_REP=0(lsasm_copy), 8 runslsasm_copy_cold), 8 runsa frame's replay time (ms): submitBefore the measured stretch (the title, the load into the Market and its first seconds: 74 to 103 of the 600-submit
lines a run with 5 or more pages copied, about 146 pages a submit), the copying took 0.062 against 0.079 ms a submit
(1.26×, p = 0.0002), 0.043 against 0.054 ms per 100 pages. So the kernel is faster both in the load and in steady play.
What a player sees: at 6 submits a frame, the copy's saving is about 0.16 ms a frame of the submitting threads' time (the
capture runs at submit, on the game's threads), about 1.3 % of calm game time. Twice the standard error of the
difference of the two sides' means is 0.33 ms (2.6 %) of calm game time, so a saving that size cannot show in frame
time at 8 runs a side, and none is claimed: calm game time, the frame rate, p99 and frames of 50 ms or more are within
the spread. One run of #1947's kit
cffa2f46(memcpy, neither switch), calm game_ms 13.07, is a reference only: anotherbuild.
Risks
What could break: a copy that gets bytes wrong would show as wrong textures or geometry in any scene; the test checks
lsasm_copy_coldagainstmemcpyat every alignment and around each size where its strategy changes, and its faults'unwind on Windows. Turn it off with
RX_VK_CAPTURE_REP=0. Renderer files: the runtime is rebuilt; nothing changes forplayers' 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.@Beanstalk This copies captures into cold buffers of 4 KB and more with a hand-written x86-64 string copy (gpu/vk), in every scene; RX_VK_CAPTURE_REP=0 turns it off. It sits on #2128 (
bb9161d5) and #1947 (cffa2f46, the asm kit), so its diff against its merge base includes those PRs' commits; the farm cannot take them 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_REP=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_REP=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 69736 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.Description: Summary now says where this makes a difference at all: only in a build for a CPU with AVX2, on copies of 4 KB to under 16 KB. In a build without AVX2, lsasm_copy's SSE2 body already uses rep movsb from 4 KB, so RX_VK_CAPTURE_REP on and off run the same code there. No code change.
@Beanstalk This replaces my request 69736 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 copies captures into cold buffers of 4 KB and more with a hand-written x86-64 string copy (gpu/vk), in every scene; RX_VK_CAPTURE_REP=0 turns it off. It sits on #2128 (
bb9161d5) and #1947 (cffa2f46, the asm kit), so its diff against its merge base includes those PRs' commits; the farm cannot take them 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_REP=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_REP=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.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.