| 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 |
|
| 4 |
// Implemented features: |
| 5 |
// [!] Renderer: User texture binding. Use 'VkDescriptorSet' as ImTextureID. Call ImGui_ImplVulkan_AddTexture() to register one. Read the FAQ about ImTextureID! See https://github.com/ocornut/imgui/pull/914 for discussions. |
| 6 |
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset). |
| 7 |
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'. |
| 8 |
|
| 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 |
|
| 12 |
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. |
| 13 |
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. |
| 14 |
// Learn about Dear ImGui: |
| 15 |
// - FAQ https://dearimgui.com/faq |
| 16 |
// - Getting Started https://dearimgui.com/getting-started |
| 17 |
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder). |
| 18 |
// - Introduction, links and more at the top of imgui.cpp |
| 19 |
|
| 20 |
// Important note to the reader who wish to integrate imgui_impl_vulkan.cpp/.h in their own engine/app. |
| 21 |
// - Common ImGui_ImplVulkan_XXX functions and structures are used to interface with imgui_impl_vulkan.cpp/.h. |
| 22 |
// You will use those if you want to use this rendering backend in your engine/app. |
| 23 |
// - Helper ImGui_ImplVulkanH_XXX functions and structures are only used by this example (main.cpp) and by |
| 24 |
// the backend itself (imgui_impl_vulkan.cpp), but should PROBABLY NOT be used by your own engine/app code. |
| 25 |
// Read comments in imgui_impl_vulkan.h. |
| 26 |
|
| 27 |
#pragma once |
| 28 |
#ifndef IMGUI_DISABLE |
| 29 |
#include "imgui.h" // IMGUI_IMPL_API |
| 30 |
|
| 31 |
// [Configuration] in order to use a custom Vulkan function loader: |
| 32 |
// (1) You'll need to disable default Vulkan function prototypes. |
| 33 |
// We provide a '#define IMGUI_IMPL_VULKAN_NO_PROTOTYPES' convenience configuration flag. |
| 34 |
// In order to make sure this is visible from the imgui_impl_vulkan.cpp compilation unit: |
| 35 |
// - Add '#define IMGUI_IMPL_VULKAN_NO_PROTOTYPES' in your imconfig.h file |
| 36 |
// - Or as a compilation flag in your build system |
| 37 |
// - Or uncomment here (not recommended because you'd be modifying imgui sources!) |
| 38 |
// - Do not simply add it in a .cpp file! |
| 39 |
// (2) Call ImGui_ImplVulkan_LoadFunctions() before ImGui_ImplVulkan_Init() with your custom function. |
| 40 |
// If you have no idea what this is, leave it alone! |
| 41 |
//#define IMGUI_IMPL_VULKAN_NO_PROTOTYPES |
| 42 |
|
| 43 |
// Convenience support for Volk |
| 44 |
// (you can also technically use IMGUI_IMPL_VULKAN_NO_PROTOTYPES + wrap Volk via ImGui_ImplVulkan_LoadFunctions().) |
| 45 |
//#define IMGUI_IMPL_VULKAN_USE_VOLK |
| 46 |
|
| 47 |
#if defined(IMGUI_IMPL_VULKAN_NO_PROTOTYPES) && !defined(VK_NO_PROTOTYPES) |
| 48 |
#define VK_NO_PROTOTYPES |
| 49 |
#endif |
| 50 |
#if defined(VK_USE_PLATFORM_WIN32_KHR) && !defined(NOMINMAX) |
| 51 |
#define NOMINMAX |
| 52 |
#endif |
| 53 |
|
| 54 |
// Vulkan includes |
| 55 |
#ifdef IMGUI_IMPL_VULKAN_USE_VOLK |
| 56 |
#include <volk.h> |
| 57 |
#else |
| 58 |
#include <vulkan/vulkan.h> |
| 59 |
#endif |
| 60 |
#if defined(VK_VERSION_1_3) || defined(VK_KHR_dynamic_rendering) |
| 61 |
#define IMGUI_IMPL_VULKAN_HAS_DYNAMIC_RENDERING |
| 62 |
#endif |
| 63 |
|
| 64 |
// Initialization data, for ImGui_ImplVulkan_Init() |
| 65 |
// [Please zero-clear before use!] |
| 66 |
// - About descriptor pool: |
| 67 |
// - A VkDescriptorPool should be created with VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, |
| 68 |
// and must contain a pool size large enough to hold a small number of VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER descriptors. |
| 69 |
// - As an convenience, by setting DescriptorPoolSize > 0 the backend will create one for you. |
| 70 |
// - Current version of the backend use 1 descriptor for the font atlas + as many as additional calls done to ImGui_ImplVulkan_AddTexture(). |
| 71 |
// - It is expected that as early as Q1 2025 the backend will use a few more descriptors, so aim at 10 + number of desierd calls to ImGui_ImplVulkan_AddTexture(). |
| 72 |
// - About dynamic rendering: |
| 73 |
// - When using dynamic rendering, set UseDynamicRendering=true and fill PipelineRenderingCreateInfo structure. |
| 74 |
struct ImGui_ImplVulkan_InitInfo |
| 75 |
{ |
| 76 |
VkInstance Instance; |
| 77 |
VkPhysicalDevice PhysicalDevice; |
| 78 |
VkDevice Device; |
| 79 |
uint32_t QueueFamily; |
| 80 |
VkQueue Queue; |
| 81 |
VkDescriptorPool DescriptorPool; // See requirements in note above; ignored if using DescriptorPoolSize > 0 |
| 82 |
VkRenderPass RenderPass; // Ignored if using dynamic rendering |
| 83 |
uint32_t MinImageCount; // >= 2 |
| 84 |
uint32_t ImageCount; // >= MinImageCount |
| 85 |
VkSampleCountFlagBits MSAASamples; // 0 defaults to VK_SAMPLE_COUNT_1_BIT |
| 86 |
|
| 87 |
// (Optional) |
| 88 |
VkPipelineCache PipelineCache; |
| 89 |
uint32_t Subpass; |
| 90 |
|
| 91 |
// (Optional) Set to create internal descriptor pool instead of using DescriptorPool |
| 92 |
uint32_t DescriptorPoolSize; |
| 93 |
|
| 94 |
// (Optional) Dynamic Rendering |
| 95 |
// Need to explicitly enable VK_KHR_dynamic_rendering extension to use this, even for Vulkan 1.3. |
| 96 |
bool UseDynamicRendering; |
| 97 |
#ifdef IMGUI_IMPL_VULKAN_HAS_DYNAMIC_RENDERING |
| 98 |
VkPipelineRenderingCreateInfoKHR PipelineRenderingCreateInfo; |
| 99 |
#endif |
| 100 |
|
| 101 |
// (Optional) Allocation, Debugging |
| 102 |
const VkAllocationCallbacks* Allocator; |
| 103 |
void (*CheckVkResultFn)(VkResult err); |
| 104 |
VkDeviceSize MinAllocationSize; // Minimum allocation size. Set to 1024*1024 to satisfy zealous best practices validation layer and waste a little memory. |
| 105 |
}; |
| 106 |
|
| 107 |
// Follow "Getting Started" link and check examples/ folder to learn about using backends! |
| 108 |
IMGUI_IMPL_API bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info); |
| 109 |
IMGUI_IMPL_API void ImGui_ImplVulkan_Shutdown(); |
| 110 |
IMGUI_IMPL_API void ImGui_ImplVulkan_NewFrame(); |
| 111 |
IMGUI_IMPL_API void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer command_buffer, VkPipeline pipeline = VK_NULL_HANDLE); |
| 112 |
IMGUI_IMPL_API bool ImGui_ImplVulkan_CreateFontsTexture(); |
| 113 |
IMGUI_IMPL_API void ImGui_ImplVulkan_DestroyFontsTexture(); |
| 114 |
IMGUI_IMPL_API void ImGui_ImplVulkan_SetMinImageCount(uint32_t min_image_count); // To override MinImageCount after initialization (e.g. if swap chain is recreated) |
| 115 |
|
| 116 |
// Register a texture (VkDescriptorSet == ImTextureID) |
| 117 |
// FIXME: This is experimental in the sense that we are unsure how to best design/tackle this problem |
| 118 |
// Please post to https://github.com/ocornut/imgui/pull/914 if you have suggestions. |
| 119 |
IMGUI_IMPL_API VkDescriptorSet ImGui_ImplVulkan_AddTexture(VkSampler sampler, VkImageView image_view, VkImageLayout image_layout); |
| 120 |
IMGUI_IMPL_API void ImGui_ImplVulkan_RemoveTexture(VkDescriptorSet descriptor_set); |
| 121 |
|
| 122 |
// Optional: load Vulkan functions with a custom function loader |
| 123 |
// This is only useful with IMGUI_IMPL_VULKAN_NO_PROTOTYPES / VK_NO_PROTOTYPES |
| 124 |
IMGUI_IMPL_API bool ImGui_ImplVulkan_LoadFunctions(PFN_vkVoidFunction(*loader_func)(const char* function_name, void* user_data), void* user_data = nullptr); |
| 125 |
|
| 126 |
// [BETA] Selected render state data shared with callbacks. |
| 127 |
// This is temporarily stored in GetPlatformIO().Renderer_RenderState during the ImGui_ImplVulkan_RenderDrawData() call. |
| 128 |
// (Please open an issue if you feel you need access to more data) |
| 129 |
struct ImGui_ImplVulkan_RenderState |
| 130 |
{ |
| 131 |
VkCommandBuffer CommandBuffer; |
| 132 |
VkPipeline Pipeline; |
| 133 |
VkPipelineLayout PipelineLayout; |
| 134 |
}; |
| 135 |
|
| 136 |
//------------------------------------------------------------------------- |
| 137 |
// Internal / Miscellaneous Vulkan Helpers |
| 138 |
// (Used by example's main.cpp. Used by multi-viewport features. PROBABLY NOT used by your own engine/app.) |
| 139 |
//------------------------------------------------------------------------- |
| 140 |
// You probably do NOT need to use or care about those functions. |
| 141 |
// Those functions only exist because: |
| 142 |
// 1) they facilitate the readability and maintenance of the multiple main.cpp examples files. |
| 143 |
// 2) the multi-viewport / platform window implementation needs them internally. |
| 144 |
// Generally we avoid exposing any kind of superfluous high-level helpers in the bindings, |
| 145 |
// but it is too much code to duplicate everywhere so we exceptionally expose them. |
| 146 |
// |
| 147 |
// Your engine/app will likely _already_ have code to setup all that stuff (swap chain, render pass, frame buffers, etc.). |
| 148 |
// You may read this code to learn about Vulkan, but it is recommended you use you own custom tailored code to do equivalent work. |
| 149 |
// (The ImGui_ImplVulkanH_XXX functions do not interact with any of the state used by the regular ImGui_ImplVulkan_XXX functions) |
| 150 |
//------------------------------------------------------------------------- |
| 151 |
|
| 152 |
struct ImGui_ImplVulkanH_Frame; |
| 153 |
struct ImGui_ImplVulkanH_Window; |
| 154 |
|
| 155 |
// Helpers |
| 156 |
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); |
| 157 |
IMGUI_IMPL_API void ImGui_ImplVulkanH_DestroyWindow(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_Window* wnd, const VkAllocationCallbacks* allocator); |
| 158 |
IMGUI_IMPL_API VkSurfaceFormatKHR ImGui_ImplVulkanH_SelectSurfaceFormat(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkFormat* request_formats, int request_formats_count, VkColorSpaceKHR request_color_space); |
| 159 |
IMGUI_IMPL_API VkPresentModeKHR ImGui_ImplVulkanH_SelectPresentMode(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkPresentModeKHR* request_modes, int request_modes_count); |
| 160 |
IMGUI_IMPL_API int ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(VkPresentModeKHR present_mode); |
| 161 |
|
| 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 |
|
| 175 |
struct ImGui_ImplVulkanH_FrameSemaphores |
| 176 |
{ |
| 177 |
VkSemaphore ImageAcquiredSemaphore; |
| 178 |
VkSemaphore RenderCompleteSemaphore; |
| 179 |
}; |
| 180 |
|
| 181 |
// Helper structure to hold the data needed by one rendering context into one OS window |
| 182 |
// (Used by example's main.cpp. Used by multi-viewport features. Probably NOT used by your own engine/app.) |
| 183 |
struct ImGui_ImplVulkanH_Window |
| 184 |
{ |
| 185 |
int Width; |
| 186 |
int Height; |
| 187 |
VkSwapchainKHR Swapchain; |
| 188 |
VkSurfaceKHR Surface; |
| 189 |
VkSurfaceFormatKHR SurfaceFormat; |
| 190 |
VkPresentModeKHR PresentMode; |
| 191 |
VkRenderPass RenderPass; |
| 192 |
VkPipeline Pipeline; // The window pipeline may uses a different VkRenderPass than the one passed in ImGui_ImplVulkan_InitInfo |
| 193 |
bool UseDynamicRendering; |
| 194 |
bool ClearEnable; |
| 195 |
VkClearValue ClearValue; |
| 196 |
uint32_t FrameIndex; // Current frame being rendered to (0 <= FrameIndex < FrameInFlightCount) |
| 197 |
uint32_t ImageCount; // Number of simultaneous in-flight frames (returned by vkGetSwapchainImagesKHR, usually derived from min_image_count) |
| 198 |
uint32_t SemaphoreCount; // Number of simultaneous in-flight frames + 1, to be able to use it in vkAcquireNextImageKHR |
| 199 |
uint32_t SemaphoreIndex; // Current set of swapchain wait semaphores we're using (needs to be distinct from per frame data) |
| 200 |
ImGui_ImplVulkanH_Frame* Frames; |
| 201 |
ImGui_ImplVulkanH_FrameSemaphores* FrameSemaphores; |
| 202 |
|
| 203 |
ImGui_ImplVulkanH_Window() |
| 204 |
{ |
| 205 |
memset((void*)this, 0, sizeof(*this)); |
| 206 |
PresentMode = (VkPresentModeKHR)~0; // Ensure we get an error if user doesn't set this. |
| 207 |
ClearEnable = true; |
| 208 |
} |
| 209 |
}; |
| 210 |
|
| 211 |
#endif // #ifndef IMGUI_DISABLE |