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 authored and RoryO committed Aug 18, 2020
1 parent ac30911 commit aef0254
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 29 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ Other Changes:
- Backends: OpenGL3: Added support for glad2 loader. (#3330) [@moritz-h]
- Backends: Allegro 5: Fixed horizontal scrolling direction with mouse wheel / touch pads (it seems
like Allegro 5 reports it differently from GLFW and SDL). (#3394, #2424, #1463) [@nobody-special666]
- 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]
- CI: Emscripten has stopped their support for their fastcomp backend, switching to latest sdk [@Xipiryon]

Expand Down
29 changes: 13 additions & 16 deletions examples/example_glfw_vulkan/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE;

static ImGui_ImplVulkanH_Window g_MainWindowData;
static int g_MinImageCount = 2;
static GLFWwindow* g_Window;
static bool g_SwapChainRebuild = false;
static int g_SwapChainResizeWidth = 0;
static int g_SwapChainResizeHeight = 0;
Expand Down Expand Up @@ -311,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 @@ -322,15 +321,13 @@ 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(g_Window, &g_SwapChainResizeWidth, &g_SwapChainResizeHeight);
if (err == VK_ERROR_OUT_OF_DATE_KHR)
{
glfwGetFramebufferSize(window, &g_SwapChainResizeWidth, &g_SwapChainResizeHeight);
g_SwapChainRebuild = true;
return;
}
else
{
check_vk_result(err);
}
check_vk_result(err);
wd->SemaphoreIndex = (wd->SemaphoreIndex + 1) % wd->ImageCount; // Now we can use the next set of semaphores
}

Expand All @@ -347,7 +344,7 @@ int main(int, char**)
return 1;

glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
g_Window = glfwCreateWindow(1280, 720, "Dear ImGui GLFW+Vulkan example", NULL, NULL);
GLFWwindow *window = glfwCreateWindow(1280, 720, "Dear ImGui GLFW+Vulkan example", NULL, NULL);

// Setup Vulkan
if (!glfwVulkanSupported())
Expand All @@ -361,12 +358,12 @@ int main(int, char**)

// Create Window Surface
VkSurfaceKHR surface;
VkResult err = glfwCreateWindowSurface(g_Instance, g_Window, g_Allocator, &surface);
VkResult err = glfwCreateWindowSurface(g_Instance, window, g_Allocator, &surface);
check_vk_result(err);

// Create Framebuffers
int w, h;
glfwGetFramebufferSize(g_Window, &w, &h);
glfwGetFramebufferSize(window, &w, &h);
ImGui_ImplVulkanH_Window* wd = &g_MainWindowData;
SetupVulkanWindow(wd, surface, w, h);

Expand Down Expand Up @@ -394,7 +391,7 @@ int main(int, char**)
}

// Setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForVulkan(g_Window, true);
ImGui_ImplGlfw_InitForVulkan(window, true);
ImGui_ImplVulkan_InitInfo init_info = {};
init_info.Instance = g_Instance;
init_info.PhysicalDevice = g_PhysicalDevice;
Expand Down Expand Up @@ -464,7 +461,7 @@ int main(int, char**)
wd->ClearValue.color.float32[3] = clear_color.w;

// Main loop
while (!glfwWindowShouldClose(g_Window))
while (!glfwWindowShouldClose(window))
{
// Poll and handle events (inputs, window resize, etc.)
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
Expand Down Expand Up @@ -547,9 +544,9 @@ int main(int, char**)

// Present Main Platform Window
if (!main_is_minimized)
FramePresent(wd);
}
FramePresent(wd, window);

}
// Cleanup
err = vkDeviceWaitIdle(g_Device);
check_vk_result(err);
Expand All @@ -560,7 +557,7 @@ int main(int, char**)
CleanupVulkanWindow();
CleanupVulkan();

glfwDestroyWindow(g_Window);
glfwDestroyWindow(window);
glfwTerminate();

return 0;
Expand Down
25 changes: 12 additions & 13 deletions examples/example_sdl_vulkan/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ static uint32_t g_MinImageCount = 2;
static bool g_SwapChainRebuild = false;
static int g_SwapChainResizeWidth = 0;
static int g_SwapChainResizeHeight = 0;
static SDL_Window* g_Window;

static void check_vk_result(VkResult err)
{
Expand Down Expand Up @@ -303,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 @@ -314,9 +313,9 @@ 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)
if (err == VK_ERROR_OUT_OF_DATE_KHR)
{
SDL_GetWindowSize(g_Window, &g_SwapChainResizeWidth, &g_SwapChainResizeHeight);
SDL_GetWindowSize(window, &g_SwapChainResizeWidth, &g_SwapChainResizeHeight);
g_SwapChainRebuild = true;
return;
}
Expand All @@ -335,28 +334,28 @@ int main(int, char**)

// Setup window
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
g_Window = SDL_CreateWindow("Dear ImGui SDL2+Vulkan example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);
SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+Vulkan example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);

// Setup Vulkan
uint32_t extensions_count = 0;
SDL_Vulkan_GetInstanceExtensions(g_Window, &extensions_count, NULL);
SDL_Vulkan_GetInstanceExtensions(window, &extensions_count, NULL);
const char** extensions = new const char*[extensions_count];
SDL_Vulkan_GetInstanceExtensions(g_Window, &extensions_count, extensions);
SDL_Vulkan_GetInstanceExtensions(window, &extensions_count, extensions);
SetupVulkan(extensions, extensions_count);
delete[] extensions;

// Create Window Surface
VkSurfaceKHR surface;
VkResult err;
if (SDL_Vulkan_CreateSurface(g_Window, g_Instance, &surface) == 0)
if (SDL_Vulkan_CreateSurface(window, g_Instance, &surface) == 0)
{
printf("Failed to create Vulkan surface.\n");
return 1;
}

// Create Framebuffers
int w, h;
SDL_GetWindowSize(g_Window, &w, &h);
SDL_GetWindowSize(window, &w, &h);
ImGui_ImplVulkanH_Window* wd = &g_MainWindowData;
SetupVulkanWindow(wd, surface, w, h);

Expand Down Expand Up @@ -384,7 +383,7 @@ int main(int, char**)
}

// Setup Platform/Renderer bindings
ImGui_ImplSDL2_InitForVulkan(g_Window);
ImGui_ImplSDL2_InitForVulkan(window);
ImGui_ImplVulkan_InitInfo init_info = {};
init_info.Instance = g_Instance;
init_info.PhysicalDevice = g_PhysicalDevice;
Expand Down Expand Up @@ -481,7 +480,7 @@ int main(int, char**)

// Start the Dear ImGui frame
ImGui_ImplVulkan_NewFrame();
ImGui_ImplSDL2_NewFrame(g_Window);
ImGui_ImplSDL2_NewFrame(window);
ImGui::NewFrame();

// 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
Expand Down Expand Up @@ -544,7 +543,7 @@ int main(int, char**)

// Present Main Platform Window
if (!main_is_minimized)
FramePresent(wd);
FramePresent(wd, window);
}

// Cleanup
Expand All @@ -557,7 +556,7 @@ int main(int, char**)
CleanupVulkanWindow();
CleanupVulkan();

SDL_DestroyWindow(g_Window);
SDL_DestroyWindow(window);
SDL_Quit();

return 0;
Expand Down

0 comments on commit aef0254

Please sign in to comment.