From 5ac2b21dfe7166567d0326582f33290c3bb1c9cf Mon Sep 17 00:00:00 2001 From: Yoan Lecoq Date: Mon, 16 May 2022 11:20:01 +0200 Subject: [PATCH] SwapchainBuilder: provide some control over minImageCount --- src/VkBootstrap.cpp | 20 +++++++++++++++++++- src/VkBootstrap.h | 9 +++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/VkBootstrap.cpp b/src/VkBootstrap.cpp index 23b9d5b..111deb1 100644 --- a/src/VkBootstrap.cpp +++ b/src/VkBootstrap.cpp @@ -1774,10 +1774,18 @@ detail::Result SwapchainBuilder::build() const { return detail::Error{ SwapchainError::failed_query_surface_support_details, surface_support_ret.vk_result() }; auto surface_support = surface_support_ret.value(); - uint32_t image_count = surface_support.capabilities.minImageCount + 1; + uint32_t image_count; + if (info.min_image_count == 0) { + image_count = surface_support.capabilities.minImageCount + info.add_to_min_image_count; + } else { + image_count = info.min_image_count; + if (image_count < surface_support.capabilities.minImageCount) + image_count = surface_support.capabilities.minImageCount; + } if (surface_support.capabilities.maxImageCount > 0 && image_count > surface_support.capabilities.maxImageCount) { image_count = surface_support.capabilities.maxImageCount; } + VkSurfaceFormatKHR surface_format = detail::find_surface_format(info.physical_device, surface_support.formats, desired_formats, info.format_feature_flags); @@ -1972,6 +1980,16 @@ SwapchainBuilder& SwapchainBuilder::set_image_array_layer_count(uint32_t array_l info.array_layer_count = array_layer_count; return *this; } +SwapchainBuilder& SwapchainBuilder::set_desired_min_image_count(uint32_t min_image_count) { + info.min_image_count = min_image_count; + info.add_to_min_image_count = 0; // Unnecessary, but let's do it for clarity + return *this; +} +SwapchainBuilder& SwapchainBuilder::use_default_min_image_count(uint32_t add_to_min_image_count = 1) { + info.min_image_count = 0; + info.add_to_min_image_count = add_to_min_image_count; + return *this; +} SwapchainBuilder& SwapchainBuilder::set_clipped(bool clipped) { info.clipped = clipped; return *this; diff --git a/src/VkBootstrap.h b/src/VkBootstrap.h index 40c6f34..65fee99 100644 --- a/src/VkBootstrap.h +++ b/src/VkBootstrap.h @@ -846,6 +846,13 @@ class SwapchainBuilder { // Set the number of views in for multiview/stereo surface SwapchainBuilder& set_image_array_layer_count(uint32_t array_layer_count); + // Sets the desired minimum image count for the swapchain. Note that the presentation engine is always free to create more images than requested. + SwapchainBuilder& set_desired_min_image_count(uint32_t min_image_count); + // Use the default desired minimum image count for the swapchain. The chosen value is calculated as `capabilitites.minImageCount + add_to_min_image_count`. + // The default behavior so far was equivalent to setting `add_to_min_image_count` to 1, and generally led to a triple buffering setup. + // A discussion of the trade-offs involved between double buffering and triple buffering is available as https://en.wikipedia.org/wiki/Multiple_buffering + SwapchainBuilder& use_default_min_image_count(uint32_t add_to_min_image_count = 1); + // Set whether the Vulkan implementation is allowed to discard rendering operations that // affect regions of the surface that are not visible. Default is true. // Note: Applications should use the default of true if they do not expect to read back the content @@ -884,6 +891,8 @@ class SwapchainBuilder { uint32_t desired_width = 256; uint32_t desired_height = 256; uint32_t array_layer_count = 1; + uint32_t min_image_count = 0; + uint32_t add_to_min_image_count = 1; // Keep in sync with default parameter value in use_default_min_image_count() VkImageUsageFlags image_usage_flags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; VkFormatFeatureFlags format_feature_flags = VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT; uint32_t graphics_queue_index = 0;