WIP: fix(vk): the presenter thread keeps the swapchain's external sync, and a drain that gives up is safe #1043
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
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!1043
Loading…
Reference in a new issue
No description provided.
Delete branch "TrashPanda/Longshot:perf/stage2-presenter"
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: low · Tested on: Linux, RTX 4070 Ti · Needs: AMD (Windows and Linux), Windows NVIDIA and the Steam Deck before
RX_VK_PRESENT_THREADcould become the default (a later PR, only if it measures faster there)Stage 2 of the renderer memory-sync redesign. Independent of #1034 and #1037: it branches from
main.Why
The presenter thread (
RX_VK_PRESENT_THREAD=1) presents each frame off the game's thread once its copy is done, which measured 19-20 ms a frame against 22-23 ms on an RTX 3090 under Wayland. It stayed off because of two problems written up next to it invk_replay.cpp: the swapchain was used from two threads without the external synchronization Vulkan requires, and a drain that gave up after 5 s could leave the presenter presenting to a destroyed swapchain. This PR fixes both, so the switch is safe to measure on more hardware. It does not turn it on.What changed
dev.swapchain_mu(gpu/vk/vk_internal.h): the acquire, both present paths, the presenter's wait for a present to be shown (in short polls, so the acquire doesn't wait for the frame to be shown), andcreate_swapchain/retire_swapchain. Lock order: afterpresent_mu, beforepresent_queue_muandpresenter_queue_mu; never held overdevice_wait_idle_locked.dev.swapchain_generation), checked under the lock. A present dropped that way is counted in its 600-present line ("N not made (their swapchain made again)").swap_spare), the acquire drains the presenter first.tests/test_present_thread.pychecks the lock around each swapchain call and the generation check;tests/test_present_semaphores.pyfollows the moved semaphore line.Measurements
Verdict: On this PC the presenter thread neither helps nor hurts: the frame here is not waiting on the present (the copy's fence is 2-5 ms, the present 0.04-0.11 ms). It stays off by default. No present was dropped and no error appeared, so the fix holds in normal running; a resize during the drain was not exercised (headless).
Hardware: Ubuntu 26.04.1 LTS (kernel 7.0.0-43), RTX 4070 Ti 12 GB, NVIDIA 595.99.02, Ryzen 7 9800X3D, 32 GB
Scene: Kokiri Forest (teleport 0) and Hyrule Field (teleport 1) from the repo save
kokiri-start, the camera turning; 1920x1080, no upscaler,HOS_FPS_MAX=240, headless (HOS_NO_SDL=1)Method: this PR @ 178a1794 with
RX_VK_PRESENT_THREAD=1against unset, the runs alternated; each run's second 20 s window, fps fromHOS_PRESENT_TRACERX_VK_PRESENT_THREAD=1* One run had 91 ms hitches that its log doesn't explain: as many slow submits as an off run (23), the same VRAM. The two runs either side of it are 112.4.
The tested-configurations row is
docs/tested/2026-11-15-present-thread-trashpanda-ab.md.Flags and settings
RX_VK_PRESENT_THREAD=1vkQueuePresentKHRTesting
Tested:
tests/test_present_thread.pyandtests/test_present_semaphores.py, andpython -I -B tests/run_all.pywith only the failuresmainhas too. In the game: the runs above, 5 with the thread on.Not tested: Windows, AMD, the Steam Deck; a window resized or the surface lost while the presenter holds a present (the drain path).
Must be tested: before the default flips, frame time with and without the switch on AMD (Windows and Linux), Windows NVIDIA and the Deck, with a window resized during play. A test request will follow.
Risks
What could break: the lock is taken around the acquire and the present with the switch off too; it is uncontended there (one thread), so it costs a lock a call. A wrong lock order would deadlock at a resize; the order is written next to the mutex and the test checks the calls.
Turning it off:
RX_VK_PRESENT_THREADunset (the default) for the presenter; revert for the lock.Compatibility: no API change.
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).third_party/untouched (a new folder with aPROVENANCE.mdinstead).When it applies
docs/tested/for each new measured run (format indocs/tested-configurations.md).MKWVK_API_VERSIONbumped: not needed,vk_api.hunchanged.tests/that reads changed source updated in this PR, not weakened.Tested in game: Linux (Ubuntu 26.04.1), RTX 4070 Ti, NVIDIA 595.99.02: Kokiri Forest and Hyrule Field from kokiri-start, the camera turning, RX_VK_PRESENT_THREAD=1 against unset alternated (5 runs on, 5 off). Same fps both ways (Kokiri 119.6/120.6 on against 116.1/120.1 off; Hyrule 112.4 on against 113.1-113.8 off); presenter's copy fence 2-5 ms, present 0.04-0.11 ms, 0 presents dropped, no error lines. Resize during the drain not exercised (headless).
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.