From 08257f19fd04b94fc0fff370be6c8e1479fef7c1 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Tue, 11 Nov 2025 09:52:15 -0500 Subject: [PATCH] xo-imgui: refactor ex4a: create_swapchain() -> .cpp --- xo-imgui/example/ex4a/VulkanApp.cpp | 50 ++++++++++++++++++ xo-imgui/example/ex4a/VulkanApp.hpp | 80 ++++++++--------------------- 2 files changed, 70 insertions(+), 60 deletions(-) diff --git a/xo-imgui/example/ex4a/VulkanApp.cpp b/xo-imgui/example/ex4a/VulkanApp.cpp index 288b4f44..95a8ad6e 100644 --- a/xo-imgui/example/ex4a/VulkanApp.cpp +++ b/xo-imgui/example/ex4a/VulkanApp.cpp @@ -165,4 +165,54 @@ MinimalImGuiVulkan::create_logical_device() vkGetDeviceQueue(device_, graphics_queue_family_, 0, &graphics_queue_); } +void +MinimalImGuiVulkan::create_swapchain() +{ + VkSurfaceCapabilitiesKHR capabilities; + vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physical_device_, surface_, &capabilities); + + uint32_t n_format; + vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device_, surface_, &n_format, nullptr); + std::vector formats(n_format); + vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device_, surface_, &n_format, formats.data()); + + VkSurfaceFormatKHR surface_format = formats[0]; + this->swapchain_image_format_ = surface_format.format; + + int width, height; + SDL_Vulkan_GetDrawableSize(window_, &width, &height); + this->swapchain_extent_ = { + static_cast(width), + static_cast(height) + }; + + uint32_t n_image = capabilities.minImageCount + 1; + if (capabilities.maxImageCount > 0 && n_image > capabilities.maxImageCount) { + n_image = capabilities.maxImageCount; + } + + VkSwapchainCreateInfoKHR create_info{}; + create_info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; + create_info.surface = surface_; + create_info.minImageCount = n_image; + create_info.imageFormat = surface_format.format; + create_info.imageColorSpace = surface_format.colorSpace; + create_info.imageExtent = swapchain_extent_; + create_info.imageArrayLayers = 1; + create_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; + create_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; + create_info.preTransform = capabilities.currentTransform; + create_info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; + create_info.presentMode = VK_PRESENT_MODE_FIFO_KHR; + create_info.clipped = VK_TRUE; + + if (vkCreateSwapchainKHR(device_, &create_info, nullptr, &(this->swapchain_)) != VK_SUCCESS) { + throw std::runtime_error("Failed to create swap chain!"); + } + + vkGetSwapchainImagesKHR(device_, swapchain_, &n_image, nullptr); + this->swapchain_images_.resize(n_image); + vkGetSwapchainImagesKHR(device_, swapchain_, &n_image, this->swapchain_images_.data()); +} + /* end VulkanApp.cpp */ diff --git a/xo-imgui/example/ex4a/VulkanApp.hpp b/xo-imgui/example/ex4a/VulkanApp.hpp index 4ca4da9b..3e0a1cb7 100644 --- a/xo-imgui/example/ex4a/VulkanApp.hpp +++ b/xo-imgui/example/ex4a/VulkanApp.hpp @@ -47,60 +47,20 @@ private: */ void create_logical_device(); - void create_swapchain() { - VkSurfaceCapabilitiesKHR capabilities; - vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physical_device_, surface_, &capabilities); - - uint32_t formatCount; - vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device_, surface_, &formatCount, nullptr); - std::vector formats(formatCount); - vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device_, surface_, &formatCount, formats.data()); - - VkSurfaceFormatKHR surfaceFormat = formats[0]; - swapchainImageFormat = surfaceFormat.format; - - int width, height; - SDL_Vulkan_GetDrawableSize(window_, &width, &height); - swapchainExtent = {static_cast(width), static_cast(height)}; - - uint32_t imageCount = capabilities.minImageCount + 1; - if (capabilities.maxImageCount > 0 && imageCount > capabilities.maxImageCount) { - imageCount = capabilities.maxImageCount; - } - - VkSwapchainCreateInfoKHR createInfo{}; - createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; - createInfo.surface = surface_; - createInfo.minImageCount = imageCount; - createInfo.imageFormat = surfaceFormat.format; - createInfo.imageColorSpace = surfaceFormat.colorSpace; - createInfo.imageExtent = swapchainExtent; - createInfo.imageArrayLayers = 1; - createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; - createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; - createInfo.preTransform = capabilities.currentTransform; - createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; - createInfo.presentMode = VK_PRESENT_MODE_FIFO_KHR; - createInfo.clipped = VK_TRUE; - - if (vkCreateSwapchainKHR(device_, &createInfo, nullptr, &swapchain) != VK_SUCCESS) { - throw std::runtime_error("Failed to create swap chain!"); - } - - vkGetSwapchainImagesKHR(device_, swapchain, &imageCount, nullptr); - swapchainImages.resize(imageCount); - vkGetSwapchainImagesKHR(device_, swapchain, &imageCount, swapchainImages.data()); - } + /* + * populates @ref swapchain_, @ref swapchain_images_ + */ + void create_swapchain(); void create_image_views() { - swapchainImageViews.resize(swapchainImages.size()); + swapchainImageViews.resize(swapchain_images_.size()); - for (size_t i = 0; i < swapchainImages.size(); i++) { + for (size_t i = 0; i < swapchain_images_.size(); i++) { VkImageViewCreateInfo createInfo{}; createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; - createInfo.image = swapchainImages[i]; + createInfo.image = swapchain_images_[i]; createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; - createInfo.format = swapchainImageFormat; + createInfo.format = swapchain_image_format_; createInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY; createInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY; createInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY; @@ -119,7 +79,7 @@ private: void create_render_pass() { VkAttachmentDescription colorAttachment{}; - colorAttachment.format = swapchainImageFormat; + colorAttachment.format = swapchain_image_format_; colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT; colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE; @@ -170,8 +130,8 @@ private: framebufferInfo.renderPass = renderPass; framebufferInfo.attachmentCount = 1; framebufferInfo.pAttachments = attachments; - framebufferInfo.width = swapchainExtent.width; - framebufferInfo.height = swapchainExtent.height; + framebufferInfo.width = swapchain_extent_.width; + framebufferInfo.height = swapchain_extent_.height; framebufferInfo.layers = 1; if (vkCreateFramebuffer(device_, &framebufferInfo, nullptr, &framebuffers[i]) != VK_SUCCESS) { @@ -275,7 +235,7 @@ private: init_info.RenderPass = renderPass; init_info.Subpass = 0; init_info.MinImageCount = MAX_FRAMES_IN_FLIGHT; - init_info.ImageCount = static_cast(swapchainImages.size()); + init_info.ImageCount = static_cast(swapchain_images_.size()); init_info.MSAASamples = VK_SAMPLE_COUNT_1_BIT; init_info.Allocator = nullptr; init_info.CheckVkResultFn = nullptr; @@ -345,7 +305,7 @@ private: vkWaitForFences(device_, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX); uint32_t imageIndex; - VkResult result = vkAcquireNextImageKHR(device_, swapchain, UINT64_MAX, + VkResult result = vkAcquireNextImageKHR(device_, swapchain_, UINT64_MAX, imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex); @@ -393,7 +353,7 @@ private: presentInfo.waitSemaphoreCount = 1; presentInfo.pWaitSemaphores = signalSemaphores; - VkSwapchainKHR swapChains[] = {swapchain}; + VkSwapchainKHR swapChains[] = {swapchain_}; presentInfo.swapchainCount = 1; presentInfo.pSwapchains = swapChains; @@ -432,7 +392,7 @@ private: renderPassInfo.renderPass = renderPass; renderPassInfo.framebuffer = framebuffers[imageIndex]; renderPassInfo.renderArea.offset = {0, 0}; - renderPassInfo.renderArea.extent = swapchainExtent; + renderPassInfo.renderArea.extent = swapchain_extent_; VkClearValue clearColor = {{{0.0f, 0.0f, 0.0f, 1.0f}}}; renderPassInfo.clearValueCount = 1; @@ -509,7 +469,7 @@ private: } void cleanupSwapchain() { - vkDestroySwapchainKHR(device_, swapchain, nullptr); + vkDestroySwapchainKHR(device_, this->swapchain_, nullptr); } void cleanup() { @@ -558,10 +518,10 @@ private: VkDevice device_; VkQueue graphics_queue_; - VkSwapchainKHR swapchain; - VkFormat swapchainImageFormat; - VkExtent2D swapchainExtent; - std::vector swapchainImages; + VkSwapchainKHR swapchain_; + VkFormat swapchain_image_format_; + VkExtent2D swapchain_extent_; + std::vector swapchain_images_; std::vector swapchainImageViews; VkRenderPass renderPass; std::vector framebuffers;