Andrew Tomazos
Andrew Tomazos

Reputation: 68648

3 vs 2 VkSwapchain images?

So a Vulkan swapchain basically has a pool of images, user-defined in number, that it allocates when it is created, and images from the pool cycle like this:

1. An unused image is acquired by the program from the swapchain
2. The image is rendered by the program
3. The image is delivered for presentation to the surface.
4. The image is returned to the pool.

Given that, of what advantage is having 3 images in the swapchain rather than 2?

(I'm asking because I received a BestPractice validation complaint that I was using 2 rather than 3.)

With 2 images, isn't it the case that one will be being presented (3.) while the other is rendered (2.), and then they alternate those roles back and forth?

With 3 images, isn't it the case that one of the three will be idle? Particularly if the loop is locked to the refresh rate anyway?

I'm probably missing something.

Upvotes: 2

Views: 725

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473447

So what happens if one image is being presented, one has finished being rendered to by the GPU, and you have some work to render for later presentation? Well, if you're rendering to a swapchain image, that GPU work cannot start until image 0 has finished being presented.

Double buffering will therefore lead to stalling the GPU if the time to present a frame is greater than the time to render a frame. Yes, triple buffering will do the same if the render time consistently is shorter than the present time, but your frame time is right on the edge of the present time, then double buffering has a greater chance of wasting GPU time.

Of course, the downside is latency; triple buffering means that the image you display will be seen one frame later than double buffering.

Upvotes: 4

Related Questions