WIP: perf(vk): a draw's state compares in x86-64 assembly (dynamic state, vertex set, binding range, pipeline key) #2132
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!2132
Loading…
Reference in a new issue
No description provided.
Delete branch "siantm/Longshot:perf/asm-draw-compare"
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
Every draw compares its state with the last one's to skip what did not change (
gpu/vk/draw_key.h): the dynamic state(
dyn_same), the vertex streams (vertex_set_same), the bound range (vertex_bind_range) and the pipeline key(
pipe_same, 32 arguments).This adds
gpu/vk/asm/draw.Son the asm kit:lsasm_same_bytes(memcmp's equality, no byte past n read),lsasm_same_f32(C's==on each float: -0 equals 0, a NaN nothing),lsasm_diff_mask64x4(a bit for each of n laneswhere any of four arrays differs) and
lsasm_vertex_set_same(vertex_set_same's rule over the 16 streams), AVX2 andSSE2 bodies. The four functions compare through them;
pipe_same's 32 arguments become one padding-free 280-bytePipeSame(astatic_assertonhas_unique_object_representations) compared in one call, andvk_draw.cpp'sPipeFastkeeps one. The functions still read nothing but their arguments. Main's own part:dyn_samealreadycompared the words and bytes with
memcmp(#391);lsasm_same_bytesstands for those andlsasm_same_f32for thefloat loop. The result expected: each compare answers as before, in fewer cycles a draw; the micro-benchmark (Measurements) finds that where the states match, and the C faster where it stopped at its first compare.
No switch:
draw_key.h's functions read no switch by design, and each answers as before.From 079737ce 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.Read first:
gpu/vk/asm/draw.S, thendyn_same,vertex_set_same,vertex_bind_range,PipeSameandpipe_sameingpu/vk/draw_key.h, andPipeFastingpu/vk/vk_draw.cpp.Testing
Tested: Windows 11, i7-9700, RTX 4060, the builder's llvm-mingw clang 23 (
CC/CXX,RX_CPMset); head7243747fon #1947's head
cffa2f46(the asm kit, on main9dd41326). Through the PC's job queue, each run in a hidden console,tests/run_all.py --strict --exact --jobs 2 --timeout 1200: the change's own checks (light jobpr-tb-AsmCut5-light,log
t-AsmCut5-asm-draw):test_asm_draw.py,test_fallback_pipeline_state.py,test_same_draw.py,test_same_dyn.py,test_vertex_bind_range.pypass (5 of 6);test_draw_binds.pyfails here and on #1947's head @cffa2f46too (light job, logt-AsmCut5-base-kit-own).--changed cffa2f46(build jobpr-tb-AsmCut5-build, logt-AsmCut5-chg-asm-draw): 162 checks, 148 pass, 14 fail, each of which fails on #1947's head @cffa2f46too (a worktreeat
cffa2f46in the queue's jobs, logst-AsmCut5-base-kit-changedandt-AsmCut5-base-kit-moreandt-AsmCut5-base-kit-own):test_dev_packs.py,test_device_lost_exit.py,test_draw_binds.py,test_draw_stream_visibility.py,test_frame_overlap.py,test_fsr_uniform_ownership.py,test_linux_pressure.py,test_mesh_conv.py,test_pipeline_play_cache.py,test_prepare_cache_save.py,test_recorder_own_idle.py,test_sampler_admission.py,test_upscaler_rows.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-g2132, the Market, 18 runs), withno FATAL, device lost or Vulkan error in any run.
Still to test:
uncapped (
HOS_FPS_MAX=240), on #1947's head and on this PR, each warmed a minute, then at least three interleavedruns of each (AB AB AB), and one run of each with
HOS_PROFILE=1(python -I scripts/perf.py reporton its folder:the draw-recording thread's running share and its hottest renderer functions), expecting the same picture, no
FATAL, that share lower and fps and frame times of this PR at least the base's; a difference beyond the runs' spread
is a gain or a loss, one within it is no difference measured (by hand)
#1947's head and on this PR, warmed and interleaved, expecting the same picture and frame times within the runs'
spread or better on this PR (@Beanstalk linux+nvidia, linux+amd, windows+amd, windows+intel, steamdeck: Kokiri
Forest and Hyrule Field three minutes each, #1947's head against this PR, warmed and interleaved, frame times and
screenshots)
python3 -I -B tests/test_asm_draw.py --cxx g++and--cxx clang++, expecting PASS (by hand)Measurements
Verdict: a gain where a draw's state matches the last one's, the usual case: per call
dyn_same1.32× (SSE2)and 1.99× (AVX2),
pipe_same1.95× and 3.35×,vertex_set_same1.01-1.19× and 3.06×,vertex_bind_range1.21-1.44×;on the mixed inputs 1.28-2.83×, but for
vertex_set_samein the SSE2 build (0.94×). Slower where the C stopped at itsfirst compare:
dyn_samewith its first float differing (0.35-0.38×: 0.9 ns against 2.3-2.5 ns),vertex_set_samewith its first stream moved (0.10-0.16×: 1.2-1.4 ns against 7.2-13.9 ns), and
vertex_bind_rangewith all 16 moved(0.71-0.77×). A micro-benchmark on authored input.
In play (the Market, this head against its parent, the asm kit
cffa2f46, 8 runs each, below): no gain shown in frametime or in the renderer's totals. Those lean against this head by 1 to 5 %, but none of the leans is shown (p 0.09 or
more in a test that swaps runs between the sides). Of the fine profile's slices where the compares run, dynamic state
is 6.9 % faster (p = 0.0009): there the state matches the draw before in 84 % of draws, the case this PR is faster in.
The pipeline bind and the vertex and index binds do not differ (p 0.14 and 0.63).
Baseline: the C this PR replaces, as #1947's head
cffa2f46has it (the asm kit on main9dd41326, which this PR depends on), not main itself.The micro-benchmark:
dyn_same,vertex_set_same,vertex_bind_rangeandpipe_samecut fromgpu/vk/draw_key.hatcffa2f46against the same four cut from this PR'sdraw_key.h(withPipeSame), inline in one program; 256 authored pairs of a draw's state and the kept one, walked 8 times a repetition (2,048 calls). A script on this PC, not in this PR (asm_draw_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 call. Change: before ÷ after (above 1: this PR faster).cffa2f467243747fdyn_same, the same (13 floats, 35 words, 80 bytes), SSE2dyn_same, the same (13 floats, 35 words, 80 bytes), AVX2dyn_same, 1 pair in 5 differing at a random place, SSE2dyn_same, 1 pair in 5 differing at a random place, AVX2dyn_same, the first float differs, SSE2dyn_same, the first float differs, AVX2vertex_set_same, 3 streams kept, SSE2vertex_set_same, 3 streams kept, AVX2vertex_set_same, 8 streams kept, SSE2vertex_set_same, 8 streams kept, AVX2vertex_set_same, the first stream moved, SSE2vertex_set_same, the first stream moved, AVX2vertex_set_same, 1 in 5 a stream moved, SSE2vertex_set_same, 1 in 5 a stream moved, AVX2vertex_bind_range, none moved, SSE2vertex_bind_range, none moved, AVX2vertex_bind_range, 2 moved, SSE2vertex_bind_range, 2 moved, AVX2vertex_bind_range, all 16 moved, SSE2vertex_bind_range, all 16 moved, AVX2vertex_bind_range, 1 in 3 with 1-2 moved, SSE2vertex_bind_range, 1 in 3 with 1-2 moved, AVX2pipe_same, the same (280-byte key), SSE2pipe_same, the same (280-byte key), AVX2pipe_same, the program differs, SSE2pipe_same, the program differs, AVX2pipe_same, 1 in 5 differing at a random byte, SSE2pipe_same, 1 in 5 differing at a random byte, AVX2In play (held job
fb-g2132, 00:12 to 00:56 UTC on 2026-11-23): the Market, chosen because its draws are the mostof the scenes on disk (about 650-700 a frame in the frame line, 1,100-1,340 in the fine profile's count). Each run was
60 s. The two builds are this head
7243747f(rt-adc-7243747f) and its parent, the asm kitcffa2f46(
rt-kit-cffa2f46): a warm-up each, then kit and head interleaved (kit head head kit, four times), withRX_VK_PROF_FINE=1on both (the fine profile's counts and slices), every run loading at create, at the game's 2560x1440output on a 2560x1440 desktop at 60 Hz. No run was left out, and no load marker or strike file appeared. Reach: in the
head's warm-up the fine profile counted pipeline keys from the fast pair (each a
pipe_samethat matched, by this PR'skernel) in every 120-frame line, about half a draw. In the measured stretch it was 0.45 a draw, about 560 a frame.
How often each case of the micro-benchmark comes up in play (the fine profile's counts a draw, the same on both sides):
the pipeline key matches the last one (
pipe_sameequal: fast pair) in 45 % of draws and differs in 55 %; this PR isfaster in both (1.95-3.35× equal, 4.4-5.2× with the program differing). The dynamic state is the draw before's in 84 %
of draws (
dyn_sameequal, 1.32-1.99×) and worked out again in 16 %: the slow case (0.35×) is only the part of those16 % whose first float differs. Vertex streams are bound anew on 61 % of draws: some of those are
vertex_set_same'sslow case (the first stream moved, 0.10-0.16×), and the profile does not tell which part.
cffa2f46(C compares), 8 runs7243747f, 8 runsa frame's replay time (ms): submit / drawThis is this head against its parent: two builds, not one build's switch (this PR has none). The play profile the
back end is built with was recorded before this change, so functions this change edits can build without their profile
in the head's build (a compile with clang's profile warnings on showed that for #1994's file; not checked for this
PR's). A difference between the two builds can therefore come from that as well as from the kernels. What is shown in
play is the dynamic-state slice, faster with the kernel. Nothing in frame time, and no loss, is shown.
Risks
What could break: a compare that says "same" for a state that changed would draw with the last draw's state (wrong
blending, depth, vertices or pipeline); one that says "different" for the same state only costs time. The test runs
the four functions cut from
draw_key.hagainst main's C on random mostly-equal inputs and the edges (every length toa few hundred bytes, -0 and NaNs, the 16th stream, all 64 lanes), the kernels on buffers that end at a faulting page.
A revert is enough. Renderer files: the runtime is rebuilt; nothing changes for players' saves, options or caches.
Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named).main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
tests/that reads changed source is updated, not weakened.Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.Possibly related, for context:
@Beanstalk This makes every draw's state comparisons with a hand-written x86-64 kernel (gpu/vk), in every scene. 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. No switch: a revert gives back main's code. 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 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; frame time p99 equal to main's or better, within the run-to-run spread. 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 69755 above, for its picture point: please take each picture 5 s (300 frames at 60 fps) after the first frame whose picture is not a single uniform colour, with no input in between, and take the main-against-itself picture from the same point. A picture taken during the load's fade-in from black is not comparable: run 70269 on #2156 caught the fade in 3 of 4 starts.
@siantm only members of
BigBeanscan start the farm. One of them can approve this request by reacting 👍 to it or to this comment.@Beanstalk This replaces my request 69755 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 every draw's state comparisons with a hand-written x86-64 kernel (gpu/vk), in every scene. 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. No switch: a revert gives back main's code. 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 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; frame time p99 equal to main's or better, within the run-to-run spread. 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.