A swapchain rebuild that fails is retried with the swapchain the failed call retired as oldSwapchain (VUID-VkSwapchainCreateInfoKHR-oldSwapchain-01933) #208

Open
opened 2026-11-27 09:06:21 +00:00 by jorijnvdgraaf · 0 comments

Summary

When vkCreateSwapchainKHR fails during a rebuild, the swapchain it was given as oldSwapchain is retired all the same, and swapChain still names it. RebuildSwapchain leaves swapchainRebuildPending set, so the next Render() (or a Resize or SetPresentMode outside a frame) tries the rebuild again, and that retry passes the same, now retired, swapchain as oldSwapchain, which valid usage forbids. Line numbers refer to e1ca2fd.

Details

  • implementations/Crafter.Graphics-Window.cpp:820: RebuildSwapchain sets swapchainRebuildPending = true before it rebuilds and clears it only once the swapchain is made (:844), so a rebuild that throws is tried again.
  • :1765 VkSwapchainKHR oldSwapchain = swapChain; and :1854 swapchainCI.oldSwapchain = oldSwapchain; in CreateSwapchain. The old swapchain's image views are destroyed before the create (:1862-1865).
  • :1870-1872: the new swapchain is made into a local and goes into swapChain only when the call succeeds, so after a failure swapChain still names the old swapchain. Before e1ca2fd the call wrote into the member itself, which then had undefined contents after a failure: no better for the retry.
  • The retries: RebuildIfDeferred (:913), first thing in every Render() (:1431) and after its present (:1676). Resize (:814) and SetPresentMode (:938) outside a frame call RebuildSwapchain themselves, since swapChain is not null.
  • The specification, chapters/VK_KHR_surface/wsi.adoc at Vulkan-Docs v1.4.369, lines 6119-6121: "Upon calling vkCreateSwapchainKHR with an oldSwapchain that is not VK_NULL_HANDLE, oldSwapchain is retired -- even if creation of the new swapchain fails." And VUID-VkSwapchainCreateInfoKHR-oldSwapchain-01933, lines 6306-6309: "If oldSwapchain is not VK_NULL_HANDLE, oldSwapchain must be a non-retired swapchain associated with the native window referred to by surface".

Found by reading the code, in the pre-push review of #169's fix; not reproduced, since no test makes vkCreateSwapchainKHR itself fail. The retry is invalid usage, so what a driver does with it is undefined; the validation layer would report 01933.

Suggested fix

When vkCreateSwapchainKHR fails with an oldSwapchain, destroy that swapchain where a successful create already destroys it (:1875-1876, after the same wait for the queue) and leave the window with no swapchain, so that the retry passes VK_NULL_HANDLE. A null swapChain now means "not made yet" to Resize (:802), SetPresentMode (:929) and the configure listener (hasSwapchain, :2073), and images would still hold the destroyed swapchain's images, so those would read swapchainRebuildPending too: a window whose rebuild failed takes a size and a present mode as one with a swapchain does, and draws nothing until the retry succeeds.

## Summary When `vkCreateSwapchainKHR` fails during a rebuild, the swapchain it was given as `oldSwapchain` is retired all the same, and `swapChain` still names it. `RebuildSwapchain` leaves `swapchainRebuildPending` set, so the next `Render()` (or a `Resize` or `SetPresentMode` outside a frame) tries the rebuild again, and that retry passes the same, now retired, swapchain as `oldSwapchain`, which valid usage forbids. Line numbers refer to `e1ca2fd`. ## Details - `implementations/Crafter.Graphics-Window.cpp:820`: `RebuildSwapchain` sets `swapchainRebuildPending = true` before it rebuilds and clears it only once the swapchain is made (`:844`), so a rebuild that throws is tried again. - `:1765` `VkSwapchainKHR oldSwapchain = swapChain;` and `:1854` `swapchainCI.oldSwapchain = oldSwapchain;` in `CreateSwapchain`. The old swapchain's image views are destroyed before the create (`:1862-1865`). - `:1870-1872`: the new swapchain is made into a local and goes into `swapChain` only when the call succeeds, so after a failure `swapChain` still names the old swapchain. Before `e1ca2fd` the call wrote into the member itself, which then had undefined contents after a failure: no better for the retry. - The retries: `RebuildIfDeferred` (`:913`), first thing in every `Render()` (`:1431`) and after its present (`:1676`). `Resize` (`:814`) and `SetPresentMode` (`:938`) outside a frame call `RebuildSwapchain` themselves, since `swapChain` is not null. - The specification, `chapters/VK_KHR_surface/wsi.adoc` at Vulkan-Docs `v1.4.369`, lines 6119-6121: "Upon calling vkCreateSwapchainKHR with an oldSwapchain that is not VK_NULL_HANDLE, oldSwapchain is retired -- even if creation of the new swapchain fails." And VUID-VkSwapchainCreateInfoKHR-oldSwapchain-01933, lines 6306-6309: "If oldSwapchain is not VK_NULL_HANDLE, oldSwapchain must be a non-retired swapchain associated with the native window referred to by surface". Found by reading the code, in the pre-push review of #169's fix; not reproduced, since no test makes `vkCreateSwapchainKHR` itself fail. The retry is invalid usage, so what a driver does with it is undefined; the validation layer would report 01933. ## Suggested fix When `vkCreateSwapchainKHR` fails with an `oldSwapchain`, destroy that swapchain where a successful create already destroys it (`:1875-1876`, after the same wait for the queue) and leave the window with no swapchain, so that the retry passes `VK_NULL_HANDLE`. A null `swapChain` now means "not made yet" to `Resize` (`:802`), `SetPresentMode` (`:929`) and the configure listener (`hasSwapchain`, `:2073`), and `images` would still hold the destroyed swapchain's images, so those would read `swapchainRebuildPending` too: a window whose rebuild failed takes a size and a present mode as one with a swapchain does, and draws nothing until the retry succeeds.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Catcrafts/Crafter.Graphics#208
No description provided.