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

source code of imgui/backends/imgui_impl_vulkan.h