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