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 // [X] Renderer: Support for large meshes (64k+ vertices) with 16-bit indices.
6 // Missing features:
7 // [ ] Renderer: User texture binding. Changes of ImTextureID aren't supported by this backend! See https://github.com/ocornut/imgui/pull/914
8
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
14 // The aim of imgui_impl_vulkan.h/.cpp is to be usable in your engine without any modification.
15 // IF YOU FEEL YOU NEED TO MAKE ANY CHANGE TO THIS CODE, please share them and your feedback at https://github.com/ocornut/imgui/
16
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
24 // CHANGELOG
25 // (minor and older changes stripped away, please see git history for details)
26 // 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX).
27 // 2021-03-22: Vulkan: Fix mapped memory validation error when buffer sizes are not multiple of VkPhysicalDeviceLimits::nonCoherentAtomSize.
28 // 2021-02-18: Vulkan: Change blending equation to preserve alpha in output buffer.
29 // 2021-01-27: Vulkan: Added support for custom function load and IMGUI_IMPL_VULKAN_NO_PROTOTYPES by using ImGui_ImplVulkan_LoadFunctions().
30 // 2020-11-11: Vulkan: Added support for specifying which subpass to reference during VkPipeline creation.
31 // 2020-09-07: Vulkan: Added VkPipeline parameter to ImGui_ImplVulkan_RenderDrawData (default to one passed to ImGui_ImplVulkan_Init).
32 // 2020-05-04: Vulkan: Fixed crash if initial frame has no vertices.
33 // 2020-04-26: Vulkan: Fixed edge case where render callbacks wouldn't be called if the ImDrawData didn't have vertices.
34 // 2019-08-01: Vulkan: Added support for specifying multisample count. Set ImGui_ImplVulkan_InitInfo::MSAASamples to one of the VkSampleCountFlagBits values to use, default is non-multisampled as before.
35 // 2019-05-29: Vulkan: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_RendererHasVtxOffset flag.
36 // 2019-04-30: Vulkan: Added support for special ImDrawCallback_ResetRenderState callback to reset render state.
37 // 2019-04-04: *BREAKING CHANGE*: Vulkan: Added ImageCount/MinImageCount fields in ImGui_ImplVulkan_InitInfo, required for initialization (was previously a hard #define IMGUI_VK_QUEUED_FRAMES 2). Added ImGui_ImplVulkan_SetMinImageCount().
38 // 2019-04-04: Vulkan: Added VkInstance argument to ImGui_ImplVulkanH_CreateWindow() optional helper.
39 // 2019-04-04: Vulkan: Avoid passing negative coordinates to vkCmdSetScissor, which debug validation layers do not like.
40 // 2019-04-01: Vulkan: Support for 32-bit index buffer (#define ImDrawIdx unsigned int).
41 // 2019-02-16: Vulkan: Viewport and clipping rectangles correctly using draw_data->FramebufferScale to allow retina display.
42 // 2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window.
43 // 2018-08-25: Vulkan: Fixed mishandled VkSurfaceCapabilitiesKHR::maxImageCount=0 case.
44 // 2018-06-22: Inverted the parameters to ImGui_ImplVulkan_RenderDrawData() to be consistent with other backends.
45 // 2018-06-08: Misc: Extracted imgui_impl_vulkan.cpp/.h away from the old combined GLFW+Vulkan example.
46 // 2018-06-08: Vulkan: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle.
47 // 2018-03-03: Vulkan: Various refactor, created a couple of ImGui_ImplVulkanH_XXX helper that the example can use and that viewport support will use.
48 // 2018-03-01: Vulkan: Renamed ImGui_ImplVulkan_Init_Info to ImGui_ImplVulkan_InitInfo and fields to match more closely Vulkan terminology.
49 // 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback, ImGui_ImplVulkan_Render() calls ImGui_ImplVulkan_RenderDrawData() itself.
50 // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
51 // 2017-05-15: Vulkan: Fix scissor offset being negative. Fix new Vulkan validation warnings. Set required depth member for buffer image copy.
52 // 2016-11-13: Vulkan: Fix validation layer warnings and errors and redeclare gl_PerVertex.
53 // 2016-10-18: Vulkan: Add location decorators & change to use structs as in/out in glsl, update embedded spv (produced with glslangValidator -x). Null the released resources.
54 // 2016-08-27: Vulkan: Fix Vulkan example for use when a depth buffer is active.
55
56 #include "imgui_impl_vulkan.h"
57 #include <stdio.h>
58
59 // Reusable buffers used for rendering 1 current in-flight frame, for ImGui_ImplVulkan_RenderDrawData()
60 // [Please zero-clear before use!]
61 struct ImGui_ImplVulkanH_FrameRenderBuffers
62 {
63 VkDeviceMemory VertexBufferMemory;
64 VkDeviceMemory IndexBufferMemory;
65 VkDeviceSize VertexBufferSize;
66 VkDeviceSize IndexBufferSize;
67 VkBuffer VertexBuffer;
68 VkBuffer IndexBuffer;
69 };
70
71 // Each viewport will hold 1 ImGui_ImplVulkanH_WindowRenderBuffers
72 // [Please zero-clear before use!]
73 struct ImGui_ImplVulkanH_WindowRenderBuffers
74 {
75 uint32_t Index;
76 uint32_t Count;
77 ImGui_ImplVulkanH_FrameRenderBuffers* FrameRenderBuffers;
78 };
79
80 // Vulkan data
81 struct ImGui_ImplVulkan_Data
82 {
83 ImGui_ImplVulkan_InitInfo VulkanInitInfo;
84 VkRenderPass RenderPass;
85 VkDeviceSize BufferMemoryAlignment;
86 VkPipelineCreateFlags PipelineCreateFlags;
87 VkDescriptorSetLayout DescriptorSetLayout;
88 VkPipelineLayout PipelineLayout;
89 VkDescriptorSet DescriptorSet;
90 VkPipeline Pipeline;
91 uint32_t Subpass;
92 VkShaderModule ShaderModuleVert;
93 VkShaderModule ShaderModuleFrag;
94
95 // Font data
96 VkSampler FontSampler;
97 VkDeviceMemory FontMemory;
98 VkImage FontImage;
99 VkImageView FontView;
100 VkDeviceMemory UploadBufferMemory;
101 VkBuffer UploadBuffer;
102
103 // Render buffers
104 ImGui_ImplVulkanH_WindowRenderBuffers MainWindowRenderBuffers;
105
ImGui_ImplVulkan_DataImGui_ImplVulkan_Data106 ImGui_ImplVulkan_Data()
107 {
108 memset(this, 0, sizeof(*this));
109 BufferMemoryAlignment = 256;
110 }
111 };
112
113 // Forward Declarations
114 bool ImGui_ImplVulkan_CreateDeviceObjects();
115 void ImGui_ImplVulkan_DestroyDeviceObjects();
116 void ImGui_ImplVulkanH_DestroyFrame(VkDevice device, ImGui_ImplVulkanH_Frame* fd, const VkAllocationCallbacks* allocator);
117 void ImGui_ImplVulkanH_DestroyFrameSemaphores(VkDevice device, ImGui_ImplVulkanH_FrameSemaphores* fsd, const VkAllocationCallbacks* allocator);
118 void ImGui_ImplVulkanH_DestroyFrameRenderBuffers(VkDevice device, ImGui_ImplVulkanH_FrameRenderBuffers* buffers, const VkAllocationCallbacks* allocator);
119 void ImGui_ImplVulkanH_DestroyWindowRenderBuffers(VkDevice device, ImGui_ImplVulkanH_WindowRenderBuffers* buffers, const VkAllocationCallbacks* allocator);
120 void ImGui_ImplVulkanH_CreateWindowSwapChain(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count);
121 void ImGui_ImplVulkanH_CreateWindowCommandBuffers(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator);
122
123 // Vulkan prototypes for use with custom loaders
124 // (see description of IMGUI_IMPL_VULKAN_NO_PROTOTYPES in imgui_impl_vulkan.h
125 #ifdef VK_NO_PROTOTYPES
126 static bool g_FunctionsLoaded = false;
127 #else
128 static bool g_FunctionsLoaded = true;
129 #endif
130 #ifdef VK_NO_PROTOTYPES
131 #define IMGUI_VULKAN_FUNC_MAP(IMGUI_VULKAN_FUNC_MAP_MACRO) \
132 IMGUI_VULKAN_FUNC_MAP_MACRO(vkAllocateCommandBuffers) \
133 IMGUI_VULKAN_FUNC_MAP_MACRO(vkAllocateDescriptorSets) \
134 IMGUI_VULKAN_FUNC_MAP_MACRO(vkAllocateMemory) \
135 IMGUI_VULKAN_FUNC_MAP_MACRO(vkBindBufferMemory) \
136 IMGUI_VULKAN_FUNC_MAP_MACRO(vkBindImageMemory) \
137 IMGUI_VULKAN_FUNC_MAP_MACRO(vkCmdBindDescriptorSets) \
138 IMGUI_VULKAN_FUNC_MAP_MACRO(vkCmdBindIndexBuffer) \
139 IMGUI_VULKAN_FUNC_MAP_MACRO(vkCmdBindPipeline) \
140 IMGUI_VULKAN_FUNC_MAP_MACRO(vkCmdBindVertexBuffers) \
141 IMGUI_VULKAN_FUNC_MAP_MACRO(vkCmdCopyBufferToImage) \
142 IMGUI_VULKAN_FUNC_MAP_MACRO(vkCmdDrawIndexed) \
143 IMGUI_VULKAN_FUNC_MAP_MACRO(vkCmdPipelineBarrier) \
144 IMGUI_VULKAN_FUNC_MAP_MACRO(vkCmdPushConstants) \
145 IMGUI_VULKAN_FUNC_MAP_MACRO(vkCmdSetScissor) \
146 IMGUI_VULKAN_FUNC_MAP_MACRO(vkCmdSetViewport) \
147 IMGUI_VULKAN_FUNC_MAP_MACRO(vkCreateBuffer) \
148 IMGUI_VULKAN_FUNC_MAP_MACRO(vkCreateCommandPool) \
149 IMGUI_VULKAN_FUNC_MAP_MACRO(vkCreateDescriptorSetLayout) \
150 IMGUI_VULKAN_FUNC_MAP_MACRO(vkCreateFence) \
151 IMGUI_VULKAN_FUNC_MAP_MACRO(vkCreateFramebuffer) \
152 IMGUI_VULKAN_FUNC_MAP_MACRO(vkCreateGraphicsPipelines) \
153 IMGUI_VULKAN_FUNC_MAP_MACRO(vkCreateImage) \
154 IMGUI_VULKAN_FUNC_MAP_MACRO(vkCreateImageView) \
155 IMGUI_VULKAN_FUNC_MAP_MACRO(vkCreatePipelineLayout) \
156 IMGUI_VULKAN_FUNC_MAP_MACRO(vkCreateRenderPass) \
157 IMGUI_VULKAN_FUNC_MAP_MACRO(vkCreateSampler) \
158 IMGUI_VULKAN_FUNC_MAP_MACRO(vkCreateSemaphore) \
159 IMGUI_VULKAN_FUNC_MAP_MACRO(vkCreateShaderModule) \
160 IMGUI_VULKAN_FUNC_MAP_MACRO(vkCreateSwapchainKHR) \
161 IMGUI_VULKAN_FUNC_MAP_MACRO(vkDestroyBuffer) \
162 IMGUI_VULKAN_FUNC_MAP_MACRO(vkDestroyCommandPool) \
163 IMGUI_VULKAN_FUNC_MAP_MACRO(vkDestroyDescriptorSetLayout) \
164 IMGUI_VULKAN_FUNC_MAP_MACRO(vkDestroyFence) \
165 IMGUI_VULKAN_FUNC_MAP_MACRO(vkDestroyFramebuffer) \
166 IMGUI_VULKAN_FUNC_MAP_MACRO(vkDestroyImage) \
167 IMGUI_VULKAN_FUNC_MAP_MACRO(vkDestroyImageView) \
168 IMGUI_VULKAN_FUNC_MAP_MACRO(vkDestroyPipeline) \
169 IMGUI_VULKAN_FUNC_MAP_MACRO(vkDestroyPipelineLayout) \
170 IMGUI_VULKAN_FUNC_MAP_MACRO(vkDestroyRenderPass) \
171 IMGUI_VULKAN_FUNC_MAP_MACRO(vkDestroySampler) \
172 IMGUI_VULKAN_FUNC_MAP_MACRO(vkDestroySemaphore) \
173 IMGUI_VULKAN_FUNC_MAP_MACRO(vkDestroyShaderModule) \
174 IMGUI_VULKAN_FUNC_MAP_MACRO(vkDestroySurfaceKHR) \
175 IMGUI_VULKAN_FUNC_MAP_MACRO(vkDestroySwapchainKHR) \
176 IMGUI_VULKAN_FUNC_MAP_MACRO(vkDeviceWaitIdle) \
177 IMGUI_VULKAN_FUNC_MAP_MACRO(vkFlushMappedMemoryRanges) \
178 IMGUI_VULKAN_FUNC_MAP_MACRO(vkFreeCommandBuffers) \
179 IMGUI_VULKAN_FUNC_MAP_MACRO(vkFreeMemory) \
180 IMGUI_VULKAN_FUNC_MAP_MACRO(vkGetBufferMemoryRequirements) \
181 IMGUI_VULKAN_FUNC_MAP_MACRO(vkGetImageMemoryRequirements) \
182 IMGUI_VULKAN_FUNC_MAP_MACRO(vkGetPhysicalDeviceMemoryProperties) \
183 IMGUI_VULKAN_FUNC_MAP_MACRO(vkGetPhysicalDeviceSurfaceCapabilitiesKHR) \
184 IMGUI_VULKAN_FUNC_MAP_MACRO(vkGetPhysicalDeviceSurfaceFormatsKHR) \
185 IMGUI_VULKAN_FUNC_MAP_MACRO(vkGetPhysicalDeviceSurfacePresentModesKHR) \
186 IMGUI_VULKAN_FUNC_MAP_MACRO(vkGetSwapchainImagesKHR) \
187 IMGUI_VULKAN_FUNC_MAP_MACRO(vkMapMemory) \
188 IMGUI_VULKAN_FUNC_MAP_MACRO(vkUnmapMemory) \
189 IMGUI_VULKAN_FUNC_MAP_MACRO(vkUpdateDescriptorSets)
190
191 // Define function pointers
192 #define IMGUI_VULKAN_FUNC_DEF(func) static PFN_##func func;
193 IMGUI_VULKAN_FUNC_MAP(IMGUI_VULKAN_FUNC_DEF)
194 #undef IMGUI_VULKAN_FUNC_DEF
195 #endif // VK_NO_PROTOTYPES
196
197 //-----------------------------------------------------------------------------
198 // SHADERS
199 //-----------------------------------------------------------------------------
200
201 // glsl_shader.vert, compiled with:
202 // # glslangValidator -V -x -o glsl_shader.vert.u32 glsl_shader.vert
203 /*
204 #version 450 core
205 layout(location = 0) in vec2 aPos;
206 layout(location = 1) in vec2 aUV;
207 layout(location = 2) in vec4 aColor;
208 layout(push_constant) uniform uPushConstant { vec2 uScale; vec2 uTranslate; } pc;
209
210 out gl_PerVertex { vec4 gl_Position; };
211 layout(location = 0) out struct { vec4 Color; vec2 UV; } Out;
212
213 void main()
214 {
215 Out.Color = aColor;
216 Out.UV = aUV;
217 gl_Position = vec4(aPos * pc.uScale + pc.uTranslate, 0, 1);
218 }
219 */
220 static uint32_t __glsl_shader_vert_spv[] =
221 {
222 0x07230203,0x00010000,0x00080001,0x0000002e,0x00000000,0x00020011,0x00000001,0x0006000b,
223 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001,
224 0x000a000f,0x00000000,0x00000004,0x6e69616d,0x00000000,0x0000000b,0x0000000f,0x00000015,
225 0x0000001b,0x0000001c,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d,
226 0x00000000,0x00030005,0x00000009,0x00000000,0x00050006,0x00000009,0x00000000,0x6f6c6f43,
227 0x00000072,0x00040006,0x00000009,0x00000001,0x00005655,0x00030005,0x0000000b,0x0074754f,
228 0x00040005,0x0000000f,0x6c6f4361,0x0000726f,0x00030005,0x00000015,0x00565561,0x00060005,
229 0x00000019,0x505f6c67,0x65567265,0x78657472,0x00000000,0x00060006,0x00000019,0x00000000,
230 0x505f6c67,0x7469736f,0x006e6f69,0x00030005,0x0000001b,0x00000000,0x00040005,0x0000001c,
231 0x736f5061,0x00000000,0x00060005,0x0000001e,0x73755075,0x6e6f4368,0x6e617473,0x00000074,
232 0x00050006,0x0000001e,0x00000000,0x61635375,0x0000656c,0x00060006,0x0000001e,0x00000001,
233 0x61725475,0x616c736e,0x00006574,0x00030005,0x00000020,0x00006370,0x00040047,0x0000000b,
234 0x0000001e,0x00000000,0x00040047,0x0000000f,0x0000001e,0x00000002,0x00040047,0x00000015,
235 0x0000001e,0x00000001,0x00050048,0x00000019,0x00000000,0x0000000b,0x00000000,0x00030047,
236 0x00000019,0x00000002,0x00040047,0x0000001c,0x0000001e,0x00000000,0x00050048,0x0000001e,
237 0x00000000,0x00000023,0x00000000,0x00050048,0x0000001e,0x00000001,0x00000023,0x00000008,
238 0x00030047,0x0000001e,0x00000002,0x00020013,0x00000002,0x00030021,0x00000003,0x00000002,
239 0x00030016,0x00000006,0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,0x00040017,
240 0x00000008,0x00000006,0x00000002,0x0004001e,0x00000009,0x00000007,0x00000008,0x00040020,
241 0x0000000a,0x00000003,0x00000009,0x0004003b,0x0000000a,0x0000000b,0x00000003,0x00040015,
242 0x0000000c,0x00000020,0x00000001,0x0004002b,0x0000000c,0x0000000d,0x00000000,0x00040020,
243 0x0000000e,0x00000001,0x00000007,0x0004003b,0x0000000e,0x0000000f,0x00000001,0x00040020,
244 0x00000011,0x00000003,0x00000007,0x0004002b,0x0000000c,0x00000013,0x00000001,0x00040020,
245 0x00000014,0x00000001,0x00000008,0x0004003b,0x00000014,0x00000015,0x00000001,0x00040020,
246 0x00000017,0x00000003,0x00000008,0x0003001e,0x00000019,0x00000007,0x00040020,0x0000001a,
247 0x00000003,0x00000019,0x0004003b,0x0000001a,0x0000001b,0x00000003,0x0004003b,0x00000014,
248 0x0000001c,0x00000001,0x0004001e,0x0000001e,0x00000008,0x00000008,0x00040020,0x0000001f,
249 0x00000009,0x0000001e,0x0004003b,0x0000001f,0x00000020,0x00000009,0x00040020,0x00000021,
250 0x00000009,0x00000008,0x0004002b,0x00000006,0x00000028,0x00000000,0x0004002b,0x00000006,
251 0x00000029,0x3f800000,0x00050036,0x00000002,0x00000004,0x00000000,0x00000003,0x000200f8,
252 0x00000005,0x0004003d,0x00000007,0x00000010,0x0000000f,0x00050041,0x00000011,0x00000012,
253 0x0000000b,0x0000000d,0x0003003e,0x00000012,0x00000010,0x0004003d,0x00000008,0x00000016,
254 0x00000015,0x00050041,0x00000017,0x00000018,0x0000000b,0x00000013,0x0003003e,0x00000018,
255 0x00000016,0x0004003d,0x00000008,0x0000001d,0x0000001c,0x00050041,0x00000021,0x00000022,
256 0x00000020,0x0000000d,0x0004003d,0x00000008,0x00000023,0x00000022,0x00050085,0x00000008,
257 0x00000024,0x0000001d,0x00000023,0x00050041,0x00000021,0x00000025,0x00000020,0x00000013,
258 0x0004003d,0x00000008,0x00000026,0x00000025,0x00050081,0x00000008,0x00000027,0x00000024,
259 0x00000026,0x00050051,0x00000006,0x0000002a,0x00000027,0x00000000,0x00050051,0x00000006,
260 0x0000002b,0x00000027,0x00000001,0x00070050,0x00000007,0x0000002c,0x0000002a,0x0000002b,
261 0x00000028,0x00000029,0x00050041,0x00000011,0x0000002d,0x0000001b,0x0000000d,0x0003003e,
262 0x0000002d,0x0000002c,0x000100fd,0x00010038
263 };
264
265 // glsl_shader.frag, compiled with:
266 // # glslangValidator -V -x -o glsl_shader.frag.u32 glsl_shader.frag
267 /*
268 #version 450 core
269 layout(location = 0) out vec4 fColor;
270 layout(set=0, binding=0) uniform sampler2D sTexture;
271 layout(location = 0) in struct { vec4 Color; vec2 UV; } In;
272 void main()
273 {
274 fColor = In.Color * texture(sTexture, In.UV.st);
275 }
276 */
277 static uint32_t __glsl_shader_frag_spv[] =
278 {
279 0x07230203,0x00010000,0x00080001,0x0000001e,0x00000000,0x00020011,0x00000001,0x0006000b,
280 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001,
281 0x0007000f,0x00000004,0x00000004,0x6e69616d,0x00000000,0x00000009,0x0000000d,0x00030010,
282 0x00000004,0x00000007,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d,
283 0x00000000,0x00040005,0x00000009,0x6c6f4366,0x0000726f,0x00030005,0x0000000b,0x00000000,
284 0x00050006,0x0000000b,0x00000000,0x6f6c6f43,0x00000072,0x00040006,0x0000000b,0x00000001,
285 0x00005655,0x00030005,0x0000000d,0x00006e49,0x00050005,0x00000016,0x78655473,0x65727574,
286 0x00000000,0x00040047,0x00000009,0x0000001e,0x00000000,0x00040047,0x0000000d,0x0000001e,
287 0x00000000,0x00040047,0x00000016,0x00000022,0x00000000,0x00040047,0x00000016,0x00000021,
288 0x00000000,0x00020013,0x00000002,0x00030021,0x00000003,0x00000002,0x00030016,0x00000006,
289 0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,0x00040020,0x00000008,0x00000003,
290 0x00000007,0x0004003b,0x00000008,0x00000009,0x00000003,0x00040017,0x0000000a,0x00000006,
291 0x00000002,0x0004001e,0x0000000b,0x00000007,0x0000000a,0x00040020,0x0000000c,0x00000001,
292 0x0000000b,0x0004003b,0x0000000c,0x0000000d,0x00000001,0x00040015,0x0000000e,0x00000020,
293 0x00000001,0x0004002b,0x0000000e,0x0000000f,0x00000000,0x00040020,0x00000010,0x00000001,
294 0x00000007,0x00090019,0x00000013,0x00000006,0x00000001,0x00000000,0x00000000,0x00000000,
295 0x00000001,0x00000000,0x0003001b,0x00000014,0x00000013,0x00040020,0x00000015,0x00000000,
296 0x00000014,0x0004003b,0x00000015,0x00000016,0x00000000,0x0004002b,0x0000000e,0x00000018,
297 0x00000001,0x00040020,0x00000019,0x00000001,0x0000000a,0x00050036,0x00000002,0x00000004,
298 0x00000000,0x00000003,0x000200f8,0x00000005,0x00050041,0x00000010,0x00000011,0x0000000d,
299 0x0000000f,0x0004003d,0x00000007,0x00000012,0x00000011,0x0004003d,0x00000014,0x00000017,
300 0x00000016,0x00050041,0x00000019,0x0000001a,0x0000000d,0x00000018,0x0004003d,0x0000000a,
301 0x0000001b,0x0000001a,0x00050057,0x00000007,0x0000001c,0x00000017,0x0000001b,0x00050085,
302 0x00000007,0x0000001d,0x00000012,0x0000001c,0x0003003e,0x00000009,0x0000001d,0x000100fd,
303 0x00010038
304 };
305
306 //-----------------------------------------------------------------------------
307 // FUNCTIONS
308 //-----------------------------------------------------------------------------
309
310 // Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts
311 // It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
312 // FIXME: multi-context support is not tested and probably dysfunctional in this backend.
ImGui_ImplVulkan_GetBackendData()313 static ImGui_ImplVulkan_Data* ImGui_ImplVulkan_GetBackendData()
314 {
315 return ImGui::GetCurrentContext() ? (ImGui_ImplVulkan_Data*)ImGui::GetIO().BackendRendererUserData : NULL;
316 }
317
ImGui_ImplVulkan_MemoryType(VkMemoryPropertyFlags properties,uint32_t type_bits)318 static uint32_t ImGui_ImplVulkan_MemoryType(VkMemoryPropertyFlags properties, uint32_t type_bits)
319 {
320 ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
321 ImGui_ImplVulkan_InitInfo* v = &bd->VulkanInitInfo;
322 VkPhysicalDeviceMemoryProperties prop;
323 vkGetPhysicalDeviceMemoryProperties(v->PhysicalDevice, &prop);
324 for (uint32_t i = 0; i < prop.memoryTypeCount; i++)
325 if ((prop.memoryTypes[i].propertyFlags & properties) == properties && type_bits & (1 << i))
326 return i;
327 return 0xFFFFFFFF; // Unable to find memoryType
328 }
329
check_vk_result(VkResult err)330 static void check_vk_result(VkResult err)
331 {
332 ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
333 if (!bd)
334 return;
335 ImGui_ImplVulkan_InitInfo* v = &bd->VulkanInitInfo;
336 if (v->CheckVkResultFn)
337 v->CheckVkResultFn(err);
338 }
339
CreateOrResizeBuffer(VkBuffer & buffer,VkDeviceMemory & buffer_memory,VkDeviceSize & p_buffer_size,size_t new_size,VkBufferUsageFlagBits usage)340 static void CreateOrResizeBuffer(VkBuffer& buffer, VkDeviceMemory& buffer_memory, VkDeviceSize& p_buffer_size, size_t new_size, VkBufferUsageFlagBits usage)
341 {
342 ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
343 ImGui_ImplVulkan_InitInfo* v = &bd->VulkanInitInfo;
344 VkResult err;
345 if (buffer != VK_NULL_HANDLE)
346 vkDestroyBuffer(v->Device, buffer, v->Allocator);
347 if (buffer_memory != VK_NULL_HANDLE)
348 vkFreeMemory(v->Device, buffer_memory, v->Allocator);
349
350 VkDeviceSize vertex_buffer_size_aligned = ((new_size - 1) / bd->BufferMemoryAlignment + 1) * bd->BufferMemoryAlignment;
351 VkBufferCreateInfo buffer_info = {};
352 buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
353 buffer_info.size = vertex_buffer_size_aligned;
354 buffer_info.usage = usage;
355 buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
356 err = vkCreateBuffer(v->Device, &buffer_info, v->Allocator, &buffer);
357 check_vk_result(err);
358
359 VkMemoryRequirements req;
360 vkGetBufferMemoryRequirements(v->Device, buffer, &req);
361 bd->BufferMemoryAlignment = (bd->BufferMemoryAlignment > req.alignment) ? bd->BufferMemoryAlignment : req.alignment;
362 VkMemoryAllocateInfo alloc_info = {};
363 alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
364 alloc_info.allocationSize = req.size;
365 alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
366 err = vkAllocateMemory(v->Device, &alloc_info, v->Allocator, &buffer_memory);
367 check_vk_result(err);
368
369 err = vkBindBufferMemory(v->Device, buffer, buffer_memory, 0);
370 check_vk_result(err);
371 p_buffer_size = req.size;
372 }
373
ImGui_ImplVulkan_SetupRenderState(ImDrawData * draw_data,VkPipeline pipeline,VkCommandBuffer command_buffer,ImGui_ImplVulkanH_FrameRenderBuffers * rb,int fb_width,int fb_height)374 static void ImGui_ImplVulkan_SetupRenderState(ImDrawData* draw_data, VkPipeline pipeline, VkCommandBuffer command_buffer, ImGui_ImplVulkanH_FrameRenderBuffers* rb, int fb_width, int fb_height)
375 {
376 ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
377
378 // Bind pipeline and descriptor sets:
379 {
380 vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
381 VkDescriptorSet desc_set[1] = { bd->DescriptorSet };
382 vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, bd->PipelineLayout, 0, 1, desc_set, 0, NULL);
383 }
384
385 // Bind Vertex And Index Buffer:
386 if (draw_data->TotalVtxCount > 0)
387 {
388 VkBuffer vertex_buffers[1] = { rb->VertexBuffer };
389 VkDeviceSize vertex_offset[1] = { 0 };
390 vkCmdBindVertexBuffers(command_buffer, 0, 1, vertex_buffers, vertex_offset);
391 vkCmdBindIndexBuffer(command_buffer, rb->IndexBuffer, 0, sizeof(ImDrawIdx) == 2 ? VK_INDEX_TYPE_UINT16 : VK_INDEX_TYPE_UINT32);
392 }
393
394 // Setup viewport:
395 {
396 VkViewport viewport;
397 viewport.x = 0;
398 viewport.y = 0;
399 viewport.width = (float)fb_width;
400 viewport.height = (float)fb_height;
401 viewport.minDepth = 0.0f;
402 viewport.maxDepth = 1.0f;
403 vkCmdSetViewport(command_buffer, 0, 1, &viewport);
404 }
405
406 // Setup scale and translation:
407 // Our visible imgui space lies from draw_data->DisplayPps (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayPos is (0,0) for single viewport apps.
408 {
409 float scale[2];
410 scale[0] = 2.0f / draw_data->DisplaySize.x;
411 scale[1] = 2.0f / draw_data->DisplaySize.y;
412 float translate[2];
413 translate[0] = -1.0f - draw_data->DisplayPos.x * scale[0];
414 translate[1] = -1.0f - draw_data->DisplayPos.y * scale[1];
415 vkCmdPushConstants(command_buffer, bd->PipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, sizeof(float) * 0, sizeof(float) * 2, scale);
416 vkCmdPushConstants(command_buffer, bd->PipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, sizeof(float) * 2, sizeof(float) * 2, translate);
417 }
418 }
419
420 // Render function
ImGui_ImplVulkan_RenderDrawData(ImDrawData * draw_data,VkCommandBuffer command_buffer,VkPipeline pipeline)421 void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer command_buffer, VkPipeline pipeline)
422 {
423 // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
424 int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x);
425 int fb_height = (int)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y);
426 if (fb_width <= 0 || fb_height <= 0)
427 return;
428
429 ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
430 ImGui_ImplVulkan_InitInfo* v = &bd->VulkanInitInfo;
431 if (pipeline == VK_NULL_HANDLE)
432 pipeline = bd->Pipeline;
433
434 // Allocate array to store enough vertex/index buffers
435 ImGui_ImplVulkanH_WindowRenderBuffers* wrb = &bd->MainWindowRenderBuffers;
436 if (wrb->FrameRenderBuffers == NULL)
437 {
438 wrb->Index = 0;
439 wrb->Count = v->ImageCount;
440 wrb->FrameRenderBuffers = (ImGui_ImplVulkanH_FrameRenderBuffers*)IM_ALLOC(sizeof(ImGui_ImplVulkanH_FrameRenderBuffers) * wrb->Count);
441 memset(wrb->FrameRenderBuffers, 0, sizeof(ImGui_ImplVulkanH_FrameRenderBuffers) * wrb->Count);
442 }
443 IM_ASSERT(wrb->Count == v->ImageCount);
444 wrb->Index = (wrb->Index + 1) % wrb->Count;
445 ImGui_ImplVulkanH_FrameRenderBuffers* rb = &wrb->FrameRenderBuffers[wrb->Index];
446
447 if (draw_data->TotalVtxCount > 0)
448 {
449 // Create or resize the vertex/index buffers
450 size_t vertex_size = draw_data->TotalVtxCount * sizeof(ImDrawVert);
451 size_t index_size = draw_data->TotalIdxCount * sizeof(ImDrawIdx);
452 if (rb->VertexBuffer == VK_NULL_HANDLE || rb->VertexBufferSize < vertex_size)
453 CreateOrResizeBuffer(rb->VertexBuffer, rb->VertexBufferMemory, rb->VertexBufferSize, vertex_size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
454 if (rb->IndexBuffer == VK_NULL_HANDLE || rb->IndexBufferSize < index_size)
455 CreateOrResizeBuffer(rb->IndexBuffer, rb->IndexBufferMemory, rb->IndexBufferSize, index_size, VK_BUFFER_USAGE_INDEX_BUFFER_BIT);
456
457 // Upload vertex/index data into a single contiguous GPU buffer
458 ImDrawVert* vtx_dst = NULL;
459 ImDrawIdx* idx_dst = NULL;
460 VkResult err = vkMapMemory(v->Device, rb->VertexBufferMemory, 0, rb->VertexBufferSize, 0, (void**)(&vtx_dst));
461 check_vk_result(err);
462 err = vkMapMemory(v->Device, rb->IndexBufferMemory, 0, rb->IndexBufferSize, 0, (void**)(&idx_dst));
463 check_vk_result(err);
464 for (int n = 0; n < draw_data->CmdListsCount; n++)
465 {
466 const ImDrawList* cmd_list = draw_data->CmdLists[n];
467 memcpy(vtx_dst, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert));
468 memcpy(idx_dst, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx));
469 vtx_dst += cmd_list->VtxBuffer.Size;
470 idx_dst += cmd_list->IdxBuffer.Size;
471 }
472 VkMappedMemoryRange range[2] = {};
473 range[0].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
474 range[0].memory = rb->VertexBufferMemory;
475 range[0].size = VK_WHOLE_SIZE;
476 range[1].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
477 range[1].memory = rb->IndexBufferMemory;
478 range[1].size = VK_WHOLE_SIZE;
479 err = vkFlushMappedMemoryRanges(v->Device, 2, range);
480 check_vk_result(err);
481 vkUnmapMemory(v->Device, rb->VertexBufferMemory);
482 vkUnmapMemory(v->Device, rb->IndexBufferMemory);
483 }
484
485 // Setup desired Vulkan state
486 ImGui_ImplVulkan_SetupRenderState(draw_data, pipeline, command_buffer, rb, fb_width, fb_height);
487
488 // Will project scissor/clipping rectangles into framebuffer space
489 ImVec2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports
490 ImVec2 clip_scale = draw_data->FramebufferScale; // (1,1) unless using retina display which are often (2,2)
491
492 // Render command lists
493 // (Because we merged all buffers into a single one, we maintain our own offset into them)
494 int global_vtx_offset = 0;
495 int global_idx_offset = 0;
496 for (int n = 0; n < draw_data->CmdListsCount; n++)
497 {
498 const ImDrawList* cmd_list = draw_data->CmdLists[n];
499 for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
500 {
501 const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
502 if (pcmd->UserCallback != NULL)
503 {
504 // User callback, registered via ImDrawList::AddCallback()
505 // (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
506 if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
507 ImGui_ImplVulkan_SetupRenderState(draw_data, pipeline, command_buffer, rb, fb_width, fb_height);
508 else
509 pcmd->UserCallback(cmd_list, pcmd);
510 }
511 else
512 {
513 // Project scissor/clipping rectangles into framebuffer space
514 ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y);
515 ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y);
516
517 // Clamp to viewport as vkCmdSetScissor() won't accept values that are off bounds
518 if (clip_min.x < 0.0f) { clip_min.x = 0.0f; }
519 if (clip_min.y < 0.0f) { clip_min.y = 0.0f; }
520 if (clip_max.x > fb_width) { clip_max.x = (float)fb_width; }
521 if (clip_max.y > fb_height) { clip_max.y = (float)fb_height; }
522 if (clip_max.x < clip_min.x || clip_max.y < clip_min.y)
523 continue;
524
525 // Apply scissor/clipping rectangle
526 VkRect2D scissor;
527 scissor.offset.x = (int32_t)(clip_min.x);
528 scissor.offset.y = (int32_t)(clip_min.y);
529 scissor.extent.width = (uint32_t)(clip_max.x - clip_min.x);
530 scissor.extent.height = (uint32_t)(clip_max.y - clip_min.y);
531 vkCmdSetScissor(command_buffer, 0, 1, &scissor);
532
533 // Draw
534 vkCmdDrawIndexed(command_buffer, pcmd->ElemCount, 1, pcmd->IdxOffset + global_idx_offset, pcmd->VtxOffset + global_vtx_offset, 0);
535 }
536 }
537 global_idx_offset += cmd_list->IdxBuffer.Size;
538 global_vtx_offset += cmd_list->VtxBuffer.Size;
539 }
540 }
541
ImGui_ImplVulkan_CreateFontsTexture(VkCommandBuffer command_buffer)542 bool ImGui_ImplVulkan_CreateFontsTexture(VkCommandBuffer command_buffer)
543 {
544 ImGuiIO& io = ImGui::GetIO();
545 ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
546 ImGui_ImplVulkan_InitInfo* v = &bd->VulkanInitInfo;
547
548 unsigned char* pixels;
549 int width, height;
550 io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
551 size_t upload_size = width * height * 4 * sizeof(char);
552
553 VkResult err;
554
555 // Create the Image:
556 {
557 VkImageCreateInfo info = {};
558 info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
559 info.imageType = VK_IMAGE_TYPE_2D;
560 info.format = VK_FORMAT_R8G8B8A8_UNORM;
561 info.extent.width = width;
562 info.extent.height = height;
563 info.extent.depth = 1;
564 info.mipLevels = 1;
565 info.arrayLayers = 1;
566 info.samples = VK_SAMPLE_COUNT_1_BIT;
567 info.tiling = VK_IMAGE_TILING_OPTIMAL;
568 info.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
569 info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
570 info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
571 err = vkCreateImage(v->Device, &info, v->Allocator, &bd->FontImage);
572 check_vk_result(err);
573 VkMemoryRequirements req;
574 vkGetImageMemoryRequirements(v->Device, bd->FontImage, &req);
575 VkMemoryAllocateInfo alloc_info = {};
576 alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
577 alloc_info.allocationSize = req.size;
578 alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, req.memoryTypeBits);
579 err = vkAllocateMemory(v->Device, &alloc_info, v->Allocator, &bd->FontMemory);
580 check_vk_result(err);
581 err = vkBindImageMemory(v->Device, bd->FontImage, bd->FontMemory, 0);
582 check_vk_result(err);
583 }
584
585 // Create the Image View:
586 {
587 VkImageViewCreateInfo info = {};
588 info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
589 info.image = bd->FontImage;
590 info.viewType = VK_IMAGE_VIEW_TYPE_2D;
591 info.format = VK_FORMAT_R8G8B8A8_UNORM;
592 info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
593 info.subresourceRange.levelCount = 1;
594 info.subresourceRange.layerCount = 1;
595 err = vkCreateImageView(v->Device, &info, v->Allocator, &bd->FontView);
596 check_vk_result(err);
597 }
598
599 // Update the Descriptor Set:
600 {
601 VkDescriptorImageInfo desc_image[1] = {};
602 desc_image[0].sampler = bd->FontSampler;
603 desc_image[0].imageView = bd->FontView;
604 desc_image[0].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
605 VkWriteDescriptorSet write_desc[1] = {};
606 write_desc[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
607 write_desc[0].dstSet = bd->DescriptorSet;
608 write_desc[0].descriptorCount = 1;
609 write_desc[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
610 write_desc[0].pImageInfo = desc_image;
611 vkUpdateDescriptorSets(v->Device, 1, write_desc, 0, NULL);
612 }
613
614 // Create the Upload Buffer:
615 {
616 VkBufferCreateInfo buffer_info = {};
617 buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
618 buffer_info.size = upload_size;
619 buffer_info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
620 buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
621 err = vkCreateBuffer(v->Device, &buffer_info, v->Allocator, &bd->UploadBuffer);
622 check_vk_result(err);
623 VkMemoryRequirements req;
624 vkGetBufferMemoryRequirements(v->Device, bd->UploadBuffer, &req);
625 bd->BufferMemoryAlignment = (bd->BufferMemoryAlignment > req.alignment) ? bd->BufferMemoryAlignment : req.alignment;
626 VkMemoryAllocateInfo alloc_info = {};
627 alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
628 alloc_info.allocationSize = req.size;
629 alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
630 err = vkAllocateMemory(v->Device, &alloc_info, v->Allocator, &bd->UploadBufferMemory);
631 check_vk_result(err);
632 err = vkBindBufferMemory(v->Device, bd->UploadBuffer, bd->UploadBufferMemory, 0);
633 check_vk_result(err);
634 }
635
636 // Upload to Buffer:
637 {
638 char* map = NULL;
639 err = vkMapMemory(v->Device, bd->UploadBufferMemory, 0, upload_size, 0, (void**)(&map));
640 check_vk_result(err);
641 memcpy(map, pixels, upload_size);
642 VkMappedMemoryRange range[1] = {};
643 range[0].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
644 range[0].memory = bd->UploadBufferMemory;
645 range[0].size = upload_size;
646 err = vkFlushMappedMemoryRanges(v->Device, 1, range);
647 check_vk_result(err);
648 vkUnmapMemory(v->Device, bd->UploadBufferMemory);
649 }
650
651 // Copy to Image:
652 {
653 VkImageMemoryBarrier copy_barrier[1] = {};
654 copy_barrier[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
655 copy_barrier[0].dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
656 copy_barrier[0].oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
657 copy_barrier[0].newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
658 copy_barrier[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
659 copy_barrier[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
660 copy_barrier[0].image = bd->FontImage;
661 copy_barrier[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
662 copy_barrier[0].subresourceRange.levelCount = 1;
663 copy_barrier[0].subresourceRange.layerCount = 1;
664 vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 0, NULL, 1, copy_barrier);
665
666 VkBufferImageCopy region = {};
667 region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
668 region.imageSubresource.layerCount = 1;
669 region.imageExtent.width = width;
670 region.imageExtent.height = height;
671 region.imageExtent.depth = 1;
672 vkCmdCopyBufferToImage(command_buffer, bd->UploadBuffer, bd->FontImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion);
673
674 VkImageMemoryBarrier use_barrier[1] = {};
675 use_barrier[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
676 use_barrier[0].srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
677 use_barrier[0].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
678 use_barrier[0].oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
679 use_barrier[0].newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
680 use_barrier[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
681 use_barrier[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
682 use_barrier[0].image = bd->FontImage;
683 use_barrier[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
684 use_barrier[0].subresourceRange.levelCount = 1;
685 use_barrier[0].subresourceRange.layerCount = 1;
686 vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, NULL, 0, NULL, 1, use_barrier);
687 }
688
689 // Store our identifier
690 io.Fonts->SetTexID((ImTextureID)(intptr_t)bd->FontImage);
691
692 return true;
693 }
694
ImGui_ImplVulkan_CreateShaderModules(VkDevice device,const VkAllocationCallbacks * allocator)695 static void ImGui_ImplVulkan_CreateShaderModules(VkDevice device, const VkAllocationCallbacks* allocator)
696 {
697 // Create the shader modules
698 ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
699 if (bd->ShaderModuleVert == VK_NULL_HANDLE)
700 {
701 VkShaderModuleCreateInfo vert_info = {};
702 vert_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
703 vert_info.codeSize = sizeof(__glsl_shader_vert_spv);
704 vert_info.pCode = (uint32_t*)__glsl_shader_vert_spv;
705 VkResult err = vkCreateShaderModule(device, &vert_info, allocator, &bd->ShaderModuleVert);
706 check_vk_result(err);
707 }
708 if (bd->ShaderModuleFrag == VK_NULL_HANDLE)
709 {
710 VkShaderModuleCreateInfo frag_info = {};
711 frag_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
712 frag_info.codeSize = sizeof(__glsl_shader_frag_spv);
713 frag_info.pCode = (uint32_t*)__glsl_shader_frag_spv;
714 VkResult err = vkCreateShaderModule(device, &frag_info, allocator, &bd->ShaderModuleFrag);
715 check_vk_result(err);
716 }
717 }
718
ImGui_ImplVulkan_CreateFontSampler(VkDevice device,const VkAllocationCallbacks * allocator)719 static void ImGui_ImplVulkan_CreateFontSampler(VkDevice device, const VkAllocationCallbacks* allocator)
720 {
721 ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
722 if (bd->FontSampler)
723 return;
724
725 VkSamplerCreateInfo info = {};
726 info.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
727 info.magFilter = VK_FILTER_LINEAR;
728 info.minFilter = VK_FILTER_LINEAR;
729 info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
730 info.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
731 info.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
732 info.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
733 info.minLod = -1000;
734 info.maxLod = 1000;
735 info.maxAnisotropy = 1.0f;
736 VkResult err = vkCreateSampler(device, &info, allocator, &bd->FontSampler);
737 check_vk_result(err);
738 }
739
ImGui_ImplVulkan_CreateDescriptorSetLayout(VkDevice device,const VkAllocationCallbacks * allocator)740 static void ImGui_ImplVulkan_CreateDescriptorSetLayout(VkDevice device, const VkAllocationCallbacks* allocator)
741 {
742 ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
743 if (bd->DescriptorSetLayout)
744 return;
745
746 ImGui_ImplVulkan_CreateFontSampler(device, allocator);
747 VkSampler sampler[1] = { bd->FontSampler };
748 VkDescriptorSetLayoutBinding binding[1] = {};
749 binding[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
750 binding[0].descriptorCount = 1;
751 binding[0].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
752 binding[0].pImmutableSamplers = sampler;
753 VkDescriptorSetLayoutCreateInfo info = {};
754 info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
755 info.bindingCount = 1;
756 info.pBindings = binding;
757 VkResult err = vkCreateDescriptorSetLayout(device, &info, allocator, &bd->DescriptorSetLayout);
758 check_vk_result(err);
759 }
760
ImGui_ImplVulkan_CreatePipelineLayout(VkDevice device,const VkAllocationCallbacks * allocator)761 static void ImGui_ImplVulkan_CreatePipelineLayout(VkDevice device, const VkAllocationCallbacks* allocator)
762 {
763 ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
764 if (bd->PipelineLayout)
765 return;
766
767 // Constants: we are using 'vec2 offset' and 'vec2 scale' instead of a full 3d projection matrix
768 ImGui_ImplVulkan_CreateDescriptorSetLayout(device, allocator);
769 VkPushConstantRange push_constants[1] = {};
770 push_constants[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
771 push_constants[0].offset = sizeof(float) * 0;
772 push_constants[0].size = sizeof(float) * 4;
773 VkDescriptorSetLayout set_layout[1] = { bd->DescriptorSetLayout };
774 VkPipelineLayoutCreateInfo layout_info = {};
775 layout_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
776 layout_info.setLayoutCount = 1;
777 layout_info.pSetLayouts = set_layout;
778 layout_info.pushConstantRangeCount = 1;
779 layout_info.pPushConstantRanges = push_constants;
780 VkResult err = vkCreatePipelineLayout(device, &layout_info, allocator, &bd->PipelineLayout);
781 check_vk_result(err);
782 }
783
ImGui_ImplVulkan_CreatePipeline(VkDevice device,const VkAllocationCallbacks * allocator,VkPipelineCache pipelineCache,VkRenderPass renderPass,VkSampleCountFlagBits MSAASamples,VkPipeline * pipeline,uint32_t subpass)784 static void ImGui_ImplVulkan_CreatePipeline(VkDevice device, const VkAllocationCallbacks* allocator, VkPipelineCache pipelineCache, VkRenderPass renderPass, VkSampleCountFlagBits MSAASamples, VkPipeline* pipeline, uint32_t subpass)
785 {
786 ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
787 ImGui_ImplVulkan_CreateShaderModules(device, allocator);
788
789 VkPipelineShaderStageCreateInfo stage[2] = {};
790 stage[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
791 stage[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
792 stage[0].module = bd->ShaderModuleVert;
793 stage[0].pName = "main";
794 stage[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
795 stage[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
796 stage[1].module = bd->ShaderModuleFrag;
797 stage[1].pName = "main";
798
799 VkVertexInputBindingDescription binding_desc[1] = {};
800 binding_desc[0].stride = sizeof(ImDrawVert);
801 binding_desc[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
802
803 VkVertexInputAttributeDescription attribute_desc[3] = {};
804 attribute_desc[0].location = 0;
805 attribute_desc[0].binding = binding_desc[0].binding;
806 attribute_desc[0].format = VK_FORMAT_R32G32_SFLOAT;
807 attribute_desc[0].offset = IM_OFFSETOF(ImDrawVert, pos);
808 attribute_desc[1].location = 1;
809 attribute_desc[1].binding = binding_desc[0].binding;
810 attribute_desc[1].format = VK_FORMAT_R32G32_SFLOAT;
811 attribute_desc[1].offset = IM_OFFSETOF(ImDrawVert, uv);
812 attribute_desc[2].location = 2;
813 attribute_desc[2].binding = binding_desc[0].binding;
814 attribute_desc[2].format = VK_FORMAT_R8G8B8A8_UNORM;
815 attribute_desc[2].offset = IM_OFFSETOF(ImDrawVert, col);
816
817 VkPipelineVertexInputStateCreateInfo vertex_info = {};
818 vertex_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
819 vertex_info.vertexBindingDescriptionCount = 1;
820 vertex_info.pVertexBindingDescriptions = binding_desc;
821 vertex_info.vertexAttributeDescriptionCount = 3;
822 vertex_info.pVertexAttributeDescriptions = attribute_desc;
823
824 VkPipelineInputAssemblyStateCreateInfo ia_info = {};
825 ia_info.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
826 ia_info.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
827
828 VkPipelineViewportStateCreateInfo viewport_info = {};
829 viewport_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
830 viewport_info.viewportCount = 1;
831 viewport_info.scissorCount = 1;
832
833 VkPipelineRasterizationStateCreateInfo raster_info = {};
834 raster_info.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
835 raster_info.polygonMode = VK_POLYGON_MODE_FILL;
836 raster_info.cullMode = VK_CULL_MODE_NONE;
837 raster_info.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
838 raster_info.lineWidth = 1.0f;
839
840 VkPipelineMultisampleStateCreateInfo ms_info = {};
841 ms_info.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
842 ms_info.rasterizationSamples = (MSAASamples != 0) ? MSAASamples : VK_SAMPLE_COUNT_1_BIT;
843
844 VkPipelineColorBlendAttachmentState color_attachment[1] = {};
845 color_attachment[0].blendEnable = VK_TRUE;
846 color_attachment[0].srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
847 color_attachment[0].dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
848 color_attachment[0].colorBlendOp = VK_BLEND_OP_ADD;
849 color_attachment[0].srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
850 color_attachment[0].dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
851 color_attachment[0].alphaBlendOp = VK_BLEND_OP_ADD;
852 color_attachment[0].colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
853
854 VkPipelineDepthStencilStateCreateInfo depth_info = {};
855 depth_info.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
856
857 VkPipelineColorBlendStateCreateInfo blend_info = {};
858 blend_info.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
859 blend_info.attachmentCount = 1;
860 blend_info.pAttachments = color_attachment;
861
862 VkDynamicState dynamic_states[2] = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
863 VkPipelineDynamicStateCreateInfo dynamic_state = {};
864 dynamic_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
865 dynamic_state.dynamicStateCount = (uint32_t)IM_ARRAYSIZE(dynamic_states);
866 dynamic_state.pDynamicStates = dynamic_states;
867
868 ImGui_ImplVulkan_CreatePipelineLayout(device, allocator);
869
870 VkGraphicsPipelineCreateInfo info = {};
871 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
872 info.flags = bd->PipelineCreateFlags;
873 info.stageCount = 2;
874 info.pStages = stage;
875 info.pVertexInputState = &vertex_info;
876 info.pInputAssemblyState = &ia_info;
877 info.pViewportState = &viewport_info;
878 info.pRasterizationState = &raster_info;
879 info.pMultisampleState = &ms_info;
880 info.pDepthStencilState = &depth_info;
881 info.pColorBlendState = &blend_info;
882 info.pDynamicState = &dynamic_state;
883 info.layout = bd->PipelineLayout;
884 info.renderPass = renderPass;
885 info.subpass = subpass;
886 VkResult err = vkCreateGraphicsPipelines(device, pipelineCache, 1, &info, allocator, pipeline);
887 check_vk_result(err);
888 }
889
ImGui_ImplVulkan_CreateDeviceObjects()890 bool ImGui_ImplVulkan_CreateDeviceObjects()
891 {
892 ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
893 ImGui_ImplVulkan_InitInfo* v = &bd->VulkanInitInfo;
894 VkResult err;
895
896 if (!bd->FontSampler)
897 {
898 VkSamplerCreateInfo info = {};
899 info.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
900 info.magFilter = VK_FILTER_LINEAR;
901 info.minFilter = VK_FILTER_LINEAR;
902 info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
903 info.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
904 info.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
905 info.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
906 info.minLod = -1000;
907 info.maxLod = 1000;
908 info.maxAnisotropy = 1.0f;
909 err = vkCreateSampler(v->Device, &info, v->Allocator, &bd->FontSampler);
910 check_vk_result(err);
911 }
912
913 if (!bd->DescriptorSetLayout)
914 {
915 VkSampler sampler[1] = {bd->FontSampler};
916 VkDescriptorSetLayoutBinding binding[1] = {};
917 binding[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
918 binding[0].descriptorCount = 1;
919 binding[0].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
920 binding[0].pImmutableSamplers = sampler;
921 VkDescriptorSetLayoutCreateInfo info = {};
922 info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
923 info.bindingCount = 1;
924 info.pBindings = binding;
925 err = vkCreateDescriptorSetLayout(v->Device, &info, v->Allocator, &bd->DescriptorSetLayout);
926 check_vk_result(err);
927 }
928
929 // Create Descriptor Set:
930 {
931 VkDescriptorSetAllocateInfo alloc_info = {};
932 alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
933 alloc_info.descriptorPool = v->DescriptorPool;
934 alloc_info.descriptorSetCount = 1;
935 alloc_info.pSetLayouts = &bd->DescriptorSetLayout;
936 err = vkAllocateDescriptorSets(v->Device, &alloc_info, &bd->DescriptorSet);
937 check_vk_result(err);
938 }
939
940 if (!bd->PipelineLayout)
941 {
942 // Constants: we are using 'vec2 offset' and 'vec2 scale' instead of a full 3d projection matrix
943 VkPushConstantRange push_constants[1] = {};
944 push_constants[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
945 push_constants[0].offset = sizeof(float) * 0;
946 push_constants[0].size = sizeof(float) * 4;
947 VkDescriptorSetLayout set_layout[1] = { bd->DescriptorSetLayout };
948 VkPipelineLayoutCreateInfo layout_info = {};
949 layout_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
950 layout_info.setLayoutCount = 1;
951 layout_info.pSetLayouts = set_layout;
952 layout_info.pushConstantRangeCount = 1;
953 layout_info.pPushConstantRanges = push_constants;
954 err = vkCreatePipelineLayout(v->Device, &layout_info, v->Allocator, &bd->PipelineLayout);
955 check_vk_result(err);
956 }
957
958 ImGui_ImplVulkan_CreatePipeline(v->Device, v->Allocator, v->PipelineCache, bd->RenderPass, v->MSAASamples, &bd->Pipeline, bd->Subpass);
959
960 return true;
961 }
962
ImGui_ImplVulkan_DestroyFontUploadObjects()963 void ImGui_ImplVulkan_DestroyFontUploadObjects()
964 {
965 ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
966 ImGui_ImplVulkan_InitInfo* v = &bd->VulkanInitInfo;
967 if (bd->UploadBuffer)
968 {
969 vkDestroyBuffer(v->Device, bd->UploadBuffer, v->Allocator);
970 bd->UploadBuffer = VK_NULL_HANDLE;
971 }
972 if (bd->UploadBufferMemory)
973 {
974 vkFreeMemory(v->Device, bd->UploadBufferMemory, v->Allocator);
975 bd->UploadBufferMemory = VK_NULL_HANDLE;
976 }
977 }
978
ImGui_ImplVulkan_DestroyDeviceObjects()979 void ImGui_ImplVulkan_DestroyDeviceObjects()
980 {
981 ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
982 ImGui_ImplVulkan_InitInfo* v = &bd->VulkanInitInfo;
983 ImGui_ImplVulkanH_DestroyWindowRenderBuffers(v->Device, &bd->MainWindowRenderBuffers, v->Allocator);
984 ImGui_ImplVulkan_DestroyFontUploadObjects();
985
986 if (bd->ShaderModuleVert) { vkDestroyShaderModule(v->Device, bd->ShaderModuleVert, v->Allocator); bd->ShaderModuleVert = VK_NULL_HANDLE; }
987 if (bd->ShaderModuleFrag) { vkDestroyShaderModule(v->Device, bd->ShaderModuleFrag, v->Allocator); bd->ShaderModuleFrag = VK_NULL_HANDLE; }
988 if (bd->FontView) { vkDestroyImageView(v->Device, bd->FontView, v->Allocator); bd->FontView = VK_NULL_HANDLE; }
989 if (bd->FontImage) { vkDestroyImage(v->Device, bd->FontImage, v->Allocator); bd->FontImage = VK_NULL_HANDLE; }
990 if (bd->FontMemory) { vkFreeMemory(v->Device, bd->FontMemory, v->Allocator); bd->FontMemory = VK_NULL_HANDLE; }
991 if (bd->FontSampler) { vkDestroySampler(v->Device, bd->FontSampler, v->Allocator); bd->FontSampler = VK_NULL_HANDLE; }
992 if (bd->DescriptorSetLayout) { vkDestroyDescriptorSetLayout(v->Device, bd->DescriptorSetLayout, v->Allocator); bd->DescriptorSetLayout = VK_NULL_HANDLE; }
993 if (bd->PipelineLayout) { vkDestroyPipelineLayout(v->Device, bd->PipelineLayout, v->Allocator); bd->PipelineLayout = VK_NULL_HANDLE; }
994 if (bd->Pipeline) { vkDestroyPipeline(v->Device, bd->Pipeline, v->Allocator); bd->Pipeline = VK_NULL_HANDLE; }
995 }
996
ImGui_ImplVulkan_LoadFunctions(PFN_vkVoidFunction (* loader_func)(const char * function_name,void * user_data),void * user_data)997 bool ImGui_ImplVulkan_LoadFunctions(PFN_vkVoidFunction(*loader_func)(const char* function_name, void* user_data), void* user_data)
998 {
999 // Load function pointers
1000 // You can use the default Vulkan loader using:
1001 // ImGui_ImplVulkan_LoadFunctions([](const char* function_name, void*) { return vkGetInstanceProcAddr(your_vk_isntance, function_name); });
1002 // But this would be equivalent to not setting VK_NO_PROTOTYPES.
1003 #ifdef VK_NO_PROTOTYPES
1004 #define IMGUI_VULKAN_FUNC_LOAD(func) \
1005 func = reinterpret_cast<decltype(func)>(loader_func(#func, user_data)); \
1006 if (func == NULL) \
1007 return false;
1008 IMGUI_VULKAN_FUNC_MAP(IMGUI_VULKAN_FUNC_LOAD)
1009 #undef IMGUI_VULKAN_FUNC_LOAD
1010 #else
1011 IM_UNUSED(loader_func);
1012 IM_UNUSED(user_data);
1013 #endif
1014 g_FunctionsLoaded = true;
1015 return true;
1016 }
1017
ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo * info,VkRenderPass render_pass)1018 bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass render_pass)
1019 {
1020 IM_ASSERT(g_FunctionsLoaded && "Need to call ImGui_ImplVulkan_LoadFunctions() if IMGUI_IMPL_VULKAN_NO_PROTOTYPES or VK_NO_PROTOTYPES are set!");
1021
1022 ImGuiIO& io = ImGui::GetIO();
1023 IM_ASSERT(io.BackendRendererUserData == NULL && "Already initialized a renderer backend!");
1024
1025 // Setup backend capabilities flags
1026 ImGui_ImplVulkan_Data* bd = IM_NEW(ImGui_ImplVulkan_Data)();
1027 io.BackendRendererUserData = (void*)bd;
1028 io.BackendRendererName = "imgui_impl_vulkan";
1029 io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
1030
1031 IM_ASSERT(info->Instance != VK_NULL_HANDLE);
1032 IM_ASSERT(info->PhysicalDevice != VK_NULL_HANDLE);
1033 IM_ASSERT(info->Device != VK_NULL_HANDLE);
1034 IM_ASSERT(info->Queue != VK_NULL_HANDLE);
1035 IM_ASSERT(info->DescriptorPool != VK_NULL_HANDLE);
1036 IM_ASSERT(info->MinImageCount >= 2);
1037 IM_ASSERT(info->ImageCount >= info->MinImageCount);
1038 IM_ASSERT(render_pass != VK_NULL_HANDLE);
1039
1040 bd->VulkanInitInfo = *info;
1041 bd->RenderPass = render_pass;
1042 bd->Subpass = info->Subpass;
1043
1044 ImGui_ImplVulkan_CreateDeviceObjects();
1045
1046 return true;
1047 }
1048
ImGui_ImplVulkan_Shutdown()1049 void ImGui_ImplVulkan_Shutdown()
1050 {
1051 ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
1052 IM_ASSERT(bd != NULL && "No renderer backend to shutdown, or already shutdown?");
1053 ImGuiIO& io = ImGui::GetIO();
1054
1055 ImGui_ImplVulkan_DestroyDeviceObjects();
1056 io.BackendRendererName = NULL;
1057 io.BackendRendererUserData = NULL;
1058 IM_DELETE(bd);
1059 }
1060
ImGui_ImplVulkan_NewFrame()1061 void ImGui_ImplVulkan_NewFrame()
1062 {
1063 ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
1064 IM_ASSERT(bd != NULL && "Did you call ImGui_ImplVulkan_Init()?");
1065 IM_UNUSED(bd);
1066 }
1067
ImGui_ImplVulkan_SetMinImageCount(uint32_t min_image_count)1068 void ImGui_ImplVulkan_SetMinImageCount(uint32_t min_image_count)
1069 {
1070 ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
1071 IM_ASSERT(min_image_count >= 2);
1072 if (bd->VulkanInitInfo.MinImageCount == min_image_count)
1073 return;
1074
1075 ImGui_ImplVulkan_InitInfo* v = &bd->VulkanInitInfo;
1076 VkResult err = vkDeviceWaitIdle(v->Device);
1077 check_vk_result(err);
1078 ImGui_ImplVulkanH_DestroyWindowRenderBuffers(v->Device, &bd->MainWindowRenderBuffers, v->Allocator);
1079 bd->VulkanInitInfo.MinImageCount = min_image_count;
1080 }
1081
1082
1083 //-------------------------------------------------------------------------
1084 // Internal / Miscellaneous Vulkan Helpers
1085 // (Used by example's main.cpp. Used by multi-viewport features. PROBABLY NOT used by your own app.)
1086 //-------------------------------------------------------------------------
1087 // You probably do NOT need to use or care about those functions.
1088 // Those functions only exist because:
1089 // 1) they facilitate the readability and maintenance of the multiple main.cpp examples files.
1090 // 2) the upcoming multi-viewport feature will need them internally.
1091 // Generally we avoid exposing any kind of superfluous high-level helpers in the backends,
1092 // but it is too much code to duplicate everywhere so we exceptionally expose them.
1093 //
1094 // Your engine/app will likely _already_ have code to setup all that stuff (swap chain, render pass, frame buffers, etc.).
1095 // You may read this code to learn about Vulkan, but it is recommended you use you own custom tailored code to do equivalent work.
1096 // (The ImGui_ImplVulkanH_XXX functions do not interact with any of the state used by the regular ImGui_ImplVulkan_XXX functions)
1097 //-------------------------------------------------------------------------
1098
ImGui_ImplVulkanH_SelectSurfaceFormat(VkPhysicalDevice physical_device,VkSurfaceKHR surface,const VkFormat * request_formats,int request_formats_count,VkColorSpaceKHR request_color_space)1099 VkSurfaceFormatKHR ImGui_ImplVulkanH_SelectSurfaceFormat(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkFormat* request_formats, int request_formats_count, VkColorSpaceKHR request_color_space)
1100 {
1101 IM_ASSERT(g_FunctionsLoaded && "Need to call ImGui_ImplVulkan_LoadFunctions() if IMGUI_IMPL_VULKAN_NO_PROTOTYPES or VK_NO_PROTOTYPES are set!");
1102 IM_ASSERT(request_formats != NULL);
1103 IM_ASSERT(request_formats_count > 0);
1104
1105 // Per Spec Format and View Format are expected to be the same unless VK_IMAGE_CREATE_MUTABLE_BIT was set at image creation
1106 // Assuming that the default behavior is without setting this bit, there is no need for separate Swapchain image and image view format
1107 // Additionally several new color spaces were introduced with Vulkan Spec v1.0.40,
1108 // hence we must make sure that a format with the mostly available color space, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR, is found and used.
1109 uint32_t avail_count;
1110 vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface, &avail_count, NULL);
1111 ImVector<VkSurfaceFormatKHR> avail_format;
1112 avail_format.resize((int)avail_count);
1113 vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface, &avail_count, avail_format.Data);
1114
1115 // First check if only one format, VK_FORMAT_UNDEFINED, is available, which would imply that any format is available
1116 if (avail_count == 1)
1117 {
1118 if (avail_format[0].format == VK_FORMAT_UNDEFINED)
1119 {
1120 VkSurfaceFormatKHR ret;
1121 ret.format = request_formats[0];
1122 ret.colorSpace = request_color_space;
1123 return ret;
1124 }
1125 else
1126 {
1127 // No point in searching another format
1128 return avail_format[0];
1129 }
1130 }
1131 else
1132 {
1133 // Request several formats, the first found will be used
1134 for (int request_i = 0; request_i < request_formats_count; request_i++)
1135 for (uint32_t avail_i = 0; avail_i < avail_count; avail_i++)
1136 if (avail_format[avail_i].format == request_formats[request_i] && avail_format[avail_i].colorSpace == request_color_space)
1137 return avail_format[avail_i];
1138
1139 // If none of the requested image formats could be found, use the first available
1140 return avail_format[0];
1141 }
1142 }
1143
ImGui_ImplVulkanH_SelectPresentMode(VkPhysicalDevice physical_device,VkSurfaceKHR surface,const VkPresentModeKHR * request_modes,int request_modes_count)1144 VkPresentModeKHR ImGui_ImplVulkanH_SelectPresentMode(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkPresentModeKHR* request_modes, int request_modes_count)
1145 {
1146 IM_ASSERT(g_FunctionsLoaded && "Need to call ImGui_ImplVulkan_LoadFunctions() if IMGUI_IMPL_VULKAN_NO_PROTOTYPES or VK_NO_PROTOTYPES are set!");
1147 IM_ASSERT(request_modes != NULL);
1148 IM_ASSERT(request_modes_count > 0);
1149
1150 // Request a certain mode and confirm that it is available. If not use VK_PRESENT_MODE_FIFO_KHR which is mandatory
1151 uint32_t avail_count = 0;
1152 vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface, &avail_count, NULL);
1153 ImVector<VkPresentModeKHR> avail_modes;
1154 avail_modes.resize((int)avail_count);
1155 vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface, &avail_count, avail_modes.Data);
1156 //for (uint32_t avail_i = 0; avail_i < avail_count; avail_i++)
1157 // printf("[vulkan] avail_modes[%d] = %d\n", avail_i, avail_modes[avail_i]);
1158
1159 for (int request_i = 0; request_i < request_modes_count; request_i++)
1160 for (uint32_t avail_i = 0; avail_i < avail_count; avail_i++)
1161 if (request_modes[request_i] == avail_modes[avail_i])
1162 return request_modes[request_i];
1163
1164 return VK_PRESENT_MODE_FIFO_KHR; // Always available
1165 }
1166
ImGui_ImplVulkanH_CreateWindowCommandBuffers(VkPhysicalDevice physical_device,VkDevice device,ImGui_ImplVulkanH_Window * wd,uint32_t queue_family,const VkAllocationCallbacks * allocator)1167 void ImGui_ImplVulkanH_CreateWindowCommandBuffers(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator)
1168 {
1169 IM_ASSERT(physical_device != VK_NULL_HANDLE && device != VK_NULL_HANDLE);
1170 (void)physical_device;
1171 (void)allocator;
1172
1173 // Create Command Buffers
1174 VkResult err;
1175 for (uint32_t i = 0; i < wd->ImageCount; i++)
1176 {
1177 ImGui_ImplVulkanH_Frame* fd = &wd->Frames[i];
1178 ImGui_ImplVulkanH_FrameSemaphores* fsd = &wd->FrameSemaphores[i];
1179 {
1180 VkCommandPoolCreateInfo info = {};
1181 info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
1182 info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
1183 info.queueFamilyIndex = queue_family;
1184 err = vkCreateCommandPool(device, &info, allocator, &fd->CommandPool);
1185 check_vk_result(err);
1186 }
1187 {
1188 VkCommandBufferAllocateInfo info = {};
1189 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
1190 info.commandPool = fd->CommandPool;
1191 info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
1192 info.commandBufferCount = 1;
1193 err = vkAllocateCommandBuffers(device, &info, &fd->CommandBuffer);
1194 check_vk_result(err);
1195 }
1196 {
1197 VkFenceCreateInfo info = {};
1198 info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1199 info.flags = VK_FENCE_CREATE_SIGNALED_BIT;
1200 err = vkCreateFence(device, &info, allocator, &fd->Fence);
1201 check_vk_result(err);
1202 }
1203 {
1204 VkSemaphoreCreateInfo info = {};
1205 info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
1206 err = vkCreateSemaphore(device, &info, allocator, &fsd->ImageAcquiredSemaphore);
1207 check_vk_result(err);
1208 err = vkCreateSemaphore(device, &info, allocator, &fsd->RenderCompleteSemaphore);
1209 check_vk_result(err);
1210 }
1211 }
1212 }
1213
ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(VkPresentModeKHR present_mode)1214 int ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(VkPresentModeKHR present_mode)
1215 {
1216 if (present_mode == VK_PRESENT_MODE_MAILBOX_KHR)
1217 return 3;
1218 if (present_mode == VK_PRESENT_MODE_FIFO_KHR || present_mode == VK_PRESENT_MODE_FIFO_RELAXED_KHR)
1219 return 2;
1220 if (present_mode == VK_PRESENT_MODE_IMMEDIATE_KHR)
1221 return 1;
1222 IM_ASSERT(0);
1223 return 1;
1224 }
1225
1226 // Also destroy old swap chain and in-flight frames data, if any.
ImGui_ImplVulkanH_CreateWindowSwapChain(VkPhysicalDevice physical_device,VkDevice device,ImGui_ImplVulkanH_Window * wd,const VkAllocationCallbacks * allocator,int w,int h,uint32_t min_image_count)1227 void ImGui_ImplVulkanH_CreateWindowSwapChain(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count)
1228 {
1229 VkResult err;
1230 VkSwapchainKHR old_swapchain = wd->Swapchain;
1231 wd->Swapchain = VK_NULL_HANDLE;
1232 err = vkDeviceWaitIdle(device);
1233 check_vk_result(err);
1234
1235 // We don't use ImGui_ImplVulkanH_DestroyWindow() because we want to preserve the old swapchain to create the new one.
1236 // Destroy old Framebuffer
1237 for (uint32_t i = 0; i < wd->ImageCount; i++)
1238 {
1239 ImGui_ImplVulkanH_DestroyFrame(device, &wd->Frames[i], allocator);
1240 ImGui_ImplVulkanH_DestroyFrameSemaphores(device, &wd->FrameSemaphores[i], allocator);
1241 }
1242 IM_FREE(wd->Frames);
1243 IM_FREE(wd->FrameSemaphores);
1244 wd->Frames = NULL;
1245 wd->FrameSemaphores = NULL;
1246 wd->ImageCount = 0;
1247 if (wd->RenderPass)
1248 vkDestroyRenderPass(device, wd->RenderPass, allocator);
1249 if (wd->Pipeline)
1250 vkDestroyPipeline(device, wd->Pipeline, allocator);
1251
1252 // If min image count was not specified, request different count of images dependent on selected present mode
1253 if (min_image_count == 0)
1254 min_image_count = ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(wd->PresentMode);
1255
1256 // Create Swapchain
1257 {
1258 VkSwapchainCreateInfoKHR info = {};
1259 info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
1260 info.surface = wd->Surface;
1261 info.minImageCount = min_image_count;
1262 info.imageFormat = wd->SurfaceFormat.format;
1263 info.imageColorSpace = wd->SurfaceFormat.colorSpace;
1264 info.imageArrayLayers = 1;
1265 info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1266 info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; // Assume that graphics family == present family
1267 info.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
1268 info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
1269 info.presentMode = wd->PresentMode;
1270 info.clipped = VK_TRUE;
1271 info.oldSwapchain = old_swapchain;
1272 VkSurfaceCapabilitiesKHR cap;
1273 err = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physical_device, wd->Surface, &cap);
1274 check_vk_result(err);
1275 if (info.minImageCount < cap.minImageCount)
1276 info.minImageCount = cap.minImageCount;
1277 else if (cap.maxImageCount != 0 && info.minImageCount > cap.maxImageCount)
1278 info.minImageCount = cap.maxImageCount;
1279
1280 if (cap.currentExtent.width == 0xffffffff)
1281 {
1282 info.imageExtent.width = wd->Width = w;
1283 info.imageExtent.height = wd->Height = h;
1284 }
1285 else
1286 {
1287 info.imageExtent.width = wd->Width = cap.currentExtent.width;
1288 info.imageExtent.height = wd->Height = cap.currentExtent.height;
1289 }
1290 err = vkCreateSwapchainKHR(device, &info, allocator, &wd->Swapchain);
1291 check_vk_result(err);
1292 err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->ImageCount, NULL);
1293 check_vk_result(err);
1294 VkImage backbuffers[16] = {};
1295 IM_ASSERT(wd->ImageCount >= min_image_count);
1296 IM_ASSERT(wd->ImageCount < IM_ARRAYSIZE(backbuffers));
1297 err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->ImageCount, backbuffers);
1298 check_vk_result(err);
1299
1300 IM_ASSERT(wd->Frames == NULL);
1301 wd->Frames = (ImGui_ImplVulkanH_Frame*)IM_ALLOC(sizeof(ImGui_ImplVulkanH_Frame) * wd->ImageCount);
1302 wd->FrameSemaphores = (ImGui_ImplVulkanH_FrameSemaphores*)IM_ALLOC(sizeof(ImGui_ImplVulkanH_FrameSemaphores) * wd->ImageCount);
1303 memset(wd->Frames, 0, sizeof(wd->Frames[0]) * wd->ImageCount);
1304 memset(wd->FrameSemaphores, 0, sizeof(wd->FrameSemaphores[0]) * wd->ImageCount);
1305 for (uint32_t i = 0; i < wd->ImageCount; i++)
1306 wd->Frames[i].Backbuffer = backbuffers[i];
1307 }
1308 if (old_swapchain)
1309 vkDestroySwapchainKHR(device, old_swapchain, allocator);
1310
1311 // Create the Render Pass
1312 {
1313 VkAttachmentDescription attachment = {};
1314 attachment.format = wd->SurfaceFormat.format;
1315 attachment.samples = VK_SAMPLE_COUNT_1_BIT;
1316 attachment.loadOp = wd->ClearEnable ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_DONT_CARE;
1317 attachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
1318 attachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
1319 attachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
1320 attachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
1321 attachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
1322 VkAttachmentReference color_attachment = {};
1323 color_attachment.attachment = 0;
1324 color_attachment.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
1325 VkSubpassDescription subpass = {};
1326 subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
1327 subpass.colorAttachmentCount = 1;
1328 subpass.pColorAttachments = &color_attachment;
1329 VkSubpassDependency dependency = {};
1330 dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
1331 dependency.dstSubpass = 0;
1332 dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1333 dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1334 dependency.srcAccessMask = 0;
1335 dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
1336 VkRenderPassCreateInfo info = {};
1337 info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
1338 info.attachmentCount = 1;
1339 info.pAttachments = &attachment;
1340 info.subpassCount = 1;
1341 info.pSubpasses = &subpass;
1342 info.dependencyCount = 1;
1343 info.pDependencies = &dependency;
1344 err = vkCreateRenderPass(device, &info, allocator, &wd->RenderPass);
1345 check_vk_result(err);
1346
1347 // We do not create a pipeline by default as this is also used by examples' main.cpp,
1348 // but secondary viewport in multi-viewport mode may want to create one with:
1349 //ImGui_ImplVulkan_CreatePipeline(device, allocator, VK_NULL_HANDLE, wd->RenderPass, VK_SAMPLE_COUNT_1_BIT, &wd->Pipeline, bd->Subpass);
1350 }
1351
1352 // Create The Image Views
1353 {
1354 VkImageViewCreateInfo info = {};
1355 info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
1356 info.viewType = VK_IMAGE_VIEW_TYPE_2D;
1357 info.format = wd->SurfaceFormat.format;
1358 info.components.r = VK_COMPONENT_SWIZZLE_R;
1359 info.components.g = VK_COMPONENT_SWIZZLE_G;
1360 info.components.b = VK_COMPONENT_SWIZZLE_B;
1361 info.components.a = VK_COMPONENT_SWIZZLE_A;
1362 VkImageSubresourceRange image_range = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
1363 info.subresourceRange = image_range;
1364 for (uint32_t i = 0; i < wd->ImageCount; i++)
1365 {
1366 ImGui_ImplVulkanH_Frame* fd = &wd->Frames[i];
1367 info.image = fd->Backbuffer;
1368 err = vkCreateImageView(device, &info, allocator, &fd->BackbufferView);
1369 check_vk_result(err);
1370 }
1371 }
1372
1373 // Create Framebuffer
1374 {
1375 VkImageView attachment[1];
1376 VkFramebufferCreateInfo info = {};
1377 info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
1378 info.renderPass = wd->RenderPass;
1379 info.attachmentCount = 1;
1380 info.pAttachments = attachment;
1381 info.width = wd->Width;
1382 info.height = wd->Height;
1383 info.layers = 1;
1384 for (uint32_t i = 0; i < wd->ImageCount; i++)
1385 {
1386 ImGui_ImplVulkanH_Frame* fd = &wd->Frames[i];
1387 attachment[0] = fd->BackbufferView;
1388 err = vkCreateFramebuffer(device, &info, allocator, &fd->Framebuffer);
1389 check_vk_result(err);
1390 }
1391 }
1392 }
1393
1394 // Create or resize window
ImGui_ImplVulkanH_CreateOrResizeWindow(VkInstance instance,VkPhysicalDevice physical_device,VkDevice device,ImGui_ImplVulkanH_Window * wd,uint32_t queue_family,const VkAllocationCallbacks * allocator,int width,int height,uint32_t min_image_count)1395 void ImGui_ImplVulkanH_CreateOrResizeWindow(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator, int width, int height, uint32_t min_image_count)
1396 {
1397 IM_ASSERT(g_FunctionsLoaded && "Need to call ImGui_ImplVulkan_LoadFunctions() if IMGUI_IMPL_VULKAN_NO_PROTOTYPES or VK_NO_PROTOTYPES are set!");
1398 (void)instance;
1399 ImGui_ImplVulkanH_CreateWindowSwapChain(physical_device, device, wd, allocator, width, height, min_image_count);
1400 ImGui_ImplVulkanH_CreateWindowCommandBuffers(physical_device, device, wd, queue_family, allocator);
1401 }
1402
ImGui_ImplVulkanH_DestroyWindow(VkInstance instance,VkDevice device,ImGui_ImplVulkanH_Window * wd,const VkAllocationCallbacks * allocator)1403 void ImGui_ImplVulkanH_DestroyWindow(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_Window* wd, const VkAllocationCallbacks* allocator)
1404 {
1405 vkDeviceWaitIdle(device); // FIXME: We could wait on the Queue if we had the queue in wd-> (otherwise VulkanH functions can't use globals)
1406 //vkQueueWaitIdle(bd->Queue);
1407
1408 for (uint32_t i = 0; i < wd->ImageCount; i++)
1409 {
1410 ImGui_ImplVulkanH_DestroyFrame(device, &wd->Frames[i], allocator);
1411 ImGui_ImplVulkanH_DestroyFrameSemaphores(device, &wd->FrameSemaphores[i], allocator);
1412 }
1413 IM_FREE(wd->Frames);
1414 IM_FREE(wd->FrameSemaphores);
1415 wd->Frames = NULL;
1416 wd->FrameSemaphores = NULL;
1417 vkDestroyPipeline(device, wd->Pipeline, allocator);
1418 vkDestroyRenderPass(device, wd->RenderPass, allocator);
1419 vkDestroySwapchainKHR(device, wd->Swapchain, allocator);
1420 vkDestroySurfaceKHR(instance, wd->Surface, allocator);
1421
1422 *wd = ImGui_ImplVulkanH_Window();
1423 }
1424
ImGui_ImplVulkanH_DestroyFrame(VkDevice device,ImGui_ImplVulkanH_Frame * fd,const VkAllocationCallbacks * allocator)1425 void ImGui_ImplVulkanH_DestroyFrame(VkDevice device, ImGui_ImplVulkanH_Frame* fd, const VkAllocationCallbacks* allocator)
1426 {
1427 vkDestroyFence(device, fd->Fence, allocator);
1428 vkFreeCommandBuffers(device, fd->CommandPool, 1, &fd->CommandBuffer);
1429 vkDestroyCommandPool(device, fd->CommandPool, allocator);
1430 fd->Fence = VK_NULL_HANDLE;
1431 fd->CommandBuffer = VK_NULL_HANDLE;
1432 fd->CommandPool = VK_NULL_HANDLE;
1433
1434 vkDestroyImageView(device, fd->BackbufferView, allocator);
1435 vkDestroyFramebuffer(device, fd->Framebuffer, allocator);
1436 }
1437
ImGui_ImplVulkanH_DestroyFrameSemaphores(VkDevice device,ImGui_ImplVulkanH_FrameSemaphores * fsd,const VkAllocationCallbacks * allocator)1438 void ImGui_ImplVulkanH_DestroyFrameSemaphores(VkDevice device, ImGui_ImplVulkanH_FrameSemaphores* fsd, const VkAllocationCallbacks* allocator)
1439 {
1440 vkDestroySemaphore(device, fsd->ImageAcquiredSemaphore, allocator);
1441 vkDestroySemaphore(device, fsd->RenderCompleteSemaphore, allocator);
1442 fsd->ImageAcquiredSemaphore = fsd->RenderCompleteSemaphore = VK_NULL_HANDLE;
1443 }
1444
ImGui_ImplVulkanH_DestroyFrameRenderBuffers(VkDevice device,ImGui_ImplVulkanH_FrameRenderBuffers * buffers,const VkAllocationCallbacks * allocator)1445 void ImGui_ImplVulkanH_DestroyFrameRenderBuffers(VkDevice device, ImGui_ImplVulkanH_FrameRenderBuffers* buffers, const VkAllocationCallbacks* allocator)
1446 {
1447 if (buffers->VertexBuffer) { vkDestroyBuffer(device, buffers->VertexBuffer, allocator); buffers->VertexBuffer = VK_NULL_HANDLE; }
1448 if (buffers->VertexBufferMemory) { vkFreeMemory(device, buffers->VertexBufferMemory, allocator); buffers->VertexBufferMemory = VK_NULL_HANDLE; }
1449 if (buffers->IndexBuffer) { vkDestroyBuffer(device, buffers->IndexBuffer, allocator); buffers->IndexBuffer = VK_NULL_HANDLE; }
1450 if (buffers->IndexBufferMemory) { vkFreeMemory(device, buffers->IndexBufferMemory, allocator); buffers->IndexBufferMemory = VK_NULL_HANDLE; }
1451 buffers->VertexBufferSize = 0;
1452 buffers->IndexBufferSize = 0;
1453 }
1454
ImGui_ImplVulkanH_DestroyWindowRenderBuffers(VkDevice device,ImGui_ImplVulkanH_WindowRenderBuffers * buffers,const VkAllocationCallbacks * allocator)1455 void ImGui_ImplVulkanH_DestroyWindowRenderBuffers(VkDevice device, ImGui_ImplVulkanH_WindowRenderBuffers* buffers, const VkAllocationCallbacks* allocator)
1456 {
1457 for (uint32_t n = 0; n < buffers->Count; n++)
1458 ImGui_ImplVulkanH_DestroyFrameRenderBuffers(device, &buffers->FrameRenderBuffers[n], allocator);
1459 IM_FREE(buffers->FrameRenderBuffers);
1460 buffers->FrameRenderBuffers = NULL;
1461 buffers->Index = 0;
1462 buffers->Count = 0;
1463 }
1464