Examples: Vulkan: Reworked buffer resize handling, amend df89a16d (#3390, #2626)

This commit is contained in:
ocornut 2020-09-02 12:43:05 +02:00
parent 9a9ee7f813
commit b73305be11
2 changed files with 38 additions and 20 deletions

View File

@ -43,8 +43,6 @@ static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE;
static ImGui_ImplVulkanH_Window g_MainWindowData; static ImGui_ImplVulkanH_Window g_MainWindowData;
static int g_MinImageCount = 2; static int g_MinImageCount = 2;
static bool g_SwapChainRebuild = false; static bool g_SwapChainRebuild = false;
static int g_SwapChainResizeWidth = 0;
static int g_SwapChainResizeHeight = 0;
static void check_vk_result(VkResult err) static void check_vk_result(VkResult err)
{ {
@ -255,6 +253,11 @@ static void FrameRender(ImGui_ImplVulkanH_Window* wd, ImDrawData* draw_data)
VkSemaphore image_acquired_semaphore = wd->FrameSemaphores[wd->SemaphoreIndex].ImageAcquiredSemaphore; VkSemaphore image_acquired_semaphore = wd->FrameSemaphores[wd->SemaphoreIndex].ImageAcquiredSemaphore;
VkSemaphore render_complete_semaphore = wd->FrameSemaphores[wd->SemaphoreIndex].RenderCompleteSemaphore; VkSemaphore render_complete_semaphore = wd->FrameSemaphores[wd->SemaphoreIndex].RenderCompleteSemaphore;
err = vkAcquireNextImageKHR(g_Device, wd->Swapchain, UINT64_MAX, image_acquired_semaphore, VK_NULL_HANDLE, &wd->FrameIndex); err = vkAcquireNextImageKHR(g_Device, wd->Swapchain, UINT64_MAX, image_acquired_semaphore, VK_NULL_HANDLE, &wd->FrameIndex);
if (err == VK_ERROR_OUT_OF_DATE_KHR)
{
g_SwapChainRebuild = true;
return;
}
check_vk_result(err); check_vk_result(err);
ImGui_ImplVulkanH_Frame* fd = &wd->Frames[wd->FrameIndex]; ImGui_ImplVulkanH_Frame* fd = &wd->Frames[wd->FrameIndex];
@ -310,8 +313,10 @@ static void FrameRender(ImGui_ImplVulkanH_Window* wd, ImDrawData* draw_data)
} }
} }
static void FramePresent(ImGui_ImplVulkanH_Window* wd, GLFWwindow* window) static void FramePresent(ImGui_ImplVulkanH_Window* wd)
{ {
if (g_SwapChainRebuild)
return;
VkSemaphore render_complete_semaphore = wd->FrameSemaphores[wd->SemaphoreIndex].RenderCompleteSemaphore; VkSemaphore render_complete_semaphore = wd->FrameSemaphores[wd->SemaphoreIndex].RenderCompleteSemaphore;
VkPresentInfoKHR info = {}; VkPresentInfoKHR info = {};
info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
@ -323,7 +328,6 @@ static void FramePresent(ImGui_ImplVulkanH_Window* wd, GLFWwindow* window)
VkResult err = vkQueuePresentKHR(g_Queue, &info); VkResult err = vkQueuePresentKHR(g_Queue, &info);
if (err == VK_ERROR_OUT_OF_DATE_KHR) if (err == VK_ERROR_OUT_OF_DATE_KHR)
{ {
glfwGetFramebufferSize(window, &g_SwapChainResizeWidth, &g_SwapChainResizeHeight);
g_SwapChainRebuild = true; g_SwapChainRebuild = true;
return; return;
} }
@ -455,12 +459,17 @@ int main(int, char**)
glfwPollEvents(); glfwPollEvents();
// Resize swap chain? // Resize swap chain?
if (g_SwapChainRebuild && g_SwapChainResizeWidth > 0 && g_SwapChainResizeHeight > 0) if (g_SwapChainRebuild)
{
int width, height;
glfwGetFramebufferSize(window, &width, &height);
if (width > 0 && height > 0)
{ {
g_SwapChainRebuild = false;
ImGui_ImplVulkan_SetMinImageCount(g_MinImageCount); ImGui_ImplVulkan_SetMinImageCount(g_MinImageCount);
ImGui_ImplVulkanH_CreateOrResizeWindow(g_Instance, g_PhysicalDevice, g_Device, &g_MainWindowData, g_QueueFamily, g_Allocator, g_SwapChainResizeWidth, g_SwapChainResizeHeight, g_MinImageCount); ImGui_ImplVulkanH_CreateOrResizeWindow(g_Instance, g_PhysicalDevice, g_Device, &g_MainWindowData, g_QueueFamily, g_Allocator, width, height, g_MinImageCount);
g_MainWindowData.FrameIndex = 0; g_MainWindowData.FrameIndex = 0;
g_SwapChainRebuild = false;
}
} }
// Start the Dear ImGui frame // Start the Dear ImGui frame
@ -513,7 +522,7 @@ int main(int, char**)
{ {
memcpy(&wd->ClearValue.color.float32[0], &clear_color, 4 * sizeof(float)); memcpy(&wd->ClearValue.color.float32[0], &clear_color, 4 * sizeof(float));
FrameRender(wd, draw_data); FrameRender(wd, draw_data);
FramePresent(wd, window); FramePresent(wd);
} }
} }

View File

@ -35,8 +35,6 @@ static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE;
static ImGui_ImplVulkanH_Window g_MainWindowData; static ImGui_ImplVulkanH_Window g_MainWindowData;
static uint32_t g_MinImageCount = 2; static uint32_t g_MinImageCount = 2;
static bool g_SwapChainRebuild = false; static bool g_SwapChainRebuild = false;
static int g_SwapChainResizeWidth = 0;
static int g_SwapChainResizeHeight = 0;
static void check_vk_result(VkResult err) static void check_vk_result(VkResult err)
{ {
@ -247,6 +245,11 @@ static void FrameRender(ImGui_ImplVulkanH_Window* wd, ImDrawData* draw_data)
VkSemaphore image_acquired_semaphore = wd->FrameSemaphores[wd->SemaphoreIndex].ImageAcquiredSemaphore; VkSemaphore image_acquired_semaphore = wd->FrameSemaphores[wd->SemaphoreIndex].ImageAcquiredSemaphore;
VkSemaphore render_complete_semaphore = wd->FrameSemaphores[wd->SemaphoreIndex].RenderCompleteSemaphore; VkSemaphore render_complete_semaphore = wd->FrameSemaphores[wd->SemaphoreIndex].RenderCompleteSemaphore;
err = vkAcquireNextImageKHR(g_Device, wd->Swapchain, UINT64_MAX, image_acquired_semaphore, VK_NULL_HANDLE, &wd->FrameIndex); err = vkAcquireNextImageKHR(g_Device, wd->Swapchain, UINT64_MAX, image_acquired_semaphore, VK_NULL_HANDLE, &wd->FrameIndex);
if (err == VK_ERROR_OUT_OF_DATE_KHR)
{
g_SwapChainRebuild = true;
return;
}
check_vk_result(err); check_vk_result(err);
ImGui_ImplVulkanH_Frame* fd = &wd->Frames[wd->FrameIndex]; ImGui_ImplVulkanH_Frame* fd = &wd->Frames[wd->FrameIndex];
@ -302,8 +305,10 @@ static void FrameRender(ImGui_ImplVulkanH_Window* wd, ImDrawData* draw_data)
} }
} }
static void FramePresent(ImGui_ImplVulkanH_Window* wd, SDL_Window* window) static void FramePresent(ImGui_ImplVulkanH_Window* wd)
{ {
if (g_SwapChainRebuild)
return;
VkSemaphore render_complete_semaphore = wd->FrameSemaphores[wd->SemaphoreIndex].RenderCompleteSemaphore; VkSemaphore render_complete_semaphore = wd->FrameSemaphores[wd->SemaphoreIndex].RenderCompleteSemaphore;
VkPresentInfoKHR info = {}; VkPresentInfoKHR info = {};
info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
@ -315,7 +320,6 @@ static void FramePresent(ImGui_ImplVulkanH_Window* wd, SDL_Window* window)
VkResult err = vkQueuePresentKHR(g_Queue, &info); VkResult err = vkQueuePresentKHR(g_Queue, &info);
if (err == VK_ERROR_OUT_OF_DATE_KHR) if (err == VK_ERROR_OUT_OF_DATE_KHR)
{ {
SDL_GetWindowSize(window, &g_SwapChainResizeWidth, &g_SwapChainResizeHeight);
g_SwapChainRebuild = true; g_SwapChainRebuild = true;
return; return;
} }
@ -454,12 +458,17 @@ int main(int, char**)
} }
// Resize swap chain? // Resize swap chain?
if (g_SwapChainRebuild && g_SwapChainResizeWidth > 0 && g_SwapChainResizeHeight > 0) if (g_SwapChainRebuild)
{
int width, height;
SDL_GetWindowSize(window, &width, &height);
if (width > 0 && height > 0)
{ {
g_SwapChainRebuild = false;
ImGui_ImplVulkan_SetMinImageCount(g_MinImageCount); ImGui_ImplVulkan_SetMinImageCount(g_MinImageCount);
ImGui_ImplVulkanH_CreateOrResizeWindow(g_Instance, g_PhysicalDevice, g_Device, &g_MainWindowData, g_QueueFamily, g_Allocator, g_SwapChainResizeWidth, g_SwapChainResizeHeight, g_MinImageCount); ImGui_ImplVulkanH_CreateOrResizeWindow(g_Instance, g_PhysicalDevice, g_Device, &g_MainWindowData, g_QueueFamily, g_Allocator, width, height, g_MinImageCount);
g_MainWindowData.FrameIndex = 0; g_MainWindowData.FrameIndex = 0;
g_SwapChainRebuild = false;
}
} }
// Start the Dear ImGui frame // Start the Dear ImGui frame
@ -512,7 +521,7 @@ int main(int, char**)
{ {
memcpy(&wd->ClearValue.color.float32[0], &clear_color, 4 * sizeof(float)); memcpy(&wd->ClearValue.color.float32[0], &clear_color, 4 * sizeof(float));
FrameRender(wd, draw_data); FrameRender(wd, draw_data);
FramePresent(wd, window); FramePresent(wd);
} }
} }