imgui_impl_vulkan.h 16 KB

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