imgui_impl_vulkan.h 13 KB

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