Creating a Swapchain

A swapchain manages a collection of image buffers

At this point, the application has some means of presenting images through the surface. However, it needs some kind of structure which can manage these images and eventually present them to the surface. In Vulkan, this is done through a swapchain.

What is a swapchain?

A swapchain contains a collection of image buffers which can be thought of as the individual frames of the application. The application renders to these image buffers and then returns them to their place in the swapchain. The swapchain then controls how these images are presented to the surface.

As mentioned before, functionality for a swapchain is found in an extension, VK_KHR_swapchain, not in the core Vulkan API. The core API cannot assume what the application is going to do, because Vulkan supports both graphics and compute applications. Compute applications use the GPU to perform mathematical calculations, such as scientific simulations, and therefore do not need the ability to present rendered images to screen. In all cases where the application is required to display visual output, a swapchain is needed.

The swapchain and the set of images it contains are created together, so there are several characteristics of the images and the swapchain that need to be chosen. The available options for these characteristics depend on what is supported by the surface. An application can query a physical device for the surface capabilities to determine this.

The characteristics which need to be set during swapchain creation include:

  • Presentation mode
  • Minimum number of images in the swapchain
  • Image format
  • Image colour space
  • Image extent
  • Intended use of the images
  • Transformations to apply to the images before presentation
  • Whether Vulkan should discard non-visible regions of the image.

The colour space describes the range of colours which can be represented on screen. The most common type of colour space for monitors is RGB where colours are defined by different combinations of red, green and blue. All of the available colour spaces in Vulkan are explained here.

The image format describes how image buffers store pixel colour data. An example of an image format in Vulkan is VK_FORMAT_R8G8B8A8_UNORM. This is a four-component format (RGBA) with 8-bits in each color component. Each component is an unsigned normalised integer. All of the other image formats supported by Vulkan are described here.

The colour space and image format are specified together in the surface format struct, VkSurfaceFormatKHR.

Image extent describes the dimensions of the image in width and height. If the extent changes due to the surface size changing, then the swap chain has to be recreated.

Finally, the presentation mode defines how the swapchain presents images to the surface. It can have several different settings such as:

  • VK_PRESENT_MODE_IMMEDIATE_KHR - where newly-rendered images are presented as soon as they are ready, which could result in screen tearing.
  • VK_PRESENT_MODE_FIFO_KHR - where the images form a queue, newly rendered images are placed at the back of the queue, and images at the front of the queue are presented to the surface.

There are other presentation modes which are explained here.

Example: initSwapChain()

In the example code, this works as follows:

  1. Supported surface formats, presentation modes, and surface capabilities are retrieved from the physical device.
  2. The application selects the first available supported surface format, provided it is not undefined.
  3. It then checks to see if VK_PRESENT_MODE_IMMEDIATE_KHR is supported, if not it defaults to VK_PRESENT_MODE_FIFO_KHR.
  4. Finally, it selects an image extent based on the surface capabilities to ensure compatibility between the images and the surface.

This application is triple-buffered, so a minimum of three images are requested to be created alongside the swapchain.