Skip to content

Commit

Permalink
Examples: Vulkan: Reworked buffer resize handling, fix for Linux/X11. (
Browse files Browse the repository at this point in the history
  • Loading branch information
ocornut committed Aug 7, 2020
1 parent ede8825 commit a65a6ca
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Other Changes:
- Demo: Improved "Layout & Scrolling" -> "Child Windows" section.
- Style Editor: Added preview of circle auto-tessellation when editing the corresponding value.
- Backends: OpenGL3: Added support for glad2 loader. (#3330) [@moritz-h]
- Examples: Vulkan: Reworked buffer resize handling, fix for Linux/X11. (#3390, #2626) [@RoryO]
- Examples: Vulkan: Fixed GLFW+Vulkan and SDL+Vulkan clear color not being set. (#3390) [@RoryO]


Expand Down
18 changes: 8 additions & 10 deletions examples/example_glfw_vulkan/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ static void FrameRender(ImGui_ImplVulkanH_Window* wd, ImDrawData* draw_data)
}
}

static void FramePresent(ImGui_ImplVulkanH_Window* wd)
static void FramePresent(ImGui_ImplVulkanH_Window* wd, GLFWwindow* window)
{
VkSemaphore render_complete_semaphore = wd->FrameSemaphores[wd->SemaphoreIndex].RenderCompleteSemaphore;
VkPresentInfoKHR info = {};
Expand All @@ -321,6 +321,12 @@ static void FramePresent(ImGui_ImplVulkanH_Window* wd)
info.pSwapchains = &wd->Swapchain;
info.pImageIndices = &wd->FrameIndex;
VkResult err = vkQueuePresentKHR(g_Queue, &info);
if (err == VK_ERROR_OUT_OF_DATE_KHR)
{
glfwGetFramebufferSize(window, &g_SwapChainResizeWidth, &g_SwapChainResizeHeight);
g_SwapChainRebuild = true;
return;
}
check_vk_result(err);
wd->SemaphoreIndex = (wd->SemaphoreIndex + 1) % wd->ImageCount; // Now we can use the next set of semaphores
}
Expand All @@ -330,13 +336,6 @@ static void glfw_error_callback(int error, const char* description)
fprintf(stderr, "Glfw Error %d: %s\n", error, description);
}

static void glfw_resize_callback(GLFWwindow*, int w, int h)
{
g_SwapChainRebuild = true;
g_SwapChainResizeWidth = w;
g_SwapChainResizeHeight = h;
}

int main(int, char**)
{
// Setup GLFW window
Expand Down Expand Up @@ -365,7 +364,6 @@ int main(int, char**)
// Create Framebuffers
int w, h;
glfwGetFramebufferSize(window, &w, &h);
glfwSetFramebufferSizeCallback(window, glfw_resize_callback);
ImGui_ImplVulkanH_Window* wd = &g_MainWindowData;
SetupVulkanWindow(wd, surface, w, h);

Expand Down Expand Up @@ -515,7 +513,7 @@ int main(int, char**)
{
memcpy(&wd->ClearValue.color.float32[0], &clear_color, 4 * sizeof(float));
FrameRender(wd, draw_data);
FramePresent(wd);
FramePresent(wd, window);
}
}

Expand Down
19 changes: 8 additions & 11 deletions examples/example_sdl_vulkan/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ static void FrameRender(ImGui_ImplVulkanH_Window* wd, ImDrawData* draw_data)
}
}

static void FramePresent(ImGui_ImplVulkanH_Window* wd)
static void FramePresent(ImGui_ImplVulkanH_Window* wd, SDL_Window* window)
{
VkSemaphore render_complete_semaphore = wd->FrameSemaphores[wd->SemaphoreIndex].RenderCompleteSemaphore;
VkPresentInfoKHR info = {};
Expand All @@ -313,6 +313,12 @@ static void FramePresent(ImGui_ImplVulkanH_Window* wd)
info.pSwapchains = &wd->Swapchain;
info.pImageIndices = &wd->FrameIndex;
VkResult err = vkQueuePresentKHR(g_Queue, &info);
if (err == VK_ERROR_OUT_OF_DATE_KHR)
{
SDL_GetWindowSize(window, &g_SwapChainResizeWidth, &g_SwapChainResizeHeight);
g_SwapChainRebuild = true;
return;
}
check_vk_result(err);
wd->SemaphoreIndex = (wd->SemaphoreIndex + 1) % wd->ImageCount; // Now we can use the next set of semaphores
}
Expand Down Expand Up @@ -445,15 +451,6 @@ int main(int, char**)
ImGui_ImplSDL2_ProcessEvent(&event);
if (event.type == SDL_QUIT)
done = true;
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESIZED && event.window.windowID == SDL_GetWindowID(window))
{
// Note: your own application may rely on SDL_WINDOWEVENT_MINIMIZED/SDL_WINDOWEVENT_RESTORED to skip updating all-together.
// Here ImGui_ImplSDL2_NewFrame() will set io.DisplaySize to zero which will disable rendering but let application run.
// Please note that you can't Present into a minimized window.
g_SwapChainResizeWidth = (int)event.window.data1;
g_SwapChainResizeHeight = (int)event.window.data2;
g_SwapChainRebuild = true;
}
}

// Resize swap chain?
Expand Down Expand Up @@ -515,7 +512,7 @@ int main(int, char**)
{
memcpy(&wd->ClearValue.color.float32[0], &clear_color, 4 * sizeof(float));
FrameRender(wd, draw_data);
FramePresent(wd);
FramePresent(wd, window);
}
}

Expand Down

0 comments on commit a65a6ca

Please sign in to comment.