Skip to content

vulkan_present: a VK_SUBOPTIMAL_KHR on an unchanged surface no longer rebuilds the swapchain - #7391

Open
Cattn wants to merge 1 commit into
ItsNotRexed:mainfrom
Cattn:present-suboptimal-no-rebuild
Open

Cattn wants to merge 1 commit into
ItsNotRexed:mainfrom
Cattn:present-suboptimal-no-rebuild

Conversation

@Cattn

@Cattn Cattn commented Nov 23, 2026

Copy link
Copy Markdown

Claimed in the coordination issue: #7231 (comment), from @cherryinbubbles-ops's finding #7231 (comment) (thanks). This is a separate implementation; the patch attached to the finding was not used.

What and why

VK_SUBOPTIMAL_KHR is advisory: the image was acquired or presented, and the swapchain still works. VulkanPresenter::submit_now treated it like VK_ERROR_OUT_OF_DATE_KHR. Acquire and present both set stale_, so the next frame ran recreate(): a vkQueueWaitIdle, then a new swapchain, even when the new one would be identical. A driver or compositor that keeps reporting SUBOPTIMAL therefore stalls the queue and rebuilds the swapchain on every frame.

Now:

  • recreate()'s surface queries and choices move into choose(vsync, srgb, hdr, SwapchainChoice&): extent (including the 0xFFFFFFFF / extent_fn_ path), surface format and colour space, HDR, present mode, image usage, min image count, pre-transform, composite alpha and, with LEONIDA_SWAPCHAIN_MAINTENANCE1, the tear mode. recreate() calls it and fills the same VkSwapchainCreateInfoKHR as before. A successful recreate keeps the choice in chosen_.
  • SUBOPTIMAL from acquire or present sets suboptimal_ instead of stale_.
  • On the next present, submit_now runs choose() with the current swapchain's request (vsync_, swapchain_srgb_, swapchain_hdr_req_). It sets stale_, so the existing path rebuilds, only when the choice differs from chosen_ or a surface query fails. Otherwise the swapchain is kept, with no queue wait.
  • Unchanged: VK_ERROR_OUT_OF_DATE_KHR, the lost-surface recovery from presenter: recover a lost surface instead of a sticky E_FAIL #6832, the minimised (zero-extent) path, and every recreate triggered by a vsync, sRGB or HDR change.

Related: #7344 handles the opposite case, rebuilding when the client size changed but the driver has not reported SUBOPTIMAL or OUT_OF_DATE yet. The two changes are independent, and this branch merges cleanly with #7344's head (git merge-tree).

The five comment lines in recreate() that the diff shows as added are existing comments; the function moved below choose(), and their text is unchanged.

Tests

New step at the end of runtime/test/vk_presenter_sync_test.cpp. Its synthetic driver layer now also hooks vkAcquireNextImageKHR, vkCreateSwapchainKHR and vkQueueWaitIdle. SUBOPTIMAL is injected only after the real call succeeded, so the frame really was acquired or presented.

  • 12 presents on an unchanged window, with 6 acquires and 6 presents answered SUBOPTIMAL: expect 0 vkCreateSwapchainKHR, 0 vkQueueWaitIdle and the same extent.
  • The window resized to a 256x192 client area, then one SUBOPTIMAL present: within 20 presents the swapchain must reach the new client size, with at least one vkCreateSwapchainKHR.

Measured on an NVIDIA GeForce RTX 4070, driver 617.42, Windows 11, MSVC 14.43, RelWithDebInfo:

vkCreateSwapchainKHR vkQueueWaitIdle
main 8575d36ed with only the test applied, 12 injected SUBOPTIMAL, unchanged window 6 6
this PR, same input 0 0

The main counts are the same in 3 of 3 runs. With only the test applied, the new step fails on main at swapchain_creates == 0.

  • ctest -R "present|swapchain" (7 tests: vk_presenter_capture_colour, vk_presenter_sync, d3d12_present, xbox_present, dxgi_swapchain, dxgi_swapchain_count, xbox_present_readback_size): 7/7 pass with this PR on 8575d36ed in 3 of 3 runs and again after rebasing on b2801e26e; 7/7 on main without it.
  • gta6rt builds. The only /W4 warning in these files is the existing C4189 (device unused in mark_surface_lost), which is on main too.
  • No game files.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

…e changed

SUBOPTIMAL from vkAcquireNextImageKHR or vkQueuePresentKHR marked the
presenter stale, so the next frame waited for the queue to go idle and
recreated the swapchain even on an unchanged surface. The surface queries
and choices of recreate() now live in choose(); after a SUBOPTIMAL result
the next submit compares a fresh choice with the one the swapchain was
built from and rebuilds only when they differ. OUT_OF_DATE, the lost
surface path and the minimised path are unchanged.

vk_presenter_sync_test injects SUBOPTIMAL after successful acquires and
presents: 12 on an unchanged window now rebuild nothing (main: 6 swapchain
creations and 6 vkQueueWaitIdle), and one after a resize still follows the
new client size.

Reported by cherryinbubbles-ops in ItsNotRexed#7231.
@MartinIndra02

Copy link
Copy Markdown

Independent Windows + NVIDIA GPU test of 4abe9b3e7: pass.

Setup: Windows 11 x64, MSVC 19.44.35220 (RelWithDebInfo, Windows SDK 10.0.26100.0), NVIDIA GeForce RTX 3080 (driver 32.0.16.2124 / 621.24), Vulkan SDK 1.4.363.1, no game files.

  • vk_presenter_sync_test.exe: PASS (exit 0).
  • xbox_present_test.exe: xbox_present_test: all cases pass (exit 0).
  • d3d12_present_test.exe: PASS (exit 0) — M0: 70 frames cleared and presented through the Xbox-ABI proxy on Vulkan, swapchain recreated on resize, colour confirmed in the presented images.

AI-assisted review/test.

@thehollowedone

Copy link
Copy Markdown

Independent native Windows validation check of 4abe9b3e. RTX 3080 Ti, NVIDIA 621.35, Vulkan SDK 1.4.363.0.

The focused vk_presenter_sync run consumed all 12 simulated advisories on an unchanged surface, six from successful acquisitions and six from successful presents. The extent stayed the same, with zero new swapchains and zero calls to vkQueueWaitIdle. The resize control created one swapchain at 256 × 192. Khronos core and synchronization validation reported zero errors. This run covered the advisory and resize cases.

AI-assisted.

@nnonno91

Copy link
Copy Markdown

Tested 4abe9b3e7 on the Ryzen 7 9800X3D Radeon gfx1036 iGPU (AMD driver 32.0.21042.62) and RTX 5090 (NVIDIA driver 617.42). Windows 11 build 26200, MSVC 19.51 Release, Khronos validation layer 1.4.357.0 and implicit Vulkan layers disabled.

I used the PR's regression test against its parent b2801e26e with a print of the two counters, then ran the unmodified PR head. Each run injected six acquire and six present VK_SUBOPTIMAL_KHR results after successful real calls, on an unchanged Win32 surface.

GPU Parent rebuilds / queue-idle calls PR rebuilds / queue-idle calls
Radeon gfx1036 6 / 6 (3 of 3 runs) 0 / 0 (3 of 3 runs)
RTX 5090 6 / 6 (3 of 3 runs) 0 / 0 (3 of 3 runs)

All six patched runs recovered the expected swapchain extent after a real window resize and reported zero validation errors. The seven-test presentation group also passed on each GPU (vk_presenter_capture_colour, vk_presenter_sync, d3d12_present, xbox_present, dxgi_swapchain, dxgi_swapchain_count, and xbox_present_readback_size).

AI-assisted verification. Synthetic fixtures only.

@byPreaZy byPreaZy left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review (static): the SUBOPTIMAL handling is correct. Checked the edge cases the diff does not spell out:

  • chosen_ is always populated before it is compared: the first swapchain goes through recreate() (ok_ = recreate(true, false) in prepare), and the zero-extent minimized path leaves swapchain_ null so the suboptimal_ && swapchain_ guard skips the compare anyway.
  • suboptimal_ self-clears inside the guard (suboptimal_ = false before choose()), and recreate() clears it again on success, so a failed choose() cannot spin: it sets stale_ once and the existing recreate-retry path takes over.
  • choose() is side-effect free (surface queries + find_tear_mode only), so calling it speculatively on the suboptimal check is safe, and the extra vkGetPhysicalDeviceSurfaceCapabilitiesKHR per suboptimal episode is one call per event, not per frame in the steady state.
  • SwapchainChoice::tear is redundant in operator== (derived from mode/min_images/usage/extent) but harmless.

The test's two cases cover both directions: unchanged surface keeps the swapchain (0 creates, 0 wait-idles over 12 presents), resized surface still rebuilds. LGTM pending the maintainer's GPU run; AI-assisted (Devin), no local run.

@illusionart453-max

Copy link
Copy Markdown

Independent Windows/MSVC 19.44, RTX 5060 driver 32.0.16.1088 verification: #7391 diff applied cleanly to main 8575d36 audit checkout (already contains unrelated #7125 test correction and #7357 handle fix). Rebuilt vk_presenter_sync_test, RelWithDebInfo, ran exit 0. Output confirms 12 SUBOPTIMAL advisories on unchanged surface rebuilt nothing, and SUBOPTIMAL after resize rebuilt at new extent. Other sync/capture/concurrent-present regression checks passed. Validation layer unavailable, no validation coverage claimed. Existing C4189 and TEMP MSB8029 warnings remain. Synthetic windows only, no game files. AI-assisted (Codex).

@RAINighty

Copy link
Copy Markdown

Tested 4abe9b3e7 on Windows 11 (10.0.26300), RTX 5060 Laptop GPU (driver 32.0.16.2124), MSVC 19.44.35228, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0, Ninja, RelWithDebInfo: pass.

  • vk_presenter_sync_test.exe with LEONIDA_VK_VALIDATION=1: exit 0, both new steps print ok ("12 SUBOPTIMAL results on an unchanged surface rebuilt nothing", "SUBOPTIMAL after a resize rebuilt the swapchain at the new extent").
  • Control: this PR's test with runtime/src/vulkan_present.h from the merge base b2801e26e fails at line 575: driver::swapchain_creates.load() == 0, so the test does catch the problem.
  • ctest -R "present|swapchain": 7/7 pass, with and without validation.
  • The SUBOPTIMAL results are injected by the test's synthetic driver layer, not reported by a real driver. This laptop is hybrid (Intel UHD + RTX 5060). I only ran it; I did not review the code.

AI-assisted (Claude Code).

@RAINighty RAINighty mentioned this pull request Nov 23, 2026
2 tasks
@byPreaZy

Copy link
Copy Markdown

Windows GPU check on AMD: Radeon RX 9060 XT, driver 32.0.32015.7029, Windows 11 x64, MSVC 14.44, RelWithDebInfo, Vulkan SDK 1.4.363.0, no game files. PR head applied on main 8575d36ed (diff applied cleanly).

vk_presenter_sync_test: ok, exit 0, including the two new steps:

  • presenter sync: 12 SUBOPTIMAL results on an unchanged surface rebuilt nothing ok
  • presenter sync: SUBOPTIMAL after a resize rebuilt the swapchain at the new extent ok

Independent AMD confirmation of the RTX 4070 numbers: SUBOPTIMAL no longer forces a swapchain rebuild when the chosen parameters are unchanged, and a real surface change still rebuilds.

AI-assisted (Devin).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants