Merge pull request #20643 from hrydgard/windows-minimize-vulkan

Windows/Vulkan: Correctly handle minimizing and restoring the window
This commit is contained in:
Henrik Rydgård authored and GitHub committed 2025-07-18 21:34:38 +02:00
commit 28d2bd5b0d
11 files changed
+86 -44

No files matched your search

+11
View File
@@ -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;
}
+8 -9
View File
@@ -232,9 +232,6 @@ public:
VkFence CreateFence(bool presignalled);
bool CreateShaderModule(const std::vector<uint32_t> &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_;
+33 -21
View File
@@ -309,37 +309,49 @@ void VulkanQueueRunner::RunSteps(std::vector<VKRStep *> &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;
+11 -12
View File
@@ -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;
+1 -1
View File
@@ -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_;
+1
View File
@@ -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; }
+1
View File
@@ -279,6 +279,7 @@ enum class UIMessage {
APP_RESUMED,
REQUEST_PLAY_SOUND,
WINDOW_MINIMIZED,
WINDOW_RESTORED,
LOST_FOCUS,
GOT_FOCUS,
GPU_CONFIG_CHANGED,
+5
View File
@@ -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());
+4 -1
View File
@@ -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;
}
}
+5
View File
@@ -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;
};
+6
View File
@@ -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;