imgui_impl_vulkan.h 8.6 KB

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