Skip to content

vulkan_present: keep continuous readback valid on OUT_OF_DATE present - #2507

Closed
lixonik wants to merge 2 commits into
ProjectLeonida:mainfrom
lixonik:rt-present-resize-readback
Closed

lixonik wants to merge 2 commits into
ProjectLeonida:mainfrom
lixonik:rt-present-resize-readback

Conversation

@lixonik

@lixonik lixonik commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes a regression from 9f98dcc61 (RT-POST-LOAD-GPU-MEMORY) in VulkanPresenter::submit_now() (runtime/src/vulkan_present.h) that broke xbox_present case F (xbox_present_test.cpp:1226) on NVIDIA RTX 5070 during window resize.

When the window is resized between presents, vkQueuePresentKHR on Windows/NVIDIA can return VK_ERROR_OUT_OF_DATE_KHR after the command buffer -- including the vkCmdCopyImageToBuffer transfer into capture_buffer_ -- has already been submitted (submitted == VK_SUCCESS). submit_now() still returns VK_SUCCESS, and xbox_present_test.cpp:1224 explicitly expects the composed frame to remain readable through read_back().

Commit 9f98dcc61 cleared captured_ = false before submission and moved the captured_ = true assignment inside if (result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR). As a result, any VK_ERROR_OUT_OF_DATE_KHR from vkQueuePresentKHR left captured_ == false and caused read_back() to fail.

This change:

  • Keeps disk-capture scheduling (pending_capture_ = frame.done) gated on VK_SUCCESS || VK_SUBOPTIMAL_KHR so only shown frames are written to disk.
  • Sets captured_, captured_extent_, and captured_format_ whenever copy_frame && capture_ptr_ ran and result is VK_SUCCESS, VK_SUBOPTIMAL_KHR, or VK_ERROR_OUT_OF_DATE_KHR (leaving VK_ERROR_SURFACE_LOST_KHR and other failures uncaptured).

Verification

  • Negative control (main 39f52f39c): ctest --output-on-failure -R ^xbox_present$ failed deterministically at xbox_present_test.cpp:1226 (line 1226: present_planes(&one, 1)) during case F window resize (320x240 -> 400x300).
  • With this PR:

Claimed in #1608 (comment) (RT-PRESENT-RESIZE-READBACK). No game files used.

GPUs tested: NVIDIA GeForce RTX 5070 12 GB (Windows 11, driver 610.88, Vulkan 1.4.341; VK_LAYER_KHRONOS_validation not installed locally).
AI-assisted.

Independent follow-up

Three independent reports tested this head on RTX 5070 Ti and RTX 3080 on native Windows, and RTX 4070 SUPER and AMD Raphael under Wine. The window-resize test failed on their baseline and passed with the patch. The Wine report also forced WSI result codes after a real present and checked continuous readback and disk capture separately. RTX 5070 Ti report · RTX 3080 report · Wine and controlled WSI report

A later independent Wine run reproduced the first-resize failure on both main and the #2566 integration tree. Applying this PR alone made all six tested configurations pass 3/3, including desktop RTX 4070 SUPER and headless RTX 4070 SUPER / AMD Raphael configurations. The diagnostic RADV run traced the failure to missing continuous readback after PresentX succeeded. Desktop and headless follow-up

@Tyrannys Tyrannys left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Independent verification of head b8486b4a5 on profile M: RTX 4070 SUPER 12 GB, NVIDIA 621.01, Windows 11, Ryzen 7 9800X3D. MSVC RelWithDebInfo.

  • Negative control, main 39f52f39c: ctest -C RelWithDebInfo --output-on-failure -R "^xbox_present$" failed 2 of 2 runs, at line 1226: present_planes(&one, 1) (case F, window resize). So this is not just an RTX 5070 issue: the 4070 SUPER on 621.01 hits it too.
  • With this PR (merged on 39f52f39c together with #2505, which only touches a compile-pool test): ctest -C RelWithDebInfo --output-on-failure -R "^(xbox_present|d3d12_present)$" passed 3 of 3 runs (xbox_present 2.95-4.06 s, d3d12_present 1.62-1.97 s). The resize case is part of xbox_present, and it ran on the card.
  • Code read: pending_capture_ (disk capture) is still set only for a shown present. captured_ is now also set for VK_ERROR_OUT_OF_DATE_KHR, but only after vkQueueSubmit succeeded, so the copy into capture_buffer_ was really queued. VK_ERROR_SURFACE_LOST_KHR still clears it.

Clean. Approving. No game files used.

AI-assisted (Claude Code).

@timbot103

Copy link
Copy Markdown
Contributor

Reviewed at head b8486b4a5 on Windows 11 (MSVC 2022 19.44) with dual-GPU Vulkan validation (LEONIDA_VK_VALIDATION=1, Vulkan SDK 1.4.363.0).

Verification

  • Built xbox_present_readback_size_test and xbox_present_test.
  • xbox_present_readback_size_test: passed.
  • xbox_present_test (LEONIDA_PRESENT_PLANE1=0, LEONIDA_VK_VALIDATION=1): all cases passed with 0 validation errors on both NVIDIA GeForce RTX 5080 (616.64) and AMD Radeon(TM) Graphics (25.10.30.02) — notably fixing the resize OUT_OF_DATE failure previously reproduced on RTX 5080 (window resized 320x240 -> 400x300 -> 200x150 -> 320x240 between composed presents: ok).

Notes (non-blocking)

  • Advancing frame_index_ after submit and publishing capture extent/format on VK_ERROR_OUT_OF_DATE_KHR (vulkan_present.h:364-374, 650-717, 1336-1412) is sound because the capture copy and transfer-to-host barrier were already submitted and fenced before vkQueuePresentKHR, while surface-loss clears capture and minimize skips it.
  • Minor (vulkan_present.h:360-363): Consider updating the contract comment stating a skipped present stays unreadable until a frame is shown so it mentions the OUT_OF_DATE submitted-capture case as well.

Approve.

AI-assisted review + local MSVC/dual-GPU verification.

@zmasi

zmasi commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

#2507 | head b8486b4 (merged cleanly with main 48bae6c — no conflicts on current main) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^(xbox_present_readback_size|vk_presenter_sync|d3d12_present|xbox_present)$" --output-on-failure (LEONIDA_VK_VALIDATION=1) | red on main: xbox_present fails deterministically on RTX 3090 at line 1226: present_planes(&one, 1) during case F window resize (VK_ERROR_OUT_OF_DATE_KHR after frame copy submission) | green on PR+main: 4/4 CTests (xbox_present_readback_size, vk_presenter_sync, d3d12_present, xbox_present — compose: window resized 320x240 -> 400x300 -> 200x150 -> 320x240 between composed presents: ok and xbox_present_test: all cases pass, including HDR10 ST.2084 + tearing/non-tearing modes) pass in 8.19 s on RTX 3090 with 0 Khronos validation errors; check_no_game_data.py 0 violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes

@timbot103 timbot103 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed head b8486b4a5 by static inspection only, with no builds or GPU runs.

In runtime/src/vulkan_present.h:650, the copy submission succeeds before vkQueuePresentKHR runs. The capture path orders staging writes, makes them visible to the host, and transitions the image to PRESENT_SRC_KHR in runtime/src/vulkan_present.h:1336. The patch records the copied extent and format for VK_ERROR_OUT_OF_DATE_KHR, and read_back() still waits for that submission's fence before reading the bytes in runtime/src/vulkan_present.h:364.

VK_SUCCESS and VK_SUBOPTIMAL_KHR still qualify for disk sampling. An out-of-date or suboptimal present marks the swapchain stale, and recreation waits for the queue to idle before replacing its image semaphores and capture storage. Surface loss follows its existing recovery path and clears capture validity. Other present errors leave the new capture unpublished.

Case F in runtime/test/xbox_present_test.cpp:1212 already verifies readback and pixel contents across resizes, including the settled extent. The exact WSI result during resize depends on the driver. No findings.

AI-assisted.

@Diegobecker07

Copy link
Copy Markdown
Contributor

Verified on another setup: an RTX 5070 Ti with a newer driver (621.40), a display that exposes HDR10, and today's main 51d343b. That main already has #2508, which moved the failing check from line 1226 to 1320.

  • Base: main 51d343b, then main + PR head b8486b4 (git merge --no-ff, local only, clean). Targets built: xbox_present_test, xbox_present_readback_size_test, vk_presenter_sync_test, d3d12_present_test, vk_present_dump_test and xs_present_lazy_test (Ninja, RelWithDebInfo, MSVC 19.44, LEONIDA_REQUIRE_VULKAN=ON).
  • Red (MEASURED, 3 runs): on plain main, xbox_present fails 3 of 3 at line 1320: present_planes(&one, 1), the first resize round of case F.
  • Green (MEASURED, 3 runs plus 1 with validation): with the PR, xbox_present passes 3 of 3 (window resized 320x240 -> 400x300 -> 200x150 -> 320x240 between composed presents: ok, all cases pass). It also passes with LEONIDA_VK_VALIDATION=1 VK_VALIDATE_SYNC=1 (the device line reads validation on), with 0 VUID lines. This surface has HDR10, so the runs include HDR10 plane to an HDR10 (ST.2084) swapchain ... ok; as the test notes, the forced-encoding checks are skipped on such a surface.
  • The other seven present tests pass with and without the PR: xbox_present_readback_size, vk_presenter_sync, vk_present_dump (plus _default and _invalid), d3d12_present and xs_present_lazy.
  • The five present failed (VkResult -11) lines appear in every run, red and green, so they are not the symptom (as noted in #1608).

GPUs tested: NVIDIA GeForce RTX 5070 Ti 16 GB (desktop, profile M), driver 621.40, HDR10 display; Windows 11 Pro 26300; Vulkan SDK 1.4.363.0.

AI-assisted.

@merzleckeier

Copy link
Copy Markdown
Contributor

AGENT-MSG | 2026-11-29 14:19 UTC | merzleckeier | AI-assisted: Codex

BASE: 9bb67cb53 versus the same base + b8486b4a5, clean. The PR also merges cleanly with ddfe24fba.
MACHINE: RTX 4070 SUPER (NVIDIA 620.73.04) and AMD Raphael iGPU (1002:164e, RADV Mesa 26.3.1) | CachyOS, Wine 11.22, headless Gamescope.
EVIDENCE: MEASURED, 12 stock resize runs and 16 controlled result checks.

Stock xbox_present with LEONIDA_PRESENT_PLANE1=0: main failed at the first resized present_planes(&one, 1) in 3/3 runs on each GPU. The PR passed 3/3 on each GPU, including the 320×240 → 400×300 → 200×150 → 320×240 sequence.

The extra fixture composes red, then blue, with continuous readback and disk capture. It performs the real GPU copy and present and overrides only a successful WSI return after the second frame. Both GPUs gave the same results:

Second return to Mandy Continuous readback on main Continuous readback with PR Disk files on both builds
VK_SUCCESS or VK_SUBOPTIMAL_KHR all blue pixels all blue pixels red and blue
VK_ERROR_OUT_OF_DATE_KHR unavailable after a recorded GPU copy all blue pixels red only
VK_ERROR_SURFACE_LOST_KHR unavailable unavailable red only

The fixture checks the full pixel contents, capture extent, Vulkan format and PPM bytes. Run vk_presenter_sync_test with --reply-ok, --reply-suboptimal, --reply-out-of-date, or --reply-surface-lost. The last two pass on the PR, and only --reply-out-of-date fails on main. Host validation reported only the baseline Wine device-creation VUID 01387. Gamescope itself returned 139 during NVIDIA teardown on both builds after the Wine child had exited. The Wine child exit and assertions were recorded separately.

@TheGeogeo

Copy link
Copy Markdown
Contributor

Tested b8486b4 merged (no commit) with main 8fb9748: MSVC 19.52, Windows 11 10.0.26300.
GPUs tested: NVIDIA GeForce RTX 3080, driver 621.40.

  • Green: xbox_present passes with this PR.
  • Red: with runtime/src/vulkan_present.h back to main, xbox_present fails as it does on main: "present failed (VkResult -11)", then "line 1319: present_planes(&one, 1)".
  • gta6rt links with nothing new over main: no new unresolved symbol, same imports.
  • The 296 relinked CTests pass, except one run of xs_proxy_mock, which failed once on a timing check (line 4846, the 40 ms idle-timeout case). This is unrelated to the PR: that test binary does not depend on vulkan_present.h (it was not rebuilt when the file went back to main), and the same binary then passed 5 of 5 reruns.
  • Merges cleanly with current main 91bab93 too.
  • No game data check passes.

AI-assisted (Claude Code).

@ammonium231

Copy link
Copy Markdown
Contributor

RTX 3090 / 621.40, validation on, b8486b4 merged onto main 20fe0c7, CTest env: xbox_present all cases pass incl. the 320x240 -> 400x300 -> 200x150 resize (main fails at xbox_present_test.cpp:1320 on the first resize); d3d12_present passes as on main. No validation errors.

@imShangLi

Copy link
Copy Markdown
Contributor

VERIFY-2507 — Linux, Fedora 45, GCC, no GPU device, no game files

Verdict: PASS on the reading, and I could not build or run it. The change does exactly what the pull request says, and its placement relative to the two neighbouring blocks is the part that matters — I checked that it is right. But runtime/src/vulkan_present.h is not compiled on Linux at all (I probed it), so there is no test I can run and no compile check either.

Head e2bce1a3, base the current main 88a9f133, merge base ed867d7b, 1 commit, 1 file, +9/−6: runtime/src/vulkan_present.h.

1. What it changes

VulkanPresenter::submit_now() used to set the "a frame was captured" flag only for a present that succeeded:

if (result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR) {
    …
    if (copy_frame && capture_ptr_) {
        captured_ = true;
        captured_extent_ = extent_;
        captured_format_ = format_;
        if (disk_sample) pending_capture_ = frame.done;
    }
}

read_back() hands the frame out only when captured_ is set (runtime/src/vulkan_present.h:366). After 9f98dcc61 moved that assignment inside the "shown" branch, a present that reported VK_ERROR_OUT_OF_DATE_KHR — which on Windows/NVIDIA happens on a resize, after the command buffer with the vkCmdCopyImageToBuffer into capture_buffer_ has already been submitted — left captured_ false, and read_back() failed even though the frame was there.

The pull request splits the two:

    if (copy_frame && capture_ptr_ && disk_sample) pending_capture_ = frame.done;   // still inside the shown branch
}
if (copy_frame && capture_ptr_ &&
    (result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR || result == VK_ERROR_OUT_OF_DATE_KHR)) {
    captured_ = true;
    captured_extent_ = extent_;
    captured_format_ = format_;
}

2. The placement is the whole change, and it is right

The new block sits between two others, and both orderings matter:

if (result == VK_ERROR_SURFACE_LOST_KHR) {
    captured_ = false;            // a lost surface still clears it
    mark_surface_lost();
    result = VK_SUCCESS;
}
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR) {
    stale_ = true;
    result = VK_SUCCESS;          // the caller sees success
}
  • The new block is before the SURFACE_LOST reset, so a lost surface still clears captured_ — and the new block's condition does not include VK_ERROR_SURFACE_LOST_KHR anyway, so the two agree.
  • It is before the OUT_OF_DATE → VK_SUCCESS conversion, so the comparison still sees the original VK_ERROR_OUT_OF_DATE_KHR rather than the converted success. Had the conversion come first, the new clause would have been dead.

And pending_capture_ = frame.done — the disk sample — stays inside the "shown" branch, so only frames that were actually shown are written to disk; the pull request's own summary says that is deliberate, and the code keeps it.

3. Why I could not run it

runtime/src/vulkan_present.h is included by runtime/shims/d3d12_vulkan_device.cpp, runtime/shims/xbox_present.cpp, runtime/src/vulkan_present_splash.h and three tests — and all of the ones that reach it are behind the Windows shim (if(WIN32 AND LEONIDA_BUILD_WINDOWS_RUNTIME)) or include <windows.h>. I probed it: putting #error at the end of the header and rebuilding the Linux runtime produced no error, so the header is not compiled here at all.

The test the pull request uses is xbox_present_test.cpp case F, which resizes a window during a present — that needs a Win32 window and a real Vulkan swapchain, and the pull request's own measurement is on an NVIDIA RTX 5070. The negative control it quotes (case F failing deterministically on main) is the right experiment and I have no way to run it.

4. What I did not check

  • That the fix works. No build, no test, no run. My verdict is about the reading of the change and its placement.
  • The regression it fixes (9f98dcc61) and the xbox_present case F behaviour.
  • The d3d12_present test the pull request also lists.
  • Whether VK_ERROR_OUT_OF_DATE_KHR is the only present result that can arrive with the frame already copied — the new condition covers VK_SUCCESS, VK_SUBOPTIMAL_KHR and VK_ERROR_OUT_OF_DATE_KHR and nothing else, and I did not enumerate what other results vkQueuePresentKHR can return on the platforms in question.

AI-assisted: MiniMax M3.1 Flash.

@aslan2142

Copy link
Copy Markdown
Collaborator

Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

@aslan2142 aslan2142 closed this Dec 1, 2026
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.

10 participants