2
0

imgui_impl_vulkan.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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: Large meshes support (64k+ vertices) with 16-bit indices.
  5. // [x] Renderer: Multi-viewport / platform windows. With issues (flickering when creating a new viewport).
  6. // [!] Renderer: User texture binding. Use 'VkDescriptorSet' as ImTextureID. Read the FAQ about ImTextureID! See https://github.com/ocornut/imgui/pull/914 for discussions.
  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. // 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. // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
  12. // Read online: https://github.com/ocornut/imgui/tree/master/docs
  13. // The aim of imgui_impl_vulkan.h/.cpp is to be usable in your engine without any modification.
  14. // IF YOU FEEL YOU NEED TO MAKE ANY CHANGE TO THIS CODE, please share them and your feedback at https://github.com/ocornut/imgui/
  15. // Important note to the reader who wish to integrate imgui_impl_vulkan.cpp/.h in their own engine/app.
  16. // - Common ImGui_ImplVulkan_XXX functions and structures are used to interface with imgui_impl_vulkan.cpp/.h.
  17. // You will use those if you want to use this rendering backend in your engine/app.
  18. // - Helper ImGui_ImplVulkanH_XXX functions and structures are only used by this example (main.cpp) and by
  19. // the backend itself (imgui_impl_vulkan.cpp), but should PROBABLY NOT be used by your own engine/app code.
  20. // Read comments in imgui_impl_vulkan.h.
  21. #pragma once
  22. #include "imgui.h" // IMGUI_IMPL_API
  23. // [Configuration] in order to use a custom Vulkan function loader:
  24. // (1) You'll need to disable default Vulkan function prototypes.
  25. // We provide a '#define IMGUI_IMPL_VULKAN_NO_PROTOTYPES' convenience configuration flag.
  26. // In order to make sure this is visible from the imgui_impl_vulkan.cpp compilation unit:
  27. // - Add '#define IMGUI_IMPL_VULKAN_NO_PROTOTYPES' in your imconfig.h file
  28. // - Or as a compilation flag in your build system
  29. // - Or uncomment here (not recommended because you'd be modifying imgui sources!)
  30. // - Do not simply add it in a .cpp file!
  31. // (2) Call ImGui_ImplVulkan_LoadFunctions() before ImGui_ImplVulkan_Init() with your custom function.
  32. // If you have no idea what this is, leave it alone!
  33. //#define IMGUI_IMPL_VULKAN_NO_PROTOTYPES
  34. // Vulkan includes
  35. #if defined(IMGUI_IMPL_VULKAN_NO_PROTOTYPES) && !defined(VK_NO_PROTOTYPES)
  36. #define VK_NO_PROTOTYPES
  37. #endif
  38. #include <vulkan/vulkan.h>
  39. // Initialization data, for ImGui_ImplVulkan_Init()
  40. // [Please zero-clear before use!]
  41. struct ImGui_ImplVulkan_InitInfo
  42. {
  43. VkInstance Instance;
  44. VkPhysicalDevice PhysicalDevice;
  45. VkDevice Device;
  46. uint32_t QueueFamily;
  47. VkQueue Queue;
  48. VkPipelineCache PipelineCache;
  49. VkDescriptorPool DescriptorPool;
  50. uint32_t Subpass;
  51. uint32_t MinImageCount; // >= 2
  52. uint32_t ImageCount; // >= MinImageCount
  53. VkSampleCountFlagBits MSAASamples; // >= VK_SAMPLE_COUNT_1_BIT (0 -> default to VK_SAMPLE_COUNT_1_BIT)
  54. const VkAllocationCallbacks* Allocator;
  55. void (*CheckVkResultFn)(VkResult err);
  56. };
  57. // Called by user code
  58. IMGUI_IMPL_API bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass render_pass);
  59. IMGUI_IMPL_API void ImGui_ImplVulkan_Shutdown();
  60. IMGUI_IMPL_API void ImGui_ImplVulkan_NewFrame();
  61. IMGUI_IMPL_API void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer command_buffer, VkPipeline pipeline = VK_NULL_HANDLE);
  62. IMGUI_IMPL_API bool ImGui_ImplVulkan_CreateFontsTexture(VkCommandBuffer command_buffer);
  63. IMGUI_IMPL_API void ImGui_ImplVulkan_DestroyFontUploadObjects();
  64. IMGUI_IMPL_API void ImGui_ImplVulkan_SetMinImageCount(uint32_t min_image_count); // To override MinImageCount after initialization (e.g. if swap chain is recreated)
  65. // Register a texture (VkDescriptorSet == ImTextureID)
  66. // FIXME: This is experimental in the sense that we are unsure how to best design/tackle this problem, please post to https://github.com/ocornut/imgui/pull/914 if you have suggestions.
  67. IMGUI_IMPL_API VkDescriptorSet ImGui_ImplVulkan_AddTexture(VkSampler sampler, VkImageView image_view, VkImageLayout image_layout);
  68. // Optional: load Vulkan functions with a custom function loader
  69. // This is only useful with IMGUI_IMPL_VULKAN_NO_PROTOTYPES / VK_NO_PROTOTYPES
  70. IMGUI_IMPL_API bool ImGui_ImplVulkan_LoadFunctions(PFN_vkVoidFunction(*loader_func)(const char* function_name, void* user_data), void* user_data = NULL);
  71. //-------------------------------------------------------------------------
  72. // Internal / Miscellaneous Vulkan Helpers
  73. // (Used by example's main.cpp. Used by multi-viewport features. PROBABLY NOT used by your own engine/app.)
  74. //-------------------------------------------------------------------------
  75. // You probably do NOT need to use or care about those functions.
  76. // Those functions only exist because:
  77. // 1) they facilitate the readability and maintenance of the multiple main.cpp examples files.
  78. // 2) the multi-viewport / platform window implementation needs them internally.
  79. // Generally we avoid exposing any kind of superfluous high-level helpers in the bindings,
  80. // but it is too much code to duplicate everywhere so we exceptionally expose them.
  81. //
  82. // Your engine/app will likely _already_ have code to setup all that stuff (swap chain, render pass, frame buffers, etc.).
  83. // You may read this code to learn about Vulkan, but it is recommended you use you own custom tailored code to do equivalent work.
  84. // (The ImGui_ImplVulkanH_XXX functions do not interact with any of the state used by the regular ImGui_ImplVulkan_XXX functions)
  85. //-------------------------------------------------------------------------
  86. struct ImGui_ImplVulkanH_Frame;
  87. struct ImGui_ImplVulkanH_Window;
  88. // Helpers
  89. 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);
  90. IMGUI_IMPL_API void ImGui_ImplVulkanH_DestroyWindow(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_Window* wnd, const VkAllocationCallbacks* allocator);
  91. IMGUI_IMPL_API VkSurfaceFormatKHR ImGui_ImplVulkanH_SelectSurfaceFormat(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkFormat* request_formats, int request_formats_count, VkColorSpaceKHR request_color_space);
  92. IMGUI_IMPL_API VkPresentModeKHR ImGui_ImplVulkanH_SelectPresentMode(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkPresentModeKHR* request_modes, int request_modes_count);
  93. IMGUI_IMPL_API int ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(VkPresentModeKHR present_mode);
  94. // Helper structure to hold the data needed by one rendering frame
  95. // (Used by example's main.cpp. Used by multi-viewport features. Probably NOT used by your own engine/app.)
  96. // [Please zero-clear before use!]
  97. struct ImGui_ImplVulkanH_Frame
  98. {
  99. VkCommandPool CommandPool;
  100. VkCommandBuffer CommandBuffer;
  101. VkFence Fence;
  102. VkImage Backbuffer;
  103. VkImageView BackbufferView;
  104. VkFramebuffer Framebuffer;
  105. };
  106. struct ImGui_ImplVulkanH_FrameSemaphores
  107. {
  108. VkSemaphore ImageAcquiredSemaphore;
  109. VkSemaphore RenderCompleteSemaphore;
  110. };
  111. // Helper structure to hold the data needed by one rendering context into one OS window
  112. // (Used by example's main.cpp. Used by multi-viewport features. Probably NOT used by your own engine/app.)
  113. struct ImGui_ImplVulkanH_Window
  114. {
  115. int Width;
  116. int Height;
  117. VkSwapchainKHR Swapchain;
  118. VkSurfaceKHR Surface;
  119. VkSurfaceFormatKHR SurfaceFormat;
  120. VkPresentModeKHR PresentMode;
  121. VkRenderPass RenderPass;
  122. VkPipeline Pipeline; // The window pipeline may uses a different VkRenderPass than the one passed in ImGui_ImplVulkan_InitInfo
  123. bool ClearEnable;
  124. VkClearValue ClearValue;
  125. uint32_t FrameIndex; // Current frame being rendered to (0 <= FrameIndex < FrameInFlightCount)
  126. uint32_t ImageCount; // Number of simultaneous in-flight frames (returned by vkGetSwapchainImagesKHR, usually derived from min_image_count)
  127. uint32_t SemaphoreIndex; // Current set of swapchain wait semaphores we're using (needs to be distinct from per frame data)
  128. ImGui_ImplVulkanH_Frame* Frames;
  129. ImGui_ImplVulkanH_FrameSemaphores* FrameSemaphores;
  130. ImGui_ImplVulkanH_Window()
  131. {
  132. memset((void*)this, 0, sizeof(*this));
  133. PresentMode = (VkPresentModeKHR)~0; // Ensure we get an error if user doesn't set this.
  134. ClearEnable = true;
  135. }
  136. };