Skip to content

dxgi: a failed ResizeBuffers leaves the swap chain without buffers until the next one succeeds, as on the host - #8171

Open
lines9686-rgb wants to merge 2 commits into
ItsNotRexed:mainfrom
lines9686-rgb:dxgi-resizebuffers-lost
Open

lines9686-rgb wants to merge 2 commits into
ItsNotRexed:mainfrom
lines9686-rgb:dxgi-resizebuffers-lost

Conversation

@lines9686-rgb

@lines9686-rgb lines9686-rgb commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue: #7694 (comment). This is the ResizeBuffers failure item @tedin7 released, with their partial branch c2-resizebuffers-fail.

What the host does

The item assumed DXGI keeps the old buffers when ResizeBuffers fails. A probe on the host runtime says it doesn't. The probe is standalone, with no Leonida code. I ran it on an RTX 3060 Ti (NVIDIA 610.88) and on WARP:

Call RTX 3060 Ti WARP
ResizeBuffers(2, 16384, 16384) S_OK S_OK
ResizeBuffers(2, 16385, 16) / (16, 16385) E_INVALIDARG S_OK
ResizeBuffers(2, 32768, 32768), (2^20, 2^20) E_INVALIDARG E_OUTOFMEMORY
after any of these failures: GetDesc1 the requested size and count the requested size and count
… GetBuffer(0), Present DXGI_ERROR_INVALID_CALL DXGI_ERROR_INVALID_CALL
… the next ResizeBuffers(2, 128, 128) S_OK, then GetBuffer and Present S_OK same
ResizeBuffers with a back buffer still held DXGI_ERROR_INVALID_CALL, nothing changes same

So once the arguments are accepted, the old buffers are gone. A failure leaves the swap chain without buffers until the next successful ResizeBuffers. Keeping the old swapchain alive, as the partial branch did, would make the layer differ from the host. This PR implements the host's behaviour instead, so tedin7's commit is not in it. Their oversized-resize test case is the starting point of the comparison below.

What the layer did

  • It returned E_FAIL for every failure and kept describing the old buffers.
  • It passed a side above 16384 straight to vkCreateImage. With validation on, that is several errors (extent past maxImageDimension2D and maxFramebufferWidth, a 4 TiB allocation) before the driver fails it.

Change (runtime/shims/dxgi_com.cpp)

  • After the argument checks: a side above D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION is E_INVALIDARG, before anything reaches Vulkan. A Vulkan failure is E_OUTOFMEMORY.
  • After either failure the swap chain is "without buffers":
    • GetDesc/GetDesc1 report the requested size, count and format;
    • GetBuffer and Present are DXGI_ERROR_INVALID_CALL;
    • the next ResizeBuffers (count 0 and format UNKNOWN take the requested ones) rebuilds and clears the state.

Tests

  • dxgi_swapchain_test gets compare_failed_resize. It runs the same 17 steps on our swap chain and on the host's default adapter, and requires every HRESULT and description field to match. The steps are: 16385×16, its description, GetBuffer and Present; 16×16385; recovery with count 0; GetBuffer and Present after recovering; a resize with a held back buffer and what follows. WARP answers differently, so this compares with the default adapter, as the existing CreateSwapChainForHwnd comparison does.
  • dxgi_swapchain_count_test (fake Vulkan table, no GPU) gets failure injection for the Vulkan side.
    • The offscreen ring fails, or vkCreateSwapchainKHR fails. The fake follows the rule that the old swapchain is retired even then.
    • Each failure is checked on the offscreen and on the direct path.
    • In each case the next resize builds a swapchain that acquires and presents.

Tested (Windows 11, MSVC, RTX 3060 Ti / NVIDIA 610.88)

Branch on main dde82f27e, MSVC RelWithDebInfo, Ninja.

AI-assisted: yes (Claude Code)

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md). The host numbers come from a synthetic probe.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

🤖 Generated with Claude Code

…til the next one succeeds, as on the host

Measured against the host runtime (RTX 3060 Ti, NVIDIA 610.88, and WARP): once ResizeBuffers accepts its arguments
the old buffers are gone. If the new ones can't be made it fails, and the swap chain stays without buffers. GetDesc1
already reports the requested size and count, GetBuffer and Present are DXGI_ERROR_INVALID_CALL, and the next
successful ResizeBuffers restores everything. A side above 16384 is E_INVALIDARG on the hardware device. A call
refused for its arguments (a back buffer still held) changes nothing.

The layer answered E_FAIL and kept describing the old buffers. It now answers E_INVALIDARG for a side above 16384
before calling Vulkan (it used to pass the extent to vkCreateImage, past the device's limits) and E_OUTOFMEMORY when
Vulkan fails. It reports the requested description, refuses Present, and recovers on the next ResizeBuffers.

dxgi_swapchain_test compares 15 steps of that with the host's default adapter. dxgi_swapchain_count_test gets
failure injection on its fake Vulkan table: whether the offscreen ring or vkCreateSwapchainKHR fails (the latter
retires the old swapchain anyway), on the offscreen and on the direct path, the next resize builds a working
swapchain.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@KarolKurzyna

Copy link
Copy Markdown
Contributor

Independent Windows 11 MSVC + dual-GPU review of ae6810fab6 (AI-assisted, synthetic tests only, no game data).

Environment

  • OS / Toolchain: Windows 11 x64, MSVC 19.52.37029 (x64, Ninja), CMake 4.3.1-msvc1, Vulkan loader 1.4.363, VK_LOADER_LAYERS_DISABLE=~implicit~, LEONIDA_VK_VALIDATION=1.
  • GPU 1: NVIDIA GeForce RTX 3070 Ti (driver 621.24, Vulkan 1.4.357).
  • GPU 2: Intel(R) UHD Graphics 770 (Xe-LP, driver 101.6129, Vulkan 1.3.295).

Build & Results

  • Built cleanly with MSVC (0 new warnings) across dxgi_swapchain_test and dxgi_swapchain_count_test.
  • python -I scripts/check_no_game_data.py runtime/shims/dxgi_com.cpp runtime/test/dxgi_swapchain_test.cpp runtime/test/dxgi_swapchain_count_test.cpp: clean.
  • dxgi_swapchain_count_test.exe (CPU fake Vulkan): PASS (3 existing cases + 4 failed-resize failure-injection cases).
  • dxgi_swapchain_test.exe (LEONIDA_VK_VALIDATION=1, both RTX 3070 Ti and Intel UHD Graphics 770):
    • CreateSwapChainForHwnd: 45/45 matched host, 0 Vulkan validation errors on both GPUs.
    • compare_failed_resize: 13/15 steps matched host (including E_INVALIDARG on 16385x16 / 16x16385, GetDesc1, GetBuffer(0) == DXGI_ERROR_INVALID_CALL, Present == DXGI_ERROR_INVALID_CALL while lost, recovery ResizeBuffers(0, 64, 64), and ResizeBuffers with held buffer 0), with 0 Vulkan validation errors on both GPUs — but failed on the two post-recovery Present(0, 0) steps:
      failed ResizeBuffers, Present after recovering: ours 0x00000000, host 0x087a0001
      failed ResizeBuffers, Present after the refused call: ours 0x00000000, host 0x087a0001
      

Finding (test bug on hidden/occluded Windows 11 desktop)

  • 0x087a0001 is DXGI_STATUS_OCCLUDED (SUCCEEDED(hr) == true).
  • create_test_window(64, 64) (dxgi_swapchain_test.cpp:78) creates a non-WS_VISIBLE (hidden) HWND. When the window/desktop is occluded, host DXGI legitimately returns DXGI_STATUS_OCCLUDED from Present(0, 0) instead of S_OK (0x00000000). All earlier Present(0, 0) checks in dxgi_swapchain_test.cpp (e.g. lines 869, 1079, 1783) use FAILED(...) rather than != S_OK.
  • In compare_failed_resize (dxgi_swapchain_test.cpp:394, 400, 406), normalizing DXGI_STATUS_OCCLUDED to S_OK in a small present lambda (e.g. const HRESULT hr = chain->Present(0, 0); return static_cast<long>(hr == DXGI_STATUS_OCCLUDED ? S_OK : hr);) would still strictly enforce DXGI_ERROR_INVALID_CALL after a failed ResizeBuffers while avoiding false failures on occluded/headless-display Windows setups.

Minor code-audit note (non-blocking)

  • In runtime/shims/dxgi_com.cpp:521 (SwapChain::SetSourceSize), while lost_ == true after a failed ResizeBuffers, SetSourceSize still checks against the stale swapchain_.extent() rather than the lost state/extent.

Core runtime fix and Vulkan validation look solid across both NVIDIA and Intel — happy to re-verify once the DXGI_STATUS_OCCLUDED comparison in compare_failed_resize is adjusted!

…ffers; the comparison treats DXGI_STATUS_OCCLUDED as S_OK

Review of ItsNotRexed#8171 (@KarolKurzyna):
- The host answers DXGI_STATUS_OCCLUDED from Present for a window that isn't on screen, which depends on the
  desktop, so compare_failed_resize now counts it as S_OK. The failures stay strict.
- After a failed ResizeBuffers the host checks SetSourceSize against the size that was asked for (RTX 3060 Ti:
  after 16x16385, 100x10 is DXGI_ERROR_INVALID_CALL; after 16385x16 it is S_OK), so the layer does too. The
  comparison gains those two steps (17 now).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@Xiiano Xiiano 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.

Copilot SDK:
I found two actionable concerns: the new boolean failure mapping conflates allocation failures with unrelated Vulkan/device/surface failures, and the fake test assumes oldSwapchain is retired on a failed vkCreateSwapchainKHR without proving the spec/lifecycle behavior. I’m posting the review against the changed resize call now.

addComment "Blocking: VulkanSwapchain::resize() re…"
Reviewed and posted a comment on PR #8171 covering:

Incorrectly mapping all Vulkan resize failures to E_OUTOFMEMORY.
Ambiguous oldSwapchain retirement and recovery semantics.
Missing cleanup/error distinction after partial swapchain creation.
AI used: GitHub Copilot SDK in VS Code; model information is not exposed in this session.

@lines9686-rgb

Copy link
Copy Markdown
Contributor Author

Thanks @KarolKurzyna, both were right. Fixed in 4131eb7:

  • DXGI_STATUS_OCCLUDED. compare_failed_resize now counts it as S_OK through a small present lambda, so a window that isn't on screen no longer fails the comparison. DXGI_ERROR_INVALID_CALL while the buffers are lost is still compared exactly. My desktop never returned OCCLUDED, which is why I missed it.
  • SetSourceSize while lost. I measured the host on the RTX 3060 Ti. After a failed ResizeBuffers to 16×16385, SetSourceSize(100, 10) is DXGI_ERROR_INVALID_CALL. After 16385×16 the same call is S_OK. So the host checks against the size that was asked for, and the layer now does the same. The comparison gains those two steps, 17 in all.

Re-run here: dxgi_swapchain_test with LEONIDA_VK_VALIDATION=1 reports 17 steps compared with the host, 0 mismatches, 0 validation errors. dxgi_swapchain_count_test passes. AI-assisted (Claude Code).

@lines9686-rgb

Copy link
Copy Markdown
Contributor Author

@Xiiano, thanks. Point by point (the inline comment your review mentions didn't get posted, so I'm going by the summary):

  1. Every Vulkan failure becomes E_OUTOFMEMORY. Fair, but it isn't a regression. VulkanSwapchain::resize() returns a bool, so the cause doesn't reach ResizeBuffers. Before this PR every failure was E_FAIL, which the host never answers here. E_OUTOFMEMORY is what the host returns when the new buffers can't be allocated (measured on WARP at 32768²). A device or surface loss would ideally surface as DXGI_ERROR_DEVICE_REMOVED and the like, but that needs resize() to return a VkResult first. I'd keep that as a separate change, since it touches every caller, not just the failure path here.
  2. oldSwapchain retired on a failed create. That's the spec, not an assumption. The VkSwapchainCreateInfoKHR description says: "Upon calling vkCreateSwapchainKHR with an oldSwapchain that is not VK_NULL_HANDLE, oldSwapchain is retired — even if creation of the new swapchain fails." The fake table models exactly that, and the recovery case checks that the next resize, which passes the retired handle as oldSwapchain (allowed), builds a working swapchain.
  3. Cleanup after a partial creation. Nothing leaks on that path. If the new swapchain is created but its offscreen ring fails, that swapchain stays as swapchain_. The next rebuild() retires and destroys it, and destroy_swapchain() destroys it on release. The half-made ring is freed inside create_offscreen_backbuffers itself. No VkImage or memory is left without an owner.

AI-assisted (Claude Code).

@KarolKurzyna

Copy link
Copy Markdown
Contributor

Re-verified 4131eb75f1 on Windows 11 (MSVC 19.52.37029, Ninja, LEONIDA_VK_VALIDATION=1, AI-assisted, synthetic tests only, no game data): PASS.

  • dxgi_swapchain_count_test.exe (CPU): PASS (3 existing + 4 failed-resize cases).
  • NVIDIA GeForce RTX 3070 Ti (driver 621.24, Vulkan 1.4.357): dxgi_swapchain_test.exe — PASS (45/45 CreateSwapChainForHwnd, 17/17 failed ResizeBuffers including the two new SetSourceSize steps, 0 mismatches, 0 validation errors).
  • Intel(R) UHD Graphics 770 (driver 101.6129, Vulkan 1.3.295): dxgi_swapchain_test.exe — PASS (45/45 CreateSwapChainForHwnd, 17/17 failed ResizeBuffers, 0 mismatches, 0 validation errors).

Thanks for the fast fix and the hardware measurement of SetSourceSize, @lines9686-rgb!

@memorypressure

Copy link
Copy Markdown

Heads tested: ae6810f, then your force-pushed 4131eb7 (delta reviewed on its own; head re-confirmed at check end); main dde82f2; recheck clean vs d612f65/867d922f0 (tree 473803aa3). C++ — review-only; CI (pending at check time) is the gate. The lost-buffers state machine matches the host table line for line — GetDesc/GetDesc1 report the requested size/count/format, GetBuffer and Present give DXGI_ERROR_INVALID_CALL until the next successful ResizeBuffers, >16384 E_INVALIDARG lands before anything reaches vkCreateImage, Vulkan failure maps to E_OUTOFMEMORY, and unregister_buffers() before the failure paths is the right ordering. The force-push delta is coherent: SetSourceSize bounds-checks against the lost extent, and the host comparison normalizes only Present's DXGI_STATUS_OCCLUDED while failures stay strict. The RTX 3060 Ti + WARP per-call host table is hardware-tier evidence; the WARP 16385 divergence is acknowledged in code. Overlap: conflicts with #8176 in dxgi_swapchain_count_test.cpp — later merger rebases. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

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.

4 participants