From 400da1aa8da376d38b3c4a086bc7f656f590de2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Quang=20Ng=C3=B4?= Date: Mon, 23 Dec 2024 21:21:48 +0700 Subject: [PATCH] Handle swapchain recreation (#1830) --- src/video_core/renderer_vulkan/vk_presenter.cpp | 10 +++++++--- src/video_core/renderer_vulkan/vk_swapchain.cpp | 4 +++- src/video_core/renderer_vulkan/vk_swapchain.h | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/video_core/renderer_vulkan/vk_presenter.cpp b/src/video_core/renderer_vulkan/vk_presenter.cpp index 139fd962..bc55cde2 100644 --- a/src/video_core/renderer_vulkan/vk_presenter.cpp +++ b/src/video_core/renderer_vulkan/vk_presenter.cpp @@ -634,9 +634,11 @@ void Presenter::Present(Frame* frame) { swapchain.Recreate(window.GetWidth(), window.GetHeight()); } - ImGui::Core::NewFrame(); + if (!swapchain.AcquireNextImage()) { + swapchain.Recreate(window.GetWidth(), window.GetHeight()); + } - swapchain.AcquireNextImage(); + ImGui::Core::NewFrame(); const vk::Image swapchain_image = swapchain.Image(); @@ -731,7 +733,9 @@ void Presenter::Present(Frame* frame) { // Present to swapchain. std::scoped_lock submit_lock{Scheduler::submit_mutex}; - swapchain.Present(); + if (!swapchain.Present()) { + swapchain.Recreate(window.GetWidth(), window.GetHeight()); + } // Free the frame for reuse std::scoped_lock fl{free_mutex}; diff --git a/src/video_core/renderer_vulkan/vk_swapchain.cpp b/src/video_core/renderer_vulkan/vk_swapchain.cpp index d0bc7ebd..380660a2 100644 --- a/src/video_core/renderer_vulkan/vk_swapchain.cpp +++ b/src/video_core/renderer_vulkan/vk_swapchain.cpp @@ -112,7 +112,7 @@ bool Swapchain::AcquireNextImage() { return !needs_recreation; } -void Swapchain::Present() { +bool Swapchain::Present() { const vk::PresentInfoKHR present_info = { .waitSemaphoreCount = 1, @@ -131,6 +131,8 @@ void Swapchain::Present() { } frame_index = (frame_index + 1) % image_count; + + return !needs_recreation; } void Swapchain::FindPresentFormat() { diff --git a/src/video_core/renderer_vulkan/vk_swapchain.h b/src/video_core/renderer_vulkan/vk_swapchain.h index a41b3ca7..19ae9b2d 100644 --- a/src/video_core/renderer_vulkan/vk_swapchain.h +++ b/src/video_core/renderer_vulkan/vk_swapchain.h @@ -32,7 +32,7 @@ public: bool AcquireNextImage(); /// Presents the current image and move to the next one - void Present(); + bool Present(); vk::SurfaceKHR GetSurface() const { return surface;