Skip to content

Conversation

@thatcosmonaut
Copy link
Collaborator

According to the Vulkan spec, we have to destroy swapchains before any associated surfaces.

I can't actually repro the validation error on my dev environment, so let's have the issue reporter verify the fix.

Resolves #14648

@zopsicle
Copy link

zopsicle commented Dec 15, 2025

I still get the validation error. With some debug printing I narrowed it down to this call:

if (swapchainSupportDetails.capabilities.currentExtent.width == 0 ||
swapchainSupportDetails.capabilities.currentExtent.height == 0) {
renderer->vkDestroySurfaceKHR(
renderer->instance,
windowData->surface,
NULL);
windowData->surface = VK_NULL_HANDLE;
if (swapchainSupportDetails.formatsLength > 0) {

I think the old swapchain still exists here, because it is used later in swapchainCreateInfo.oldSwapchain = windowData->swapchain;. If I replace this call to vkDestroySurfaceKHR with a call to VULKAN_INTERNAL_DestroySwapchain, the validation error disappears. There are some other calls to vkDestroySurfaceKHR nearby, they may have this same issue but I don't know how to trigger them.


Side note: there is a comment that says "NVIDIA + Win32 can return 0 extent when the window is minimized." I think this is always the case on Windows, not just with Nvidia, quoting the Vulkan spec on VkWin32SurfaceCreateInfoKHR:

The window size may become (0, 0) on this platform (e.g. when the window is minimized), and so a swapchain cannot be created until the size changes.

@thatcosmonaut
Copy link
Collaborator Author

I see... I'm trying to remember why exactly we're destroying the surface here.

@thatcosmonaut
Copy link
Collaborator Author

This problem was introduced by this patch: #14113

Before, we would always destroy the old swapchain, but now we hold on to it.

@zopsicle
Copy link

zopsicle commented Dec 15, 2025

IIUC the surface can just be kept alive the entire time between claiming the window and unclaiming the window, and reused with each swapchain recreation. It would only have to be destroyed when claiming the window fails, and when unclaiming the window.

@thatcosmonaut
Copy link
Collaborator Author

I believe you are correct. I'll revise this patch.

@thatcosmonaut thatcosmonaut changed the title GPU Vulkan: Destroy swapchain before surfaces GPU Vulkan: Only modify surface on window claim or release Dec 15, 2025
@thatcosmonaut
Copy link
Collaborator Author

This is much cleaner now.

Tagging @zopsicle and @slouken for re-review.

@zopsicle
Copy link

The new version does not trigger the validation error.

@thatcosmonaut thatcosmonaut merged commit 1260c10 into libsdl-org:main Dec 15, 2025
43 checks passed
@thatcosmonaut thatcosmonaut deleted the vk_swapchain_destroy_order branch December 15, 2025 20:54
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.

GPU: Vulkan surface destroyed before Vulkan swapchain is destroyed when minimizing window

3 participants