fix(vk): lift AMD Anti-Lag 2 Market FIFO 1% lows +11.4% on AMD GPUs by dropping mutex across driver calls and deferring PRESENT to vkQueuePresentKHR #2704
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
4 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!2704
Loading…
Reference in a new issue
No description provided.
Delete branch "Jer9/Longshot:fix/vk-antilag2-mutex-present"
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
What a player notices:
VK_AMD_anti_lag, recovering+11.4%Market FIFO 1% low FPS (26.49 -> 29.52 FPS,83%of theAL0 -> AL1deficit) and cutting Kokiri Auto FG host GPU resource wait by25%(0.88 -> 0.66 ms).44.62 -> 44.40 FPSMarket FIFO,44.32 -> 44.38 FPSMarket Immediate) and setups withoutVK_AMD_anti_lag(NVIDIA / Steam Deck /RX_VK_ANTILAG=0).Two bugs in the AMD Anti-Lag 2 integration caused pacing stalls and inaccurate
PRESENTtiming:antilag_input()ingpu/vk/vk_antilag.cpphelda.muacross the blockingvkAntiLagUpdateAMD(INPUT)driver call (vk_replay.cppdo_present), stallingdisplay_main(present_commitholdspresent_image_mu+dev.present_muwhile callingantilag_present()). The fix snapshots/allocates state undera.muand dropsa.mubefore callingvkAntiLagUpdateAMD, usingneed_off+in_flight+take_off()so disabling Anti-Lag 2 while a call is in flight emitsVK_ANTI_LAG_MODE_OFF_AMDonce after all in-flightMODE_ON_AMDcalls return.present_thread_on()is alwaystrue),present_outhand_offcalledantilag_present()beforepresenter_wait_copy(done)instead of immediately beforevkQueuePresentKHR. The fix snapshotsantilag_idviaantilag_frame()athand_off, carriesantilag_idonPresenter::Job, and callsantilag_present()inpresenter_loopright beforevkQueuePresentKHR(leavinggpu/vk/vk_antilag.huntouched).Files to read first:
gpu/vk/vk_antilag.cpp,gpu/vk/vk_replay.cpp, andtests/test_antilag.py.Testing
Tested:
26.10.1 (LLPC).market-entrance), walk straight forward5 s(0:LUP/5000) to the central fountain, and traverse for19.5 safter a12.5 ssettle underV-Sync On (FIFO)andV-Sync Auto (Immediate)withRX_VK_ANTILAG=1vsRX_VK_ANTILAG=0.kokiri-start) at1600x1000windowed120 Hzwith Built-in Auto FG (frame_generation=1,frame_generation_by=1) andRX_VK_ANTILAG=1vsRX_VK_ANTILAG=0.python -I -B tests/test_antilag.pyfailed onforge/main(86b3b9e4) for both bugs and passes--stricton62e78b22(clang++andg++, including concurrentwant=0+antilag_frame()/antilag_present()tests).python -I -B tests/run_all.py --strict tests/test_antilag.py tests/test_fps_stats.py tests/test_present_acquire_ahead.py tests/test_present_thread.py:4 passed, 0 failed, 0 skipped.python -I -B tests/run_all.py --changed forge/main:130 passed, 0 failed, 12 skipped(7cargonot on PATH, 4 Linux-onlyspirv-val/setenv, 1 Vulkan 1.3 offscreen).python -I -B scripts/check_guide_screenshots.py --review-sources:0issues on branch-touched files.Still to test:
mirrors=1, Windows & Linux Mesa RADV): run Market FIFO/Immediate + Auto FG withRX_VK_ANTILAG=1, expecting recovered 1% lows and zeroDEVICE_LOST(@Beanstalkafter push).VK_AMD_anti_lagabsent): short smoke run, expecting clean no-op parity (@Beanstalkafter push).Measurements
Verdict: Market FIFO
RX_VK_ANTILAG=11% low FPS improved +11.4% (26.49 -> 29.52 FPS, recovering83%of theAL0 -> AL1deficit) and Kokiri Auto FG host GPU resource wait dropped -25% (0.88 -> 0.66 ms, matchingAL00.69 ms), with0DEVICE_LOSTand0without-inputsfallbacks across 27 serialized runs.Setup: Windows 11, AMD Radeon 860M (
26.10.1 LLPC), windowed120 Hz(4804x1753Market /3600x1753Kokiri DPI-scaled swapchains),nvn: window crop 0,0 2304x1440(100% render scale,output_res=2560x1440,ultrawide=1,handheld_profile=0),RX_VK_PRECOMPILE=0, 3-round interleaved medians (RX_VK_ANTILAG=1),presenter thread: the copy's fence2.95–8.91 msin Kokiri logs. (Benchmark runs executed onad617253;62e78b22only adds theneed_off/in_flightOff-transition guard undera.muwith identical steady-stateONhot path.)Baseline:
main @ 86b3b9e4withRX_VK_ANTILAG=0(30.14 FPS1% low Market FIFO,25.34 FPS1% low Market Immediate,0.69 msgwaitKokiri Auto FG) shows the target pacing ceiling without Anti-Lag 2.Where it makes a difference:
86b3b9e462e78b22100% render scale1% low FPS (RX_VK_ANTILAG=1, FG Off)gwait(RX_VK_ANTILAG=1, host GPU resource wait)Where it does NOT make a difference (within run-to-run variance):
86b3b9e462e78b22100% render scaleMean FPS (RX_VK_ANTILAG=1, FG Off)100% render scaleMean FPS (RX_VK_ANTILAG=1, FG Off)100% render scale1% low FPS (RX_VK_ANTILAG=1, FG Off)1600x1000Auto FG Mean FPS (RX_VK_ANTILAG=1)1600x1000Auto FG 1% low FPS (RX_VK_ANTILAG=1)Risks
VK_AMD_anti_lagvkAntiLagUpdateAMDhas noexternsyncrequirement invk.xml, andin_flight+need_offguaranteesMODE_OFF_AMDfires once after all in-flightMODE_ON_AMDcalls return.RX_VK_ANTILAG=0or the menu toggle; reverting the PR is a clean 2-file revert. Nothing changes in saves,longshot_options.ini, or shader caches.Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (python -I -B tests/run_all.py:744 passed, 14 failed, 102 skipped; all 14 are pre-existing Windows-env failures — 13 shared across cleanmain+ 16-waytest_sampler_admission.pyflake).main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
mainand passes here.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.Tested in game: Windows 11 Radeon 860M (26.10.1 LLPC): 27 serialized 3-round interleaved runs in Market FIFO/Immediate and Kokiri Auto FG; Market FIFO 1% low +11.4%, Kokiri gwait -25%, 0 DEVICE_LOST
@Beanstalk Please run Market FIFO/Immediate + Auto FG (
RX_VK_ANTILAG=1) on discrete AMD RDNA 3/4 (Windows and Linux Mesa RADV), plus a short smoke check on NVIDIA RTX and Steam Deck (VK_AMD_anti_lagabsent no-op parity).@Jer9 only members of
BigBeanscan start the farm. One of them can approve this request by reacting 👍 to it or to this comment.@review
@beanstalk
Beanstalk test farm · an earlier run: commit
62e78b2(the newer results are in a comment below)Commit
62e78b2· approved by HumanBean (reviewed) · dashboard⚠️ Note: Linux RADV and Steam Deck present-path tests are omitted because window scenarios currently run only on Windows; the Windows AMD and NVIDIA targets still exercise the requested paths.
⚠️ Note: The farm cannot reproduce the specified window sizes or refresh rate, measure the reported host GPU resource wait, or verify the exact timing and ordering of driver calls.
⚠️ Note: The Market measurements are stationary after the initial walk rather than the reported traversal, and the live toggle cannot guarantee an in-flight call; the plan also does not compare Anti-Lag on against off.
Started at 00:55 UTC.
Estimate: done in about 1 h 15 min, from the queue and the testers online now; some jobs can't start yet.
windows+amd+windowwindows+amd+igpu+windowwindows+nvidia+window+gpu-rtx-*(GPU "rtx *")windows+intel+windowCouldn't be judged: the tester's PC could not finish the test. The farm tries another PC, or the same one after a fix; it says nothing about the PR on its own.
windows+amd+window, report 1:main's longshot-builder failed (exit 1): could not compile 'proc-macro2' (build script) due to 1 previous errorWhat was tested
What will be tested · AI plan (
gpt-6.1-terra)The plan compares the PR with main in a real Windows window, using three starts per side on AMD, NVIDIA and Intel targets. It measures Market frame times with V-Sync On and Auto, then Kokiri frame times with built-in Auto frame generation, with Anti-Lag 2 enabled and a live off/on toggle. Each measurement lasts 20 seconds after warm-up; the run takes about 40 minutes per tester. A crash, device-lost log, or frame-time regression over 15% fails the plan. The plan does not directly require the reported 11.4% improvement.
market-entrance(settings vsync=1, amd_anti-lag_2=1, frame_generation=0; switchesRX_VK_ANTILAG=1; in a hidden window (the present path runs)), then move the stick, measure market-fifo for 20 s (after 5 s warm-up), set vsync to 0, measure market-auto for 20 s (after 10 s warm-up), set amd_anti-lag_2 to 0, set amd_anti-lag_2 to 1, teleport to Kokiri Forest, set frame_generation_by to 1, set frame_generation to 1, measure kokiri-auto-fg for 20 s (after 10 s warm-up); 3 runs on each side.Measurements on `windows+amd+window`, for the PR description
Verdict: queued on windows+amd+window (1 report).
Hardware: 1: windows+amd+amd-proprietary+dgpu+xess
AMD Radeon RX 6700 XT, 2.0.353AMD Ryzen 7 2700X Eight-Core Processor, 16 GB RAMScene: market-and-kokiri, from the repository's save market-entrance (vsync=1, amd_anti-lag_2=1, frame_generation=0, RX_VK_ANTILAG=1); each cell's settings and switches as the table names them
Method: the Beanstalk test farm: main and this PR built and run on the same PC in one session, game starts alternating (ABBA); market-and-kokiri: 3 game starts a side, market-fifo 20 s after 5 s warm-up, market-auto 20 s after 10 s warm-up, kokiri-auto-fg 20 s after 10 s warm-up; each number the median of the runs, then of 1 report
How to reproduce: the farm's plan above (
@Beanstalkon this PR runs it again)Nothing was measured: no metric came back.
Flags and settings
None: the plan varied no setting, switch or build option.
As Markdown for the PR description:
The testers' hardware
windows+amd+windowAMD Radeon RX 6700 XT, 2.0.353AMD Ryzen 7 2700X Eight-Core Processor, 16 GB RAMRecommendation: Request changes. The new presenter-thread call reads the menu setting without synchronization, and an Off call can overtake a later On call. The reported performance gain is promising, but it was measured on an earlier commit and the affected Tier 1 systems remain untested.
Moves the Anti-Lag present marker closer to presentation and lets driver calls overlap, reducing the reported Market FIFO stutter on one AMD iGPU. The added concurrency needs safe setting access and ordered mode transitions.
Blocking (1): must change before merging.
gpu/vk/vk_replay.cpp:6981: The new presenter-thread call reads the menu's plain integer setting without the lock used by its writer. (more on the line) Do: Publish the selected Anti-Lag value through an atomic or synchronize all its readers and writers with the same lock, and cover concurrent menu changes in the test.Should fix (2): not blocking, but worth doing before merging.
gpu/vk/vk_antilag.cpp:74: Claiming an Off transition does not prevent a later On call from overtaking it. (more on the line) Do: Order Off against subsequent On calls as well as earlier in-flight calls, without holding the state mutex across a blocking INPUT. Add a test that pauses the claimed Off while another thread turns the setting back on.Testing before merging (2) (runs on the systems it can change; it doesn't hold the recommendation)
gpu/vk/vk_antilag.cpp:182: The final concurrency implementation and its effect on pacing and latency need runs on affected AMD systems. (more on the line) Do: Run interleaved base and final-head Market FIFO and Immediate and Auto FG comparisons on the 860M and representative discrete AMD Windows and RADV systems; report FPS, 1% lows, frame times and latency or marker timing, including run spreads.gpu/vk/vk_replay.cpp:6800: The changed presenter path still needs the requested Tier 1 no-extension smoke runs. (more on the line) Do: Smoke-test presentation and frame generation on NVIDIA and Steam Deck and compare pacing with the base. (@fixdoes its part first: Skip the state-locking snapshot when the extension is unavailable.)@fixcan do L1, L2, L3 and L5.Checks: Still running: 10 checks.
More: does it help, other systems, switches, what was read
Does it help: Unproven on the final head. The PR reports three-round interleaved medians on Windows Radeon 860M: Market FIFO 1% low rose from 26.49 to 29.52 FPS and Kokiri Auto FG GPU wait fell from 0.88 to 0.66 ms, but those runs used ad617253, before the final concurrency guard. Kokiri 1% low fell from 14.18 to 13.31 FPS within the reported spread.
Other systems: The presenter path runs on all GPUs; systems without the extension still take the Anti-Lag state lock. AMD driver calls and marker timing also affect frame generation and both Windows and Linux. Discrete AMD, NVIDIA and Steam Deck runs are still requested and no results are supplied.
Switches: None added, changed or removed. The existing RX_VK_ANTILAG overrides the menu setting, which defaults to off.
Reviewed:
62e78b2261against86b3b9e4f0: Read AGENTS.md, CONTRIBUTING.md, the PR template, standards, the full Anti-Lag implementation and test, and the changed presenter paths and their callers. Reviewed the full diffs for all six files and the relevant menu and frame-generation paths; skimmed the rest of the large replay and test files. docs/review-checklist.md and the adjacent Tooling checkout were absent.; nothing run (a code review).Checked and fine:
Testing: Read-only review; no code, scripts, builds or tests were run. The author reports game-free checks and Windows 860M runs; CI is still running.
Overlaps and merge order: Not checked; network closed.
Automated code review by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.
@ -69,1 +71,4 @@/// With a.mu held: claims the single Off driver call once no Mode-On stage call is in flightbool take_off() {AntiLag& a = al();L2 (should fix): Claiming an Off transition does not prevent a later On call from overtaking it.
When: One thread claims Off with in_flight zero and releases a.mu before update(false); another observes the row turned on and issues an INPUT with MODE_ON. The first thread can then issue or finish MODE_OFF after that On, leaving the driver off despite the current on setting until a later On update.
Do: Order Off against subsequent On calls as well as earlier in-flight calls, without holding the state mutex across a blocking INPUT. Add a test that pauses the claimed Off while another thread turns the setting back on.
@ -136,0 +179,4 @@return;}if (!f) return;stage(VK_ANTI_LAG_STAGE_INPUT_AMD, f); // (the driver's wait, when it wants one; a.mu dropped so presents never wait on it)L4 (should fix): The final concurrency implementation and its effect on pacing and latency need runs on affected AMD systems.
When: The reported runs precede the final guard, and moving PRESENT after the copy fence changes its timing relative to INPUT and presentation. The supplied results do not establish the final head's gain or latency on discrete AMD Windows and Linux drivers.
Do: Run interleaved base and final-head Market FIFO and Immediate and Auto FG comparisons on the 860M and representative discrete AMD Windows and RADV systems; report FPS, 1% lows, frame times and latency or marker timing, including run spreads.
@ -6798,2 +6798,3 @@if (reflex_id) reflex_present_handed_off();antilag_present(); // (AMD Anti-Lag 2: the frame's present stage)// (AMD Anti-Lag 2: snapshot the frame index now before the next input stage advances it; presenter_loop fires PRESENT right before vkQueuePresentKHR)const uint64_t antilag_id = antilag_present_frame == kAntiLagThisFrame ? antilag_frame() : antilag_present_frame;L5 (should fix): The changed presenter path still needs the requested Tier 1 no-extension smoke runs.
When: NVIDIA and Steam Deck also execute the new hand-off snapshot, but no game run on either system is reported.
Do: Smoke-test presentation and frame generation on NVIDIA and Steam Deck and compare pacing with the base.
@ -6975,1 +6978,4 @@pi.pImageIndices = &index;if (antilag_id) {antilag_present_frame = antilag_id;antilag_present();L1 (blocking): The new presenter-thread call reads the menu's plain integer setting without the lock used by its writer.
When: With the extension available and no RX_VK_ANTILAG override, the presenter calls antilag_present(), which reads opts.antilag through wanted(), while menu_tick can write that same value under menu_mu. The Anti-Lag mutex does not protect the menu write, so changing the row while a present is in flight is a C++ data race.
Do: Publish the selected Anti-Lag value through an atomic or synchronize all its readers and writers with the same lock, and cover concurrent menu changes in the test.
Beanstalk test farm
Commit
0a06873· approved by nobody yet · dashboardA member of
BigBeanscan review this commit and comment@Beanstalkto test it.Addressed BeanBot review items L1, L2, and L5 in commit
0a068733:live_antilagstd::atomic<int>(menu_antilag_changed(int)/menu_antilag()) synced inmenu_init(),menu_tick(), and.on_changeonamd_anti-lag_2, read viamenu_antilag()inwanted(). Added static wiring checks and a multi-threaded concurrent toggle stress test (Concurrency 7) intests/test_antilag.py.off_in_flight+send_off()ingpu/vk/vk_antilag.cppsoMODE_OFF_AMDandMODE_ON_AMDare mutually excluded in both directions without holdinga.muacross blockingINPUTcalls. Added paused-Off+ concurrentwant=1test (Concurrency 6) intests/test_antilag.py.AntiLag::readystd::atomic<bool>with lock-free!readyearly returns inantilag_frame()/antilag_present()/antilag_input()plus!antilag_available()guard athand_off(gpu/vk/vk_replay.cpp:6800), verified with a mutex-held runtime test.Checks:
tests/test_antilag.py --strictpasses on bothclang++andg++;tests/run_all.py --changed forge/mainpasses (211/211non-skipped);longshot_vk.dllrebuilt cleanly.@review
Recommendation: Approve. The atomic setting and Off/On ordering fixes address the code issues, and the unavailable-extension path avoids the added lock. The final implementation still needs the requested AMD pacing and latency runs and Tier 1 no-extension smoke runs. Nothing blocking is left: what remains is for the author before merging (below). Recommend approval; not approved on the forge.
The change moves Anti-Lag driver calls outside the state mutex and sends the present marker closer to presentation. It adds concurrency state and atomic setting publication to preserve ordering while reducing stalls.
Should fix (1): not blocking, but worth doing before merging.
Testing before merging (2) (runs on the systems it can change; it doesn't hold the recommendation)
gpu/vk/vk_antilag.cpp:182: The final concurrency implementation and its effect on pacing and latency need runs on affected AMD systems. When: The reported runs precede the final guard, and moving PRESENT after the copy fence changes its timing relative to INPUT and presentation. The supplied results do not establish the final head's gain or latency on discrete AMD Windows and Linux drivers. Do: Run interleaved base and final-head Market FIFO and Immediate and Auto FG comparisons on the 860M and representative discrete AMD Windows and RADV systems; report FPS, 1% lows, frame times and latency or marker timing, including run spreads.gpu/vk/vk_replay.cpp:6800: The changed presenter path still needs the requested Tier 1 no-extension smoke runs. When: NVIDIA and Steam Deck also execute the new hand-off snapshot, but no game run on either system is reported. Do: Smoke-test presentation and frame generation on NVIDIA and Steam Deck and compare pacing with the base. (@fixdoes its part first: Skip the state-locking snapshot when the extension is unavailable.)@fixcan do L3 and L5.Since the last review (on
62e78b2261)Resolved: L1 (The new presenter-thread call reads the menu's plain integer setting without the lock used…); L2 (Claiming an Off transition does not prevent a later On call from overtaking it.)
Still open: L3 (The standalone test command in Testing omits a required argument.); L4 (The final concurrency implementation and its effect on pacing and latency need runs on aff…); L5 (The changed presenter path still needs the requested Tier 1 no-extension smoke runs.)
Checks: Still running: 10 checks.
More: does it help, other systems, switches, what was read
Does it help: Unproven for the final implementation. On the earlier benchmarked commit, the author reports Market FIFO 1% lows rising from 26.49 to 29.52 FPS and Kokiri Auto FG host GPU resource wait falling from 0.88 to 0.66 ms on a Windows Radeon 860M; the final concurrency changes have not been measured.
Other systems: AMD Windows and Linux drivers can see different timing and concurrent driver calls; the requested discrete AMD runs remain outstanding. NVIDIA and Steam Deck lack the extension and now skip the state lock, but their requested smoke runs remain outstanding.
Switches: No switches added, changed or removed. RX_VK_ANTILAG continues to override the menu setting.
Reviewed:
0a068733cfagainst86b3b9e4f0: Read AGENTS.md, CONTRIBUTING.md, the pull request template, gpu/vk/vk_antilag.cpp and the changes since the ledger head; inspected the relevant menu and presenter diffs and test changes. The remaining changed files and surrounding code were not read in full; recorded runs were read from the pull request and comments.; nothing run (a code review).Checked and fine:
Testing: No commands from the project were run. The author reports the Anti-Lag test passing on clang++ and g++, changed tests passing, and a Windows renderer rebuild; CI is still running.
Overlaps and merge order: Not checked; network access is closed.
Automated code review by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.