WIP: perf(vk): three per-draw cuts of the replay thread, measured together, each behind its own switch #3328
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/mods
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!3328
Loading…
Reference in a new issue
No description provided.
Delete branch "klimovich008/Longshot:perf/replay-stack"
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
Three small cuts of the replay's CPU time per draw. Each one is about 1 % of a frame or less. Alone, that is below what
the in-game A/B can see: std_test asks for 6 pairs under a 5 % difference, and one cut's share is lost in the noise. So
they are measured together here. Each keeps its own switch, on by default; each
=0restoresmain's path, and thereplay bench shows the three of them at
=0recording the same commands asmainat the same speed.RX_VK_PUSH_WORDS_BULK(gpu/vk/draw_key.hwords_store_all,gpu/vk/vk_draw.cpppushed_words_store).bind_descriptors_leanremembers a full descriptor push as 64-bit words, so the next draws of the program push onlythe writes that changed.
words_appendgrew that vector one out-of-linepush_backper word: ~19,000 calls a frameat Market center (~720 full pushes of ~27 words).
words_store_allcounts a push's words, sizes the words and thewrite offsets once and fills them; a buffer's three words are its
VkDescriptorBufferInfo, copied whole(static_asserted). The words are the same, bit for bit.
=checkbuilds both and compares them.RX_VK_SPAN_HEAT_PLAIN(gpu/vk/ubo_ssbo_fast.hadd_read_span_heat). A hit in the per-thread read spans(
fast_read_bind's uniform and storage buffer lookups) added its pool's heat with a lockedfetch_add. The lockprefix waits for the replay thread's earlier stores to drain. It is now a relaxed load and store. The heat is the
mirrors' estimate of how much a pool is read (
mirrors_by_heat), decayed by a quarter every 16 submissions. An addmade at the same moment on another thread may now be lost, which that estimate absorbs.
RX_VK_RECORDER_RETIRED(gpu/vk/vk_recorder.hrec::Retired,rec::ring_mark,rec::retired_on;gpu/vk/vk_texture.cpppool_put,pool_take_memory;gpu/vk/vk_replay.cppdo_present).pool_take_memorybinds a new render target to a pooled image's memory, then destroys the pooled image and itsviews. Every
vkDestroy*is a recorder boundary, so the render thread waited for the recorder to drain the ring.At Market center that is ~430–660 waits and 95–140 ms per 10 s.
pool_putnow keeps the ring's position (
rec::ring_mark).rec::Retired(mark), a scope around those two destroys only,lets them go to the driver without the wait once the recorder has run past that position.
do_presentkeeps its source image (the headless blit, the window's own) to the present's value and past whattexture_frame's sweep reads as done. The sweep then cannot pool it, and give its memory to a new target, whilethe present still reads it.
Read first:
words_store_allandpushed_words_store;add_read_span_heat;rec::Retired/ring_mark/retired_on, then their two callers invk_texture.cppand thedo_presenthunk.Testing
Tested: Windows 11, AMD Radeon RX 9070 XT (driver 32.0.32029.2010), Ryzen 7 5700X3D. In the game at Market center
(the fountain save), the camera turning, Threaded rendering on (the render and recorder threads). Two A/Bs of three
interleaved pairs each, the stack at its defaults against all three switches
=0, in one runtime (Measurements).Every counted run loaded the save and played its measured window at the game's own speed (0.999–1.001). Then the same spot with Khronos validation and synchronization validation, the stack against its
control: on a runtime built from this PR's head (
b8238c35d), each run playing 90 s after the load. Both runs show the samethree error kinds and no synchronization hazard:
b8238c35d=0: this PR @b8238c35dVUID-VkImageCreateInfo-imageCreateMaxMipLevels-02251VUID-vkGetQueryPoolResults-None-09401VUID-RuntimeSpirv-OpTypeCooperativeMatrixKHR-10163SYNC-HAZARD-*Every
02251error is a BC5 or BC6H texture created with storage usage, from the texture upload path. By format, thecounts differ in both directions (BC5 SNORM 2 against 5, BC5 UNORM 492 against 488, BC6H 9 and 9), so the one extra
follows from which textures each run streamed in. None of the three changes creates an image or picks a format.
Checks (
claude-perf/with_lock.sh, light):tests/test_push_words_bulk.py(new) runswords_store_allwithvk_draw.cpp's ownPushed,push_words_bulk_checkandpushed_words_store(srccut) against thewords_appendreference:zero-count writes with pointers set, counts 0–40, up to 80 writes;
=checkflipped between pushes; the same modes over a deliberately brokenwords_store_all(
=0and=checkmust keepwords_append's words,=checkmust count each difference);push_words_bulk_begin_recordingwith and without a HOS_AB host;tests/test_span_heat_plain.py(new) compiles the realubo_ssbo_fast.h: one random trace through a table withthe plain add and one with the locked add must leave every pool the same heat, equal to the heat counted beside
them. On x86-64 the plain lookup's assembly must hold no lock-prefixed instruction and the locked one's must. Five
broken variants must fail.
tests/test_recorder_parts.py(retired cases,tests/recorder_parts_test.cpp):=0, destroys outside the scope and a buffer's inside it;Retiredwith no producer (the recorder off) skips nothing;pool_put's mark,retired_on, anddo_present's source keptbefore the sweep, with broken variants and controls for each;
mode_initstores the switch, then publishes the mode once with release, and thatmode()returns what it read with acquire. 8 broken variants must fail: a relaxed publish, a relaxed read, a relaxed losing
mode_init, the switch stored after the publish, a relaxed store over it, a second publish,mode()reading againrelaxed, and a relaxed store of the mode elsewhere.
tests/test_hos_ab.py:RX_VK_PUSH_WORDS_BULKandRX_VK_SPAN_HEAT_PLAINare WIRED (read at each recording'sstart).
RX_VK_RECORDER_RETIREDis read once, at the recorder'smode_init, likeRX_VK_RECORDER_PARTS, so it isnot: the test requires WIRED to be exactly the
ab_valuereaders.draw_stream_visibility_test.cpp,fsr_uniform_ownership_test.cpp,recording_admission_test.cpp,test_target_reuse_memory.py,test_upscale_independent.py,test_vk_draw_overhead_lean.py.tests/run_all.py: on this branch on6c06d9810, 876 passed, 14 failed, 68 skipped (since thenmainadded two website commits only). The same 14 fail onmain6c06d9810:test_astc_decoder_gpu.py,test_device_lost_exit.py,test_fps_stats.py,test_helpers_extract.py,test_longshot_builder.py,test_lost_wait_said.py,test_parity_gates.py,test_present_acquire_ahead.py,test_present_device_loss.py,test_present_probe.py,test_present_probe_retirement.py,test_present_thread.py,test_queue_priority.py,test_submit_oom.py.mode_initcouldstore
g_retiredat once, and a thread could read it before the store reached it.b8238c35dmakesg_retiredanatomic, false until
mode_initstores the switch. A review of that commit found the rest: the mode was publishedrelaxed, so
do_presentcould see the recorder thread's mode and still read the switch's initial false, not keepits source, and a later
Retiredcould let that image's destroys go while the present's blit was in the ring.3e27a4e24publishes the mode with release and reads it inmode()with acquire. A review of that commit found itssource check too loose: it passed with the switch stored after the publish, or a relaxed store over it.
45b99719echecks the order, the one publish and
mode()'s whole body. That review also found that two threads inmode_initat once race on its plain settings (g_own,g_idle_waitand others). That ismain's own; thisPR's
g_retiredis an atomic and widens nothing.main's own, and this PR leaves them as they were or narrower: a frame generation pass reading thepresent's source after the present's value is done, and the headless output effects' use of a
Texturethatretire_finishmay erase in the same sweep. Onmaintoo the destroys wait only for the recorder, not for the GPU.Here the source's
last_valueis kept later, and the sweep reads an earlier value.RX_VK_RECORDER_RETIRED. It is read once, at the recorder's start.stack at its defaults, and with every switch
=0, records the same commands and picture asmainin every scene.Still to test:
RX_VK_PUSH_WORDS_BULK=0 RX_VK_SPAN_HEAT_PLAIN=0 RX_VK_RECORDER_RETIRED=0. Expecting the same picture and no loss (@Beanstalk).Measurements
Verdict: about 2 % more fps and 0.8 ms less CPU a frame at Market center, the three measured together. Of 5 valid pairs over two A/Bs, 4 favour the stack and one is flat. std_test labels it "diagnostic": a difference under 5 % needs 6 pairs.
Setup: Windows 11, RX 9070 XT (driver 32.0.32029.2010), Ryzen 7 5700X3D.
claude-perf/std_test.py: Market center,16:9 at 2048x1152 (graphics scale 1), uncapped, the camera turning, 20 s measured after the load, a GPU co-load
holding the clock. Threaded rendering on. One runtime (What was measured, below): After = defaults, Before = the
three switches
=0. Order ABBAAB, 3 pairs a run. CPU ms is the whole process's CPU time per frame.Baseline: this PR's own build with its three switches
=0, which restoresmain's paths, against the same build at its defaults, in one runtime. The switches turn off the three changes and nothing else, so the A/B holds everything else the same. Before ==0, After = defaults; both columns are 9d282c355,mainf359e2621with this PR's first four commits (What was measured).What was measured. The in-game A/Bs and the recorder-wait counts used one runtime:
mainf359e2621(2026-12-04) plusthis PR's first four commits, before the
g_retiredfix. That fix changes no generated code on x86-64: a relaxed atomicbool's load and store are plain moves. The replay bench used 554d1229b on
main2d87a5c9a. The branch is now rebased onto7a014436fwithout conflicts. The validation runs used a runtime built fromb8238c35d. The two commits since change only the memory order of the recorder's mode publish and read (3e27a4e24: the same x86-64 instructions) and a test (45b99719e).main's commits sincef359e2621don't touch the target pool, do_present's sweep, the descriptor push or the span table. In the recorder they added
vmaDestroyPoolas a boundary call. #3159 made Threaded rendering's Auto run the recorder thread at any core count,which widens where
RX_VK_RECORDER_RETIREDapplies.A/B 1,
stack1205a(2026-12-05, 22:10–22:18 UTC). GPU 3,246–3,279 MHz in every run.=0, fps: this PR @ 9d282c355=0, CPU ms: this PR @ 9d282c355A/B 2,
stack1205c(2026-12-06, 07:35–07:42 UTC). GPU 3,253–3,281 MHz in every run.=0, fps: this PR @ 9d282c355=0, CPU ms: this PR @ 9d282c355Pair 1 was rejected by std_test's own rule, not by choice. Its stack run had a 0.55 s frame inside the measured window, 15.7 s
after the load. The frame's present returned at once, so the time went to the game's own work. That run alone also had two
long frames before the load (332 and 278 ms); no other run of either arm did. std_test runs exactly 3 pairs and replaces none.
Void:
stack1205b(2026-12-06, 06:44–06:53 UTC), not counted. The GPU clock was 2,908–3,162 MHz against ~3,270 in thetwo A/Bs above. Windows Update's servicing and Defender used about 1.4 cores throughout, and one minute of outside disk load
(06:47–06:48 UTC) hit pair 0. Its medians were 75.54 fps (stack) against 74.09 (off).
The 5 valid pairs together:
=0: this PR @ 9d282c355Four of the five pairs favour the stack. One is flat:
stack1205cpair 0, −0.1 %. std_test labels both A/Bs"diagnostic": a difference under 5 % needs 6 pairs for its admission, and each A/B has 3.
RX_VK_RECORDER_RETIREDin the same runs. The recorder's stats line counts how often the render thread waited for it.These are the last two 10 s lines of each run, the measured window:
Per change, on the replay bench (Longshot #3268's game-free bench: synthetic scenes through the real replay, 7 runs a side, fastest frames;
the Vulkan census compares what each side records). Each row: the stack's DLL at its defaults against that one switch
=0, so each row is what turning that change off costs:RX_VK_PUSH_WORDS_BULK=0RX_VK_PUSH_WORDS_BULK=0RX_VK_SPAN_HEAT_PLAIN=0RX_VK_SPAN_HEAT_PLAIN=0RX_VK_RECORDER_RETIRED=0RX_VK_RECORDER_RETIRED's bench row is within the bench's noise. Its waits in the game come from new render targetstaking pooled targets' memory by size, a path this bench does not measure. Its measure is the recorder's own count in
the game, above: 408–662 waits per 10 s off, 0–87 on.
main2d87a5c9a, same bench, fastest frames:=0againstmain: draws −0.2 %, mixed +0.2 %, dispatches +5.5 %.=0the build runsmain's paths. So neither dispatchesnumber is a change's cost: it is the difference between the two DLL builds in that scene, larger at
=0than atthe defaults.
main's in every scene, at the defaults and at=0.Risks
RX_VK_PUSH_WORDS_BULK: a wrong word makes the next draw of the program skip a write that changed, or push onethat did not. The randomized check and
=checkcompare it withwords_appendbit for bit.RX_VK_SPAN_HEAT_PLAIN: a lost heat add moves the mirrors' estimate slightly, never a binding.RX_VK_RECORDER_RETIRED: a destroy let go too early would free an image the recorder has yet to name. The mark istaken when the image is pooled, after every packet naming it was written. The scope covers those two destroys only,
and
do_presentkeeps the present's source out of the sweep's pool until the present is done.=0turns its change off:main's path. Nothing changes for a player after updating: no files, novk_api.hchange, no shader-cache stamp file (vk_shader.cpp,vk_bank.cpp,vk_state.h).What each change leaves out:
RX_VK_PUSH_WORDS_BULK: onlybind_descriptors_lean's full push. The delta pushes (words_same_store_lean) and thelegacy
bind_descriptors(RX_VK_DRAW_OVERHEAD_LEAN=0) are unchanged.RX_VK_SPAN_HEAT_PLAIN: only a hit in the per-thread span table (ReadSpanTable::lookup's two ways). Every otherheat add keeps its locked add.
RX_VK_RECORDER_RETIRED: onlypool_take_memory's destroys of the pooled image and its views. Every other destroywaits as before. It does nothing without the recorder thread (Threaded rendering off,
RX_VK_RECORDERnotthread,or a broken ring).
do_present's change applies only while it is active.Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named).main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
gpu/vk/switches.def.tests/that reads changed source is updated, not weakened.Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.🤖 Generated with Claude Code
Tested in game: Windows 11, AMD Radeon RX 9070 XT (driver 32.0.32029.2010), Ryzen 7 5700X3D: Market center (the fountain save), camera turning, Threaded rendering on. Two std_test A/Bs of 3 interleaved pairs, the stack at its defaults against RX_VK_PUSH_WORDS_BULK=0 RX_VK_SPAN_HEAT_PLAIN=0 RX_VK_RECORDER_RETIRED=0 in one runtime: 5 valid pairs (one rejected by std_test), medians 95.99 vs 94.24 fps (+1.9 %) and 54.87 vs 55.67 CPU ms a frame; 4 pairs for the stack, one flat (-0.1 %); std_test: diagnostic. The render thread's waits for the recorder: 0-87 per 10 s with the stack, 408-662 without. Every counted run played its window at the game's speed. Validation (Khronos + sync) on the PR head's runtime: pending.
Possibly related, for context:
@Beanstalk measure this PR's three switches together on Windows with NVIDIA and on the Steam Deck. They are CPU cuts on the replay's render and recorder threads, so the question is whether they also help, or at least don't hurt, off this author's AMD PC.
RX_VK_PUSH_WORDS_BULK=0 RX_VK_SPAN_HEAT_PLAIN=0 RX_VK_RECORDER_RETIRED=0, all three set together on one side (main's paths). A[[pass.compare]]on average frame time, 3 runs a side, interleaved.frame_rate_cap = 6,vsync = 2,frame_generation = 0, windowed.run_hitch_countandrun_frametime_max_msreport-only.Beanstalk test farm
Commit
45b9971· approved by nobody yet · dashboardA member of
BigBeanscan review this commit and comment@Beanstalkto test it.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.