17.9 KB、287行 生 Blame 履歴
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 a VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE 'VkDescriptorSet' as texture identifier. Call ImGui_ImplVulkan_AddTexture() to register one. Read the FAQ about ImTextureID/ImTextureRef + 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: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
8// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
9// [x] Renderer: Multi-viewport / platform windows. With issues (flickering when creating a new viewport).
10
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
14// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
15// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
16// Learn about Dear ImGui:
17// - FAQ https://dearimgui.com/faq
18// - Getting Started https://dearimgui.com/getting-started
19// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
20// - Introduction, links and more at the top of imgui.cpp
21
22// Important note to the reader who wish to integrate imgui_impl_vulkan.cpp/.h in their own engine/app.
23// - Common ImGui_ImplVulkan_XXX functions and structures are used to interface with imgui_impl_vulkan.cpp/.h.
24// You will use those if you want to use this rendering backend in your engine/app.
25// - Helper ImGui_ImplVulkanH_XXX functions and structures are only used by this example (main.cpp) and by
26// the backend itself (imgui_impl_vulkan.cpp), but should PROBABLY NOT be used by your own engine/app code.
27// Read comments in imgui_impl_vulkan.h.
28
29#pragma once
30#ifndef IMGUI_DISABLE
31#include "imgui.h" // IMGUI_IMPL_API
32
33// [Configuration] in order to use a custom Vulkan function loader:
34// (1) You'll need to disable default Vulkan function prototypes.
35// We provide a '#define IMGUI_IMPL_VULKAN_NO_PROTOTYPES' convenience configuration flag.
36// In order to make sure this is visible from the imgui_impl_vulkan.cpp compilation unit:
37// - Add '#define IMGUI_IMPL_VULKAN_NO_PROTOTYPES' in your imconfig.h file
38// - Or as a compilation flag in your build system
39// - Or uncomment here (not recommended because you'd be modifying imgui sources!)
40// - Do not simply add it in a .cpp file!
41// (2) Call ImGui_ImplVulkan_LoadFunctions() before ImGui_ImplVulkan_Init() with your custom function.
42// If you have no idea what this is, leave it alone!
43//#define IMGUI_IMPL_VULKAN_NO_PROTOTYPES
44
45// [Configuration] Convenience support for Volk
46// (you can also technically use IMGUI_IMPL_VULKAN_NO_PROTOTYPES + wrap Volk via ImGui_ImplVulkan_LoadFunctions().)
47// (When using Volk from directory outside your include directories list you can specify full path to the volk.h header,
48// for example when using Volk from VulkanSDK and using include_directories(${Vulkan_INCLUDE_DIRS})' from 'find_package(Vulkan REQUIRED)')
49//#define IMGUI_IMPL_VULKAN_USE_VOLK
50//#define IMGUI_IMPL_VULKAN_VOLK_FILENAME <Volk/volk.h>
51//#define IMGUI_IMPL_VULKAN_VOLK_FILENAME <volk.h> // Default
52// Reminder: make those changes in your imconfig.h file, not here!
53
54// Clang/GCC warnings with -Weverything
55#if defined(__clang__)
56#pragma clang diagnostic push
57#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast
58#endif
59
60#if defined(IMGUI_IMPL_VULKAN_NO_PROTOTYPES) && !defined(VK_NO_PROTOTYPES)
61#define VK_NO_PROTOTYPES
62#endif
63#if defined(VK_USE_PLATFORM_WIN32_KHR) && !defined(NOMINMAX)
64#define NOMINMAX
65#endif
66
67// Vulkan includes
68#ifdef IMGUI_IMPL_VULKAN_USE_VOLK
69#ifdef IMGUI_IMPL_VULKAN_VOLK_FILENAME
70#include IMGUI_IMPL_VULKAN_VOLK_FILENAME
71#else
72#include <volk.h>
73#endif
74#else
75#include <vulkan/vulkan.h>
76#endif
77#if defined(VK_VERSION_1_3) || defined(VK_KHR_dynamic_rendering)
78#define IMGUI_IMPL_VULKAN_HAS_DYNAMIC_RENDERING
79#endif
80
81// Backend uses a small number of descriptors per font atlas + as many as additional calls done to ImGui_ImplVulkan_AddTexture().
82#define IMGUI_IMPL_VULKAN_MINIMUM_SAMPLED_IMAGE_POOL_SIZE (8) // Minimum per atlas
83#define IMGUI_IMPL_VULKAN_MINIMUM_SAMPLER_POOL_SIZE (2) // Minimum for linear + nearest
84
85// Specify settings to create pipeline and swapchain
86struct ImGui_ImplVulkan_PipelineInfo
87{
88 // For Main viewport only
89 VkRenderPass RenderPass; // Ignored if using dynamic rendering
90
91 // For Main and Secondary viewports
92 uint32_t Subpass; //
93 VkSampleCountFlagBits MSAASamples = {}; // 0 defaults to VK_SAMPLE_COUNT_1_BIT
94 ImVector<VkDynamicState> ExtraDynamicStates; // Optional, allows to insert more dynamic states into our VkPipeline
95#ifdef IMGUI_IMPL_VULKAN_HAS_DYNAMIC_RENDERING
96 VkPipelineRenderingCreateInfoKHR PipelineRenderingCreateInfo; // Optional, valid if .sType == VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR
97#endif
98
99 // For Secondary viewports only (created/managed by backend)
100 VkImageUsageFlags SwapChainImageUsage; // Extra flags for vkCreateSwapchainKHR() calls for secondary viewports. We automatically add VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT. You can add e.g. VK_IMAGE_USAGE_TRANSFER_SRC_BIT if you need to capture from viewports.
101};
102
103// Initialization data, for ImGui_ImplVulkan_Init()
104// [Please zero-clear before use!]
105// - About descriptor pool:
106// - A VkDescriptorPool should be created with VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT,
107// and must contain a pool size large enough to hold a small number of VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER descriptors.
108// - As an convenience, by setting DescriptorPoolSize > 0 the backend will create one for you.
109// - About dynamic rendering:
110// - When using dynamic rendering, set UseDynamicRendering=true + fill PipelineInfoMain.PipelineRenderingCreateInfo structure.
111struct ImGui_ImplVulkan_InitInfo
112{
113 uint32_t ApiVersion; // Fill with API version of Instance, e.g. VK_API_VERSION_1_3 or your value of VkApplicationInfo::apiVersion. May be lower than header version (VK_HEADER_VERSION_COMPLETE)
114 VkInstance Instance;
115 VkPhysicalDevice PhysicalDevice;
116 VkDevice Device;
117 uint32_t QueueFamily;
118 VkQueue Queue;
119 VkDescriptorPool DescriptorPool; // See requirements in note above; ignored if using DescriptorPoolSize > 0
120 uint32_t DescriptorPoolSize; // Optional: set to create internal ImageView descriptor pool automatically instead of using DescriptorPool.
121 uint32_t MinImageCount; // >= 2
122 uint32_t ImageCount; // >= MinImageCount
123 VkPipelineCache PipelineCache; // Optional
124
125 // Pipeline
126 ImGui_ImplVulkan_PipelineInfo PipelineInfoMain; // Infos for Main Viewport (created by app/user)
127 ImGui_ImplVulkan_PipelineInfo PipelineInfoForViewports; // Infos for Secondary Viewports (created by backend)
128 //VkRenderPass RenderPass; // --> Since 2025/09/26: set 'PipelineInfoMain.RenderPass' instead
129 //uint32_t Subpass; // --> Since 2025/09/26: set 'PipelineInfoMain.Subpass' instead
130 //VkSampleCountFlagBits MSAASamples; // --> Since 2025/09/26: set 'PipelineInfoMain.MSAASamples' instead
131 //VkPipelineRenderingCreateInfoKHR PipelineRenderingCreateInfo; // Since 2025/09/26: set 'PipelineInfoMain.PipelineRenderingCreateInfo' instead
132
133 // (Optional) Dynamic Rendering
134 // Need to explicitly enable VK_KHR_dynamic_rendering extension to use this, even for Vulkan 1.3 + setup PipelineInfoMain.PipelineRenderingCreateInfo and PipelineInfoViewports.PipelineRenderingCreateInfo.
135 bool UseDynamicRendering;
136
137 // (Optional) Allocation, Debugging
138 const VkAllocationCallbacks* Allocator;
139 void (*CheckVkResultFn)(VkResult err);
140 VkDeviceSize MinAllocationSize; // Minimum allocation size. Set to 1024*1024 to satisfy zealous best practices validation layer and waste a little memory.
141
142 // (Optional) Customize default vertex/fragment shaders.
143 // - if .sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO we use specified structs, otherwise we use defaults.
144 // - Shader inputs/outputs need to match ours. Code/data pointed to by the structure needs to survive for whole during of backend usage.
145 VkShaderModuleCreateInfo CustomShaderVertCreateInfo;
146 VkShaderModuleCreateInfo CustomShaderFragCreateInfo;
147};
148
149// Follow "Getting Started" link and check examples/ folder to learn about using backends!
150IMGUI_IMPL_API bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info);
151IMGUI_IMPL_API void ImGui_ImplVulkan_Shutdown();
152IMGUI_IMPL_API void ImGui_ImplVulkan_NewFrame();
153IMGUI_IMPL_API void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer command_buffer, VkPipeline pipeline = VK_NULL_HANDLE);
154IMGUI_IMPL_API void ImGui_ImplVulkan_SetMinImageCount(uint32_t min_image_count); // To override MinImageCount after initialization (e.g. if swap chain is recreated)
155
156// (Advanced) Use e.g. if you need to recreate pipeline without reinitializing the backend (see #8110, #8111)
157// The main window pipeline will be created by ImGui_ImplVulkan_Init() if possible (== RenderPass xor (UseDynamicRendering && PipelineRenderingCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR))
158// Else, the pipeline can be created, or re-created, using ImGui_ImplVulkan_CreateMainPipeline() before rendering.
159IMGUI_IMPL_API void ImGui_ImplVulkan_CreateMainPipeline(const ImGui_ImplVulkan_PipelineInfo* info);
160
161// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = nullptr to handle this manually.
162IMGUI_IMPL_API void ImGui_ImplVulkan_UpdateTexture(ImTextureData* tex);
163
164// Register a texture (VkDescriptorSet for a VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE == ImTextureID)
165IMGUI_IMPL_API VkDescriptorSet ImGui_ImplVulkan_AddTexture(VkImageView image_view, VkImageLayout image_layout);
166IMGUI_IMPL_API void ImGui_ImplVulkan_RemoveTexture(VkDescriptorSet descriptor_set);
167
168#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
169IMGUI_IMPL_API VkDescriptorSet ImGui_ImplVulkan_AddTexture(VkSampler sampler, VkImageView image_view, VkImageLayout image_layout); // Ignore VkSampler
170#endif
171
172// Optional: load Vulkan functions with a custom function loader
173// This is only useful with IMGUI_IMPL_VULKAN_NO_PROTOTYPES / VK_NO_PROTOTYPES
174IMGUI_IMPL_API bool ImGui_ImplVulkan_LoadFunctions(uint32_t api_version, PFN_vkVoidFunction(*loader_func)(const char* function_name, void* user_data), void* user_data = nullptr);
175
176// [BETA] Selected render state data shared with callbacks.
177// This is temporarily stored in GetPlatformIO().Renderer_RenderState during the ImGui_ImplVulkan_RenderDrawData() call.
178// ImGui_ImplVulkan_RenderState* render_state = (ImGui_ImplVulkan_RenderState*)ImGui::GetPlatformIO().Renderer_RenderState;
179// (Please open an issue if you feel you need access to more data)
180struct ImGui_ImplVulkan_RenderState
181{
182 VkCommandBuffer CommandBuffer;
183 VkPipeline Pipeline;
184 VkPipelineLayout PipelineLayout;
185};
186
187//-------------------------------------------------------------------------
188// Internal / Miscellaneous Vulkan Helpers
189//-------------------------------------------------------------------------
190// Used by example's main.cpp. Used by multi-viewport features. PROBABLY NOT used by your own engine/app.
191//
192// You probably do NOT need to use or care about those functions.
193// WE DO NOT PROVIDE STRONG GUARANTEES OF BACKWARD/FORWARD COMPATIBILITY.
194// Those functions only exist because:
195// 1) they facilitate the readability and maintenance of the multiple main.cpp examples files.
196// 2) the multi-viewport / platform window implementation needs them internally.
197// Generally we avoid exposing any kind of superfluous high-level helpers in the backends,
198// but it is too much code to duplicate everywhere so we exceptionally expose them.
199//
200// Your engine/app will likely _already_ have code to setup all that stuff (swap chain,
201// render pass, frame buffers, etc.). You may read this code if you are curious, but
202// it is recommended you use your own custom tailored code to do equivalent work.
203//
204// The ImGui_ImplVulkanH_XXX functions should NOT interact with any of the state used
205// by the regular ImGui_ImplVulkan_XXX functions.
206//-------------------------------------------------------------------------
207
208struct ImGui_ImplVulkanH_Frame;
209struct ImGui_ImplVulkanH_Window;
210
211// Helpers
212IMGUI_IMPL_API void ImGui_ImplVulkanH_CreateOrResizeWindow(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count, VkImageUsageFlags image_usage);
213IMGUI_IMPL_API void ImGui_ImplVulkanH_DestroyWindow(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_Window* wd, const VkAllocationCallbacks* allocator);
214IMGUI_IMPL_API VkSurfaceFormatKHR ImGui_ImplVulkanH_SelectSurfaceFormat(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkFormat* request_formats, int request_formats_count, VkColorSpaceKHR request_color_space);
215IMGUI_IMPL_API VkPresentModeKHR ImGui_ImplVulkanH_SelectPresentMode(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkPresentModeKHR* request_modes, int request_modes_count);
216IMGUI_IMPL_API VkPhysicalDevice ImGui_ImplVulkanH_SelectPhysicalDevice(VkInstance instance);
217IMGUI_IMPL_API uint32_t ImGui_ImplVulkanH_SelectQueueFamilyIndex(VkPhysicalDevice physical_device);
218IMGUI_IMPL_API int ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(VkPresentModeKHR present_mode);
219IMGUI_IMPL_API ImGui_ImplVulkanH_Window* ImGui_ImplVulkanH_GetWindowDataFromViewport(ImGuiViewport* viewport); // Access to Vulkan objects associated with a viewport (e.g to export a screenshot)
220
221// Helper structure to hold the data needed by one rendering frame
222// (Used by example's main.cpp. Used by multi-viewport features. Probably NOT used by your own engine/app.)
223// [Please zero-clear before use!]
224struct ImGui_ImplVulkanH_Frame
225{
226 VkCommandPool CommandPool;
227 VkCommandBuffer CommandBuffer;
228 VkFence Fence;
229 VkImage Backbuffer;
230 VkImageView BackbufferView;
231 VkFramebuffer Framebuffer;
232};
233
234struct ImGui_ImplVulkanH_FrameSemaphores
235{
236 VkSemaphore ImageAcquiredSemaphore;
237 VkSemaphore RenderCompleteSemaphore;
238};
239
240// Helper structure to hold the data needed by one rendering context into one OS window
241// (Used by example's main.cpp. Used by multi-viewport features. Probably NOT used by your own engine/app.)
242struct ImGui_ImplVulkanH_Window
243{
244 // Input
245 bool UseDynamicRendering;
246 VkSurfaceKHR Surface; // Surface created and destroyed by caller.
247 VkSurfaceFormatKHR SurfaceFormat;
248 VkPresentModeKHR PresentMode;
249 VkAttachmentDescription AttachmentDesc; // RenderPass creation: main attachment description.
250 VkClearValue ClearValue; // RenderPass creation: clear value when using VK_ATTACHMENT_LOAD_OP_CLEAR.
251
252 // Internal
253 int Width; // Generally same as passed to ImGui_ImplVulkanH_CreateOrResizeWindow()
254 int Height;
255 VkSwapchainKHR Swapchain;
256 VkRenderPass RenderPass;
257 uint32_t FrameIndex; // Current frame being rendered to (0 <= FrameIndex < FrameInFlightCount)
258 uint32_t ImageCount; // Number of simultaneous in-flight frames (returned by vkGetSwapchainImagesKHR, usually derived from min_image_count)
259 uint32_t SemaphoreCount; // Number of simultaneous in-flight frames + 1, to be able to use it in vkAcquireNextImageKHR
260 uint32_t SemaphoreIndex; // Current set of swapchain wait semaphores we're using (needs to be distinct from per frame data)
261 ImVector<ImGui_ImplVulkanH_Frame> Frames;
262 ImVector<ImGui_ImplVulkanH_FrameSemaphores> FrameSemaphores;
263
264 ImGui_ImplVulkanH_Window()
265 {
266 memset((void*)this, 0, sizeof(*this));
267
268 // Parameters to create SwapChain
269 PresentMode = VK_PRESENT_MODE_MAX_ENUM_KHR; // Ensure we get an error if user doesn't set this.
270
271 // Parameters to create RenderPass
272 AttachmentDesc.format = VK_FORMAT_UNDEFINED; // Will automatically use wd->SurfaceFormat.format.
273 AttachmentDesc.samples = VK_SAMPLE_COUNT_1_BIT;
274 AttachmentDesc.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
275 AttachmentDesc.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
276 AttachmentDesc.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
277 AttachmentDesc.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
278 AttachmentDesc.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
279 AttachmentDesc.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
280 }
281};
282
283#if defined(__clang__)
284#pragma clang diagnostic pop
285#endif
286
287#endif // #ifndef IMGUI_DISABLE