From aa752bc208c9764924dfa3e8836d9ece3792a5c3 Mon Sep 17 00:00:00 2001 From: Riley King-Saunders Date: Thu, 10 Jul 2025 21:50:26 +1000 Subject: [PATCH] Extended SwapchainSupportDetails API to force it to requery values, use darray's rather than initializer lists and extended the get_framebuffer_extent function to allow disabling the final clamping step at the end of the function. This is used by the swapchain to avoid repeatedly requerying when minimised. --- include/vulkan/basalt_physical_device.h | 9 ++++--- src/vulkan/basalt_physical_device.cpp | 35 +++++++++++++++++++++---- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/include/vulkan/basalt_physical_device.h b/include/vulkan/basalt_physical_device.h index 064131e..c45bd5f 100644 --- a/include/vulkan/basalt_physical_device.h +++ b/include/vulkan/basalt_physical_device.h @@ -19,9 +19,11 @@ namespace basalt SwapchainSupportDetails(VkPhysicalDevice phy, VkSurfaceKHR surface); ~SwapchainSupportDetails(); - VkSurfaceFormatKHR get_surface_format(const std::initializer_list& allowed_formats); - VkPresentModeKHR get_present_mode(const std::initializer_list& allowed_present_modes); - VkExtent2D get_framebuffer_extent(basalt::Window& window); + void requery(basalt::Window& window, VkPhysicalDevice phy, const basalt::darray& allowed_formats, const basalt::darray& allowed_present_modes); + + VkSurfaceFormatKHR get_surface_format(const basalt::darray& allowed_formats); + VkPresentModeKHR get_present_mode(const basalt::darray& allowed_present_modes); + VkExtent2D get_framebuffer_extent(basalt::Window& window, bool clamp=true); VkSurfaceCapabilitiesKHR surface_capabilities; basalt::darray surface_formats; @@ -37,6 +39,7 @@ namespace basalt operator VkPhysicalDevice() noexcept; + VkPhysicalDeviceMemoryProperties memory_props; basalt::darray enabled_extensions; DeviceQueueFamilyIndicies indicies; VkSurfaceKHR surface; diff --git a/src/vulkan/basalt_physical_device.cpp b/src/vulkan/basalt_physical_device.cpp index ebb60c3..bf923fb 100644 --- a/src/vulkan/basalt_physical_device.cpp +++ b/src/vulkan/basalt_physical_device.cpp @@ -333,7 +333,9 @@ basalt::DeviceQueueFamilyIndicies::DeviceQueueFamilyIndicies(u32 graphics, u32 p basalt::PhysicalDevice::PhysicalDevice(VkPhysicalDevice dev, const DeviceQueueFamilyIndicies& indicies, basalt::darray&& enabled_extensions, VkSurfaceKHR surface) : phys(dev), indicies(indicies), enabled_extensions(std::move(enabled_extensions)), surface(surface) -{ } +{ + vkGetPhysicalDeviceMemoryProperties(dev, &memory_props); +} basalt::PhysicalDevice::operator VkPhysicalDevice() noexcept { return this->phys; } @@ -364,7 +366,27 @@ basalt::SwapchainSupportDetails::~SwapchainSupportDetails() BTRACE("Destroyed %s (%p) with present modes at %p, and surface formats at %p\n", typeid(SwapchainSupportDetails).name(), this, this->present_modes.m_pdata, this->surface_formats.m_pdata); } -VkSurfaceFormatKHR basalt::SwapchainSupportDetails::get_surface_format(const std::initializer_list& allowed_formats) +void basalt::SwapchainSupportDetails::requery(basalt::Window& window, VkPhysicalDevice phy, const basalt::darray& allowed_formats, const basalt::darray& allowed_present_modes) +{ + vkGetPhysicalDeviceSurfaceCapabilitiesKHR(phy, window, &this->surface_capabilities); + u32 tmp = 0; + vkGetPhysicalDeviceSurfaceFormatsKHR(phy, window, &tmp, VK_NULL_HANDLE); + this->surface_formats.resize(tmp); + vkGetPhysicalDeviceSurfaceFormatsKHR(phy, window, &tmp, this->surface_formats.begin()); + + vkGetPhysicalDeviceSurfacePresentModesKHR(phy, window, &tmp, VK_NULL_HANDLE); + this->present_modes.resize(tmp); + vkGetPhysicalDeviceSurfacePresentModesKHR(phy, window, &tmp, this->present_modes.begin()); + + this->cached_format.format = VK_FORMAT_MAX_ENUM; + this->cached_format.colorSpace = VK_COLOR_SPACE_MAX_ENUM_KHR; + get_surface_format(allowed_formats); + this->cached_present_mode = VK_PRESENT_MODE_MAX_ENUM_KHR; + get_present_mode(allowed_present_modes); + get_framebuffer_extent(window); +} + +VkSurfaceFormatKHR basalt::SwapchainSupportDetails::get_surface_format(const basalt::darray& allowed_formats) { if (this->cached_format.colorSpace != VK_COLOR_SPACE_MAX_ENUM_KHR && this->cached_format.format != VK_FORMAT_MAX_ENUM) return this->cached_format; @@ -383,7 +405,7 @@ VkSurfaceFormatKHR basalt::SwapchainSupportDetails::get_surface_format(const std return surface_formats[0]; } -VkPresentModeKHR basalt::SwapchainSupportDetails::get_present_mode(const std::initializer_list& allowed_present_modes) +VkPresentModeKHR basalt::SwapchainSupportDetails::get_present_mode(const basalt::darray& allowed_present_modes) { if (this->cached_present_mode != VK_PRESENT_MODE_MAX_ENUM_KHR) return this->cached_present_mode; @@ -402,7 +424,7 @@ VkPresentModeKHR basalt::SwapchainSupportDetails::get_present_mode(const std::in return VK_PRESENT_MODE_FIFO_KHR; } -VkExtent2D basalt::SwapchainSupportDetails::get_framebuffer_extent(basalt::Window& window) +VkExtent2D basalt::SwapchainSupportDetails::get_framebuffer_extent(basalt::Window& window, bool clamp) { i32 width, height; glfwGetFramebufferSize(window, &width, &height); @@ -410,7 +432,10 @@ VkExtent2D basalt::SwapchainSupportDetails::get_framebuffer_extent(basalt::Windo .width = (u32)width, .height = (u32)height }; - BCLAMP_EXTENT(ext, this->surface_capabilities.minImageExtent, this->surface_capabilities.maxImageExtent); + if (clamp) + { + BCLAMP_EXTENT(ext, this->surface_capabilities.minImageExtent, this->surface_capabilities.maxImageExtent); + } return ext; }