1 | // dear imgui: Renderer Backend for Vulkan |
2 | // This needs to be used along with a Platform Backend (e.g. GLFW, SDL, Win32, custom..) |
3 | |
4 | // Implemented features: |
5 | // [!] Renderer: User texture binding. Use 'VkDescriptorSet' as ImTextureID. Call ImGui_ImplVulkan_AddTexture() to register one. Read the FAQ about ImTextureID! See https://github.com/ocornut/imgui/pull/914 for discussions. |
6 | // [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset). |
7 | // [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'. |
8 | // [x] Renderer: Multi-viewport / platform windows. With issues (flickering when creating a new viewport). |
9 | |
10 | // The aim of imgui_impl_vulkan.h/.cpp is to be usable in your engine without any modification. |
11 | // IF YOU FEEL YOU NEED TO MAKE ANY CHANGE TO THIS CODE, please share them and your feedback at https://github.com/ocornut/imgui/ |
12 | |
13 | // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. |
14 | // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. |
15 | // Learn about Dear ImGui: |
16 | // - FAQ https://dearimgui.com/faq |
17 | // - Getting Started https://dearimgui.com/getting-started |
18 | // - Documentation https://dearimgui.com/docs (same as your local docs/ folder). |
19 | // - Introduction, links and more at the top of imgui.cpp |
20 | |
21 | // Important note to the reader who wish to integrate imgui_impl_vulkan.cpp/.h in their own engine/app. |
22 | // - Common ImGui_ImplVulkan_XXX functions and structures are used to interface with imgui_impl_vulkan.cpp/.h. |
23 | // You will use those if you want to use this rendering backend in your engine/app. |
24 | // - Helper ImGui_ImplVulkanH_XXX functions and structures are only used by this example (main.cpp) and by |
25 | // the backend itself (imgui_impl_vulkan.cpp), but should PROBABLY NOT be used by your own engine/app code. |
26 | // Read comments in imgui_impl_vulkan.h. |
27 | |
28 | #pragma once |
29 | #ifndef IMGUI_DISABLE |
30 | #include "imgui.h" // IMGUI_IMPL_API |
31 | |
32 | // [Configuration] in order to use a custom Vulkan function loader: |
33 | // (1) You'll need to disable default Vulkan function prototypes. |
34 | // We provide a '#define IMGUI_IMPL_VULKAN_NO_PROTOTYPES' convenience configuration flag. |
35 | // In order to make sure this is visible from the imgui_impl_vulkan.cpp compilation unit: |
36 | // - Add '#define IMGUI_IMPL_VULKAN_NO_PROTOTYPES' in your imconfig.h file |
37 | // - Or as a compilation flag in your build system |
38 | // - Or uncomment here (not recommended because you'd be modifying imgui sources!) |
39 | // - Do not simply add it in a .cpp file! |
40 | // (2) Call ImGui_ImplVulkan_LoadFunctions() before ImGui_ImplVulkan_Init() with your custom function. |
41 | // If you have no idea what this is, leave it alone! |
42 | //#define IMGUI_IMPL_VULKAN_NO_PROTOTYPES |
43 | |
44 | // Convenience support for Volk |
45 | // (you can also technically use IMGUI_IMPL_VULKAN_NO_PROTOTYPES + wrap Volk via ImGui_ImplVulkan_LoadFunctions().) |
46 | //#define IMGUI_IMPL_VULKAN_USE_VOLK |
47 | |
48 | #if defined(IMGUI_IMPL_VULKAN_NO_PROTOTYPES) && !defined(VK_NO_PROTOTYPES) |
49 | #define VK_NO_PROTOTYPES |
50 | #endif |
51 | #if defined(VK_USE_PLATFORM_WIN32_KHR) && !defined(NOMINMAX) |
52 | #define NOMINMAX |
53 | #endif |
54 | |
55 | // Vulkan includes |
56 | #ifdef IMGUI_IMPL_VULKAN_USE_VOLK |
57 | #include <volk.h> |
58 | #else |
59 | #include <vulkan/vulkan.h> |
60 | #endif |
61 | #if defined(VK_VERSION_1_3) || defined(VK_KHR_dynamic_rendering) |
62 | #define IMGUI_IMPL_VULKAN_HAS_DYNAMIC_RENDERING |
63 | #endif |
64 | |
65 | // Initialization data, for ImGui_ImplVulkan_Init() |
66 | // [Please zero-clear before use!] |
67 | // - About descriptor pool: |
68 | // - A VkDescriptorPool should be created with VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, |
69 | // and must contain a pool size large enough to hold a small number of VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER descriptors. |
70 | // - As an convenience, by setting DescriptorPoolSize > 0 the backend will create one for you. |
71 | // - Current version of the backend use 1 descriptor for the font atlas + as many as additional calls done to ImGui_ImplVulkan_AddTexture(). |
72 | // - It is expected that as early as Q1 2025 the backend will use a few more descriptors, so aim at 10 + number of desierd calls to ImGui_ImplVulkan_AddTexture(). |
73 | // - About dynamic rendering: |
74 | // - When using dynamic rendering, set UseDynamicRendering=true and fill PipelineRenderingCreateInfo structure. |
75 | struct ImGui_ImplVulkan_InitInfo |
76 | { |
77 | VkInstance Instance; |
78 | VkPhysicalDevice PhysicalDevice; |
79 | VkDevice Device; |
80 | uint32_t QueueFamily; |
81 | VkQueue Queue; |
82 | VkDescriptorPool DescriptorPool; // See requirements in note above; ignored if using DescriptorPoolSize > 0 |
83 | VkRenderPass RenderPass; // Ignored if using dynamic rendering |
84 | uint32_t MinImageCount; // >= 2 |
85 | uint32_t ImageCount; // >= MinImageCount |
86 | VkSampleCountFlagBits MSAASamples; // 0 defaults to VK_SAMPLE_COUNT_1_BIT |
87 | |
88 | // (Optional) |
89 | VkPipelineCache PipelineCache; |
90 | uint32_t Subpass; |
91 | |
92 | // (Optional) Set to create internal descriptor pool instead of using DescriptorPool |
93 | uint32_t DescriptorPoolSize; |
94 | |
95 | // (Optional) Dynamic Rendering |
96 | // Need to explicitly enable VK_KHR_dynamic_rendering extension to use this, even for Vulkan 1.3. |
97 | bool UseDynamicRendering; |
98 | #ifdef IMGUI_IMPL_VULKAN_HAS_DYNAMIC_RENDERING |
99 | VkPipelineRenderingCreateInfoKHR PipelineRenderingCreateInfo; |
100 | #endif |
101 | |
102 | // (Optional) Allocation, Debugging |
103 | const VkAllocationCallbacks* Allocator; |
104 | void (*CheckVkResultFn)(VkResult err); |
105 | VkDeviceSize MinAllocationSize; // Minimum allocation size. Set to 1024*1024 to satisfy zealous best practices validation layer and waste a little memory. |
106 | }; |
107 | |
108 | // Follow "Getting Started" link and check examples/ folder to learn about using backends! |
109 | IMGUI_IMPL_API bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info); |
110 | IMGUI_IMPL_API void ImGui_ImplVulkan_Shutdown(); |
111 | IMGUI_IMPL_API void ImGui_ImplVulkan_NewFrame(); |
112 | IMGUI_IMPL_API void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer command_buffer, VkPipeline pipeline = VK_NULL_HANDLE); |
113 | IMGUI_IMPL_API bool ImGui_ImplVulkan_CreateFontsTexture(); |
114 | IMGUI_IMPL_API void ImGui_ImplVulkan_DestroyFontsTexture(); |
115 | IMGUI_IMPL_API void ImGui_ImplVulkan_SetMinImageCount(uint32_t min_image_count); // To override MinImageCount after initialization (e.g. if swap chain is recreated) |
116 | |
117 | // Register a texture (VkDescriptorSet == ImTextureID) |
118 | // FIXME: This is experimental in the sense that we are unsure how to best design/tackle this problem |
119 | // Please post to https://github.com/ocornut/imgui/pull/914 if you have suggestions. |
120 | IMGUI_IMPL_API VkDescriptorSet ImGui_ImplVulkan_AddTexture(VkSampler sampler, VkImageView image_view, VkImageLayout image_layout); |
121 | IMGUI_IMPL_API void ImGui_ImplVulkan_RemoveTexture(VkDescriptorSet descriptor_set); |
122 | |
123 | // Optional: load Vulkan functions with a custom function loader |
124 | // This is only useful with IMGUI_IMPL_VULKAN_NO_PROTOTYPES / VK_NO_PROTOTYPES |
125 | IMGUI_IMPL_API bool ImGui_ImplVulkan_LoadFunctions(PFN_vkVoidFunction(*loader_func)(const char* function_name, void* user_data), void* user_data = nullptr); |
126 | |
127 | // [BETA] Selected render state data shared with callbacks. |
128 | // This is temporarily stored in GetPlatformIO().Renderer_RenderState during the ImGui_ImplVulkan_RenderDrawData() call. |
129 | // (Please open an issue if you feel you need access to more data) |
130 | struct ImGui_ImplVulkan_RenderState |
131 | { |
132 | VkCommandBuffer CommandBuffer; |
133 | VkPipeline Pipeline; |
134 | VkPipelineLayout PipelineLayout; |
135 | }; |
136 | |
137 | //------------------------------------------------------------------------- |
138 | // Internal / Miscellaneous Vulkan Helpers |
139 | // (Used by example's main.cpp. Used by multi-viewport features. PROBABLY NOT used by your own engine/app.) |
140 | //------------------------------------------------------------------------- |
141 | // You probably do NOT need to use or care about those functions. |
142 | // Those functions only exist because: |
143 | // 1) they facilitate the readability and maintenance of the multiple main.cpp examples files. |
144 | // 2) the multi-viewport / platform window implementation needs them internally. |
145 | // Generally we avoid exposing any kind of superfluous high-level helpers in the bindings, |
146 | // but it is too much code to duplicate everywhere so we exceptionally expose them. |
147 | // |
148 | // Your engine/app will likely _already_ have code to setup all that stuff (swap chain, render pass, frame buffers, etc.). |
149 | // You may read this code to learn about Vulkan, but it is recommended you use you own custom tailored code to do equivalent work. |
150 | // (The ImGui_ImplVulkanH_XXX functions do not interact with any of the state used by the regular ImGui_ImplVulkan_XXX functions) |
151 | //------------------------------------------------------------------------- |
152 | |
153 | struct ImGui_ImplVulkanH_Frame; |
154 | struct ImGui_ImplVulkanH_Window; |
155 | |
156 | // Helpers |
157 | IMGUI_IMPL_API void ImGui_ImplVulkanH_CreateOrResizeWindow(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count); |
158 | IMGUI_IMPL_API void ImGui_ImplVulkanH_DestroyWindow(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_Window* wd, const VkAllocationCallbacks* allocator); |
159 | IMGUI_IMPL_API VkSurfaceFormatKHR ImGui_ImplVulkanH_SelectSurfaceFormat(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkFormat* request_formats, int request_formats_count, VkColorSpaceKHR request_color_space); |
160 | IMGUI_IMPL_API VkPresentModeKHR ImGui_ImplVulkanH_SelectPresentMode(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkPresentModeKHR* request_modes, int request_modes_count); |
161 | IMGUI_IMPL_API int ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(VkPresentModeKHR present_mode); |
162 | |
163 | // Helper structure to hold the data needed by one rendering frame |
164 | // (Used by example's main.cpp. Used by multi-viewport features. Probably NOT used by your own engine/app.) |
165 | // [Please zero-clear before use!] |
166 | struct ImGui_ImplVulkanH_Frame |
167 | { |
168 | VkCommandPool CommandPool; |
169 | VkCommandBuffer CommandBuffer; |
170 | VkFence Fence; |
171 | VkImage Backbuffer; |
172 | VkImageView BackbufferView; |
173 | VkFramebuffer Framebuffer; |
174 | }; |
175 | |
176 | struct ImGui_ImplVulkanH_FrameSemaphores |
177 | { |
178 | VkSemaphore ImageAcquiredSemaphore; |
179 | VkSemaphore RenderCompleteSemaphore; |
180 | }; |
181 | |
182 | // Helper structure to hold the data needed by one rendering context into one OS window |
183 | // (Used by example's main.cpp. Used by multi-viewport features. Probably NOT used by your own engine/app.) |
184 | struct ImGui_ImplVulkanH_Window |
185 | { |
186 | int Width; |
187 | int Height; |
188 | VkSwapchainKHR Swapchain; |
189 | VkSurfaceKHR Surface; |
190 | VkSurfaceFormatKHR SurfaceFormat; |
191 | VkPresentModeKHR PresentMode; |
192 | VkRenderPass RenderPass; |
193 | bool UseDynamicRendering; |
194 | bool ClearEnable; |
195 | VkClearValue ClearValue; |
196 | uint32_t FrameIndex; // Current frame being rendered to (0 <= FrameIndex < FrameInFlightCount) |
197 | uint32_t ImageCount; // Number of simultaneous in-flight frames (returned by vkGetSwapchainImagesKHR, usually derived from min_image_count) |
198 | uint32_t SemaphoreCount; // Number of simultaneous in-flight frames + 1, to be able to use it in vkAcquireNextImageKHR |
199 | uint32_t SemaphoreIndex; // Current set of swapchain wait semaphores we're using (needs to be distinct from per frame data) |
200 | ImGui_ImplVulkanH_Frame* Frames; |
201 | ImGui_ImplVulkanH_FrameSemaphores* FrameSemaphores; |
202 | |
203 | ImGui_ImplVulkanH_Window() |
204 | { |
205 | memset(s: (void*)this, c: 0, n: sizeof(*this)); |
206 | PresentMode = (VkPresentModeKHR)~0; // Ensure we get an error if user doesn't set this. |
207 | ClearEnable = true; |
208 | } |
209 | }; |
210 | |
211 | #endif // #ifndef IMGUI_DISABLE |
212 | |