imgui_impl_vulkan.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. // [x] Renderer: User texture binding. Use 'VkDescriptorSet' as ImTextureID. Read the FAQ about ImTextureID! See https://github.com/ocornut/imgui/pull/914 for discussions.
  5. // [X] Renderer: Large meshes support (64k+ vertices) with 16-bit indices.
  6. // [x] Renderer: Multi-viewport / platform windows. With issues (flickering when creating a new viewport).
  7. // Important: on 32-bit systems, user texture binding is only supported if your imconfig file has '#define ImTextureID ImU64'.
  8. // See imgui_impl_vulkan.cpp file for details.
  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. // Vulkan includes
  39. #if defined(IMGUI_IMPL_VULKAN_NO_PROTOTYPES) && !defined(VK_NO_PROTOTYPES)
  40. #define VK_NO_PROTOTYPES
  41. #endif
  42. #include <vulkan/vulkan.h>
  43. // Initialization data, for ImGui_ImplVulkan_Init()
  44. // [Please zero-clear before use!]
  45. struct ImGui_ImplVulkan_InitInfo
  46. {
  47. VkInstance Instance;
  48. VkPhysicalDevice PhysicalDevice;
  49. VkDevice Device;
  50. uint32_t QueueFamily;
  51. VkQueue Queue;
  52. VkPipelineCache PipelineCache;
  53. VkDescriptorPool DescriptorPool;
  54. uint32_t Subpass;
  55. uint32_t MinImageCount; // >= 2
  56. uint32_t ImageCount; // >= MinImageCount
  57. VkSampleCountFlagBits MSAASamples; // >= VK_SAMPLE_COUNT_1_BIT (0 -> default to VK_SAMPLE_COUNT_1_BIT)
  58. // Dynamic Rendering (Optional)
  59. bool UseDynamicRendering; // Need to explicitly enable VK_KHR_dynamic_rendering extension to use this, even for Vulkan 1.3.
  60. VkFormat ColorAttachmentFormat; // Required for dynamic rendering
  61. // Allocation, Debugging
  62. const VkAllocationCallbacks* Allocator;
  63. void (*CheckVkResultFn)(VkResult err);
  64. VkDeviceSize MinAllocationSize; // Minimum allocation size. Set to 1024*1024 to satisfy zealous best practices validation layer and waste a little memory.
  65. };
  66. // Called by user code
  67. IMGUI_IMPL_API bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass render_pass);
  68. IMGUI_IMPL_API void ImGui_ImplVulkan_Shutdown();
  69. IMGUI_IMPL_API void ImGui_ImplVulkan_NewFrame();
  70. IMGUI_IMPL_API void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer command_buffer, VkPipeline pipeline = VK_NULL_HANDLE);
  71. IMGUI_IMPL_API bool ImGui_ImplVulkan_CreateFontsTexture();
  72. IMGUI_IMPL_API void ImGui_ImplVulkan_DestroyFontsTexture();
  73. IMGUI_IMPL_API void ImGui_ImplVulkan_SetMinImageCount(uint32_t min_image_count); // To override MinImageCount after initialization (e.g. if swap chain is recreated)
  74. // Register a texture (VkDescriptorSet == ImTextureID)
  75. // FIXME: This is experimental in the sense that we are unsure how to best design/tackle this problem
  76. // Please post to https://github.com/ocornut/imgui/pull/914 if you have suggestions.
  77. IMGUI_IMPL_API VkDescriptorSet ImGui_ImplVulkan_AddTexture(VkSampler sampler, VkImageView image_view, VkImageLayout image_layout);
  78. IMGUI_IMPL_API void ImGui_ImplVulkan_RemoveTexture(VkDescriptorSet descriptor_set);
  79. // Optional: load Vulkan functions with a custom function loader
  80. // This is only useful with IMGUI_IMPL_VULKAN_NO_PROTOTYPES / VK_NO_PROTOTYPES
  81. IMGUI_IMPL_API bool ImGui_ImplVulkan_LoadFunctions(PFN_vkVoidFunction(*loader_func)(const char* function_name, void* user_data), void* user_data = nullptr);
  82. //-------------------------------------------------------------------------
  83. // Internal / Miscellaneous Vulkan Helpers
  84. // (Used by example's main.cpp. Used by multi-viewport features. PROBABLY NOT used by your own engine/app.)
  85. //-------------------------------------------------------------------------
  86. // You probably do NOT need to use or care about those functions.
  87. // Those functions only exist because:
  88. // 1) they facilitate the readability and maintenance of the multiple main.cpp examples files.
  89. // 2) the multi-viewport / platform window implementation needs them internally.
  90. // Generally we avoid exposing any kind of superfluous high-level helpers in the bindings,
  91. // but it is too much code to duplicate everywhere so we exceptionally expose them.
  92. //
  93. // Your engine/app will likely _already_ have code to setup all that stuff (swap chain, render pass, frame buffers, etc.).
  94. // You may read this code to learn about Vulkan, but it is recommended you use you own custom tailored code to do equivalent work.
  95. // (The ImGui_ImplVulkanH_XXX functions do not interact with any of the state used by the regular ImGui_ImplVulkan_XXX functions)
  96. //-------------------------------------------------------------------------
  97. struct ImGui_ImplVulkanH_Frame;
  98. struct ImGui_ImplVulkanH_Window;
  99. // Helpers
  100. 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);
  101. IMGUI_IMPL_API void ImGui_ImplVulkanH_DestroyWindow(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_Window* wnd, const VkAllocationCallbacks* allocator);
  102. IMGUI_IMPL_API VkSurfaceFormatKHR ImGui_ImplVulkanH_SelectSurfaceFormat(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkFormat* request_formats, int request_formats_count, VkColorSpaceKHR request_color_space);
  103. IMGUI_IMPL_API VkPresentModeKHR ImGui_ImplVulkanH_SelectPresentMode(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkPresentModeKHR* request_modes, int request_modes_count);
  104. IMGUI_IMPL_API int ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(VkPresentModeKHR present_mode);
  105. // Helper structure to hold the data needed by one rendering frame
  106. // (Used by example's main.cpp. Used by multi-viewport features. Probably NOT used by your own engine/app.)
  107. // [Please zero-clear before use!]
  108. struct ImGui_ImplVulkanH_Frame
  109. {
  110. VkCommandPool CommandPool;
  111. VkCommandBuffer CommandBuffer;
  112. VkFence Fence;
  113. VkImage Backbuffer;
  114. VkImageView BackbufferView;
  115. VkFramebuffer Framebuffer;
  116. };
  117. struct ImGui_ImplVulkanH_FrameSemaphores
  118. {
  119. VkSemaphore ImageAcquiredSemaphore;
  120. VkSemaphore RenderCompleteSemaphore;
  121. };
  122. // Helper structure to hold the data needed by one rendering context into one OS window
  123. // (Used by example's main.cpp. Used by multi-viewport features. Probably NOT used by your own engine/app.)
  124. struct ImGui_ImplVulkanH_Window
  125. {
  126. int Width;
  127. int Height;
  128. VkSwapchainKHR Swapchain;
  129. VkSurfaceKHR Surface;
  130. VkSurfaceFormatKHR SurfaceFormat;
  131. VkPresentModeKHR PresentMode;
  132. VkRenderPass RenderPass;
  133. VkPipeline Pipeline; // The window pipeline may uses a different VkRenderPass than the one passed in ImGui_ImplVulkan_InitInfo
  134. bool UseDynamicRendering;
  135. bool ClearEnable;
  136. VkClearValue ClearValue;
  137. uint32_t FrameIndex; // Current frame being rendered to (0 <= FrameIndex < FrameInFlightCount)
  138. uint32_t ImageCount; // Number of simultaneous in-flight frames (returned by vkGetSwapchainImagesKHR, usually derived from min_image_count)
  139. uint32_t SemaphoreIndex; // Current set of swapchain wait semaphores we're using (needs to be distinct from per frame data)
  140. ImGui_ImplVulkanH_Frame* Frames;
  141. ImGui_ImplVulkanH_FrameSemaphores* FrameSemaphores;
  142. ImGui_ImplVulkanH_Window()
  143. {
  144. memset((void*)this, 0, sizeof(*this));
  145. PresentMode = (VkPresentModeKHR)~0; // Ensure we get an error if user doesn't set this.
  146. ClearEnable = true;
  147. }
  148. };
  149. #endif // #ifndef IMGUI_DISABLE