From 4d53a61ed2fd8923bcc54c6562fa6c24da3e5836 Mon Sep 17 00:00:00 2001 From: InsideBSI Date: Sat, 20 Apr 2024 19:30:49 +0200 Subject: [PATCH 1/3] fix for https://github.com/ocornut/imgui/issues/7508 --- backends/imgui_impl_vulkan.cpp | 16 +++++++++++++--- backends/imgui_impl_vulkan.h | 2 ++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/backends/imgui_impl_vulkan.cpp b/backends/imgui_impl_vulkan.cpp index e7f7c8d82559..15923f99c902 100644 --- a/backends/imgui_impl_vulkan.cpp +++ b/backends/imgui_impl_vulkan.cpp @@ -1731,12 +1731,17 @@ static void ImGui_ImplVulkan_RenderWindow(ImGuiViewport* viewport, void*) { { err = vkAcquireNextImageKHR(v->Device, wd->Swapchain, UINT64_MAX, fsd->ImageAcquiredSemaphore, VK_NULL_HANDLE, &wd->FrameIndex); - check_vk_result(err); + if (err == VK_ERROR_OUT_OF_DATE_KHR) { + ImGui_ImplVulkanH_CreateOrResizeWindow(v->Instance, v->PhysicalDevice, v->Device, &vd->Window, v->QueueFamily, v->Allocator, (int)viewport->Size.x, (int)viewport->Size.y, v->MinImageCount); + wd->CanPresent = false; + return; + } else + check_vk_result(err); fd = &wd->Frames[wd->FrameIndex]; } for (;;) { - err = vkWaitForFences(v->Device, 1, &fd->Fence, VK_TRUE, 100); + err = vkWaitForFences(v->Device, 1, &fd->Fence, VK_TRUE, UINT64_MAX); if (err == VK_SUCCESS) break; if (err == VK_TIMEOUT) continue; check_vk_result(err); @@ -1858,6 +1863,11 @@ static void ImGui_ImplVulkan_SwapBuffers(ImGuiViewport* viewport, void*) ImGui_ImplVulkanH_Window* wd = &vd->Window; ImGui_ImplVulkan_InitInfo* v = &bd->VulkanInitInfo; + if (!wd->CanPresent) { + wd->CanPresent = true; + return; + } + VkResult err; uint32_t present_index = wd->FrameIndex; @@ -1872,7 +1882,7 @@ static void ImGui_ImplVulkan_SwapBuffers(ImGuiViewport* viewport, void*) err = vkQueuePresentKHR(v->Queue, &info); if (err == VK_ERROR_OUT_OF_DATE_KHR || err == VK_SUBOPTIMAL_KHR) ImGui_ImplVulkanH_CreateOrResizeWindow(v->Instance, v->PhysicalDevice, v->Device, &vd->Window, v->QueueFamily, v->Allocator, (int)viewport->Size.x, (int)viewport->Size.y, v->MinImageCount); - else + else check_vk_result(err); wd->FrameIndex = (wd->FrameIndex + 1) % wd->ImageCount; // This is for the next vkWaitForFences() diff --git a/backends/imgui_impl_vulkan.h b/backends/imgui_impl_vulkan.h index e1bbb4e224d4..8d8580013295 100644 --- a/backends/imgui_impl_vulkan.h +++ b/backends/imgui_impl_vulkan.h @@ -170,6 +170,7 @@ struct ImGui_ImplVulkanH_Window VkPipeline Pipeline; // The window pipeline may uses a different VkRenderPass than the one passed in ImGui_ImplVulkan_InitInfo bool UseDynamicRendering; bool ClearEnable; + bool CanPresent; VkClearValue ClearValue; uint32_t FrameIndex; // Current frame being rendered to (0 <= FrameIndex < FrameInFlightCount) uint32_t ImageCount; // Number of simultaneous in-flight frames (returned by vkGetSwapchainImagesKHR, usually derived from min_image_count) @@ -183,6 +184,7 @@ struct ImGui_ImplVulkanH_Window memset((void*)this, 0, sizeof(*this)); PresentMode = (VkPresentModeKHR)~0; // Ensure we get an error if user doesn't set this. ClearEnable = true; + CanPresent = true; } }; From 4d97ce3e43d5c7203c392d910a2aa473369b2a06 Mon Sep 17 00:00:00 2001 From: InsideBSI Date: Sat, 20 Apr 2024 19:37:25 +0200 Subject: [PATCH 2/3] mb this can stay as 100 --- backends/imgui_impl_vulkan.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backends/imgui_impl_vulkan.cpp b/backends/imgui_impl_vulkan.cpp index 15923f99c902..168097da35bd 100644 --- a/backends/imgui_impl_vulkan.cpp +++ b/backends/imgui_impl_vulkan.cpp @@ -1741,7 +1741,7 @@ static void ImGui_ImplVulkan_RenderWindow(ImGuiViewport* viewport, void*) } for (;;) { - err = vkWaitForFences(v->Device, 1, &fd->Fence, VK_TRUE, UINT64_MAX); + err = vkWaitForFences(v->Device, 1, &fd->Fence, VK_TRUE, 100); if (err == VK_SUCCESS) break; if (err == VK_TIMEOUT) continue; check_vk_result(err); From c8a9b5a2682280997074ba36a3d39993bfba93e8 Mon Sep 17 00:00:00 2001 From: InsideBSI Date: Sat, 20 Apr 2024 20:56:56 +0200 Subject: [PATCH 3/3] this should also probably return --- backends/imgui_impl_vulkan.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/backends/imgui_impl_vulkan.cpp b/backends/imgui_impl_vulkan.cpp index 168097da35bd..0cb069a6b2ae 100644 --- a/backends/imgui_impl_vulkan.cpp +++ b/backends/imgui_impl_vulkan.cpp @@ -1880,8 +1880,10 @@ static void ImGui_ImplVulkan_SwapBuffers(ImGuiViewport* viewport, void*) info.pSwapchains = &wd->Swapchain; info.pImageIndices = &present_index; err = vkQueuePresentKHR(v->Queue, &info); - if (err == VK_ERROR_OUT_OF_DATE_KHR || err == VK_SUBOPTIMAL_KHR) + if (err == VK_ERROR_OUT_OF_DATE_KHR || err == VK_SUBOPTIMAL_KHR) { ImGui_ImplVulkanH_CreateOrResizeWindow(v->Instance, v->PhysicalDevice, v->Device, &vd->Window, v->QueueFamily, v->Allocator, (int)viewport->Size.x, (int)viewport->Size.y, v->MinImageCount); + return; + } else check_vk_result(err);