imgui_impl_vulkan.h 16 KB

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