imgui_impl_vulkan.h 16 KB

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