imgui_impl_vulkan.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. // Convenience support for Volk
  38. // (you can also technically use IMGUI_IMPL_VULKAN_NO_PROTOTYPES + wrap Volk via ImGui_ImplVulkan_LoadFunctions().)
  39. //#define IMGUI_IMPL_VULKAN_USE_VOLK
  40. #if defined(IMGUI_IMPL_VULKAN_NO_PROTOTYPES) && !defined(VK_NO_PROTOTYPES)
  41. #define VK_NO_PROTOTYPES
  42. #endif
  43. #if defined(VK_USE_PLATFORM_WIN32_KHR) && !defined(NOMINMAX)
  44. #define NOMINMAX
  45. #endif
  46. // Vulkan includes
  47. #ifdef IMGUI_IMPL_VULKAN_USE_VOLK
  48. #include <volk.h>
  49. #else
  50. #include <vulkan/vulkan.h>
  51. #endif
  52. #if defined(VK_VERSION_1_3) || defined(VK_KHR_dynamic_rendering)
  53. #define IMGUI_IMPL_VULKAN_HAS_DYNAMIC_RENDERING
  54. #endif
  55. // Backend uses a small number of descriptors per font atlas + as many as additional calls done to ImGui_ImplVulkan_AddTexture().
  56. #define IMGUI_IMPL_VULKAN_MINIMUM_IMAGE_SAMPLER_POOL_SIZE (8) // Minimum per atlas
  57. // Specify settings to create pipeline and swapchain
  58. struct ImGui_ImplVulkan_PipelineInfo
  59. {
  60. // For Main and Secondary viewports
  61. VkRenderPass RenderPass; // Ignored if using dynamic rendering
  62. uint32_t Subpass; //
  63. VkSampleCountFlagBits MSAASamples = {}; // 0 defaults to VK_SAMPLE_COUNT_1_BIT
  64. #ifdef IMGUI_IMPL_VULKAN_HAS_DYNAMIC_RENDERING
  65. VkPipelineRenderingCreateInfoKHR PipelineRenderingCreateInfo; // Optional, valid if .sType == VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR
  66. #endif
  67. };
  68. // Initialization data, for ImGui_ImplVulkan_Init()
  69. // [Please zero-clear before use!]
  70. // - About descriptor pool:
  71. // - A VkDescriptorPool should be created with VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT,
  72. // and must contain a pool size large enough to hold a small number of VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER descriptors.
  73. // - As an convenience, by setting DescriptorPoolSize > 0 the backend will create one for you.
  74. // - About dynamic rendering:
  75. // - When using dynamic rendering, set UseDynamicRendering=true and fill PipelineRenderingCreateInfo structure.
  76. struct ImGui_ImplVulkan_InitInfo
  77. {
  78. 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)
  79. VkInstance Instance;
  80. VkPhysicalDevice PhysicalDevice;
  81. VkDevice Device;
  82. uint32_t QueueFamily;
  83. VkQueue Queue;
  84. VkDescriptorPool DescriptorPool; // See requirements in note above; ignored if using DescriptorPoolSize > 0
  85. uint32_t DescriptorPoolSize; // Optional: set to create internal descriptor pool automatically instead of using DescriptorPool.
  86. uint32_t MinImageCount; // >= 2
  87. uint32_t ImageCount; // >= MinImageCount
  88. VkPipelineCache PipelineCache; // Optional
  89. // Pipeline
  90. ImGui_ImplVulkan_PipelineInfo PipelineInfoMain; // Infos for Main Viewport (created by app/user)
  91. //VkRenderPass RenderPass; // --> Since 2025/09/26: set 'PipelineInfoMain.RenderPass' instead
  92. //uint32_t Subpass; // --> Since 2025/09/26: set 'PipelineInfoMain.Subpass' instead
  93. //VkSampleCountFlagBits MSAASamples; // --> Since 2025/09/26: set 'PipelineInfoMain.MSAASamples' instead
  94. //VkPipelineRenderingCreateInfoKHR PipelineRenderingCreateInfo; // Since 2025/09/26: set 'PipelineInfoMain.PipelineRenderingCreateInfo' instead
  95. // (Optional) Dynamic Rendering
  96. // Need to explicitly enable VK_KHR_dynamic_rendering extension to use this, even for Vulkan 1.3 + setup PipelineInfoMain.PipelineRenderingCreateInfo.
  97. bool UseDynamicRendering;
  98. // (Optional) Allocation, Debugging
  99. const VkAllocationCallbacks* Allocator;
  100. void (*CheckVkResultFn)(VkResult err);
  101. VkDeviceSize MinAllocationSize; // Minimum allocation size. Set to 1024*1024 to satisfy zealous best practices validation layer and waste a little memory.
  102. // (Optional) Customize default vertex/fragment shaders.
  103. // - if .sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO we use specified structs, otherwise we use defaults.
  104. // - Shader inputs/outputs need to match ours. Code/data pointed to by the structure needs to survive for whole during of backend usage.
  105. VkShaderModuleCreateInfo CustomShaderVertCreateInfo;
  106. VkShaderModuleCreateInfo CustomShaderFragCreateInfo;
  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 void ImGui_ImplVulkan_SetMinImageCount(uint32_t min_image_count); // To override MinImageCount after initialization (e.g. if swap chain is recreated)
  114. // (Advanced) Use e.g. if you need to recreate pipeline without reinitializing the backend (see #8110, #8111)
  115. // 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))
  116. // Else, the pipeline can be created, or re-created, using ImGui_ImplVulkan_CreateMainPipeline() before rendering.
  117. IMGUI_IMPL_API void ImGui_ImplVulkan_CreateMainPipeline(const ImGui_ImplVulkan_PipelineInfo* info);
  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.
  119. IMGUI_IMPL_API void ImGui_ImplVulkan_UpdateTexture(ImTextureData* tex);
  120. // Register a texture (VkDescriptorSet == ImTextureID)
  121. // FIXME: This is experimental in the sense that we are unsure how to best design/tackle this problem
  122. // Please post to https://github.com/ocornut/imgui/pull/914 if you have suggestions.
  123. IMGUI_IMPL_API VkDescriptorSet ImGui_ImplVulkan_AddTexture(VkSampler sampler, VkImageView image_view, VkImageLayout image_layout);
  124. IMGUI_IMPL_API void ImGui_ImplVulkan_RemoveTexture(VkDescriptorSet descriptor_set);
  125. // Optional: load Vulkan functions with a custom function loader
  126. // This is only useful with IMGUI_IMPL_VULKAN_NO_PROTOTYPES / VK_NO_PROTOTYPES
  127. 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);
  128. // [BETA] Selected render state data shared with callbacks.
  129. // This is temporarily stored in GetPlatformIO().Renderer_RenderState during the ImGui_ImplVulkan_RenderDrawData() call.
  130. // (Please open an issue if you feel you need access to more data)
  131. struct ImGui_ImplVulkan_RenderState
  132. {
  133. VkCommandBuffer CommandBuffer;
  134. VkPipeline Pipeline;
  135. VkPipelineLayout PipelineLayout;
  136. };
  137. //-------------------------------------------------------------------------
  138. // Internal / Miscellaneous Vulkan Helpers
  139. //-------------------------------------------------------------------------
  140. // Used by example's main.cpp. Used by multi-viewport features. PROBABLY NOT used by your own engine/app.
  141. //
  142. // You probably do NOT need to use or care about those functions.
  143. // Those functions only exist because:
  144. // 1) they facilitate the readability and maintenance of the multiple main.cpp examples files.
  145. // 2) the multi-viewport / platform window implementation needs them internally.
  146. // Generally we avoid exposing any kind of superfluous high-level helpers in the backends,
  147. // but it is too much code to duplicate everywhere so we exceptionally expose them.
  148. //
  149. // Your engine/app will likely _already_ have code to setup all that stuff (swap chain,
  150. // render pass, frame buffers, etc.). You may read this code if you are curious, but
  151. // it is recommended you use your own custom tailored code to do equivalent work.
  152. //
  153. // We don't provide a strong guarantee that we won't change those functions API.
  154. //
  155. // The ImGui_ImplVulkanH_XXX functions should NOT interact with any of the state used
  156. // by the regular ImGui_ImplVulkan_XXX functions.
  157. //-------------------------------------------------------------------------
  158. struct ImGui_ImplVulkanH_Frame;
  159. struct ImGui_ImplVulkanH_Window;
  160. // Helpers
  161. 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);
  162. IMGUI_IMPL_API void ImGui_ImplVulkanH_DestroyWindow(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_Window* wd, const VkAllocationCallbacks* allocator);
  163. IMGUI_IMPL_API VkSurfaceFormatKHR ImGui_ImplVulkanH_SelectSurfaceFormat(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkFormat* request_formats, int request_formats_count, VkColorSpaceKHR request_color_space);
  164. IMGUI_IMPL_API VkPresentModeKHR ImGui_ImplVulkanH_SelectPresentMode(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkPresentModeKHR* request_modes, int request_modes_count);
  165. IMGUI_IMPL_API VkPhysicalDevice ImGui_ImplVulkanH_SelectPhysicalDevice(VkInstance instance);
  166. IMGUI_IMPL_API uint32_t ImGui_ImplVulkanH_SelectQueueFamilyIndex(VkPhysicalDevice physical_device);
  167. IMGUI_IMPL_API int ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(VkPresentModeKHR present_mode);
  168. // Helper structure to hold the data needed by one rendering frame
  169. // (Used by example's main.cpp. Used by multi-viewport features. Probably NOT used by your own engine/app.)
  170. // [Please zero-clear before use!]
  171. struct ImGui_ImplVulkanH_Frame
  172. {
  173. VkCommandPool CommandPool;
  174. VkCommandBuffer CommandBuffer;
  175. VkFence Fence;
  176. VkImage Backbuffer;
  177. VkImageView BackbufferView;
  178. VkFramebuffer Framebuffer;
  179. };
  180. struct ImGui_ImplVulkanH_FrameSemaphores
  181. {
  182. VkSemaphore ImageAcquiredSemaphore;
  183. VkSemaphore RenderCompleteSemaphore;
  184. };
  185. // Helper structure to hold the data needed by one rendering context into one OS window
  186. // (Used by example's main.cpp. Used by multi-viewport features. Probably NOT used by your own engine/app.)
  187. struct ImGui_ImplVulkanH_Window
  188. {
  189. int Width;
  190. int Height;
  191. VkSwapchainKHR Swapchain;
  192. VkSurfaceKHR Surface;
  193. VkSurfaceFormatKHR SurfaceFormat;
  194. VkPresentModeKHR PresentMode;
  195. VkRenderPass RenderPass;
  196. VkPipeline Pipeline; // The window pipeline may uses a different VkRenderPass than the one passed in ImGui_ImplVulkan_InitInfo
  197. bool UseDynamicRendering;
  198. bool ClearEnable;
  199. VkClearValue ClearValue;
  200. uint32_t FrameIndex; // Current frame being rendered to (0 <= FrameIndex < FrameInFlightCount)
  201. uint32_t ImageCount; // Number of simultaneous in-flight frames (returned by vkGetSwapchainImagesKHR, usually derived from min_image_count)
  202. uint32_t SemaphoreCount; // Number of simultaneous in-flight frames + 1, to be able to use it in vkAcquireNextImageKHR
  203. uint32_t SemaphoreIndex; // Current set of swapchain wait semaphores we're using (needs to be distinct from per frame data)
  204. ImVector<ImGui_ImplVulkanH_Frame> Frames;
  205. ImVector<ImGui_ImplVulkanH_FrameSemaphores> FrameSemaphores;
  206. ImGui_ImplVulkanH_Window()
  207. {
  208. memset((void*)this, 0, sizeof(*this));
  209. PresentMode = (VkPresentModeKHR)~0; // Ensure we get an error if user doesn't set this.
  210. ClearEnable = true;
  211. }
  212. };
  213. #endif // #ifndef IMGUI_DISABLE