How to Propely Handle vkDestroySwapchainKHR (I mean really properly)

For most of the time I have been using Vulkan I have been just calling vkDestroySwapchainKHR on the old swapchain on swapchain recreation. Now I knew this wasn’t correct according to the specs. The specs suggests that it’s not correct to destroy a swapchain whose resources are still potentially in use (that is they are somehow being used on windows for example by the DWM).

BUT, the problem is that in practice I have never ever had any issue with destroying the image views of the old swapchain and destroying the swapchain itself without caring about resources being used. I just call vkDestroySwapchain and I move on. I never had any error message through the layers, nor any crash. I have read somewhere that some drivers where doing this work for us, that is deferring the destruction of the old swapchain until it knows (say from the DWM) that it’s safe to do so. I use NV drivers. I have also put this on DWM itself which might be well written enough (for once) so that it checks if the resources are still valid by the time it uses them: if it realizes that the source images have been removed, it just ignores them. Maybe.

Would that explain why it never complains / crash?

I have looked at countless examples from Chrome to Blender source code and the many many examples that are online including NV minimal sample, etc. Most of them just don’t care. They call vkDestroySwapchain and that’s it. Some projects do hold a queue of retired swapchains and use the presence-fence mechanism to destroy them when it’s safe. I guess one can make this work. You can associate fences per swapchain, retire the swapchain and at some time in your app, go through the list of retired swapchain and destroy the swapchain and its image views when all fences are signaled.

Bottom line I am upper confused by the topic now:

  • Why doesn’t it crash or complain when I just call vkDestroySwapchain even when very likely the resources are still in use? Which most projects seem to do?
  • Is it REALLY necessary to check for presents associated with swapchain images to be done prior to destroying it if the driver handles this? Even though the spec tells you, you should do so.
  • If the driver takes care of it, why is it either not specified by the driver vendor and or don’t we change the specs?
  • If the “right way” is to proceed to destruction only when it’s effectively safe, what’s the best way of doing this. I am happy to have N frame in flight fences per swapchain and iterate through the retired swapchain to see of all fences are signaled, but that feel like a LOT of work for just this one thing.
  • Of course I am left with vkDeviceWaitIdle (or vkQueueWaitIdle) but that’s the hammer right? Though again if the driver does this under the hood that is block until resources are released, it’s similar in effect to blocking using vkQueueWaitIdle.

Thank you for your patience. Maybe this topic has been discussed in length here, but somehow I have been banging my head on this for week, spent days on looking for proper explanations on the net, and cannot find a source of truth.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.