From 9137eedb7be5bb9e4789569a3b584c7737f9f20f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Fri, 18 Jul 2025 20:01:48 +0200 Subject: [PATCH] Windows/Vulkan: Correctly handle minimizing and restoring the window We can't create a swapchain in this case but we still want emulation to keep running. So we ditch the last renderpass in VulkanQueueRunner but run all the rest (if PauseWhenMinimized isn't set). Now works without any validation errors or hangs. --- Common/GPU/Vulkan/VulkanContext.cpp | 11 +++++ Common/GPU/Vulkan/VulkanContext.h | 17 ++++--- Common/GPU/Vulkan/VulkanQueueRunner.cpp | 54 ++++++++++++++--------- Common/GPU/Vulkan/VulkanRenderManager.cpp | 23 +++++----- Common/GPU/Vulkan/VulkanRenderManager.h | 2 +- Common/GraphicsContext.h | 1 + Common/System/System.h | 1 + UI/NativeApp.cpp | 5 +++ Windows/GPU/WindowsVulkanContext.cpp | 5 ++- Windows/GPU/WindowsVulkanContext.h | 5 +++ Windows/MainWindow.cpp | 6 +++ 11 files changed, 86 insertions(+), 44 deletions(-) diff --git a/Common/GPU/Vulkan/VulkanContext.cpp b/Common/GPU/Vulkan/VulkanContext.cpp index c044d2bcac..c121d58cfe 100644 --- a/Common/GPU/Vulkan/VulkanContext.cpp +++ b/Common/GPU/Vulkan/VulkanContext.cpp @@ -390,6 +390,7 @@ void VulkanContext::DestroySwapchain() { vkDestroySwapchainKHR(device_, swapchain_, nullptr); swapchain_ = VK_NULL_HANDLE; } + swapchainInited_ = false; } void VulkanContext::DestroySurface() { @@ -1343,6 +1344,15 @@ bool VulkanContext::InitSwapchain() { ERROR_LOG(Log::G3D, "VK: Surface lost in InitSwapchain"); return false; } + + if (surfCapabilities_.maxImageExtent.width == 0 || surfCapabilities_.maxImageExtent.height == 0) { + WARN_LOG(Log::G3D, "Max image extent is 0 - app is probably minimized. Faking having a swapchain."); + swapChainExtent_ = {}; // makes it so querying width/height returns 0. + // We pretend to have a swapchain initialized - though we won't actually render to it. + swapchainInited_ = true; + return true; + } + _dbg_assert_(res == VK_SUCCESS); uint32_t presentModeCount; res = vkGetPhysicalDeviceSurfacePresentModesKHR(physical_devices_[physical_device_], surface_, &presentModeCount, nullptr); @@ -1522,6 +1532,7 @@ bool VulkanContext::InitSwapchain() { return false; } INFO_LOG(Log::G3D, "Created swapchain: %dx%d %s", swap_chain_info.imageExtent.width, swap_chain_info.imageExtent.height, (surfCapabilities_.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) ? "(TRANSFER_SRC_BIT supported)" : ""); + swapchainInited_ = true; return true; } diff --git a/Common/GPU/Vulkan/VulkanContext.h b/Common/GPU/Vulkan/VulkanContext.h index 8bb2057435..dfe130c922 100644 --- a/Common/GPU/Vulkan/VulkanContext.h +++ b/Common/GPU/Vulkan/VulkanContext.h @@ -232,9 +232,6 @@ public: VkFence CreateFence(bool presignalled); bool CreateShaderModule(const std::vector &spirv, VkShaderModule *shaderModule, const char *tag); - int GetBackbufferWidth() { return (int)swapChainExtent_.width; } - int GetBackbufferHeight() { return (int)swapChainExtent_.height; } - void BeginFrame(VkCommandBuffer firstCommandBuffer); void EndFrame(); @@ -373,12 +370,13 @@ public: return curFrame_; } - VkSwapchainKHR GetSwapchain() const { - return swapchain_; - } - VkFormat GetSwapchainFormat() const { - return swapchainFormat_; - } + VkSwapchainKHR GetSwapchain() const { return swapchain_; } + VkFormat GetSwapchainFormat() const { return swapchainFormat_; } + bool IsSwapchainInited() const { return swapchainInited_; } + bool HasRealSwapchain() const { return swapChainExtent_.width > 0; } + + int GetBackbufferWidth() { return (int)swapChainExtent_.width; } + int GetBackbufferHeight() { return (int)swapChainExtent_.height; } void SetProfilerEnabledPtr(bool *enabled) { for (auto &frame : frame_) { @@ -511,6 +509,7 @@ private: VkFormat swapchainFormat_ = VK_FORMAT_UNDEFINED; uint32_t queue_count = 0; + bool swapchainInited_ = false; PhysicalDeviceFeatures deviceFeatures_; diff --git a/Common/GPU/Vulkan/VulkanQueueRunner.cpp b/Common/GPU/Vulkan/VulkanQueueRunner.cpp index c6badb55e6..4c92d4acaa 100644 --- a/Common/GPU/Vulkan/VulkanQueueRunner.cpp +++ b/Common/GPU/Vulkan/VulkanQueueRunner.cpp @@ -309,37 +309,49 @@ void VulkanQueueRunner::RunSteps(std::vector &steps, int curFrame, Fr switch (step.stepType) { case VKRStepType::RENDER: + { + bool perform = true; if (!step.render.framebuffer) { if (emitLabels) { vkCmdEndDebugUtilsLabelEXT(cmd); } frameData.Submit(vulkan_, FrameSubmitType::Pending, frameDataShared); - // When stepping in the GE debugger, we can end up here multiple times in a "frame". - // So only acquire once. - if (!frameData.hasAcquired) { - frameData.AcquireNextImage(vulkan_); - SetBackbuffer(framebuffers_[frameData.curSwapchainImage], frameDataShared.swapchainImages_[frameData.curSwapchainImage].image); - } + // If the window is minimized and we don't have a swap chain, don't bother. + if (frameDataShared.swapchainImageCount_ > 0) { + // When stepping in the GE debugger, we can end up here multiple times in a "frame". + // So only acquire once. + if (!frameData.hasAcquired) { + frameData.AcquireNextImage(vulkan_); + SetBackbuffer(framebuffers_[frameData.curSwapchainImage], frameDataShared.swapchainImages_[frameData.curSwapchainImage].image); + } - if (!frameData.hasPresentCommands) { - // A RENDER step rendering to the backbuffer is normally the last step that happens in a frame, - // unless taking a screenshot, in which case there might be a READBACK_IMAGE after it. - // This is why we have to switch cmd to presentCmd, in this case. - VkCommandBufferBeginInfo begin{ VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO }; - begin.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; - vkBeginCommandBuffer(frameData.presentCmd, &begin); - frameData.hasPresentCommands = true; - } - cmd = frameData.presentCmd; - if (emitLabels) { - VkDebugUtilsLabelEXT labelInfo{ VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT }; - labelInfo.pLabelName = "present"; - vkCmdBeginDebugUtilsLabelEXT(cmd, &labelInfo); + if (!frameData.hasPresentCommands) { + // A RENDER step rendering to the backbuffer is normally the last step that happens in a frame, + // unless taking a screenshot, in which case there might be a READBACK_IMAGE after it. + // This is why we have to switch cmd to presentCmd, in this case. + VkCommandBufferBeginInfo begin{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO}; + begin.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; + vkBeginCommandBuffer(frameData.presentCmd, &begin); + frameData.hasPresentCommands = true; + } + cmd = frameData.presentCmd; + if (emitLabels) { + VkDebugUtilsLabelEXT labelInfo{VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT}; + labelInfo.pLabelName = "present"; + vkCmdBeginDebugUtilsLabelEXT(cmd, &labelInfo); + } + } else { + perform = false; } } - PerformRenderPass(step, cmd, curFrame, frameData.profile); + if (perform) { + PerformRenderPass(step, cmd, curFrame, frameData.profile); + } else { + frameData.skipSwap = true; + } break; + } case VKRStepType::COPY: PerformCopy(step, cmd); break; diff --git a/Common/GPU/Vulkan/VulkanRenderManager.cpp b/Common/GPU/Vulkan/VulkanRenderManager.cpp index 342f7532fd..577f44bfa1 100644 --- a/Common/GPU/Vulkan/VulkanRenderManager.cpp +++ b/Common/GPU/Vulkan/VulkanRenderManager.cpp @@ -316,24 +316,22 @@ VulkanRenderManager::VulkanRenderManager(VulkanContext *vulkan, bool useThread, } bool VulkanRenderManager::CreateBackbuffers() { - if (!vulkan_->GetSwapchain()) { + if (!vulkan_->IsSwapchainInited()) { ERROR_LOG(Log::G3D, "No swapchain - can't create backbuffers"); return false; } VkCommandBuffer cmdInit = GetInitCmd(); - if (!CreateSwapchain(cmdInit, &postInitBarrier_, frameDataShared_)) { - return false; + if (vulkan_->HasRealSwapchain()) { + if (!CreateSwapchainViewsAndDepth(cmdInit, &postInitBarrier_, frameDataShared_)) { + return false; + } } curWidthRaw_ = -1; curHeightRaw_ = -1; - if (HasBackbuffers()) { - VLOG("Backbuffers Created"); - } - if (newInflightFrames_ != -1) { INFO_LOG(Log::G3D, "Updating inflight frames to %d", newInflightFrames_); vulkan_->UpdateInflightFrames(newInflightFrames_); @@ -348,13 +346,11 @@ bool VulkanRenderManager::CreateBackbuffers() { } // Start the thread(s). - if (HasBackbuffers()) { - StartThreads(); - } + StartThreads(); return true; } -bool VulkanRenderManager::CreateSwapchain(VkCommandBuffer cmdInit, VulkanBarrierBatch *barriers, FrameDataShared &frameDataShared) { +bool VulkanRenderManager::CreateSwapchainViewsAndDepth(VkCommandBuffer cmdInit, VulkanBarrierBatch *barriers, FrameDataShared &frameDataShared) { VkResult res = vkGetSwapchainImagesKHR(vulkan_->GetDevice(), vulkan_->GetSwapchain(), &frameDataShared.swapchainImageCount_, nullptr); _dbg_assert_(res == VK_SUCCESS); @@ -487,6 +483,7 @@ void VulkanRenderManager::DestroyBackbuffers() { vkDestroySemaphore(vulkan_->GetDevice(), image.renderingCompleteSemaphore, nullptr); } frameDataShared_.swapchainImages_.clear(); + frameDataShared_.swapchainImageCount_ = 0; queueRunner_.DestroyBackBuffers(); } @@ -1580,7 +1577,9 @@ void VulkanRenderManager::Run(VKRRenderThreadTask &task) { } } else { // We only get here if vkAcquireNextImage returned VK_ERROR_OUT_OF_DATE. - outOfDateFrames_++; + if (vulkan_->HasRealSwapchain()) { + outOfDateFrames_++; + } frameData.skipSwap = false; } return; diff --git a/Common/GPU/Vulkan/VulkanRenderManager.h b/Common/GPU/Vulkan/VulkanRenderManager.h index 8b7facdc31..ce5ffad52b 100644 --- a/Common/GPU/Vulkan/VulkanRenderManager.h +++ b/Common/GPU/Vulkan/VulkanRenderManager.h @@ -561,7 +561,7 @@ private: void FlushDescriptors(int frame); void SanityCheckPassesOnAdd(); - bool CreateSwapchain(VkCommandBuffer cmdInit, VulkanBarrierBatch *barriers, FrameDataShared &frameDataShared); + bool CreateSwapchainViewsAndDepth(VkCommandBuffer cmdInit, VulkanBarrierBatch *barriers, FrameDataShared &frameDataShared); FrameDataShared frameDataShared_; diff --git a/Common/GraphicsContext.h b/Common/GraphicsContext.h index a0ea7356c6..5df4ebcdc4 100644 --- a/Common/GraphicsContext.h +++ b/Common/GraphicsContext.h @@ -21,6 +21,7 @@ public: virtual void Resume() {} virtual void Resize() = 0; + virtual void NotifyWindowRestored() {} // Needs casting to the appropriate type, unfortunately. Should find a better solution.. virtual void *GetAPIContext() { return nullptr; } diff --git a/Common/System/System.h b/Common/System/System.h index a629d05c70..2c81a70452 100644 --- a/Common/System/System.h +++ b/Common/System/System.h @@ -279,6 +279,7 @@ enum class UIMessage { APP_RESUMED, REQUEST_PLAY_SOUND, WINDOW_MINIMIZED, + WINDOW_RESTORED, LOST_FOCUS, GOT_FOCUS, GPU_CONFIG_CHANGED, diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index 19f470093a..dba27333aa 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -1134,6 +1134,11 @@ void NativeFrame(GraphicsContext *graphicsContext) { } for (const auto &item : toProcess) { + // Hack. + if (item.message == UIMessage::WINDOW_RESTORED && graphicsContext) { + graphicsContext->NotifyWindowRestored(); + } + if (HandleGlobalMessage(item.message, item.value)) { // TODO: Add a to-string thingy. VERBOSE_LOG(Log::System, "Handled global message: %d / %s", (int)item.message, item.value.c_str()); diff --git a/Windows/GPU/WindowsVulkanContext.cpp b/Windows/GPU/WindowsVulkanContext.cpp index ffb475b859..d16f1983ff 100644 --- a/Windows/GPU/WindowsVulkanContext.cpp +++ b/Windows/GPU/WindowsVulkanContext.cpp @@ -184,8 +184,11 @@ void WindowsVulkanContext::Resize() { void WindowsVulkanContext::Poll() { // Check for existing swapchain to avoid issues during shutdown. - if (vulkan_->GetSwapchain() && renderManager_->NeedsSwapchainRecreate()) { + if (vulkan_->IsSwapchainInited() && renderManager_->NeedsSwapchainRecreate()) { Resize(); + } else if (vulkan_->IsSwapchainInited() && windowRestored_) { + Resize(); + windowRestored_ = false; } } diff --git a/Windows/GPU/WindowsVulkanContext.h b/Windows/GPU/WindowsVulkanContext.h index f71522c69f..3e0d688a87 100644 --- a/Windows/GPU/WindowsVulkanContext.h +++ b/Windows/GPU/WindowsVulkanContext.h @@ -32,6 +32,10 @@ public: void Resize() override; void Poll() override; + void NotifyWindowRestored() override { + windowRestored_ = true; + } + void *GetAPIContext() override; Draw::DrawContext *GetDrawContext() override { return draw_; } @@ -39,5 +43,6 @@ private: Draw::DrawContext *draw_; VulkanContext *vulkan_ = nullptr; VulkanRenderManager *renderManager_ = nullptr; + bool windowRestored_ = false; }; diff --git a/Windows/MainWindow.cpp b/Windows/MainWindow.cpp index 10a1778099..e762c24363 100644 --- a/Windows/MainWindow.cpp +++ b/Windows/MainWindow.cpp @@ -639,6 +639,7 @@ namespace MainWindow } static bool first = true; + static bool wasMinimized = false; switch (message) { case WM_CREATE: @@ -790,6 +791,10 @@ namespace MainWindow if (hasFocus) { g_InputManager.GainFocus(); } + if (wasMinimized) { + System_PostUIMessage(UIMessage::WINDOW_RESTORED, "true"); + wasMinimized = false; + } break; case SIZE_MINIMIZED: @@ -798,6 +803,7 @@ namespace MainWindow System_PostUIMessage(UIMessage::WINDOW_MINIMIZED, "true"); } g_InputManager.LoseFocus(); + wasMinimized = true; break; default: break;