imgui_impl_vulkan.h 9.9 KB

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