After getting familiar with tons of books, tutorials and documentation regarding Vulkan I am still really confused by how does swapchain image count work.
Documentation on swapchain image count:
VkSwapchainCreateInfoKHR::minImageCount
is the minimum number of presentable images that the application needs. The implementation will either create the swapchain with at least that many images, or it will fail to create the swapchain.
After reading this field's description, my understanding is that if I will create swapchain with minImageCount
value greater than or equal to VkSurfaceCapabilitiesKHR::minImageCount
and lesser or equal to VkSurfaceCapabilitiesKHR::maxImageCount
then I will be able to acquire minImageCount
images, because it is number of images that the application needs.
Let's assume the following values:
VkSurfaceCapabilitiesKHR::minImageCount == 2
VkSurfaceCapabilitiesKHR::maxImageCount == 8
VkSwapchainCreateInfoKHR::minImageCount == 3
In such case I expect to be able to acquire 3 images from swapchain, let's say one designated to be presented, one waiting for being presented and one for drawing (just like in triple buffering case).
On the other hand many tutorials advise to set VkSwapchainCreateInfoKHR::minImageCount
value to VkSwapchainCreateInfoKHR::minImageCount + 1
, explaining that not all images created in swapchain are designated to be acquired by the application, because some of them might be used by driver internally.
Example: Discussion
Is there any reliable explanation on how to pick number of images in swapchain so that the application won't be forced to wait for image acquisition?
If the swapchain has 3+ images, then yes, if it has 2 then no. Sorry, something went wrong. In other words, you generally can acquire swapchainImageCount - minImageCount + 1 number of images. If the swapchain has 3+ images, then yes, if it has 2 then no.
minImageCount is the minimum number of presentable images that the application needs. The implementation will either create the swapchain with at least that many images, or it will fail to create the swapchain. You might get more images than minImageCount.
Swapchain is very similar. It can be used to achieve double buffering. In simple words, Swapchain is a collection Images (Stored in Image Buffers), that can be used to draw frames and present them on a screen when needed. Swapchain is a chain of images that we can render onto and then present to our window.
Using more swapchain images merely makes it less likely. However, you can easily tell when blocking happens simply by looking at how vkAcquireNextImageKHR behaves with a timeout of 0. If it returned that no image could be acquired, then you know that you need to wait.
Ultimately, the details of image presentation are not in your control. Asking for more images may make it less likely to encounter a CPU blocking situation, but there is no count, or other parameter, which can guarantee that it can't happen. Using more swapchain images merely makes it less likely.
However, you can easily tell when blocking happens simply by looking at how vkAcquireNextImageKHR
behaves with a timeout of 0. If it returned that no image could be acquired, then you know that you need to wait. This gives you the opportunity to decide what to do with this information.
When things like this happens, you can note that it happened. If it happens frequently enough, it may be worthwhile to recreate the swapchain set with more images. Obviously, this is not a light-weight solution, but it would be fairly hardware neutral.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With