From b88a3b271123f08fbfc53f5a746a4583b086224b Mon Sep 17 00:00:00 2001 From: MindSpunk Date: Sun, 9 Sep 2018 16:04:44 +1000 Subject: [PATCH 01/19] Examples: Vulkan: Added calls to supports runtime changing back buffer count. (#2071) --- examples/example_glfw_vulkan/main.cpp | 41 ++++++++++--- examples/example_sdl_vulkan/main.cpp | 48 ++++++++++++++- examples/imgui_impl_vulkan.cpp | 87 ++++++++++++++++++++------- examples/imgui_impl_vulkan.h | 10 ++- 4 files changed, 150 insertions(+), 36 deletions(-) diff --git a/examples/example_glfw_vulkan/main.cpp b/examples/example_glfw_vulkan/main.cpp index 0a9b8ccc..6459a7aa 100644 --- a/examples/example_glfw_vulkan/main.cpp +++ b/examples/example_glfw_vulkan/main.cpp @@ -23,6 +23,7 @@ #define IMGUI_VULKAN_DEBUG_REPORT #endif +static uint32_t g_MinImageCount = 2; static VkAllocationCallbacks* g_Allocator = NULL; static VkInstance g_Instance = VK_NULL_HANDLE; static VkPhysicalDevice g_PhysicalDevice = VK_NULL_HANDLE; @@ -211,8 +212,8 @@ static void SetupVulkanWindowData(ImGui_ImplVulkanH_WindowData* wd, VkSurfaceKHR //printf("[vulkan] Selected PresentMode = %d\n", wd->PresentMode); // Create SwapChain, RenderPass, Framebuffer, etc. - ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, wd, g_Allocator); - ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, wd, g_Allocator, width, height); + ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, wd, g_Allocator, width, height, g_MinImageCount); + ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, wd, g_Allocator); } static void CleanupVulkan() @@ -373,6 +374,7 @@ int main(int, char**) init_info.DescriptorPool = g_DescriptorPool; init_info.Allocator = g_Allocator; init_info.CheckVkResultFn = check_vk_result; + init_info.QueuedFrames = wd->BackBufferCount; ImGui_ImplVulkan_Init(&init_info, wd->RenderPass); // Load Fonts @@ -433,11 +435,14 @@ int main(int, char**) // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. glfwPollEvents(); - if (g_ResizeWanted) - { - ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, &g_WindowData, g_Allocator, g_ResizeWidth, g_ResizeHeight); - g_ResizeWanted = false; - } + if (g_ResizeWanted) + { + ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, &g_WindowData, g_Allocator, g_ResizeWidth, g_ResizeHeight, g_MinImageCount); + ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, &g_WindowData, g_Allocator); + ImGui_ImplVulkan_SetQueuedFramesCount(g_WindowData.BackBufferCount); + g_WindowData.FrameIndex = 0; + g_ResizeWanted = false; + } // Start the Dear ImGui frame ImGui_ImplVulkan_NewFrame(); @@ -478,6 +483,28 @@ int main(int, char**) ImGui::Text("Hello from another window!"); if (ImGui::Button("Close Me")) show_another_window = false; + + if (ImGui::Button("Increase")) + { + g_MinImageCount++; + g_ResizeWanted = true; + } + + ImGui::SameLine(); + if (ImGui::Button("Decrease")) + { + if (g_MinImageCount != 2) + { + g_MinImageCount--; + g_ResizeWanted = true; + } + } + + ImGui::SameLine(); + ImGui::Text("Back Buffers: %i", g_MinImageCount); + + ImGui::Text("Frame Index %i", wd->FrameIndex); + ImGui::End(); } diff --git a/examples/example_sdl_vulkan/main.cpp b/examples/example_sdl_vulkan/main.cpp index e17a8ab7..b9bcffe9 100644 --- a/examples/example_sdl_vulkan/main.cpp +++ b/examples/example_sdl_vulkan/main.cpp @@ -15,6 +15,8 @@ #define IMGUI_VULKAN_DEBUG_REPORT #endif +static uint32_t g_MinImageCount = 2; +static bool g_PendingSwapchainRebuild = false; static VkAllocationCallbacks* g_Allocator = NULL; static VkInstance g_Instance = VK_NULL_HANDLE; static VkPhysicalDevice g_PhysicalDevice = VK_NULL_HANDLE; @@ -201,8 +203,8 @@ static void SetupVulkanWindowData(ImGui_ImplVulkanH_WindowData* wd, VkSurfaceKHR //printf("[vulkan] Selected PresentMode = %d\n", wd->PresentMode); // Create SwapChain, RenderPass, Framebuffer, etc. - ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, wd, g_Allocator); - ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, wd, g_Allocator, width, height); + ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, wd, g_Allocator, width, height, g_MinImageCount); + ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, wd, g_Allocator); } static void CleanupVulkan() @@ -296,6 +298,20 @@ static void FramePresent(ImGui_ImplVulkanH_WindowData* wd) check_vk_result(err); } +static void RebuildSwapChain(int width, int height) +{ + ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, &g_WindowData, g_Allocator, width, height, g_MinImageCount); + ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, &g_WindowData, g_Allocator); + ImGui_ImplVulkan_SetQueuedFramesCount(g_WindowData.BackBufferCount); + g_WindowData.FrameIndex = 0; + g_PendingSwapchainRebuild = false; +} + +static void RebuildSwapChain() +{ + RebuildSwapChain(g_WindowData.Width, g_WindowData.Height); +} + int main(int, char**) { // Setup SDL @@ -354,6 +370,7 @@ int main(int, char**) init_info.PipelineCache = g_PipelineCache; init_info.DescriptorPool = g_DescriptorPool; init_info.Allocator = g_Allocator; + init_info.QueuedFrames = wd->BackBufferCount; init_info.CheckVkResultFn = check_vk_result; ImGui_ImplVulkan_Init(&init_info, wd->RenderPass); @@ -422,9 +439,12 @@ int main(int, char**) if (event.type == SDL_QUIT) done = true; if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESIZED && event.window.windowID == SDL_GetWindowID(window)) - ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, &g_WindowData, g_Allocator, (int)event.window.data1, (int)event.window.data2); + RebuildSwapChain((int)event.window.data1, (int)event.window.data2); } + if (g_PendingSwapchainRebuild) + RebuildSwapChain(); + // Start the Dear ImGui frame ImGui_ImplVulkan_NewFrame(); ImGui_ImplSDL2_NewFrame(window); @@ -464,6 +484,28 @@ int main(int, char**) ImGui::Text("Hello from another window!"); if (ImGui::Button("Close Me")) show_another_window = false; + + if (ImGui::Button("Increase")) + { + g_MinImageCount++; + g_PendingSwapchainRebuild = true; + } + + ImGui::SameLine(); + if (ImGui::Button("Decrease")) + { + if (g_MinImageCount != 2) + { + g_MinImageCount--; + g_PendingSwapchainRebuild = true; + } + } + + ImGui::SameLine(); + ImGui::Text("Back Buffers: %i", g_MinImageCount); + + ImGui::Text("Frame Index %i", wd->FrameIndex); + ImGui::End(); } diff --git a/examples/imgui_impl_vulkan.cpp b/examples/imgui_impl_vulkan.cpp index 2d497931..833e4d3e 100644 --- a/examples/imgui_impl_vulkan.cpp +++ b/examples/imgui_impl_vulkan.cpp @@ -64,8 +64,8 @@ struct FrameDataForRender VkBuffer VertexBuffer; VkBuffer IndexBuffer; }; -static int g_FrameIndex = 0; -static FrameDataForRender g_FramesDataBuffers[IMGUI_VK_QUEUED_FRAMES] = {}; +static int g_FrameIndex = 0; +static ImVector g_FramesDataBuffers = {}; // Font data static VkSampler g_FontSampler = VK_NULL_HANDLE; @@ -240,7 +240,7 @@ void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer comm VkResult err; FrameDataForRender* fd = &g_FramesDataBuffers[g_FrameIndex]; - g_FrameIndex = (g_FrameIndex + 1) % IM_ARRAYSIZE(g_FramesDataBuffers); + g_FrameIndex = (g_FrameIndex + 1) % g_FramesDataBuffers.size(); // Create the Vertex and Index buffers: size_t vertex_size = draw_data->TotalVtxCount * sizeof(ImDrawVert); @@ -728,15 +728,6 @@ void ImGui_ImplVulkan_InvalidateDeviceObjects() { ImGui_ImplVulkan_InvalidateFontUploadObjects(); - for (int i = 0; i < IM_ARRAYSIZE(g_FramesDataBuffers); i++) - { - FrameDataForRender* fd = &g_FramesDataBuffers[i]; - if (fd->VertexBuffer) { vkDestroyBuffer (g_Device, fd->VertexBuffer, g_Allocator); fd->VertexBuffer = VK_NULL_HANDLE; } - if (fd->VertexBufferMemory) { vkFreeMemory (g_Device, fd->VertexBufferMemory, g_Allocator); fd->VertexBufferMemory = VK_NULL_HANDLE; } - if (fd->IndexBuffer) { vkDestroyBuffer (g_Device, fd->IndexBuffer, g_Allocator); fd->IndexBuffer = VK_NULL_HANDLE; } - if (fd->IndexBufferMemory) { vkFreeMemory (g_Device, fd->IndexBufferMemory, g_Allocator); fd->IndexBufferMemory = VK_NULL_HANDLE; } - } - if (g_FontView) { vkDestroyImageView(g_Device, g_FontView, g_Allocator); g_FontView = VK_NULL_HANDLE; } if (g_FontImage) { vkDestroyImage(g_Device, g_FontImage, g_Allocator); g_FontImage = VK_NULL_HANDLE; } if (g_FontMemory) { vkFreeMemory(g_Device, g_FontMemory, g_Allocator); g_FontMemory = VK_NULL_HANDLE; } @@ -746,6 +737,18 @@ void ImGui_ImplVulkan_InvalidateDeviceObjects() if (g_Pipeline) { vkDestroyPipeline(g_Device, g_Pipeline, g_Allocator); g_Pipeline = VK_NULL_HANDLE; } } +void ImGui_ImplVulkan_InvalidateFrameDeviceObjects() +{ + for (int i = 0; i < g_FramesDataBuffers.size(); i++) + { + FrameDataForRender* fd = &g_FramesDataBuffers[i]; + if (fd->VertexBuffer) { vkDestroyBuffer(g_Device, fd->VertexBuffer, g_Allocator); fd->VertexBuffer = VK_NULL_HANDLE; } + if (fd->VertexBufferMemory) { vkFreeMemory (g_Device, fd->VertexBufferMemory, g_Allocator); fd->VertexBufferMemory = VK_NULL_HANDLE; } + if (fd->IndexBuffer) { vkDestroyBuffer(g_Device, fd->IndexBuffer, g_Allocator); fd->IndexBuffer = VK_NULL_HANDLE; } + if (fd->IndexBufferMemory) { vkFreeMemory (g_Device, fd->IndexBufferMemory, g_Allocator); fd->IndexBufferMemory = VK_NULL_HANDLE; } + } +} + bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass render_pass) { ImGuiIO& io = ImGui::GetIO(); @@ -768,6 +771,11 @@ bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass rend g_DescriptorPool = info->DescriptorPool; g_Allocator = info->Allocator; g_CheckVkResultFn = info->CheckVkResultFn; + g_FramesDataBuffers.resize(info->QueuedFrames); + for (int i = 0; i < g_FramesDataBuffers.size(); i++) + { + g_FramesDataBuffers[i] = FrameDataForRender(); + } ImGui_ImplVulkan_CreateDeviceObjects(); @@ -776,6 +784,7 @@ bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass rend void ImGui_ImplVulkan_Shutdown() { + ImGui_ImplVulkan_InvalidateFrameDeviceObjects(); ImGui_ImplVulkan_InvalidateDeviceObjects(); } @@ -783,6 +792,24 @@ void ImGui_ImplVulkan_NewFrame() { } +void ImGui_ImplVulkan_SetQueuedFramesCount(uint32_t count) +{ + if (count == g_FramesDataBuffers.size()) + { + return; + } + ImGui_ImplVulkan_InvalidateFrameDeviceObjects(); + + uint32_t old_size = g_FramesDataBuffers.size(); + g_FramesDataBuffers.resize(count); + for (uint32_t i = old_size; i < count; i++) + { + + g_FramesDataBuffers[i] = FrameDataForRender(); + } + g_FrameIndex = 0; +} + //------------------------------------------------------------------------- // Internal / Miscellaneous Vulkan Helpers @@ -901,7 +928,7 @@ void ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(VkPhysicalDevice physical_ // Create Command Buffers VkResult err; - for (int i = 0; i < IM_ARRAYSIZE(wd->Frames); i++) + for (int i = 0; i < wd->Frames.size(); i++) { ImGui_ImplVulkanH_FrameData* fd = &wd->Frames[i]; { @@ -951,10 +978,8 @@ int ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(VkPresentModeKHR present_m return 1; } -void ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator, int w, int h) +void ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count) { - uint32_t min_image_count = 2; // FIXME: this should become a function parameter - VkResult err; VkSwapchainKHR old_swapchain = wd->Swapchain; err = vkDeviceWaitIdle(device); @@ -1015,7 +1040,18 @@ void ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(VkPhysicalDevice err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->BackBufferCount, NULL); check_vk_result(err); err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->BackBufferCount, wd->BackBuffer); - check_vk_result(err); + check_vk_result(err); + + for (uint32_t i = 0; i < wd->Frames.size(); i++) + { + ImGui_ImplVulkanH_DestroyFrameData(g_Instance, device, &wd->Frames[i], allocator); + } + uint32_t old_size = wd->Frames.size(); + wd->Frames.resize(wd->BackBufferCount); + for (uint32_t i = 0; i < wd->Frames.size(); i++) + { + wd->Frames[i] = ImGui_ImplVulkanH_FrameData(); + } } if (old_swapchain) vkDestroySwapchainKHR(device, old_swapchain, allocator); @@ -1102,14 +1138,10 @@ void ImGui_ImplVulkanH_DestroyWindowData(VkInstance instance, VkDevice device, I vkDeviceWaitIdle(device); // FIXME: We could wait on the Queue if we had the queue in wd-> (otherwise VulkanH functions can't use globals) //vkQueueWaitIdle(g_Queue); - for (int i = 0; i < IM_ARRAYSIZE(wd->Frames); i++) + for (int i = 0; i < wd->Frames.size(); i++) { ImGui_ImplVulkanH_FrameData* fd = &wd->Frames[i]; - vkDestroyFence(device, fd->Fence, allocator); - vkFreeCommandBuffers(device, fd->CommandPool, 1, &fd->CommandBuffer); - vkDestroyCommandPool(device, fd->CommandPool, allocator); - vkDestroySemaphore(device, fd->ImageAcquiredSemaphore, allocator); - vkDestroySemaphore(device, fd->RenderCompleteSemaphore, allocator); + ImGui_ImplVulkanH_DestroyFrameData(instance, device, fd, allocator); } for (uint32_t i = 0; i < wd->BackBufferCount; i++) { @@ -1121,3 +1153,12 @@ void ImGui_ImplVulkanH_DestroyWindowData(VkInstance instance, VkDevice device, I vkDestroySurfaceKHR(instance, wd->Surface, allocator); *wd = ImGui_ImplVulkanH_WindowData(); } + +void ImGui_ImplVulkanH_DestroyFrameData(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_FrameData* fd, const VkAllocationCallbacks* allocator) +{ + vkDestroyFence(device, fd->Fence, allocator); + vkFreeCommandBuffers(device, fd->CommandPool, 1, &fd->CommandBuffer); + vkDestroyCommandPool(device, fd->CommandPool, allocator); + vkDestroySemaphore(device, fd->ImageAcquiredSemaphore, allocator); + vkDestroySemaphore(device, fd->RenderCompleteSemaphore, allocator); +} diff --git a/examples/imgui_impl_vulkan.h b/examples/imgui_impl_vulkan.h index 8bb35c86..96cf52be 100644 --- a/examples/imgui_impl_vulkan.h +++ b/examples/imgui_impl_vulkan.h @@ -15,7 +15,7 @@ #include -#define IMGUI_VK_QUEUED_FRAMES 2 +//#define IMGUI_VK_QUEUED_FRAMES 2 // Please zero-clear before use. struct ImGui_ImplVulkan_InitInfo @@ -27,6 +27,7 @@ struct ImGui_ImplVulkan_InitInfo VkQueue Queue; VkPipelineCache PipelineCache; VkDescriptorPool DescriptorPool; + int QueuedFrames; const VkAllocationCallbacks* Allocator; void (*CheckVkResultFn)(VkResult err); }; @@ -35,6 +36,7 @@ struct ImGui_ImplVulkan_InitInfo IMGUI_IMPL_API bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass render_pass); IMGUI_IMPL_API void ImGui_ImplVulkan_Shutdown(); IMGUI_IMPL_API void ImGui_ImplVulkan_NewFrame(); +IMGUI_IMPL_API void ImGui_ImplVulkan_SetQueuedFramesCount(uint32_t count); IMGUI_IMPL_API void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer command_buffer); IMGUI_IMPL_API bool ImGui_ImplVulkan_CreateFontsTexture(VkCommandBuffer command_buffer); IMGUI_IMPL_API void ImGui_ImplVulkan_InvalidateFontUploadObjects(); @@ -42,6 +44,7 @@ IMGUI_IMPL_API void ImGui_ImplVulkan_InvalidateFontUploadObjects(); // Called by ImGui_ImplVulkan_Init() might be useful elsewhere. IMGUI_IMPL_API bool ImGui_ImplVulkan_CreateDeviceObjects(); IMGUI_IMPL_API void ImGui_ImplVulkan_InvalidateDeviceObjects(); +IMGUI_IMPL_API void ImGui_ImplVulkan_InvalidateFrameDeviceObjects(); //------------------------------------------------------------------------- @@ -63,8 +66,9 @@ struct ImGui_ImplVulkanH_FrameData; struct ImGui_ImplVulkanH_WindowData; IMGUI_IMPL_API void ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(VkPhysicalDevice physical_device, VkDevice device, uint32_t queue_family, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator); -IMGUI_IMPL_API void ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator, int w, int h); +IMGUI_IMPL_API void ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count); IMGUI_IMPL_API void ImGui_ImplVulkanH_DestroyWindowData(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator); +IMGUI_IMPL_API void ImGui_ImplVulkanH_DestroyFrameData(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_FrameData* fd, const VkAllocationCallbacks* allocator); IMGUI_IMPL_API VkSurfaceFormatKHR ImGui_ImplVulkanH_SelectSurfaceFormat(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkFormat* request_formats, int request_formats_count, VkColorSpaceKHR request_color_space); IMGUI_IMPL_API VkPresentModeKHR ImGui_ImplVulkanH_SelectPresentMode(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkPresentModeKHR* request_modes, int request_modes_count); IMGUI_IMPL_API int ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(VkPresentModeKHR present_mode); @@ -101,7 +105,7 @@ struct ImGui_ImplVulkanH_WindowData VkImageView BackBufferView[16]; VkFramebuffer Framebuffer[16]; uint32_t FrameIndex; - ImGui_ImplVulkanH_FrameData Frames[IMGUI_VK_QUEUED_FRAMES]; + ImVector Frames; IMGUI_IMPL_API ImGui_ImplVulkanH_WindowData(); }; From c7eef99a331595600c69965426ffc7c26837aa5e Mon Sep 17 00:00:00 2001 From: omar Date: Tue, 11 Sep 2018 22:18:24 +0200 Subject: [PATCH 02/19] Examples: Vulkan: Fixed tabs->space, removed extraneous braces and empty lines. (#2071) --- examples/example_glfw_vulkan/main.cpp | 56 +++++++++---------- examples/example_sdl_vulkan/main.cpp | 63 ++++++++++------------ examples/imgui_impl_vulkan.cpp | 77 ++++++++++++--------------- examples/imgui_impl_vulkan.h | 2 +- 4 files changed, 91 insertions(+), 107 deletions(-) diff --git a/examples/example_glfw_vulkan/main.cpp b/examples/example_glfw_vulkan/main.cpp index 6459a7aa..6e25185f 100644 --- a/examples/example_glfw_vulkan/main.cpp +++ b/examples/example_glfw_vulkan/main.cpp @@ -23,7 +23,7 @@ #define IMGUI_VULKAN_DEBUG_REPORT #endif -static uint32_t g_MinImageCount = 2; +static uint32_t g_MinImageCount = 2; static VkAllocationCallbacks* g_Allocator = NULL; static VkInstance g_Instance = VK_NULL_HANDLE; static VkPhysicalDevice g_PhysicalDevice = VK_NULL_HANDLE; @@ -213,7 +213,7 @@ static void SetupVulkanWindowData(ImGui_ImplVulkanH_WindowData* wd, VkSurfaceKHR // Create SwapChain, RenderPass, Framebuffer, etc. ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, wd, g_Allocator, width, height, g_MinImageCount); - ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, wd, g_Allocator); + ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, wd, g_Allocator); } static void CleanupVulkan() @@ -374,7 +374,7 @@ int main(int, char**) init_info.DescriptorPool = g_DescriptorPool; init_info.Allocator = g_Allocator; init_info.CheckVkResultFn = check_vk_result; - init_info.QueuedFrames = wd->BackBufferCount; + init_info.QueuedFrames = wd->BackBufferCount; ImGui_ImplVulkan_Init(&init_info, wd->RenderPass); // Load Fonts @@ -435,14 +435,14 @@ int main(int, char**) // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. glfwPollEvents(); - if (g_ResizeWanted) - { - ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, &g_WindowData, g_Allocator, g_ResizeWidth, g_ResizeHeight, g_MinImageCount); - ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, &g_WindowData, g_Allocator); - ImGui_ImplVulkan_SetQueuedFramesCount(g_WindowData.BackBufferCount); - g_WindowData.FrameIndex = 0; - g_ResizeWanted = false; - } + if (g_ResizeWanted) + { + ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, &g_WindowData, g_Allocator, g_ResizeWidth, g_ResizeHeight, g_MinImageCount); + ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, &g_WindowData, g_Allocator); + ImGui_ImplVulkan_SetQueuedFramesCount(g_WindowData.BackBufferCount); + g_WindowData.FrameIndex = 0; + g_ResizeWanted = false; + } // Start the Dear ImGui frame ImGui_ImplVulkan_NewFrame(); @@ -484,26 +484,26 @@ int main(int, char**) if (ImGui::Button("Close Me")) show_another_window = false; - if (ImGui::Button("Increase")) - { - g_MinImageCount++; - g_ResizeWanted = true; - } + if (ImGui::Button("Increase")) + { + g_MinImageCount++; + g_ResizeWanted = true; + } - ImGui::SameLine(); - if (ImGui::Button("Decrease")) - { - if (g_MinImageCount != 2) - { - g_MinImageCount--; - g_ResizeWanted = true; - } - } + ImGui::SameLine(); + if (ImGui::Button("Decrease")) + { + if (g_MinImageCount != 2) + { + g_MinImageCount--; + g_ResizeWanted = true; + } + } - ImGui::SameLine(); - ImGui::Text("Back Buffers: %i", g_MinImageCount); + ImGui::SameLine(); + ImGui::Text("Back Buffers: %i", g_MinImageCount); - ImGui::Text("Frame Index %i", wd->FrameIndex); + ImGui::Text("Frame Index %i", wd->FrameIndex); ImGui::End(); } diff --git a/examples/example_sdl_vulkan/main.cpp b/examples/example_sdl_vulkan/main.cpp index b9bcffe9..b430f5a0 100644 --- a/examples/example_sdl_vulkan/main.cpp +++ b/examples/example_sdl_vulkan/main.cpp @@ -15,8 +15,8 @@ #define IMGUI_VULKAN_DEBUG_REPORT #endif -static uint32_t g_MinImageCount = 2; -static bool g_PendingSwapchainRebuild = false; +static uint32_t g_MinImageCount = 2; +static bool g_PendingSwapchainRebuild = false; static VkAllocationCallbacks* g_Allocator = NULL; static VkInstance g_Instance = VK_NULL_HANDLE; static VkPhysicalDevice g_PhysicalDevice = VK_NULL_HANDLE; @@ -204,7 +204,7 @@ static void SetupVulkanWindowData(ImGui_ImplVulkanH_WindowData* wd, VkSurfaceKHR // Create SwapChain, RenderPass, Framebuffer, etc. ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, wd, g_Allocator, width, height, g_MinImageCount); - ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, wd, g_Allocator); + ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, wd, g_Allocator); } static void CleanupVulkan() @@ -300,16 +300,11 @@ static void FramePresent(ImGui_ImplVulkanH_WindowData* wd) static void RebuildSwapChain(int width, int height) { - ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, &g_WindowData, g_Allocator, width, height, g_MinImageCount); - ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, &g_WindowData, g_Allocator); - ImGui_ImplVulkan_SetQueuedFramesCount(g_WindowData.BackBufferCount); - g_WindowData.FrameIndex = 0; - g_PendingSwapchainRebuild = false; -} - -static void RebuildSwapChain() -{ - RebuildSwapChain(g_WindowData.Width, g_WindowData.Height); + ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, &g_WindowData, g_Allocator, width, height, g_MinImageCount); + ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, &g_WindowData, g_Allocator); + ImGui_ImplVulkan_SetQueuedFramesCount(g_WindowData.BackBufferCount); + g_WindowData.FrameIndex = 0; + g_PendingSwapchainRebuild = false; } int main(int, char**) @@ -370,7 +365,7 @@ int main(int, char**) init_info.PipelineCache = g_PipelineCache; init_info.DescriptorPool = g_DescriptorPool; init_info.Allocator = g_Allocator; - init_info.QueuedFrames = wd->BackBufferCount; + init_info.QueuedFrames = wd->BackBufferCount; init_info.CheckVkResultFn = check_vk_result; ImGui_ImplVulkan_Init(&init_info, wd->RenderPass); @@ -439,11 +434,11 @@ int main(int, char**) if (event.type == SDL_QUIT) done = true; if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESIZED && event.window.windowID == SDL_GetWindowID(window)) - RebuildSwapChain((int)event.window.data1, (int)event.window.data2); + RebuildSwapChain((int)event.window.data1, (int)event.window.data2); } - if (g_PendingSwapchainRebuild) - RebuildSwapChain(); + if (g_PendingSwapchainRebuild) + RebuildSwapChain(g_WindowData.Width, g_WindowData.Height); // Start the Dear ImGui frame ImGui_ImplVulkan_NewFrame(); @@ -485,26 +480,26 @@ int main(int, char**) if (ImGui::Button("Close Me")) show_another_window = false; - if (ImGui::Button("Increase")) - { - g_MinImageCount++; - g_PendingSwapchainRebuild = true; - } + if (ImGui::Button("Increase")) + { + g_MinImageCount++; + g_PendingSwapchainRebuild = true; + } - ImGui::SameLine(); - if (ImGui::Button("Decrease")) - { - if (g_MinImageCount != 2) - { - g_MinImageCount--; - g_PendingSwapchainRebuild = true; - } - } + ImGui::SameLine(); + if (ImGui::Button("Decrease")) + { + if (g_MinImageCount != 2) + { + g_MinImageCount--; + g_PendingSwapchainRebuild = true; + } + } - ImGui::SameLine(); - ImGui::Text("Back Buffers: %i", g_MinImageCount); + ImGui::SameLine(); + ImGui::Text("Back Buffers: %i", g_MinImageCount); - ImGui::Text("Frame Index %i", wd->FrameIndex); + ImGui::Text("Frame Index %i", wd->FrameIndex); ImGui::End(); } diff --git a/examples/imgui_impl_vulkan.cpp b/examples/imgui_impl_vulkan.cpp index 833e4d3e..a74323ee 100644 --- a/examples/imgui_impl_vulkan.cpp +++ b/examples/imgui_impl_vulkan.cpp @@ -739,14 +739,14 @@ void ImGui_ImplVulkan_InvalidateDeviceObjects() void ImGui_ImplVulkan_InvalidateFrameDeviceObjects() { - for (int i = 0; i < g_FramesDataBuffers.size(); i++) - { - FrameDataForRender* fd = &g_FramesDataBuffers[i]; - if (fd->VertexBuffer) { vkDestroyBuffer(g_Device, fd->VertexBuffer, g_Allocator); fd->VertexBuffer = VK_NULL_HANDLE; } - if (fd->VertexBufferMemory) { vkFreeMemory (g_Device, fd->VertexBufferMemory, g_Allocator); fd->VertexBufferMemory = VK_NULL_HANDLE; } - if (fd->IndexBuffer) { vkDestroyBuffer(g_Device, fd->IndexBuffer, g_Allocator); fd->IndexBuffer = VK_NULL_HANDLE; } - if (fd->IndexBufferMemory) { vkFreeMemory (g_Device, fd->IndexBufferMemory, g_Allocator); fd->IndexBufferMemory = VK_NULL_HANDLE; } - } + for (int i = 0; i < g_FramesDataBuffers.size(); i++) + { + FrameDataForRender* fd = &g_FramesDataBuffers[i]; + if (fd->VertexBuffer) { vkDestroyBuffer(g_Device, fd->VertexBuffer, g_Allocator); fd->VertexBuffer = VK_NULL_HANDLE; } + if (fd->VertexBufferMemory) { vkFreeMemory (g_Device, fd->VertexBufferMemory, g_Allocator); fd->VertexBufferMemory = VK_NULL_HANDLE; } + if (fd->IndexBuffer) { vkDestroyBuffer(g_Device, fd->IndexBuffer, g_Allocator); fd->IndexBuffer = VK_NULL_HANDLE; } + if (fd->IndexBufferMemory) { vkFreeMemory (g_Device, fd->IndexBufferMemory, g_Allocator); fd->IndexBufferMemory = VK_NULL_HANDLE; } + } } bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass render_pass) @@ -771,11 +771,9 @@ bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass rend g_DescriptorPool = info->DescriptorPool; g_Allocator = info->Allocator; g_CheckVkResultFn = info->CheckVkResultFn; - g_FramesDataBuffers.resize(info->QueuedFrames); - for (int i = 0; i < g_FramesDataBuffers.size(); i++) - { - g_FramesDataBuffers[i] = FrameDataForRender(); - } + g_FramesDataBuffers.resize(info->QueuedFrames); + for (int i = 0; i < g_FramesDataBuffers.size(); i++) + g_FramesDataBuffers[i] = FrameDataForRender(); ImGui_ImplVulkan_CreateDeviceObjects(); @@ -784,7 +782,7 @@ bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass rend void ImGui_ImplVulkan_Shutdown() { - ImGui_ImplVulkan_InvalidateFrameDeviceObjects(); + ImGui_ImplVulkan_InvalidateFrameDeviceObjects(); ImGui_ImplVulkan_InvalidateDeviceObjects(); } @@ -794,20 +792,15 @@ void ImGui_ImplVulkan_NewFrame() void ImGui_ImplVulkan_SetQueuedFramesCount(uint32_t count) { - if (count == g_FramesDataBuffers.size()) - { - return; - } - ImGui_ImplVulkan_InvalidateFrameDeviceObjects(); + if (count == g_FramesDataBuffers.size()) + return; + ImGui_ImplVulkan_InvalidateFrameDeviceObjects(); - uint32_t old_size = g_FramesDataBuffers.size(); - g_FramesDataBuffers.resize(count); - for (uint32_t i = old_size; i < count; i++) - { - - g_FramesDataBuffers[i] = FrameDataForRender(); - } - g_FrameIndex = 0; + uint32_t old_size = g_FramesDataBuffers.size(); + g_FramesDataBuffers.resize(count); + for (uint32_t i = old_size; i < count; i++) + g_FramesDataBuffers[i] = FrameDataForRender(); + g_FrameIndex = 0; } @@ -1040,18 +1033,14 @@ void ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(VkPhysicalDevice err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->BackBufferCount, NULL); check_vk_result(err); err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->BackBufferCount, wd->BackBuffer); - check_vk_result(err); + check_vk_result(err); - for (uint32_t i = 0; i < wd->Frames.size(); i++) - { - ImGui_ImplVulkanH_DestroyFrameData(g_Instance, device, &wd->Frames[i], allocator); - } - uint32_t old_size = wd->Frames.size(); - wd->Frames.resize(wd->BackBufferCount); - for (uint32_t i = 0; i < wd->Frames.size(); i++) - { - wd->Frames[i] = ImGui_ImplVulkanH_FrameData(); - } + for (uint32_t i = 0; i < wd->Frames.size(); i++) + ImGui_ImplVulkanH_DestroyFrameData(g_Instance, device, &wd->Frames[i], allocator); + uint32_t old_size = wd->Frames.size(); + wd->Frames.resize(wd->BackBufferCount); + for (uint32_t i = 0; i < wd->Frames.size(); i++) + wd->Frames[i] = ImGui_ImplVulkanH_FrameData(); } if (old_swapchain) vkDestroySwapchainKHR(device, old_swapchain, allocator); @@ -1141,7 +1130,7 @@ void ImGui_ImplVulkanH_DestroyWindowData(VkInstance instance, VkDevice device, I for (int i = 0; i < wd->Frames.size(); i++) { ImGui_ImplVulkanH_FrameData* fd = &wd->Frames[i]; - ImGui_ImplVulkanH_DestroyFrameData(instance, device, fd, allocator); + ImGui_ImplVulkanH_DestroyFrameData(instance, device, fd, allocator); } for (uint32_t i = 0; i < wd->BackBufferCount; i++) { @@ -1156,9 +1145,9 @@ void ImGui_ImplVulkanH_DestroyWindowData(VkInstance instance, VkDevice device, I void ImGui_ImplVulkanH_DestroyFrameData(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_FrameData* fd, const VkAllocationCallbacks* allocator) { - vkDestroyFence(device, fd->Fence, allocator); - vkFreeCommandBuffers(device, fd->CommandPool, 1, &fd->CommandBuffer); - vkDestroyCommandPool(device, fd->CommandPool, allocator); - vkDestroySemaphore(device, fd->ImageAcquiredSemaphore, allocator); - vkDestroySemaphore(device, fd->RenderCompleteSemaphore, allocator); + vkDestroyFence(device, fd->Fence, allocator); + vkFreeCommandBuffers(device, fd->CommandPool, 1, &fd->CommandBuffer); + vkDestroyCommandPool(device, fd->CommandPool, allocator); + vkDestroySemaphore(device, fd->ImageAcquiredSemaphore, allocator); + vkDestroySemaphore(device, fd->RenderCompleteSemaphore, allocator); } diff --git a/examples/imgui_impl_vulkan.h b/examples/imgui_impl_vulkan.h index 96cf52be..9164e6c3 100644 --- a/examples/imgui_impl_vulkan.h +++ b/examples/imgui_impl_vulkan.h @@ -27,7 +27,7 @@ struct ImGui_ImplVulkan_InitInfo VkQueue Queue; VkPipelineCache PipelineCache; VkDescriptorPool DescriptorPool; - int QueuedFrames; + int QueuedFrames; const VkAllocationCallbacks* Allocator; void (*CheckVkResultFn)(VkResult err); }; From 317859a3dad32ef8acf1ba77d6e266e6c1f7e379 Mon Sep 17 00:00:00 2001 From: omar Date: Wed, 3 Apr 2019 15:48:22 +0200 Subject: [PATCH 03/19] Examples: Vulkan: Updated Changelog, removed debug code, tweaked code, made GLFW/SDL match each others. Initialize FrameDataForRender fields. Added Assertion. Clearing fields on DestroyFrameData(). (#2071) --- docs/CHANGELOG.txt | 3 ++ examples/example_glfw_vulkan/main.cpp | 35 ++++------------- examples/example_sdl_vulkan/main.cpp | 54 +++++++++------------------ examples/imgui_impl_vulkan.cpp | 51 +++++++++++++++---------- examples/imgui_impl_vulkan.h | 4 +- 5 files changed, 61 insertions(+), 86 deletions(-) diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index a4cbd4de..0220f23a 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -49,6 +49,9 @@ Other Changes: - Examples: OpenGL3: Minor tweaks + not calling glBindBuffer more than necessary in the render loop. - Examples: Vulkan: Added missing support for 32-bit indices (#define ImDrawIdx unsigned int). - Examples: Vulkan: Avoid passing negative coordinates to vkCmdSetScissor, which debug validation layers do not like. +- Examples: Vulkan: Added QueuedFramesCount field in ImGui_ImplVulkan_InitInfo, required during + initialization (was previously a hard #define IMGUI_VK_QUEUED_FRAMES 2). (#2071) [@nathanvoglsam] + Added ImGui_ImplVulkan_SetQueuedFramesCount() to override QueuedFramesCount while running. - Examples: DirectX9: Fixed erroneous assert in ImGui_ImplDX9_InvalidateDeviceObjects(). (#2454) - Examples: GLUT: Fixed existing FreeGLUT example to work with regular GLUT. (#2465) [@andrewwillmott] - Examples: GLUT: Renamed imgui_impl_freeglut.cpp/.h to imgui_impl_glut.cpp/.h. (#2465) [@andrewwillmott] diff --git a/examples/example_glfw_vulkan/main.cpp b/examples/example_glfw_vulkan/main.cpp index 6e25185f..b04bde38 100644 --- a/examples/example_glfw_vulkan/main.cpp +++ b/examples/example_glfw_vulkan/main.cpp @@ -23,7 +23,6 @@ #define IMGUI_VULKAN_DEBUG_REPORT #endif -static uint32_t g_MinImageCount = 2; static VkAllocationCallbacks* g_Allocator = NULL; static VkInstance g_Instance = VK_NULL_HANDLE; static VkPhysicalDevice g_PhysicalDevice = VK_NULL_HANDLE; @@ -35,7 +34,8 @@ static VkPipelineCache g_PipelineCache = VK_NULL_HANDLE; static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE; static ImGui_ImplVulkanH_WindowData g_WindowData; -static bool g_ResizeWanted = false; +static int g_MinImageCount = 2; +static bool g_WantSwapChainRebuild = false; static int g_ResizeWidth = 0, g_ResizeHeight = 0; static void check_vk_result(VkResult err) @@ -214,6 +214,7 @@ static void SetupVulkanWindowData(ImGui_ImplVulkanH_WindowData* wd, VkSurfaceKHR // Create SwapChain, RenderPass, Framebuffer, etc. ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, wd, g_Allocator, width, height, g_MinImageCount); ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, wd, g_Allocator); + IM_ASSERT(wd->BackBufferCount > 0); } static void CleanupVulkan() @@ -314,7 +315,7 @@ static void glfw_error_callback(int error, const char* description) static void glfw_resize_callback(GLFWwindow*, int w, int h) { - g_ResizeWanted = true; + g_WantSwapChainRebuild = true; g_ResizeWidth = w; g_ResizeHeight = h; } @@ -373,8 +374,8 @@ int main(int, char**) init_info.PipelineCache = g_PipelineCache; init_info.DescriptorPool = g_DescriptorPool; init_info.Allocator = g_Allocator; + init_info.QueuedFramesCount = (int)wd->BackBufferCount; init_info.CheckVkResultFn = check_vk_result; - init_info.QueuedFrames = wd->BackBufferCount; ImGui_ImplVulkan_Init(&init_info, wd->RenderPass); // Load Fonts @@ -435,13 +436,13 @@ int main(int, char**) // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. glfwPollEvents(); - if (g_ResizeWanted) + if (g_WantSwapChainRebuild) { ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, &g_WindowData, g_Allocator, g_ResizeWidth, g_ResizeHeight, g_MinImageCount); ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, &g_WindowData, g_Allocator); ImGui_ImplVulkan_SetQueuedFramesCount(g_WindowData.BackBufferCount); g_WindowData.FrameIndex = 0; - g_ResizeWanted = false; + g_WantSwapChainRebuild = false; } // Start the Dear ImGui frame @@ -483,28 +484,6 @@ int main(int, char**) ImGui::Text("Hello from another window!"); if (ImGui::Button("Close Me")) show_another_window = false; - - if (ImGui::Button("Increase")) - { - g_MinImageCount++; - g_ResizeWanted = true; - } - - ImGui::SameLine(); - if (ImGui::Button("Decrease")) - { - if (g_MinImageCount != 2) - { - g_MinImageCount--; - g_ResizeWanted = true; - } - } - - ImGui::SameLine(); - ImGui::Text("Back Buffers: %i", g_MinImageCount); - - ImGui::Text("Frame Index %i", wd->FrameIndex); - ImGui::End(); } diff --git a/examples/example_sdl_vulkan/main.cpp b/examples/example_sdl_vulkan/main.cpp index b430f5a0..15eb62c2 100644 --- a/examples/example_sdl_vulkan/main.cpp +++ b/examples/example_sdl_vulkan/main.cpp @@ -15,8 +15,6 @@ #define IMGUI_VULKAN_DEBUG_REPORT #endif -static uint32_t g_MinImageCount = 2; -static bool g_PendingSwapchainRebuild = false; static VkAllocationCallbacks* g_Allocator = NULL; static VkInstance g_Instance = VK_NULL_HANDLE; static VkPhysicalDevice g_PhysicalDevice = VK_NULL_HANDLE; @@ -28,6 +26,8 @@ static VkPipelineCache g_PipelineCache = VK_NULL_HANDLE; static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE; static ImGui_ImplVulkanH_WindowData g_WindowData; +static uint32_t g_MinImageCount = 2; +static bool g_WantSwapChainRebuild = false; static void check_vk_result(VkResult err) { @@ -205,6 +205,7 @@ static void SetupVulkanWindowData(ImGui_ImplVulkanH_WindowData* wd, VkSurfaceKHR // Create SwapChain, RenderPass, Framebuffer, etc. ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, wd, g_Allocator, width, height, g_MinImageCount); ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, wd, g_Allocator); + IM_ASSERT(wd->BackBufferCount > 0); } static void CleanupVulkan() @@ -298,15 +299,6 @@ static void FramePresent(ImGui_ImplVulkanH_WindowData* wd) check_vk_result(err); } -static void RebuildSwapChain(int width, int height) -{ - ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, &g_WindowData, g_Allocator, width, height, g_MinImageCount); - ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, &g_WindowData, g_Allocator); - ImGui_ImplVulkan_SetQueuedFramesCount(g_WindowData.BackBufferCount); - g_WindowData.FrameIndex = 0; - g_PendingSwapchainRebuild = false; -} - int main(int, char**) { // Setup SDL @@ -365,7 +357,7 @@ int main(int, char**) init_info.PipelineCache = g_PipelineCache; init_info.DescriptorPool = g_DescriptorPool; init_info.Allocator = g_Allocator; - init_info.QueuedFrames = wd->BackBufferCount; + init_info.QueuedFramesCount = wd->BackBufferCount; init_info.CheckVkResultFn = check_vk_result; ImGui_ImplVulkan_Init(&init_info, wd->RenderPass); @@ -434,11 +426,21 @@ int main(int, char**) if (event.type == SDL_QUIT) done = true; if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESIZED && event.window.windowID == SDL_GetWindowID(window)) - RebuildSwapChain((int)event.window.data1, (int)event.window.data2); + { + g_WindowData.Width = (int)event.window.data1; + g_WindowData.Height = (int)event.window.data2; + g_WantSwapChainRebuild = true; + } } - if (g_PendingSwapchainRebuild) - RebuildSwapChain(g_WindowData.Width, g_WindowData.Height); + if (g_WantSwapChainRebuild) + { + ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, &g_WindowData, g_Allocator, g_WindowData.Width, g_WindowData.Height, g_MinImageCount); + ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, &g_WindowData, g_Allocator); + ImGui_ImplVulkan_SetQueuedFramesCount(g_WindowData.BackBufferCount); + g_WindowData.FrameIndex = 0; + g_WantSwapChainRebuild = false; + } // Start the Dear ImGui frame ImGui_ImplVulkan_NewFrame(); @@ -479,28 +481,6 @@ int main(int, char**) ImGui::Text("Hello from another window!"); if (ImGui::Button("Close Me")) show_another_window = false; - - if (ImGui::Button("Increase")) - { - g_MinImageCount++; - g_PendingSwapchainRebuild = true; - } - - ImGui::SameLine(); - if (ImGui::Button("Decrease")) - { - if (g_MinImageCount != 2) - { - g_MinImageCount--; - g_PendingSwapchainRebuild = true; - } - } - - ImGui::SameLine(); - ImGui::Text("Back Buffers: %i", g_MinImageCount); - - ImGui::Text("Frame Index %i", wd->FrameIndex); - ImGui::End(); } diff --git a/examples/imgui_impl_vulkan.cpp b/examples/imgui_impl_vulkan.cpp index a74323ee..9eff80fd 100644 --- a/examples/imgui_impl_vulkan.cpp +++ b/examples/imgui_impl_vulkan.cpp @@ -13,6 +13,8 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2019-XX-XX: Vulkan: Added QueuedFramesCount field in ImGui_ImplVulkan_InitInfo, required for initialization (was previously a hard #define IMGUI_VK_QUEUED_FRAMES 2). +// 2019-XX-XX: Vulkan: Added ImGui_ImplVulkan_SetQueuedFramesCount() to override QueuedFramesCount while running. // 2019-04-04: Vulkan: Avoid passing negative coordinates to vkCmdSetScissor, which debug validation layers do not like. // 2019-04-01: Vulkan: Support for 32-bit index buffer (#define ImDrawIdx unsigned int). // 2019-02-16: Vulkan: Viewport and clipping rectangles correctly using draw_data->FramebufferScale to allow retina display. @@ -63,9 +65,11 @@ struct FrameDataForRender VkDeviceSize IndexBufferSize; VkBuffer VertexBuffer; VkBuffer IndexBuffer; + + FrameDataForRender() { VertexBufferMemory = IndexBufferMemory = VK_NULL_HANDLE; VertexBufferSize = IndexBufferSize = VK_NULL_HANDLE; VertexBuffer = IndexBuffer = VK_NULL_HANDLE; } }; static int g_FrameIndex = 0; -static ImVector g_FramesDataBuffers = {}; +static ImVector g_FramesDataBuffers; // Font data static VkSampler g_FontSampler = VK_NULL_HANDLE; @@ -240,7 +244,7 @@ void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer comm VkResult err; FrameDataForRender* fd = &g_FramesDataBuffers[g_FrameIndex]; - g_FrameIndex = (g_FrameIndex + 1) % g_FramesDataBuffers.size(); + g_FrameIndex = (g_FrameIndex + 1) % g_FramesDataBuffers.Size; // Create the Vertex and Index buffers: size_t vertex_size = draw_data->TotalVtxCount * sizeof(ImDrawVert); @@ -739,7 +743,7 @@ void ImGui_ImplVulkan_InvalidateDeviceObjects() void ImGui_ImplVulkan_InvalidateFrameDeviceObjects() { - for (int i = 0; i < g_FramesDataBuffers.size(); i++) + for (int i = 0; i < g_FramesDataBuffers.Size; i++) { FrameDataForRender* fd = &g_FramesDataBuffers[i]; if (fd->VertexBuffer) { vkDestroyBuffer(g_Device, fd->VertexBuffer, g_Allocator); fd->VertexBuffer = VK_NULL_HANDLE; } @@ -747,6 +751,7 @@ void ImGui_ImplVulkan_InvalidateFrameDeviceObjects() if (fd->IndexBuffer) { vkDestroyBuffer(g_Device, fd->IndexBuffer, g_Allocator); fd->IndexBuffer = VK_NULL_HANDLE; } if (fd->IndexBufferMemory) { vkFreeMemory (g_Device, fd->IndexBufferMemory, g_Allocator); fd->IndexBufferMemory = VK_NULL_HANDLE; } } + g_FramesDataBuffers.clear(); } bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass render_pass) @@ -759,6 +764,7 @@ bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass rend IM_ASSERT(info->Device != VK_NULL_HANDLE); IM_ASSERT(info->Queue != VK_NULL_HANDLE); IM_ASSERT(info->DescriptorPool != VK_NULL_HANDLE); + IM_ASSERT(info->QueuedFramesCount >= 2); IM_ASSERT(render_pass != VK_NULL_HANDLE); g_Instance = info->Instance; @@ -771,9 +777,10 @@ bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass rend g_DescriptorPool = info->DescriptorPool; g_Allocator = info->Allocator; g_CheckVkResultFn = info->CheckVkResultFn; - g_FramesDataBuffers.resize(info->QueuedFrames); - for (int i = 0; i < g_FramesDataBuffers.size(); i++) - g_FramesDataBuffers[i] = FrameDataForRender(); + + g_FramesDataBuffers.reserve(info->QueuedFramesCount); + for (int i = 0; i < info->QueuedFramesCount; i++) + g_FramesDataBuffers.push_back(FrameDataForRender()); ImGui_ImplVulkan_CreateDeviceObjects(); @@ -790,17 +797,16 @@ void ImGui_ImplVulkan_NewFrame() { } -void ImGui_ImplVulkan_SetQueuedFramesCount(uint32_t count) +void ImGui_ImplVulkan_SetQueuedFramesCount(int count) { - if (count == g_FramesDataBuffers.size()) + if (count == g_FramesDataBuffers.Size) return; ImGui_ImplVulkan_InvalidateFrameDeviceObjects(); - uint32_t old_size = g_FramesDataBuffers.size(); - g_FramesDataBuffers.resize(count); - for (uint32_t i = old_size; i < count; i++) - g_FramesDataBuffers[i] = FrameDataForRender(); g_FrameIndex = 0; + g_FramesDataBuffers.reserve(count); + for (int i = 0; i < count; i++) + g_FramesDataBuffers.push_back(FrameDataForRender()); } @@ -921,7 +927,7 @@ void ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(VkPhysicalDevice physical_ // Create Command Buffers VkResult err; - for (int i = 0; i < wd->Frames.size(); i++) + for (int i = 0; i < wd->Frames.Size; i++) { ImGui_ImplVulkanH_FrameData* fd = &wd->Frames[i]; { @@ -1035,12 +1041,13 @@ void ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(VkPhysicalDevice err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->BackBufferCount, wd->BackBuffer); check_vk_result(err); - for (uint32_t i = 0; i < wd->Frames.size(); i++) + for (int i = 0; i < wd->Frames.Size; i++) ImGui_ImplVulkanH_DestroyFrameData(g_Instance, device, &wd->Frames[i], allocator); - uint32_t old_size = wd->Frames.size(); - wd->Frames.resize(wd->BackBufferCount); - for (uint32_t i = 0; i < wd->Frames.size(); i++) - wd->Frames[i] = ImGui_ImplVulkanH_FrameData(); + wd->Frames.clear(); + + wd->Frames.reserve((int)wd->BackBufferCount); + for (int i = 0; i < (int)wd->BackBufferCount; i++) + wd->Frames.push_back(ImGui_ImplVulkanH_FrameData()); } if (old_swapchain) vkDestroySwapchainKHR(device, old_swapchain, allocator); @@ -1127,7 +1134,7 @@ void ImGui_ImplVulkanH_DestroyWindowData(VkInstance instance, VkDevice device, I vkDeviceWaitIdle(device); // FIXME: We could wait on the Queue if we had the queue in wd-> (otherwise VulkanH functions can't use globals) //vkQueueWaitIdle(g_Queue); - for (int i = 0; i < wd->Frames.size(); i++) + for (int i = 0; i < wd->Frames.Size; i++) { ImGui_ImplVulkanH_FrameData* fd = &wd->Frames[i]; ImGui_ImplVulkanH_DestroyFrameData(instance, device, fd, allocator); @@ -1145,9 +1152,15 @@ void ImGui_ImplVulkanH_DestroyWindowData(VkInstance instance, VkDevice device, I void ImGui_ImplVulkanH_DestroyFrameData(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_FrameData* fd, const VkAllocationCallbacks* allocator) { + (void)instance; vkDestroyFence(device, fd->Fence, allocator); vkFreeCommandBuffers(device, fd->CommandPool, 1, &fd->CommandBuffer); vkDestroyCommandPool(device, fd->CommandPool, allocator); vkDestroySemaphore(device, fd->ImageAcquiredSemaphore, allocator); vkDestroySemaphore(device, fd->RenderCompleteSemaphore, allocator); + fd->BackbufferIndex = 0; + fd->Fence = VK_NULL_HANDLE; + fd->CommandBuffer = VK_NULL_HANDLE; + fd->CommandPool = VK_NULL_HANDLE; + fd->ImageAcquiredSemaphore = fd->RenderCompleteSemaphore = VK_NULL_HANDLE; } diff --git a/examples/imgui_impl_vulkan.h b/examples/imgui_impl_vulkan.h index 9164e6c3..f54caf3d 100644 --- a/examples/imgui_impl_vulkan.h +++ b/examples/imgui_impl_vulkan.h @@ -27,7 +27,7 @@ struct ImGui_ImplVulkan_InitInfo VkQueue Queue; VkPipelineCache PipelineCache; VkDescriptorPool DescriptorPool; - int QueuedFrames; + int QueuedFramesCount; const VkAllocationCallbacks* Allocator; void (*CheckVkResultFn)(VkResult err); }; @@ -36,7 +36,7 @@ struct ImGui_ImplVulkan_InitInfo IMGUI_IMPL_API bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass render_pass); IMGUI_IMPL_API void ImGui_ImplVulkan_Shutdown(); IMGUI_IMPL_API void ImGui_ImplVulkan_NewFrame(); -IMGUI_IMPL_API void ImGui_ImplVulkan_SetQueuedFramesCount(uint32_t count); +IMGUI_IMPL_API void ImGui_ImplVulkan_SetQueuedFramesCount(int queued_frames_count); // To override QueuedFramesCount after initialization IMGUI_IMPL_API void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer command_buffer); IMGUI_IMPL_API bool ImGui_ImplVulkan_CreateFontsTexture(VkCommandBuffer command_buffer); IMGUI_IMPL_API void ImGui_ImplVulkan_InvalidateFontUploadObjects(); From 54b8a65d9edcdc560ae643f3c4e812ca2639dedf Mon Sep 17 00:00:00 2001 From: omar Date: Wed, 3 Apr 2019 17:56:18 +0200 Subject: [PATCH 04/19] Examples: Vulkan: Renamed QueuedFramesCount to FramesQueueSize. Moved Framebuffer, Backbuffer to FrameData structure. (#2071) --- docs/CHANGELOG.txt | 4 +- examples/example_glfw_vulkan/main.cpp | 8 +-- examples/example_sdl_vulkan/main.cpp | 8 +-- examples/imgui_impl_vulkan.cpp | 83 ++++++++++++--------------- examples/imgui_impl_vulkan.h | 15 +++-- 5 files changed, 54 insertions(+), 64 deletions(-) diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index 0220f23a..6046f19b 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -49,9 +49,9 @@ Other Changes: - Examples: OpenGL3: Minor tweaks + not calling glBindBuffer more than necessary in the render loop. - Examples: Vulkan: Added missing support for 32-bit indices (#define ImDrawIdx unsigned int). - Examples: Vulkan: Avoid passing negative coordinates to vkCmdSetScissor, which debug validation layers do not like. -- Examples: Vulkan: Added QueuedFramesCount field in ImGui_ImplVulkan_InitInfo, required during +- Examples: Vulkan: Added FramesQueueSize field in ImGui_ImplVulkan_InitInfo, required during initialization (was previously a hard #define IMGUI_VK_QUEUED_FRAMES 2). (#2071) [@nathanvoglsam] - Added ImGui_ImplVulkan_SetQueuedFramesCount() to override QueuedFramesCount while running. + Added ImGui_ImplVulkan_SetFramesQueueSize() to override FramesQueueSize while running. - Examples: DirectX9: Fixed erroneous assert in ImGui_ImplDX9_InvalidateDeviceObjects(). (#2454) - Examples: GLUT: Fixed existing FreeGLUT example to work with regular GLUT. (#2465) [@andrewwillmott] - Examples: GLUT: Renamed imgui_impl_freeglut.cpp/.h to imgui_impl_glut.cpp/.h. (#2465) [@andrewwillmott] diff --git a/examples/example_glfw_vulkan/main.cpp b/examples/example_glfw_vulkan/main.cpp index b04bde38..cec1217d 100644 --- a/examples/example_glfw_vulkan/main.cpp +++ b/examples/example_glfw_vulkan/main.cpp @@ -214,7 +214,7 @@ static void SetupVulkanWindowData(ImGui_ImplVulkanH_WindowData* wd, VkSurfaceKHR // Create SwapChain, RenderPass, Framebuffer, etc. ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, wd, g_Allocator, width, height, g_MinImageCount); ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, wd, g_Allocator); - IM_ASSERT(wd->BackBufferCount > 0); + IM_ASSERT(wd->FramesQueueSize >= 2); } static void CleanupVulkan() @@ -262,7 +262,7 @@ static void FrameRender(ImGui_ImplVulkanH_WindowData* wd) VkRenderPassBeginInfo info = {}; info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; info.renderPass = wd->RenderPass; - info.framebuffer = wd->Framebuffer[wd->FrameIndex]; + info.framebuffer = fd->Framebuffer; info.renderArea.extent.width = wd->Width; info.renderArea.extent.height = wd->Height; info.clearValueCount = 1; @@ -374,7 +374,7 @@ int main(int, char**) init_info.PipelineCache = g_PipelineCache; init_info.DescriptorPool = g_DescriptorPool; init_info.Allocator = g_Allocator; - init_info.QueuedFramesCount = (int)wd->BackBufferCount; + init_info.FramesQueueSize = wd->FramesQueueSize; init_info.CheckVkResultFn = check_vk_result; ImGui_ImplVulkan_Init(&init_info, wd->RenderPass); @@ -440,7 +440,7 @@ int main(int, char**) { ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, &g_WindowData, g_Allocator, g_ResizeWidth, g_ResizeHeight, g_MinImageCount); ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, &g_WindowData, g_Allocator); - ImGui_ImplVulkan_SetQueuedFramesCount(g_WindowData.BackBufferCount); + ImGui_ImplVulkan_SetFramesQueueSize(g_WindowData.FramesQueueSize); g_WindowData.FrameIndex = 0; g_WantSwapChainRebuild = false; } diff --git a/examples/example_sdl_vulkan/main.cpp b/examples/example_sdl_vulkan/main.cpp index 15eb62c2..70ba5a2d 100644 --- a/examples/example_sdl_vulkan/main.cpp +++ b/examples/example_sdl_vulkan/main.cpp @@ -205,7 +205,7 @@ static void SetupVulkanWindowData(ImGui_ImplVulkanH_WindowData* wd, VkSurfaceKHR // Create SwapChain, RenderPass, Framebuffer, etc. ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, wd, g_Allocator, width, height, g_MinImageCount); ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, wd, g_Allocator); - IM_ASSERT(wd->BackBufferCount > 0); + IM_ASSERT(wd->FramesQueueSize >= 2); } static void CleanupVulkan() @@ -253,7 +253,7 @@ static void FrameRender(ImGui_ImplVulkanH_WindowData* wd) VkRenderPassBeginInfo info = {}; info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; info.renderPass = wd->RenderPass; - info.framebuffer = wd->Framebuffer[wd->FrameIndex]; + info.framebuffer = fd->Framebuffer; info.renderArea.extent.width = wd->Width; info.renderArea.extent.height = wd->Height; info.clearValueCount = 1; @@ -357,7 +357,7 @@ int main(int, char**) init_info.PipelineCache = g_PipelineCache; init_info.DescriptorPool = g_DescriptorPool; init_info.Allocator = g_Allocator; - init_info.QueuedFramesCount = wd->BackBufferCount; + init_info.FramesQueueSize = wd->FramesQueueSize; init_info.CheckVkResultFn = check_vk_result; ImGui_ImplVulkan_Init(&init_info, wd->RenderPass); @@ -437,7 +437,7 @@ int main(int, char**) { ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, &g_WindowData, g_Allocator, g_WindowData.Width, g_WindowData.Height, g_MinImageCount); ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, &g_WindowData, g_Allocator); - ImGui_ImplVulkan_SetQueuedFramesCount(g_WindowData.BackBufferCount); + ImGui_ImplVulkan_SetFramesQueueSize(g_WindowData.FramesQueueSize); g_WindowData.FrameIndex = 0; g_WantSwapChainRebuild = false; } diff --git a/examples/imgui_impl_vulkan.cpp b/examples/imgui_impl_vulkan.cpp index 9eff80fd..2d6fc373 100644 --- a/examples/imgui_impl_vulkan.cpp +++ b/examples/imgui_impl_vulkan.cpp @@ -13,8 +13,7 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) -// 2019-XX-XX: Vulkan: Added QueuedFramesCount field in ImGui_ImplVulkan_InitInfo, required for initialization (was previously a hard #define IMGUI_VK_QUEUED_FRAMES 2). -// 2019-XX-XX: Vulkan: Added ImGui_ImplVulkan_SetQueuedFramesCount() to override QueuedFramesCount while running. +// 2019-XX-XX: Vulkan: Added FramesQueueSize field in ImGui_ImplVulkan_InitInfo, required for initialization (was previously a hard #define IMGUI_VK_QUEUED_FRAMES 2). Added ImGui_ImplVulkan_SetFramesQueueSize() to override FramesQueueSize while running. // 2019-04-04: Vulkan: Avoid passing negative coordinates to vkCmdSetScissor, which debug validation layers do not like. // 2019-04-01: Vulkan: Support for 32-bit index buffer (#define ImDrawIdx unsigned int). // 2019-02-16: Vulkan: Viewport and clipping rectangles correctly using draw_data->FramebufferScale to allow retina display. @@ -764,7 +763,7 @@ bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass rend IM_ASSERT(info->Device != VK_NULL_HANDLE); IM_ASSERT(info->Queue != VK_NULL_HANDLE); IM_ASSERT(info->DescriptorPool != VK_NULL_HANDLE); - IM_ASSERT(info->QueuedFramesCount >= 2); + IM_ASSERT(info->FramesQueueSize >= 2); IM_ASSERT(render_pass != VK_NULL_HANDLE); g_Instance = info->Instance; @@ -778,8 +777,8 @@ bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass rend g_Allocator = info->Allocator; g_CheckVkResultFn = info->CheckVkResultFn; - g_FramesDataBuffers.reserve(info->QueuedFramesCount); - for (int i = 0; i < info->QueuedFramesCount; i++) + g_FramesDataBuffers.reserve(info->FramesQueueSize); + for (int i = 0; i < info->FramesQueueSize; i++) g_FramesDataBuffers.push_back(FrameDataForRender()); ImGui_ImplVulkan_CreateDeviceObjects(); @@ -797,15 +796,15 @@ void ImGui_ImplVulkan_NewFrame() { } -void ImGui_ImplVulkan_SetQueuedFramesCount(int count) +void ImGui_ImplVulkan_SetFramesQueueSize(int frames_queue_size) { - if (count == g_FramesDataBuffers.Size) + if (frames_queue_size == g_FramesDataBuffers.Size) return; ImGui_ImplVulkan_InvalidateFrameDeviceObjects(); g_FrameIndex = 0; - g_FramesDataBuffers.reserve(count); - for (int i = 0; i < count; i++) + g_FramesDataBuffers.reserve(frames_queue_size); + for (int i = 0; i < frames_queue_size; i++) g_FramesDataBuffers.push_back(FrameDataForRender()); } @@ -828,12 +827,14 @@ void ImGui_ImplVulkan_SetQueuedFramesCount(int count) ImGui_ImplVulkanH_FrameData::ImGui_ImplVulkanH_FrameData() { - BackbufferIndex = 0; CommandPool = VK_NULL_HANDLE; CommandBuffer = VK_NULL_HANDLE; Fence = VK_NULL_HANDLE; ImageAcquiredSemaphore = VK_NULL_HANDLE; RenderCompleteSemaphore = VK_NULL_HANDLE; + BackBuffer = VK_NULL_HANDLE; + BackBufferView = VK_NULL_HANDLE; + Framebuffer = VK_NULL_HANDLE; } ImGui_ImplVulkanH_WindowData::ImGui_ImplVulkanH_WindowData() @@ -846,10 +847,7 @@ ImGui_ImplVulkanH_WindowData::ImGui_ImplVulkanH_WindowData() RenderPass = VK_NULL_HANDLE; ClearEnable = true; memset(&ClearValue, 0, sizeof(ClearValue)); - BackBufferCount = 0; - memset(&BackBuffer, 0, sizeof(BackBuffer)); - memset(&BackBufferView, 0, sizeof(BackBufferView)); - memset(&Framebuffer, 0, sizeof(Framebuffer)); + FramesQueueSize = 0; FrameIndex = 0; } @@ -985,14 +983,10 @@ void ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(VkPhysicalDevice check_vk_result(err); // Destroy old Framebuffer - for (uint32_t i = 0; i < wd->BackBufferCount; i++) - { - if (wd->BackBufferView[i]) - vkDestroyImageView(device, wd->BackBufferView[i], allocator); - if (wd->Framebuffer[i]) - vkDestroyFramebuffer(device, wd->Framebuffer[i], allocator); - } - wd->BackBufferCount = 0; + for (uint32_t i = 0; i < wd->FramesQueueSize; i++) + ImGui_ImplVulkanH_DestroyFrameData(g_Instance, device, &wd->Frames[i], allocator); + wd->Frames.clear(); + wd->FramesQueueSize = 0; if (wd->RenderPass) vkDestroyRenderPass(device, wd->RenderPass, allocator); @@ -1036,18 +1030,19 @@ void ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(VkPhysicalDevice } err = vkCreateSwapchainKHR(device, &info, allocator, &wd->Swapchain); check_vk_result(err); - err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->BackBufferCount, NULL); + err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->FramesQueueSize, NULL); check_vk_result(err); - err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->BackBufferCount, wd->BackBuffer); + VkImage backbuffers[16] = {}; + IM_ASSERT(wd->FramesQueueSize < IM_ARRAYSIZE(backbuffers)); + err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->FramesQueueSize, backbuffers); check_vk_result(err); - for (int i = 0; i < wd->Frames.Size; i++) - ImGui_ImplVulkanH_DestroyFrameData(g_Instance, device, &wd->Frames[i], allocator); - wd->Frames.clear(); - - wd->Frames.reserve((int)wd->BackBufferCount); - for (int i = 0; i < (int)wd->BackBufferCount; i++) + wd->Frames.reserve((int)wd->FramesQueueSize); + for (int i = 0; i < (int)wd->FramesQueueSize; i++) + { wd->Frames.push_back(ImGui_ImplVulkanH_FrameData()); + wd->Frames[i].BackBuffer = backbuffers[i]; + } } if (old_swapchain) vkDestroySwapchainKHR(device, old_swapchain, allocator); @@ -1101,10 +1096,11 @@ void ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(VkPhysicalDevice info.components.a = VK_COMPONENT_SWIZZLE_A; VkImageSubresourceRange image_range = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 }; info.subresourceRange = image_range; - for (uint32_t i = 0; i < wd->BackBufferCount; i++) + for (uint32_t i = 0; i < wd->FramesQueueSize; i++) { - info.image = wd->BackBuffer[i]; - err = vkCreateImageView(device, &info, allocator, &wd->BackBufferView[i]); + ImGui_ImplVulkanH_FrameData* fd = &wd->Frames[i]; + info.image = fd->BackBuffer; + err = vkCreateImageView(device, &info, allocator, &fd->BackBufferView); check_vk_result(err); } } @@ -1120,10 +1116,11 @@ void ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(VkPhysicalDevice info.width = wd->Width; info.height = wd->Height; info.layers = 1; - for (uint32_t i = 0; i < wd->BackBufferCount; i++) + for (uint32_t i = 0; i < wd->FramesQueueSize; i++) { - attachment[0] = wd->BackBufferView[i]; - err = vkCreateFramebuffer(device, &info, allocator, &wd->Framebuffer[i]); + ImGui_ImplVulkanH_FrameData* fd = &wd->Frames[i]; + attachment[0] = fd->BackBufferView; + err = vkCreateFramebuffer(device, &info, allocator, &fd->Framebuffer); check_vk_result(err); } } @@ -1135,15 +1132,7 @@ void ImGui_ImplVulkanH_DestroyWindowData(VkInstance instance, VkDevice device, I //vkQueueWaitIdle(g_Queue); for (int i = 0; i < wd->Frames.Size; i++) - { - ImGui_ImplVulkanH_FrameData* fd = &wd->Frames[i]; - ImGui_ImplVulkanH_DestroyFrameData(instance, device, fd, allocator); - } - for (uint32_t i = 0; i < wd->BackBufferCount; i++) - { - vkDestroyImageView(device, wd->BackBufferView[i], allocator); - vkDestroyFramebuffer(device, wd->Framebuffer[i], allocator); - } + ImGui_ImplVulkanH_DestroyFrameData(instance, device, &wd->Frames[i], allocator); vkDestroyRenderPass(device, wd->RenderPass, allocator); vkDestroySwapchainKHR(device, wd->Swapchain, allocator); vkDestroySurfaceKHR(instance, wd->Surface, allocator); @@ -1158,9 +1147,11 @@ void ImGui_ImplVulkanH_DestroyFrameData(VkInstance instance, VkDevice device, Im vkDestroyCommandPool(device, fd->CommandPool, allocator); vkDestroySemaphore(device, fd->ImageAcquiredSemaphore, allocator); vkDestroySemaphore(device, fd->RenderCompleteSemaphore, allocator); - fd->BackbufferIndex = 0; fd->Fence = VK_NULL_HANDLE; fd->CommandBuffer = VK_NULL_HANDLE; fd->CommandPool = VK_NULL_HANDLE; fd->ImageAcquiredSemaphore = fd->RenderCompleteSemaphore = VK_NULL_HANDLE; + + vkDestroyImageView(device, fd->BackBufferView, allocator); + vkDestroyFramebuffer(device, fd->Framebuffer, allocator); } diff --git a/examples/imgui_impl_vulkan.h b/examples/imgui_impl_vulkan.h index f54caf3d..495f6d47 100644 --- a/examples/imgui_impl_vulkan.h +++ b/examples/imgui_impl_vulkan.h @@ -27,7 +27,7 @@ struct ImGui_ImplVulkan_InitInfo VkQueue Queue; VkPipelineCache PipelineCache; VkDescriptorPool DescriptorPool; - int QueuedFramesCount; + int FramesQueueSize; // >= 2, generally matches the image count returned by vkGetSwapchainImagesKHR const VkAllocationCallbacks* Allocator; void (*CheckVkResultFn)(VkResult err); }; @@ -36,7 +36,7 @@ struct ImGui_ImplVulkan_InitInfo IMGUI_IMPL_API bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass render_pass); IMGUI_IMPL_API void ImGui_ImplVulkan_Shutdown(); IMGUI_IMPL_API void ImGui_ImplVulkan_NewFrame(); -IMGUI_IMPL_API void ImGui_ImplVulkan_SetQueuedFramesCount(int queued_frames_count); // To override QueuedFramesCount after initialization +IMGUI_IMPL_API void ImGui_ImplVulkan_SetFramesQueueSize(int frames_queue_size); // To override FramesQueueSize after initialization IMGUI_IMPL_API void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer command_buffer); IMGUI_IMPL_API bool ImGui_ImplVulkan_CreateFontsTexture(VkCommandBuffer command_buffer); IMGUI_IMPL_API void ImGui_ImplVulkan_InvalidateFontUploadObjects(); @@ -77,12 +77,14 @@ IMGUI_IMPL_API int ImGui_ImplVulkanH_GetMinImageCountFromPresen // (Used by example's main.cpp. Used by multi-viewport features. Probably NOT used by your own app.) struct ImGui_ImplVulkanH_FrameData { - uint32_t BackbufferIndex; // Keep track of recently rendered swapchain frame indices VkCommandPool CommandPool; VkCommandBuffer CommandBuffer; VkFence Fence; VkSemaphore ImageAcquiredSemaphore; VkSemaphore RenderCompleteSemaphore; + VkImage BackBuffer; + VkImageView BackBufferView; + VkFramebuffer Framebuffer; IMGUI_IMPL_API ImGui_ImplVulkanH_FrameData(); }; @@ -100,11 +102,8 @@ struct ImGui_ImplVulkanH_WindowData VkRenderPass RenderPass; bool ClearEnable; VkClearValue ClearValue; - uint32_t BackBufferCount; - VkImage BackBuffer[16]; - VkImageView BackBufferView[16]; - VkFramebuffer Framebuffer[16]; - uint32_t FrameIndex; + uint32_t FramesQueueSize; // Number of simultaneous in-flight frames (returned by vkGetSwapchainImagesKHR) + uint32_t FrameIndex; // Current frame being rendered to (0 <= FrameIndex < FrameInFlightCount) ImVector Frames; IMGUI_IMPL_API ImGui_ImplVulkanH_WindowData(); From f586764cdd534c9735a918c474f9a0a8b3d747bd Mon Sep 17 00:00:00 2001 From: omar Date: Wed, 3 Apr 2019 18:33:13 +0200 Subject: [PATCH 05/19] Examples: Vulkan: Merged helpers into ImGui_ImplVulkanH_CreateWindowData. Removed ImGui_ImplVulkan_InvalidateFrameDeviceObjects from API. Comments. (#2071) --- examples/example_glfw_vulkan/main.cpp | 11 +++-- examples/example_sdl_vulkan/main.cpp | 11 +++-- examples/imgui_impl_vulkan.cpp | 59 ++++++++++++++++++--------- examples/imgui_impl_vulkan.h | 16 +++----- 4 files changed, 60 insertions(+), 37 deletions(-) diff --git a/examples/example_glfw_vulkan/main.cpp b/examples/example_glfw_vulkan/main.cpp index cec1217d..fbb2f499 100644 --- a/examples/example_glfw_vulkan/main.cpp +++ b/examples/example_glfw_vulkan/main.cpp @@ -1,6 +1,11 @@ // dear imgui: standalone example application for Glfw + Vulkan // If you are new to dear imgui, see examples/README.txt and documentation at the top of imgui.cpp. +// Important note to the reader who wish to integrate imgui_impl_vulkan.cpp/.h in their own application. +// - Common ImGui_ImplVulkan_XXXX functions and structures are used to interface with imgui_impl_vulkan.cpp/.h. +// - Helper ImGui_ImplVulkanH_XXXX functions and structures are used by this example (main.cpp) and by imgui_impl_vulkan.cpp, +// but should PROBABLY NOT be used by your own app code. Read comments in imgui_impl_vulkan.h. + #include "imgui.h" #include "imgui_impl_glfw.h" #include "imgui_impl_vulkan.h" @@ -212,8 +217,7 @@ static void SetupVulkanWindowData(ImGui_ImplVulkanH_WindowData* wd, VkSurfaceKHR //printf("[vulkan] Selected PresentMode = %d\n", wd->PresentMode); // Create SwapChain, RenderPass, Framebuffer, etc. - ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, wd, g_Allocator, width, height, g_MinImageCount); - ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, wd, g_Allocator); + ImGui_ImplVulkanH_CreateWindowData(g_PhysicalDevice, g_Device, wd, g_QueueFamily, g_Allocator, width, height, g_MinImageCount); IM_ASSERT(wd->FramesQueueSize >= 2); } @@ -438,8 +442,7 @@ int main(int, char**) glfwPollEvents(); if (g_WantSwapChainRebuild) { - ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, &g_WindowData, g_Allocator, g_ResizeWidth, g_ResizeHeight, g_MinImageCount); - ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, &g_WindowData, g_Allocator); + ImGui_ImplVulkanH_CreateWindowData(g_PhysicalDevice, g_Device, &g_WindowData, g_QueueFamily, g_Allocator, g_ResizeWidth, g_ResizeHeight, g_MinImageCount); ImGui_ImplVulkan_SetFramesQueueSize(g_WindowData.FramesQueueSize); g_WindowData.FrameIndex = 0; g_WantSwapChainRebuild = false; diff --git a/examples/example_sdl_vulkan/main.cpp b/examples/example_sdl_vulkan/main.cpp index 70ba5a2d..af331302 100644 --- a/examples/example_sdl_vulkan/main.cpp +++ b/examples/example_sdl_vulkan/main.cpp @@ -1,6 +1,11 @@ // dear imgui: standalone example application for SDL2 + Vulkan // If you are new to dear imgui, see examples/README.txt and documentation at the top of imgui.cpp. +// Important note to the reader who wish to integrate imgui_impl_vulkan.cpp/.h in their own application. +// - Common ImGui_ImplVulkan_XXXX functions and structures are used to interface with imgui_impl_vulkan.cpp/.h. +// - Helper ImGui_ImplVulkanH_XXXX functions and structures are used by this example (main.cpp) and by imgui_impl_vulkan.cpp, +// but should PROBABLY NOT be used by your own app code. Read comments in imgui_impl_vulkan.h. + #include "imgui.h" #include "imgui_impl_sdl.h" #include "imgui_impl_vulkan.h" @@ -203,8 +208,7 @@ static void SetupVulkanWindowData(ImGui_ImplVulkanH_WindowData* wd, VkSurfaceKHR //printf("[vulkan] Selected PresentMode = %d\n", wd->PresentMode); // Create SwapChain, RenderPass, Framebuffer, etc. - ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, wd, g_Allocator, width, height, g_MinImageCount); - ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, wd, g_Allocator); + ImGui_ImplVulkanH_CreateWindowData(g_PhysicalDevice, g_Device, wd, g_QueueFamily, g_Allocator, width, height, g_MinImageCount); IM_ASSERT(wd->FramesQueueSize >= 2); } @@ -435,8 +439,7 @@ int main(int, char**) if (g_WantSwapChainRebuild) { - ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, &g_WindowData, g_Allocator, g_WindowData.Width, g_WindowData.Height, g_MinImageCount); - ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, &g_WindowData, g_Allocator); + ImGui_ImplVulkanH_CreateWindowData(g_PhysicalDevice, g_Device, &g_WindowData, g_QueueFamily, g_Allocator, g_WindowData.Width, g_WindowData.Height, g_MinImageCount); ImGui_ImplVulkan_SetFramesQueueSize(g_WindowData.FramesQueueSize); g_WindowData.FrameIndex = 0; g_WantSwapChainRebuild = false; diff --git a/examples/imgui_impl_vulkan.cpp b/examples/imgui_impl_vulkan.cpp index 2d6fc373..97a5b9a8 100644 --- a/examples/imgui_impl_vulkan.cpp +++ b/examples/imgui_impl_vulkan.cpp @@ -78,6 +78,15 @@ static VkImageView g_FontView = VK_NULL_HANDLE; static VkDeviceMemory g_UploadBufferMemory = VK_NULL_HANDLE; static VkBuffer g_UploadBuffer = VK_NULL_HANDLE; +// Forward Declarations +void ImGui_ImplVulkanH_DestroyFrameData(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_FrameData* fd, const VkAllocationCallbacks* allocator); +void ImGui_ImplVulkanH_CreateWindowDataSwapChain(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count); +void ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator); + +//----------------------------------------------------------------------------- +// SHADERS +//----------------------------------------------------------------------------- + // glsl_shader.vert, compiled with: // # glslangValidator -V -x -o glsl_shader.vert.u32 glsl_shader.vert /* @@ -183,6 +192,10 @@ static uint32_t __glsl_shader_frag_spv[] = 0x00010038 }; +//----------------------------------------------------------------------------- +// FUNCTIONS +//----------------------------------------------------------------------------- + static uint32_t ImGui_ImplVulkan_MemoryType(VkMemoryPropertyFlags properties, uint32_t type_bits) { VkPhysicalDeviceMemoryProperties prop; @@ -727,20 +740,7 @@ void ImGui_ImplVulkan_InvalidateFontUploadObjects() } } -void ImGui_ImplVulkan_InvalidateDeviceObjects() -{ - ImGui_ImplVulkan_InvalidateFontUploadObjects(); - - if (g_FontView) { vkDestroyImageView(g_Device, g_FontView, g_Allocator); g_FontView = VK_NULL_HANDLE; } - if (g_FontImage) { vkDestroyImage(g_Device, g_FontImage, g_Allocator); g_FontImage = VK_NULL_HANDLE; } - if (g_FontMemory) { vkFreeMemory(g_Device, g_FontMemory, g_Allocator); g_FontMemory = VK_NULL_HANDLE; } - if (g_FontSampler) { vkDestroySampler(g_Device, g_FontSampler, g_Allocator); g_FontSampler = VK_NULL_HANDLE; } - if (g_DescriptorSetLayout) { vkDestroyDescriptorSetLayout(g_Device, g_DescriptorSetLayout, g_Allocator); g_DescriptorSetLayout = VK_NULL_HANDLE; } - if (g_PipelineLayout) { vkDestroyPipelineLayout(g_Device, g_PipelineLayout, g_Allocator); g_PipelineLayout = VK_NULL_HANDLE; } - if (g_Pipeline) { vkDestroyPipeline(g_Device, g_Pipeline, g_Allocator); g_Pipeline = VK_NULL_HANDLE; } -} - -void ImGui_ImplVulkan_InvalidateFrameDeviceObjects() +static void ImGui_ImplVulkan_InvalidateFrameDeviceObjects() { for (int i = 0; i < g_FramesDataBuffers.Size; i++) { @@ -753,6 +753,20 @@ void ImGui_ImplVulkan_InvalidateFrameDeviceObjects() g_FramesDataBuffers.clear(); } +void ImGui_ImplVulkan_InvalidateDeviceObjects() +{ + ImGui_ImplVulkan_InvalidateFrameDeviceObjects(); + ImGui_ImplVulkan_InvalidateFontUploadObjects(); + + if (g_FontView) { vkDestroyImageView(g_Device, g_FontView, g_Allocator); g_FontView = VK_NULL_HANDLE; } + if (g_FontImage) { vkDestroyImage(g_Device, g_FontImage, g_Allocator); g_FontImage = VK_NULL_HANDLE; } + if (g_FontMemory) { vkFreeMemory(g_Device, g_FontMemory, g_Allocator); g_FontMemory = VK_NULL_HANDLE; } + if (g_FontSampler) { vkDestroySampler(g_Device, g_FontSampler, g_Allocator); g_FontSampler = VK_NULL_HANDLE; } + if (g_DescriptorSetLayout) { vkDestroyDescriptorSetLayout(g_Device, g_DescriptorSetLayout, g_Allocator); g_DescriptorSetLayout = VK_NULL_HANDLE; } + if (g_PipelineLayout) { vkDestroyPipelineLayout(g_Device, g_PipelineLayout, g_Allocator); g_PipelineLayout = VK_NULL_HANDLE; } + if (g_Pipeline) { vkDestroyPipeline(g_Device, g_Pipeline, g_Allocator); g_Pipeline = VK_NULL_HANDLE; } +} + bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass render_pass) { ImGuiIO& io = ImGui::GetIO(); @@ -788,7 +802,6 @@ bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass rend void ImGui_ImplVulkan_Shutdown() { - ImGui_ImplVulkan_InvalidateFrameDeviceObjects(); ImGui_ImplVulkan_InvalidateDeviceObjects(); } @@ -811,6 +824,7 @@ void ImGui_ImplVulkan_SetFramesQueueSize(int frames_queue_size) //------------------------------------------------------------------------- // Internal / Miscellaneous Vulkan Helpers +// (Used by example's main.cpp. Used by multi-viewport features. PROBABLY NOT used by your own app.) //------------------------------------------------------------------------- // You probably do NOT need to use or care about those functions. // Those functions only exist because: @@ -818,9 +832,10 @@ void ImGui_ImplVulkan_SetFramesQueueSize(int frames_queue_size) // 2) the upcoming multi-viewport feature will need them internally. // Generally we avoid exposing any kind of superfluous high-level helpers in the bindings, // but it is too much code to duplicate everywhere so we exceptionally expose them. -// Your application/engine will likely already have code to setup all that stuff (swap chain, render pass, frame buffers, etc.). +// +// Your application/engine will likely _already_ have code to setup all that stuff (swap chain, render pass, frame buffers, etc.). // You may read this code to learn about Vulkan, but it is recommended you use you own custom tailored code to do equivalent work. -// (those functions do not interact with any of the state used by the regular ImGui_ImplVulkan_XXX functions) +// (The ImGui_ImplVulkanH_XXX functions do not interact with any of the state used by the regular ImGui_ImplVulkan_XXX functions) //------------------------------------------------------------------------- #include // malloc @@ -917,7 +932,7 @@ VkPresentModeKHR ImGui_ImplVulkanH_SelectPresentMode(VkPhysicalDevice physical_d return VK_PRESENT_MODE_FIFO_KHR; // Always available } -void ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(VkPhysicalDevice physical_device, VkDevice device, uint32_t queue_family, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator) +void ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator) { IM_ASSERT(physical_device != VK_NULL_HANDLE && device != VK_NULL_HANDLE); (void)physical_device; @@ -975,7 +990,7 @@ int ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(VkPresentModeKHR present_m return 1; } -void ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count) +void ImGui_ImplVulkanH_CreateWindowDataSwapChain(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count) { VkResult err; VkSwapchainKHR old_swapchain = wd->Swapchain; @@ -1126,6 +1141,12 @@ void ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(VkPhysicalDevice } } +void ImGui_ImplVulkanH_CreateWindowData(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator, int width, int height, uint32_t min_image_count) +{ + ImGui_ImplVulkanH_CreateWindowDataSwapChain(physical_device, device, wd, allocator, width, height, min_image_count); + ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(physical_device, device, wd, queue_family, allocator); +} + void ImGui_ImplVulkanH_DestroyWindowData(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator) { vkDeviceWaitIdle(device); // FIXME: We could wait on the Queue if we had the queue in wd-> (otherwise VulkanH functions can't use globals) diff --git a/examples/imgui_impl_vulkan.h b/examples/imgui_impl_vulkan.h index 495f6d47..1fb2711b 100644 --- a/examples/imgui_impl_vulkan.h +++ b/examples/imgui_impl_vulkan.h @@ -15,8 +15,6 @@ #include -//#define IMGUI_VK_QUEUED_FRAMES 2 - // Please zero-clear before use. struct ImGui_ImplVulkan_InitInfo { @@ -41,15 +39,14 @@ IMGUI_IMPL_API void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, V IMGUI_IMPL_API bool ImGui_ImplVulkan_CreateFontsTexture(VkCommandBuffer command_buffer); IMGUI_IMPL_API void ImGui_ImplVulkan_InvalidateFontUploadObjects(); -// Called by ImGui_ImplVulkan_Init() might be useful elsewhere. +// Called by ImGui_ImplVulkan_Init(), might be useful elsewhere. IMGUI_IMPL_API bool ImGui_ImplVulkan_CreateDeviceObjects(); IMGUI_IMPL_API void ImGui_ImplVulkan_InvalidateDeviceObjects(); -IMGUI_IMPL_API void ImGui_ImplVulkan_InvalidateFrameDeviceObjects(); //------------------------------------------------------------------------- // Internal / Miscellaneous Vulkan Helpers -// (Used by example's main.cpp. Used by multi-viewport features. Probably NOT used by your own app.) +// (Used by example's main.cpp. Used by multi-viewport features. PROBABLY NOT used by your own app.) //------------------------------------------------------------------------- // You probably do NOT need to use or care about those functions. // Those functions only exist because: @@ -57,18 +54,17 @@ IMGUI_IMPL_API void ImGui_ImplVulkan_InvalidateFrameDeviceObjects(); // 2) the upcoming multi-viewport feature will need them internally. // Generally we avoid exposing any kind of superfluous high-level helpers in the bindings, // but it is too much code to duplicate everywhere so we exceptionally expose them. -// Your application/engine will likely already have code to setup all that stuff (swap chain, render pass, frame buffers, etc.). +// +// Your application/engine will likely _already_ have code to setup all that stuff (swap chain, render pass, frame buffers, etc.). // You may read this code to learn about Vulkan, but it is recommended you use you own custom tailored code to do equivalent work. -// (those functions do not interact with any of the state used by the regular ImGui_ImplVulkan_XXX functions) +// (The ImGui_ImplVulkanH_XXX functions do not interact with any of the state used by the regular ImGui_ImplVulkan_XXX functions) //------------------------------------------------------------------------- struct ImGui_ImplVulkanH_FrameData; struct ImGui_ImplVulkanH_WindowData; -IMGUI_IMPL_API void ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(VkPhysicalDevice physical_device, VkDevice device, uint32_t queue_family, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator); -IMGUI_IMPL_API void ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count); +IMGUI_IMPL_API void ImGui_ImplVulkanH_CreateWindowData(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count); IMGUI_IMPL_API void ImGui_ImplVulkanH_DestroyWindowData(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator); -IMGUI_IMPL_API void ImGui_ImplVulkanH_DestroyFrameData(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_FrameData* fd, const VkAllocationCallbacks* allocator); IMGUI_IMPL_API VkSurfaceFormatKHR ImGui_ImplVulkanH_SelectSurfaceFormat(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkFormat* request_formats, int request_formats_count, VkColorSpaceKHR request_color_space); IMGUI_IMPL_API VkPresentModeKHR ImGui_ImplVulkanH_SelectPresentMode(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkPresentModeKHR* request_modes, int request_modes_count); IMGUI_IMPL_API int ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(VkPresentModeKHR present_mode); From 6bf981c85c6045da790c50f17527c8c7f021b4f6 Mon Sep 17 00:00:00 2001 From: omar Date: Wed, 3 Apr 2019 19:21:06 +0200 Subject: [PATCH 06/19] Vulkan: More renaming. Comments. --- examples/example_glfw_vulkan/main.cpp | 10 +++++---- examples/example_sdl_vulkan/main.cpp | 10 +++++---- examples/imgui_impl_vulkan.cpp | 29 +++++++++++++++++---------- examples/imgui_impl_vulkan.h | 22 +++++++++++++------- 4 files changed, 45 insertions(+), 26 deletions(-) diff --git a/examples/example_glfw_vulkan/main.cpp b/examples/example_glfw_vulkan/main.cpp index fbb2f499..f9f260d7 100644 --- a/examples/example_glfw_vulkan/main.cpp +++ b/examples/example_glfw_vulkan/main.cpp @@ -1,10 +1,12 @@ // dear imgui: standalone example application for Glfw + Vulkan // If you are new to dear imgui, see examples/README.txt and documentation at the top of imgui.cpp. -// Important note to the reader who wish to integrate imgui_impl_vulkan.cpp/.h in their own application. +// Important note to the reader who wish to integrate imgui_impl_vulkan.cpp/.h in their own engine/app. // - Common ImGui_ImplVulkan_XXXX functions and structures are used to interface with imgui_impl_vulkan.cpp/.h. -// - Helper ImGui_ImplVulkanH_XXXX functions and structures are used by this example (main.cpp) and by imgui_impl_vulkan.cpp, -// but should PROBABLY NOT be used by your own app code. Read comments in imgui_impl_vulkan.h. +// You will use those if you want to use this rendering back-end in your engine/app. +// - Helper ImGui_ImplVulkanH_XXXX functions and structures are only used by this example (main.cpp) and by +// the back-end itself (imgui_impl_vulkan.cpp), but should PROBABLY NOT be used by your own engine/app code. +// Read comments in imgui_impl_vulkan.h. #include "imgui.h" #include "imgui_impl_glfw.h" @@ -424,7 +426,7 @@ int main(int, char**) err = vkDeviceWaitIdle(g_Device); check_vk_result(err); - ImGui_ImplVulkan_InvalidateFontUploadObjects(); + ImGui_ImplVulkan_DestroyFontUploadObjects(); } bool show_demo_window = true; diff --git a/examples/example_sdl_vulkan/main.cpp b/examples/example_sdl_vulkan/main.cpp index af331302..6c5684e8 100644 --- a/examples/example_sdl_vulkan/main.cpp +++ b/examples/example_sdl_vulkan/main.cpp @@ -1,10 +1,12 @@ // dear imgui: standalone example application for SDL2 + Vulkan // If you are new to dear imgui, see examples/README.txt and documentation at the top of imgui.cpp. -// Important note to the reader who wish to integrate imgui_impl_vulkan.cpp/.h in their own application. +// Important note to the reader who wish to integrate imgui_impl_vulkan.cpp/.h in their own engine/app. // - Common ImGui_ImplVulkan_XXXX functions and structures are used to interface with imgui_impl_vulkan.cpp/.h. -// - Helper ImGui_ImplVulkanH_XXXX functions and structures are used by this example (main.cpp) and by imgui_impl_vulkan.cpp, -// but should PROBABLY NOT be used by your own app code. Read comments in imgui_impl_vulkan.h. +// You will use those if you want to use this rendering back-end in your engine/app. +// - Helper ImGui_ImplVulkanH_XXXX functions and structures are only used by this example (main.cpp) and by +// the back-end itself (imgui_impl_vulkan.cpp), but should PROBABLY NOT be used by your own engine/app code. +// Read comments in imgui_impl_vulkan.h. #include "imgui.h" #include "imgui_impl_sdl.h" @@ -407,7 +409,7 @@ int main(int, char**) err = vkDeviceWaitIdle(g_Device); check_vk_result(err); - ImGui_ImplVulkan_InvalidateFontUploadObjects(); + ImGui_ImplVulkan_DestroyFontUploadObjects(); } bool show_demo_window = true; diff --git a/examples/imgui_impl_vulkan.cpp b/examples/imgui_impl_vulkan.cpp index 97a5b9a8..bdf1e661 100644 --- a/examples/imgui_impl_vulkan.cpp +++ b/examples/imgui_impl_vulkan.cpp @@ -11,6 +11,13 @@ // The aim of imgui_impl_vulkan.h/.cpp is to be usable in your engine without any modification. // IF YOU FEEL YOU NEED TO MAKE ANY CHANGE TO THIS CODE, please share them and your feedback at https://github.com/ocornut/imgui/ +// Important note to the reader who wish to integrate imgui_impl_vulkan.cpp/.h in their own engine/app. +// - Common ImGui_ImplVulkan_XXXX functions and structures are used to interface with imgui_impl_vulkan.cpp/.h. +// You will use those if you want to use this rendering back-end in your engine/app. +// - Helper ImGui_ImplVulkanH_XXXX functions and structures are only used by this example (main.cpp) and by +// the back-end itself (imgui_impl_vulkan.cpp), but should PROBABLY NOT be used by your own engine/app code. +// Read comments in imgui_impl_vulkan.h. + // CHANGELOG // (minor and older changes stripped away, please see git history for details) // 2019-XX-XX: Vulkan: Added FramesQueueSize field in ImGui_ImplVulkan_InitInfo, required for initialization (was previously a hard #define IMGUI_VK_QUEUED_FRAMES 2). Added ImGui_ImplVulkan_SetFramesQueueSize() to override FramesQueueSize while running. @@ -726,7 +733,7 @@ bool ImGui_ImplVulkan_CreateDeviceObjects() return true; } -void ImGui_ImplVulkan_InvalidateFontUploadObjects() +void ImGui_ImplVulkan_DestroyFontUploadObjects() { if (g_UploadBuffer) { @@ -753,10 +760,10 @@ static void ImGui_ImplVulkan_InvalidateFrameDeviceObjects() g_FramesDataBuffers.clear(); } -void ImGui_ImplVulkan_InvalidateDeviceObjects() +void ImGui_ImplVulkan_DestroyDeviceObjects() { ImGui_ImplVulkan_InvalidateFrameDeviceObjects(); - ImGui_ImplVulkan_InvalidateFontUploadObjects(); + ImGui_ImplVulkan_DestroyFontUploadObjects(); if (g_FontView) { vkDestroyImageView(g_Device, g_FontView, g_Allocator); g_FontView = VK_NULL_HANDLE; } if (g_FontImage) { vkDestroyImage(g_Device, g_FontImage, g_Allocator); g_FontImage = VK_NULL_HANDLE; } @@ -802,7 +809,7 @@ bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass rend void ImGui_ImplVulkan_Shutdown() { - ImGui_ImplVulkan_InvalidateDeviceObjects(); + ImGui_ImplVulkan_DestroyDeviceObjects(); } void ImGui_ImplVulkan_NewFrame() @@ -847,8 +854,8 @@ ImGui_ImplVulkanH_FrameData::ImGui_ImplVulkanH_FrameData() Fence = VK_NULL_HANDLE; ImageAcquiredSemaphore = VK_NULL_HANDLE; RenderCompleteSemaphore = VK_NULL_HANDLE; - BackBuffer = VK_NULL_HANDLE; - BackBufferView = VK_NULL_HANDLE; + Backbuffer = VK_NULL_HANDLE; + BackbufferView = VK_NULL_HANDLE; Framebuffer = VK_NULL_HANDLE; } @@ -1056,7 +1063,7 @@ void ImGui_ImplVulkanH_CreateWindowDataSwapChain(VkPhysicalDevice physical_devic for (int i = 0; i < (int)wd->FramesQueueSize; i++) { wd->Frames.push_back(ImGui_ImplVulkanH_FrameData()); - wd->Frames[i].BackBuffer = backbuffers[i]; + wd->Frames[i].Backbuffer = backbuffers[i]; } } if (old_swapchain) @@ -1114,8 +1121,8 @@ void ImGui_ImplVulkanH_CreateWindowDataSwapChain(VkPhysicalDevice physical_devic for (uint32_t i = 0; i < wd->FramesQueueSize; i++) { ImGui_ImplVulkanH_FrameData* fd = &wd->Frames[i]; - info.image = fd->BackBuffer; - err = vkCreateImageView(device, &info, allocator, &fd->BackBufferView); + info.image = fd->Backbuffer; + err = vkCreateImageView(device, &info, allocator, &fd->BackbufferView); check_vk_result(err); } } @@ -1134,7 +1141,7 @@ void ImGui_ImplVulkanH_CreateWindowDataSwapChain(VkPhysicalDevice physical_devic for (uint32_t i = 0; i < wd->FramesQueueSize; i++) { ImGui_ImplVulkanH_FrameData* fd = &wd->Frames[i]; - attachment[0] = fd->BackBufferView; + attachment[0] = fd->BackbufferView; err = vkCreateFramebuffer(device, &info, allocator, &fd->Framebuffer); check_vk_result(err); } @@ -1173,6 +1180,6 @@ void ImGui_ImplVulkanH_DestroyFrameData(VkInstance instance, VkDevice device, Im fd->CommandPool = VK_NULL_HANDLE; fd->ImageAcquiredSemaphore = fd->RenderCompleteSemaphore = VK_NULL_HANDLE; - vkDestroyImageView(device, fd->BackBufferView, allocator); + vkDestroyImageView(device, fd->BackbufferView, allocator); vkDestroyFramebuffer(device, fd->Framebuffer, allocator); } diff --git a/examples/imgui_impl_vulkan.h b/examples/imgui_impl_vulkan.h index 1fb2711b..6306f30e 100644 --- a/examples/imgui_impl_vulkan.h +++ b/examples/imgui_impl_vulkan.h @@ -11,11 +11,19 @@ // The aim of imgui_impl_vulkan.h/.cpp is to be usable in your engine without any modification. // IF YOU FEEL YOU NEED TO MAKE ANY CHANGE TO THIS CODE, please share them and your feedback at https://github.com/ocornut/imgui/ +// Important note to the reader who wish to integrate imgui_impl_vulkan.cpp/.h in their own engine/app. +// - Common ImGui_ImplVulkan_XXXX functions and structures are used to interface with imgui_impl_vulkan.cpp/.h. +// You will use those if you want to use this rendering back-end in your engine/app. +// - Helper ImGui_ImplVulkanH_XXXX functions and structures are only used by this example (main.cpp) and by +// the back-end itself (imgui_impl_vulkan.cpp), but should PROBABLY NOT be used by your own engine/app code. +// Read comments in imgui_impl_vulkan.h. + #pragma once #include -// Please zero-clear before use. +// Initialization data, for ImGui_ImplVulkan_Init() +// [Please zero-clear before use!] struct ImGui_ImplVulkan_InitInfo { VkInstance Instance; @@ -37,16 +45,16 @@ IMGUI_IMPL_API void ImGui_ImplVulkan_NewFrame(); IMGUI_IMPL_API void ImGui_ImplVulkan_SetFramesQueueSize(int frames_queue_size); // To override FramesQueueSize after initialization IMGUI_IMPL_API void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer command_buffer); IMGUI_IMPL_API bool ImGui_ImplVulkan_CreateFontsTexture(VkCommandBuffer command_buffer); -IMGUI_IMPL_API void ImGui_ImplVulkan_InvalidateFontUploadObjects(); +IMGUI_IMPL_API void ImGui_ImplVulkan_DestroyFontUploadObjects(); // Called by ImGui_ImplVulkan_Init(), might be useful elsewhere. IMGUI_IMPL_API bool ImGui_ImplVulkan_CreateDeviceObjects(); -IMGUI_IMPL_API void ImGui_ImplVulkan_InvalidateDeviceObjects(); +IMGUI_IMPL_API void ImGui_ImplVulkan_DestroyDeviceObjects(); //------------------------------------------------------------------------- // Internal / Miscellaneous Vulkan Helpers -// (Used by example's main.cpp. Used by multi-viewport features. PROBABLY NOT used by your own app.) +// (Used by example's main.cpp. Used by multi-viewport features. PROBABLY NOT used by your own engine/app.) //------------------------------------------------------------------------- // You probably do NOT need to use or care about those functions. // Those functions only exist because: @@ -55,7 +63,7 @@ IMGUI_IMPL_API void ImGui_ImplVulkan_InvalidateDeviceObjects(); // Generally we avoid exposing any kind of superfluous high-level helpers in the bindings, // but it is too much code to duplicate everywhere so we exceptionally expose them. // -// Your application/engine will likely _already_ have code to setup all that stuff (swap chain, render pass, frame buffers, etc.). +// Your engine/app will likely _already_ have code to setup all that stuff (swap chain, render pass, frame buffers, etc.). // You may read this code to learn about Vulkan, but it is recommended you use you own custom tailored code to do equivalent work. // (The ImGui_ImplVulkanH_XXX functions do not interact with any of the state used by the regular ImGui_ImplVulkan_XXX functions) //------------------------------------------------------------------------- @@ -78,8 +86,8 @@ struct ImGui_ImplVulkanH_FrameData VkFence Fence; VkSemaphore ImageAcquiredSemaphore; VkSemaphore RenderCompleteSemaphore; - VkImage BackBuffer; - VkImageView BackBufferView; + VkImage Backbuffer; + VkImageView BackbufferView; VkFramebuffer Framebuffer; IMGUI_IMPL_API ImGui_ImplVulkanH_FrameData(); From 66f4be2e13105d7ae636eff161ff1644275227bd Mon Sep 17 00:00:00 2001 From: omar Date: Thu, 4 Apr 2019 22:27:29 +0200 Subject: [PATCH 07/19] Vulkan: Added extra parameter to ImGui_ImplVulkan_RenderDrawData(). User is in charge of owning/storing 1 ImGui_ImplVulkan_FrameRenderBuffers per in-flight rendering frame. Removed ImGui_ImplVulkan_SetFramesQueueSize. (#2461, #2348, #2378, #2097, #2071, #1677) --- docs/CHANGELOG.txt | 10 +- examples/example_glfw_vulkan/main.cpp | 7 +- examples/example_sdl_vulkan/main.cpp | 7 +- examples/imgui_impl_vulkan.cpp | 178 ++++++++++---------------- examples/imgui_impl_vulkan.h | 35 +++-- 5 files changed, 109 insertions(+), 128 deletions(-) diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index 6046f19b..0b3ed421 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -34,6 +34,13 @@ HOW TO UPDATE? ----------------------------------------------------------------------- Breaking Changes: +- Examples: Vulkan: Added extra parameter to ImGui_ImplVulkan_RenderDrawData(). Engine/app is + in charge of owning/storing 1 ImGui_ImplVulkan_FrameRenderBuffers per in-flight rendering frame. + (The demo helper ImGui_ImplVulkanH_WindowData structure carries them.) (#2461, #2348, #2378, #2097) +- Examples: Vulkan: Added FramesQueueSize field in ImGui_ImplVulkan_InitInfo, required during + initialization to specify the number of in-flight image required by the swap chain. + (was previously a hard #define IMGUI_VK_QUEUED_FRAMES 2). (#2071, #1677) [@nathanvoglsam] + Other Changes: - InputText: Fixed selection background starts rendering one frame after the cursor movement @@ -49,9 +56,6 @@ Other Changes: - Examples: OpenGL3: Minor tweaks + not calling glBindBuffer more than necessary in the render loop. - Examples: Vulkan: Added missing support for 32-bit indices (#define ImDrawIdx unsigned int). - Examples: Vulkan: Avoid passing negative coordinates to vkCmdSetScissor, which debug validation layers do not like. -- Examples: Vulkan: Added FramesQueueSize field in ImGui_ImplVulkan_InitInfo, required during - initialization (was previously a hard #define IMGUI_VK_QUEUED_FRAMES 2). (#2071) [@nathanvoglsam] - Added ImGui_ImplVulkan_SetFramesQueueSize() to override FramesQueueSize while running. - Examples: DirectX9: Fixed erroneous assert in ImGui_ImplDX9_InvalidateDeviceObjects(). (#2454) - Examples: GLUT: Fixed existing FreeGLUT example to work with regular GLUT. (#2465) [@andrewwillmott] - Examples: GLUT: Renamed imgui_impl_freeglut.cpp/.h to imgui_impl_glut.cpp/.h. (#2465) [@andrewwillmott] diff --git a/examples/example_glfw_vulkan/main.cpp b/examples/example_glfw_vulkan/main.cpp index f9f260d7..037d4791 100644 --- a/examples/example_glfw_vulkan/main.cpp +++ b/examples/example_glfw_vulkan/main.cpp @@ -115,6 +115,7 @@ static void SetupVulkan(const char** extensions, uint32_t extensions_count) uint32_t gpu_count; err = vkEnumeratePhysicalDevices(g_Instance, &gpu_count, NULL); check_vk_result(err); + IM_ASSERT(gpu_count > 0); VkPhysicalDevice* gpus = (VkPhysicalDevice*)malloc(sizeof(VkPhysicalDevice) * gpu_count); err = vkEnumeratePhysicalDevices(g_Instance, &gpu_count, gpus); @@ -219,7 +220,7 @@ static void SetupVulkanWindowData(ImGui_ImplVulkanH_WindowData* wd, VkSurfaceKHR //printf("[vulkan] Selected PresentMode = %d\n", wd->PresentMode); // Create SwapChain, RenderPass, Framebuffer, etc. - ImGui_ImplVulkanH_CreateWindowData(g_PhysicalDevice, g_Device, wd, g_QueueFamily, g_Allocator, width, height, g_MinImageCount); + ImGui_ImplVulkanH_CreateWindowData(g_Instance, g_PhysicalDevice, g_Device, wd, g_QueueFamily, g_Allocator, width, height, g_MinImageCount); IM_ASSERT(wd->FramesQueueSize >= 2); } @@ -277,7 +278,7 @@ static void FrameRender(ImGui_ImplVulkanH_WindowData* wd) } // Record Imgui Draw Data and draw funcs into command buffer - ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), fd->CommandBuffer); + ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), fd->CommandBuffer, &fd->RenderBuffers); // Submit command buffer vkCmdEndRenderPass(fd->CommandBuffer); @@ -444,7 +445,7 @@ int main(int, char**) glfwPollEvents(); if (g_WantSwapChainRebuild) { - ImGui_ImplVulkanH_CreateWindowData(g_PhysicalDevice, g_Device, &g_WindowData, g_QueueFamily, g_Allocator, g_ResizeWidth, g_ResizeHeight, g_MinImageCount); + ImGui_ImplVulkanH_CreateWindowData(g_Instance, g_PhysicalDevice, g_Device, &g_WindowData, g_QueueFamily, g_Allocator, g_ResizeWidth, g_ResizeHeight, g_MinImageCount); ImGui_ImplVulkan_SetFramesQueueSize(g_WindowData.FramesQueueSize); g_WindowData.FrameIndex = 0; g_WantSwapChainRebuild = false; diff --git a/examples/example_sdl_vulkan/main.cpp b/examples/example_sdl_vulkan/main.cpp index 6c5684e8..e4721943 100644 --- a/examples/example_sdl_vulkan/main.cpp +++ b/examples/example_sdl_vulkan/main.cpp @@ -106,6 +106,7 @@ static void SetupVulkan(const char** extensions, uint32_t extensions_count) uint32_t gpu_count; err = vkEnumeratePhysicalDevices(g_Instance, &gpu_count, NULL); check_vk_result(err); + IM_ASSERT(gpu_count > 0); VkPhysicalDevice* gpus = (VkPhysicalDevice*)malloc(sizeof(VkPhysicalDevice) * gpu_count); err = vkEnumeratePhysicalDevices(g_Instance, &gpu_count, gpus); @@ -210,7 +211,7 @@ static void SetupVulkanWindowData(ImGui_ImplVulkanH_WindowData* wd, VkSurfaceKHR //printf("[vulkan] Selected PresentMode = %d\n", wd->PresentMode); // Create SwapChain, RenderPass, Framebuffer, etc. - ImGui_ImplVulkanH_CreateWindowData(g_PhysicalDevice, g_Device, wd, g_QueueFamily, g_Allocator, width, height, g_MinImageCount); + ImGui_ImplVulkanH_CreateWindowData(g_Instance, g_PhysicalDevice, g_Device, wd, g_QueueFamily, g_Allocator, width, height, g_MinImageCount); IM_ASSERT(wd->FramesQueueSize >= 2); } @@ -268,7 +269,7 @@ static void FrameRender(ImGui_ImplVulkanH_WindowData* wd) } // Record Imgui Draw Data and draw funcs into command buffer - ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), fd->CommandBuffer); + ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), fd->CommandBuffer, &fd->RenderBuffers); // Submit command buffer vkCmdEndRenderPass(fd->CommandBuffer); @@ -441,7 +442,7 @@ int main(int, char**) if (g_WantSwapChainRebuild) { - ImGui_ImplVulkanH_CreateWindowData(g_PhysicalDevice, g_Device, &g_WindowData, g_QueueFamily, g_Allocator, g_WindowData.Width, g_WindowData.Height, g_MinImageCount); + ImGui_ImplVulkanH_CreateWindowData(g_Instance, g_PhysicalDevice, g_Device, &g_WindowData, g_QueueFamily, g_Allocator, g_WindowData.Width, g_WindowData.Height, g_MinImageCount); ImGui_ImplVulkan_SetFramesQueueSize(g_WindowData.FramesQueueSize); g_WindowData.FrameIndex = 0; g_WantSwapChainRebuild = false; diff --git a/examples/imgui_impl_vulkan.cpp b/examples/imgui_impl_vulkan.cpp index bdf1e661..61351679 100644 --- a/examples/imgui_impl_vulkan.cpp +++ b/examples/imgui_impl_vulkan.cpp @@ -20,7 +20,9 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) -// 2019-XX-XX: Vulkan: Added FramesQueueSize field in ImGui_ImplVulkan_InitInfo, required for initialization (was previously a hard #define IMGUI_VK_QUEUED_FRAMES 2). Added ImGui_ImplVulkan_SetFramesQueueSize() to override FramesQueueSize while running. +// 2019-XX-XX: *BREAKING CHANGE*: Vulkan: Added extra parameter to ImGui_ImplVulkan_RenderDrawData(). Engine/app is in charge of owning/storing 1 ImGui_ImplVulkan_FrameRenderBuffers per in-flight rendering frame. (The demo helper ImGui_ImplVulkanH_WindowData structure carries them.) +// 2019-XX-XX: *BREAKING CHANGE*: Vulkan: Added FramesQueueSize field in ImGui_ImplVulkan_InitInfo, required for initialization (was previously a hard #define IMGUI_VK_QUEUED_FRAMES 2). Added ImGui_ImplVulkan_SetFramesQueueSize() to override FramesQueueSize while running. +// 2019-XX-XX: Vulkan: Added VkInstance argument to ImGui_ImplVulkanH_CreateWindowData() optional helper. // 2019-04-04: Vulkan: Avoid passing negative coordinates to vkCmdSetScissor, which debug validation layers do not like. // 2019-04-01: Vulkan: Support for 32-bit index buffer (#define ImDrawIdx unsigned int). // 2019-02-16: Vulkan: Viewport and clipping rectangles correctly using draw_data->FramebufferScale to allow retina display. @@ -44,51 +46,37 @@ // Vulkan data static const VkAllocationCallbacks* g_Allocator = NULL; -static VkPhysicalDevice g_PhysicalDevice = VK_NULL_HANDLE; -static VkInstance g_Instance = VK_NULL_HANDLE; -static VkDevice g_Device = VK_NULL_HANDLE; -static uint32_t g_QueueFamily = (uint32_t)-1; -static VkQueue g_Queue = VK_NULL_HANDLE; -static VkPipelineCache g_PipelineCache = VK_NULL_HANDLE; -static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE; -static VkRenderPass g_RenderPass = VK_NULL_HANDLE; -static void (*g_CheckVkResultFn)(VkResult err) = NULL; +static VkPhysicalDevice g_PhysicalDevice = VK_NULL_HANDLE; +static VkInstance g_Instance = VK_NULL_HANDLE; +static VkDevice g_Device = VK_NULL_HANDLE; +static uint32_t g_QueueFamily = (uint32_t)-1; +static VkQueue g_Queue = VK_NULL_HANDLE; +static VkPipelineCache g_PipelineCache = VK_NULL_HANDLE; +static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE; +static int g_FramesQueueSize = 0; +static VkRenderPass g_RenderPass = VK_NULL_HANDLE; +static void (*g_CheckVkResultFn)(VkResult err) = NULL; -static VkDeviceSize g_BufferMemoryAlignment = 256; -static VkPipelineCreateFlags g_PipelineCreateFlags = 0; - -static VkDescriptorSetLayout g_DescriptorSetLayout = VK_NULL_HANDLE; -static VkPipelineLayout g_PipelineLayout = VK_NULL_HANDLE; -static VkDescriptorSet g_DescriptorSet = VK_NULL_HANDLE; -static VkPipeline g_Pipeline = VK_NULL_HANDLE; - -// Frame data -struct FrameDataForRender -{ - VkDeviceMemory VertexBufferMemory; - VkDeviceMemory IndexBufferMemory; - VkDeviceSize VertexBufferSize; - VkDeviceSize IndexBufferSize; - VkBuffer VertexBuffer; - VkBuffer IndexBuffer; - - FrameDataForRender() { VertexBufferMemory = IndexBufferMemory = VK_NULL_HANDLE; VertexBufferSize = IndexBufferSize = VK_NULL_HANDLE; VertexBuffer = IndexBuffer = VK_NULL_HANDLE; } -}; -static int g_FrameIndex = 0; -static ImVector g_FramesDataBuffers; +static VkDeviceSize g_BufferMemoryAlignment = 256; +static VkPipelineCreateFlags g_PipelineCreateFlags = 0x00; +static VkDescriptorSetLayout g_DescriptorSetLayout = VK_NULL_HANDLE; +static VkPipelineLayout g_PipelineLayout = VK_NULL_HANDLE; +static VkDescriptorSet g_DescriptorSet = VK_NULL_HANDLE; +static VkPipeline g_Pipeline = VK_NULL_HANDLE; // Font data -static VkSampler g_FontSampler = VK_NULL_HANDLE; -static VkDeviceMemory g_FontMemory = VK_NULL_HANDLE; -static VkImage g_FontImage = VK_NULL_HANDLE; -static VkImageView g_FontView = VK_NULL_HANDLE; -static VkDeviceMemory g_UploadBufferMemory = VK_NULL_HANDLE; -static VkBuffer g_UploadBuffer = VK_NULL_HANDLE; +static VkSampler g_FontSampler = VK_NULL_HANDLE; +static VkDeviceMemory g_FontMemory = VK_NULL_HANDLE; +static VkImage g_FontImage = VK_NULL_HANDLE; +static VkImageView g_FontView = VK_NULL_HANDLE; +static VkDeviceMemory g_UploadBufferMemory = VK_NULL_HANDLE; +static VkBuffer g_UploadBuffer = VK_NULL_HANDLE; // Forward Declarations void ImGui_ImplVulkanH_DestroyFrameData(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_FrameData* fd, const VkAllocationCallbacks* allocator); -void ImGui_ImplVulkanH_CreateWindowDataSwapChain(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count); -void ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator); +void ImGui_ImplVulkanH_DestroyFrameRenderBuffers(VkInstance instance, VkDevice device, ImGui_ImplVulkan_FrameRenderBuffers* frb, const VkAllocationCallbacks* allocator); +void ImGui_ImplVulkanH_CreateWindowDataSwapChain(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count); +void ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator); //----------------------------------------------------------------------------- // SHADERS @@ -253,7 +241,7 @@ static void CreateOrResizeBuffer(VkBuffer& buffer, VkDeviceMemory& buffer_memory // Render function // (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop) -void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer command_buffer) +void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer command_buffer, ImGui_ImplVulkan_FrameRenderBuffers* fd) { // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates) int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x); @@ -262,8 +250,6 @@ void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer comm return; VkResult err; - FrameDataForRender* fd = &g_FramesDataBuffers[g_FrameIndex]; - g_FrameIndex = (g_FrameIndex + 1) % g_FramesDataBuffers.Size; // Create the Vertex and Index buffers: size_t vertex_size = draw_data->TotalVtxCount * sizeof(ImDrawVert); @@ -747,22 +733,8 @@ void ImGui_ImplVulkan_DestroyFontUploadObjects() } } -static void ImGui_ImplVulkan_InvalidateFrameDeviceObjects() -{ - for (int i = 0; i < g_FramesDataBuffers.Size; i++) - { - FrameDataForRender* fd = &g_FramesDataBuffers[i]; - if (fd->VertexBuffer) { vkDestroyBuffer(g_Device, fd->VertexBuffer, g_Allocator); fd->VertexBuffer = VK_NULL_HANDLE; } - if (fd->VertexBufferMemory) { vkFreeMemory (g_Device, fd->VertexBufferMemory, g_Allocator); fd->VertexBufferMemory = VK_NULL_HANDLE; } - if (fd->IndexBuffer) { vkDestroyBuffer(g_Device, fd->IndexBuffer, g_Allocator); fd->IndexBuffer = VK_NULL_HANDLE; } - if (fd->IndexBufferMemory) { vkFreeMemory (g_Device, fd->IndexBufferMemory, g_Allocator); fd->IndexBufferMemory = VK_NULL_HANDLE; } - } - g_FramesDataBuffers.clear(); -} - void ImGui_ImplVulkan_DestroyDeviceObjects() { - ImGui_ImplVulkan_InvalidateFrameDeviceObjects(); ImGui_ImplVulkan_DestroyFontUploadObjects(); if (g_FontView) { vkDestroyImageView(g_Device, g_FontView, g_Allocator); g_FontView = VK_NULL_HANDLE; } @@ -795,13 +767,10 @@ bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass rend g_RenderPass = render_pass; g_PipelineCache = info->PipelineCache; g_DescriptorPool = info->DescriptorPool; + g_FramesQueueSize = info->FramesQueueSize; g_Allocator = info->Allocator; g_CheckVkResultFn = info->CheckVkResultFn; - g_FramesDataBuffers.reserve(info->FramesQueueSize); - for (int i = 0; i < info->FramesQueueSize; i++) - g_FramesDataBuffers.push_back(FrameDataForRender()); - ImGui_ImplVulkan_CreateDeviceObjects(); return true; @@ -818,14 +787,7 @@ void ImGui_ImplVulkan_NewFrame() void ImGui_ImplVulkan_SetFramesQueueSize(int frames_queue_size) { - if (frames_queue_size == g_FramesDataBuffers.Size) - return; - ImGui_ImplVulkan_InvalidateFrameDeviceObjects(); - - g_FrameIndex = 0; - g_FramesDataBuffers.reserve(frames_queue_size); - for (int i = 0; i < frames_queue_size; i++) - g_FramesDataBuffers.push_back(FrameDataForRender()); + (void)frames_queue_size; } @@ -840,39 +802,13 @@ void ImGui_ImplVulkan_SetFramesQueueSize(int frames_queue_size) // Generally we avoid exposing any kind of superfluous high-level helpers in the bindings, // but it is too much code to duplicate everywhere so we exceptionally expose them. // -// Your application/engine will likely _already_ have code to setup all that stuff (swap chain, render pass, frame buffers, etc.). +// Your engine/app will likely _already_ have code to setup all that stuff (swap chain, render pass, frame buffers, etc.). // You may read this code to learn about Vulkan, but it is recommended you use you own custom tailored code to do equivalent work. // (The ImGui_ImplVulkanH_XXX functions do not interact with any of the state used by the regular ImGui_ImplVulkan_XXX functions) //------------------------------------------------------------------------- #include // malloc -ImGui_ImplVulkanH_FrameData::ImGui_ImplVulkanH_FrameData() -{ - CommandPool = VK_NULL_HANDLE; - CommandBuffer = VK_NULL_HANDLE; - Fence = VK_NULL_HANDLE; - ImageAcquiredSemaphore = VK_NULL_HANDLE; - RenderCompleteSemaphore = VK_NULL_HANDLE; - Backbuffer = VK_NULL_HANDLE; - BackbufferView = VK_NULL_HANDLE; - Framebuffer = VK_NULL_HANDLE; -} - -ImGui_ImplVulkanH_WindowData::ImGui_ImplVulkanH_WindowData() -{ - Width = Height = 0; - Swapchain = VK_NULL_HANDLE; - Surface = VK_NULL_HANDLE; - memset(&SurfaceFormat, 0, sizeof(SurfaceFormat)); - PresentMode = VK_PRESENT_MODE_MAX_ENUM_KHR; - RenderPass = VK_NULL_HANDLE; - ClearEnable = true; - memset(&ClearValue, 0, sizeof(ClearValue)); - FramesQueueSize = 0; - FrameIndex = 0; -} - VkSurfaceFormatKHR ImGui_ImplVulkanH_SelectSurfaceFormat(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkFormat* request_formats, int request_formats_count, VkColorSpaceKHR request_color_space) { IM_ASSERT(request_formats != NULL); @@ -939,15 +875,16 @@ VkPresentModeKHR ImGui_ImplVulkanH_SelectPresentMode(VkPhysicalDevice physical_d return VK_PRESENT_MODE_FIFO_KHR; // Always available } -void ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator) +void ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator) { - IM_ASSERT(physical_device != VK_NULL_HANDLE && device != VK_NULL_HANDLE); + IM_ASSERT(instance != VK_NULL_HANDLE && physical_device != VK_NULL_HANDLE && device != VK_NULL_HANDLE); + (void)instance; (void)physical_device; (void)allocator; // Create Command Buffers VkResult err; - for (int i = 0; i < wd->Frames.Size; i++) + for (uint32_t i = 0; i < wd->FramesQueueSize; i++) { ImGui_ImplVulkanH_FrameData* fd = &wd->Frames[i]; { @@ -985,6 +922,7 @@ void ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(VkPhysicalDevice physical_ } } +// MinImageCount will usually turn into FramesQueueSize int ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(VkPresentModeKHR present_mode) { if (present_mode == VK_PRESENT_MODE_MAILBOX_KHR) @@ -997,17 +935,20 @@ int ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(VkPresentModeKHR present_m return 1; } -void ImGui_ImplVulkanH_CreateWindowDataSwapChain(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count) +// Also destroy old swap chain and in-flight frames data, if any. +void ImGui_ImplVulkanH_CreateWindowDataSwapChain(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count) { VkResult err; VkSwapchainKHR old_swapchain = wd->Swapchain; err = vkDeviceWaitIdle(device); check_vk_result(err); + // We don't use ImGui_ImplVulkanH_DestroyWindowData() because we want to preserve the old swapchain to create the new one. // Destroy old Framebuffer for (uint32_t i = 0; i < wd->FramesQueueSize; i++) - ImGui_ImplVulkanH_DestroyFrameData(g_Instance, device, &wd->Frames[i], allocator); - wd->Frames.clear(); + ImGui_ImplVulkanH_DestroyFrameData(instance, device, &wd->Frames[i], allocator); + delete[] wd->Frames; + wd->Frames = NULL; wd->FramesQueueSize = 0; if (wd->RenderPass) vkDestroyRenderPass(device, wd->RenderPass, allocator); @@ -1059,12 +1000,11 @@ void ImGui_ImplVulkanH_CreateWindowDataSwapChain(VkPhysicalDevice physical_devic err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->FramesQueueSize, backbuffers); check_vk_result(err); - wd->Frames.reserve((int)wd->FramesQueueSize); - for (int i = 0; i < (int)wd->FramesQueueSize; i++) - { - wd->Frames.push_back(ImGui_ImplVulkanH_FrameData()); + IM_ASSERT(wd->Frames == NULL); + wd->Frames = new ImGui_ImplVulkanH_FrameData[wd->FramesQueueSize]; + memset(wd->Frames, 0, sizeof(wd->Frames[0]) * wd->FramesQueueSize); + for (uint32_t i = 0; i < wd->FramesQueueSize; i++) wd->Frames[i].Backbuffer = backbuffers[i]; - } } if (old_swapchain) vkDestroySwapchainKHR(device, old_swapchain, allocator); @@ -1148,10 +1088,10 @@ void ImGui_ImplVulkanH_CreateWindowDataSwapChain(VkPhysicalDevice physical_devic } } -void ImGui_ImplVulkanH_CreateWindowData(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator, int width, int height, uint32_t min_image_count) +void ImGui_ImplVulkanH_CreateWindowData(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator, int width, int height, uint32_t min_image_count) { - ImGui_ImplVulkanH_CreateWindowDataSwapChain(physical_device, device, wd, allocator, width, height, min_image_count); - ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(physical_device, device, wd, queue_family, allocator); + ImGui_ImplVulkanH_CreateWindowDataSwapChain(instance, physical_device, device, wd, allocator, width, height, min_image_count); + ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(instance, physical_device, device, wd, queue_family, allocator); } void ImGui_ImplVulkanH_DestroyWindowData(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator) @@ -1159,11 +1099,14 @@ void ImGui_ImplVulkanH_DestroyWindowData(VkInstance instance, VkDevice device, I vkDeviceWaitIdle(device); // FIXME: We could wait on the Queue if we had the queue in wd-> (otherwise VulkanH functions can't use globals) //vkQueueWaitIdle(g_Queue); - for (int i = 0; i < wd->Frames.Size; i++) + for (uint32_t i = 0; i < wd->FramesQueueSize; i++) ImGui_ImplVulkanH_DestroyFrameData(instance, device, &wd->Frames[i], allocator); + delete[] wd->Frames; + wd->Frames = NULL; vkDestroyRenderPass(device, wd->RenderPass, allocator); vkDestroySwapchainKHR(device, wd->Swapchain, allocator); vkDestroySurfaceKHR(instance, wd->Surface, allocator); + *wd = ImGui_ImplVulkanH_WindowData(); } @@ -1182,4 +1125,17 @@ void ImGui_ImplVulkanH_DestroyFrameData(VkInstance instance, VkDevice device, Im vkDestroyImageView(device, fd->BackbufferView, allocator); vkDestroyFramebuffer(device, fd->Framebuffer, allocator); + + ImGui_ImplVulkanH_DestroyFrameRenderBuffers(instance, device, &fd->RenderBuffers, allocator); +} + +void ImGui_ImplVulkanH_DestroyFrameRenderBuffers(VkInstance instance, VkDevice device, ImGui_ImplVulkan_FrameRenderBuffers* frb, const VkAllocationCallbacks* allocator) +{ + (void)instance; + if (frb->VertexBuffer) { vkDestroyBuffer(device, frb->VertexBuffer, allocator); frb->VertexBuffer = VK_NULL_HANDLE; } + if (frb->VertexBufferMemory) { vkFreeMemory (device, frb->VertexBufferMemory, allocator); frb->VertexBufferMemory = VK_NULL_HANDLE; } + if (frb->IndexBuffer) { vkDestroyBuffer(device, frb->IndexBuffer, allocator); frb->IndexBuffer = VK_NULL_HANDLE; } + if (frb->IndexBufferMemory) { vkFreeMemory (device, frb->IndexBufferMemory, allocator); frb->IndexBufferMemory = VK_NULL_HANDLE; } + frb->VertexBufferSize = 0; + frb->IndexBufferSize = 0; } diff --git a/examples/imgui_impl_vulkan.h b/examples/imgui_impl_vulkan.h index 6306f30e..334910f6 100644 --- a/examples/imgui_impl_vulkan.h +++ b/examples/imgui_impl_vulkan.h @@ -38,12 +38,26 @@ struct ImGui_ImplVulkan_InitInfo void (*CheckVkResultFn)(VkResult err); }; +// Reusable buffers used for rendering by current in-flight frame, for ImGui_ImplVulkan_RenderDrawData() +// [Please zero-clear before use!] +// In the examples we store those in the helper ImGui_ImplVulkanH_FrameData structure, however as your own engine/app likely won't use the ImGui_Impl_VulkanH_XXXX helpers, +// you are expected to hold on as many ImGui_ImplVulkan_FrameRenderBuffers structures on your side as you have in-flight frames (== init_info.FramesQueueSize) +struct ImGui_ImplVulkan_FrameRenderBuffers +{ + VkDeviceMemory VertexBufferMemory; + VkDeviceMemory IndexBufferMemory; + VkDeviceSize VertexBufferSize; + VkDeviceSize IndexBufferSize; + VkBuffer VertexBuffer; + VkBuffer IndexBuffer; +}; + // Called by user code IMGUI_IMPL_API bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass render_pass); IMGUI_IMPL_API void ImGui_ImplVulkan_Shutdown(); IMGUI_IMPL_API void ImGui_ImplVulkan_NewFrame(); IMGUI_IMPL_API void ImGui_ImplVulkan_SetFramesQueueSize(int frames_queue_size); // To override FramesQueueSize after initialization -IMGUI_IMPL_API void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer command_buffer); +IMGUI_IMPL_API void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer command_buffer, ImGui_ImplVulkan_FrameRenderBuffers* buffers); IMGUI_IMPL_API bool ImGui_ImplVulkan_CreateFontsTexture(VkCommandBuffer command_buffer); IMGUI_IMPL_API void ImGui_ImplVulkan_DestroyFontUploadObjects(); @@ -71,7 +85,7 @@ IMGUI_IMPL_API void ImGui_ImplVulkan_DestroyDeviceObjects(); struct ImGui_ImplVulkanH_FrameData; struct ImGui_ImplVulkanH_WindowData; -IMGUI_IMPL_API void ImGui_ImplVulkanH_CreateWindowData(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count); +IMGUI_IMPL_API void ImGui_ImplVulkanH_CreateWindowData(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count); IMGUI_IMPL_API void ImGui_ImplVulkanH_DestroyWindowData(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator); IMGUI_IMPL_API VkSurfaceFormatKHR ImGui_ImplVulkanH_SelectSurfaceFormat(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkFormat* request_formats, int request_formats_count, VkColorSpaceKHR request_color_space); IMGUI_IMPL_API VkPresentModeKHR ImGui_ImplVulkanH_SelectPresentMode(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkPresentModeKHR* request_modes, int request_modes_count); @@ -79,6 +93,7 @@ IMGUI_IMPL_API int ImGui_ImplVulkanH_GetMinImageCountFromPresen // Helper structure to hold the data needed by one rendering frame // (Used by example's main.cpp. Used by multi-viewport features. Probably NOT used by your own app.) +// [Please zero-clear before use!] struct ImGui_ImplVulkanH_FrameData { VkCommandPool CommandPool; @@ -89,8 +104,7 @@ struct ImGui_ImplVulkanH_FrameData VkImage Backbuffer; VkImageView BackbufferView; VkFramebuffer Framebuffer; - - IMGUI_IMPL_API ImGui_ImplVulkanH_FrameData(); + ImGui_ImplVulkan_FrameRenderBuffers RenderBuffers; }; // Helper structure to hold the data needed by one rendering context into one OS window @@ -106,10 +120,15 @@ struct ImGui_ImplVulkanH_WindowData VkRenderPass RenderPass; bool ClearEnable; VkClearValue ClearValue; - uint32_t FramesQueueSize; // Number of simultaneous in-flight frames (returned by vkGetSwapchainImagesKHR) - uint32_t FrameIndex; // Current frame being rendered to (0 <= FrameIndex < FrameInFlightCount) - ImVector Frames; + uint32_t FrameIndex; // Current frame being rendered to (0 <= FrameIndex < FrameInFlightCount) + uint32_t FramesQueueSize; // Number of simultaneous in-flight frames (returned by vkGetSwapchainImagesKHR, usually derived from min_image_count) + ImGui_ImplVulkanH_FrameData* Frames; - IMGUI_IMPL_API ImGui_ImplVulkanH_WindowData(); + ImGui_ImplVulkanH_WindowData() + { + memset(this, 0, sizeof(*this)); + PresentMode = VK_PRESENT_MODE_MAX_ENUM_KHR; + ClearEnable = true; + } }; From 86f5945f497c42a665c8184edf04fb831c279ecb Mon Sep 17 00:00:00 2001 From: omar Date: Thu, 4 Apr 2019 22:36:12 +0200 Subject: [PATCH 08/19] Vulkan: Storing user info into a single g_VulkanInitInfo structure to simplify code. --- examples/imgui_impl_vulkan.cpp | 126 +++++++++++++++------------------ 1 file changed, 57 insertions(+), 69 deletions(-) diff --git a/examples/imgui_impl_vulkan.cpp b/examples/imgui_impl_vulkan.cpp index 61351679..25b6428b 100644 --- a/examples/imgui_impl_vulkan.cpp +++ b/examples/imgui_impl_vulkan.cpp @@ -45,18 +45,8 @@ #include // Vulkan data -static const VkAllocationCallbacks* g_Allocator = NULL; -static VkPhysicalDevice g_PhysicalDevice = VK_NULL_HANDLE; -static VkInstance g_Instance = VK_NULL_HANDLE; -static VkDevice g_Device = VK_NULL_HANDLE; -static uint32_t g_QueueFamily = (uint32_t)-1; -static VkQueue g_Queue = VK_NULL_HANDLE; -static VkPipelineCache g_PipelineCache = VK_NULL_HANDLE; -static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE; -static int g_FramesQueueSize = 0; +static ImGui_ImplVulkan_InitInfo g_VulkanInitInfo = {}; static VkRenderPass g_RenderPass = VK_NULL_HANDLE; -static void (*g_CheckVkResultFn)(VkResult err) = NULL; - static VkDeviceSize g_BufferMemoryAlignment = 256; static VkPipelineCreateFlags g_PipelineCreateFlags = 0x00; static VkDescriptorSetLayout g_DescriptorSetLayout = VK_NULL_HANDLE; @@ -193,8 +183,9 @@ static uint32_t __glsl_shader_frag_spv[] = static uint32_t ImGui_ImplVulkan_MemoryType(VkMemoryPropertyFlags properties, uint32_t type_bits) { + ImGui_ImplVulkan_InitInfo* v = &g_VulkanInitInfo; VkPhysicalDeviceMemoryProperties prop; - vkGetPhysicalDeviceMemoryProperties(g_PhysicalDevice, &prop); + vkGetPhysicalDeviceMemoryProperties(v->PhysicalDevice, &prop); for (uint32_t i = 0; i < prop.memoryTypeCount; i++) if ((prop.memoryTypes[i].propertyFlags & properties) == properties && type_bits & (1<CheckVkResultFn) + v->CheckVkResultFn(err); } static void CreateOrResizeBuffer(VkBuffer& buffer, VkDeviceMemory& buffer_memory, VkDeviceSize& p_buffer_size, size_t new_size, VkBufferUsageFlagBits usage) { + ImGui_ImplVulkan_InitInfo* v = &g_VulkanInitInfo; VkResult err; if (buffer != VK_NULL_HANDLE) - vkDestroyBuffer(g_Device, buffer, g_Allocator); + vkDestroyBuffer(v->Device, buffer, v->Allocator); if (buffer_memory) - vkFreeMemory(g_Device, buffer_memory, g_Allocator); + vkFreeMemory(v->Device, buffer_memory, v->Allocator); VkDeviceSize vertex_buffer_size_aligned = ((new_size - 1) / g_BufferMemoryAlignment + 1) * g_BufferMemoryAlignment; VkBufferCreateInfo buffer_info = {}; @@ -221,20 +214,20 @@ static void CreateOrResizeBuffer(VkBuffer& buffer, VkDeviceMemory& buffer_memory buffer_info.size = vertex_buffer_size_aligned; buffer_info.usage = usage; buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE; - err = vkCreateBuffer(g_Device, &buffer_info, g_Allocator, &buffer); + err = vkCreateBuffer(v->Device, &buffer_info, v->Allocator, &buffer); check_vk_result(err); VkMemoryRequirements req; - vkGetBufferMemoryRequirements(g_Device, buffer, &req); + vkGetBufferMemoryRequirements(v->Device, buffer, &req); g_BufferMemoryAlignment = (g_BufferMemoryAlignment > req.alignment) ? g_BufferMemoryAlignment : req.alignment; VkMemoryAllocateInfo alloc_info = {}; alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; alloc_info.allocationSize = req.size; alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits); - err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &buffer_memory); + err = vkAllocateMemory(v->Device, &alloc_info, v->Allocator, &buffer_memory); check_vk_result(err); - err = vkBindBufferMemory(g_Device, buffer, buffer_memory, 0); + err = vkBindBufferMemory(v->Device, buffer, buffer_memory, 0); check_vk_result(err); p_buffer_size = new_size; } @@ -249,6 +242,7 @@ void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer comm if (fb_width <= 0 || fb_height <= 0 || draw_data->TotalVtxCount == 0) return; + ImGui_ImplVulkan_InitInfo* v = &g_VulkanInitInfo; VkResult err; // Create the Vertex and Index buffers: @@ -263,9 +257,9 @@ void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer comm { ImDrawVert* vtx_dst = NULL; ImDrawIdx* idx_dst = NULL; - err = vkMapMemory(g_Device, fd->VertexBufferMemory, 0, vertex_size, 0, (void**)(&vtx_dst)); + err = vkMapMemory(v->Device, fd->VertexBufferMemory, 0, vertex_size, 0, (void**)(&vtx_dst)); check_vk_result(err); - err = vkMapMemory(g_Device, fd->IndexBufferMemory, 0, index_size, 0, (void**)(&idx_dst)); + err = vkMapMemory(v->Device, fd->IndexBufferMemory, 0, index_size, 0, (void**)(&idx_dst)); check_vk_result(err); for (int n = 0; n < draw_data->CmdListsCount; n++) { @@ -282,10 +276,10 @@ void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer comm range[1].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE; range[1].memory = fd->IndexBufferMemory; range[1].size = VK_WHOLE_SIZE; - err = vkFlushMappedMemoryRanges(g_Device, 2, range); + err = vkFlushMappedMemoryRanges(v->Device, 2, range); check_vk_result(err); - vkUnmapMemory(g_Device, fd->VertexBufferMemory); - vkUnmapMemory(g_Device, fd->IndexBufferMemory); + vkUnmapMemory(v->Device, fd->VertexBufferMemory); + vkUnmapMemory(v->Device, fd->IndexBufferMemory); } // Bind pipeline and descriptor sets: @@ -383,6 +377,7 @@ void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer comm bool ImGui_ImplVulkan_CreateFontsTexture(VkCommandBuffer command_buffer) { + ImGui_ImplVulkan_InitInfo* v = &g_VulkanInitInfo; ImGuiIO& io = ImGui::GetIO(); unsigned char* pixels; @@ -408,17 +403,17 @@ bool ImGui_ImplVulkan_CreateFontsTexture(VkCommandBuffer command_buffer) info.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; info.sharingMode = VK_SHARING_MODE_EXCLUSIVE; info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; - err = vkCreateImage(g_Device, &info, g_Allocator, &g_FontImage); + err = vkCreateImage(v->Device, &info, v->Allocator, &g_FontImage); check_vk_result(err); VkMemoryRequirements req; - vkGetImageMemoryRequirements(g_Device, g_FontImage, &req); + vkGetImageMemoryRequirements(v->Device, g_FontImage, &req); VkMemoryAllocateInfo alloc_info = {}; alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; alloc_info.allocationSize = req.size; alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, req.memoryTypeBits); - err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &g_FontMemory); + err = vkAllocateMemory(v->Device, &alloc_info, v->Allocator, &g_FontMemory); check_vk_result(err); - err = vkBindImageMemory(g_Device, g_FontImage, g_FontMemory, 0); + err = vkBindImageMemory(v->Device, g_FontImage, g_FontMemory, 0); check_vk_result(err); } @@ -432,7 +427,7 @@ bool ImGui_ImplVulkan_CreateFontsTexture(VkCommandBuffer command_buffer) info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; info.subresourceRange.levelCount = 1; info.subresourceRange.layerCount = 1; - err = vkCreateImageView(g_Device, &info, g_Allocator, &g_FontView); + err = vkCreateImageView(v->Device, &info, v->Allocator, &g_FontView); check_vk_result(err); } @@ -448,7 +443,7 @@ bool ImGui_ImplVulkan_CreateFontsTexture(VkCommandBuffer command_buffer) write_desc[0].descriptorCount = 1; write_desc[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; write_desc[0].pImageInfo = desc_image; - vkUpdateDescriptorSets(g_Device, 1, write_desc, 0, NULL); + vkUpdateDescriptorSets(v->Device, 1, write_desc, 0, NULL); } // Create the Upload Buffer: @@ -458,34 +453,34 @@ bool ImGui_ImplVulkan_CreateFontsTexture(VkCommandBuffer command_buffer) buffer_info.size = upload_size; buffer_info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT; buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE; - err = vkCreateBuffer(g_Device, &buffer_info, g_Allocator, &g_UploadBuffer); + err = vkCreateBuffer(v->Device, &buffer_info, v->Allocator, &g_UploadBuffer); check_vk_result(err); VkMemoryRequirements req; - vkGetBufferMemoryRequirements(g_Device, g_UploadBuffer, &req); + vkGetBufferMemoryRequirements(v->Device, g_UploadBuffer, &req); g_BufferMemoryAlignment = (g_BufferMemoryAlignment > req.alignment) ? g_BufferMemoryAlignment : req.alignment; VkMemoryAllocateInfo alloc_info = {}; alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; alloc_info.allocationSize = req.size; alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits); - err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &g_UploadBufferMemory); + err = vkAllocateMemory(v->Device, &alloc_info, v->Allocator, &g_UploadBufferMemory); check_vk_result(err); - err = vkBindBufferMemory(g_Device, g_UploadBuffer, g_UploadBufferMemory, 0); + err = vkBindBufferMemory(v->Device, g_UploadBuffer, g_UploadBufferMemory, 0); check_vk_result(err); } // Upload to Buffer: { char* map = NULL; - err = vkMapMemory(g_Device, g_UploadBufferMemory, 0, upload_size, 0, (void**)(&map)); + err = vkMapMemory(v->Device, g_UploadBufferMemory, 0, upload_size, 0, (void**)(&map)); check_vk_result(err); memcpy(map, pixels, upload_size); VkMappedMemoryRange range[1] = {}; range[0].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE; range[0].memory = g_UploadBufferMemory; range[0].size = upload_size; - err = vkFlushMappedMemoryRanges(g_Device, 1, range); + err = vkFlushMappedMemoryRanges(v->Device, 1, range); check_vk_result(err); - vkUnmapMemory(g_Device, g_UploadBufferMemory); + vkUnmapMemory(v->Device, g_UploadBufferMemory); } // Copy to Image: @@ -534,6 +529,7 @@ bool ImGui_ImplVulkan_CreateFontsTexture(VkCommandBuffer command_buffer) bool ImGui_ImplVulkan_CreateDeviceObjects() { + ImGui_ImplVulkan_InitInfo* v = &g_VulkanInitInfo; VkResult err; VkShaderModule vert_module; VkShaderModule frag_module; @@ -544,13 +540,13 @@ bool ImGui_ImplVulkan_CreateDeviceObjects() vert_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; vert_info.codeSize = sizeof(__glsl_shader_vert_spv); vert_info.pCode = (uint32_t*)__glsl_shader_vert_spv; - err = vkCreateShaderModule(g_Device, &vert_info, g_Allocator, &vert_module); + err = vkCreateShaderModule(v->Device, &vert_info, v->Allocator, &vert_module); check_vk_result(err); VkShaderModuleCreateInfo frag_info = {}; frag_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; frag_info.codeSize = sizeof(__glsl_shader_frag_spv); frag_info.pCode = (uint32_t*)__glsl_shader_frag_spv; - err = vkCreateShaderModule(g_Device, &frag_info, g_Allocator, &frag_module); + err = vkCreateShaderModule(v->Device, &frag_info, v->Allocator, &frag_module); check_vk_result(err); } @@ -567,7 +563,7 @@ bool ImGui_ImplVulkan_CreateDeviceObjects() info.minLod = -1000; info.maxLod = 1000; info.maxAnisotropy = 1.0f; - err = vkCreateSampler(g_Device, &info, g_Allocator, &g_FontSampler); + err = vkCreateSampler(v->Device, &info, v->Allocator, &g_FontSampler); check_vk_result(err); } @@ -583,7 +579,7 @@ bool ImGui_ImplVulkan_CreateDeviceObjects() info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; info.bindingCount = 1; info.pBindings = binding; - err = vkCreateDescriptorSetLayout(g_Device, &info, g_Allocator, &g_DescriptorSetLayout); + err = vkCreateDescriptorSetLayout(v->Device, &info, v->Allocator, &g_DescriptorSetLayout); check_vk_result(err); } @@ -591,10 +587,10 @@ bool ImGui_ImplVulkan_CreateDeviceObjects() { VkDescriptorSetAllocateInfo alloc_info = {}; alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; - alloc_info.descriptorPool = g_DescriptorPool; + alloc_info.descriptorPool = v->DescriptorPool; alloc_info.descriptorSetCount = 1; alloc_info.pSetLayouts = &g_DescriptorSetLayout; - err = vkAllocateDescriptorSets(g_Device, &alloc_info, &g_DescriptorSet); + err = vkAllocateDescriptorSets(v->Device, &alloc_info, &g_DescriptorSet); check_vk_result(err); } @@ -612,7 +608,7 @@ bool ImGui_ImplVulkan_CreateDeviceObjects() layout_info.pSetLayouts = set_layout; layout_info.pushConstantRangeCount = 1; layout_info.pPushConstantRanges = push_constants; - err = vkCreatePipelineLayout(g_Device, &layout_info, g_Allocator, &g_PipelineLayout); + err = vkCreatePipelineLayout(v->Device, &layout_info, v->Allocator, &g_PipelineLayout); check_vk_result(err); } @@ -710,40 +706,42 @@ bool ImGui_ImplVulkan_CreateDeviceObjects() info.pDynamicState = &dynamic_state; info.layout = g_PipelineLayout; info.renderPass = g_RenderPass; - err = vkCreateGraphicsPipelines(g_Device, g_PipelineCache, 1, &info, g_Allocator, &g_Pipeline); + err = vkCreateGraphicsPipelines(v->Device, v->PipelineCache, 1, &info, v->Allocator, &g_Pipeline); check_vk_result(err); - vkDestroyShaderModule(g_Device, vert_module, g_Allocator); - vkDestroyShaderModule(g_Device, frag_module, g_Allocator); + vkDestroyShaderModule(v->Device, vert_module, v->Allocator); + vkDestroyShaderModule(v->Device, frag_module, v->Allocator); return true; } void ImGui_ImplVulkan_DestroyFontUploadObjects() { + ImGui_ImplVulkan_InitInfo* v = &g_VulkanInitInfo; if (g_UploadBuffer) { - vkDestroyBuffer(g_Device, g_UploadBuffer, g_Allocator); + vkDestroyBuffer(v->Device, g_UploadBuffer, v->Allocator); g_UploadBuffer = VK_NULL_HANDLE; } if (g_UploadBufferMemory) { - vkFreeMemory(g_Device, g_UploadBufferMemory, g_Allocator); + vkFreeMemory(v->Device, g_UploadBufferMemory, v->Allocator); g_UploadBufferMemory = VK_NULL_HANDLE; } } void ImGui_ImplVulkan_DestroyDeviceObjects() { - ImGui_ImplVulkan_DestroyFontUploadObjects(); + ImGui_ImplVulkan_InitInfo* v = &g_VulkanInitInfo; - if (g_FontView) { vkDestroyImageView(g_Device, g_FontView, g_Allocator); g_FontView = VK_NULL_HANDLE; } - if (g_FontImage) { vkDestroyImage(g_Device, g_FontImage, g_Allocator); g_FontImage = VK_NULL_HANDLE; } - if (g_FontMemory) { vkFreeMemory(g_Device, g_FontMemory, g_Allocator); g_FontMemory = VK_NULL_HANDLE; } - if (g_FontSampler) { vkDestroySampler(g_Device, g_FontSampler, g_Allocator); g_FontSampler = VK_NULL_HANDLE; } - if (g_DescriptorSetLayout) { vkDestroyDescriptorSetLayout(g_Device, g_DescriptorSetLayout, g_Allocator); g_DescriptorSetLayout = VK_NULL_HANDLE; } - if (g_PipelineLayout) { vkDestroyPipelineLayout(g_Device, g_PipelineLayout, g_Allocator); g_PipelineLayout = VK_NULL_HANDLE; } - if (g_Pipeline) { vkDestroyPipeline(g_Device, g_Pipeline, g_Allocator); g_Pipeline = VK_NULL_HANDLE; } + ImGui_ImplVulkan_DestroyFontUploadObjects(); + if (g_FontView) { vkDestroyImageView(v->Device, g_FontView, v->Allocator); g_FontView = VK_NULL_HANDLE; } + if (g_FontImage) { vkDestroyImage(v->Device, g_FontImage, v->Allocator); g_FontImage = VK_NULL_HANDLE; } + if (g_FontMemory) { vkFreeMemory(v->Device, g_FontMemory, v->Allocator); g_FontMemory = VK_NULL_HANDLE; } + if (g_FontSampler) { vkDestroySampler(v->Device, g_FontSampler, v->Allocator); g_FontSampler = VK_NULL_HANDLE; } + if (g_DescriptorSetLayout) { vkDestroyDescriptorSetLayout(v->Device, g_DescriptorSetLayout, v->Allocator); g_DescriptorSetLayout = VK_NULL_HANDLE; } + if (g_PipelineLayout) { vkDestroyPipelineLayout(v->Device, g_PipelineLayout, v->Allocator); g_PipelineLayout = VK_NULL_HANDLE; } + if (g_Pipeline) { vkDestroyPipeline(v->Device, g_Pipeline, v->Allocator); g_Pipeline = VK_NULL_HANDLE; } } bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass render_pass) @@ -759,18 +757,8 @@ bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass rend IM_ASSERT(info->FramesQueueSize >= 2); IM_ASSERT(render_pass != VK_NULL_HANDLE); - g_Instance = info->Instance; - g_PhysicalDevice = info->PhysicalDevice; - g_Device = info->Device; - g_QueueFamily = info->QueueFamily; - g_Queue = info->Queue; + g_VulkanInitInfo = *info; g_RenderPass = render_pass; - g_PipelineCache = info->PipelineCache; - g_DescriptorPool = info->DescriptorPool; - g_FramesQueueSize = info->FramesQueueSize; - g_Allocator = info->Allocator; - g_CheckVkResultFn = info->CheckVkResultFn; - ImGui_ImplVulkan_CreateDeviceObjects(); return true; From 4f54a527ab047f7d668b978b2589c74cf66d2a24 Mon Sep 17 00:00:00 2001 From: omar Date: Thu, 4 Apr 2019 23:11:15 +0200 Subject: [PATCH 09/19] Vulkan: Renaming, we want InitInfo to source MinImageCount which is the "source" value (so viewport creation can use this). Made ImGui_ImplVulkan_DestroyFrameRenderBuffers public. (#2071) --- docs/CHANGELOG.txt | 4 +-- examples/example_glfw_vulkan/main.cpp | 21 ++++++++++----- examples/example_sdl_vulkan/main.cpp | 21 ++++++++++----- examples/imgui_impl_vulkan.cpp | 38 +++++++++++++++------------ examples/imgui_impl_vulkan.h | 5 ++-- 5 files changed, 56 insertions(+), 33 deletions(-) diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index 0b3ed421..b4f17df9 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -37,8 +37,8 @@ Breaking Changes: - Examples: Vulkan: Added extra parameter to ImGui_ImplVulkan_RenderDrawData(). Engine/app is in charge of owning/storing 1 ImGui_ImplVulkan_FrameRenderBuffers per in-flight rendering frame. (The demo helper ImGui_ImplVulkanH_WindowData structure carries them.) (#2461, #2348, #2378, #2097) -- Examples: Vulkan: Added FramesQueueSize field in ImGui_ImplVulkan_InitInfo, required during - initialization to specify the number of in-flight image required by the swap chain. +- Examples: Vulkan: Added MinImageCount field in ImGui_ImplVulkan_InitInfo, required during + initialization to specify the number of in-flight image requested by swap chains. (was previously a hard #define IMGUI_VK_QUEUED_FRAMES 2). (#2071, #1677) [@nathanvoglsam] diff --git a/examples/example_glfw_vulkan/main.cpp b/examples/example_glfw_vulkan/main.cpp index 037d4791..ae6bb661 100644 --- a/examples/example_glfw_vulkan/main.cpp +++ b/examples/example_glfw_vulkan/main.cpp @@ -192,7 +192,7 @@ static void SetupVulkan(const char** extensions, uint32_t extensions_count) } } -static void SetupVulkanWindowData(ImGui_ImplVulkanH_WindowData* wd, VkSurfaceKHR surface, int width, int height) +static void SetupVulkanWindow(ImGui_ImplVulkanH_WindowData* wd, VkSurfaceKHR surface, int width, int height) { wd->Surface = surface; @@ -226,8 +226,6 @@ static void SetupVulkanWindowData(ImGui_ImplVulkanH_WindowData* wd, VkSurfaceKHR static void CleanupVulkan() { - ImGui_ImplVulkanH_WindowData* wd = &g_WindowData; - ImGui_ImplVulkanH_DestroyWindowData(g_Instance, g_Device, wd, g_Allocator); vkDestroyDescriptorPool(g_Device, g_DescriptorPool, g_Allocator); #ifdef IMGUI_VULKAN_DEBUG_REPORT @@ -240,6 +238,15 @@ static void CleanupVulkan() vkDestroyInstance(g_Instance, g_Allocator); } +static void CleanupVulkanWindow() +{ + // In a normal engine/app integration, you wouldn't use the ImGui_ImplVulkanH_WindowData helpers, + // however you would instead need to call ImGui_ImplVulkan_DestroyFrameRenderBuffers() on each + // ImGui_ImplVulkan_FrameRenderBuffers structure that you own. + ImGui_ImplVulkanH_WindowData* wd = &g_WindowData; + ImGui_ImplVulkanH_DestroyWindowData(g_Instance, g_Device, wd, g_Allocator); +} + static void FrameRender(ImGui_ImplVulkanH_WindowData* wd) { VkResult err; @@ -357,7 +364,7 @@ int main(int, char**) glfwGetFramebufferSize(window, &w, &h); glfwSetFramebufferSizeCallback(window, glfw_resize_callback); ImGui_ImplVulkanH_WindowData* wd = &g_WindowData; - SetupVulkanWindowData(wd, surface, w, h); + SetupVulkanWindow(wd, surface, w, h); // Setup Dear ImGui context IMGUI_CHECKVERSION(); @@ -381,7 +388,7 @@ int main(int, char**) init_info.PipelineCache = g_PipelineCache; init_info.DescriptorPool = g_DescriptorPool; init_info.Allocator = g_Allocator; - init_info.FramesQueueSize = wd->FramesQueueSize; + init_info.MinImageCount = g_MinImageCount; init_info.CheckVkResultFn = check_vk_result; ImGui_ImplVulkan_Init(&init_info, wd->RenderPass); @@ -446,7 +453,7 @@ int main(int, char**) if (g_WantSwapChainRebuild) { ImGui_ImplVulkanH_CreateWindowData(g_Instance, g_PhysicalDevice, g_Device, &g_WindowData, g_QueueFamily, g_Allocator, g_ResizeWidth, g_ResizeHeight, g_MinImageCount); - ImGui_ImplVulkan_SetFramesQueueSize(g_WindowData.FramesQueueSize); + ImGui_ImplVulkan_SetSwapChainMinImageCount(g_MinImageCount); g_WindowData.FrameIndex = 0; g_WantSwapChainRebuild = false; } @@ -507,6 +514,8 @@ int main(int, char**) ImGui_ImplVulkan_Shutdown(); ImGui_ImplGlfw_Shutdown(); ImGui::DestroyContext(); + + CleanupVulkanWindow(); CleanupVulkan(); glfwDestroyWindow(window); diff --git a/examples/example_sdl_vulkan/main.cpp b/examples/example_sdl_vulkan/main.cpp index e4721943..58d34448 100644 --- a/examples/example_sdl_vulkan/main.cpp +++ b/examples/example_sdl_vulkan/main.cpp @@ -183,7 +183,7 @@ static void SetupVulkan(const char** extensions, uint32_t extensions_count) } } -static void SetupVulkanWindowData(ImGui_ImplVulkanH_WindowData* wd, VkSurfaceKHR surface, int width, int height) +static void SetupVulkanWindow(ImGui_ImplVulkanH_WindowData* wd, VkSurfaceKHR surface, int width, int height) { wd->Surface = surface; @@ -217,8 +217,6 @@ static void SetupVulkanWindowData(ImGui_ImplVulkanH_WindowData* wd, VkSurfaceKHR static void CleanupVulkan() { - ImGui_ImplVulkanH_WindowData* wd = &g_WindowData; - ImGui_ImplVulkanH_DestroyWindowData(g_Instance, g_Device, wd, g_Allocator); vkDestroyDescriptorPool(g_Device, g_DescriptorPool, g_Allocator); #ifdef IMGUI_VULKAN_DEBUG_REPORT @@ -231,6 +229,15 @@ static void CleanupVulkan() vkDestroyInstance(g_Instance, g_Allocator); } +static void CleanupVulkanWindow() +{ + // In a normal engine/app integration, you wouldn't use the ImGui_ImplVulkanH_WindowData helpers, + // however you would instead need to call ImGui_ImplVulkan_DestroyFrameRenderBuffers() on each + // ImGui_ImplVulkan_FrameRenderBuffers structure that you own. + ImGui_ImplVulkanH_WindowData* wd = &g_WindowData; + ImGui_ImplVulkanH_DestroyWindowData(g_Instance, g_Device, wd, g_Allocator); +} + static void FrameRender(ImGui_ImplVulkanH_WindowData* wd) { VkResult err; @@ -342,7 +349,7 @@ int main(int, char**) int w, h; SDL_GetWindowSize(window, &w, &h); ImGui_ImplVulkanH_WindowData* wd = &g_WindowData; - SetupVulkanWindowData(wd, surface, w, h); + SetupVulkanWindow(wd, surface, w, h); // Setup Dear ImGui context ImGui::CreateContext(); @@ -364,7 +371,7 @@ int main(int, char**) init_info.PipelineCache = g_PipelineCache; init_info.DescriptorPool = g_DescriptorPool; init_info.Allocator = g_Allocator; - init_info.FramesQueueSize = wd->FramesQueueSize; + init_info.MinImageCount = g_MinImageCount; init_info.CheckVkResultFn = check_vk_result; ImGui_ImplVulkan_Init(&init_info, wd->RenderPass); @@ -443,7 +450,7 @@ int main(int, char**) if (g_WantSwapChainRebuild) { ImGui_ImplVulkanH_CreateWindowData(g_Instance, g_PhysicalDevice, g_Device, &g_WindowData, g_QueueFamily, g_Allocator, g_WindowData.Width, g_WindowData.Height, g_MinImageCount); - ImGui_ImplVulkan_SetFramesQueueSize(g_WindowData.FramesQueueSize); + ImGui_ImplVulkan_SetSwapChainMinImageCount(g_MinImageCount); g_WindowData.FrameIndex = 0; g_WantSwapChainRebuild = false; } @@ -504,6 +511,8 @@ int main(int, char**) ImGui_ImplVulkan_Shutdown(); ImGui_ImplSDL2_Shutdown(); ImGui::DestroyContext(); + + CleanupVulkanWindow(); CleanupVulkan(); SDL_DestroyWindow(window); diff --git a/examples/imgui_impl_vulkan.cpp b/examples/imgui_impl_vulkan.cpp index 25b6428b..afe6dcef 100644 --- a/examples/imgui_impl_vulkan.cpp +++ b/examples/imgui_impl_vulkan.cpp @@ -21,7 +21,7 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) // 2019-XX-XX: *BREAKING CHANGE*: Vulkan: Added extra parameter to ImGui_ImplVulkan_RenderDrawData(). Engine/app is in charge of owning/storing 1 ImGui_ImplVulkan_FrameRenderBuffers per in-flight rendering frame. (The demo helper ImGui_ImplVulkanH_WindowData structure carries them.) -// 2019-XX-XX: *BREAKING CHANGE*: Vulkan: Added FramesQueueSize field in ImGui_ImplVulkan_InitInfo, required for initialization (was previously a hard #define IMGUI_VK_QUEUED_FRAMES 2). Added ImGui_ImplVulkan_SetFramesQueueSize() to override FramesQueueSize while running. +// 2019-XX-XX: *BREAKING CHANGE*: Vulkan: Added MinImageCount field in ImGui_ImplVulkan_InitInfo, required for initialization (was previously a hard #define IMGUI_VK_QUEUED_FRAMES 2). Added ImGui_ImplVulkan_SetSwapChainMinImageCount(). // 2019-XX-XX: Vulkan: Added VkInstance argument to ImGui_ImplVulkanH_CreateWindowData() optional helper. // 2019-04-04: Vulkan: Avoid passing negative coordinates to vkCmdSetScissor, which debug validation layers do not like. // 2019-04-01: Vulkan: Support for 32-bit index buffer (#define ImDrawIdx unsigned int). @@ -64,7 +64,6 @@ static VkBuffer g_UploadBuffer = VK_NULL_HANDLE; // Forward Declarations void ImGui_ImplVulkanH_DestroyFrameData(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_FrameData* fd, const VkAllocationCallbacks* allocator); -void ImGui_ImplVulkanH_DestroyFrameRenderBuffers(VkInstance instance, VkDevice device, ImGui_ImplVulkan_FrameRenderBuffers* frb, const VkAllocationCallbacks* allocator); void ImGui_ImplVulkanH_CreateWindowDataSwapChain(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count); void ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator); @@ -754,7 +753,7 @@ bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass rend IM_ASSERT(info->Device != VK_NULL_HANDLE); IM_ASSERT(info->Queue != VK_NULL_HANDLE); IM_ASSERT(info->DescriptorPool != VK_NULL_HANDLE); - IM_ASSERT(info->FramesQueueSize >= 2); + IM_ASSERT(info->MinImageCount >= 2); IM_ASSERT(render_pass != VK_NULL_HANDLE); g_VulkanInitInfo = *info; @@ -773,9 +772,24 @@ void ImGui_ImplVulkan_NewFrame() { } -void ImGui_ImplVulkan_SetFramesQueueSize(int frames_queue_size) +// Note: this has no effect in the 'master' branch, but multi-viewports needs this to recreate swap-chains. +void ImGui_ImplVulkan_SetSwapChainMinImageCount(int min_image_count) { - (void)frames_queue_size; + IM_ASSERT(min_image_count >= 2); + g_VulkanInitInfo.MinImageCount = min_image_count; +} + +// This is a public function because we require the user to pass a ImGui_ImplVulkan_FrameRenderBuffers +// structure to ImGui_ImplVulkan_RenderDrawData. +void ImGui_ImplVulkan_DestroyFrameRenderBuffers(VkInstance instance, VkDevice device, ImGui_ImplVulkan_FrameRenderBuffers* buffers, const VkAllocationCallbacks* allocator) +{ + (void)instance; + if (buffers->VertexBuffer) { vkDestroyBuffer(device, buffers->VertexBuffer, allocator); buffers->VertexBuffer = VK_NULL_HANDLE; } + if (buffers->VertexBufferMemory) { vkFreeMemory (device, buffers->VertexBufferMemory, allocator); buffers->VertexBufferMemory = VK_NULL_HANDLE; } + if (buffers->IndexBuffer) { vkDestroyBuffer(device, buffers->IndexBuffer, allocator); buffers->IndexBuffer = VK_NULL_HANDLE; } + if (buffers->IndexBufferMemory) { vkFreeMemory (device, buffers->IndexBufferMemory, allocator); buffers->IndexBufferMemory = VK_NULL_HANDLE; } + buffers->VertexBufferSize = 0; + buffers->IndexBufferSize = 0; } @@ -910,7 +924,6 @@ void ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(VkInstance instance, VkPhy } } -// MinImageCount will usually turn into FramesQueueSize int ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(VkPresentModeKHR present_mode) { if (present_mode == VK_PRESENT_MODE_MAILBOX_KHR) @@ -984,6 +997,7 @@ void ImGui_ImplVulkanH_CreateWindowDataSwapChain(VkInstance instance, VkPhysical err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->FramesQueueSize, NULL); check_vk_result(err); VkImage backbuffers[16] = {}; + IM_ASSERT(wd->FramesQueueSize >= min_image_count); IM_ASSERT(wd->FramesQueueSize < IM_ARRAYSIZE(backbuffers)); err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->FramesQueueSize, backbuffers); check_vk_result(err); @@ -1114,16 +1128,6 @@ void ImGui_ImplVulkanH_DestroyFrameData(VkInstance instance, VkDevice device, Im vkDestroyImageView(device, fd->BackbufferView, allocator); vkDestroyFramebuffer(device, fd->Framebuffer, allocator); - ImGui_ImplVulkanH_DestroyFrameRenderBuffers(instance, device, &fd->RenderBuffers, allocator); + ImGui_ImplVulkan_DestroyFrameRenderBuffers(instance, device, &fd->RenderBuffers, allocator); } -void ImGui_ImplVulkanH_DestroyFrameRenderBuffers(VkInstance instance, VkDevice device, ImGui_ImplVulkan_FrameRenderBuffers* frb, const VkAllocationCallbacks* allocator) -{ - (void)instance; - if (frb->VertexBuffer) { vkDestroyBuffer(device, frb->VertexBuffer, allocator); frb->VertexBuffer = VK_NULL_HANDLE; } - if (frb->VertexBufferMemory) { vkFreeMemory (device, frb->VertexBufferMemory, allocator); frb->VertexBufferMemory = VK_NULL_HANDLE; } - if (frb->IndexBuffer) { vkDestroyBuffer(device, frb->IndexBuffer, allocator); frb->IndexBuffer = VK_NULL_HANDLE; } - if (frb->IndexBufferMemory) { vkFreeMemory (device, frb->IndexBufferMemory, allocator); frb->IndexBufferMemory = VK_NULL_HANDLE; } - frb->VertexBufferSize = 0; - frb->IndexBufferSize = 0; -} diff --git a/examples/imgui_impl_vulkan.h b/examples/imgui_impl_vulkan.h index 334910f6..4e62ec8a 100644 --- a/examples/imgui_impl_vulkan.h +++ b/examples/imgui_impl_vulkan.h @@ -33,7 +33,7 @@ struct ImGui_ImplVulkan_InitInfo VkQueue Queue; VkPipelineCache PipelineCache; VkDescriptorPool DescriptorPool; - int FramesQueueSize; // >= 2, generally matches the image count returned by vkGetSwapchainImagesKHR + int MinImageCount; // >= 2 const VkAllocationCallbacks* Allocator; void (*CheckVkResultFn)(VkResult err); }; @@ -56,10 +56,11 @@ struct ImGui_ImplVulkan_FrameRenderBuffers IMGUI_IMPL_API bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass render_pass); IMGUI_IMPL_API void ImGui_ImplVulkan_Shutdown(); IMGUI_IMPL_API void ImGui_ImplVulkan_NewFrame(); -IMGUI_IMPL_API void ImGui_ImplVulkan_SetFramesQueueSize(int frames_queue_size); // To override FramesQueueSize after initialization IMGUI_IMPL_API void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer command_buffer, ImGui_ImplVulkan_FrameRenderBuffers* buffers); IMGUI_IMPL_API bool ImGui_ImplVulkan_CreateFontsTexture(VkCommandBuffer command_buffer); IMGUI_IMPL_API void ImGui_ImplVulkan_DestroyFontUploadObjects(); +IMGUI_IMPL_API void ImGui_ImplVulkan_DestroyFrameRenderBuffers(VkInstance instance, VkDevice device, ImGui_ImplVulkan_FrameRenderBuffers* buffers, const VkAllocationCallbacks* allocator); +IMGUI_IMPL_API void ImGui_ImplVulkan_SetSwapChainMinImageCount(int frames_queue_size); // To override MinImageCount after initialization // Called by ImGui_ImplVulkan_Init(), might be useful elsewhere. IMGUI_IMPL_API bool ImGui_ImplVulkan_CreateDeviceObjects(); From 0034e65c26204aee147ca130b5fe1b1052dea6fa Mon Sep 17 00:00:00 2001 From: omar Date: Thu, 4 Apr 2019 23:28:29 +0200 Subject: [PATCH 10/19] Vulkan: Renaming demo/helper structures. Tidying up examples main.cpp. --- docs/CHANGELOG.txt | 4 +- examples/example_glfw_vulkan/main.cpp | 64 ++++++++++++++------------- examples/example_sdl_vulkan/main.cpp | 60 +++++++++++++------------ examples/imgui_impl_vulkan.cpp | 40 ++++++++--------- examples/imgui_impl_vulkan.h | 41 ++++++++--------- 5 files changed, 108 insertions(+), 101 deletions(-) diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index b4f17df9..7f038134 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -36,10 +36,12 @@ HOW TO UPDATE? Breaking Changes: - Examples: Vulkan: Added extra parameter to ImGui_ImplVulkan_RenderDrawData(). Engine/app is in charge of owning/storing 1 ImGui_ImplVulkan_FrameRenderBuffers per in-flight rendering frame. - (The demo helper ImGui_ImplVulkanH_WindowData structure carries them.) (#2461, #2348, #2378, #2097) + (The demo helper ImGui_ImplVulkanH_Window structure carries them.) (#2461, #2348, #2378, #2097) - Examples: Vulkan: Added MinImageCount field in ImGui_ImplVulkan_InitInfo, required during initialization to specify the number of in-flight image requested by swap chains. (was previously a hard #define IMGUI_VK_QUEUED_FRAMES 2). (#2071, #1677) [@nathanvoglsam] +- Examples: Vulkan: Tidying up the demo/internals helpers (most engine/app should not rely + on them but it is possible you have!). Other Changes: diff --git a/examples/example_glfw_vulkan/main.cpp b/examples/example_glfw_vulkan/main.cpp index ae6bb661..7a9a4502 100644 --- a/examples/example_glfw_vulkan/main.cpp +++ b/examples/example_glfw_vulkan/main.cpp @@ -30,20 +30,21 @@ #define IMGUI_VULKAN_DEBUG_REPORT #endif -static VkAllocationCallbacks* g_Allocator = NULL; -static VkInstance g_Instance = VK_NULL_HANDLE; -static VkPhysicalDevice g_PhysicalDevice = VK_NULL_HANDLE; -static VkDevice g_Device = VK_NULL_HANDLE; -static uint32_t g_QueueFamily = (uint32_t)-1; -static VkQueue g_Queue = VK_NULL_HANDLE; -static VkDebugReportCallbackEXT g_DebugReport = VK_NULL_HANDLE; -static VkPipelineCache g_PipelineCache = VK_NULL_HANDLE; -static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE; +static VkAllocationCallbacks* g_Allocator = NULL; +static VkInstance g_Instance = VK_NULL_HANDLE; +static VkPhysicalDevice g_PhysicalDevice = VK_NULL_HANDLE; +static VkDevice g_Device = VK_NULL_HANDLE; +static uint32_t g_QueueFamily = (uint32_t)-1; +static VkQueue g_Queue = VK_NULL_HANDLE; +static VkDebugReportCallbackEXT g_DebugReport = VK_NULL_HANDLE; +static VkPipelineCache g_PipelineCache = VK_NULL_HANDLE; +static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE; -static ImGui_ImplVulkanH_WindowData g_WindowData; -static int g_MinImageCount = 2; -static bool g_WantSwapChainRebuild = false; -static int g_ResizeWidth = 0, g_ResizeHeight = 0; +static ImGui_ImplVulkanH_Window g_WindowData; +static int g_MinImageCount = 2; +static bool g_SwapChainRebuild = false; +static int g_SwapChainResizeWidth = 0; +static int g_SwapChainResizeHeight = 0; static void check_vk_result(VkResult err) { @@ -192,7 +193,7 @@ static void SetupVulkan(const char** extensions, uint32_t extensions_count) } } -static void SetupVulkanWindow(ImGui_ImplVulkanH_WindowData* wd, VkSurfaceKHR surface, int width, int height) +static void SetupVulkanWindow(ImGui_ImplVulkanH_Window* wd, VkSurfaceKHR surface, int width, int height) { wd->Surface = surface; @@ -220,8 +221,8 @@ static void SetupVulkanWindow(ImGui_ImplVulkanH_WindowData* wd, VkSurfaceKHR sur //printf("[vulkan] Selected PresentMode = %d\n", wd->PresentMode); // Create SwapChain, RenderPass, Framebuffer, etc. - ImGui_ImplVulkanH_CreateWindowData(g_Instance, g_PhysicalDevice, g_Device, wd, g_QueueFamily, g_Allocator, width, height, g_MinImageCount); - IM_ASSERT(wd->FramesQueueSize >= 2); + IM_ASSERT(g_MinImageCount >= 2); + ImGui_ImplVulkanH_CreateWindow(g_Instance, g_PhysicalDevice, g_Device, wd, g_QueueFamily, g_Allocator, width, height, g_MinImageCount); } static void CleanupVulkan() @@ -240,14 +241,14 @@ static void CleanupVulkan() static void CleanupVulkanWindow() { - // In a normal engine/app integration, you wouldn't use the ImGui_ImplVulkanH_WindowData helpers, + // In a normal engine/app integration, you wouldn't use the ImGui_ImplVulkanH_Window helpers, // however you would instead need to call ImGui_ImplVulkan_DestroyFrameRenderBuffers() on each // ImGui_ImplVulkan_FrameRenderBuffers structure that you own. - ImGui_ImplVulkanH_WindowData* wd = &g_WindowData; - ImGui_ImplVulkanH_DestroyWindowData(g_Instance, g_Device, wd, g_Allocator); + ImGui_ImplVulkanH_Window* wd = &g_WindowData; + ImGui_ImplVulkanH_DestroyWindow(g_Instance, g_Device, wd, g_Allocator); } -static void FrameRender(ImGui_ImplVulkanH_WindowData* wd) +static void FrameRender(ImGui_ImplVulkanH_Window* wd) { VkResult err; @@ -255,7 +256,7 @@ static void FrameRender(ImGui_ImplVulkanH_WindowData* wd) err = vkAcquireNextImageKHR(g_Device, wd->Swapchain, UINT64_MAX, image_acquired_semaphore, VK_NULL_HANDLE, &wd->FrameIndex); check_vk_result(err); - ImGui_ImplVulkanH_FrameData* fd = &wd->Frames[wd->FrameIndex]; + ImGui_ImplVulkanH_Frame* fd = &wd->Frames[wd->FrameIndex]; { err = vkWaitForFences(g_Device, 1, &fd->Fence, VK_TRUE, UINT64_MAX); // wait indefinitely instead of periodically checking check_vk_result(err); @@ -308,9 +309,9 @@ static void FrameRender(ImGui_ImplVulkanH_WindowData* wd) } } -static void FramePresent(ImGui_ImplVulkanH_WindowData* wd) +static void FramePresent(ImGui_ImplVulkanH_Window* wd) { - ImGui_ImplVulkanH_FrameData* fd = &wd->Frames[wd->FrameIndex]; + ImGui_ImplVulkanH_Frame* fd = &wd->Frames[wd->FrameIndex]; VkPresentInfoKHR info = {}; info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; info.waitSemaphoreCount = 1; @@ -329,14 +330,14 @@ static void glfw_error_callback(int error, const char* description) static void glfw_resize_callback(GLFWwindow*, int w, int h) { - g_WantSwapChainRebuild = true; - g_ResizeWidth = w; - g_ResizeHeight = h; + g_SwapChainRebuild = true; + g_SwapChainResizeWidth = w; + g_SwapChainResizeHeight = h; } int main(int, char**) { - // Setup window + // Setup GLFW window glfwSetErrorCallback(glfw_error_callback); if (!glfwInit()) return 1; @@ -363,7 +364,7 @@ int main(int, char**) int w, h; glfwGetFramebufferSize(window, &w, &h); glfwSetFramebufferSizeCallback(window, glfw_resize_callback); - ImGui_ImplVulkanH_WindowData* wd = &g_WindowData; + ImGui_ImplVulkanH_Window* wd = &g_WindowData; SetupVulkanWindow(wd, surface, w, h); // Setup Dear ImGui context @@ -450,12 +451,13 @@ int main(int, char**) // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. glfwPollEvents(); - if (g_WantSwapChainRebuild) + + if (g_SwapChainRebuild) { - ImGui_ImplVulkanH_CreateWindowData(g_Instance, g_PhysicalDevice, g_Device, &g_WindowData, g_QueueFamily, g_Allocator, g_ResizeWidth, g_ResizeHeight, g_MinImageCount); + g_SwapChainRebuild = false; + ImGui_ImplVulkanH_CreateWindow(g_Instance, g_PhysicalDevice, g_Device, &g_WindowData, g_QueueFamily, g_Allocator, g_SwapChainResizeWidth, g_SwapChainResizeHeight, g_MinImageCount); ImGui_ImplVulkan_SetSwapChainMinImageCount(g_MinImageCount); g_WindowData.FrameIndex = 0; - g_WantSwapChainRebuild = false; } // Start the Dear ImGui frame diff --git a/examples/example_sdl_vulkan/main.cpp b/examples/example_sdl_vulkan/main.cpp index 58d34448..2e7e6c6e 100644 --- a/examples/example_sdl_vulkan/main.cpp +++ b/examples/example_sdl_vulkan/main.cpp @@ -22,19 +22,21 @@ #define IMGUI_VULKAN_DEBUG_REPORT #endif -static VkAllocationCallbacks* g_Allocator = NULL; -static VkInstance g_Instance = VK_NULL_HANDLE; -static VkPhysicalDevice g_PhysicalDevice = VK_NULL_HANDLE; -static VkDevice g_Device = VK_NULL_HANDLE; -static uint32_t g_QueueFamily = (uint32_t)-1; -static VkQueue g_Queue = VK_NULL_HANDLE; -static VkDebugReportCallbackEXT g_DebugReport = VK_NULL_HANDLE; -static VkPipelineCache g_PipelineCache = VK_NULL_HANDLE; -static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE; +static VkAllocationCallbacks* g_Allocator = NULL; +static VkInstance g_Instance = VK_NULL_HANDLE; +static VkPhysicalDevice g_PhysicalDevice = VK_NULL_HANDLE; +static VkDevice g_Device = VK_NULL_HANDLE; +static uint32_t g_QueueFamily = (uint32_t)-1; +static VkQueue g_Queue = VK_NULL_HANDLE; +static VkDebugReportCallbackEXT g_DebugReport = VK_NULL_HANDLE; +static VkPipelineCache g_PipelineCache = VK_NULL_HANDLE; +static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE; -static ImGui_ImplVulkanH_WindowData g_WindowData; -static uint32_t g_MinImageCount = 2; -static bool g_WantSwapChainRebuild = false; +static ImGui_ImplVulkanH_Window g_WindowData; +static uint32_t g_MinImageCount = 2; +static bool g_SwapChainRebuild = false; +static int g_SwapChainResizeWidth = 0; +static int g_SwapChainResizeHeight = 0; static void check_vk_result(VkResult err) { @@ -183,7 +185,7 @@ static void SetupVulkan(const char** extensions, uint32_t extensions_count) } } -static void SetupVulkanWindow(ImGui_ImplVulkanH_WindowData* wd, VkSurfaceKHR surface, int width, int height) +static void SetupVulkanWindow(ImGui_ImplVulkanH_Window* wd, VkSurfaceKHR surface, int width, int height) { wd->Surface = surface; @@ -211,8 +213,8 @@ static void SetupVulkanWindow(ImGui_ImplVulkanH_WindowData* wd, VkSurfaceKHR sur //printf("[vulkan] Selected PresentMode = %d\n", wd->PresentMode); // Create SwapChain, RenderPass, Framebuffer, etc. - ImGui_ImplVulkanH_CreateWindowData(g_Instance, g_PhysicalDevice, g_Device, wd, g_QueueFamily, g_Allocator, width, height, g_MinImageCount); - IM_ASSERT(wd->FramesQueueSize >= 2); + IM_ASSERT(g_MinImageCount >= 2); + ImGui_ImplVulkanH_CreateWindow(g_Instance, g_PhysicalDevice, g_Device, wd, g_QueueFamily, g_Allocator, width, height, g_MinImageCount); } static void CleanupVulkan() @@ -231,14 +233,14 @@ static void CleanupVulkan() static void CleanupVulkanWindow() { - // In a normal engine/app integration, you wouldn't use the ImGui_ImplVulkanH_WindowData helpers, + // In a normal engine/app integration, you wouldn't use the ImGui_ImplVulkanH_Window helpers, // however you would instead need to call ImGui_ImplVulkan_DestroyFrameRenderBuffers() on each // ImGui_ImplVulkan_FrameRenderBuffers structure that you own. - ImGui_ImplVulkanH_WindowData* wd = &g_WindowData; - ImGui_ImplVulkanH_DestroyWindowData(g_Instance, g_Device, wd, g_Allocator); + ImGui_ImplVulkanH_Window* wd = &g_WindowData; + ImGui_ImplVulkanH_DestroyWindow(g_Instance, g_Device, wd, g_Allocator); } -static void FrameRender(ImGui_ImplVulkanH_WindowData* wd) +static void FrameRender(ImGui_ImplVulkanH_Window* wd) { VkResult err; @@ -246,7 +248,7 @@ static void FrameRender(ImGui_ImplVulkanH_WindowData* wd) err = vkAcquireNextImageKHR(g_Device, wd->Swapchain, UINT64_MAX, image_acquired_semaphore, VK_NULL_HANDLE, &wd->FrameIndex); check_vk_result(err); - ImGui_ImplVulkanH_FrameData* fd = &wd->Frames[wd->FrameIndex]; + ImGui_ImplVulkanH_Frame* fd = &wd->Frames[wd->FrameIndex]; { err = vkWaitForFences(g_Device, 1, &fd->Fence, VK_TRUE, UINT64_MAX); // wait indefinitely instead of periodically checking check_vk_result(err); @@ -299,9 +301,9 @@ static void FrameRender(ImGui_ImplVulkanH_WindowData* wd) } } -static void FramePresent(ImGui_ImplVulkanH_WindowData* wd) +static void FramePresent(ImGui_ImplVulkanH_Window* wd) { - ImGui_ImplVulkanH_FrameData* fd = &wd->Frames[wd->FrameIndex]; + ImGui_ImplVulkanH_Frame* fd = &wd->Frames[wd->FrameIndex]; VkPresentInfoKHR info = {}; info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; info.waitSemaphoreCount = 1; @@ -348,7 +350,7 @@ int main(int, char**) // Create Framebuffers int w, h; SDL_GetWindowSize(window, &w, &h); - ImGui_ImplVulkanH_WindowData* wd = &g_WindowData; + ImGui_ImplVulkanH_Window* wd = &g_WindowData; SetupVulkanWindow(wd, surface, w, h); // Setup Dear ImGui context @@ -441,18 +443,18 @@ int main(int, char**) done = true; if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESIZED && event.window.windowID == SDL_GetWindowID(window)) { - g_WindowData.Width = (int)event.window.data1; - g_WindowData.Height = (int)event.window.data2; - g_WantSwapChainRebuild = true; + g_SwapChainResizeWidth = (int)event.window.data1; + g_SwapChainResizeHeight = (int)event.window.data2; + g_SwapChainRebuild = true; } } - if (g_WantSwapChainRebuild) + if (g_SwapChainRebuild) { - ImGui_ImplVulkanH_CreateWindowData(g_Instance, g_PhysicalDevice, g_Device, &g_WindowData, g_QueueFamily, g_Allocator, g_WindowData.Width, g_WindowData.Height, g_MinImageCount); + g_SwapChainRebuild = false; + ImGui_ImplVulkanH_CreateWindow(g_Instance, g_PhysicalDevice, g_Device, &g_WindowData, g_QueueFamily, g_Allocator, g_SwapChainResizeWidth, g_SwapChainResizeHeight, g_MinImageCount); ImGui_ImplVulkan_SetSwapChainMinImageCount(g_MinImageCount); g_WindowData.FrameIndex = 0; - g_WantSwapChainRebuild = false; } // Start the Dear ImGui frame diff --git a/examples/imgui_impl_vulkan.cpp b/examples/imgui_impl_vulkan.cpp index afe6dcef..edf4848b 100644 --- a/examples/imgui_impl_vulkan.cpp +++ b/examples/imgui_impl_vulkan.cpp @@ -20,9 +20,9 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) -// 2019-XX-XX: *BREAKING CHANGE*: Vulkan: Added extra parameter to ImGui_ImplVulkan_RenderDrawData(). Engine/app is in charge of owning/storing 1 ImGui_ImplVulkan_FrameRenderBuffers per in-flight rendering frame. (The demo helper ImGui_ImplVulkanH_WindowData structure carries them.) +// 2019-XX-XX: *BREAKING CHANGE*: Vulkan: Added extra parameter to ImGui_ImplVulkan_RenderDrawData(). Engine/app is in charge of owning/storing 1 ImGui_ImplVulkan_FrameRenderBuffers per in-flight rendering frame. (The demo helper ImGui_ImplVulkanH_Window structure carries them.) // 2019-XX-XX: *BREAKING CHANGE*: Vulkan: Added MinImageCount field in ImGui_ImplVulkan_InitInfo, required for initialization (was previously a hard #define IMGUI_VK_QUEUED_FRAMES 2). Added ImGui_ImplVulkan_SetSwapChainMinImageCount(). -// 2019-XX-XX: Vulkan: Added VkInstance argument to ImGui_ImplVulkanH_CreateWindowData() optional helper. +// 2019-XX-XX: Vulkan: Added VkInstance argument to ImGui_ImplVulkanH_CreateWindow() optional helper. // 2019-04-04: Vulkan: Avoid passing negative coordinates to vkCmdSetScissor, which debug validation layers do not like. // 2019-04-01: Vulkan: Support for 32-bit index buffer (#define ImDrawIdx unsigned int). // 2019-02-16: Vulkan: Viewport and clipping rectangles correctly using draw_data->FramebufferScale to allow retina display. @@ -63,9 +63,9 @@ static VkDeviceMemory g_UploadBufferMemory = VK_NULL_HANDLE; static VkBuffer g_UploadBuffer = VK_NULL_HANDLE; // Forward Declarations -void ImGui_ImplVulkanH_DestroyFrameData(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_FrameData* fd, const VkAllocationCallbacks* allocator); -void ImGui_ImplVulkanH_CreateWindowDataSwapChain(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count); -void ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator); +void ImGui_ImplVulkanH_DestroyFrame(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_Frame* fd, const VkAllocationCallbacks* allocator); +void ImGui_ImplVulkanH_CreateWindowSwapChain(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count); +void ImGui_ImplVulkanH_CreateWindowCommandBuffers(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator); //----------------------------------------------------------------------------- // SHADERS @@ -877,7 +877,7 @@ VkPresentModeKHR ImGui_ImplVulkanH_SelectPresentMode(VkPhysicalDevice physical_d return VK_PRESENT_MODE_FIFO_KHR; // Always available } -void ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator) +void ImGui_ImplVulkanH_CreateWindowCommandBuffers(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator) { IM_ASSERT(instance != VK_NULL_HANDLE && physical_device != VK_NULL_HANDLE && device != VK_NULL_HANDLE); (void)instance; @@ -888,7 +888,7 @@ void ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(VkInstance instance, VkPhy VkResult err; for (uint32_t i = 0; i < wd->FramesQueueSize; i++) { - ImGui_ImplVulkanH_FrameData* fd = &wd->Frames[i]; + ImGui_ImplVulkanH_Frame* fd = &wd->Frames[i]; { VkCommandPoolCreateInfo info = {}; info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; @@ -937,17 +937,17 @@ int ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(VkPresentModeKHR present_m } // Also destroy old swap chain and in-flight frames data, if any. -void ImGui_ImplVulkanH_CreateWindowDataSwapChain(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count) +void ImGui_ImplVulkanH_CreateWindowSwapChain(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count) { VkResult err; VkSwapchainKHR old_swapchain = wd->Swapchain; err = vkDeviceWaitIdle(device); check_vk_result(err); - // We don't use ImGui_ImplVulkanH_DestroyWindowData() because we want to preserve the old swapchain to create the new one. + // We don't use ImGui_ImplVulkanH_DestroyWindow() because we want to preserve the old swapchain to create the new one. // Destroy old Framebuffer for (uint32_t i = 0; i < wd->FramesQueueSize; i++) - ImGui_ImplVulkanH_DestroyFrameData(instance, device, &wd->Frames[i], allocator); + ImGui_ImplVulkanH_DestroyFrame(instance, device, &wd->Frames[i], allocator); delete[] wd->Frames; wd->Frames = NULL; wd->FramesQueueSize = 0; @@ -1003,7 +1003,7 @@ void ImGui_ImplVulkanH_CreateWindowDataSwapChain(VkInstance instance, VkPhysical check_vk_result(err); IM_ASSERT(wd->Frames == NULL); - wd->Frames = new ImGui_ImplVulkanH_FrameData[wd->FramesQueueSize]; + wd->Frames = new ImGui_ImplVulkanH_Frame[wd->FramesQueueSize]; memset(wd->Frames, 0, sizeof(wd->Frames[0]) * wd->FramesQueueSize); for (uint32_t i = 0; i < wd->FramesQueueSize; i++) wd->Frames[i].Backbuffer = backbuffers[i]; @@ -1062,7 +1062,7 @@ void ImGui_ImplVulkanH_CreateWindowDataSwapChain(VkInstance instance, VkPhysical info.subresourceRange = image_range; for (uint32_t i = 0; i < wd->FramesQueueSize; i++) { - ImGui_ImplVulkanH_FrameData* fd = &wd->Frames[i]; + ImGui_ImplVulkanH_Frame* fd = &wd->Frames[i]; info.image = fd->Backbuffer; err = vkCreateImageView(device, &info, allocator, &fd->BackbufferView); check_vk_result(err); @@ -1082,7 +1082,7 @@ void ImGui_ImplVulkanH_CreateWindowDataSwapChain(VkInstance instance, VkPhysical info.layers = 1; for (uint32_t i = 0; i < wd->FramesQueueSize; i++) { - ImGui_ImplVulkanH_FrameData* fd = &wd->Frames[i]; + ImGui_ImplVulkanH_Frame* fd = &wd->Frames[i]; attachment[0] = fd->BackbufferView; err = vkCreateFramebuffer(device, &info, allocator, &fd->Framebuffer); check_vk_result(err); @@ -1090,29 +1090,29 @@ void ImGui_ImplVulkanH_CreateWindowDataSwapChain(VkInstance instance, VkPhysical } } -void ImGui_ImplVulkanH_CreateWindowData(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator, int width, int height, uint32_t min_image_count) +void ImGui_ImplVulkanH_CreateWindow(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator, int width, int height, uint32_t min_image_count) { - ImGui_ImplVulkanH_CreateWindowDataSwapChain(instance, physical_device, device, wd, allocator, width, height, min_image_count); - ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(instance, physical_device, device, wd, queue_family, allocator); + ImGui_ImplVulkanH_CreateWindowSwapChain(instance, physical_device, device, wd, allocator, width, height, min_image_count); + ImGui_ImplVulkanH_CreateWindowCommandBuffers(instance, physical_device, device, wd, queue_family, allocator); } -void ImGui_ImplVulkanH_DestroyWindowData(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator) +void ImGui_ImplVulkanH_DestroyWindow(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_Window* wd, const VkAllocationCallbacks* allocator) { vkDeviceWaitIdle(device); // FIXME: We could wait on the Queue if we had the queue in wd-> (otherwise VulkanH functions can't use globals) //vkQueueWaitIdle(g_Queue); for (uint32_t i = 0; i < wd->FramesQueueSize; i++) - ImGui_ImplVulkanH_DestroyFrameData(instance, device, &wd->Frames[i], allocator); + ImGui_ImplVulkanH_DestroyFrame(instance, device, &wd->Frames[i], allocator); delete[] wd->Frames; wd->Frames = NULL; vkDestroyRenderPass(device, wd->RenderPass, allocator); vkDestroySwapchainKHR(device, wd->Swapchain, allocator); vkDestroySurfaceKHR(instance, wd->Surface, allocator); - *wd = ImGui_ImplVulkanH_WindowData(); + *wd = ImGui_ImplVulkanH_Window(); } -void ImGui_ImplVulkanH_DestroyFrameData(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_FrameData* fd, const VkAllocationCallbacks* allocator) +void ImGui_ImplVulkanH_DestroyFrame(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_Frame* fd, const VkAllocationCallbacks* allocator) { (void)instance; vkDestroyFence(device, fd->Fence, allocator); diff --git a/examples/imgui_impl_vulkan.h b/examples/imgui_impl_vulkan.h index 4e62ec8a..0a770afa 100644 --- a/examples/imgui_impl_vulkan.h +++ b/examples/imgui_impl_vulkan.h @@ -26,16 +26,16 @@ // [Please zero-clear before use!] struct ImGui_ImplVulkan_InitInfo { - VkInstance Instance; - VkPhysicalDevice PhysicalDevice; - VkDevice Device; - uint32_t QueueFamily; - VkQueue Queue; - VkPipelineCache PipelineCache; - VkDescriptorPool DescriptorPool; - int MinImageCount; // >= 2 - const VkAllocationCallbacks* Allocator; - void (*CheckVkResultFn)(VkResult err); + VkInstance Instance; + VkPhysicalDevice PhysicalDevice; + VkDevice Device; + uint32_t QueueFamily; + VkQueue Queue; + VkPipelineCache PipelineCache; + VkDescriptorPool DescriptorPool; + int MinImageCount; // >= 2 + const VkAllocationCallbacks* Allocator; + void (*CheckVkResultFn)(VkResult err); }; // Reusable buffers used for rendering by current in-flight frame, for ImGui_ImplVulkan_RenderDrawData() @@ -83,19 +83,20 @@ IMGUI_IMPL_API void ImGui_ImplVulkan_DestroyDeviceObjects(); // (The ImGui_ImplVulkanH_XXX functions do not interact with any of the state used by the regular ImGui_ImplVulkan_XXX functions) //------------------------------------------------------------------------- -struct ImGui_ImplVulkanH_FrameData; -struct ImGui_ImplVulkanH_WindowData; +struct ImGui_ImplVulkanH_Frame; +struct ImGui_ImplVulkanH_Window; -IMGUI_IMPL_API void ImGui_ImplVulkanH_CreateWindowData(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count); -IMGUI_IMPL_API void ImGui_ImplVulkanH_DestroyWindowData(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator); +// Helpers +IMGUI_IMPL_API void ImGui_ImplVulkanH_CreateWindow(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wnd, uint32_t queue_family, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count); +IMGUI_IMPL_API void ImGui_ImplVulkanH_DestroyWindow(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_Window* wnd, const VkAllocationCallbacks* allocator); IMGUI_IMPL_API VkSurfaceFormatKHR ImGui_ImplVulkanH_SelectSurfaceFormat(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkFormat* request_formats, int request_formats_count, VkColorSpaceKHR request_color_space); IMGUI_IMPL_API VkPresentModeKHR ImGui_ImplVulkanH_SelectPresentMode(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkPresentModeKHR* request_modes, int request_modes_count); IMGUI_IMPL_API int ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(VkPresentModeKHR present_mode); // Helper structure to hold the data needed by one rendering frame -// (Used by example's main.cpp. Used by multi-viewport features. Probably NOT used by your own app.) +// (Used by example's main.cpp. Used by multi-viewport features. Probably NOT used by your own engine/app.) // [Please zero-clear before use!] -struct ImGui_ImplVulkanH_FrameData +struct ImGui_ImplVulkanH_Frame { VkCommandPool CommandPool; VkCommandBuffer CommandBuffer; @@ -109,8 +110,8 @@ struct ImGui_ImplVulkanH_FrameData }; // Helper structure to hold the data needed by one rendering context into one OS window -// (Used by example's main.cpp. Used by multi-viewport features. Probably NOT used by your own app.) -struct ImGui_ImplVulkanH_WindowData +// (Used by example's main.cpp. Used by multi-viewport features. Probably NOT used by your own engine/app.) +struct ImGui_ImplVulkanH_Window { int Width; int Height; @@ -123,9 +124,9 @@ struct ImGui_ImplVulkanH_WindowData VkClearValue ClearValue; uint32_t FrameIndex; // Current frame being rendered to (0 <= FrameIndex < FrameInFlightCount) uint32_t FramesQueueSize; // Number of simultaneous in-flight frames (returned by vkGetSwapchainImagesKHR, usually derived from min_image_count) - ImGui_ImplVulkanH_FrameData* Frames; + ImGui_ImplVulkanH_Frame* Frames; - ImGui_ImplVulkanH_WindowData() + ImGui_ImplVulkanH_Window() { memset(this, 0, sizeof(*this)); PresentMode = VK_PRESENT_MODE_MAX_ENUM_KHR; From a45840746e171b3e258f29a716d7634cfd832e1c Mon Sep 17 00:00:00 2001 From: MindSpunk Date: Fri, 5 Apr 2019 15:50:21 +1100 Subject: [PATCH 11/19] Vulkan, Viewports: Fix for resizing viewport windows crashing. (#2472) --- examples/example_glfw_vulkan/main.cpp | 9 ++++---- examples/example_sdl_vulkan/main.cpp | 9 ++++---- examples/imgui_impl_vulkan.cpp | 30 +++++++++++++++++++++------ examples/imgui_impl_vulkan.h | 11 ++++++++-- 4 files changed, 43 insertions(+), 16 deletions(-) diff --git a/examples/example_glfw_vulkan/main.cpp b/examples/example_glfw_vulkan/main.cpp index 7a9a4502..c8417391 100644 --- a/examples/example_glfw_vulkan/main.cpp +++ b/examples/example_glfw_vulkan/main.cpp @@ -252,7 +252,8 @@ static void FrameRender(ImGui_ImplVulkanH_Window* wd) { VkResult err; - VkSemaphore& image_acquired_semaphore = wd->Frames[wd->FrameIndex].ImageAcquiredSemaphore; + VkSemaphore image_acquired_semaphore = wd->FrameSemaphores[wd->SemaphoreIndex].ImageAcquiredSemaphore; + 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); check_vk_result(err); @@ -300,7 +301,7 @@ static void FrameRender(ImGui_ImplVulkanH_Window* wd) info.commandBufferCount = 1; info.pCommandBuffers = &fd->CommandBuffer; info.signalSemaphoreCount = 1; - info.pSignalSemaphores = &fd->RenderCompleteSemaphore; + info.pSignalSemaphores = &render_complete_semaphore; err = vkEndCommandBuffer(fd->CommandBuffer); check_vk_result(err); @@ -311,11 +312,11 @@ static void FrameRender(ImGui_ImplVulkanH_Window* wd) static void FramePresent(ImGui_ImplVulkanH_Window* wd) { - ImGui_ImplVulkanH_Frame* fd = &wd->Frames[wd->FrameIndex]; + VkSemaphore render_complete_semaphore = wd->FrameSemaphores[wd->SemaphoreIndex].RenderCompleteSemaphore; VkPresentInfoKHR info = {}; info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; info.waitSemaphoreCount = 1; - info.pWaitSemaphores = &fd->RenderCompleteSemaphore; + info.pWaitSemaphores = &render_complete_semaphore; info.swapchainCount = 1; info.pSwapchains = &wd->Swapchain; info.pImageIndices = &wd->FrameIndex; diff --git a/examples/example_sdl_vulkan/main.cpp b/examples/example_sdl_vulkan/main.cpp index 2e7e6c6e..2124f613 100644 --- a/examples/example_sdl_vulkan/main.cpp +++ b/examples/example_sdl_vulkan/main.cpp @@ -244,7 +244,8 @@ static void FrameRender(ImGui_ImplVulkanH_Window* wd) { VkResult err; - VkSemaphore& image_acquired_semaphore = wd->Frames[wd->FrameIndex].ImageAcquiredSemaphore; + VkSemaphore image_acquired_semaphore = wd->FrameSemaphores[wd->SemaphoreIndex].ImageAcquiredSemaphore; + 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); check_vk_result(err); @@ -292,7 +293,7 @@ static void FrameRender(ImGui_ImplVulkanH_Window* wd) info.commandBufferCount = 1; info.pCommandBuffers = &fd->CommandBuffer; info.signalSemaphoreCount = 1; - info.pSignalSemaphores = &fd->RenderCompleteSemaphore; + info.pSignalSemaphores = &render_complete_semaphore; err = vkEndCommandBuffer(fd->CommandBuffer); check_vk_result(err); @@ -303,11 +304,11 @@ static void FrameRender(ImGui_ImplVulkanH_Window* wd) static void FramePresent(ImGui_ImplVulkanH_Window* wd) { - ImGui_ImplVulkanH_Frame* fd = &wd->Frames[wd->FrameIndex]; + VkSemaphore render_complete_semaphore = wd->FrameSemaphores[wd->SemaphoreIndex].RenderCompleteSemaphore; VkPresentInfoKHR info = {}; info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; info.waitSemaphoreCount = 1; - info.pWaitSemaphores = &fd->RenderCompleteSemaphore; + info.pWaitSemaphores = &render_complete_semaphore; info.swapchainCount = 1; info.pSwapchains = &wd->Swapchain; info.pImageIndices = &wd->FrameIndex; diff --git a/examples/imgui_impl_vulkan.cpp b/examples/imgui_impl_vulkan.cpp index edf4848b..c8bb7ab9 100644 --- a/examples/imgui_impl_vulkan.cpp +++ b/examples/imgui_impl_vulkan.cpp @@ -64,6 +64,7 @@ static VkBuffer g_UploadBuffer = VK_NULL_HANDLE; // Forward Declarations void ImGui_ImplVulkanH_DestroyFrame(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_Frame* fd, const VkAllocationCallbacks* allocator); +void ImGui_ImplVulkanH_DestroyFrameSemaphores(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_FrameSemaphores* fsd, const VkAllocationCallbacks* allocator); void ImGui_ImplVulkanH_CreateWindowSwapChain(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count); void ImGui_ImplVulkanH_CreateWindowCommandBuffers(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator); @@ -889,6 +890,7 @@ void ImGui_ImplVulkanH_CreateWindowCommandBuffers(VkInstance instance, VkPhysica for (uint32_t i = 0; i < wd->FramesQueueSize; i++) { ImGui_ImplVulkanH_Frame* fd = &wd->Frames[i]; + ImGui_ImplVulkanH_FrameSemaphores* fsd = &wd->FrameSemaphores[i]; { VkCommandPoolCreateInfo info = {}; info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; @@ -916,9 +918,9 @@ void ImGui_ImplVulkanH_CreateWindowCommandBuffers(VkInstance instance, VkPhysica { VkSemaphoreCreateInfo info = {}; info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; - err = vkCreateSemaphore(device, &info, allocator, &fd->ImageAcquiredSemaphore); + err = vkCreateSemaphore(device, &info, allocator, &fsd->ImageAcquiredSemaphore); check_vk_result(err); - err = vkCreateSemaphore(device, &info, allocator, &fd->RenderCompleteSemaphore); + err = vkCreateSemaphore(device, &info, allocator, &fsd->RenderCompleteSemaphore); check_vk_result(err); } } @@ -947,9 +949,14 @@ void ImGui_ImplVulkanH_CreateWindowSwapChain(VkInstance instance, VkPhysicalDevi // We don't use ImGui_ImplVulkanH_DestroyWindow() because we want to preserve the old swapchain to create the new one. // Destroy old Framebuffer for (uint32_t i = 0; i < wd->FramesQueueSize; i++) + { ImGui_ImplVulkanH_DestroyFrame(instance, device, &wd->Frames[i], allocator); + ImGui_ImplVulkanH_DestroyFrameSemaphores(instance, device, &wd->FrameSemaphores[i], allocator); + } delete[] wd->Frames; + delete[] wd->FrameSemaphores; wd->Frames = NULL; + wd->FrameSemaphores = NULL; wd->FramesQueueSize = 0; if (wd->RenderPass) vkDestroyRenderPass(device, wd->RenderPass, allocator); @@ -1004,7 +1011,9 @@ void ImGui_ImplVulkanH_CreateWindowSwapChain(VkInstance instance, VkPhysicalDevi IM_ASSERT(wd->Frames == NULL); wd->Frames = new ImGui_ImplVulkanH_Frame[wd->FramesQueueSize]; + wd->FrameSemaphores = new ImGui_ImplVulkanH_FrameSemaphores[wd->FramesQueueSize]; memset(wd->Frames, 0, sizeof(wd->Frames[0]) * wd->FramesQueueSize); + memset(wd->FrameSemaphores, 0, sizeof(wd->FrameSemaphores[0]) * wd->FramesQueueSize); for (uint32_t i = 0; i < wd->FramesQueueSize; i++) wd->Frames[i].Backbuffer = backbuffers[i]; } @@ -1102,9 +1111,14 @@ void ImGui_ImplVulkanH_DestroyWindow(VkInstance instance, VkDevice device, ImGui //vkQueueWaitIdle(g_Queue); for (uint32_t i = 0; i < wd->FramesQueueSize; i++) + { ImGui_ImplVulkanH_DestroyFrame(instance, device, &wd->Frames[i], allocator); + ImGui_ImplVulkanH_DestroyFrameSemaphores(instance, device, &wd->FrameSemaphores[i], allocator); + } delete[] wd->Frames; + delete[] wd->FrameSemaphores; wd->Frames = NULL; + wd->FrameSemaphores = NULL; vkDestroyRenderPass(device, wd->RenderPass, allocator); vkDestroySwapchainKHR(device, wd->Swapchain, allocator); vkDestroySurfaceKHR(instance, wd->Surface, allocator); @@ -1112,22 +1126,26 @@ void ImGui_ImplVulkanH_DestroyWindow(VkInstance instance, VkDevice device, ImGui *wd = ImGui_ImplVulkanH_Window(); } +void ImGui_ImplVulkanH_DestroyFrameSemaphores(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_FrameSemaphores* fsd, const VkAllocationCallbacks* allocator) +{ + (void)instance; + vkDestroySemaphore(device, fsd->ImageAcquiredSemaphore, allocator); + vkDestroySemaphore(device, fsd->RenderCompleteSemaphore, allocator); + fsd->ImageAcquiredSemaphore = fsd->RenderCompleteSemaphore = VK_NULL_HANDLE; +} + void ImGui_ImplVulkanH_DestroyFrame(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_Frame* fd, const VkAllocationCallbacks* allocator) { (void)instance; vkDestroyFence(device, fd->Fence, allocator); vkFreeCommandBuffers(device, fd->CommandPool, 1, &fd->CommandBuffer); vkDestroyCommandPool(device, fd->CommandPool, allocator); - vkDestroySemaphore(device, fd->ImageAcquiredSemaphore, allocator); - vkDestroySemaphore(device, fd->RenderCompleteSemaphore, allocator); fd->Fence = VK_NULL_HANDLE; fd->CommandBuffer = VK_NULL_HANDLE; fd->CommandPool = VK_NULL_HANDLE; - fd->ImageAcquiredSemaphore = fd->RenderCompleteSemaphore = VK_NULL_HANDLE; vkDestroyImageView(device, fd->BackbufferView, allocator); vkDestroyFramebuffer(device, fd->Framebuffer, allocator); ImGui_ImplVulkan_DestroyFrameRenderBuffers(instance, device, &fd->RenderBuffers, allocator); } - diff --git a/examples/imgui_impl_vulkan.h b/examples/imgui_impl_vulkan.h index 0a770afa..b872ae21 100644 --- a/examples/imgui_impl_vulkan.h +++ b/examples/imgui_impl_vulkan.h @@ -101,14 +101,18 @@ struct ImGui_ImplVulkanH_Frame VkCommandPool CommandPool; VkCommandBuffer CommandBuffer; VkFence Fence; - VkSemaphore ImageAcquiredSemaphore; - VkSemaphore RenderCompleteSemaphore; VkImage Backbuffer; VkImageView BackbufferView; VkFramebuffer Framebuffer; ImGui_ImplVulkan_FrameRenderBuffers RenderBuffers; }; +struct ImGui_ImplVulkanH_FrameSemaphores +{ + VkSemaphore ImageAcquiredSemaphore; + VkSemaphore RenderCompleteSemaphore; +}; + // Helper structure to hold the data needed by one rendering context into one OS window // (Used by example's main.cpp. Used by multi-viewport features. Probably NOT used by your own engine/app.) struct ImGui_ImplVulkanH_Window @@ -124,7 +128,10 @@ struct ImGui_ImplVulkanH_Window VkClearValue ClearValue; uint32_t FrameIndex; // Current frame being rendered to (0 <= FrameIndex < FrameInFlightCount) uint32_t FramesQueueSize; // Number of simultaneous in-flight frames (returned by vkGetSwapchainImagesKHR, usually derived from min_image_count) + uint32_t SemaphoreIndex; // Current set of swapchain wait semaphores we're using (needs to be distinct from per frame data) ImGui_ImplVulkanH_Frame* Frames; + ImGui_ImplVulkanH_FrameSemaphores* FrameSemaphores; + ImGui_ImplVulkanH_Window() { From ec76722d2d7945d8b6bf7f152a7acc81e0a507e6 Mon Sep 17 00:00:00 2001 From: omar Date: Fri, 5 Apr 2019 17:22:24 +0200 Subject: [PATCH 12/19] Vulkan: Added ImageCount to InitInfo structure (!= MinImageCount) will be needed for viewports. Renamed FramesQueueSize -> ImageCount. (#2472, #2071) --- docs/CHANGELOG.txt | 4 ++-- examples/example_glfw_vulkan/main.cpp | 1 + examples/example_sdl_vulkan/main.cpp | 1 + examples/imgui_impl_vulkan.cpp | 31 ++++++++++++++------------- examples/imgui_impl_vulkan.h | 14 ++++++------ 5 files changed, 27 insertions(+), 24 deletions(-) diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index 7f038134..a3283b1b 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -37,8 +37,8 @@ Breaking Changes: - Examples: Vulkan: Added extra parameter to ImGui_ImplVulkan_RenderDrawData(). Engine/app is in charge of owning/storing 1 ImGui_ImplVulkan_FrameRenderBuffers per in-flight rendering frame. (The demo helper ImGui_ImplVulkanH_Window structure carries them.) (#2461, #2348, #2378, #2097) -- Examples: Vulkan: Added MinImageCount field in ImGui_ImplVulkan_InitInfo, required during - initialization to specify the number of in-flight image requested by swap chains. +- Examples: Vulkan: Added MinImageCount/ImageCount fields in ImGui_ImplVulkan_InitInfo, required + during initialization to specify the number of in-flight image requested by swap chains. (was previously a hard #define IMGUI_VK_QUEUED_FRAMES 2). (#2071, #1677) [@nathanvoglsam] - Examples: Vulkan: Tidying up the demo/internals helpers (most engine/app should not rely on them but it is possible you have!). diff --git a/examples/example_glfw_vulkan/main.cpp b/examples/example_glfw_vulkan/main.cpp index c8417391..81630838 100644 --- a/examples/example_glfw_vulkan/main.cpp +++ b/examples/example_glfw_vulkan/main.cpp @@ -391,6 +391,7 @@ int main(int, char**) init_info.DescriptorPool = g_DescriptorPool; init_info.Allocator = g_Allocator; init_info.MinImageCount = g_MinImageCount; + init_info.ImageCount = wd->ImageCount; init_info.CheckVkResultFn = check_vk_result; ImGui_ImplVulkan_Init(&init_info, wd->RenderPass); diff --git a/examples/example_sdl_vulkan/main.cpp b/examples/example_sdl_vulkan/main.cpp index 2124f613..976d026e 100644 --- a/examples/example_sdl_vulkan/main.cpp +++ b/examples/example_sdl_vulkan/main.cpp @@ -375,6 +375,7 @@ int main(int, char**) init_info.DescriptorPool = g_DescriptorPool; init_info.Allocator = g_Allocator; init_info.MinImageCount = g_MinImageCount; + init_info.ImageCount = wd->ImageCount; init_info.CheckVkResultFn = check_vk_result; ImGui_ImplVulkan_Init(&init_info, wd->RenderPass); diff --git a/examples/imgui_impl_vulkan.cpp b/examples/imgui_impl_vulkan.cpp index c8bb7ab9..df8f84fc 100644 --- a/examples/imgui_impl_vulkan.cpp +++ b/examples/imgui_impl_vulkan.cpp @@ -755,6 +755,7 @@ bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass rend IM_ASSERT(info->Queue != VK_NULL_HANDLE); IM_ASSERT(info->DescriptorPool != VK_NULL_HANDLE); IM_ASSERT(info->MinImageCount >= 2); + IM_ASSERT(info->ImageCount >= info->MinImageCount); IM_ASSERT(render_pass != VK_NULL_HANDLE); g_VulkanInitInfo = *info; @@ -887,7 +888,7 @@ void ImGui_ImplVulkanH_CreateWindowCommandBuffers(VkInstance instance, VkPhysica // Create Command Buffers VkResult err; - for (uint32_t i = 0; i < wd->FramesQueueSize; i++) + for (uint32_t i = 0; i < wd->ImageCount; i++) { ImGui_ImplVulkanH_Frame* fd = &wd->Frames[i]; ImGui_ImplVulkanH_FrameSemaphores* fsd = &wd->FrameSemaphores[i]; @@ -948,7 +949,7 @@ void ImGui_ImplVulkanH_CreateWindowSwapChain(VkInstance instance, VkPhysicalDevi // We don't use ImGui_ImplVulkanH_DestroyWindow() because we want to preserve the old swapchain to create the new one. // Destroy old Framebuffer - for (uint32_t i = 0; i < wd->FramesQueueSize; i++) + for (uint32_t i = 0; i < wd->ImageCount; i++) { ImGui_ImplVulkanH_DestroyFrame(instance, device, &wd->Frames[i], allocator); ImGui_ImplVulkanH_DestroyFrameSemaphores(instance, device, &wd->FrameSemaphores[i], allocator); @@ -957,7 +958,7 @@ void ImGui_ImplVulkanH_CreateWindowSwapChain(VkInstance instance, VkPhysicalDevi delete[] wd->FrameSemaphores; wd->Frames = NULL; wd->FrameSemaphores = NULL; - wd->FramesQueueSize = 0; + wd->ImageCount = 0; if (wd->RenderPass) vkDestroyRenderPass(device, wd->RenderPass, allocator); @@ -1001,20 +1002,20 @@ void ImGui_ImplVulkanH_CreateWindowSwapChain(VkInstance instance, VkPhysicalDevi } err = vkCreateSwapchainKHR(device, &info, allocator, &wd->Swapchain); check_vk_result(err); - err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->FramesQueueSize, NULL); + err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->ImageCount, NULL); check_vk_result(err); VkImage backbuffers[16] = {}; - IM_ASSERT(wd->FramesQueueSize >= min_image_count); - IM_ASSERT(wd->FramesQueueSize < IM_ARRAYSIZE(backbuffers)); - err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->FramesQueueSize, backbuffers); + IM_ASSERT(wd->ImageCount >= min_image_count); + IM_ASSERT(wd->ImageCount < IM_ARRAYSIZE(backbuffers)); + err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->ImageCount, backbuffers); check_vk_result(err); IM_ASSERT(wd->Frames == NULL); - wd->Frames = new ImGui_ImplVulkanH_Frame[wd->FramesQueueSize]; - wd->FrameSemaphores = new ImGui_ImplVulkanH_FrameSemaphores[wd->FramesQueueSize]; - memset(wd->Frames, 0, sizeof(wd->Frames[0]) * wd->FramesQueueSize); - memset(wd->FrameSemaphores, 0, sizeof(wd->FrameSemaphores[0]) * wd->FramesQueueSize); - for (uint32_t i = 0; i < wd->FramesQueueSize; i++) + wd->Frames = new ImGui_ImplVulkanH_Frame[wd->ImageCount]; + wd->FrameSemaphores = new ImGui_ImplVulkanH_FrameSemaphores[wd->ImageCount]; + memset(wd->Frames, 0, sizeof(wd->Frames[0]) * wd->ImageCount); + memset(wd->FrameSemaphores, 0, sizeof(wd->FrameSemaphores[0]) * wd->ImageCount); + for (uint32_t i = 0; i < wd->ImageCount; i++) wd->Frames[i].Backbuffer = backbuffers[i]; } if (old_swapchain) @@ -1069,7 +1070,7 @@ void ImGui_ImplVulkanH_CreateWindowSwapChain(VkInstance instance, VkPhysicalDevi info.components.a = VK_COMPONENT_SWIZZLE_A; VkImageSubresourceRange image_range = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 }; info.subresourceRange = image_range; - for (uint32_t i = 0; i < wd->FramesQueueSize; i++) + for (uint32_t i = 0; i < wd->ImageCount; i++) { ImGui_ImplVulkanH_Frame* fd = &wd->Frames[i]; info.image = fd->Backbuffer; @@ -1089,7 +1090,7 @@ void ImGui_ImplVulkanH_CreateWindowSwapChain(VkInstance instance, VkPhysicalDevi info.width = wd->Width; info.height = wd->Height; info.layers = 1; - for (uint32_t i = 0; i < wd->FramesQueueSize; i++) + for (uint32_t i = 0; i < wd->ImageCount; i++) { ImGui_ImplVulkanH_Frame* fd = &wd->Frames[i]; attachment[0] = fd->BackbufferView; @@ -1110,7 +1111,7 @@ void ImGui_ImplVulkanH_DestroyWindow(VkInstance instance, VkDevice device, ImGui vkDeviceWaitIdle(device); // FIXME: We could wait on the Queue if we had the queue in wd-> (otherwise VulkanH functions can't use globals) //vkQueueWaitIdle(g_Queue); - for (uint32_t i = 0; i < wd->FramesQueueSize; i++) + for (uint32_t i = 0; i < wd->ImageCount; i++) { ImGui_ImplVulkanH_DestroyFrame(instance, device, &wd->Frames[i], allocator); ImGui_ImplVulkanH_DestroyFrameSemaphores(instance, device, &wd->FrameSemaphores[i], allocator); diff --git a/examples/imgui_impl_vulkan.h b/examples/imgui_impl_vulkan.h index b872ae21..55d72dfb 100644 --- a/examples/imgui_impl_vulkan.h +++ b/examples/imgui_impl_vulkan.h @@ -33,7 +33,8 @@ struct ImGui_ImplVulkan_InitInfo VkQueue Queue; VkPipelineCache PipelineCache; VkDescriptorPool DescriptorPool; - int MinImageCount; // >= 2 + uint32_t MinImageCount; // >= 2 + uint32_t ImageCount; // >= MinImageCount const VkAllocationCallbacks* Allocator; void (*CheckVkResultFn)(VkResult err); }; @@ -60,7 +61,7 @@ IMGUI_IMPL_API void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, V IMGUI_IMPL_API bool ImGui_ImplVulkan_CreateFontsTexture(VkCommandBuffer command_buffer); IMGUI_IMPL_API void ImGui_ImplVulkan_DestroyFontUploadObjects(); IMGUI_IMPL_API void ImGui_ImplVulkan_DestroyFrameRenderBuffers(VkInstance instance, VkDevice device, ImGui_ImplVulkan_FrameRenderBuffers* buffers, const VkAllocationCallbacks* allocator); -IMGUI_IMPL_API void ImGui_ImplVulkan_SetSwapChainMinImageCount(int frames_queue_size); // To override MinImageCount after initialization +IMGUI_IMPL_API void ImGui_ImplVulkan_SetSwapChainMinImageCount(int min_image_count); // To override MinImageCount after initialization // Called by ImGui_ImplVulkan_Init(), might be useful elsewhere. IMGUI_IMPL_API bool ImGui_ImplVulkan_CreateDeviceObjects(); @@ -127,12 +128,11 @@ struct ImGui_ImplVulkanH_Window bool ClearEnable; VkClearValue ClearValue; uint32_t FrameIndex; // Current frame being rendered to (0 <= FrameIndex < FrameInFlightCount) - uint32_t FramesQueueSize; // Number of simultaneous in-flight frames (returned by vkGetSwapchainImagesKHR, usually derived from min_image_count) + uint32_t ImageCount; // Number of simultaneous in-flight frames (returned by vkGetSwapchainImagesKHR, usually derived from min_image_count) uint32_t SemaphoreIndex; // Current set of swapchain wait semaphores we're using (needs to be distinct from per frame data) - ImGui_ImplVulkanH_Frame* Frames; - ImGui_ImplVulkanH_FrameSemaphores* FrameSemaphores; - - + ImGui_ImplVulkanH_Frame* Frames; + ImGui_ImplVulkanH_FrameSemaphores* FrameSemaphores; + ImGui_ImplVulkanH_Window() { memset(this, 0, sizeof(*this)); From 452047c7cadb9e4c88537a4c0c2e1b9e571ceccd Mon Sep 17 00:00:00 2001 From: omar Date: Fri, 5 Apr 2019 17:48:47 +0200 Subject: [PATCH 13/19] Vulkan: Removed requirement for user to pass their own render buffer storage to ImGui_ImplVulkan_RenderDrawData(), this is managed internally. --- docs/CHANGELOG.txt | 6 +- examples/example_glfw_vulkan/main.cpp | 4 +- examples/example_sdl_vulkan/main.cpp | 4 +- examples/imgui_impl_vulkan.cpp | 136 ++++++++++++++++++-------- examples/imgui_impl_vulkan.h | 22 +---- 5 files changed, 103 insertions(+), 69 deletions(-) diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index a3283b1b..1822526f 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -34,16 +34,12 @@ HOW TO UPDATE? ----------------------------------------------------------------------- Breaking Changes: -- Examples: Vulkan: Added extra parameter to ImGui_ImplVulkan_RenderDrawData(). Engine/app is - in charge of owning/storing 1 ImGui_ImplVulkan_FrameRenderBuffers per in-flight rendering frame. - (The demo helper ImGui_ImplVulkanH_Window structure carries them.) (#2461, #2348, #2378, #2097) - Examples: Vulkan: Added MinImageCount/ImageCount fields in ImGui_ImplVulkan_InitInfo, required during initialization to specify the number of in-flight image requested by swap chains. (was previously a hard #define IMGUI_VK_QUEUED_FRAMES 2). (#2071, #1677) [@nathanvoglsam] - Examples: Vulkan: Tidying up the demo/internals helpers (most engine/app should not rely on them but it is possible you have!). - Other Changes: - InputText: Fixed selection background starts rendering one frame after the cursor movement when first transitioning from no-selection to has-selection. (Bug in 1.69) (#2436) [@Nazg-Gul] @@ -56,8 +52,10 @@ Other Changes: - Examples: OpenGL: Added a dummy GL call + comments in ImGui_ImplOpenGL3_Init() to detect uninitialized GL function loaders early, and help users understand what they are missing. (#2421) - Examples: OpenGL3: Minor tweaks + not calling glBindBuffer more than necessary in the render loop. +- Examples: Vulkan: Fixed in-flight buffers issues when using multi-viewports. (#2461, #2348, #2378, #2097) - Examples: Vulkan: Added missing support for 32-bit indices (#define ImDrawIdx unsigned int). - Examples: Vulkan: Avoid passing negative coordinates to vkCmdSetScissor, which debug validation layers do not like. +- Examples: Vulkan: Added ImGui_ImplVulkan_SetMinImageCount() to change min image count at runtime. (#2071) [@nathanvoglsam] - Examples: DirectX9: Fixed erroneous assert in ImGui_ImplDX9_InvalidateDeviceObjects(). (#2454) - Examples: GLUT: Fixed existing FreeGLUT example to work with regular GLUT. (#2465) [@andrewwillmott] - Examples: GLUT: Renamed imgui_impl_freeglut.cpp/.h to imgui_impl_glut.cpp/.h. (#2465) [@andrewwillmott] diff --git a/examples/example_glfw_vulkan/main.cpp b/examples/example_glfw_vulkan/main.cpp index 81630838..972c12b0 100644 --- a/examples/example_glfw_vulkan/main.cpp +++ b/examples/example_glfw_vulkan/main.cpp @@ -287,7 +287,7 @@ static void FrameRender(ImGui_ImplVulkanH_Window* wd) } // Record Imgui Draw Data and draw funcs into command buffer - ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), fd->CommandBuffer, &fd->RenderBuffers); + ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), fd->CommandBuffer); // Submit command buffer vkCmdEndRenderPass(fd->CommandBuffer); @@ -457,8 +457,8 @@ int main(int, char**) if (g_SwapChainRebuild) { g_SwapChainRebuild = false; + ImGui_ImplVulkan_SetMinImageCount(g_MinImageCount); ImGui_ImplVulkanH_CreateWindow(g_Instance, g_PhysicalDevice, g_Device, &g_WindowData, g_QueueFamily, g_Allocator, g_SwapChainResizeWidth, g_SwapChainResizeHeight, g_MinImageCount); - ImGui_ImplVulkan_SetSwapChainMinImageCount(g_MinImageCount); g_WindowData.FrameIndex = 0; } diff --git a/examples/example_sdl_vulkan/main.cpp b/examples/example_sdl_vulkan/main.cpp index 976d026e..d2a829c4 100644 --- a/examples/example_sdl_vulkan/main.cpp +++ b/examples/example_sdl_vulkan/main.cpp @@ -279,7 +279,7 @@ static void FrameRender(ImGui_ImplVulkanH_Window* wd) } // Record Imgui Draw Data and draw funcs into command buffer - ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), fd->CommandBuffer, &fd->RenderBuffers); + ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), fd->CommandBuffer); // Submit command buffer vkCmdEndRenderPass(fd->CommandBuffer); @@ -454,8 +454,8 @@ int main(int, char**) if (g_SwapChainRebuild) { g_SwapChainRebuild = false; + ImGui_ImplVulkan_SetMinImageCount(g_MinImageCount); ImGui_ImplVulkanH_CreateWindow(g_Instance, g_PhysicalDevice, g_Device, &g_WindowData, g_QueueFamily, g_Allocator, g_SwapChainResizeWidth, g_SwapChainResizeHeight, g_MinImageCount); - ImGui_ImplVulkan_SetSwapChainMinImageCount(g_MinImageCount); g_WindowData.FrameIndex = 0; } diff --git a/examples/imgui_impl_vulkan.cpp b/examples/imgui_impl_vulkan.cpp index df8f84fc..fb54801f 100644 --- a/examples/imgui_impl_vulkan.cpp +++ b/examples/imgui_impl_vulkan.cpp @@ -20,8 +20,7 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) -// 2019-XX-XX: *BREAKING CHANGE*: Vulkan: Added extra parameter to ImGui_ImplVulkan_RenderDrawData(). Engine/app is in charge of owning/storing 1 ImGui_ImplVulkan_FrameRenderBuffers per in-flight rendering frame. (The demo helper ImGui_ImplVulkanH_Window structure carries them.) -// 2019-XX-XX: *BREAKING CHANGE*: Vulkan: Added MinImageCount field in ImGui_ImplVulkan_InitInfo, required for initialization (was previously a hard #define IMGUI_VK_QUEUED_FRAMES 2). Added ImGui_ImplVulkan_SetSwapChainMinImageCount(). +// 2019-XX-XX: *BREAKING CHANGE*: Vulkan: Added ImageCount/MinImageCount fields in ImGui_ImplVulkan_InitInfo, required for initialization (was previously a hard #define IMGUI_VK_QUEUED_FRAMES 2). Added ImGui_ImplVulkan_SetMinImageCount(). // 2019-XX-XX: Vulkan: Added VkInstance argument to ImGui_ImplVulkanH_CreateWindow() optional helper. // 2019-04-04: Vulkan: Avoid passing negative coordinates to vkCmdSetScissor, which debug validation layers do not like. // 2019-04-01: Vulkan: Support for 32-bit index buffer (#define ImDrawIdx unsigned int). @@ -44,6 +43,27 @@ #include "imgui_impl_vulkan.h" #include +// Reusable buffers used for rendering 1 current in-flight frame, for ImGui_ImplVulkan_RenderDrawData() +// [Please zero-clear before use!] +struct ImGui_ImplVulkanH_FrameRenderBuffers +{ + VkDeviceMemory VertexBufferMemory; + VkDeviceMemory IndexBufferMemory; + VkDeviceSize VertexBufferSize; + VkDeviceSize IndexBufferSize; + VkBuffer VertexBuffer; + VkBuffer IndexBuffer; +}; + +// Each viewport will hold 1 ImGui_ImplVulkanH_WindowRenderBuffers +// [Please zero-clear before use!] +struct ImGui_ImplVulkanH_WindowRenderBuffers +{ + uint32_t Index; + uint32_t Count; + ImGui_ImplVulkanH_FrameRenderBuffers* FrameRenderBuffers; +}; + // Vulkan data static ImGui_ImplVulkan_InitInfo g_VulkanInitInfo = {}; static VkRenderPass g_RenderPass = VK_NULL_HANDLE; @@ -62,9 +82,14 @@ static VkImageView g_FontView = VK_NULL_HANDLE; static VkDeviceMemory g_UploadBufferMemory = VK_NULL_HANDLE; static VkBuffer g_UploadBuffer = VK_NULL_HANDLE; +// Render buffers +static ImGui_ImplVulkanH_WindowRenderBuffers g_MainWindowRenderBuffers; + // Forward Declarations void ImGui_ImplVulkanH_DestroyFrame(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_Frame* fd, const VkAllocationCallbacks* allocator); void ImGui_ImplVulkanH_DestroyFrameSemaphores(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_FrameSemaphores* fsd, const VkAllocationCallbacks* allocator); +void ImGui_ImplVulkanH_DestroyFrameRenderBuffers(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_FrameRenderBuffers* buffers, const VkAllocationCallbacks* allocator); +void ImGui_ImplVulkanH_DestroyWindowRenderBuffers(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_WindowRenderBuffers* buffers, const VkAllocationCallbacks* allocator); void ImGui_ImplVulkanH_CreateWindowSwapChain(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count); void ImGui_ImplVulkanH_CreateWindowCommandBuffers(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator); @@ -234,7 +259,7 @@ static void CreateOrResizeBuffer(VkBuffer& buffer, VkDeviceMemory& buffer_memory // Render function // (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop) -void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer command_buffer, ImGui_ImplVulkan_FrameRenderBuffers* fd) +void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer command_buffer) { // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates) int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x); @@ -243,23 +268,37 @@ void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer comm return; ImGui_ImplVulkan_InitInfo* v = &g_VulkanInitInfo; + + // Allocate array to store enough vertex/index buffers + ImGui_ImplVulkanH_WindowRenderBuffers* wrb = &g_MainWindowRenderBuffers; + if (wrb->FrameRenderBuffers == NULL) + { + wrb->Index = 0; + wrb->Count = v->ImageCount; + wrb->FrameRenderBuffers = new ImGui_ImplVulkanH_FrameRenderBuffers[wrb->Count]; + memset(wrb->FrameRenderBuffers, 0, sizeof(ImGui_ImplVulkanH_FrameRenderBuffers) * wrb->Count); + } + IM_ASSERT(wrb->Count == v->ImageCount); + wrb->Index = (wrb->Index + 1) % wrb->Count; + ImGui_ImplVulkanH_FrameRenderBuffers* rb = &wrb->FrameRenderBuffers[wrb->Index]; + VkResult err; - // Create the Vertex and Index buffers: + // Create or resize the vertex/index buffers size_t vertex_size = draw_data->TotalVtxCount * sizeof(ImDrawVert); size_t index_size = draw_data->TotalIdxCount * sizeof(ImDrawIdx); - if (fd->VertexBuffer == VK_NULL_HANDLE || fd->VertexBufferSize < vertex_size) - CreateOrResizeBuffer(fd->VertexBuffer, fd->VertexBufferMemory, fd->VertexBufferSize, vertex_size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); - if (fd->IndexBuffer == VK_NULL_HANDLE || fd->IndexBufferSize < index_size) - CreateOrResizeBuffer(fd->IndexBuffer, fd->IndexBufferMemory, fd->IndexBufferSize, index_size, VK_BUFFER_USAGE_INDEX_BUFFER_BIT); + if (rb->VertexBuffer == VK_NULL_HANDLE || rb->VertexBufferSize < vertex_size) + CreateOrResizeBuffer(rb->VertexBuffer, rb->VertexBufferMemory, rb->VertexBufferSize, vertex_size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); + if (rb->IndexBuffer == VK_NULL_HANDLE || rb->IndexBufferSize < index_size) + CreateOrResizeBuffer(rb->IndexBuffer, rb->IndexBufferMemory, rb->IndexBufferSize, index_size, VK_BUFFER_USAGE_INDEX_BUFFER_BIT); // Upload vertex/index data into a single contiguous GPU buffer { ImDrawVert* vtx_dst = NULL; ImDrawIdx* idx_dst = NULL; - err = vkMapMemory(v->Device, fd->VertexBufferMemory, 0, vertex_size, 0, (void**)(&vtx_dst)); + err = vkMapMemory(v->Device, rb->VertexBufferMemory, 0, vertex_size, 0, (void**)(&vtx_dst)); check_vk_result(err); - err = vkMapMemory(v->Device, fd->IndexBufferMemory, 0, index_size, 0, (void**)(&idx_dst)); + err = vkMapMemory(v->Device, rb->IndexBufferMemory, 0, index_size, 0, (void**)(&idx_dst)); check_vk_result(err); for (int n = 0; n < draw_data->CmdListsCount; n++) { @@ -271,15 +310,15 @@ void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer comm } VkMappedMemoryRange range[2] = {}; range[0].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE; - range[0].memory = fd->VertexBufferMemory; + range[0].memory = rb->VertexBufferMemory; range[0].size = VK_WHOLE_SIZE; range[1].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE; - range[1].memory = fd->IndexBufferMemory; + range[1].memory = rb->IndexBufferMemory; range[1].size = VK_WHOLE_SIZE; err = vkFlushMappedMemoryRanges(v->Device, 2, range); check_vk_result(err); - vkUnmapMemory(v->Device, fd->VertexBufferMemory); - vkUnmapMemory(v->Device, fd->IndexBufferMemory); + vkUnmapMemory(v->Device, rb->VertexBufferMemory); + vkUnmapMemory(v->Device, rb->IndexBufferMemory); } // Bind pipeline and descriptor sets: @@ -291,10 +330,10 @@ void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer comm // Bind Vertex And Index Buffer: { - VkBuffer vertex_buffers[1] = { fd->VertexBuffer }; + VkBuffer vertex_buffers[1] = { rb->VertexBuffer }; VkDeviceSize vertex_offset[1] = { 0 }; vkCmdBindVertexBuffers(command_buffer, 0, 1, vertex_buffers, vertex_offset); - vkCmdBindIndexBuffer(command_buffer, fd->IndexBuffer, 0, sizeof(ImDrawIdx) == 2 ? VK_INDEX_TYPE_UINT16 : VK_INDEX_TYPE_UINT32); + vkCmdBindIndexBuffer(command_buffer, rb->IndexBuffer, 0, sizeof(ImDrawIdx) == 2 ? VK_INDEX_TYPE_UINT16 : VK_INDEX_TYPE_UINT32); } // Setup viewport: @@ -733,8 +772,9 @@ void ImGui_ImplVulkan_DestroyFontUploadObjects() void ImGui_ImplVulkan_DestroyDeviceObjects() { ImGui_ImplVulkan_InitInfo* v = &g_VulkanInitInfo; - + ImGui_ImplVulkanH_DestroyWindowRenderBuffers(v->Instance, v->Device, &g_MainWindowRenderBuffers, v->Allocator); ImGui_ImplVulkan_DestroyFontUploadObjects(); + if (g_FontView) { vkDestroyImageView(v->Device, g_FontView, v->Allocator); g_FontView = VK_NULL_HANDLE; } if (g_FontImage) { vkDestroyImage(v->Device, g_FontImage, v->Allocator); g_FontImage = VK_NULL_HANDLE; } if (g_FontMemory) { vkFreeMemory(v->Device, g_FontMemory, v->Allocator); g_FontMemory = VK_NULL_HANDLE; } @@ -774,24 +814,17 @@ void ImGui_ImplVulkan_NewFrame() { } -// Note: this has no effect in the 'master' branch, but multi-viewports needs this to recreate swap-chains. -void ImGui_ImplVulkan_SetSwapChainMinImageCount(int min_image_count) +void ImGui_ImplVulkan_SetMinImageCount(uint32_t min_image_count) { IM_ASSERT(min_image_count >= 2); - g_VulkanInitInfo.MinImageCount = min_image_count; -} + if (g_VulkanInitInfo.MinImageCount == min_image_count) + return; -// This is a public function because we require the user to pass a ImGui_ImplVulkan_FrameRenderBuffers -// structure to ImGui_ImplVulkan_RenderDrawData. -void ImGui_ImplVulkan_DestroyFrameRenderBuffers(VkInstance instance, VkDevice device, ImGui_ImplVulkan_FrameRenderBuffers* buffers, const VkAllocationCallbacks* allocator) -{ - (void)instance; - if (buffers->VertexBuffer) { vkDestroyBuffer(device, buffers->VertexBuffer, allocator); buffers->VertexBuffer = VK_NULL_HANDLE; } - if (buffers->VertexBufferMemory) { vkFreeMemory (device, buffers->VertexBufferMemory, allocator); buffers->VertexBufferMemory = VK_NULL_HANDLE; } - if (buffers->IndexBuffer) { vkDestroyBuffer(device, buffers->IndexBuffer, allocator); buffers->IndexBuffer = VK_NULL_HANDLE; } - if (buffers->IndexBufferMemory) { vkFreeMemory (device, buffers->IndexBufferMemory, allocator); buffers->IndexBufferMemory = VK_NULL_HANDLE; } - buffers->VertexBufferSize = 0; - buffers->IndexBufferSize = 0; + ImGui_ImplVulkan_InitInfo* v = &g_VulkanInitInfo; + VkResult err = vkDeviceWaitIdle(v->Device); + check_vk_result(err); + ImGui_ImplVulkanH_DestroyWindowRenderBuffers(v->Instance, v->Device, &g_MainWindowRenderBuffers, v->Allocator); + g_VulkanInitInfo.MinImageCount = min_image_count; } @@ -1127,14 +1160,6 @@ void ImGui_ImplVulkanH_DestroyWindow(VkInstance instance, VkDevice device, ImGui *wd = ImGui_ImplVulkanH_Window(); } -void ImGui_ImplVulkanH_DestroyFrameSemaphores(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_FrameSemaphores* fsd, const VkAllocationCallbacks* allocator) -{ - (void)instance; - vkDestroySemaphore(device, fsd->ImageAcquiredSemaphore, allocator); - vkDestroySemaphore(device, fsd->RenderCompleteSemaphore, allocator); - fsd->ImageAcquiredSemaphore = fsd->RenderCompleteSemaphore = VK_NULL_HANDLE; -} - void ImGui_ImplVulkanH_DestroyFrame(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_Frame* fd, const VkAllocationCallbacks* allocator) { (void)instance; @@ -1147,6 +1172,33 @@ void ImGui_ImplVulkanH_DestroyFrame(VkInstance instance, VkDevice device, ImGui_ vkDestroyImageView(device, fd->BackbufferView, allocator); vkDestroyFramebuffer(device, fd->Framebuffer, allocator); - - ImGui_ImplVulkan_DestroyFrameRenderBuffers(instance, device, &fd->RenderBuffers, allocator); +} + +void ImGui_ImplVulkanH_DestroyFrameSemaphores(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_FrameSemaphores* fsd, const VkAllocationCallbacks* allocator) +{ + (void)instance; + vkDestroySemaphore(device, fsd->ImageAcquiredSemaphore, allocator); + vkDestroySemaphore(device, fsd->RenderCompleteSemaphore, allocator); + fsd->ImageAcquiredSemaphore = fsd->RenderCompleteSemaphore = VK_NULL_HANDLE; +} + +void ImGui_ImplVulkanH_DestroyFrameRenderBuffers(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_FrameRenderBuffers* buffers, const VkAllocationCallbacks* allocator) +{ + (void)instance; + if (buffers->VertexBuffer) { vkDestroyBuffer(device, buffers->VertexBuffer, allocator); buffers->VertexBuffer = VK_NULL_HANDLE; } + if (buffers->VertexBufferMemory) { vkFreeMemory(device, buffers->VertexBufferMemory, allocator); buffers->VertexBufferMemory = VK_NULL_HANDLE; } + if (buffers->IndexBuffer) { vkDestroyBuffer(device, buffers->IndexBuffer, allocator); buffers->IndexBuffer = VK_NULL_HANDLE; } + if (buffers->IndexBufferMemory) { vkFreeMemory(device, buffers->IndexBufferMemory, allocator); buffers->IndexBufferMemory = VK_NULL_HANDLE; } + buffers->VertexBufferSize = 0; + buffers->IndexBufferSize = 0; +} + +void ImGui_ImplVulkanH_DestroyWindowRenderBuffers(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_WindowRenderBuffers* buffers, const VkAllocationCallbacks* allocator) +{ + for (uint32_t n = 0; n < buffers->Count; n++) + ImGui_ImplVulkanH_DestroyFrameRenderBuffers(instance, device, &buffers->FrameRenderBuffers[n], allocator); + delete[] buffers->FrameRenderBuffers; + buffers->FrameRenderBuffers = NULL; + buffers->Index = 0; + buffers->Count = 0; } diff --git a/examples/imgui_impl_vulkan.h b/examples/imgui_impl_vulkan.h index 55d72dfb..8b5421cc 100644 --- a/examples/imgui_impl_vulkan.h +++ b/examples/imgui_impl_vulkan.h @@ -39,29 +39,14 @@ struct ImGui_ImplVulkan_InitInfo void (*CheckVkResultFn)(VkResult err); }; -// Reusable buffers used for rendering by current in-flight frame, for ImGui_ImplVulkan_RenderDrawData() -// [Please zero-clear before use!] -// In the examples we store those in the helper ImGui_ImplVulkanH_FrameData structure, however as your own engine/app likely won't use the ImGui_Impl_VulkanH_XXXX helpers, -// you are expected to hold on as many ImGui_ImplVulkan_FrameRenderBuffers structures on your side as you have in-flight frames (== init_info.FramesQueueSize) -struct ImGui_ImplVulkan_FrameRenderBuffers -{ - VkDeviceMemory VertexBufferMemory; - VkDeviceMemory IndexBufferMemory; - VkDeviceSize VertexBufferSize; - VkDeviceSize IndexBufferSize; - VkBuffer VertexBuffer; - VkBuffer IndexBuffer; -}; - // Called by user code IMGUI_IMPL_API bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass render_pass); IMGUI_IMPL_API void ImGui_ImplVulkan_Shutdown(); IMGUI_IMPL_API void ImGui_ImplVulkan_NewFrame(); -IMGUI_IMPL_API void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer command_buffer, ImGui_ImplVulkan_FrameRenderBuffers* buffers); +IMGUI_IMPL_API void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer command_buffer); IMGUI_IMPL_API bool ImGui_ImplVulkan_CreateFontsTexture(VkCommandBuffer command_buffer); IMGUI_IMPL_API void ImGui_ImplVulkan_DestroyFontUploadObjects(); -IMGUI_IMPL_API void ImGui_ImplVulkan_DestroyFrameRenderBuffers(VkInstance instance, VkDevice device, ImGui_ImplVulkan_FrameRenderBuffers* buffers, const VkAllocationCallbacks* allocator); -IMGUI_IMPL_API void ImGui_ImplVulkan_SetSwapChainMinImageCount(int min_image_count); // To override MinImageCount after initialization +IMGUI_IMPL_API void ImGui_ImplVulkan_SetMinImageCount(uint32_t min_image_count); // To override MinImageCount after initialization (e.g. if swap chain is recreated) // Called by ImGui_ImplVulkan_Init(), might be useful elsewhere. IMGUI_IMPL_API bool ImGui_ImplVulkan_CreateDeviceObjects(); @@ -105,7 +90,6 @@ struct ImGui_ImplVulkanH_Frame VkImage Backbuffer; VkImageView BackbufferView; VkFramebuffer Framebuffer; - ImGui_ImplVulkan_FrameRenderBuffers RenderBuffers; }; struct ImGui_ImplVulkanH_FrameSemaphores @@ -132,7 +116,7 @@ struct ImGui_ImplVulkanH_Window uint32_t SemaphoreIndex; // Current set of swapchain wait semaphores we're using (needs to be distinct from per frame data) ImGui_ImplVulkanH_Frame* Frames; ImGui_ImplVulkanH_FrameSemaphores* FrameSemaphores; - + ImGui_ImplVulkanH_Window() { memset(this, 0, sizeof(*this)); From 6bc47dfe48825b55c428e1ecb26910676ec7a007 Mon Sep 17 00:00:00 2001 From: omar Date: Fri, 5 Apr 2019 17:54:16 +0200 Subject: [PATCH 14/19] Vulkan: Removed superfluous vkInstance parameters being passed along. --- examples/imgui_impl_vulkan.cpp | 49 ++++++++++++++++------------------ 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/examples/imgui_impl_vulkan.cpp b/examples/imgui_impl_vulkan.cpp index fb54801f..48237139 100644 --- a/examples/imgui_impl_vulkan.cpp +++ b/examples/imgui_impl_vulkan.cpp @@ -86,12 +86,12 @@ static VkBuffer g_UploadBuffer = VK_NULL_HANDLE; static ImGui_ImplVulkanH_WindowRenderBuffers g_MainWindowRenderBuffers; // Forward Declarations -void ImGui_ImplVulkanH_DestroyFrame(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_Frame* fd, const VkAllocationCallbacks* allocator); -void ImGui_ImplVulkanH_DestroyFrameSemaphores(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_FrameSemaphores* fsd, const VkAllocationCallbacks* allocator); -void ImGui_ImplVulkanH_DestroyFrameRenderBuffers(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_FrameRenderBuffers* buffers, const VkAllocationCallbacks* allocator); -void ImGui_ImplVulkanH_DestroyWindowRenderBuffers(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_WindowRenderBuffers* buffers, const VkAllocationCallbacks* allocator); -void ImGui_ImplVulkanH_CreateWindowSwapChain(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count); -void ImGui_ImplVulkanH_CreateWindowCommandBuffers(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator); +void ImGui_ImplVulkanH_DestroyFrame(VkDevice device, ImGui_ImplVulkanH_Frame* fd, const VkAllocationCallbacks* allocator); +void ImGui_ImplVulkanH_DestroyFrameSemaphores(VkDevice device, ImGui_ImplVulkanH_FrameSemaphores* fsd, const VkAllocationCallbacks* allocator); +void ImGui_ImplVulkanH_DestroyFrameRenderBuffers(VkDevice device, ImGui_ImplVulkanH_FrameRenderBuffers* buffers, const VkAllocationCallbacks* allocator); +void ImGui_ImplVulkanH_DestroyWindowRenderBuffers(VkDevice device, ImGui_ImplVulkanH_WindowRenderBuffers* buffers, const VkAllocationCallbacks* allocator); +void ImGui_ImplVulkanH_CreateWindowSwapChain(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count); +void ImGui_ImplVulkanH_CreateWindowCommandBuffers(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator); //----------------------------------------------------------------------------- // SHADERS @@ -772,7 +772,7 @@ void ImGui_ImplVulkan_DestroyFontUploadObjects() void ImGui_ImplVulkan_DestroyDeviceObjects() { ImGui_ImplVulkan_InitInfo* v = &g_VulkanInitInfo; - ImGui_ImplVulkanH_DestroyWindowRenderBuffers(v->Instance, v->Device, &g_MainWindowRenderBuffers, v->Allocator); + ImGui_ImplVulkanH_DestroyWindowRenderBuffers(v->Device, &g_MainWindowRenderBuffers, v->Allocator); ImGui_ImplVulkan_DestroyFontUploadObjects(); if (g_FontView) { vkDestroyImageView(v->Device, g_FontView, v->Allocator); g_FontView = VK_NULL_HANDLE; } @@ -823,7 +823,7 @@ void ImGui_ImplVulkan_SetMinImageCount(uint32_t min_image_count) ImGui_ImplVulkan_InitInfo* v = &g_VulkanInitInfo; VkResult err = vkDeviceWaitIdle(v->Device); check_vk_result(err); - ImGui_ImplVulkanH_DestroyWindowRenderBuffers(v->Instance, v->Device, &g_MainWindowRenderBuffers, v->Allocator); + ImGui_ImplVulkanH_DestroyWindowRenderBuffers(v->Device, &g_MainWindowRenderBuffers, v->Allocator); g_VulkanInitInfo.MinImageCount = min_image_count; } @@ -912,10 +912,9 @@ VkPresentModeKHR ImGui_ImplVulkanH_SelectPresentMode(VkPhysicalDevice physical_d return VK_PRESENT_MODE_FIFO_KHR; // Always available } -void ImGui_ImplVulkanH_CreateWindowCommandBuffers(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator) +void ImGui_ImplVulkanH_CreateWindowCommandBuffers(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator) { - IM_ASSERT(instance != VK_NULL_HANDLE && physical_device != VK_NULL_HANDLE && device != VK_NULL_HANDLE); - (void)instance; + IM_ASSERT(physical_device != VK_NULL_HANDLE && device != VK_NULL_HANDLE); (void)physical_device; (void)allocator; @@ -973,7 +972,7 @@ int ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(VkPresentModeKHR present_m } // Also destroy old swap chain and in-flight frames data, if any. -void ImGui_ImplVulkanH_CreateWindowSwapChain(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count) +void ImGui_ImplVulkanH_CreateWindowSwapChain(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count) { VkResult err; VkSwapchainKHR old_swapchain = wd->Swapchain; @@ -984,8 +983,8 @@ void ImGui_ImplVulkanH_CreateWindowSwapChain(VkInstance instance, VkPhysicalDevi // Destroy old Framebuffer for (uint32_t i = 0; i < wd->ImageCount; i++) { - ImGui_ImplVulkanH_DestroyFrame(instance, device, &wd->Frames[i], allocator); - ImGui_ImplVulkanH_DestroyFrameSemaphores(instance, device, &wd->FrameSemaphores[i], allocator); + ImGui_ImplVulkanH_DestroyFrame(device, &wd->Frames[i], allocator); + ImGui_ImplVulkanH_DestroyFrameSemaphores(device, &wd->FrameSemaphores[i], allocator); } delete[] wd->Frames; delete[] wd->FrameSemaphores; @@ -1135,8 +1134,9 @@ void ImGui_ImplVulkanH_CreateWindowSwapChain(VkInstance instance, VkPhysicalDevi void ImGui_ImplVulkanH_CreateWindow(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator, int width, int height, uint32_t min_image_count) { - ImGui_ImplVulkanH_CreateWindowSwapChain(instance, physical_device, device, wd, allocator, width, height, min_image_count); - ImGui_ImplVulkanH_CreateWindowCommandBuffers(instance, physical_device, device, wd, queue_family, allocator); + (void)instance; + ImGui_ImplVulkanH_CreateWindowSwapChain(physical_device, device, wd, allocator, width, height, min_image_count); + ImGui_ImplVulkanH_CreateWindowCommandBuffers(physical_device, device, wd, queue_family, allocator); } void ImGui_ImplVulkanH_DestroyWindow(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_Window* wd, const VkAllocationCallbacks* allocator) @@ -1146,8 +1146,8 @@ void ImGui_ImplVulkanH_DestroyWindow(VkInstance instance, VkDevice device, ImGui for (uint32_t i = 0; i < wd->ImageCount; i++) { - ImGui_ImplVulkanH_DestroyFrame(instance, device, &wd->Frames[i], allocator); - ImGui_ImplVulkanH_DestroyFrameSemaphores(instance, device, &wd->FrameSemaphores[i], allocator); + ImGui_ImplVulkanH_DestroyFrame(device, &wd->Frames[i], allocator); + ImGui_ImplVulkanH_DestroyFrameSemaphores(device, &wd->FrameSemaphores[i], allocator); } delete[] wd->Frames; delete[] wd->FrameSemaphores; @@ -1160,9 +1160,8 @@ void ImGui_ImplVulkanH_DestroyWindow(VkInstance instance, VkDevice device, ImGui *wd = ImGui_ImplVulkanH_Window(); } -void ImGui_ImplVulkanH_DestroyFrame(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_Frame* fd, const VkAllocationCallbacks* allocator) +void ImGui_ImplVulkanH_DestroyFrame(VkDevice device, ImGui_ImplVulkanH_Frame* fd, const VkAllocationCallbacks* allocator) { - (void)instance; vkDestroyFence(device, fd->Fence, allocator); vkFreeCommandBuffers(device, fd->CommandPool, 1, &fd->CommandBuffer); vkDestroyCommandPool(device, fd->CommandPool, allocator); @@ -1174,17 +1173,15 @@ void ImGui_ImplVulkanH_DestroyFrame(VkInstance instance, VkDevice device, ImGui_ vkDestroyFramebuffer(device, fd->Framebuffer, allocator); } -void ImGui_ImplVulkanH_DestroyFrameSemaphores(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_FrameSemaphores* fsd, const VkAllocationCallbacks* allocator) +void ImGui_ImplVulkanH_DestroyFrameSemaphores(VkDevice device, ImGui_ImplVulkanH_FrameSemaphores* fsd, const VkAllocationCallbacks* allocator) { - (void)instance; vkDestroySemaphore(device, fsd->ImageAcquiredSemaphore, allocator); vkDestroySemaphore(device, fsd->RenderCompleteSemaphore, allocator); fsd->ImageAcquiredSemaphore = fsd->RenderCompleteSemaphore = VK_NULL_HANDLE; } -void ImGui_ImplVulkanH_DestroyFrameRenderBuffers(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_FrameRenderBuffers* buffers, const VkAllocationCallbacks* allocator) +void ImGui_ImplVulkanH_DestroyFrameRenderBuffers(VkDevice device, ImGui_ImplVulkanH_FrameRenderBuffers* buffers, const VkAllocationCallbacks* allocator) { - (void)instance; if (buffers->VertexBuffer) { vkDestroyBuffer(device, buffers->VertexBuffer, allocator); buffers->VertexBuffer = VK_NULL_HANDLE; } if (buffers->VertexBufferMemory) { vkFreeMemory(device, buffers->VertexBufferMemory, allocator); buffers->VertexBufferMemory = VK_NULL_HANDLE; } if (buffers->IndexBuffer) { vkDestroyBuffer(device, buffers->IndexBuffer, allocator); buffers->IndexBuffer = VK_NULL_HANDLE; } @@ -1193,10 +1190,10 @@ void ImGui_ImplVulkanH_DestroyFrameRenderBuffers(VkInstance instance, VkDevice d buffers->IndexBufferSize = 0; } -void ImGui_ImplVulkanH_DestroyWindowRenderBuffers(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_WindowRenderBuffers* buffers, const VkAllocationCallbacks* allocator) +void ImGui_ImplVulkanH_DestroyWindowRenderBuffers(VkDevice device, ImGui_ImplVulkanH_WindowRenderBuffers* buffers, const VkAllocationCallbacks* allocator) { for (uint32_t n = 0; n < buffers->Count; n++) - ImGui_ImplVulkanH_DestroyFrameRenderBuffers(instance, device, &buffers->FrameRenderBuffers[n], allocator); + ImGui_ImplVulkanH_DestroyFrameRenderBuffers(device, &buffers->FrameRenderBuffers[n], allocator); delete[] buffers->FrameRenderBuffers; buffers->FrameRenderBuffers = NULL; buffers->Index = 0; From bd351e9ac57fa0730d9407e8785996a6080f1f35 Mon Sep 17 00:00:00 2001 From: omar Date: Fri, 5 Apr 2019 20:20:08 +0200 Subject: [PATCH 15/19] Misc: Added IM_MALLOC/IM_FREE macros mimicking IM_NEW/IM_DELETE so user doesn't need to revert to using the ImGui::MemAlloc()/MemFree() calls directly. --- docs/CHANGELOG.txt | 2 ++ imgui.cpp | 20 ++++++++++--------- imgui.h | 33 +++++++++++++++++++------------- imgui_draw.cpp | 22 ++++++++++----------- imgui_widgets.cpp | 4 ++-- misc/freetype/imgui_freetype.cpp | 12 ++++++------ misc/freetype/imgui_freetype.h | 2 +- 7 files changed, 53 insertions(+), 42 deletions(-) diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index 1822526f..ec24a20b 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -49,6 +49,8 @@ Other Changes: - GetMouseDragDelta(): also returns the delta on the mouse button released frame. (#2419) - GetMouseDragDelta(): verify that mouse positions are valid otherwise returns zero. - Inputs: Also add support for horizontal scroll with Shift+Mouse Wheel. (#2424, #1463) [@LucaRood] +- Misc: Added IM_MALLOC/IM_FREE macros mimicking IM_NEW/IM_DELETE so user doesn't need to revert + to using the ImGui::MemAlloc()/MemFree() calls directly. - Examples: OpenGL: Added a dummy GL call + comments in ImGui_ImplOpenGL3_Init() to detect uninitialized GL function loaders early, and help users understand what they are missing. (#2421) - Examples: OpenGL3: Minor tweaks + not calling glBindBuffer more than necessary in the render loop. diff --git a/imgui.cpp b/imgui.cpp index c0184541..4cb82e16 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -1307,7 +1307,7 @@ void ImStrncpy(char* dst, const char* src, size_t count) char* ImStrdup(const char* str) { size_t len = strlen(str); - void* buf = ImGui::MemAlloc(len + 1); + void* buf = IM_ALLOC(len + 1); return (char*)memcpy(buf, (const void*)str, len + 1); } @@ -1317,8 +1317,8 @@ char* ImStrdupcpy(char* dst, size_t* p_dst_size, const char* src) size_t src_size = strlen(src) + 1; if (dst_buf_size < src_size) { - ImGui::MemFree(dst); - dst = (char*)ImGui::MemAlloc(src_size); + IM_FREE(dst); + dst = (char*)IM_ALLOC(src_size); if (p_dst_size) *p_dst_size = src_size; } @@ -1527,7 +1527,7 @@ FILE* ImFileOpen(const char* filename, const char* mode) } // Load file content into memory -// Memory allocated with ImGui::MemAlloc(), must be freed by user using ImGui::MemFree() +// Memory allocated with IM_ALLOC(), must be freed by user using IM_FREE() == ImGui::MemFree() void* ImFileLoadToMemory(const char* filename, const char* file_open_mode, size_t* out_file_size, int padding_bytes) { IM_ASSERT(filename && file_open_mode); @@ -1546,7 +1546,7 @@ void* ImFileLoadToMemory(const char* filename, const char* file_open_mode, size_ } size_t file_size = (size_t)file_size_signed; - void* file_data = ImGui::MemAlloc(file_size + padding_bytes); + void* file_data = IM_ALLOC(file_size + padding_bytes); if (file_data == NULL) { fclose(f); @@ -1555,7 +1555,7 @@ void* ImFileLoadToMemory(const char* filename, const char* file_open_mode, size_ if (fread(file_data, 1, file_size, f) != file_size) { fclose(f); - ImGui::MemFree(file_data); + IM_FREE(file_data); return NULL; } if (padding_bytes > 0) @@ -2962,6 +2962,7 @@ float ImGui::CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x) return ImMax(wrap_pos_x - pos.x, 1.0f); } +// IM_ALLOC() == ImGui::MemAlloc() void* ImGui::MemAlloc(size_t size) { if (ImGuiContext* ctx = GImGui) @@ -2969,6 +2970,7 @@ void* ImGui::MemAlloc(size_t size) return GImAllocatorAllocFunc(size, GImAllocatorUserData); } +// IM_FREE() == ImGui::MemFree() void ImGui::MemFree(void* ptr) { if (ptr) @@ -9093,7 +9095,7 @@ void ImGui::LoadIniSettingsFromDisk(const char* ini_filename) if (!file_data) return; LoadIniSettingsFromMemory(file_data, (size_t)file_data_size); - ImGui::MemFree(file_data); + IM_FREE(file_data); } ImGuiSettingsHandler* ImGui::FindSettingsHandler(const char* type_name) @@ -9117,7 +9119,7 @@ void ImGui::LoadIniSettingsFromMemory(const char* ini_data, size_t ini_size) // For our convenience and to make the code simpler, we'll also write zero-terminators within the buffer. So let's create a writable copy.. if (ini_size == 0) ini_size = strlen(ini_data); - char* buf = (char*)ImGui::MemAlloc(ini_size + 1); + char* buf = (char*)IM_ALLOC(ini_size + 1); char* buf_end = buf + ini_size; memcpy(buf, ini_data, ini_size); buf[ini_size] = 0; @@ -9164,7 +9166,7 @@ void ImGui::LoadIniSettingsFromMemory(const char* ini_data, size_t ini_size) entry_handler->ReadLineFn(&g, entry_handler, entry_data, line); } } - ImGui::MemFree(buf); + IM_FREE(buf); g.SettingsLoaded = true; } diff --git a/imgui.h b/imgui.h index 1d907b04..c4400d84 100644 --- a/imgui.h +++ b/imgui.h @@ -13,6 +13,7 @@ Index of this file: // Forward declarations and basic types // ImGui API (Dear ImGui end-user API) // Flags & Enumerations +// Memory allocations macros // ImVector<> // ImGuiStyle // ImGuiIO @@ -1185,6 +1186,22 @@ enum ImGuiCond_ #endif }; +//----------------------------------------------------------------------------- +// Helpers: Memory allocations macros +// IM_MALLOC(), IM_FREE(), IM_NEW(), IM_PLACEMENT_NEW(), IM_DELETE() +// We call C++ constructor on own allocated memory via the placement "new(ptr) Type()" syntax. +// Defining a custom placement new() with a dummy parameter allows us to bypass including which on some platforms complains when user has disabled exceptions. +//----------------------------------------------------------------------------- + +struct ImNewDummy {}; +inline void* operator new(size_t, ImNewDummy, void* ptr) { return ptr; } +inline void operator delete(void*, ImNewDummy, void*) {} // This is only required so we can use the symmetrical new() +#define IM_ALLOC(_SIZE) ImGui::MemAlloc(_SIZE) +#define IM_FREE(_PTR) ImGui::MemFree(_PTR) +#define IM_PLACEMENT_NEW(_PTR) new(ImNewDummy(), _PTR) +#define IM_NEW(_TYPE) new(ImNewDummy(), ImGui::MemAlloc(sizeof(_TYPE))) _TYPE +template void IM_DELETE(T* p) { if (p) { p->~T(); ImGui::MemFree(p); } } + //----------------------------------------------------------------------------- // Helper: ImVector<> // Lightweight std::vector<>-like class to avoid dragging dependencies (also, some implementations of STL with debug enabled are absurdly slow, we bypass it so our code runs fast in debug). @@ -1210,7 +1227,7 @@ struct ImVector inline ImVector() { Size = Capacity = 0; Data = NULL; } inline ImVector(const ImVector& src) { Size = Capacity = 0; Data = NULL; operator=(src); } inline ImVector& operator=(const ImVector& src) { clear(); resize(src.Size); memcpy(Data, src.Data, (size_t)Size * sizeof(T)); return *this; } - inline ~ImVector() { if (Data) ImGui::MemFree(Data); } + inline ~ImVector() { if (Data) IM_FREE(Data); } inline bool empty() const { return Size == 0; } inline int size() const { return Size; } @@ -1219,7 +1236,7 @@ struct ImVector inline T& operator[](int i) { IM_ASSERT(i < Size); return Data[i]; } inline const T& operator[](int i) const { IM_ASSERT(i < Size); return Data[i]; } - inline void clear() { if (Data) { Size = Capacity = 0; ImGui::MemFree(Data); Data = NULL; } } + inline void clear() { if (Data) { Size = Capacity = 0; IM_FREE(Data); Data = NULL; } } inline T* begin() { return Data; } inline const T* begin() const { return Data; } inline T* end() { return Data + Size; } @@ -1233,7 +1250,7 @@ struct ImVector inline int _grow_capacity(int sz) const { int new_capacity = Capacity ? (Capacity + Capacity/2) : 8; return new_capacity > sz ? new_capacity : sz; } inline void resize(int new_size) { if (new_size > Capacity) reserve(_grow_capacity(new_size)); Size = new_size; } inline void resize(int new_size, const T& v) { if (new_size > Capacity) reserve(_grow_capacity(new_size)); if (new_size > Size) for (int n = Size; n < new_size; n++) memcpy(&Data[n], &v, sizeof(v)); Size = new_size; } - inline void reserve(int new_capacity) { if (new_capacity <= Capacity) return; T* new_data = (T*)ImGui::MemAlloc((size_t)new_capacity * sizeof(T)); if (Data) { memcpy(new_data, Data, (size_t)Size * sizeof(T)); ImGui::MemFree(Data); } Data = new_data; Capacity = new_capacity; } + inline void reserve(int new_capacity) { if (new_capacity <= Capacity) return; T* new_data = (T*)IM_ALLOC((size_t)new_capacity * sizeof(T)); if (Data) { memcpy(new_data, Data, (size_t)Size * sizeof(T)); IM_FREE(Data); } Data = new_data; Capacity = new_capacity; } // NB: It is illegal to call push_back/push_front/insert with a reference pointing inside the ImVector data itself! e.g. v.push_back(v[10]) is forbidden. inline void push_back(const T& v) { if (Size == Capacity) reserve(_grow_capacity(Size + 1)); memcpy(&Data[Size], &v, sizeof(v)); Size++; } @@ -1548,16 +1565,6 @@ typedef ImGuiInputTextCallbackData ImGuiTextEditCallbackData; // Helpers //----------------------------------------------------------------------------- -// Helper: IM_NEW(), IM_PLACEMENT_NEW(), IM_DELETE() macros to call MemAlloc + Placement New, Placement Delete + MemFree -// We call C++ constructor on own allocated memory via the placement "new(ptr) Type()" syntax. -// Defining a custom placement new() with a dummy parameter allows us to bypass including which on some platforms complains when user has disabled exceptions. -struct ImNewDummy {}; -inline void* operator new(size_t, ImNewDummy, void* ptr) { return ptr; } -inline void operator delete(void*, ImNewDummy, void*) {} // This is only required so we can use the symmetrical new() -#define IM_PLACEMENT_NEW(_PTR) new(ImNewDummy(), _PTR) -#define IM_NEW(_TYPE) new(ImNewDummy(), ImGui::MemAlloc(sizeof(_TYPE))) _TYPE -template void IM_DELETE(T* p) { if (p) { p->~T(); ImGui::MemFree(p); } } - // Helper: Execute a block of code at maximum once a frame. Convenient if you want to quickly create an UI within deep-nested code that runs multiple times every frame. // Usage: static ImGuiOnceUponAFrame oaf; if (oaf) ImGui::Text("This will be called only once per frame"); struct ImGuiOnceUponAFrame diff --git a/imgui_draw.cpp b/imgui_draw.cpp index 97f25bd4..290fadb8 100644 --- a/imgui_draw.cpp +++ b/imgui_draw.cpp @@ -130,8 +130,8 @@ namespace IMGUI_STB_NAMESPACE #ifndef STB_TRUETYPE_IMPLEMENTATION // in case the user already have an implementation in the _same_ compilation unit (e.g. unity builds) #ifndef IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION -#define STBTT_malloc(x,u) ((void)(u), ImGui::MemAlloc(x)) -#define STBTT_free(x,u) ((void)(u), ImGui::MemFree(x)) +#define STBTT_malloc(x,u) ((void)(u), IM_ALLOC(x)) +#define STBTT_free(x,u) ((void)(u), IM_FREE(x)) #define STBTT_assert(x) IM_ASSERT(x) #define STBTT_fmod(x,y) ImFmod(x,y) #define STBTT_sqrt(x) ImSqrt(x) @@ -1459,7 +1459,7 @@ void ImFontAtlas::ClearInputData() for (int i = 0; i < ConfigData.Size; i++) if (ConfigData[i].FontData && ConfigData[i].FontDataOwnedByAtlas) { - ImGui::MemFree(ConfigData[i].FontData); + IM_FREE(ConfigData[i].FontData); ConfigData[i].FontData = NULL; } @@ -1480,9 +1480,9 @@ void ImFontAtlas::ClearTexData() { IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); if (TexPixelsAlpha8) - ImGui::MemFree(TexPixelsAlpha8); + IM_FREE(TexPixelsAlpha8); if (TexPixelsRGBA32) - ImGui::MemFree(TexPixelsRGBA32); + IM_FREE(TexPixelsRGBA32); TexPixelsAlpha8 = NULL; TexPixelsRGBA32 = NULL; } @@ -1528,7 +1528,7 @@ void ImFontAtlas::GetTexDataAsRGBA32(unsigned char** out_pixels, int* out_wid GetTexDataAsAlpha8(&pixels, NULL, NULL); if (pixels) { - TexPixelsRGBA32 = (unsigned int*)ImGui::MemAlloc((size_t)TexWidth * (size_t)TexHeight * 4); + TexPixelsRGBA32 = (unsigned int*)IM_ALLOC((size_t)TexWidth * (size_t)TexHeight * 4); const unsigned char* src = pixels; unsigned int* dst = TexPixelsRGBA32; for (int n = TexWidth * TexHeight; n > 0; n--) @@ -1560,7 +1560,7 @@ ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg) new_font_cfg.DstFont = Fonts.back(); if (!new_font_cfg.FontDataOwnedByAtlas) { - new_font_cfg.FontData = ImGui::MemAlloc(new_font_cfg.FontDataSize); + new_font_cfg.FontData = IM_ALLOC(new_font_cfg.FontDataSize); new_font_cfg.FontDataOwnedByAtlas = true; memcpy(new_font_cfg.FontData, font_cfg->FontData, (size_t)new_font_cfg.FontDataSize); } @@ -1645,7 +1645,7 @@ ImFont* ImFontAtlas::AddFontFromMemoryTTF(void* ttf_data, int ttf_size, float si ImFont* ImFontAtlas::AddFontFromMemoryCompressedTTF(const void* compressed_ttf_data, int compressed_ttf_size, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges) { const unsigned int buf_decompressed_size = stb_decompress_length((const unsigned char*)compressed_ttf_data); - unsigned char* buf_decompressed_data = (unsigned char *)ImGui::MemAlloc(buf_decompressed_size); + unsigned char* buf_decompressed_data = (unsigned char *)IM_ALLOC(buf_decompressed_size); stb_decompress(buf_decompressed_data, (const unsigned char*)compressed_ttf_data, (unsigned int)compressed_ttf_size); ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig(); @@ -1657,10 +1657,10 @@ ImFont* ImFontAtlas::AddFontFromMemoryCompressedTTF(const void* compressed_ttf_d ImFont* ImFontAtlas::AddFontFromMemoryCompressedBase85TTF(const char* compressed_ttf_data_base85, float size_pixels, const ImFontConfig* font_cfg, const ImWchar* glyph_ranges) { int compressed_ttf_size = (((int)strlen(compressed_ttf_data_base85) + 4) / 5) * 4; - void* compressed_ttf = ImGui::MemAlloc((size_t)compressed_ttf_size); + void* compressed_ttf = IM_ALLOC((size_t)compressed_ttf_size); Decode85((const unsigned char*)compressed_ttf_data_base85, (unsigned char*)compressed_ttf); ImFont* font = AddFontFromMemoryCompressedTTF(compressed_ttf, compressed_ttf_size, size_pixels, font_cfg, glyph_ranges); - ImGui::MemFree(compressed_ttf); + IM_FREE(compressed_ttf); return font; } @@ -1960,7 +1960,7 @@ bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) // 7. Allocate texture atlas->TexHeight = (atlas->Flags & ImFontAtlasFlags_NoPowerOfTwoHeight) ? (atlas->TexHeight + 1) : ImUpperPowerOfTwo(atlas->TexHeight); atlas->TexUvScale = ImVec2(1.0f / atlas->TexWidth, 1.0f / atlas->TexHeight); - atlas->TexPixelsAlpha8 = (unsigned char*)ImGui::MemAlloc(atlas->TexWidth * atlas->TexHeight); + atlas->TexPixelsAlpha8 = (unsigned char*)IM_ALLOC(atlas->TexWidth * atlas->TexHeight); memset(atlas->TexPixelsAlpha8, 0, atlas->TexWidth * atlas->TexHeight); spc.pixels = atlas->TexPixelsAlpha8; spc.height = atlas->TexHeight; diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp index 4ebbc1f5..d6d5ac84 100644 --- a/imgui_widgets.cpp +++ b/imgui_widgets.cpp @@ -3540,7 +3540,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ const int ib = state->HasSelection() ? ImMin(state->Stb.select_start, state->Stb.select_end) : 0; const int ie = state->HasSelection() ? ImMax(state->Stb.select_start, state->Stb.select_end) : state->CurLenW; const int clipboard_data_len = ImTextCountUtf8BytesFromStr(state->TextW.Data + ib, state->TextW.Data + ie) + 1; - char* clipboard_data = (char*)MemAlloc(clipboard_data_len * sizeof(char)); + char* clipboard_data = (char*)IM_ALLOC(clipboard_data_len * sizeof(char)); ImTextStrToUtf8(clipboard_data, clipboard_data_len, state->TextW.Data + ib, state->TextW.Data + ie); SetClipboardText(clipboard_data); MemFree(clipboard_data); @@ -3559,7 +3559,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ { // Filter pasted buffer const int clipboard_len = (int)strlen(clipboard); - ImWchar* clipboard_filtered = (ImWchar*)MemAlloc((clipboard_len+1) * sizeof(ImWchar)); + ImWchar* clipboard_filtered = (ImWchar*)IM_ALLOC((clipboard_len+1) * sizeof(ImWchar)); int clipboard_filtered_len = 0; for (const char* s = clipboard; *s; ) { diff --git a/misc/freetype/imgui_freetype.cpp b/misc/freetype/imgui_freetype.cpp index 012eae75..4a579ed2 100644 --- a/misc/freetype/imgui_freetype.cpp +++ b/misc/freetype/imgui_freetype.cpp @@ -398,7 +398,7 @@ bool ImFontAtlasBuildWithFreeType(FT_Library ft_library, ImFontAtlas* atlas, uns const int BITMAP_BUFFERS_CHUNK_SIZE = 256 * 1024; int buf_bitmap_current_used_bytes = 0; ImVector buf_bitmap_buffers; - buf_bitmap_buffers.push_back((unsigned char*)ImGui::MemAlloc(BITMAP_BUFFERS_CHUNK_SIZE)); + buf_bitmap_buffers.push_back((unsigned char*)IM_ALLOC(BITMAP_BUFFERS_CHUNK_SIZE)); // 4. Gather glyphs sizes so we can pack them in our virtual canvas. // 8. Render/rasterize font characters into the texture @@ -440,7 +440,7 @@ bool ImFontAtlasBuildWithFreeType(FT_Library ft_library, ImFontAtlas* atlas, uns if (buf_bitmap_current_used_bytes + bitmap_size_in_bytes > BITMAP_BUFFERS_CHUNK_SIZE) { buf_bitmap_current_used_bytes = 0; - buf_bitmap_buffers.push_back((unsigned char*)ImGui::MemAlloc(BITMAP_BUFFERS_CHUNK_SIZE)); + buf_bitmap_buffers.push_back((unsigned char*)IM_ALLOC(BITMAP_BUFFERS_CHUNK_SIZE)); } // Blit rasterized pixels to our temporary buffer and keep a pointer to it. @@ -493,7 +493,7 @@ bool ImFontAtlasBuildWithFreeType(FT_Library ft_library, ImFontAtlas* atlas, uns // 7. Allocate texture atlas->TexHeight = (atlas->Flags & ImFontAtlasFlags_NoPowerOfTwoHeight) ? (atlas->TexHeight + 1) : ImUpperPowerOfTwo(atlas->TexHeight); atlas->TexUvScale = ImVec2(1.0f / atlas->TexWidth, 1.0f / atlas->TexHeight); - atlas->TexPixelsAlpha8 = (unsigned char*)ImGui::MemAlloc(atlas->TexWidth * atlas->TexHeight); + atlas->TexPixelsAlpha8 = (unsigned char*)IM_ALLOC(atlas->TexWidth * atlas->TexHeight); memset(atlas->TexPixelsAlpha8, 0, atlas->TexWidth * atlas->TexHeight); // 8. Copy rasterized font characters back into the main texture @@ -557,7 +557,7 @@ bool ImFontAtlasBuildWithFreeType(FT_Library ft_library, ImFontAtlas* atlas, uns // Cleanup for (int buf_i = 0; buf_i < buf_bitmap_buffers.Size; buf_i++) - ImGui::MemFree(buf_bitmap_buffers[buf_i]); + IM_FREE(buf_bitmap_buffers[buf_i]); for (int src_i = 0; src_i < src_tmp_array.Size; src_i++) src_tmp_array[src_i].~ImFontBuildSrcDataFT(); @@ -567,8 +567,8 @@ bool ImFontAtlasBuildWithFreeType(FT_Library ft_library, ImFontAtlas* atlas, uns } // Default memory allocators -static void* ImFreeTypeDefaultAllocFunc(size_t size, void* user_data) { IM_UNUSED(user_data); return ImGui::MemAlloc(size); } -static void ImFreeTypeDefaultFreeFunc(void* ptr, void* user_data) { IM_UNUSED(user_data); ImGui::MemFree(ptr); } +static void* ImFreeTypeDefaultAllocFunc(size_t size, void* user_data) { IM_UNUSED(user_data); return IM_ALLOC(size); } +static void ImFreeTypeDefaultFreeFunc(void* ptr, void* user_data) { IM_UNUSED(user_data); IM_FREE(ptr); } // Current memory allocators static void* (*GImFreeTypeAllocFunc)(size_t size, void* user_data) = ImFreeTypeDefaultAllocFunc; diff --git a/misc/freetype/imgui_freetype.h b/misc/freetype/imgui_freetype.h index 9df5780e..b4b0fd66 100644 --- a/misc/freetype/imgui_freetype.h +++ b/misc/freetype/imgui_freetype.h @@ -29,7 +29,7 @@ namespace ImGuiFreeType IMGUI_API bool BuildFontAtlas(ImFontAtlas* atlas, unsigned int extra_flags = 0); - // By default ImGuiFreeType will use ImGui::MemAlloc()/MemFree(). + // By default ImGuiFreeType will use IM_ALLOC()/IM_FREE(). // However, as FreeType does lots of allocations we provide a way for the user to redirect it to a separate memory heap if desired: IMGUI_API void SetAllocatorFunctions(void* (*alloc_func)(size_t sz, void* user_data), void (*free_func)(void* ptr, void* user_data), void* user_data = NULL); } From c8fd4afd753f64d4bd24b3f4fe21a64afaec53a1 Mon Sep 17 00:00:00 2001 From: omar Date: Fri, 5 Apr 2019 20:20:08 +0200 Subject: [PATCH 16/19] Misc: Added IM_MALLOC/IM_FREE macros mimicking IM_NEW/IM_DELETE so user doesn't need to revert to using the ImGui::MemAlloc()/MemFree() calls directly. --- docs/CHANGELOG.txt | 2 ++ imgui.cpp | 20 ++++++++++--------- imgui.h | 33 +++++++++++++++++++------------- imgui_draw.cpp | 22 ++++++++++----------- imgui_widgets.cpp | 4 ++-- misc/freetype/imgui_freetype.cpp | 12 ++++++------ misc/freetype/imgui_freetype.h | 2 +- 7 files changed, 53 insertions(+), 42 deletions(-) diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index a4cbd4de..d753375e 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -44,6 +44,8 @@ Other Changes: - GetMouseDragDelta(): also returns the delta on the mouse button released frame. (#2419) - GetMouseDragDelta(): verify that mouse positions are valid otherwise returns zero. - Inputs: Also add support for horizontal scroll with Shift+Mouse Wheel. (#2424, #1463) [@LucaRood] +- Misc: Added IM_MALLOC/IM_FREE macros mimicking IM_NEW/IM_DELETE so user doesn't need to revert + to using the ImGui::MemAlloc()/MemFree() calls directly. - Examples: OpenGL: Added a dummy GL call + comments in ImGui_ImplOpenGL3_Init() to detect uninitialized GL function loaders early, and help users understand what they are missing. (#2421) - Examples: OpenGL3: Minor tweaks + not calling glBindBuffer more than necessary in the render loop. diff --git a/imgui.cpp b/imgui.cpp index c0184541..4cb82e16 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -1307,7 +1307,7 @@ void ImStrncpy(char* dst, const char* src, size_t count) char* ImStrdup(const char* str) { size_t len = strlen(str); - void* buf = ImGui::MemAlloc(len + 1); + void* buf = IM_ALLOC(len + 1); return (char*)memcpy(buf, (const void*)str, len + 1); } @@ -1317,8 +1317,8 @@ char* ImStrdupcpy(char* dst, size_t* p_dst_size, const char* src) size_t src_size = strlen(src) + 1; if (dst_buf_size < src_size) { - ImGui::MemFree(dst); - dst = (char*)ImGui::MemAlloc(src_size); + IM_FREE(dst); + dst = (char*)IM_ALLOC(src_size); if (p_dst_size) *p_dst_size = src_size; } @@ -1527,7 +1527,7 @@ FILE* ImFileOpen(const char* filename, const char* mode) } // Load file content into memory -// Memory allocated with ImGui::MemAlloc(), must be freed by user using ImGui::MemFree() +// Memory allocated with IM_ALLOC(), must be freed by user using IM_FREE() == ImGui::MemFree() void* ImFileLoadToMemory(const char* filename, const char* file_open_mode, size_t* out_file_size, int padding_bytes) { IM_ASSERT(filename && file_open_mode); @@ -1546,7 +1546,7 @@ void* ImFileLoadToMemory(const char* filename, const char* file_open_mode, size_ } size_t file_size = (size_t)file_size_signed; - void* file_data = ImGui::MemAlloc(file_size + padding_bytes); + void* file_data = IM_ALLOC(file_size + padding_bytes); if (file_data == NULL) { fclose(f); @@ -1555,7 +1555,7 @@ void* ImFileLoadToMemory(const char* filename, const char* file_open_mode, size_ if (fread(file_data, 1, file_size, f) != file_size) { fclose(f); - ImGui::MemFree(file_data); + IM_FREE(file_data); return NULL; } if (padding_bytes > 0) @@ -2962,6 +2962,7 @@ float ImGui::CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x) return ImMax(wrap_pos_x - pos.x, 1.0f); } +// IM_ALLOC() == ImGui::MemAlloc() void* ImGui::MemAlloc(size_t size) { if (ImGuiContext* ctx = GImGui) @@ -2969,6 +2970,7 @@ void* ImGui::MemAlloc(size_t size) return GImAllocatorAllocFunc(size, GImAllocatorUserData); } +// IM_FREE() == ImGui::MemFree() void ImGui::MemFree(void* ptr) { if (ptr) @@ -9093,7 +9095,7 @@ void ImGui::LoadIniSettingsFromDisk(const char* ini_filename) if (!file_data) return; LoadIniSettingsFromMemory(file_data, (size_t)file_data_size); - ImGui::MemFree(file_data); + IM_FREE(file_data); } ImGuiSettingsHandler* ImGui::FindSettingsHandler(const char* type_name) @@ -9117,7 +9119,7 @@ void ImGui::LoadIniSettingsFromMemory(const char* ini_data, size_t ini_size) // For our convenience and to make the code simpler, we'll also write zero-terminators within the buffer. So let's create a writable copy.. if (ini_size == 0) ini_size = strlen(ini_data); - char* buf = (char*)ImGui::MemAlloc(ini_size + 1); + char* buf = (char*)IM_ALLOC(ini_size + 1); char* buf_end = buf + ini_size; memcpy(buf, ini_data, ini_size); buf[ini_size] = 0; @@ -9164,7 +9166,7 @@ void ImGui::LoadIniSettingsFromMemory(const char* ini_data, size_t ini_size) entry_handler->ReadLineFn(&g, entry_handler, entry_data, line); } } - ImGui::MemFree(buf); + IM_FREE(buf); g.SettingsLoaded = true; } diff --git a/imgui.h b/imgui.h index 1d907b04..c4400d84 100644 --- a/imgui.h +++ b/imgui.h @@ -13,6 +13,7 @@ Index of this file: // Forward declarations and basic types // ImGui API (Dear ImGui end-user API) // Flags & Enumerations +// Memory allocations macros // ImVector<> // ImGuiStyle // ImGuiIO @@ -1185,6 +1186,22 @@ enum ImGuiCond_ #endif }; +//----------------------------------------------------------------------------- +// Helpers: Memory allocations macros +// IM_MALLOC(), IM_FREE(), IM_NEW(), IM_PLACEMENT_NEW(), IM_DELETE() +// We call C++ constructor on own allocated memory via the placement "new(ptr) Type()" syntax. +// Defining a custom placement new() with a dummy parameter allows us to bypass including which on some platforms complains when user has disabled exceptions. +//----------------------------------------------------------------------------- + +struct ImNewDummy {}; +inline void* operator new(size_t, ImNewDummy, void* ptr) { return ptr; } +inline void operator delete(void*, ImNewDummy, void*) {} // This is only required so we can use the symmetrical new() +#define IM_ALLOC(_SIZE) ImGui::MemAlloc(_SIZE) +#define IM_FREE(_PTR) ImGui::MemFree(_PTR) +#define IM_PLACEMENT_NEW(_PTR) new(ImNewDummy(), _PTR) +#define IM_NEW(_TYPE) new(ImNewDummy(), ImGui::MemAlloc(sizeof(_TYPE))) _TYPE +template void IM_DELETE(T* p) { if (p) { p->~T(); ImGui::MemFree(p); } } + //----------------------------------------------------------------------------- // Helper: ImVector<> // Lightweight std::vector<>-like class to avoid dragging dependencies (also, some implementations of STL with debug enabled are absurdly slow, we bypass it so our code runs fast in debug). @@ -1210,7 +1227,7 @@ struct ImVector inline ImVector() { Size = Capacity = 0; Data = NULL; } inline ImVector(const ImVector& src) { Size = Capacity = 0; Data = NULL; operator=(src); } inline ImVector& operator=(const ImVector& src) { clear(); resize(src.Size); memcpy(Data, src.Data, (size_t)Size * sizeof(T)); return *this; } - inline ~ImVector() { if (Data) ImGui::MemFree(Data); } + inline ~ImVector() { if (Data) IM_FREE(Data); } inline bool empty() const { return Size == 0; } inline int size() const { return Size; } @@ -1219,7 +1236,7 @@ struct ImVector inline T& operator[](int i) { IM_ASSERT(i < Size); return Data[i]; } inline const T& operator[](int i) const { IM_ASSERT(i < Size); return Data[i]; } - inline void clear() { if (Data) { Size = Capacity = 0; ImGui::MemFree(Data); Data = NULL; } } + inline void clear() { if (Data) { Size = Capacity = 0; IM_FREE(Data); Data = NULL; } } inline T* begin() { return Data; } inline const T* begin() const { return Data; } inline T* end() { return Data + Size; } @@ -1233,7 +1250,7 @@ struct ImVector inline int _grow_capacity(int sz) const { int new_capacity = Capacity ? (Capacity + Capacity/2) : 8; return new_capacity > sz ? new_capacity : sz; } inline void resize(int new_size) { if (new_size > Capacity) reserve(_grow_capacity(new_size)); Size = new_size; } inline void resize(int new_size, const T& v) { if (new_size > Capacity) reserve(_grow_capacity(new_size)); if (new_size > Size) for (int n = Size; n < new_size; n++) memcpy(&Data[n], &v, sizeof(v)); Size = new_size; } - inline void reserve(int new_capacity) { if (new_capacity <= Capacity) return; T* new_data = (T*)ImGui::MemAlloc((size_t)new_capacity * sizeof(T)); if (Data) { memcpy(new_data, Data, (size_t)Size * sizeof(T)); ImGui::MemFree(Data); } Data = new_data; Capacity = new_capacity; } + inline void reserve(int new_capacity) { if (new_capacity <= Capacity) return; T* new_data = (T*)IM_ALLOC((size_t)new_capacity * sizeof(T)); if (Data) { memcpy(new_data, Data, (size_t)Size * sizeof(T)); IM_FREE(Data); } Data = new_data; Capacity = new_capacity; } // NB: It is illegal to call push_back/push_front/insert with a reference pointing inside the ImVector data itself! e.g. v.push_back(v[10]) is forbidden. inline void push_back(const T& v) { if (Size == Capacity) reserve(_grow_capacity(Size + 1)); memcpy(&Data[Size], &v, sizeof(v)); Size++; } @@ -1548,16 +1565,6 @@ typedef ImGuiInputTextCallbackData ImGuiTextEditCallbackData; // Helpers //----------------------------------------------------------------------------- -// Helper: IM_NEW(), IM_PLACEMENT_NEW(), IM_DELETE() macros to call MemAlloc + Placement New, Placement Delete + MemFree -// We call C++ constructor on own allocated memory via the placement "new(ptr) Type()" syntax. -// Defining a custom placement new() with a dummy parameter allows us to bypass including which on some platforms complains when user has disabled exceptions. -struct ImNewDummy {}; -inline void* operator new(size_t, ImNewDummy, void* ptr) { return ptr; } -inline void operator delete(void*, ImNewDummy, void*) {} // This is only required so we can use the symmetrical new() -#define IM_PLACEMENT_NEW(_PTR) new(ImNewDummy(), _PTR) -#define IM_NEW(_TYPE) new(ImNewDummy(), ImGui::MemAlloc(sizeof(_TYPE))) _TYPE -template void IM_DELETE(T* p) { if (p) { p->~T(); ImGui::MemFree(p); } } - // Helper: Execute a block of code at maximum once a frame. Convenient if you want to quickly create an UI within deep-nested code that runs multiple times every frame. // Usage: static ImGuiOnceUponAFrame oaf; if (oaf) ImGui::Text("This will be called only once per frame"); struct ImGuiOnceUponAFrame diff --git a/imgui_draw.cpp b/imgui_draw.cpp index 97f25bd4..290fadb8 100644 --- a/imgui_draw.cpp +++ b/imgui_draw.cpp @@ -130,8 +130,8 @@ namespace IMGUI_STB_NAMESPACE #ifndef STB_TRUETYPE_IMPLEMENTATION // in case the user already have an implementation in the _same_ compilation unit (e.g. unity builds) #ifndef IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION -#define STBTT_malloc(x,u) ((void)(u), ImGui::MemAlloc(x)) -#define STBTT_free(x,u) ((void)(u), ImGui::MemFree(x)) +#define STBTT_malloc(x,u) ((void)(u), IM_ALLOC(x)) +#define STBTT_free(x,u) ((void)(u), IM_FREE(x)) #define STBTT_assert(x) IM_ASSERT(x) #define STBTT_fmod(x,y) ImFmod(x,y) #define STBTT_sqrt(x) ImSqrt(x) @@ -1459,7 +1459,7 @@ void ImFontAtlas::ClearInputData() for (int i = 0; i < ConfigData.Size; i++) if (ConfigData[i].FontData && ConfigData[i].FontDataOwnedByAtlas) { - ImGui::MemFree(ConfigData[i].FontData); + IM_FREE(ConfigData[i].FontData); ConfigData[i].FontData = NULL; } @@ -1480,9 +1480,9 @@ void ImFontAtlas::ClearTexData() { IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); if (TexPixelsAlpha8) - ImGui::MemFree(TexPixelsAlpha8); + IM_FREE(TexPixelsAlpha8); if (TexPixelsRGBA32) - ImGui::MemFree(TexPixelsRGBA32); + IM_FREE(TexPixelsRGBA32); TexPixelsAlpha8 = NULL; TexPixelsRGBA32 = NULL; } @@ -1528,7 +1528,7 @@ void ImFontAtlas::GetTexDataAsRGBA32(unsigned char** out_pixels, int* out_wid GetTexDataAsAlpha8(&pixels, NULL, NULL); if (pixels) { - TexPixelsRGBA32 = (unsigned int*)ImGui::MemAlloc((size_t)TexWidth * (size_t)TexHeight * 4); + TexPixelsRGBA32 = (unsigned int*)IM_ALLOC((size_t)TexWidth * (size_t)TexHeight * 4); const unsigned char* src = pixels; unsigned int* dst = TexPixelsRGBA32; for (int n = TexWidth * TexHeight; n > 0; n--) @@ -1560,7 +1560,7 @@ ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg) new_font_cfg.DstFont = Fonts.back(); if (!new_font_cfg.FontDataOwnedByAtlas) { - new_font_cfg.FontData = ImGui::MemAlloc(new_font_cfg.FontDataSize); + new_font_cfg.FontData = IM_ALLOC(new_font_cfg.FontDataSize); new_font_cfg.FontDataOwnedByAtlas = true; memcpy(new_font_cfg.FontData, font_cfg->FontData, (size_t)new_font_cfg.FontDataSize); } @@ -1645,7 +1645,7 @@ ImFont* ImFontAtlas::AddFontFromMemoryTTF(void* ttf_data, int ttf_size, float si ImFont* ImFontAtlas::AddFontFromMemoryCompressedTTF(const void* compressed_ttf_data, int compressed_ttf_size, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges) { const unsigned int buf_decompressed_size = stb_decompress_length((const unsigned char*)compressed_ttf_data); - unsigned char* buf_decompressed_data = (unsigned char *)ImGui::MemAlloc(buf_decompressed_size); + unsigned char* buf_decompressed_data = (unsigned char *)IM_ALLOC(buf_decompressed_size); stb_decompress(buf_decompressed_data, (const unsigned char*)compressed_ttf_data, (unsigned int)compressed_ttf_size); ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig(); @@ -1657,10 +1657,10 @@ ImFont* ImFontAtlas::AddFontFromMemoryCompressedTTF(const void* compressed_ttf_d ImFont* ImFontAtlas::AddFontFromMemoryCompressedBase85TTF(const char* compressed_ttf_data_base85, float size_pixels, const ImFontConfig* font_cfg, const ImWchar* glyph_ranges) { int compressed_ttf_size = (((int)strlen(compressed_ttf_data_base85) + 4) / 5) * 4; - void* compressed_ttf = ImGui::MemAlloc((size_t)compressed_ttf_size); + void* compressed_ttf = IM_ALLOC((size_t)compressed_ttf_size); Decode85((const unsigned char*)compressed_ttf_data_base85, (unsigned char*)compressed_ttf); ImFont* font = AddFontFromMemoryCompressedTTF(compressed_ttf, compressed_ttf_size, size_pixels, font_cfg, glyph_ranges); - ImGui::MemFree(compressed_ttf); + IM_FREE(compressed_ttf); return font; } @@ -1960,7 +1960,7 @@ bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) // 7. Allocate texture atlas->TexHeight = (atlas->Flags & ImFontAtlasFlags_NoPowerOfTwoHeight) ? (atlas->TexHeight + 1) : ImUpperPowerOfTwo(atlas->TexHeight); atlas->TexUvScale = ImVec2(1.0f / atlas->TexWidth, 1.0f / atlas->TexHeight); - atlas->TexPixelsAlpha8 = (unsigned char*)ImGui::MemAlloc(atlas->TexWidth * atlas->TexHeight); + atlas->TexPixelsAlpha8 = (unsigned char*)IM_ALLOC(atlas->TexWidth * atlas->TexHeight); memset(atlas->TexPixelsAlpha8, 0, atlas->TexWidth * atlas->TexHeight); spc.pixels = atlas->TexPixelsAlpha8; spc.height = atlas->TexHeight; diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp index 4ebbc1f5..d6d5ac84 100644 --- a/imgui_widgets.cpp +++ b/imgui_widgets.cpp @@ -3540,7 +3540,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ const int ib = state->HasSelection() ? ImMin(state->Stb.select_start, state->Stb.select_end) : 0; const int ie = state->HasSelection() ? ImMax(state->Stb.select_start, state->Stb.select_end) : state->CurLenW; const int clipboard_data_len = ImTextCountUtf8BytesFromStr(state->TextW.Data + ib, state->TextW.Data + ie) + 1; - char* clipboard_data = (char*)MemAlloc(clipboard_data_len * sizeof(char)); + char* clipboard_data = (char*)IM_ALLOC(clipboard_data_len * sizeof(char)); ImTextStrToUtf8(clipboard_data, clipboard_data_len, state->TextW.Data + ib, state->TextW.Data + ie); SetClipboardText(clipboard_data); MemFree(clipboard_data); @@ -3559,7 +3559,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ { // Filter pasted buffer const int clipboard_len = (int)strlen(clipboard); - ImWchar* clipboard_filtered = (ImWchar*)MemAlloc((clipboard_len+1) * sizeof(ImWchar)); + ImWchar* clipboard_filtered = (ImWchar*)IM_ALLOC((clipboard_len+1) * sizeof(ImWchar)); int clipboard_filtered_len = 0; for (const char* s = clipboard; *s; ) { diff --git a/misc/freetype/imgui_freetype.cpp b/misc/freetype/imgui_freetype.cpp index 012eae75..4a579ed2 100644 --- a/misc/freetype/imgui_freetype.cpp +++ b/misc/freetype/imgui_freetype.cpp @@ -398,7 +398,7 @@ bool ImFontAtlasBuildWithFreeType(FT_Library ft_library, ImFontAtlas* atlas, uns const int BITMAP_BUFFERS_CHUNK_SIZE = 256 * 1024; int buf_bitmap_current_used_bytes = 0; ImVector buf_bitmap_buffers; - buf_bitmap_buffers.push_back((unsigned char*)ImGui::MemAlloc(BITMAP_BUFFERS_CHUNK_SIZE)); + buf_bitmap_buffers.push_back((unsigned char*)IM_ALLOC(BITMAP_BUFFERS_CHUNK_SIZE)); // 4. Gather glyphs sizes so we can pack them in our virtual canvas. // 8. Render/rasterize font characters into the texture @@ -440,7 +440,7 @@ bool ImFontAtlasBuildWithFreeType(FT_Library ft_library, ImFontAtlas* atlas, uns if (buf_bitmap_current_used_bytes + bitmap_size_in_bytes > BITMAP_BUFFERS_CHUNK_SIZE) { buf_bitmap_current_used_bytes = 0; - buf_bitmap_buffers.push_back((unsigned char*)ImGui::MemAlloc(BITMAP_BUFFERS_CHUNK_SIZE)); + buf_bitmap_buffers.push_back((unsigned char*)IM_ALLOC(BITMAP_BUFFERS_CHUNK_SIZE)); } // Blit rasterized pixels to our temporary buffer and keep a pointer to it. @@ -493,7 +493,7 @@ bool ImFontAtlasBuildWithFreeType(FT_Library ft_library, ImFontAtlas* atlas, uns // 7. Allocate texture atlas->TexHeight = (atlas->Flags & ImFontAtlasFlags_NoPowerOfTwoHeight) ? (atlas->TexHeight + 1) : ImUpperPowerOfTwo(atlas->TexHeight); atlas->TexUvScale = ImVec2(1.0f / atlas->TexWidth, 1.0f / atlas->TexHeight); - atlas->TexPixelsAlpha8 = (unsigned char*)ImGui::MemAlloc(atlas->TexWidth * atlas->TexHeight); + atlas->TexPixelsAlpha8 = (unsigned char*)IM_ALLOC(atlas->TexWidth * atlas->TexHeight); memset(atlas->TexPixelsAlpha8, 0, atlas->TexWidth * atlas->TexHeight); // 8. Copy rasterized font characters back into the main texture @@ -557,7 +557,7 @@ bool ImFontAtlasBuildWithFreeType(FT_Library ft_library, ImFontAtlas* atlas, uns // Cleanup for (int buf_i = 0; buf_i < buf_bitmap_buffers.Size; buf_i++) - ImGui::MemFree(buf_bitmap_buffers[buf_i]); + IM_FREE(buf_bitmap_buffers[buf_i]); for (int src_i = 0; src_i < src_tmp_array.Size; src_i++) src_tmp_array[src_i].~ImFontBuildSrcDataFT(); @@ -567,8 +567,8 @@ bool ImFontAtlasBuildWithFreeType(FT_Library ft_library, ImFontAtlas* atlas, uns } // Default memory allocators -static void* ImFreeTypeDefaultAllocFunc(size_t size, void* user_data) { IM_UNUSED(user_data); return ImGui::MemAlloc(size); } -static void ImFreeTypeDefaultFreeFunc(void* ptr, void* user_data) { IM_UNUSED(user_data); ImGui::MemFree(ptr); } +static void* ImFreeTypeDefaultAllocFunc(size_t size, void* user_data) { IM_UNUSED(user_data); return IM_ALLOC(size); } +static void ImFreeTypeDefaultFreeFunc(void* ptr, void* user_data) { IM_UNUSED(user_data); IM_FREE(ptr); } // Current memory allocators static void* (*GImFreeTypeAllocFunc)(size_t size, void* user_data) = ImFreeTypeDefaultAllocFunc; diff --git a/misc/freetype/imgui_freetype.h b/misc/freetype/imgui_freetype.h index 9df5780e..b4b0fd66 100644 --- a/misc/freetype/imgui_freetype.h +++ b/misc/freetype/imgui_freetype.h @@ -29,7 +29,7 @@ namespace ImGuiFreeType IMGUI_API bool BuildFontAtlas(ImFontAtlas* atlas, unsigned int extra_flags = 0); - // By default ImGuiFreeType will use ImGui::MemAlloc()/MemFree(). + // By default ImGuiFreeType will use IM_ALLOC()/IM_FREE(). // However, as FreeType does lots of allocations we provide a way for the user to redirect it to a separate memory heap if desired: IMGUI_API void SetAllocatorFunctions(void* (*alloc_func)(size_t sz, void* user_data), void (*free_func)(void* ptr, void* user_data), void* user_data = NULL); } From e099a7dc7493ee8732df3e8e503112930dbfb645 Mon Sep 17 00:00:00 2001 From: omar Date: Fri, 5 Apr 2019 20:27:46 +0200 Subject: [PATCH 17/19] Vulkan: Bits. Using IM_ALLOC/IM_FREE instead of new[] / delete[]. --- examples/example_glfw_vulkan/main.cpp | 20 +++++++++----------- examples/example_sdl_vulkan/main.cpp | 20 +++++++++----------- examples/imgui_impl_vulkan.cpp | 26 +++++++++++++------------- examples/imgui_impl_vulkan.h | 8 ++------ 4 files changed, 33 insertions(+), 41 deletions(-) diff --git a/examples/example_glfw_vulkan/main.cpp b/examples/example_glfw_vulkan/main.cpp index 972c12b0..53c8aebd 100644 --- a/examples/example_glfw_vulkan/main.cpp +++ b/examples/example_glfw_vulkan/main.cpp @@ -2,9 +2,9 @@ // If you are new to dear imgui, see examples/README.txt and documentation at the top of imgui.cpp. // Important note to the reader who wish to integrate imgui_impl_vulkan.cpp/.h in their own engine/app. -// - Common ImGui_ImplVulkan_XXXX functions and structures are used to interface with imgui_impl_vulkan.cpp/.h. +// - Common ImGui_ImplVulkan_XXX functions and structures are used to interface with imgui_impl_vulkan.cpp/.h. // You will use those if you want to use this rendering back-end in your engine/app. -// - Helper ImGui_ImplVulkanH_XXXX functions and structures are only used by this example (main.cpp) and by +// - Helper ImGui_ImplVulkanH_XXX functions and structures are only used by this example (main.cpp) and by // the back-end itself (imgui_impl_vulkan.cpp), but should PROBABLY NOT be used by your own engine/app code. // Read comments in imgui_impl_vulkan.h. @@ -40,7 +40,7 @@ static VkDebugReportCallbackEXT g_DebugReport = VK_NULL_HANDLE; static VkPipelineCache g_PipelineCache = VK_NULL_HANDLE; static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE; -static ImGui_ImplVulkanH_Window g_WindowData; +static ImGui_ImplVulkanH_Window g_MainWindowData; static int g_MinImageCount = 2; static bool g_SwapChainRebuild = false; static int g_SwapChainResizeWidth = 0; @@ -193,6 +193,8 @@ static void SetupVulkan(const char** extensions, uint32_t extensions_count) } } +// All the ImGui_ImplVulkanH_XXX structures/functions are optional helpers used by the demo. +// Your real engine/app may not use them. static void SetupVulkanWindow(ImGui_ImplVulkanH_Window* wd, VkSurfaceKHR surface, int width, int height) { wd->Surface = surface; @@ -241,11 +243,7 @@ static void CleanupVulkan() static void CleanupVulkanWindow() { - // In a normal engine/app integration, you wouldn't use the ImGui_ImplVulkanH_Window helpers, - // however you would instead need to call ImGui_ImplVulkan_DestroyFrameRenderBuffers() on each - // ImGui_ImplVulkan_FrameRenderBuffers structure that you own. - ImGui_ImplVulkanH_Window* wd = &g_WindowData; - ImGui_ImplVulkanH_DestroyWindow(g_Instance, g_Device, wd, g_Allocator); + ImGui_ImplVulkanH_DestroyWindow(g_Instance, g_Device, &g_MainWindowData, g_Allocator); } static void FrameRender(ImGui_ImplVulkanH_Window* wd) @@ -365,7 +363,7 @@ int main(int, char**) int w, h; glfwGetFramebufferSize(window, &w, &h); glfwSetFramebufferSizeCallback(window, glfw_resize_callback); - ImGui_ImplVulkanH_Window* wd = &g_WindowData; + ImGui_ImplVulkanH_Window* wd = &g_MainWindowData; SetupVulkanWindow(wd, surface, w, h); // Setup Dear ImGui context @@ -458,8 +456,8 @@ int main(int, char**) { g_SwapChainRebuild = false; ImGui_ImplVulkan_SetMinImageCount(g_MinImageCount); - ImGui_ImplVulkanH_CreateWindow(g_Instance, g_PhysicalDevice, g_Device, &g_WindowData, g_QueueFamily, g_Allocator, g_SwapChainResizeWidth, g_SwapChainResizeHeight, g_MinImageCount); - g_WindowData.FrameIndex = 0; + ImGui_ImplVulkanH_CreateWindow(g_Instance, g_PhysicalDevice, g_Device, &g_MainWindowData, g_QueueFamily, g_Allocator, g_SwapChainResizeWidth, g_SwapChainResizeHeight, g_MinImageCount); + g_MainWindowData.FrameIndex = 0; } // Start the Dear ImGui frame diff --git a/examples/example_sdl_vulkan/main.cpp b/examples/example_sdl_vulkan/main.cpp index d2a829c4..45f4fce8 100644 --- a/examples/example_sdl_vulkan/main.cpp +++ b/examples/example_sdl_vulkan/main.cpp @@ -2,9 +2,9 @@ // If you are new to dear imgui, see examples/README.txt and documentation at the top of imgui.cpp. // Important note to the reader who wish to integrate imgui_impl_vulkan.cpp/.h in their own engine/app. -// - Common ImGui_ImplVulkan_XXXX functions and structures are used to interface with imgui_impl_vulkan.cpp/.h. +// - Common ImGui_ImplVulkan_XXX functions and structures are used to interface with imgui_impl_vulkan.cpp/.h. // You will use those if you want to use this rendering back-end in your engine/app. -// - Helper ImGui_ImplVulkanH_XXXX functions and structures are only used by this example (main.cpp) and by +// - Helper ImGui_ImplVulkanH_XXX functions and structures are only used by this example (main.cpp) and by // the back-end itself (imgui_impl_vulkan.cpp), but should PROBABLY NOT be used by your own engine/app code. // Read comments in imgui_impl_vulkan.h. @@ -32,7 +32,7 @@ static VkDebugReportCallbackEXT g_DebugReport = VK_NULL_HANDLE; static VkPipelineCache g_PipelineCache = VK_NULL_HANDLE; static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE; -static ImGui_ImplVulkanH_Window g_WindowData; +static ImGui_ImplVulkanH_Window g_MainWindowData; static uint32_t g_MinImageCount = 2; static bool g_SwapChainRebuild = false; static int g_SwapChainResizeWidth = 0; @@ -185,6 +185,8 @@ static void SetupVulkan(const char** extensions, uint32_t extensions_count) } } +// All the ImGui_ImplVulkanH_XXX structures/functions are optional helpers used by the demo. +// Your real engine/app may not use them. static void SetupVulkanWindow(ImGui_ImplVulkanH_Window* wd, VkSurfaceKHR surface, int width, int height) { wd->Surface = surface; @@ -233,11 +235,7 @@ static void CleanupVulkan() static void CleanupVulkanWindow() { - // In a normal engine/app integration, you wouldn't use the ImGui_ImplVulkanH_Window helpers, - // however you would instead need to call ImGui_ImplVulkan_DestroyFrameRenderBuffers() on each - // ImGui_ImplVulkan_FrameRenderBuffers structure that you own. - ImGui_ImplVulkanH_Window* wd = &g_WindowData; - ImGui_ImplVulkanH_DestroyWindow(g_Instance, g_Device, wd, g_Allocator); + ImGui_ImplVulkanH_DestroyWindow(g_Instance, g_Device, &g_MainWindowData, g_Allocator); } static void FrameRender(ImGui_ImplVulkanH_Window* wd) @@ -351,7 +349,7 @@ int main(int, char**) // Create Framebuffers int w, h; SDL_GetWindowSize(window, &w, &h); - ImGui_ImplVulkanH_Window* wd = &g_WindowData; + ImGui_ImplVulkanH_Window* wd = &g_MainWindowData; SetupVulkanWindow(wd, surface, w, h); // Setup Dear ImGui context @@ -455,8 +453,8 @@ int main(int, char**) { g_SwapChainRebuild = false; ImGui_ImplVulkan_SetMinImageCount(g_MinImageCount); - ImGui_ImplVulkanH_CreateWindow(g_Instance, g_PhysicalDevice, g_Device, &g_WindowData, g_QueueFamily, g_Allocator, g_SwapChainResizeWidth, g_SwapChainResizeHeight, g_MinImageCount); - g_WindowData.FrameIndex = 0; + ImGui_ImplVulkanH_CreateWindow(g_Instance, g_PhysicalDevice, g_Device, &g_MainWindowData, g_QueueFamily, g_Allocator, g_SwapChainResizeWidth, g_SwapChainResizeHeight, g_MinImageCount); + g_MainWindowData.FrameIndex = 0; } // Start the Dear ImGui frame diff --git a/examples/imgui_impl_vulkan.cpp b/examples/imgui_impl_vulkan.cpp index 48237139..e2da42ba 100644 --- a/examples/imgui_impl_vulkan.cpp +++ b/examples/imgui_impl_vulkan.cpp @@ -12,9 +12,9 @@ // IF YOU FEEL YOU NEED TO MAKE ANY CHANGE TO THIS CODE, please share them and your feedback at https://github.com/ocornut/imgui/ // Important note to the reader who wish to integrate imgui_impl_vulkan.cpp/.h in their own engine/app. -// - Common ImGui_ImplVulkan_XXXX functions and structures are used to interface with imgui_impl_vulkan.cpp/.h. +// - Common ImGui_ImplVulkan_XXX functions and structures are used to interface with imgui_impl_vulkan.cpp/.h. // You will use those if you want to use this rendering back-end in your engine/app. -// - Helper ImGui_ImplVulkanH_XXXX functions and structures are only used by this example (main.cpp) and by +// - Helper ImGui_ImplVulkanH_XXX functions and structures are only used by this example (main.cpp) and by // the back-end itself (imgui_impl_vulkan.cpp), but should PROBABLY NOT be used by your own engine/app code. // Read comments in imgui_impl_vulkan.h. @@ -86,6 +86,8 @@ static VkBuffer g_UploadBuffer = VK_NULL_HANDLE; static ImGui_ImplVulkanH_WindowRenderBuffers g_MainWindowRenderBuffers; // Forward Declarations +bool ImGui_ImplVulkan_CreateDeviceObjects(); +void ImGui_ImplVulkan_DestroyDeviceObjects(); void ImGui_ImplVulkanH_DestroyFrame(VkDevice device, ImGui_ImplVulkanH_Frame* fd, const VkAllocationCallbacks* allocator); void ImGui_ImplVulkanH_DestroyFrameSemaphores(VkDevice device, ImGui_ImplVulkanH_FrameSemaphores* fsd, const VkAllocationCallbacks* allocator); void ImGui_ImplVulkanH_DestroyFrameRenderBuffers(VkDevice device, ImGui_ImplVulkanH_FrameRenderBuffers* buffers, const VkAllocationCallbacks* allocator); @@ -230,7 +232,7 @@ static void CreateOrResizeBuffer(VkBuffer& buffer, VkDeviceMemory& buffer_memory VkResult err; if (buffer != VK_NULL_HANDLE) vkDestroyBuffer(v->Device, buffer, v->Allocator); - if (buffer_memory) + if (buffer_memory != VK_NULL_HANDLE) vkFreeMemory(v->Device, buffer_memory, v->Allocator); VkDeviceSize vertex_buffer_size_aligned = ((new_size - 1) / g_BufferMemoryAlignment + 1) * g_BufferMemoryAlignment; @@ -275,7 +277,7 @@ void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer comm { wrb->Index = 0; wrb->Count = v->ImageCount; - wrb->FrameRenderBuffers = new ImGui_ImplVulkanH_FrameRenderBuffers[wrb->Count]; + wrb->FrameRenderBuffers = (ImGui_ImplVulkanH_FrameRenderBuffers*)IM_ALLOC(sizeof(ImGui_ImplVulkanH_FrameRenderBuffers) * wrb->Count); memset(wrb->FrameRenderBuffers, 0, sizeof(ImGui_ImplVulkanH_FrameRenderBuffers) * wrb->Count); } IM_ASSERT(wrb->Count == v->ImageCount); @@ -844,8 +846,6 @@ void ImGui_ImplVulkan_SetMinImageCount(uint32_t min_image_count) // (The ImGui_ImplVulkanH_XXX functions do not interact with any of the state used by the regular ImGui_ImplVulkan_XXX functions) //------------------------------------------------------------------------- -#include // malloc - VkSurfaceFormatKHR ImGui_ImplVulkanH_SelectSurfaceFormat(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkFormat* request_formats, int request_formats_count, VkColorSpaceKHR request_color_space) { IM_ASSERT(request_formats != NULL); @@ -986,8 +986,8 @@ void ImGui_ImplVulkanH_CreateWindowSwapChain(VkPhysicalDevice physical_device, V ImGui_ImplVulkanH_DestroyFrame(device, &wd->Frames[i], allocator); ImGui_ImplVulkanH_DestroyFrameSemaphores(device, &wd->FrameSemaphores[i], allocator); } - delete[] wd->Frames; - delete[] wd->FrameSemaphores; + IM_FREE(wd->Frames); + IM_FREE(wd->FrameSemaphores); wd->Frames = NULL; wd->FrameSemaphores = NULL; wd->ImageCount = 0; @@ -1043,8 +1043,8 @@ void ImGui_ImplVulkanH_CreateWindowSwapChain(VkPhysicalDevice physical_device, V check_vk_result(err); IM_ASSERT(wd->Frames == NULL); - wd->Frames = new ImGui_ImplVulkanH_Frame[wd->ImageCount]; - wd->FrameSemaphores = new ImGui_ImplVulkanH_FrameSemaphores[wd->ImageCount]; + wd->Frames = (ImGui_ImplVulkanH_Frame*)IM_ALLOC(sizeof(ImGui_ImplVulkanH_Frame) * wd->ImageCount); + wd->FrameSemaphores = (ImGui_ImplVulkanH_FrameSemaphores*)IM_ALLOC(sizeof(ImGui_ImplVulkanH_FrameSemaphores) * wd->ImageCount); memset(wd->Frames, 0, sizeof(wd->Frames[0]) * wd->ImageCount); memset(wd->FrameSemaphores, 0, sizeof(wd->FrameSemaphores[0]) * wd->ImageCount); for (uint32_t i = 0; i < wd->ImageCount; i++) @@ -1149,8 +1149,8 @@ void ImGui_ImplVulkanH_DestroyWindow(VkInstance instance, VkDevice device, ImGui ImGui_ImplVulkanH_DestroyFrame(device, &wd->Frames[i], allocator); ImGui_ImplVulkanH_DestroyFrameSemaphores(device, &wd->FrameSemaphores[i], allocator); } - delete[] wd->Frames; - delete[] wd->FrameSemaphores; + IM_FREE(wd->Frames); + IM_FREE(wd->FrameSemaphores); wd->Frames = NULL; wd->FrameSemaphores = NULL; vkDestroyRenderPass(device, wd->RenderPass, allocator); @@ -1194,7 +1194,7 @@ void ImGui_ImplVulkanH_DestroyWindowRenderBuffers(VkDevice device, ImGui_ImplVul { for (uint32_t n = 0; n < buffers->Count; n++) ImGui_ImplVulkanH_DestroyFrameRenderBuffers(device, &buffers->FrameRenderBuffers[n], allocator); - delete[] buffers->FrameRenderBuffers; + IM_FREE(buffers->FrameRenderBuffers); buffers->FrameRenderBuffers = NULL; buffers->Index = 0; buffers->Count = 0; diff --git a/examples/imgui_impl_vulkan.h b/examples/imgui_impl_vulkan.h index 8b5421cc..cff866d9 100644 --- a/examples/imgui_impl_vulkan.h +++ b/examples/imgui_impl_vulkan.h @@ -12,9 +12,9 @@ // IF YOU FEEL YOU NEED TO MAKE ANY CHANGE TO THIS CODE, please share them and your feedback at https://github.com/ocornut/imgui/ // Important note to the reader who wish to integrate imgui_impl_vulkan.cpp/.h in their own engine/app. -// - Common ImGui_ImplVulkan_XXXX functions and structures are used to interface with imgui_impl_vulkan.cpp/.h. +// - Common ImGui_ImplVulkan_XXX functions and structures are used to interface with imgui_impl_vulkan.cpp/.h. // You will use those if you want to use this rendering back-end in your engine/app. -// - Helper ImGui_ImplVulkanH_XXXX functions and structures are only used by this example (main.cpp) and by +// - Helper ImGui_ImplVulkanH_XXX functions and structures are only used by this example (main.cpp) and by // the back-end itself (imgui_impl_vulkan.cpp), but should PROBABLY NOT be used by your own engine/app code. // Read comments in imgui_impl_vulkan.h. @@ -48,10 +48,6 @@ IMGUI_IMPL_API bool ImGui_ImplVulkan_CreateFontsTexture(VkCommandBuffer comm IMGUI_IMPL_API void ImGui_ImplVulkan_DestroyFontUploadObjects(); IMGUI_IMPL_API void ImGui_ImplVulkan_SetMinImageCount(uint32_t min_image_count); // To override MinImageCount after initialization (e.g. if swap chain is recreated) -// Called by ImGui_ImplVulkan_Init(), might be useful elsewhere. -IMGUI_IMPL_API bool ImGui_ImplVulkan_CreateDeviceObjects(); -IMGUI_IMPL_API void ImGui_ImplVulkan_DestroyDeviceObjects(); - //------------------------------------------------------------------------- // Internal / Miscellaneous Vulkan Helpers From c43dab24147da9931f427934377b165e01084d0c Mon Sep 17 00:00:00 2001 From: omar Date: Sun, 7 Apr 2019 15:57:07 +0200 Subject: [PATCH 18/19] Vulkan: Fix not incrementing semaphore index. (#2472, #2071) --- examples/example_glfw_vulkan/main.cpp | 1 + examples/example_sdl_vulkan/main.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/examples/example_glfw_vulkan/main.cpp b/examples/example_glfw_vulkan/main.cpp index 53c8aebd..43abd3e7 100644 --- a/examples/example_glfw_vulkan/main.cpp +++ b/examples/example_glfw_vulkan/main.cpp @@ -320,6 +320,7 @@ static void FramePresent(ImGui_ImplVulkanH_Window* wd) info.pImageIndices = &wd->FrameIndex; VkResult err = vkQueuePresentKHR(g_Queue, &info); check_vk_result(err); + wd->SemaphoreIndex = (wd->SemaphoreIndex + 1) % wd->ImageCount; // Now we can use the next set of semaphores } static void glfw_error_callback(int error, const char* description) diff --git a/examples/example_sdl_vulkan/main.cpp b/examples/example_sdl_vulkan/main.cpp index 45f4fce8..ad789462 100644 --- a/examples/example_sdl_vulkan/main.cpp +++ b/examples/example_sdl_vulkan/main.cpp @@ -312,6 +312,7 @@ static void FramePresent(ImGui_ImplVulkanH_Window* wd) info.pImageIndices = &wd->FrameIndex; VkResult err = vkQueuePresentKHR(g_Queue, &info); check_vk_result(err); + wd->SemaphoreIndex = (wd->SemaphoreIndex + 1) % wd->ImageCount; // Now we can use the next set of semaphores } int main(int, char**) From 302af7b2c991d8a0857640358f6ee00f3464c995 Mon Sep 17 00:00:00 2001 From: omar Date: Sun, 7 Apr 2019 16:22:41 +0200 Subject: [PATCH 19/19] FAQ tweaks. Add missing entries in imgui.cpp (which until now where only in the README). --- docs/README.md | 23 +++++++++++++---------- docs/issue_template.md | 6 +++--- imgui.cpp | 42 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 56 insertions(+), 15 deletions(-) diff --git a/docs/README.md b/docs/README.md index febacf05..ba48517e 100644 --- a/docs/README.md +++ b/docs/README.md @@ -219,29 +219,32 @@ Frequently Asked Question (FAQ) **Where is the documentation?** -- The documentation is at the top of imgui.cpp + effectively imgui.h. -- Example code is in imgui_demo.cpp and particularly the ImGui::ShowDemoWindow() function. It covers most features of ImGui so you can read the code and call the function itself to see its output. -- Standalone example applications using e.g. OpenGL/DirectX are provided in the examples/ folder. -- We obviously needs better documentation! Consider contributing or becoming a [Patron](http://www.patreon.com/imgui) to promote this effort. -- Your programming IDE is your friend, find the type or function declaration to find comments associated to it. + This library is poorly documented at the moment and expects of the user to be acquainted with C/C++. + - Run the examples/ applications and explore them. + - See demo code in imgui_demo.cpp and particularly the ImGui::ShowDemoWindow() function. + - The demo covers most features of Dear ImGui, so you can read the code and see its output. + - See documentation and comments at the top of imgui.cpp + effectively imgui.h. + - Dozens of standalone example applications using e.g. OpenGL/DirectX are provided in the examples/ folder to explain how to integrate Dear ImGui with your own engine/application. + - Your programming IDE is your friend, find the type or function declaration to find comments associated to it. + - We obviously needs better documentation! Consider contributing or becoming a [Patron](http://www.patreon.com/imgui) to promote this effort. **Which version should I get?** I occasionally tag [Releases](https://github.com/ocornut/imgui/releases) but it is generally safe and recommended to sync to master/latest. The library is fairly stable and regressions tend to be fixed fast when reported. -You may also peak at the [Multi-Viewport](https://github.com/ocornut/imgui/issues/1542) and [Docking](https://github.com/ocornut/imgui/issues/2109) features in the `docking` branch. Several projects are using this branch and it is kept in sync with master regularly. +You may also peak at the [Multi-Viewport](https://github.com/ocornut/imgui/issues/1542) and [Docking](https://github.com/ocornut/imgui/issues/2109) features in the `docking` branch. Many projects are using this branch and it is kept in sync with master regularly. **Who uses Dear ImGui?** -See the [Quotes](https://github.com/ocornut/imgui/wiki/Quotes) and [Software using dear imgui](https://github.com/ocornut/imgui/wiki/Software-using-dear-imgui) pages for an (incomplete) list of games/software which are publicly known to use dear imgui. Please add yours if you can! +See the [Quotes](https://github.com/ocornut/imgui/wiki/Quotes) and [Software using dear imgui](https://github.com/ocornut/imgui/wiki/Software-using-dear-imgui) Wiki pages for a list of games/software which are publicly known to use dear imgui. Please add yours if you can! **Why the odd dual naming, "Dear ImGui" vs "ImGui"?** -The library started its life as "ImGui" due to the fact that I didn't give it a proper name when I released 1.0 and had no particular expectation that it would taker off. However, the term IMGUI (immediate-mode graphical user interface) was coined before and is being used in variety of other situations (e.g. Unity uses it own implementation of the IMGUI paradigm). It seemed confusing and unfair to hog the name. To reduce the ambiguity without affecting existing codebases, I have decided on an alternate, longer name "Dear ImGui" that people can use to refer to this specific library in ambiguous situations. Please try to refer to it as "Dear ImGui". +The library started its life as "ImGui" due to the fact that I didn't give it a proper name when I released 1.0 and had no particular expectation that it would take off. However, the term IMGUI (immediate-mode graphical user interface) was coined before and is being used in variety of other situations (e.g. Unity uses it own implementation of the IMGUI paradigm). To reduce this ambiguity without affecting existing codebases, I have decided on an alternate, longer name "Dear ImGui" that people can use to refer to this specific library. Please try to refer to this library as "Dear ImGui". **How can I tell whether to dispatch mouse/keyboard to imgui or to my application?**
**How can I display an image? What is ImTextureID, how does it works?** -
**How can I have multiple widgets with the same label or with an empty label? A primer on labels and the ID Stack.** +
**Why are multiple widgets reacting when I interact with a single one? How can I have multiple widgets with the same label or with an empty label? A primer on labels and the ID Stack...**
**How can I use my own math types instead of ImVec2/ImVec4?**
**How can I load a different font than the default?**
**How can I easily use icons in my application?** @@ -254,7 +257,7 @@ The library started its life as "ImGui" due to the fact that I didn't give it a
**I integrated Dear ImGui in my engine and some elements are disappearing when I move windows around..**
**How can I help?** -See the FAQ in imgui.cpp for answers. +See the FAQ in [imgui.cpp](https://github.com/ocornut/imgui/blob/master/imgui.cpp) for answers. **Can you create elaborate/serious tools with Dear ImGui?** diff --git a/docs/issue_template.md b/docs/issue_template.md index 6d88b271..21f0c1a9 100644 --- a/docs/issue_template.md +++ b/docs/issue_template.md @@ -4,11 +4,11 @@ https://github.com/ocornut/imgui/issues/2261 2. IF YOU ARE HAVING AN ISSUE COMPILING/LINKING/RUNNING/LOADING FONTS, please post on the "Getting Started" Discourse forum: -https://discourse.dearimgui.org/c/getting-started +https://discourse.dearimgui.org -3. PLEASE MAKE SURE that you have: read the FAQ in imgui.cpp; explored the contents of ShowDemoWindow() including the Examples menu; searched among Issues; used your IDE to search for keywords in all sources and text files; and read the link provided in (1). +3. PLEASE MAKE SURE that you have: read the FAQ in imgui.cpp; explored the contents of `ShowDemoWindow()` including the Examples menu; searched among Issues; used your IDE to search for keywords in all sources and text files; and read the link provided in (1). -4. Be mindful that messages are being sent to the mailbox of "Watching" users. Try to proof-read your messages before sending them. Edits are not seen by those users, unless they browse the site. +4. Be mindful that messages are being sent to the e-mail box of "Watching" users. Try to proof-read your messages before sending them. Edits are not seen by those users. 5. Delete points 1-5 and PLEASE FILL THE TEMPLATE BELOW before submitting your issue. diff --git a/imgui.cpp b/imgui.cpp index 4cb82e16..5ee019df 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -37,9 +37,14 @@ DOCUMENTATION - Using gamepad/keyboard navigation controls. - API BREAKING CHANGES (read me when you update!) - FREQUENTLY ASKED QUESTIONS (FAQ), TIPS + - Where is the documentation? + - Which version should I get? + - Who uses Dear ImGui? + - Why the odd dual naming, "Dear ImGui" vs "ImGui"? - How can I tell whether to dispatch mouse/keyboard to imgui or to my application? - How can I display an image? What is ImTextureID, how does it works? - - How can I have multiple widgets with the same label or with an empty label? A primer on labels and the ID Stack. + - Why are multiple widgets reacting when I interact with a single one? How can I have + multiple widgets with the same label or with an empty label? A primer on labels and the ID Stack... - How can I use my own math types instead of ImVec2/ImVec4? - How can I load a different font than the default? - How can I easily use icons in my application? @@ -552,6 +557,39 @@ CODE FREQUENTLY ASKED QUESTIONS (FAQ), TIPS ====================================== + Q: Where is the documentation? + A: This library is poorly documented at the moment and expects of the user to be acquainted with C/C++. + - Run the examples/ and explore them. + - See demo code in imgui_demo.cpp and particularly the ImGui::ShowDemoWindow() function. + - The demo covers most features of Dear ImGui, so you can read the code and see its output. + - See documentation and comments at the top of imgui.cpp + effectively imgui.h. + - Dozens of standalone example applications using e.g. OpenGL/DirectX are provided in the examples/ + folder to explain how to integrate Dear ImGui with your own engine/application. + - Your programming IDE is your friend, find the type or function declaration to find comments + associated to it. + + Q: Which version should I get? + A: I occasionally tag Releases (https://github.com/ocornut/imgui/releases) but it is generally safe + and recommended to sync to master/latest. The library is fairly stable and regressions tend to be + fixed fast when reported. You may also peak at the 'docking' branch which includes: + - Docking/Merging features (https://github.com/ocornut/imgui/issues/2109) + - Multi-viewport features (https://github.com/ocornut/imgui/issues/1542) + Many projects are using this branch and it is kept in sync with master regularly. + + Q: Who uses Dear ImGui? + A: See "Quotes" (https://github.com/ocornut/imgui/wiki/Quotes) and + "Software using Dear ImGui" (https://github.com/ocornut/imgui/wiki/Software-using-dear-imgui) Wiki pages + for a list of games/software which are publicly known to use dear imgui. Please add yours if you can! + + Q: Why the odd dual naming, "Dear ImGui" vs "ImGui"? + A: The library started its life as "ImGui" due to the fact that I didn't give it a proper name when + when I released 1.0, and had no particular expectation that it would take off. However, the term IMGUI + (immediate-mode graphical user interface) was coined before and is being used in variety of other + situations (e.g. Unity uses it own implementation of the IMGUI paradigm). + To reduce the ambiguity without affecting existing code bases, I have decided on an alternate, + longer name "Dear ImGui" that people can use to refer to this specific library. + Please try to refer to this library as "Dear ImGui". + Q: How can I tell whether to dispatch mouse/keyboard to imgui or to my application? A: You can read the 'io.WantCaptureMouse', 'io.WantCaptureKeyboard' and 'io.WantTextInput' flags from the ImGuiIO structure (e.g. if (ImGui::GetIO().WantCaptureMouse) { ... } ) - When 'io.WantCaptureMouse' is set, imgui wants to use your mouse state, and you may want to discard/hide the inputs from the rest of your application. @@ -653,8 +691,8 @@ CODE Finally, you may call ImGui::ShowMetricsWindow() to explore/visualize/understand how the ImDrawList are generated. + Q: Why are multiple widgets reacting when I interact with a single one? Q: How can I have multiple widgets with the same label or with an empty label? - Q: I have multiple widgets with the same label, and only the first one works. Why is that? A: A primer on labels and the ID Stack... Dear ImGui internally need to uniquely identify UI elements.