From 826298d097cdfbb22a00e546b5ccb00781c7ba82 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Tue, 11 Nov 2025 17:01:54 -0500 Subject: [PATCH] xo-imgui: refactor ex4a: cleanup() etc. --- xo-imgui/example/ex4a/VulkanApp.cpp | 88 +++++++++++++++++++++++++++++ xo-imgui/example/ex4a/VulkanApp.hpp | 84 +++++---------------------- 2 files changed, 102 insertions(+), 70 deletions(-) diff --git a/xo-imgui/example/ex4a/VulkanApp.cpp b/xo-imgui/example/ex4a/VulkanApp.cpp index 0da4b4b2..bb2f32b2 100644 --- a/xo-imgui/example/ex4a/VulkanApp.cpp +++ b/xo-imgui/example/ex4a/VulkanApp.cpp @@ -623,4 +623,92 @@ MinimalImGuiVulkan::record_command_buffer(VkCommandBuffer cmdbuf, uint32_t image } } +void +MinimalImGuiVulkan::recreate_swapchain() +{ + // handle window minimization: wait until window has valid size + this->wait_not_minimized(); + + // wait until device idle before cleaning up resources + vkDeviceWaitIdle(device_); + + // cleanup old swapchain + this->cleanup_framebuffers(); + this->cleanup_image_views(); + this->cleanup_swapchain(); + + // create new swapchain + this->create_swapchain(); + this->create_image_views(); + this->create_framebuffers(); +} + +void +MinimalImGuiVulkan::wait_not_minimized() +{ + int width = 0; + int height = 0; + SDL_GetWindowSize(window_, &width, &height); + while (width == 0 || height == 0) { + SDL_GetWindowSize(window_, &width, &height); + SDL_WaitEvent(nullptr); + } +} + +void +MinimalImGuiVulkan::cleanup_framebuffers() +{ + for (auto framebuffer : framebuffers_) { + vkDestroyFramebuffer(device_, framebuffer, nullptr); + } + framebuffers_.clear(); +} + +void +MinimalImGuiVulkan::cleanup_image_views() +{ + for (auto imageView : swapchain_image_views_) { + vkDestroyImageView(device_, imageView, nullptr); + } + swapchain_image_views_.clear(); +} + +void +MinimalImGuiVulkan::cleanup_swapchain() +{ + vkDestroySwapchainKHR(device_, this->swapchain_, nullptr); +} + +void +MinimalImGuiVulkan::cleanup() +{ + ImGui_ImplVulkan_Shutdown(); + ImGui_ImplSDL2_Shutdown(); + ImGui::DestroyContext(); + + for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { + vkDestroySemaphore(device_, render_finished_semaphores_[i], nullptr); + vkDestroySemaphore(device_, image_available_semaphores_[i], nullptr); + vkDestroyFence(device_, inflight_fences_[i], nullptr); + } + + vkDestroyCommandPool(device_, command_pool_, nullptr); + + this->cleanup_framebuffers(); + this->cleanup_image_views(); + this->cleanup_swapchain(); + + vkDestroyRenderPass(device_, render_pass_, nullptr); + vkDestroyDescriptorPool(device_, descriptor_pool_, nullptr); + vkDestroyDevice(device_, nullptr); + vkDestroySurfaceKHR(instance_, this->surface_, nullptr); + vkDestroyInstance(instance_, nullptr); + this->instance_ = nullptr; + + SDL_DestroyWindow(window_); + this->window_ = nullptr; + + SDL_Quit(); +} + /* end VulkanApp.cpp */ diff --git a/xo-imgui/example/ex4a/VulkanApp.hpp b/xo-imgui/example/ex4a/VulkanApp.hpp index 2b63100f..3d7971d0 100644 --- a/xo-imgui/example/ex4a/VulkanApp.hpp +++ b/xo-imgui/example/ex4a/VulkanApp.hpp @@ -120,81 +120,25 @@ private: /* record draw instructions into cmdbuf, framebuffers_[image_ix] */ void record_command_buffer(VkCommandBuffer commandBuffer, uint32_t image_ix); - void recreate_swapchain() { - // handle window minimization: wait until window has valid size - this->wait_not_minimized(); + /* Teardown + create swapchain (swapchain + framebuffers + image views). + * Need this after window size changes + */ + void recreate_swapchain(); - // wait until device idle before cleaning up resources - vkDeviceWaitIdle(device_); + /* wait until non-minimized window */ + void wait_not_minimized(); - // cleanup old swapchain - this->cleanup_framebuffers(); - this->cleanup_image_views(); - this->cleanup_swapchain(); + /* orderly disposal of @ref framebuffers_ */ + void cleanup_framebuffers(); - // create new swapchain - this->create_swapchain(); - this->create_image_views(); - this->create_framebuffers(); - } + /* orderly disposal of @ref swapchain_image_views_ */ + void cleanup_image_views(); - void wait_not_minimized() { - int width = 0; - int height = 0; - SDL_GetWindowSize(window_, &width, &height); - while (width == 0 || height == 0) { - SDL_GetWindowSize(window_, &width, &height); - SDL_WaitEvent(nullptr); - } - } + /* orderly disposal of @ref swapchain_ */ + void cleanup_swapchain(); - void cleanup_framebuffers() { - for (auto framebuffer : framebuffers_) { - vkDestroyFramebuffer(device_, framebuffer, nullptr); - } - framebuffers_.clear(); - } - - void cleanup_image_views() { - for (auto imageView : swapchain_image_views_) { - vkDestroyImageView(device_, imageView, nullptr); - } - swapchain_image_views_.clear(); - } - - void cleanup_swapchain() { - vkDestroySwapchainKHR(device_, this->swapchain_, nullptr); - } - - void cleanup() { - ImGui_ImplVulkan_Shutdown(); - ImGui_ImplSDL2_Shutdown(); - ImGui::DestroyContext(); - - for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { - vkDestroySemaphore(device_, render_finished_semaphores_[i], nullptr); - vkDestroySemaphore(device_, image_available_semaphores_[i], nullptr); - vkDestroyFence(device_, inflight_fences_[i], nullptr); - } - - vkDestroyCommandPool(device_, command_pool_, nullptr); - - this->cleanup_framebuffers(); - this->cleanup_image_views(); - this->cleanup_swapchain(); - - vkDestroyRenderPass(device_, render_pass_, nullptr); - vkDestroyDescriptorPool(device_, descriptor_pool_, nullptr); - vkDestroyDevice(device_, nullptr); - vkDestroySurfaceKHR(instance_, this->surface_, nullptr); - vkDestroyInstance(instance_, nullptr); - this->instance_ = nullptr; - - SDL_DestroyWindow(window_); - this->window_ = nullptr; - - SDL_Quit(); - } + /* orderly shutdown */ + void cleanup(); private: SDL_Window* window_ = nullptr;