Class Swapchain

Class Documentation

class Swapchain

RAII wrapper class for swapchains.

Public Functions

Swapchain(Device &device, VkSurfaceKHR surface, std::uint32_t width, std::uint32_t height, bool vsync_enabled)

Default constructor

Parameters
  • device – The device wrapper

  • surface – The surface

  • width – The swapchain image width

  • height – The swapchain image height

  • vsync_enabledtrue if vertical synchronization is enabled

Swapchain(const Swapchain&) = delete
Swapchain(Swapchain&&) noexcept
~Swapchain()
Swapchain &operator=(const Swapchain&) = delete
Swapchain &operator=(Swapchain&&) = delete
std::uint32_t acquire_next_image_index(std::uint64_t timeout = std::numeric_limits<std::uint64_t>::max())

Call vkAcquireNextImageKHR

Parameters

timeout – (std::numeric_limits<std::uint64_t>::max() by default)

Throws

VulkanException – vkAcquireNextImageKHR call failed

Returns

The index of the next image

inline VkExtent2D extent() const
inline const VkSemaphore *image_available_semaphore() const
inline std::uint32_t image_count() const
inline VkFormat image_format() const
inline const std::vector<VkImageView> &image_views() const
void present(std::uint32_t img_index)

Call vkQueuePresentKHR

Parameters

img_index – The image index

Throws

VulkanException – vkQueuePresentKHR call failed

void setup_swapchain(std::uint32_t width, std::uint32_t height, bool vsync_enabled)

Setup the swapchain

Parameters
  • width – The width of the swapchain images

  • height – The height of the swapchain images

  • vsync_enabledtrue if vertical synchronization is enabled

Throws
inline const VkSwapchainKHR *swapchain() const

Public Static Functions

static std::optional<VkCompositeAlphaFlagBitsKHR> choose_composite_alpha(VkCompositeAlphaFlagBitsKHR request_composite_alpha, VkCompositeAlphaFlagsKHR supported_composite_alpha)

Choose the composite alpha

Parameters
  • request_composite_alpha – requested compositing flag

  • supported_composite_alpha – Alpha compositing modes supported on a device

Throws

std::runtime_error – No compatible composite alpha could be found

Returns

The chosen composite alpha flags

static VkExtent2D choose_image_extent(const VkExtent2D &requested_extent, const VkExtent2D &min_extent, const VkExtent2D &max_extent, const VkExtent2D &current_extent)

Determine the swapchain image extent

Parameters
  • requested_extent – The image extent requested by the programmer

  • min_extent – The minimum extent

  • max_extent – The maximum extent

  • current_extent – The current extent

Returns

The chosen swapchain image extent

static VkPresentModeKHR choose_present_mode(const std::vector<VkPresentModeKHR> &available_present_modes, const std::vector<VkPresentModeKHR> &present_mode_priority_list, bool vsync_enabled)

Choose the present mode

Note

If none of the present_mode_priority_list are supported, VK_PRESENT_MODE_FIFO_KHR will be returned

Parameters
  • available_present_modes – The available present modes

  • present_mode_priority_list – The acceptable present modes (DEFAULT_PRESENT_MODE_PRIORITY_LIST by

  • vsync_enabledtrue if vertical synchronization is enabled default). Index 0 has highest priority, index n has lowest priority)

Returns

The chosen present mode

static std::optional<VkSurfaceFormatKHR> choose_surface_format(const std::vector<VkSurfaceFormatKHR> &available_formats, const std::vector<VkSurfaceFormatKHR> &format_prioriy_list = {})

Choose a surface format

Note

Index 0 has highest priority, index n has lowest priority!

Parameters
  • available_formats – The available surface formats

  • format_prioriy_list – A priority list of acceptable surface formats (empty by default)

Returns

The chosen surface format (VK_FORMAT_UNDEFINED if no suitable format was found)

Private Functions

std::vector<VkImage> get_swapchain_images()

Call vkGetSwapchainImagesKHR

Throws

inexor::vulkan_renderer::VulkanException – vkGetSwapchainImagesKHR call failed

Returns

A std::vector of swapchain images (this can be empty!)

Private Members

Device &m_device
VkSwapchainKHR m_swapchain = {VK_NULL_HANDLE}
VkSurfaceKHR m_surface = {VK_NULL_HANDLE}
std::optional<VkSurfaceFormatKHR> m_surface_format = {}
std::vector<VkImage> m_imgs
std::vector<VkImageView> m_img_views
VkExtent2D m_extent = {}
std::unique_ptr<Semaphore> m_img_available
bool m_vsync_enabled = {false}