From c8289cc8dcbdc557bfd90170f3bd7cfd24c188ce Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Tue, 5 Mar 2024 17:39:59 +0900 Subject: [PATCH] WIP impl helpers for choosing swap chain settings TODO: "Creating the swap chain" https://vulkan-tutorial.com/en/Drawing_a_triangle/Presentation/Swap_chain --- src/main.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/src/main.rs b/src/main.rs index 0f8398b..4e11982 100644 --- a/src/main.rs +++ b/src/main.rs @@ -615,6 +615,70 @@ impl VulkanApp { swap_chain_support_details } + + fn choose_swap_surface_format( + &self, + available_formats: &Vec, + ) -> Option { + if available_formats.is_empty() { + return None; + } + + for (idx, format) in available_formats.iter().enumerate() { + if format.format == ffi::VkFormat_VK_FORMAT_B8G8R8A8_SRGB + && format.colorSpace == ffi::VkColorSpaceKHR_VK_COLOR_SPACE_SRGB_NONLINEAR_KHR + { + return Some(idx); + } + } + + return Some(0); + } + + fn choose_swap_present_mode( + &self, + available_present_modes: &Vec, + ) -> ffi::VkPresentModeKHR { + for mode in available_present_modes { + if *mode == ffi::VkPresentModeKHR_VK_PRESENT_MODE_MAILBOX_KHR { + return ffi::VkPresentModeKHR_VK_PRESENT_MODE_MAILBOX_KHR; + } + } + + ffi::VkPresentModeKHR_VK_PRESENT_MODE_FIFO_KHR + } + + fn choose_swap_extent(&self, capabilities: &ffi::VkSurfaceCapabilitiesKHR) -> ffi::VkExtent2D { + if capabilities.currentExtent.width != u32::MAX { + return capabilities.currentExtent.clone(); + } + + let mut width: i32 = 0; + let mut height: i32 = 0; + unsafe { + ffi::glfwGetFramebufferSize( + self.window, + std::ptr::addr_of_mut!(width), + std::ptr::addr_of_mut!(height), + ); + } + + let mut actual_extent = ffi::VkExtent2D { + width: width as u32, + height: height as u32, + }; + + actual_extent.width = actual_extent.width.clamp( + capabilities.minImageExtent.width, + capabilities.maxImageExtent.width, + ); + actual_extent.height = actual_extent.height.clamp( + capabilities.minImageExtent.height, + capabilities.maxImageExtent.height, + ); + + actual_extent + } } impl Drop for VulkanApp {