WIP: perf(vk): the capture's copies use hand-written x86-64 kernels (lsasm_copy, lsasm_copy_nt; RX_VK_CAPTURE_ASM) #2128

Draft
siantm wants to merge 24 commits from siantm/Longshot:perf/asm-capture-copy into main
Contributor

Summary

The renderer captures the game's memory into its own buffers before each submit (gpu/vk/vk_memory.cpp): the guarded
copy of a guest range (guarded_copy), and copy_part's copies between its own buffers (the texture shadow's pairs,
the staged pages). They went through the C runtime's memcpy and, with RX_VK_CAPTURE_NT=1, the C copy_nt.

This adds two hand-written x86-64 kernels on the asm kit and makes the capture use them:

  • gpu/asm/copy.S lsasm_copy: a memcpy for the capture's copies, an AVX2 and an SSE2 body: up to 256 bytes
    overlapping moves, then a vector loop, rep movsb from 16 KB (AVX2) or 4 KB (SSE2), streaming stores over 1.5 MB
    (as the UCRT's memcpy does there);
  • gpu/vk/asm/copy_nt.S lsasm_copy_nt: the C copy_nt's strategy (the head plain, the destination's whole 64-byte
    lines streamed with a prefetch a page ahead, an sfence, the tail plain).

The guarded copy takes lsasm_copy (lsasm_copy_nt with RX_VK_CAPTURE_NT=1); copy_part's own copies go through
capture_copy. guarded_copy's __try still catches a fault on the source inside a kernel, through the kernel's
SEH unwind data. copy_fence now fences after every page-by-page retry, since lsasm_copy (and memcpy) stream over
1.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 in gpu/vk/switches.def), on by default as the
branch shipped it, read once; =0 gives back main's code: memcpy and the C copy_nt, both kept, so the two can be
compared 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, then capture_copy, guarded_copy and copy_fence in
gpu/vk/vk_memory.cpp.

Testing

Tested: Windows 11, i7-9700, RTX 4060, the builder's llvm-mingw clang 23 (CC/CXX, RX_CPM set); head bb9161d5
on #1947's head cffa2f46 (the asm kit, on main 9dd41326). 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 job pr-tb-AsmCut5-light,
log t-AsmCut5-asm-capture-copy): test_asm_copy.py, test_capture_copy_helpers.py, test_capture_copy_nt.py pass (3
of 3). --changed cffa2f46 (build job pr-tb-AsmCut5-build, log t-AsmCut5-chg-asm-capture-copy): 97 checks, 84 pass,
13 fail, each of which fails on #1947's head @ cffa2f46 too (a worktree at cffa2f46 in the queue's jobs, logs
t-AsmCut5-base-kit-changed and t-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 runs
and 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 with RX_VK_CAPTURE_NT=1 crashed 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:

  • Windows 11, i7-9700, RTX 4060 (this PC): Kokiri Forest, turning the camera and walking into Link's house and out,
    uncapped (HOS_FPS_MAX=240), on #1947's head, on this PR with RX_VK_CAPTURE_ASM=0 and 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 report on its folder: the share of copy_part and guarded_copy
    on 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)
  • Linux NVIDIA and AMD, Windows AMD and Intel, Steam Deck: Kokiri Forest, play three minutes on #1947's head, on this
    PR with RX_VK_CAPTURE_ASM=0 and on this PR with it unset, warmed and interleaved, expecting the 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, #1947's head, RX_VK_CAPTURE_ASM=0 and unset, warmed and interleaved, frame
    times and a screenshot)
  • Linux: python3 -I -B tests/test_asm_copy.py --cxx g++ and --cxx clang++, and the same for
    tests/test_capture_copy_nt.py, expecting PASS (by hand)
  • Linux: issue #2285's crossing in Hyrule Field (z about 238), this branch at its defaults against main, interleaved,
    expecting fewer frames of 100 ms or more at the crossing with this branch, where glibc's memcpy does not stream
    over 1.5 MB and this PR's lsasm_copy does (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=0 on both sides so that the copies are this PR's lsasm_copy, the
capture's copying took 0.095 against 0.094 ms a submit with the switch unset and =0 in 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_copy is at memcpy'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 C
copy_nt's speed (0.98-1.03×) at every size in both builds. A micro-benchmark on authored input. The Market's copies
are 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 movsb from 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' memcpy already
streams at these sizes. glibc's does not, and this PR's lsasm_copy streams over 1.5 MB; what that gives on Linux is
not measured by us.

The switch: RX_VK_CAPTURE_ASM, on by default as this PR proposes; level with =0 in play (below). =0 gives back
main's memcpy and C copy_nt for the A/B.

Baseline: the C this PR replaces, as #1947's head cffa2f46 has it (the asm kit on main 9dd41326, which this PR depends on), not main itself.

The micro-benchmark: the capture's copies through a function pointer, as guarded_memcpy and capture_copy call them: memcpy (the C runtime's) against lsasm_copy, and copy_nt cut from gpu/vk/vk_memory.cpp at cffa2f46 against lsasm_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).

Copy, arm Before: memcpy @ cffa2f46 After: lsasm_copy, this PR @ bb9161d5 Change
64 B, warm destination (4,096 copies), SSE2 4.08 [4.05–4.08] 3.74 [3.71–3.76] 1.09×
64 B, warm destination (4,096 copies), AVX2 4.08 [4.08–4.10] 3.56 [3.52–3.66] 1.14×
256 B, warm (2,048), SSE2 9.86 [9.86–10.1] 11.0 [11.0–11.1] 0.89×
256 B, warm (2,048), AVX2 10.4 [10.3–10.5] 10.4 [10.4–10.4] 1.00×
4 KB, warm (128), SSE2 137 [135–138] 124 [122–126] 1.10×
4 KB, warm (128), AVX2 141 [133–141] 140 [131–141] 1.01×
4 KB, cold destination (128), SSE2 284 [283–298] 229 [223–230] 1.24×
4 KB, cold destination (128), AVX2 291 [287–297] 306 [299–306] 0.95×
12 KB, cold (42), SSE2 617 [610–617] 612 [602–612] 1.01×
12 KB, cold (42), AVX2 619 [617–650] 838 [821–1029] 0.74×
12 KB, cold, both sides 3 bytes off (42), SSE2 831 [829–838] 631 [629–636] 1.32×
12 KB, cold, both sides 3 bytes off (42), AVX2 840 [833–860] 862 [855–902] 0.98×
64 KB, warm (8), SSE2 1788 [1788–1938] 1788 [1788–1938] 1.00×
64 KB, warm (8), AVX2 1825 [1788–1875] 1812 [1800–1875] 1.01×
64 KB, cold (8), SSE2 3150 [3100–3225] 3238 [3150–3312] 0.97×
64 KB, cold (8), AVX2 3125 [3075–3175] 3225 [3212–3275] 0.97×
2 MB, cold (1), SSE2 67600 [66600–69100] 71900 [71100–72400] 0.94×
2 MB, cold (1), AVX2 65700 [65700–68200] 65400 [65300–67200] 1.00×
Non-temporal copy (RX_VK_CAPTURE_NT=1), arm Before: C copy_nt @ cffa2f46 After: lsasm_copy_nt, this PR @ bb9161d5 Change
300 B, cold (1,747 copies), SSE2 62.5 [59.2–62.9] 61.9 [59.3–62.9] 1.01×
300 B, cold (1,747 copies), AVX2 61.0 [60.1–61.1] 59.4 [58.9–60.3] 1.03×
4 KB, cold (128), SSE2 171 [170–172] 173 [170–174] 0.99×
4 KB, cold (128), AVX2 163 [162–164] 166 [162–169] 0.98×
12 KB, cold (42), SSE2 450 [436–467] 448 [436–464] 1.01×
12 KB, cold (42), AVX2 421 [407–429] 421 [410–431] 1.00×
12 KB, cold, 3 bytes off (42), SSE2 493 [488–500] 490 [481–500] 1.00×
12 KB, cold, 3 bytes off (42), AVX2 455 [455–479] 452 [450–474] 1.01×
64 KB, cold (8), SSE2 2262 [2238–2312] 2275 [2188–2275] 0.99×
64 KB, cold (8), AVX2 2162 [2075–2175] 2175 [2162–2238] 0.99×
2 MB, cold (1), SSE2 71200 [69800–72900] 71000 [68500–72000] 1.00×
2 MB, cold (1), AVX2 67000 [66900–68100] 65700 [64500–68500] 1.02×

In play (held job fb-g2128, 10:33 to 11:21 UTC on 2026-11-23, one runtime of #2129's 67603ce7 on this head
bb9161d5 and #1947's cffa2f46, with #2130's 4545ceca merged in, which these runs leave at its default on both sides): the
Market, 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); no
load 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_copy ran its 32-byte loop below 16 KB and rep movsb from 16 KB. Reach: the capture's own 600-submit line (vk: capture: a submit ... N copied) shows about 213 pages
copied a submit in the unset side's warm-up, each through lsasm_copy. "Copying" below is that 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).

One =0 run, ca-off4, ended in the load into the Market (frame 8,601) with VK_ERROR_DEVICE_LOST during a write-back
diff 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.

a side: median (range) Before: this PR, RX_VK_CAPTURE_ASM=0 (memcpy), 8 runs After: this PR, unset (lsasm_copy), 8 runs change of the means p (swap test)
the capture's copying, ms a submit 0.094 (0.092 to 0.110) 0.095 (0.094 to 0.104) -0.2 % 0.96
the same, ms per 100 pages copied 0.045 (0.043 to 0.050) 0.045 (0.044 to 0.049) +0.5 % 0.81
the whole capture, ms a submit 0.172 (0.165 to 0.220) 0.173 (0.168 to 0.204) -0.9 % 0.78
its scan, ms a submit 0.077 (0.072 to 0.104) 0.077 (0.073 to 0.096) -1.9 % 0.67
pages copied a submit 214 (207 to 219) 213 (207 to 215) -0.6 % 0.50
calm-stretch game_ms 12.70 (12.19 to 12.95) 12.73 (12.31 to 13.06) +0.8 % 0.50
main_blocked_ms 10.32 (9.80 to 11.53) 10.36 (9.82 to 11.27) -0.2 % 0.93
frames a second 70.5 (58.9 to 73.6) 69.9 (60.5 to 73.4) +0.3 % 0.90
p99 frame time, ms 25.5 (22.0 to 48.8) 24.4 (22.7 to 52.3) -1.2 % 0.95
frames of 50 ms or more, all runs 112 136 - 0.63
draw, ms a frame 3.95 (3.53 to 4.28) 4.03 (3.43 to 4.33) +0.4 % 0.90
memory sync, ms a frame 0.67 (0.64 to 0.79) 0.67 (0.64 to 0.69) -3.3 % 0.23
a frame's replay time (ms): submit 7.42 (6.67 to 8.17) 7.27 (6.45 to 8.55) -1.5 % 0.70

Before 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_ms
12.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 report
on this PR, Hyrule Field at z about 238. One local runtime of this branch (bb9161d5 with #2129's 67603ce7 and #2130's
4545ceca, as In play), plus a diagnostic line that is local and not pushed: each capture copy of 3 ms or more, with its
MB, 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.

Run Crossings Captures of 3 ms or more The largest Median rate Bytes in runs over 1.5 MB / 64 KB to 1.5 MB / under 64 KB
main's C copy (RX_VK_CAPTURE_ASM=0 RX_VK_CAPTURE_REP=0: memcpy) 12 133 (46 of 50 MB or more) 224 MB in 40 ms 9.45 GB/s 80 / 15 / 5 %
this branch's kernels (unset: lsasm_copy, #2129's rep movsb) 0 (left the field) 54 235 MB in 32 ms 7.96 GB/s 79 / 16 / 5 %
main's copy_nt (RX_VK_CAPTURE_NT=1, RX_VK_CAPTURE_ASM=0) 0 (crashed, below) 29 245 MB in 26 ms 8.35 GB/s 87 / 9 / 4 %

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_NT is main's switch (the C copy_nt); with this PR's switch on, it
selects this PR's lsasm_copy_nt instead.

The RX_VK_CAPTURE_NT=1 run crashed after about two minutes, in its second leg (0xc0000005, a read inside ntdll): in
the slow-frame recorder (HOS_SLOW_FRAMES, a diagnostic the script set), in sample_all_thread walking another
thread's stack through unwind_step and RtlVirtualUnwind, not in a copy; the last capture before it was 0.9 MB. It
is 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 changes
for players' saves, options or caches.

Checklist

Ready for review

  • One topic, on current main; the title is a Conventional Commit.
  • Tested on my own PC, in the game for a change the game runs, as Testing says.
  • mise run test passes (any failure that also fails on main is named).
  • Measured against main, or Measurements says why there is nothing to measure.
  • No game files, build output or hand-edited generated files; third_party/ untouched; nothing GPL-incompatible.

When it applies

  • A switch added or changed: it ships at the default the measurements chose, with its row in gpu/vk/switches.def.
  • A check under tests/ that reads changed source is updated, not weakened.
  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.

Ready to merge

  • Everything in Still to test has run, its results in Tested.
  • Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers").
  • Approved in review, every comment answered.
  • CI passes on the head; on current main, measured again or a comment says why the numbers still hold.
## Summary The renderer captures the game's memory into its own buffers before each submit (`gpu/vk/vk_memory.cpp`): the guarded copy of a guest range (`guarded_copy`), and `copy_part`'s copies between its own buffers (the texture shadow's pairs, the staged pages). They went through the C runtime's `memcpy` and, with `RX_VK_CAPTURE_NT=1`, the C `copy_nt`. This adds two hand-written x86-64 kernels on the asm kit and makes the capture use them: - `gpu/asm/copy.S` `lsasm_copy`: a `memcpy` for the capture's copies, an AVX2 and an SSE2 body: up to 256 bytes overlapping moves, then a vector loop, `rep movsb` from 16 KB (AVX2) or 4 KB (SSE2), streaming stores over 1.5 MB (as the UCRT's `memcpy` does there); - `gpu/vk/asm/copy_nt.S` `lsasm_copy_nt`: the C `copy_nt`'s strategy (the head plain, the destination's whole 64-byte lines streamed with a prefetch a page ahead, an `sfence`, the tail plain). The guarded copy takes `lsasm_copy` (`lsasm_copy_nt` with `RX_VK_CAPTURE_NT=1`); `copy_part`'s own copies go through `capture_copy`. `guarded_copy`'s `__try` still catches a fault on the source inside a kernel, through the kernel's SEH unwind data. `copy_fence` now fences after every page-by-page retry, since `lsasm_copy` (and `memcpy`) stream over 1.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 in `gpu/vk/switches.def`), on by default as the branch shipped it, read once; `=0` gives back main's code: `memcpy` and the C `copy_nt`, both kept, so the two can be compared 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`, then `capture_copy`, `guarded_copy` and `copy_fence` in `gpu/vk/vk_memory.cpp`. ## Testing **Tested:** Windows 11, i7-9700, RTX 4060, the builder's llvm-mingw clang 23 (`CC`/`CXX`, `RX_CPM` set); head bb9161d5 on #1947's head cffa2f46 (the asm kit, on main 9dd41326). 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 job `pr-tb-AsmCut5-light`, log `t-AsmCut5-asm-capture-copy`): `test_asm_copy.py`, `test_capture_copy_helpers.py`, `test_capture_copy_nt.py` pass (3 of 3). `--changed cffa2f46` (build job `pr-tb-AsmCut5-build`, log `t-AsmCut5-chg-asm-capture-copy`): 97 checks, 84 pass, 13 fail, each of which fails on #1947's head @ cffa2f46 too (a worktree at cffa2f46 in the queue's jobs, logs `t-AsmCut5-base-kit-changed` and `t-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 runs and 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 with `RX_VK_CAPTURE_NT=1` crashed 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:** - Windows 11, i7-9700, RTX 4060 (this PC): Kokiri Forest, turning the camera and walking into Link's house and out, uncapped (`HOS_FPS_MAX=240`), on #1947's head, on this PR with `RX_VK_CAPTURE_ASM=0` and 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 report` on its folder: the share of `copy_part` and `guarded_copy` on 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) - Linux NVIDIA and AMD, Windows AMD and Intel, Steam Deck: Kokiri Forest, play three minutes on #1947's head, on this PR with `RX_VK_CAPTURE_ASM=0` and on this PR with it unset, warmed and interleaved, expecting the 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, #1947's head, RX_VK_CAPTURE_ASM=0 and unset, warmed and interleaved, frame times and a screenshot) - Linux: `python3 -I -B tests/test_asm_copy.py --cxx g++` and `--cxx clang++`, and the same for `tests/test_capture_copy_nt.py`, expecting PASS (by hand) - Linux: issue #2285's crossing in Hyrule Field (z about 238), this branch at its defaults against main, interleaved, expecting fewer frames of 100 ms or more at the crossing with this branch, where glibc's `memcpy` does not stream over 1.5 MB and this PR's `lsasm_copy` does (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=0` on both sides so that the copies are this PR's `lsasm_copy`, the capture's copying took 0.095 against 0.094 ms a submit with the switch unset and `=0` in 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_copy` is at `memcpy`'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 C `copy_nt`'s speed (0.98-1.03×) at every size in both builds. A micro-benchmark on authored input. The Market's copies are 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 movsb` from 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' `memcpy` already streams at these sizes. glibc's does not, and this PR's `lsasm_copy` streams over 1.5 MB; what that gives on Linux is not measured by us. The switch: `RX_VK_CAPTURE_ASM`, on by default as this PR proposes; level with `=0` in play (below). `=0` gives back main's `memcpy` and C `copy_nt` for the A/B. **Baseline:** the C this PR replaces, as #1947's head cffa2f46 has it (the asm kit on main 9dd41326, which this PR depends on), not main itself. The micro-benchmark: the capture's copies through a function pointer, as `guarded_memcpy` and `capture_copy` call them: `memcpy` (the C runtime's) against `lsasm_copy`, and `copy_nt` cut from `gpu/vk/vk_memory.cpp` at cffa2f46 against `lsasm_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). | Copy, arm | Before: memcpy @ cffa2f46 | After: lsasm_copy, this PR @ bb9161d5 | Change | |---|---|---|---| | 64 B, warm destination (4,096 copies), SSE2 | 4.08 [4.05–4.08] | 3.74 [3.71–3.76] | 1.09× | | 64 B, warm destination (4,096 copies), AVX2 | 4.08 [4.08–4.10] | 3.56 [3.52–3.66] | 1.14× | | 256 B, warm (2,048), SSE2 | 9.86 [9.86–10.1] | 11.0 [11.0–11.1] | 0.89× | | 256 B, warm (2,048), AVX2 | 10.4 [10.3–10.5] | 10.4 [10.4–10.4] | 1.00× | | 4 KB, warm (128), SSE2 | 137 [135–138] | 124 [122–126] | 1.10× | | 4 KB, warm (128), AVX2 | 141 [133–141] | 140 [131–141] | 1.01× | | 4 KB, cold destination (128), SSE2 | 284 [283–298] | 229 [223–230] | 1.24× | | 4 KB, cold destination (128), AVX2 | 291 [287–297] | 306 [299–306] | 0.95× | | 12 KB, cold (42), SSE2 | 617 [610–617] | 612 [602–612] | 1.01× | | 12 KB, cold (42), AVX2 | 619 [617–650] | 838 [821–1029] | 0.74× | | 12 KB, cold, both sides 3 bytes off (42), SSE2 | 831 [829–838] | 631 [629–636] | 1.32× | | 12 KB, cold, both sides 3 bytes off (42), AVX2 | 840 [833–860] | 862 [855–902] | 0.98× | | 64 KB, warm (8), SSE2 | 1788 [1788–1938] | 1788 [1788–1938] | 1.00× | | 64 KB, warm (8), AVX2 | 1825 [1788–1875] | 1812 [1800–1875] | 1.01× | | 64 KB, cold (8), SSE2 | 3150 [3100–3225] | 3238 [3150–3312] | 0.97× | | 64 KB, cold (8), AVX2 | 3125 [3075–3175] | 3225 [3212–3275] | 0.97× | | 2 MB, cold (1), SSE2 | 67600 [66600–69100] | 71900 [71100–72400] | 0.94× | | 2 MB, cold (1), AVX2 | 65700 [65700–68200] | 65400 [65300–67200] | 1.00× | | Non-temporal copy (RX_VK_CAPTURE_NT=1), arm | Before: C copy_nt @ cffa2f46 | After: lsasm_copy_nt, this PR @ bb9161d5 | Change | |---|---|---|---| | 300 B, cold (1,747 copies), SSE2 | 62.5 [59.2–62.9] | 61.9 [59.3–62.9] | 1.01× | | 300 B, cold (1,747 copies), AVX2 | 61.0 [60.1–61.1] | 59.4 [58.9–60.3] | 1.03× | | 4 KB, cold (128), SSE2 | 171 [170–172] | 173 [170–174] | 0.99× | | 4 KB, cold (128), AVX2 | 163 [162–164] | 166 [162–169] | 0.98× | | 12 KB, cold (42), SSE2 | 450 [436–467] | 448 [436–464] | 1.01× | | 12 KB, cold (42), AVX2 | 421 [407–429] | 421 [410–431] | 1.00× | | 12 KB, cold, 3 bytes off (42), SSE2 | 493 [488–500] | 490 [481–500] | 1.00× | | 12 KB, cold, 3 bytes off (42), AVX2 | 455 [455–479] | 452 [450–474] | 1.01× | | 64 KB, cold (8), SSE2 | 2262 [2238–2312] | 2275 [2188–2275] | 0.99× | | 64 KB, cold (8), AVX2 | 2162 [2075–2175] | 2175 [2162–2238] | 0.99× | | 2 MB, cold (1), SSE2 | 71200 [69800–72900] | 71000 [68500–72000] | 1.00× | | 2 MB, cold (1), AVX2 | 67000 [66900–68100] | 65700 [64500–68500] | 1.02× | **In play** (held job `fb-g2128`, 10:33 to 11:21 UTC on 2026-11-23, one runtime of #2129's 67603ce7 on this head bb9161d5 and #1947's cffa2f46, with #2130's 4545ceca merged in, which these runs leave at its default on both sides): the Market, 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); no load 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_copy` ran its 32-byte loop below 16 KB and `rep movsb` from 16 KB. Reach: the capture's own 600-submit line (`vk: capture: a submit ... N copied`) shows about 213 pages copied a submit in the unset side's warm-up, each through `lsasm_copy`. "Copying" below is that 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). One `=0` run, ca-off4, ended in the load into the Market (frame 8,601) with `VK_ERROR_DEVICE_LOST` during a write-back diff 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. | a side: median (range) | Before: this PR, `RX_VK_CAPTURE_ASM=0` (`memcpy`), 8 runs | After: this PR, unset (`lsasm_copy`), 8 runs | change of the means | p (swap test) | |---|---|---|---|---| | the capture's copying, ms a submit | 0.094 (0.092 to 0.110) | 0.095 (0.094 to 0.104) | -0.2 % | 0.96 | | the same, ms per 100 pages copied | 0.045 (0.043 to 0.050) | 0.045 (0.044 to 0.049) | +0.5 % | 0.81 | | the whole capture, ms a submit | 0.172 (0.165 to 0.220) | 0.173 (0.168 to 0.204) | -0.9 % | 0.78 | | its scan, ms a submit | 0.077 (0.072 to 0.104) | 0.077 (0.073 to 0.096) | -1.9 % | 0.67 | | pages copied a submit | 214 (207 to 219) | 213 (207 to 215) | -0.6 % | 0.50 | | calm-stretch game_ms | 12.70 (12.19 to 12.95) | 12.73 (12.31 to 13.06) | +0.8 % | 0.50 | | main_blocked_ms | 10.32 (9.80 to 11.53) | 10.36 (9.82 to 11.27) | -0.2 % | 0.93 | | frames a second | 70.5 (58.9 to 73.6) | 69.9 (60.5 to 73.4) | +0.3 % | 0.90 | | p99 frame time, ms | 25.5 (22.0 to 48.8) | 24.4 (22.7 to 52.3) | -1.2 % | 0.95 | | frames of 50 ms or more, all runs | 112 | 136 | - | 0.63 | | draw, ms a frame | 3.95 (3.53 to 4.28) | 4.03 (3.43 to 4.33) | +0.4 % | 0.90 | | memory sync, ms a frame | 0.67 (0.64 to 0.79) | 0.67 (0.64 to 0.69) | -3.3 % | 0.23 | | `a frame's replay time (ms)`: submit | 7.42 (6.67 to 8.17) | 7.27 (6.45 to 8.55) | -1.5 % | 0.70 | Before 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_ms 12.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 report on this PR, Hyrule Field at z about 238. One local runtime of this branch (bb9161d5 with #2129's 67603ce7 and #2130's 4545ceca, as In play), plus a diagnostic line that is local and not pushed: each capture copy of 3 ms or more, with its MB, 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. | Run | Crossings | Captures of 3 ms or more | The largest | Median rate | Bytes in runs over 1.5 MB / 64 KB to 1.5 MB / under 64 KB | |---|---|---|---|---|---| | main's C copy (`RX_VK_CAPTURE_ASM=0 RX_VK_CAPTURE_REP=0`: `memcpy`) | 12 | 133 (46 of 50 MB or more) | 224 MB in 40 ms | 9.45 GB/s | 80 / 15 / 5 % | | this branch's kernels (unset: `lsasm_copy`, #2129's `rep movsb`) | 0 (left the field) | 54 | 235 MB in 32 ms | 7.96 GB/s | 79 / 16 / 5 % | | main's `copy_nt` (`RX_VK_CAPTURE_NT=1`, `RX_VK_CAPTURE_ASM=0`) | 0 (crashed, below) | 29 | 245 MB in 26 ms | 8.35 GB/s | 87 / 9 / 4 % | 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_NT` is main's switch (the C `copy_nt`); with this PR's switch on, it selects this PR's `lsasm_copy_nt` instead. The `RX_VK_CAPTURE_NT=1` run crashed after about two minutes, in its second leg (`0xc0000005`, a read inside ntdll): in the slow-frame recorder (`HOS_SLOW_FRAMES`, a diagnostic the script set), in `sample_all_thread` walking another thread's stack through `unwind_step` and `RtlVirtualUnwind`, not in a copy; the last capture before it was 0.9 MB. It is 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 changes for players' saves, options or caches. ## Checklist ### Ready for review - [ ] One topic, on current `main`; the title is a Conventional Commit. - [ ] Tested on my own PC, in the game for a change the game runs, as Testing says. - [ ] `mise run test` passes (any failure that also fails on `main` is named). - [ ] Measured against `main`, or Measurements says why there is nothing to measure. - [x] No game files, build output or hand-edited generated files; `third_party/` untouched; nothing GPL-incompatible. ### When it applies - [ ] A switch added or changed: it ships at the default the measurements chose, with its row in `gpu/vk/switches.def`. - [x] A check under `tests/` that reads changed source is updated, not weakened. - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. ### Ready to merge - [ ] Everything in Still to test has run, its results in Tested. - [ ] Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers"). - [ ] Approved in review, every comment answered. - [ ] CI passes on the head; on current `main`, measured again or a comment says why the numbers still hold.
The ground the hand-written kernels stand on, without any kernel yet:

- gpu/asm/lsasm.h: a kernel is written once, in Intel syntax against the System V registers; the macros make the
  Win64 convention with its SEH unwind data on Windows (an epilogue laid out as Windows' unwinder recognises one:
  the vzeroupper and the frees before the pops) and the DWARF CFI on Linux, and LSASM_ABI for the declarations.
- tests/asm_kit.py: a kernel's check builds its fixture with the kernels at SSE2 and AVX2 (where the CPU has it),
  on Windows also the System V body through sysv_abi, and assembles every kernel with clang for ELF and COFF.
- tests/test_asm_abi.py with tests/asm_abi_probe.S: probes that use every part of the frame: six arguments arrive,
  every callee-saved register comes back, a fault inside one unwinds to the caller (Windows), and every single step
  of each probe is unwound by Windows' own unwinder to its caller.
- nx2/build.py asm_jobs: every .S of host/asm goes into the game, of gpu/vk/asm into the renderer, of gpu/asm into
  both, preprocessed with the build's CPU flags and keyed by the headers beside them and lsasm.h. No folder, no job.
- scripts/devtools/packs.py takes a .S as a source (it shows in its module's objects) and nx2/version.py counts it in
  the sources' fingerprint.
- tests/test_asm_build.py, tests/test_dev_packs.py: the jobs, their keys, the fingerprint and the packs' rules.

Assisted-by: Claude Code
tests/asm_abi_probe.S has three more probes that return rsp modulo 16 in their body: scratch alone, one push and
scratch, two pushes and the xmm saves, an even and an odd count of pushes in both conventions (Win64's LSASM_ARGS
pushes two). Win64 asks a function with a frame to keep rsp 16-byte aligned in its body, and LSASM_ALLOC's scratch
at [rsp] is aligned only so. On Windows each is stepped and unwound as the others are. tests/test_asm_abi.py also
assembles a kernel with LSASM_ALLOC 3072 (it must assemble) and 4096 (a page and a push: it must stop the assembly,
as the macros probe no stack page and its first store could skip Windows' guard page).

Both fail on this commit: lsasm.h leaves rsp at 8 modulo 16 with an even count of pushes, and takes any scratch size.

Assisted-by: Claude Code
Win64 asks a function with a frame to keep rsp 16-byte aligned in its body, and the macros did not: with an even
count of pushes (LSASM_ARGS's two included) the body ran at 8 modulo 16, so an aligned store to LSASM_ALLOC's
scratch could fault. The macros now count the pushes and take 8 bytes more below them where the count leaves rsp
misaligned, before the first xmm save or scratch (or at the prologue's end), in the SEH codes and the CFI alike; the
epilogue gives them back with its one add (Windows) or at the first pop (Linux). A push after the xmm saves or the
scratch stops the assembly, as the padding could not count it.

The macros probe no stack page, so a frame of more than a page could skip Windows' guard page with its first store:
LSASM_END_PROLOGUE stops the assembly of a frame (pushes, padding, xmm saves and scratch) over 4096 bytes.

The header also says what LSASM_ARGS maps: six integer or pointer arguments; a float argument is in another register
in each convention. The fifth and sixth stay loaded on every call: their slots lie inside the caller's own frame.

Assisted-by: Claude Code
tests/test_asm_build.py checks the kernels' object keys as compile_one names them: a header renamed with its contents or
removed changes the key (a clean compile of the kernel would fail), a .inc edit changes it, and a kernel whose depfile
exists is keyed by the headers it names (an edit to another header keeps it). A gpu/asm kernel's second job publishing
the same key after the first must leave the first job's object (the cache entry is a hard link to it) as it was. A
renderer file whose depfile names a gpu/asm declaration is keyed by it, through main()'s own vdeps_roots statement.

They fail on this commit: the key joins the folders' .h contents alone, cache_put copies over an entry that
os.link found there, and the renderer's roots leave gpu/asm out.

Assisted-by: Claude Code
asm_jobs keyed a kernel's object by the .h contents of its folder and gpu/asm joined: a header renamed with its
contents kept an object a clean compile no longer makes, and a .inc edit was not seen. A kernel is now compiled with
-MD and keyed as the host's and the back end's own files are (renderer_deps, DEPFILE_JOBS, rekey_depfiles): by the
names and contents of the headers its last compile read under its folder and gpu/asm, or by every header of the two
folders (tree_headers) before it has a depfile.

A gpu/asm kernel is compiled for each module with one key, and on a cold build both jobs could publish it: the second's
os.link failed on the first's entry, and its copyfile then wrote into that entry, a hard link to the first job's object.
cache_put leaves an entry it finds (FileExistsError), and a copy across volumes goes in whole by a rename.

The back end's own objects are keyed by gpu/asm's headers too (vdeps_roots), so a Vulkan caller of a shared kernel
compiles again when the kernel's declaration changes.

Assisted-by: Claude Code
tests/test_appimage_source_headers.py runs the AppImage script's copy block and checks that every file of gpu/asm
(lsasm.h, which every kernel includes) is carried and that every kernel's includes are, for host/asm, gpu/asm and
gpu/vk/asm (gpu/vk itself is copied whole after the block). tests/test_dev_packs.py routes the three kernel folders: a
host/asm kernel is the game's alone, so its pack builds the game without the renderer (renderer_untouched), a gpu/vk/asm
kernel builds the renderer alone, a gpu/asm kernel both.

They fail on this commit: the AppImage copies host/ and gpu/vk but not gpu/asm (a player's build with a host/asm kernel
stops listing it), and renderer_untouched takes only a host/ .c for the game's alone.

Assisted-by: Claude Code
scripts/linux/appimage.sh copied host/ and gpu/vk whole but not gpu/asm: a player's build with a host/asm kernel
stopped at asm_jobs listing the missing folder, and a gpu/asm kernel was left out of both modules. The source-copy
block now carries gpu/asm whole; tests/test_appimage_headers.py takes it for a host include of a kernel's declaration.

scripts/devtools/packs.py renderer_untouched took a host/ .c alone for the game's: a pack whose change was a host/asm
kernel, which only the game links, built and linked the renderer again. A host/asm .S is the game's alone now; a
header beside it, a gpu/asm kernel and a gpu/vk/asm kernel still build the renderer.

Assisted-by: Claude Code
tests/test_asm_kit.py records the commands tests/asm_kit.py would run, with no compiler: its CPU probe must read the
whole x86-64-v3 level (leaf 7, the extended leaf's LZCNT, the OS's ymm state through xgetbv) and a probe that crashed
must fail the check rather than read as a CPU without AVX2; two kernels of one name in two folders must be two
objects, natively and in the cross assembly, each linked once; a run with no clang found must fail instead of skipping
the cross assembly, an explicit clang must be the one used, and tests/test_asm_abi.py must take CXX without --cxx.

They fail on this commit: the probe reads AVX2, BMI2 and FMA alone and counts any exit but 0 as no AVX2, the objects
are named by the source's stem, a run without clang skips the cross assembly, and --cxx defaults to g++.

Assisted-by: Claude Code
tests/asm_kit.py ran the x86-64-v3 fixture where the CPU had AVX2, BMI2 and FMA, but a VM can mask MOVBE or LZCNT,
which the compiler emits at that level, and any exit of the probe but 0 read as no AVX2, a crash too. Its probe, built
for the baseline, now reads the whole level from CPUID (leaf 1, leaf 7, the extended leaf) and the OS's ymm state
(xgetbv), and an exit other than 0 or 1 fails the check.

The objects were named by the source's stem, so gpu/asm/hash.S and gpu/vk/asm/hash.S wrote one object and linked it
twice: each source's index is in its name now, natively and in the cross assembly.

Without clang on PATH the cross assembly (clang's assembler, the build's, for ELF and COFF) was skipped without a word,
and a CPU without AVX2 was then told its AVX2 bodies had been assembled. The clang is now the one given (--clang in
tests/test_asm_abi.py), else the one beside the compiler, else the one on PATH, and without one the check fails; its
PASS line names it. tests/test_asm_abi.py takes CXX when --cxx is not given. The unused os import is gone (Ruff F401).

Assisted-by: Claude Code
tests/test_asm_abi.py left the DWARF CFI unconsumed: removing the CFA adjustment after LSASM_ALLOC kept Linux green. A
Linux probe now stops in its body, with its scratch and padding in place, and calls the fixture, which unwinds from
there with the system's unwinder (_Unwind_Backtrace): the probe's frame must return to the harness's call, with rbx,
rbp and r12-r15 as the harness set them. A probe reads a table in LSASM_RODATA RIP-relative, so that macro is
assembled (ELF and COFF) and run.

On Windows each single step's unwind is now compared with the whole nonvolatile state of the entry: rsi, rdi and
xmm6-xmm15 as well in Win64, not the six other GPRs alone. A fault in a kernel is also taken by Windows' own dispatch
to a caller's __except, built with the game's -fms-extensions as guarded_copy is (clang; a gcc build says it skipped
this), besides the vectored handler that unwinds the one frame by hand.

Assisted-by: Claude Code
tests/asm_abi_probe.S calls a kernel of one argument on a stack that ends at its 32-byte home area, all a one-argument
Win64 call owes its callee, with the page above it left unreadable (tests/test_asm_abi.py, before any handler is set):
lsasm_probe_clobber and lsasm_probe_table must return their results there.

It fails on this commit: LSASM_ARGS reads the fifth and sixth slots on every call, so the first read lands in the
unreadable page.

Assisted-by: Claude Code
On Win64 LSASM_ARGS loaded the fifth and sixth arguments from the caller's stack on every call. A call of fewer owes
its callee the 32-byte home area alone, so the slots above it can lie past the stack's top. LSASM_ARGS now takes the
kernel's count of arguments (0 to 6; another count stops the assembly) and moves or loads only those: a one-argument
kernel reads no stack slot. The header says the mapping is for integer and pointer arguments; the probes give their
counts (6 for lsasm_probe_args, 1 for the others).

Assisted-by: Claude Code
tests/test_asm_build.py looked for asm_jobs' calls in main()'s text: it passed if a guard kept a call from running or
its jobs from a link list. main() now plans through plan_modules (nx2/build.py): the game's other jobs and the
renderer's in, the kernels joined (host/asm's and gpu/asm's to the game when it compiles the runtime, gpu/vk/asm's and
gpu/asm's to the renderer when it is built), RX_VK_ONLY's renderer-only jobs, and out the jobs and the two link lists
objs.rsp and vk_objs.rsp are written from. The build is the same as before; the kernels' jobs come after the game's
other jobs instead of after the runtime's files.

The test runs it on synthetic sources (two kernels in host/asm, one in gpu/asm, one of the same name in gpu/vk/asm)
and checks the compiled jobs and both modules' link inputs in full for a full build, a game-only build (no renderer
folder, as main() passes for RX_GAME_ONLY) and a renderer-only build (RX_VK_ONLY), and that main() plans and links
through it. tests/test_link_skip.py, test_renderer_depfiles.py and test_third_party_objects.py anchor on the planner's
line now, the lines they named being gone. docs/dev/reference/build.md names it.

Assisted-by: Claude Code
Importing nx2/build.py took the machine's build settings: RX_MARCH=native, or a CPU with x86-64-v3 and no RX_MARCH,
has clang say what -march=native is while the module loads, and RX_MARCH=native with no working clang stopped the
import before unittest started, in a check that says it runs no compiler. The test now imports it with the build's own
variables (RX_*, LONGSHOT_*, OOT_*) cleared and RX_MARCH=x86-64-v3, and with subprocess.Popen and os.system failing
while it loads: a process started by the import fails it instead of running. A test checks the CPU flags are the
fixed level's.

Assisted-by: Claude Code
hos_gpu_guest_ranges (host/nvdrv.c) sorts every GPU mapping's and native NVN pool's guest range by start before it
merges them, each time the mappings change: qsort with a comparison function call per compare. On x86-64 it now sorts
with host/asm/ranges.S's lsasm_sort_ranges, a hand-written merge sort on gpu/asm/lsasm.h: runs of 16 pairs by
insertion, then merges of runs twice as long a pass between the list and a second one (kept beside merged, grown with
it); two runs already in order (the mappings come mostly in address order) are copied, not merged, and the merge picks
its side with cmov, no branch. One body for both CPU levels: the pairs move in xmm registers. Other CPUs keep qsort
with cmp_range, under #if defined(__x86_64__). Both lists grow through hos_xrealloc (an allocation refused for 5 s
ends the run with the out-of-memory notice), where realloc's NULL was used as the list before.

It is stable where qsort is not: ranges that start together come out of qsort in any order, and the merge after the
sort makes the same ranges from any such order (one starts the merged range, the others only reach to their ends), so
the result is the same. No switch: the ranges merged are the same.

From 971f8074 on exp/x86-asm-sync, where the sort was collect.S's second kernel; here it is its own file. A
micro-benchmark there (i7-9700) found the kernel faster than qsort, more so in near order; not measured on this commit,
nor in play.

Test: tests/test_asm_ranges.py, through tests/asm_kit.py at SSE2 and AVX2, both conventions, clang ELF/COFF assembly:
the kernel against qsort with cmp_range (cut from the source) for 22 sizes around its runs and merges up to 10000 pairs
and 8 key patterns, between faulting pages: same order, stable, and hos_gpu_guest_ranges' merge makes the same ranges
from either order. The whole hos_gpu_guest_ranges, cut from the source, is compiled with its x86-64 test taken both
ways: the two give the same ranges for every case, one sorting with the kernel and the other with qsort, once a call.
scripts/profiling/cpu_functions.json describes lsasm_sort_ranges, and cmp_range as the other CPUs' comparison.

Assisted-by: Claude Code
The review asked for one shared kernel and one private kernel per module through a cold build, a cached rebuild after
an included file's rename, developer packs and the Linux AppImage's staging. tests/test_asm_end_to_end.py authors them
in a tree of its own (gpu/asm/e2e.S, host/asm/game.S, gpu/vk/asm/vk.S, each with an include, on the real lsasm.h) and
runs the build's own code on them with clang: plan_modules, plan_jobs with the hash record a build loads and saves,
compile_one on four threads (the shared kernel's two jobs at once), rekey_depfiles and main()'s own statements that
write the link lists. Each module's link list is linked with a caller and run, where this CPU runs x86-64-v3.

- a cold build compiles every kernel; a second build compiles nothing, nor does a new work folder made from its
  object cache (packs.link_objects), whose modules are the first's (module_changed);
- an include renamed while its kernel still names it compiles that kernel again and fails, as a clean build would; with
  the kernel naming the new file it compiles again, alone of the kernels;
- an include's edit compiles the kernels that read it, and its revert compiles nothing and links the cold build's
  objects, unchanged;
- packs one after another on one base (route, link_inputs_changed, renderer_untouched, link_objects, module_changed as
  cmd_pack runs them): a game kernel builds the game alone, a renderer kernel the renderer alone, a shared one both;
  each compiles the kernels that changed, and the base's objects and cache stay as they were;
- the AppImage's source-copy block, run with bash on the tree, carries the kernels and their includes, and a cold
  build of the staged tree compiles and links them.

nx2/build.py is imported as tests/test_asm_build.py imports it. The compiler is --cxx (CXX, else g++), clang beside it
(tests/asm_kit.py find_clang). The staging check needs bash: Git for Windows' on Windows, else it is skipped.

Assisted-by: Claude Code
compile_one hard-links each object into the object cache (cache_put), and links an object from the cache the same way;
a pack's work folder links the base's cache too (scripts/devtools/packs.py link_objects). clang's assembler, which
compiles the hand-written kernels (asm_jobs), writes its output in place (cc1as opens the -o path itself; clang's C and
C++ compiles write a temporary file and rename it over the object). A kernel compiled again therefore wrote through the
link: the cache's entry under the old key became the new object, and in a pack's work folder the base's cache entry and
its object did too. A build back at the old key (an edit reverted, a pack on that base) linked the edited kernel without
a word. compile_one now removes the object before it starts the compiler, so the compiler writes a new file and the
entries linked to the old one keep it. Every compile goes through it; for the C and C++ ones nothing changes, as their
compiler replaced the file anyway.

Test: tests/test_asm_end_to_end.py, which failed at the test commit: an include's edit and its revert linked the edited
kernel (the cache's entry under the cold build's key was the edited object), and a shared kernel's pack on a base wrote
into the base's objects and cache.

Assisted-by: Claude Code
The review: compile_one removes the old object but leaves its stamp (<object>.sha1) while clang's assembler writes the
new object in place. A build whose process tree is terminated during that write leaves part of an object under the old
stamp; once the edit is reverted the stamp matches the key again, and the first check (the object and its stamp) takes
the partial object before the object cache's valid entry is looked at.

tests/test_asm_end_to_end.py builds the kernels cold, edits the game kernel's include, and runs the next build with a
stand-in assembler for that kernel: a process that writes the first 64 bytes of the object at its own path and is
killed, and the build stops there (as its process tree terminated). The include is then reverted: the next build must
compile nothing and link the cold build's objects, unchanged, and the game's caller must give the cold build's values.

Assisted-by: Claude Code
The review: compile_one removed the object before it started the compiler but left its stamp (<object>.sha1). clang's
assembler, which compiles the hand-written kernels, writes its object in place, so a build whose process tree ended
during that write left part of an object under the old stamp. Once the edit was reverted the stamp matched the key
again, and the first check (the object and a stamp of its key) took the partial object before it looked in the object
cache, where the valid one is.

compile_one now removes the stamp as soon as it does not match: the object is replaced after that (linked from the
cache, or compiled), and only a finished one is stamped again. Without a stamp the next build links the cache's entry
or compiles again. The same holds for a link from the cache that stops before its stamp is written.

Test: tests/test_asm_end_to_end.py, test_a_stopped_replacement_and_its_revert, which failed at the test commit (the
revert linked the 64 bytes the stopped assembler had written).

Assisted-by: Claude Code
The review: main() sorts each compile's seconds into "renderer" or "host and game" by vk_job_objects, built from the
renderer's jobs before plan_modules joined the kernels to them (a new list), so a gpu/vk/asm or gpu/asm kernel of the
renderer was counted as host and game time.

tests/test_asm_build.py runs main()'s own statement (found in its syntax tree) on a plan of synthetic kernels: every
object the renderer links, its kernels included, is renderer time.

Assisted-by: Claude Code
vk_job_objects, by which main() counts each compile's seconds as renderer or host and game time, is now the set of the
renderer's link inputs plan_modules gives (vk_objs), its kernels included. It was built from the renderer's jobs before
the plan joined the kernels to them, so a renderer kernel was counted as host and game time.

Test: tests/test_asm_build.py, test_the_compile_time_breakdown_counts_the_renderer_kernels, which failed at the test
commit (vk_vk_draw.o alone was renderer time).

Assisted-by: Claude Code
The review: hos_gpu_guest_ranges grows the kernel's scratch list (one pair per range) through hos_xrealloc while it
holds the mapping lock. At the commit limit hos_xrealloc tries again for up to 5 s before the out-of-memory stop, and
every mapping change and lookup waits on the lock for as long.

tests/test_asm_ranges.py's fixture now holds a flag for the lock (LOCK, UNLOCK) and can refuse the scratch list: its
hos_xrealloc fails the check when asked for the sort's list while it is refused (the 5 s of tries), and the plain
realloc returns NULL. A call whose ranges outgrow every list the other cases grew, with the scratch refused, must sort
with qsort in place, not with the kernel, and leave the lock; the next call, with the scratch allowed, sorts with the
kernel, and both give the same ranges.

Assisted-by: Claude Code
fix(asm): a refused scratch list for the ranges' sort is not waited for with the mapping lock held
Some checks failed
Website / Website on Cloudflare (pull_request_target) Has been skipped
beanstalk/approval waiting for a BigBean's 👍 on the request
PR description / Description follows the template (pull_request_target) Successful in 5s
CI / Conventional Commit title (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been cancelled
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been cancelled
CI / Workflow lint (actionlint) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled
Guide screenshots / Guide images and local UI source review (pull_request) Has been cancelled
Third-party parts / As their notes say (pull_request) Has been cancelled
Third-party parts / The same as upstream (pull_request) Has been cancelled
Website / Website on Cloudflare (pull_request) Has been cancelled
cffa2f46ba
hos_gpu_guest_ranges grew lsasm_sort_ranges' scratch list (one pair per range) through hos_xrealloc with the mapping
lock held: refused at the commit limit, hos_xrealloc tries again for up to 5 s before the out-of-memory stop, and every
mapping change and lookup waited on the lock as long. The list is only the sort's; the ranges can be sorted without it.

It now grows through plain realloc, with a capacity of its own. When it is refused the call sorts with qsort and
cmp_range in place (as other CPUs do), and the next change of the mappings asks for it again. The merged list still
grows through hos_xrealloc: the call cannot go on without it (on main its realloc's NULL was used as the list).

Test: tests/test_asm_ranges.py, which failed at the test commit (the refused scratch went through hos_xrealloc's tries
with the lock held).

Assisted-by: Claude Code
perf(vk): the capture's copies use hand-written x86-64 kernels (lsasm_copy, lsasm_copy_nt; RX_VK_CAPTURE_ASM)
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
CI / Website build and checks (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request_target) Has been skipped
beanstalk/approval waiting for a BigBean's 👍 on the request
PR description / Description follows the template (pull_request_target) Successful in 5s
bb9161d501
- gpu/asm/copy.S lsasm_copy: memcpy for the capture's copies, one Intel-syntax body for Win64 (SEH) and System V (CFI)
  through gpu/asm/lsasm.h, an AVX2 and an SSE2 body: up to 256 bytes overlapping moves, then a vector loop, rep movsb
  from 16 KB (AVX2) or 4 KB (SSE2), streaming stores over 1.5 MB (as the UCRT's memcpy does there).
- gpu/vk/asm/copy_nt.S lsasm_copy_nt: the C copy_nt's strategy (the head plain, dst's whole 64-byte lines streamed with
  a prefetch a page ahead, an sfence, the tail plain), sharing copy.h's macros.
- gpu/vk/vk_memory.cpp: the guarded copy takes lsasm_copy (lsasm_copy_nt with RX_VK_CAPTURE_NT=1), and copy_part's
  own copies between our buffers (the texture shadow's pairs, the staged pages) go through capture_copy, lsasm_copy.
  guarded_copy's __try catches a fault on the source inside a kernel through the kernel's SEH unwind data.
  copy_fence now fences after every page-by-page retry: lsasm_copy, and memcpy too, stream over 1.5 MB.
- Main has its own copies here (memcpy and the C copy_nt): they stay, behind the new kill switch RX_VK_CAPTURE_ASM
  (default on, as the branch shipped; =0 or off, false, no: memcpy and copy_nt, as before), read once, so both can be
  compared in one build. The bytes copied are the same either way.
- Not measured on this port: the branch's micro-benchmarks timed the branch's code, not this one; not measured in play.
- Tests: tests/test_asm_copy.py (with tests/asm_copy_harness.S) checks lsasm_copy against memcpy for sizes 0-9300 at
  every alignment, around each strategy's edge and up to 9 MB, and on Windows a fault in each of both kernels' paths
  unwound to the caller with its callee-saved registers; tests/test_capture_copy_nt.py checks lsasm_copy_nt against the
  C copy_nt cut from vk_memory.cpp, and the wiring: the readers and the two copies' setup cut from vk_memory.cpp and run
  in a process of its own for each setting of RX_VK_CAPTURE_NT and RX_VK_CAPTURE_ASM (unset, 0, off, No, 1, yes, 2, NT=1
  with ASM unset and =0, 0 again), each then set the other way (read once); copy_part's copies and fences from its text;
  tests/test_capture_copy_helpers.py stubs capture_copy for the copy_part it cuts. Both CPU levels and both conventions
  (tests/asm_kit.py).

Needs build(asm): the kit (gpu/asm/lsasm.h, tests/asm_kit.py, nx2/build.py asm_jobs) first.

Assisted-by: Claude Code
Member

Possibly related, for context:

  • PR #1667 WIP: perf(vk): RX_VK_CAPTURE_SLOW_MS= logs a slow capture's parts (probe, off by default) — Capture-copy diagnostics and prior evidence that copying is costly.
Possibly related, for context: - PR #1667 WIP: perf(vk): RX_VK_CAPTURE_SLOW_MS=<ms> logs a slow capture's parts (probe, off by default) — Capture-copy diagnostics and prior evidence that copying is costly.
Author
Contributor

@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.

@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.
Member

@siantm only members of BigBeans can start the farm. One of them can approve this request by reacting 👍 to it or to this comment.

@siantm only members of `BigBeans` can start the farm. One of them can approve this request by reacting 👍 to it or to this comment.
Author
Contributor

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

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
Author
Contributor

@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.

@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.
Member

@siantm only members of BigBeans can start the farm. One of them can approve this request by reacting 👍 to it or to this comment.

@siantm only members of `BigBeans` can start the farm. One of them can approve this request by reacting 👍 to it or to this comment.
Author
Contributor

@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.

@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.
Member

@siantm only members of BigBeans can start the farm. One of them can approve this request by reacting 👍 to it or to this comment.

@siantm only members of `BigBeans` can start the farm. One of them can approve this request by reacting 👍 to it or to this comment.
Contributor

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 the kokiri-start save, teleport to the Market, then out of the gate) and back across one line (z ~238 in status' position, x ~0; the gate is at z ~32) stalls the game for 100 to 300 ms at every crossing, both directions. On main @ 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; with RX_VK_CAPTURE=0, none in 8 crossings (worst 42 ms). In a sampled stall, 43 % of the game's main thread is in nvnQueueSubmitCommands → 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.

**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 the `kokiri-start` save, teleport to the Market, then out of the gate) and back across one line (z ~238 in `status`' position, x ~0; the gate is at z ~32) stalls the game for 100 to 300 ms at every crossing, both directions. On `main` @ 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; with `RX_VK_CAPTURE=0`, none in 8 crossings (worst 42 ms). In a sampled stall, 43 % of the game's main thread is in `nvnQueueSubmitCommands` → `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.
Author
Contributor

@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_cold made it 1.39 times faster, about 0.16 ms a frame, with nothing shown in frame time; this PR's lsasm_copy was level with memcpy.

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 main 9dd41326 with the asm kit, Windows and NVIDIA, so if it does not reproduce here we'll say so.

@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_cold` made it 1.39 times faster, about 0.16 ms a frame, with nothing shown in frame time; this PR's `lsasm_copy` was level with `memcpy`. 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 main 9dd41326 with the asm kit, Windows and NVIDIA, so if it does not reproduce here we'll say so.
Contributor

@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:

Run Crossings Frames > 40 ms Frames > 100 ms Worst
RX_VK_CAPTURE_NT=1 12 3 0 44 ms
RX_VK_CAPTURE_NT=1 12 3 0 47 ms
main's copy 12 28 11 312 ms
RX_VK_CAPTURE_NT=1 12 2 0 63 ms
main's copy 13 23 10 328 ms

What 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=1 at 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. So lsasm_copy's streaming path over 1.5 MB should take most of it, where glibc's memcpy doesn'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_make intends.

@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: | Run | Crossings | Frames > 40 ms | Frames > 100 ms | Worst | |---|---|---|---|---| | `RX_VK_CAPTURE_NT=1` | 12 | 3 | 0 | 44 ms | | `RX_VK_CAPTURE_NT=1` | 12 | 3 | 0 | 47 ms | | main's copy | 12 | 28 | 11 | 312 ms | | `RX_VK_CAPTURE_NT=1` | 12 | 2 | 0 | 63 ms | | main's copy | 13 | 23 | 10 | 328 ms | **What 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=1` at 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. So `lsasm_copy`'s streaming path over 1.5 MB should take most of it, where glibc's `memcpy` doesn'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_make` intends.
Author
Contributor

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.

  • Main's C copy (RX_VK_CAPTURE_ASM=0 RX_VK_CAPTURE_REP=0, the C runtime's memcpy), 12 crossings of z ≈ 238:
    • 133 captures of 3 ms or more, 46 of them 50 MB or more; the largest was 224 MB in 40 ms, the slowest copy 43 ms, median 9.5 GB/s.
    • Bytes by run length: 80 % in runs over 1.5 MB, 15 % from 64 KB to 1.5 MB, 5 % under 64 KB (yours: 84 / 13 / 2).
    • At 10 of the 12 crossings no frame took 100 ms or more within about 1.5 s; the worst was 68 ms.
    • At the other two, an in-game event moved Link from z ≈ 162 to z ≈ 270 with a short load. That load had one frame of 156 ms and one of 196 ms, spent in memory sync, not in a capture copy.
  • With this branch's kernels (switches unset; that build also carried #2129's rep movsb, stacked on this one) and with RX_VK_CAPTURE_NT=1 (main's C copy_nt, as RX_VK_CAPTURE_ASM=0 was 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=1 figures are the nearest measurement there.

Two more things. RX_VK_CAPTURE_NT is main's switch (the C copy_nt); this pull request's own switch is RX_VK_CAPTURE_ASM, and with it on (the default) RX_VK_CAPTURE_NT=1 copies with this pull request's lsasm_copy_nt in place of main's copy_nt. RX_VK_CAPTURE_REP is #2129's. Also, the RX_VK_CAPTURE_NT=1 run 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.

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. - Main's C copy (`RX_VK_CAPTURE_ASM=0 RX_VK_CAPTURE_REP=0`, the C runtime's memcpy), 12 crossings of z ≈ 238: - 133 captures of 3 ms or more, 46 of them 50 MB or more; the largest was 224 MB in 40 ms, the slowest copy 43 ms, median 9.5 GB/s. - Bytes by run length: 80 % in runs over 1.5 MB, 15 % from 64 KB to 1.5 MB, 5 % under 64 KB (yours: 84 / 13 / 2). - At 10 of the 12 crossings no frame took 100 ms or more within about 1.5 s; the worst was 68 ms. - At the other two, an in-game event moved Link from z ≈ 162 to z ≈ 270 with a short load. That load had one frame of 156 ms and one of 196 ms, spent in memory sync, not in a capture copy. - With this branch's kernels (switches unset; that build also carried #2129's `rep movsb`, stacked on this one) and with `RX_VK_CAPTURE_NT=1` (main's C `copy_nt`, as `RX_VK_CAPTURE_ASM=0` was 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=1` figures are the nearest measurement there. Two more things. `RX_VK_CAPTURE_NT` is main's switch (the C `copy_nt`); this pull request's own switch is `RX_VK_CAPTURE_ASM`, and with it on (the default) `RX_VK_CAPTURE_NT=1` copies with this pull request's `lsasm_copy_nt` in place of main's `copy_nt`. `RX_VK_CAPTURE_REP` is #2129's. Also, the `RX_VK_CAPTURE_NT=1` run 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.
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
CI / Website build and checks (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request_target) Has been skipped
beanstalk/approval waiting for a BigBean's 👍 on the request
PR description / Description follows the template (pull_request_target) Successful in 5s
This pull request has changes conflicting with the target branch.
  • nx2/build.py
Some workflows are waiting to be reviewed.
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.
git fetch -u 'perf/asm-capture-copy:siantm-perf/asm-capture-copy'
git switch 'siantm-perf/asm-capture-copy'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
4 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/Longshot!2128
No description provided.