fix(vk): the swapchain's image is waited for on the CPU, so a window behind another one no longer stops the GPU's work #1458
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!1458
Loading…
Reference in a new issue
No description provided.
Delete branch "HumanBean/Longshot:fix/present-acquire-fence"
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?
Type: fix · Risk: medium · Tested on: Linux, RTX 3090 (no regression; the bug itself is AMD on Windows) · Needs: Windows, AMD RDNA 4 / RDNA 3 / Radeon 8060S (Test Farm)
Why
On AMD's Windows driver the game ends with "the GPU hung: no submission finished in 30 s, and the driver never reported the device lost" a few seconds after the fullscreen game window goes behind another window: Alt+Tab to a browser (#1376, #316), OBS in front while streaming (#1355), or the game started from the launcher with another window in front (#1391, #1429). No TDR, the driver does not restart: the GPU is not running anything, its queue is waiting.
The cause is in
present_image: the copy into the swapchain image waited on the GPU for the acquire's semaphore, on the same queue every submission of the game's work goes to. AMD's Windows driver returns fromvkAcquireNextImageKHRat once and leaves the whole wait to that semaphore. While the window is hidden the system gives the image back late or never, so every later submission of the game waits behind the copy, and the hang watch ends the game 30 s later. The watch's own comment already named the case ("A window the system stops presenting can hold the queue that long as well"). NVIDIA's driver waits inside the acquire on the CPU instead, which is why only AMD hits it.Fixes #1376
Fixes #316
See #1355, #1391, #1429 (same signature: AMD Windows driver, fullscreen, window behind another one, timeline stopped a few submissions short, empty device fault)
How to reproduce
Seen on: Windows 11, AMD Radeon RX 9070 XT, AMD driver 26.10.1 / 26.11.1 (LLPC) (reporters); RX 7600 XT and Radeon 8060S at startup (#1391, #1429)
Settings and switches: Fullscreen on, V-Sync Auto (immediate), frame generation 2x-4x in the logs; #1355 reports it with frame generation and Anti-Lag 2 off too
Steps: play (any area), Alt+Tab to a browser so it covers the game, wait 30 s
Expected: the game keeps running behind the browser and shows again when you come back
Actual (main): 1 to 20 s after Alt+Tab
vk: a capture's wait for write-back ... timed out, thenthe GPU has finished nothing for 10 s, then at 30 sFATAL: the GPU hungFrom #1376's log, after
hid: game window not in front (foreground window: ... "Chrome_WidgetWin_1"), 15 s of normal 60 fps, then:What changed
The swapchain image is now waited for on the CPU, never on the replay's queue. No submission waits for the presentation engine any more, so a hidden window can no longer stop the game's GPU work.
gpu/vk/vk_replay.cpp,present_image: the acquire signals a fence (acquire_fence) instead of a semaphore, andimage_freewaits for it on the CPU (excluded from the GPU wait counts as pacing) before any work for the image is recorded. The copy'svkQueueSubmit2has no wait semaphore. An image not free within 250 ms is held: the frame is not shown, the game goes on, and each later present checks the same image again without waiting (no second acquire while one is held) and copies into it once it is back. It logsvk: the window's image was not given back in 250 ms (the window hidden?)and... was given back: frames are shown again(the first 16 times).held_image_drop,acquire_fence_ready); no acquire runs without an unsignaled fence.Frame::acquired(the per-slot acquire semaphores) is gone.present_slots_for. With this PR, that slot wait is also bounded by the GPU's own work: the first copy no longer waits for the presentation engine.Measurements
Verdict: nothing to measure for the fix itself on this PC; the bug does not occur here (NVIDIA waits on the CPU in its acquire). The game-free test shows the bug on main and its absence here. Frame pacing on AMD with the CPU-side wait is the open question for the Test Farm (below).
Game-free test,
tests/test_present_image_free.py(cuts present_image's acquire and its copy's submit info and runs them against a presentation engine that hands an image back before letting it go, as AMD's does), expected output (not run on this PC: see Testing):c14641a6Flags and settings
None.
Testing
Tested:
8e9cfc3awith this fix applied (the same diff as this branch before its rebase onto main; the rebase only resolved a neighbouringpresent_slots_foraddition). Played into the save's area, moved the game's workspace away for 40 s (window not shown), back for 15 s, away 5 s, back 15 s. Mailbox: 60 fps game / frame interpolation on time throughout, frames shown again after each return, no hang-watch lines, no held-image lines (sway kept giving images back). V-Sync On (FIFO): the same, except the game pauses while hidden and resumes on return; main at8e9cfc3apauses the same way (nvn: GPU waits in 5 s: ... (20128 ms)on main, 20101 ms with the fix), so that is the existing Wayland FIFO behaviour, not this change.present_image_mu, the test's cuts on main and here); its findings are folded in (250 ms rather than 100 ms for slow FIFO displays, the wait kept out of the GPU wait counts, no acquire without a fresh fence).Not tested:
tests/run_all.pyor any single test; a standing rule here). Forgejo CI runs them.test_present_semaphoresandtest_present_device_losswere updated to the acquire's new arguments.Must be tested:
FATAL: the GPU hung; the log may saythe window's image was not given back in 250 msthenwas given back; the picture comes back. (Test Farm)Risks
What could break: frame pacing where the acquire returns before the image is free (AMD Windows): the copy is submitted once the image is free rather than queued at once. A display slower than about 24 Hz in FIFO could see occasional held frames. On every GPU, a hidden window now drops frames instead of blocking (as Linux already did on Wayland).
Turning it off: revert the commit.
Compatibility: nothing changes for a player's saves, options or caches; no
MKWVK_API_VERSIONchange.Issues
Fixes #1376
Fixes #316
See #1355
See #1391
See #1429
Checklist
Everyone
type(scope): description) of the status line's type.main.python -I -B tests/run_all.pypasses (any failure that also fails onmainis named). (not run on this PC; Forgejo CI runs it)third_party/untouched (a new folder with aPROVENANCE.mdinstead).When it applies
Assisted-by:/Co-Authored-By:trailers.mainand passing here. (written and reasoned through; not run on this PC, CI runs it)tests/that reads changed source updated in this PR, not weakened.🤖 Generated with Claude Code
The copy into the swapchain image waited on the GPU for the acquire's semaphore, on the queue every submission of the game's work goes to. AMD's driver on Windows hands an image back from vkAcquireNextImageKHR at once and leaves the wait to that semaphore; with the fullscreen window behind a browser or OBS the system gave the image back late or never, the replay's queue finished nothing, and the GPU hang watch ended the game 30 s later ("no submission finished in 30 s, and the driver never reported the device lost"; RX 9070 XT, #1376, #316, #1355). Now the acquire signals a fence, waited for on the CPU before the copy is recorded: no submission waits for the presentation engine. An image not free in 250 ms is held, frames are not shown meanwhile and the game goes on; each later present looks at it again without waiting and copies into it once it is given back. A swapchain made again while one is held lets it go. test_present_semaphores and test_present_device_loss read the acquire's new arguments. Co-Authored-By: Claude Opus 5.5 <[email protected]>Tested in game: Linux, RTX 3090, NVIDIA 620.70.06, headless sway 1920x1080 144 Hz, fullscreen, frame generation 2x, renderer pack of this fix over the shared base
8e9cfc3a: in play, the game's workspace left for 40 s and 5 s and come back, mailbox and V-Sync On; frames shown again each time, no hang-watch or device-lost lines; FIFO pauses while hidden exactly as main does. Not run on the bug's hardware (AMD on Windows): Test Farm asked.@Beanstalk on Windows with an AMD GPU on AMD's own driver (RDNA 4 like an RX 9070 XT if there is one, else RDNA 3), this branch against main, fullscreen, V-Sync Auto:
Beanstalk test farm
Commit
da29a64· tier reviewed · approved by HumanBean · dashboardWhat will be tested · AI plan (
gpt-6.1-terra)On Windows with an AMD proprietary driver and an RDNA 4-class GPU, this plan compares main and the branch in Hyrule Field for about 15 minutes. It tests fullscreen frame generation at 2x and 4x with V-Sync Auto and On, using frametime as a pacing proxy and comparing pictures. A crash, GPU-hang log, significant p99 frametime regression, or picture mismatch fails the plan. The farm cannot put a browser over the game or start it behind another window, so the requested hidden-window reproduction, recovery, and launcher-start checks remain untested.
0or1; frame_generation2or4.Done (started 19:09 UTC, last report 19:53 UTC). Comment
@Beanstalkto run it again.AMD Radeon RX 9070 XT, 2.0.416vsync=0, frame_generation=2vsync=0, frame_generation=4vsync=1, frame_generation=2vsync=1, frame_generation=4Measurements · windows+amd-proprietary+gpu-rx-9*
Verdict: fail on windows+amd-proprietary+gpu-rx-9* (1/1 report):
field-pacing screenshot.max_difffailed.Hardware: 1: windows+amd+amd-proprietary+dgpu
AMD Radeon RX 9070 XT, 2.0.416AMD Ryzen 7 5800X3D 8-Core Processor, 32 GB RAMScene: field-pacing, from a new save (fullscreen=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); field-pacing: 2 game starts a side, field 30 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)c14641ada29a64vsync=0, frame_generation=2· field · frame time (p99) (ms)vsync=0, frame_generation=2· forbidden log linesvsync=0, frame_generation=4· field · frame time (p99) (ms)vsync=0, frame_generation=4· forbidden log linesvsync=1, frame_generation=2· field · frame time (p99) (ms)vsync=1, frame_generation=2· forbidden log linesvsync=1, frame_generation=4· field · frame time (p99) (ms)vsync=1, frame_generation=4· forbidden log linesFlags and settings
vsync0,1frame_generation2,4Markdown for the PR description
Possibly related, for context:
WIP: fix(vk): the swapchain's image is waited for on the CPU, so a window behind another one no longer stops the GPU's workto fix(vk): the swapchain's image is waited for on the CPU, so a window behind another one no longer stops the GPU's work