9

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?

Kasata Ata
  • 365
  • 3
  • 11
  • "*I will be able to acquire minImageCount images*" What do you mean by "acquire"? Do you mean `vkAcquireNextImage`-style "acquire", or do you mean it as in common usage (ie: to get). – Nicol Bolas Oct 01 '20 at 13:25
  • Which is it: `VkSwapchainCreateInfoKHR::minImageCount == 2` or `VkSwapchainCreateInfoKHR::minImageCount == 3`? – Nicol Bolas Oct 01 '20 at 13:26
  • By acquire I mean vkAcquireNextImage without any blocking in triple buffering case. I've corrected post's contents, capabilities <2, 8>. – Kasata Ata Oct 01 '20 at 18:19
  • Why would you want to acquire 3 images *simultaneously*? You should acquire an image, use it, acquire the next one, use it, etc. That's generally how the process is meant to go. – Nicol Bolas Oct 01 '20 at 18:24
  • 1
    That's what I want to do but I don't understand why should I take minImageCount + 1 to guarantee non blocking behaviour. – Kasata Ata Oct 01 '20 at 18:51
  • 1
    I'm still confused about this. The spec clearly states that you can necessarily acquire an image when the number of acquired but not presented images is less than or equal to the difference between the number of swapchain images and VkSurfaceCapabilitiesKHR::minImageCount. Just because you have submitted a presentation request (e.g. "used" the image), that doesn't mean it's been "presented" yet. So even acquiring, then using, then acquiring, then using, still results in multiple simultaneous "acquired but not presented" images. I believe this was the point, no? What's the explanation for this? – Alexander Guyer Dec 23 '20 at 02:33

1 Answers1

4

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.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982