imgui_impl_vulkan.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. // 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. 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)
  68. VkInstance Instance;
  69. VkPhysicalDevice PhysicalDevice;
  70. VkDevice Device;
  71. uint32_t QueueFamily;
  72. VkQueue Queue;
  73. VkDescriptorPool DescriptorPool; // See requirements in note above; ignored if using DescriptorPoolSize > 0
  74. uint32_t DescriptorPoolSize; // Optional: set to create internal descriptor pool automatically instead of using DescriptorPool.
  75. uint32_t MinImageCount; // >= 2
  76. uint32_t ImageCount; // >= MinImageCount
  77. VkPipelineCache PipelineCache; // Optional
  78. // Pipeline
  79. VkRenderPass RenderPass; // Ignored if using dynamic rendering
  80. uint32_t Subpass;
  81. VkSampleCountFlagBits MSAASamples; // 0 defaults to VK_SAMPLE_COUNT_1_BIT
  82. // (Optional) Dynamic Rendering
  83. // Need to explicitly enable VK_KHR_dynamic_rendering extension to use this, even for Vulkan 1.3 + setup PipelineRenderingCreateInfo.
  84. bool UseDynamicRendering;
  85. #ifdef IMGUI_IMPL_VULKAN_HAS_DYNAMIC_RENDERING
  86. VkPipelineRenderingCreateInfoKHR PipelineRenderingCreateInfo; // Optional, valid if .sType == VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR
  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 void ImGui_ImplVulkan_SetMinImageCount(uint32_t min_image_count); // To override MinImageCount after initialization (e.g. if swap chain is recreated)
  99. // (Advanced) Use e.g. if you need to recreate pipeline without reinitializing the backend (see #8110, #8111)
  100. // 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))
  101. // Else, the pipeline can be created, or re-created, using ImGui_ImplVulkan_CreateMainPipeline() before rendering.
  102. struct ImGui_ImplVulkan_MainPipelineCreateInfo
  103. {
  104. VkRenderPass RenderPass = VK_NULL_HANDLE;
  105. uint32_t Subpass = 0;
  106. VkSampleCountFlagBits MSAASamples = {};
  107. #ifdef IMGUI_IMPL_VULKAN_HAS_DYNAMIC_RENDERING
  108. VkPipelineRenderingCreateInfoKHR PipelineRenderingCreateInfo; // Optional, valid if .sType == VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR
  109. #endif
  110. };
  111. IMGUI_IMPL_API void ImGui_ImplVulkan_CreateMainPipeline(const ImGui_ImplVulkan_MainPipelineCreateInfo& info); // (render_pass xor (p_dynamic_rendering && p_dynamic_rendering is correct (sType and pNext)))
  112. // (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.
  113. IMGUI_IMPL_API void ImGui_ImplVulkan_UpdateTexture(ImTextureData* tex);
  114. // Register a texture (VkDescriptorSet == ImTextureID)
  115. // FIXME: This is experimental in the sense that we are unsure how to best design/tackle this problem
  116. // Please post to https://github.com/ocornut/imgui/pull/914 if you have suggestions.
  117. IMGUI_IMPL_API VkDescriptorSet ImGui_ImplVulkan_AddTexture(VkSampler sampler, VkImageView image_view, VkImageLayout image_layout);
  118. IMGUI_IMPL_API void ImGui_ImplVulkan_RemoveTexture(VkDescriptorSet descriptor_set);
  119. // Optional: load Vulkan functions with a custom function loader
  120. // This is only useful with IMGUI_IMPL_VULKAN_NO_PROTOTYPES / VK_NO_PROTOTYPES
  121. 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);
  122. // [BETA] Selected render state data shared with callbacks.
  123. // This is temporarily stored in GetPlatformIO().Renderer_RenderState during the ImGui_ImplVulkan_RenderDrawData() call.
  124. // (Please open an issue if you feel you need access to more data)
  125. struct ImGui_ImplVulkan_RenderState
  126. {
  127. VkCommandBuffer CommandBuffer;
  128. VkPipeline Pipeline;
  129. VkPipelineLayout PipelineLayout;
  130. };
  131. //-------------------------------------------------------------------------
  132. // Internal / Miscellaneous Vulkan Helpers
  133. //-------------------------------------------------------------------------
  134. // Used by example's main.cpp. Used by multi-viewport features. PROBABLY NOT used by your own engine/app.
  135. //
  136. // You probably do NOT need to use or care about those functions.
  137. // Those functions only exist because:
  138. // 1) they facilitate the readability and maintenance of the multiple main.cpp examples files.
  139. // 2) the multi-viewport / platform window implementation needs them internally.
  140. // Generally we avoid exposing any kind of superfluous high-level helpers in the backends,
  141. // but it is too much code to duplicate everywhere so we exceptionally expose them.
  142. //
  143. // Your engine/app will likely _already_ have code to setup all that stuff (swap chain,
  144. // render pass, frame buffers, etc.). You may read this code if you are curious, but
  145. // it is recommended you use your own custom tailored code to do equivalent work.
  146. //
  147. // We don't provide a strong guarantee that we won't change those functions API.
  148. //
  149. // The ImGui_ImplVulkanH_XXX functions should NOT interact with any of the state used
  150. // by the regular ImGui_ImplVulkan_XXX functions.
  151. //-------------------------------------------------------------------------
  152. struct ImGui_ImplVulkanH_Frame;
  153. struct ImGui_ImplVulkanH_Window;
  154. // Helpers
  155. 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);
  156. IMGUI_IMPL_API void ImGui_ImplVulkanH_DestroyWindow(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_Window* wnd, const VkAllocationCallbacks* allocator);
  157. IMGUI_IMPL_API VkSurfaceFormatKHR ImGui_ImplVulkanH_SelectSurfaceFormat(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkFormat* request_formats, int request_formats_count, VkColorSpaceKHR request_color_space);
  158. IMGUI_IMPL_API VkPresentModeKHR ImGui_ImplVulkanH_SelectPresentMode(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkPresentModeKHR* request_modes, int request_modes_count);
  159. IMGUI_IMPL_API VkPhysicalDevice ImGui_ImplVulkanH_SelectPhysicalDevice(VkInstance instance);
  160. IMGUI_IMPL_API uint32_t ImGui_ImplVulkanH_SelectQueueFamilyIndex(VkPhysicalDevice physical_device);
  161. IMGUI_IMPL_API int ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(VkPresentModeKHR present_mode);
  162. // Helper structure to hold the data needed by one rendering frame
  163. // (Used by example's main.cpp. Used by multi-viewport features. Probably NOT used by your own engine/app.)
  164. // [Please zero-clear before use!]
  165. struct ImGui_ImplVulkanH_Frame
  166. {
  167. VkCommandPool CommandPool;
  168. VkCommandBuffer CommandBuffer;
  169. VkFence Fence;
  170. VkImage Backbuffer;
  171. VkImageView BackbufferView;
  172. VkFramebuffer Framebuffer;
  173. };
  174. struct ImGui_ImplVulkanH_FrameSemaphores
  175. {
  176. VkSemaphore ImageAcquiredSemaphore;
  177. VkSemaphore RenderCompleteSemaphore;
  178. };
  179. // Helper structure to hold the data needed by one rendering context into one OS window
  180. // (Used by example's main.cpp. Used by multi-viewport features. Probably NOT used by your own engine/app.)
  181. struct ImGui_ImplVulkanH_Window
  182. {
  183. int Width;
  184. int Height;
  185. VkSwapchainKHR Swapchain;
  186. VkSurfaceKHR Surface;
  187. VkSurfaceFormatKHR SurfaceFormat;
  188. VkPresentModeKHR PresentMode;
  189. VkRenderPass RenderPass;
  190. VkPipeline Pipeline; // The window pipeline may uses a different VkRenderPass than the one passed in ImGui_ImplVulkan_InitInfo
  191. bool UseDynamicRendering;
  192. bool ClearEnable;
  193. VkClearValue ClearValue;
  194. uint32_t FrameIndex; // Current frame being rendered to (0 <= FrameIndex < FrameInFlightCount)
  195. uint32_t ImageCount; // Number of simultaneous in-flight frames (returned by vkGetSwapchainImagesKHR, usually derived from min_image_count)
  196. uint32_t SemaphoreCount; // Number of simultaneous in-flight frames + 1, to be able to use it in vkAcquireNextImageKHR
  197. uint32_t SemaphoreIndex; // Current set of swapchain wait semaphores we're using (needs to be distinct from per frame data)
  198. ImVector<ImGui_ImplVulkanH_Frame> Frames;
  199. ImVector<ImGui_ImplVulkanH_FrameSemaphores> FrameSemaphores;
  200. ImGui_ImplVulkanH_Window()
  201. {
  202. memset((void*)this, 0, sizeof(*this));
  203. PresentMode = (VkPresentModeKHR)~0; // Ensure we get an error if user doesn't set this.
  204. ClearEnable = true;
  205. }
  206. };
  207. #endif // #ifndef IMGUI_DISABLE