imgui_impl_vulkan.cpp 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. // dear imgui: Renderer for Vulkan
  2. // This needs to be used along with a Platform Binding (e.g. GLFW, SDL, Win32, custom..)
  3. // Missing features:
  4. // [ ] Renderer: User texture binding. Changes of ImTextureID aren't supported by this binding! See https://github.com/ocornut/imgui/pull/914
  5. // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
  6. // If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp.
  7. // https://github.com/ocornut/imgui
  8. // The aim of imgui_impl_vulkan.h/.cpp is to be usable in your engine without any modification.
  9. // IF YOU FEEL YOU NEED TO MAKE ANY CHANGE TO THIS CODE, please share them and your feedback at https://github.com/ocornut/imgui/
  10. // CHANGELOG
  11. // (minor and older changes stripped away, please see git history for details)
  12. // 2019-04-04: Vulkan: Avoid passing negative coordinates to vkCmdSetScissor, which debug validation layers do not like.
  13. // 2019-04-01: Vulkan: Support for 32-bit index buffer (#define ImDrawIdx unsigned int).
  14. // 2019-02-16: Vulkan: Viewport and clipping rectangles correctly using draw_data->FramebufferScale to allow retina display.
  15. // 2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window.
  16. // 2018-08-25: Vulkan: Fixed mishandled VkSurfaceCapabilitiesKHR::maxImageCount=0 case.
  17. // 2018-06-22: Inverted the parameters to ImGui_ImplVulkan_RenderDrawData() to be consistent with other bindings.
  18. // 2018-06-08: Misc: Extracted imgui_impl_vulkan.cpp/.h away from the old combined GLFW+Vulkan example.
  19. // 2018-06-08: Vulkan: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle.
  20. // 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.
  21. // 2018-03-01: Vulkan: Renamed ImGui_ImplVulkan_Init_Info to ImGui_ImplVulkan_InitInfo and fields to match more closely Vulkan terminology.
  22. // 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback, ImGui_ImplVulkan_Render() calls ImGui_ImplVulkan_RenderDrawData() itself.
  23. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  24. // 2017-05-15: Vulkan: Fix scissor offset being negative. Fix new Vulkan validation warnings. Set required depth member for buffer image copy.
  25. // 2016-11-13: Vulkan: Fix validation layer warnings and errors and redeclare gl_PerVertex.
  26. // 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.
  27. // 2016-08-27: Vulkan: Fix Vulkan example for use when a depth buffer is active.
  28. #include "imgui.h"
  29. #include "imgui_impl_vulkan.h"
  30. #include <stdio.h>
  31. // Vulkan data
  32. static const VkAllocationCallbacks* g_Allocator = NULL;
  33. static VkPhysicalDevice g_PhysicalDevice = VK_NULL_HANDLE;
  34. static VkInstance g_Instance = VK_NULL_HANDLE;
  35. static VkDevice g_Device = VK_NULL_HANDLE;
  36. static uint32_t g_QueueFamily = (uint32_t)-1;
  37. static VkQueue g_Queue = VK_NULL_HANDLE;
  38. static VkPipelineCache g_PipelineCache = VK_NULL_HANDLE;
  39. static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE;
  40. static VkRenderPass g_RenderPass = VK_NULL_HANDLE;
  41. static void (*g_CheckVkResultFn)(VkResult err) = NULL;
  42. static VkDeviceSize g_BufferMemoryAlignment = 256;
  43. static VkPipelineCreateFlags g_PipelineCreateFlags = 0;
  44. static VkDescriptorSetLayout g_DescriptorSetLayout = VK_NULL_HANDLE;
  45. static VkPipelineLayout g_PipelineLayout = VK_NULL_HANDLE;
  46. static VkDescriptorSet g_DescriptorSet = VK_NULL_HANDLE;
  47. static VkPipeline g_Pipeline = VK_NULL_HANDLE;
  48. // Frame data
  49. struct FrameDataForRender
  50. {
  51. VkDeviceMemory VertexBufferMemory;
  52. VkDeviceMemory IndexBufferMemory;
  53. VkDeviceSize VertexBufferSize;
  54. VkDeviceSize IndexBufferSize;
  55. VkBuffer VertexBuffer;
  56. VkBuffer IndexBuffer;
  57. };
  58. static int g_FrameIndex = 0;
  59. static ImVector<FrameDataForRender> g_FramesDataBuffers = {};
  60. // Font data
  61. static VkSampler g_FontSampler = VK_NULL_HANDLE;
  62. static VkDeviceMemory g_FontMemory = VK_NULL_HANDLE;
  63. static VkImage g_FontImage = VK_NULL_HANDLE;
  64. static VkImageView g_FontView = VK_NULL_HANDLE;
  65. static VkDeviceMemory g_UploadBufferMemory = VK_NULL_HANDLE;
  66. static VkBuffer g_UploadBuffer = VK_NULL_HANDLE;
  67. // glsl_shader.vert, compiled with:
  68. // # glslangValidator -V -x -o glsl_shader.vert.u32 glsl_shader.vert
  69. /*
  70. #version 450 core
  71. layout(location = 0) in vec2 aPos;
  72. layout(location = 1) in vec2 aUV;
  73. layout(location = 2) in vec4 aColor;
  74. layout(push_constant) uniform uPushConstant { vec2 uScale; vec2 uTranslate; } pc;
  75. out gl_PerVertex { vec4 gl_Position; };
  76. layout(location = 0) out struct { vec4 Color; vec2 UV; } Out;
  77. void main()
  78. {
  79. Out.Color = aColor;
  80. Out.UV = aUV;
  81. gl_Position = vec4(aPos * pc.uScale + pc.uTranslate, 0, 1);
  82. }
  83. */
  84. static uint32_t __glsl_shader_vert_spv[] =
  85. {
  86. 0x07230203,0x00010000,0x00080001,0x0000002e,0x00000000,0x00020011,0x00000001,0x0006000b,
  87. 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001,
  88. 0x000a000f,0x00000000,0x00000004,0x6e69616d,0x00000000,0x0000000b,0x0000000f,0x00000015,
  89. 0x0000001b,0x0000001c,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d,
  90. 0x00000000,0x00030005,0x00000009,0x00000000,0x00050006,0x00000009,0x00000000,0x6f6c6f43,
  91. 0x00000072,0x00040006,0x00000009,0x00000001,0x00005655,0x00030005,0x0000000b,0x0074754f,
  92. 0x00040005,0x0000000f,0x6c6f4361,0x0000726f,0x00030005,0x00000015,0x00565561,0x00060005,
  93. 0x00000019,0x505f6c67,0x65567265,0x78657472,0x00000000,0x00060006,0x00000019,0x00000000,
  94. 0x505f6c67,0x7469736f,0x006e6f69,0x00030005,0x0000001b,0x00000000,0x00040005,0x0000001c,
  95. 0x736f5061,0x00000000,0x00060005,0x0000001e,0x73755075,0x6e6f4368,0x6e617473,0x00000074,
  96. 0x00050006,0x0000001e,0x00000000,0x61635375,0x0000656c,0x00060006,0x0000001e,0x00000001,
  97. 0x61725475,0x616c736e,0x00006574,0x00030005,0x00000020,0x00006370,0x00040047,0x0000000b,
  98. 0x0000001e,0x00000000,0x00040047,0x0000000f,0x0000001e,0x00000002,0x00040047,0x00000015,
  99. 0x0000001e,0x00000001,0x00050048,0x00000019,0x00000000,0x0000000b,0x00000000,0x00030047,
  100. 0x00000019,0x00000002,0x00040047,0x0000001c,0x0000001e,0x00000000,0x00050048,0x0000001e,
  101. 0x00000000,0x00000023,0x00000000,0x00050048,0x0000001e,0x00000001,0x00000023,0x00000008,
  102. 0x00030047,0x0000001e,0x00000002,0x00020013,0x00000002,0x00030021,0x00000003,0x00000002,
  103. 0x00030016,0x00000006,0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,0x00040017,
  104. 0x00000008,0x00000006,0x00000002,0x0004001e,0x00000009,0x00000007,0x00000008,0x00040020,
  105. 0x0000000a,0x00000003,0x00000009,0x0004003b,0x0000000a,0x0000000b,0x00000003,0x00040015,
  106. 0x0000000c,0x00000020,0x00000001,0x0004002b,0x0000000c,0x0000000d,0x00000000,0x00040020,
  107. 0x0000000e,0x00000001,0x00000007,0x0004003b,0x0000000e,0x0000000f,0x00000001,0x00040020,
  108. 0x00000011,0x00000003,0x00000007,0x0004002b,0x0000000c,0x00000013,0x00000001,0x00040020,
  109. 0x00000014,0x00000001,0x00000008,0x0004003b,0x00000014,0x00000015,0x00000001,0x00040020,
  110. 0x00000017,0x00000003,0x00000008,0x0003001e,0x00000019,0x00000007,0x00040020,0x0000001a,
  111. 0x00000003,0x00000019,0x0004003b,0x0000001a,0x0000001b,0x00000003,0x0004003b,0x00000014,
  112. 0x0000001c,0x00000001,0x0004001e,0x0000001e,0x00000008,0x00000008,0x00040020,0x0000001f,
  113. 0x00000009,0x0000001e,0x0004003b,0x0000001f,0x00000020,0x00000009,0x00040020,0x00000021,
  114. 0x00000009,0x00000008,0x0004002b,0x00000006,0x00000028,0x00000000,0x0004002b,0x00000006,
  115. 0x00000029,0x3f800000,0x00050036,0x00000002,0x00000004,0x00000000,0x00000003,0x000200f8,
  116. 0x00000005,0x0004003d,0x00000007,0x00000010,0x0000000f,0x00050041,0x00000011,0x00000012,
  117. 0x0000000b,0x0000000d,0x0003003e,0x00000012,0x00000010,0x0004003d,0x00000008,0x00000016,
  118. 0x00000015,0x00050041,0x00000017,0x00000018,0x0000000b,0x00000013,0x0003003e,0x00000018,
  119. 0x00000016,0x0004003d,0x00000008,0x0000001d,0x0000001c,0x00050041,0x00000021,0x00000022,
  120. 0x00000020,0x0000000d,0x0004003d,0x00000008,0x00000023,0x00000022,0x00050085,0x00000008,
  121. 0x00000024,0x0000001d,0x00000023,0x00050041,0x00000021,0x00000025,0x00000020,0x00000013,
  122. 0x0004003d,0x00000008,0x00000026,0x00000025,0x00050081,0x00000008,0x00000027,0x00000024,
  123. 0x00000026,0x00050051,0x00000006,0x0000002a,0x00000027,0x00000000,0x00050051,0x00000006,
  124. 0x0000002b,0x00000027,0x00000001,0x00070050,0x00000007,0x0000002c,0x0000002a,0x0000002b,
  125. 0x00000028,0x00000029,0x00050041,0x00000011,0x0000002d,0x0000001b,0x0000000d,0x0003003e,
  126. 0x0000002d,0x0000002c,0x000100fd,0x00010038
  127. };
  128. // glsl_shader.frag, compiled with:
  129. // # glslangValidator -V -x -o glsl_shader.frag.u32 glsl_shader.frag
  130. /*
  131. #version 450 core
  132. layout(location = 0) out vec4 fColor;
  133. layout(set=0, binding=0) uniform sampler2D sTexture;
  134. layout(location = 0) in struct { vec4 Color; vec2 UV; } In;
  135. void main()
  136. {
  137. fColor = In.Color * texture(sTexture, In.UV.st);
  138. }
  139. */
  140. static uint32_t __glsl_shader_frag_spv[] =
  141. {
  142. 0x07230203,0x00010000,0x00080001,0x0000001e,0x00000000,0x00020011,0x00000001,0x0006000b,
  143. 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001,
  144. 0x0007000f,0x00000004,0x00000004,0x6e69616d,0x00000000,0x00000009,0x0000000d,0x00030010,
  145. 0x00000004,0x00000007,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d,
  146. 0x00000000,0x00040005,0x00000009,0x6c6f4366,0x0000726f,0x00030005,0x0000000b,0x00000000,
  147. 0x00050006,0x0000000b,0x00000000,0x6f6c6f43,0x00000072,0x00040006,0x0000000b,0x00000001,
  148. 0x00005655,0x00030005,0x0000000d,0x00006e49,0x00050005,0x00000016,0x78655473,0x65727574,
  149. 0x00000000,0x00040047,0x00000009,0x0000001e,0x00000000,0x00040047,0x0000000d,0x0000001e,
  150. 0x00000000,0x00040047,0x00000016,0x00000022,0x00000000,0x00040047,0x00000016,0x00000021,
  151. 0x00000000,0x00020013,0x00000002,0x00030021,0x00000003,0x00000002,0x00030016,0x00000006,
  152. 0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,0x00040020,0x00000008,0x00000003,
  153. 0x00000007,0x0004003b,0x00000008,0x00000009,0x00000003,0x00040017,0x0000000a,0x00000006,
  154. 0x00000002,0x0004001e,0x0000000b,0x00000007,0x0000000a,0x00040020,0x0000000c,0x00000001,
  155. 0x0000000b,0x0004003b,0x0000000c,0x0000000d,0x00000001,0x00040015,0x0000000e,0x00000020,
  156. 0x00000001,0x0004002b,0x0000000e,0x0000000f,0x00000000,0x00040020,0x00000010,0x00000001,
  157. 0x00000007,0x00090019,0x00000013,0x00000006,0x00000001,0x00000000,0x00000000,0x00000000,
  158. 0x00000001,0x00000000,0x0003001b,0x00000014,0x00000013,0x00040020,0x00000015,0x00000000,
  159. 0x00000014,0x0004003b,0x00000015,0x00000016,0x00000000,0x0004002b,0x0000000e,0x00000018,
  160. 0x00000001,0x00040020,0x00000019,0x00000001,0x0000000a,0x00050036,0x00000002,0x00000004,
  161. 0x00000000,0x00000003,0x000200f8,0x00000005,0x00050041,0x00000010,0x00000011,0x0000000d,
  162. 0x0000000f,0x0004003d,0x00000007,0x00000012,0x00000011,0x0004003d,0x00000014,0x00000017,
  163. 0x00000016,0x00050041,0x00000019,0x0000001a,0x0000000d,0x00000018,0x0004003d,0x0000000a,
  164. 0x0000001b,0x0000001a,0x00050057,0x00000007,0x0000001c,0x00000017,0x0000001b,0x00050085,
  165. 0x00000007,0x0000001d,0x00000012,0x0000001c,0x0003003e,0x00000009,0x0000001d,0x000100fd,
  166. 0x00010038
  167. };
  168. static uint32_t ImGui_ImplVulkan_MemoryType(VkMemoryPropertyFlags properties, uint32_t type_bits)
  169. {
  170. VkPhysicalDeviceMemoryProperties prop;
  171. vkGetPhysicalDeviceMemoryProperties(g_PhysicalDevice, &prop);
  172. for (uint32_t i = 0; i < prop.memoryTypeCount; i++)
  173. if ((prop.memoryTypes[i].propertyFlags & properties) == properties && type_bits & (1<<i))
  174. return i;
  175. return 0xFFFFFFFF; // Unable to find memoryType
  176. }
  177. static void check_vk_result(VkResult err)
  178. {
  179. if (g_CheckVkResultFn)
  180. g_CheckVkResultFn(err);
  181. }
  182. static void CreateOrResizeBuffer(VkBuffer& buffer, VkDeviceMemory& buffer_memory, VkDeviceSize& p_buffer_size, size_t new_size, VkBufferUsageFlagBits usage)
  183. {
  184. VkResult err;
  185. if (buffer != VK_NULL_HANDLE)
  186. vkDestroyBuffer(g_Device, buffer, g_Allocator);
  187. if (buffer_memory)
  188. vkFreeMemory(g_Device, buffer_memory, g_Allocator);
  189. VkDeviceSize vertex_buffer_size_aligned = ((new_size - 1) / g_BufferMemoryAlignment + 1) * g_BufferMemoryAlignment;
  190. VkBufferCreateInfo buffer_info = {};
  191. buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  192. buffer_info.size = vertex_buffer_size_aligned;
  193. buffer_info.usage = usage;
  194. buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  195. err = vkCreateBuffer(g_Device, &buffer_info, g_Allocator, &buffer);
  196. check_vk_result(err);
  197. VkMemoryRequirements req;
  198. vkGetBufferMemoryRequirements(g_Device, buffer, &req);
  199. g_BufferMemoryAlignment = (g_BufferMemoryAlignment > req.alignment) ? g_BufferMemoryAlignment : req.alignment;
  200. VkMemoryAllocateInfo alloc_info = {};
  201. alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  202. alloc_info.allocationSize = req.size;
  203. alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
  204. err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &buffer_memory);
  205. check_vk_result(err);
  206. err = vkBindBufferMemory(g_Device, buffer, buffer_memory, 0);
  207. check_vk_result(err);
  208. p_buffer_size = new_size;
  209. }
  210. // Render function
  211. // (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
  212. void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer command_buffer)
  213. {
  214. // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
  215. int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x);
  216. int fb_height = (int)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y);
  217. if (fb_width <= 0 || fb_height <= 0 || draw_data->TotalVtxCount == 0)
  218. return;
  219. VkResult err;
  220. FrameDataForRender* fd = &g_FramesDataBuffers[g_FrameIndex];
  221. g_FrameIndex = (g_FrameIndex + 1) % g_FramesDataBuffers.size();
  222. // Create the Vertex and Index buffers:
  223. size_t vertex_size = draw_data->TotalVtxCount * sizeof(ImDrawVert);
  224. size_t index_size = draw_data->TotalIdxCount * sizeof(ImDrawIdx);
  225. if (fd->VertexBuffer == VK_NULL_HANDLE || fd->VertexBufferSize < vertex_size)
  226. CreateOrResizeBuffer(fd->VertexBuffer, fd->VertexBufferMemory, fd->VertexBufferSize, vertex_size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
  227. if (fd->IndexBuffer == VK_NULL_HANDLE || fd->IndexBufferSize < index_size)
  228. CreateOrResizeBuffer(fd->IndexBuffer, fd->IndexBufferMemory, fd->IndexBufferSize, index_size, VK_BUFFER_USAGE_INDEX_BUFFER_BIT);
  229. // Upload vertex/index data into a single contiguous GPU buffer
  230. {
  231. ImDrawVert* vtx_dst = NULL;
  232. ImDrawIdx* idx_dst = NULL;
  233. err = vkMapMemory(g_Device, fd->VertexBufferMemory, 0, vertex_size, 0, (void**)(&vtx_dst));
  234. check_vk_result(err);
  235. err = vkMapMemory(g_Device, fd->IndexBufferMemory, 0, index_size, 0, (void**)(&idx_dst));
  236. check_vk_result(err);
  237. for (int n = 0; n < draw_data->CmdListsCount; n++)
  238. {
  239. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  240. memcpy(vtx_dst, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert));
  241. memcpy(idx_dst, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx));
  242. vtx_dst += cmd_list->VtxBuffer.Size;
  243. idx_dst += cmd_list->IdxBuffer.Size;
  244. }
  245. VkMappedMemoryRange range[2] = {};
  246. range[0].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
  247. range[0].memory = fd->VertexBufferMemory;
  248. range[0].size = VK_WHOLE_SIZE;
  249. range[1].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
  250. range[1].memory = fd->IndexBufferMemory;
  251. range[1].size = VK_WHOLE_SIZE;
  252. err = vkFlushMappedMemoryRanges(g_Device, 2, range);
  253. check_vk_result(err);
  254. vkUnmapMemory(g_Device, fd->VertexBufferMemory);
  255. vkUnmapMemory(g_Device, fd->IndexBufferMemory);
  256. }
  257. // Bind pipeline and descriptor sets:
  258. {
  259. vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, g_Pipeline);
  260. VkDescriptorSet desc_set[1] = { g_DescriptorSet };
  261. vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, g_PipelineLayout, 0, 1, desc_set, 0, NULL);
  262. }
  263. // Bind Vertex And Index Buffer:
  264. {
  265. VkBuffer vertex_buffers[1] = { fd->VertexBuffer };
  266. VkDeviceSize vertex_offset[1] = { 0 };
  267. vkCmdBindVertexBuffers(command_buffer, 0, 1, vertex_buffers, vertex_offset);
  268. vkCmdBindIndexBuffer(command_buffer, fd->IndexBuffer, 0, sizeof(ImDrawIdx) == 2 ? VK_INDEX_TYPE_UINT16 : VK_INDEX_TYPE_UINT32);
  269. }
  270. // Setup viewport:
  271. {
  272. VkViewport viewport;
  273. viewport.x = 0;
  274. viewport.y = 0;
  275. viewport.width = (float)fb_width;
  276. viewport.height = (float)fb_height;
  277. viewport.minDepth = 0.0f;
  278. viewport.maxDepth = 1.0f;
  279. vkCmdSetViewport(command_buffer, 0, 1, &viewport);
  280. }
  281. // Setup scale and translation:
  282. // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayMin is typically (0,0) for single viewport apps.
  283. {
  284. float scale[2];
  285. scale[0] = 2.0f / draw_data->DisplaySize.x;
  286. scale[1] = 2.0f / draw_data->DisplaySize.y;
  287. float translate[2];
  288. translate[0] = -1.0f - draw_data->DisplayPos.x * scale[0];
  289. translate[1] = -1.0f - draw_data->DisplayPos.y * scale[1];
  290. vkCmdPushConstants(command_buffer, g_PipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, sizeof(float) * 0, sizeof(float) * 2, scale);
  291. vkCmdPushConstants(command_buffer, g_PipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, sizeof(float) * 2, sizeof(float) * 2, translate);
  292. }
  293. // Will project scissor/clipping rectangles into framebuffer space
  294. ImVec2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports
  295. ImVec2 clip_scale = draw_data->FramebufferScale; // (1,1) unless using retina display which are often (2,2)
  296. // Render command lists
  297. int vtx_offset = 0;
  298. int idx_offset = 0;
  299. for (int n = 0; n < draw_data->CmdListsCount; n++)
  300. {
  301. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  302. for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
  303. {
  304. const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
  305. if (pcmd->UserCallback)
  306. {
  307. // User callback (registered via ImDrawList::AddCallback)
  308. pcmd->UserCallback(cmd_list, pcmd);
  309. }
  310. else
  311. {
  312. // Project scissor/clipping rectangles into framebuffer space
  313. ImVec4 clip_rect;
  314. clip_rect.x = (pcmd->ClipRect.x - clip_off.x) * clip_scale.x;
  315. clip_rect.y = (pcmd->ClipRect.y - clip_off.y) * clip_scale.y;
  316. clip_rect.z = (pcmd->ClipRect.z - clip_off.x) * clip_scale.x;
  317. clip_rect.w = (pcmd->ClipRect.w - clip_off.y) * clip_scale.y;
  318. if (clip_rect.x < fb_width && clip_rect.y < fb_height && clip_rect.z >= 0.0f && clip_rect.w >= 0.0f)
  319. {
  320. // Negative offsets are illegal for vkCmdSetScissor
  321. if (clip_rect.x < 0.0f)
  322. clip_rect.x = 0.0f;
  323. if (clip_rect.y < 0.0f)
  324. clip_rect.y = 0.0f;
  325. // Apply scissor/clipping rectangle
  326. VkRect2D scissor;
  327. scissor.offset.x = (int32_t)(clip_rect.x);
  328. scissor.offset.y = (int32_t)(clip_rect.y);
  329. scissor.extent.width = (uint32_t)(clip_rect.z - clip_rect.x);
  330. scissor.extent.height = (uint32_t)(clip_rect.w - clip_rect.y);
  331. vkCmdSetScissor(command_buffer, 0, 1, &scissor);
  332. // Draw
  333. vkCmdDrawIndexed(command_buffer, pcmd->ElemCount, 1, idx_offset, vtx_offset, 0);
  334. }
  335. }
  336. idx_offset += pcmd->ElemCount;
  337. }
  338. vtx_offset += cmd_list->VtxBuffer.Size;
  339. }
  340. }
  341. bool ImGui_ImplVulkan_CreateFontsTexture(VkCommandBuffer command_buffer)
  342. {
  343. ImGuiIO& io = ImGui::GetIO();
  344. unsigned char* pixels;
  345. int width, height;
  346. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
  347. size_t upload_size = width*height*4*sizeof(char);
  348. VkResult err;
  349. // Create the Image:
  350. {
  351. VkImageCreateInfo info = {};
  352. info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
  353. info.imageType = VK_IMAGE_TYPE_2D;
  354. info.format = VK_FORMAT_R8G8B8A8_UNORM;
  355. info.extent.width = width;
  356. info.extent.height = height;
  357. info.extent.depth = 1;
  358. info.mipLevels = 1;
  359. info.arrayLayers = 1;
  360. info.samples = VK_SAMPLE_COUNT_1_BIT;
  361. info.tiling = VK_IMAGE_TILING_OPTIMAL;
  362. info.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
  363. info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  364. info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  365. err = vkCreateImage(g_Device, &info, g_Allocator, &g_FontImage);
  366. check_vk_result(err);
  367. VkMemoryRequirements req;
  368. vkGetImageMemoryRequirements(g_Device, g_FontImage, &req);
  369. VkMemoryAllocateInfo alloc_info = {};
  370. alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  371. alloc_info.allocationSize = req.size;
  372. alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, req.memoryTypeBits);
  373. err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &g_FontMemory);
  374. check_vk_result(err);
  375. err = vkBindImageMemory(g_Device, g_FontImage, g_FontMemory, 0);
  376. check_vk_result(err);
  377. }
  378. // Create the Image View:
  379. {
  380. VkImageViewCreateInfo info = {};
  381. info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
  382. info.image = g_FontImage;
  383. info.viewType = VK_IMAGE_VIEW_TYPE_2D;
  384. info.format = VK_FORMAT_R8G8B8A8_UNORM;
  385. info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  386. info.subresourceRange.levelCount = 1;
  387. info.subresourceRange.layerCount = 1;
  388. err = vkCreateImageView(g_Device, &info, g_Allocator, &g_FontView);
  389. check_vk_result(err);
  390. }
  391. // Update the Descriptor Set:
  392. {
  393. VkDescriptorImageInfo desc_image[1] = {};
  394. desc_image[0].sampler = g_FontSampler;
  395. desc_image[0].imageView = g_FontView;
  396. desc_image[0].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  397. VkWriteDescriptorSet write_desc[1] = {};
  398. write_desc[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  399. write_desc[0].dstSet = g_DescriptorSet;
  400. write_desc[0].descriptorCount = 1;
  401. write_desc[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
  402. write_desc[0].pImageInfo = desc_image;
  403. vkUpdateDescriptorSets(g_Device, 1, write_desc, 0, NULL);
  404. }
  405. // Create the Upload Buffer:
  406. {
  407. VkBufferCreateInfo buffer_info = {};
  408. buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  409. buffer_info.size = upload_size;
  410. buffer_info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
  411. buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  412. err = vkCreateBuffer(g_Device, &buffer_info, g_Allocator, &g_UploadBuffer);
  413. check_vk_result(err);
  414. VkMemoryRequirements req;
  415. vkGetBufferMemoryRequirements(g_Device, g_UploadBuffer, &req);
  416. g_BufferMemoryAlignment = (g_BufferMemoryAlignment > req.alignment) ? g_BufferMemoryAlignment : req.alignment;
  417. VkMemoryAllocateInfo alloc_info = {};
  418. alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  419. alloc_info.allocationSize = req.size;
  420. alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
  421. err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &g_UploadBufferMemory);
  422. check_vk_result(err);
  423. err = vkBindBufferMemory(g_Device, g_UploadBuffer, g_UploadBufferMemory, 0);
  424. check_vk_result(err);
  425. }
  426. // Upload to Buffer:
  427. {
  428. char* map = NULL;
  429. err = vkMapMemory(g_Device, g_UploadBufferMemory, 0, upload_size, 0, (void**)(&map));
  430. check_vk_result(err);
  431. memcpy(map, pixels, upload_size);
  432. VkMappedMemoryRange range[1] = {};
  433. range[0].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
  434. range[0].memory = g_UploadBufferMemory;
  435. range[0].size = upload_size;
  436. err = vkFlushMappedMemoryRanges(g_Device, 1, range);
  437. check_vk_result(err);
  438. vkUnmapMemory(g_Device, g_UploadBufferMemory);
  439. }
  440. // Copy to Image:
  441. {
  442. VkImageMemoryBarrier copy_barrier[1] = {};
  443. copy_barrier[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
  444. copy_barrier[0].dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
  445. copy_barrier[0].oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  446. copy_barrier[0].newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
  447. copy_barrier[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  448. copy_barrier[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  449. copy_barrier[0].image = g_FontImage;
  450. copy_barrier[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  451. copy_barrier[0].subresourceRange.levelCount = 1;
  452. copy_barrier[0].subresourceRange.layerCount = 1;
  453. vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 0, NULL, 1, copy_barrier);
  454. VkBufferImageCopy region = {};
  455. region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  456. region.imageSubresource.layerCount = 1;
  457. region.imageExtent.width = width;
  458. region.imageExtent.height = height;
  459. region.imageExtent.depth = 1;
  460. vkCmdCopyBufferToImage(command_buffer, g_UploadBuffer, g_FontImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
  461. VkImageMemoryBarrier use_barrier[1] = {};
  462. use_barrier[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
  463. use_barrier[0].srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
  464. use_barrier[0].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
  465. use_barrier[0].oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
  466. use_barrier[0].newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  467. use_barrier[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  468. use_barrier[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  469. use_barrier[0].image = g_FontImage;
  470. use_barrier[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  471. use_barrier[0].subresourceRange.levelCount = 1;
  472. use_barrier[0].subresourceRange.layerCount = 1;
  473. vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, NULL, 0, NULL, 1, use_barrier);
  474. }
  475. // Store our identifier
  476. io.Fonts->TexID = (ImTextureID)(intptr_t)g_FontImage;
  477. return true;
  478. }
  479. bool ImGui_ImplVulkan_CreateDeviceObjects()
  480. {
  481. VkResult err;
  482. VkShaderModule vert_module;
  483. VkShaderModule frag_module;
  484. // Create The Shader Modules:
  485. {
  486. VkShaderModuleCreateInfo vert_info = {};
  487. vert_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
  488. vert_info.codeSize = sizeof(__glsl_shader_vert_spv);
  489. vert_info.pCode = (uint32_t*)__glsl_shader_vert_spv;
  490. err = vkCreateShaderModule(g_Device, &vert_info, g_Allocator, &vert_module);
  491. check_vk_result(err);
  492. VkShaderModuleCreateInfo frag_info = {};
  493. frag_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
  494. frag_info.codeSize = sizeof(__glsl_shader_frag_spv);
  495. frag_info.pCode = (uint32_t*)__glsl_shader_frag_spv;
  496. err = vkCreateShaderModule(g_Device, &frag_info, g_Allocator, &frag_module);
  497. check_vk_result(err);
  498. }
  499. if (!g_FontSampler)
  500. {
  501. VkSamplerCreateInfo info = {};
  502. info.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
  503. info.magFilter = VK_FILTER_LINEAR;
  504. info.minFilter = VK_FILTER_LINEAR;
  505. info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
  506. info.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
  507. info.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
  508. info.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
  509. info.minLod = -1000;
  510. info.maxLod = 1000;
  511. info.maxAnisotropy = 1.0f;
  512. err = vkCreateSampler(g_Device, &info, g_Allocator, &g_FontSampler);
  513. check_vk_result(err);
  514. }
  515. if (!g_DescriptorSetLayout)
  516. {
  517. VkSampler sampler[1] = {g_FontSampler};
  518. VkDescriptorSetLayoutBinding binding[1] = {};
  519. binding[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
  520. binding[0].descriptorCount = 1;
  521. binding[0].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
  522. binding[0].pImmutableSamplers = sampler;
  523. VkDescriptorSetLayoutCreateInfo info = {};
  524. info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
  525. info.bindingCount = 1;
  526. info.pBindings = binding;
  527. err = vkCreateDescriptorSetLayout(g_Device, &info, g_Allocator, &g_DescriptorSetLayout);
  528. check_vk_result(err);
  529. }
  530. // Create Descriptor Set:
  531. {
  532. VkDescriptorSetAllocateInfo alloc_info = {};
  533. alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
  534. alloc_info.descriptorPool = g_DescriptorPool;
  535. alloc_info.descriptorSetCount = 1;
  536. alloc_info.pSetLayouts = &g_DescriptorSetLayout;
  537. err = vkAllocateDescriptorSets(g_Device, &alloc_info, &g_DescriptorSet);
  538. check_vk_result(err);
  539. }
  540. if (!g_PipelineLayout)
  541. {
  542. // Constants: we are using 'vec2 offset' and 'vec2 scale' instead of a full 3d projection matrix
  543. VkPushConstantRange push_constants[1] = {};
  544. push_constants[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
  545. push_constants[0].offset = sizeof(float) * 0;
  546. push_constants[0].size = sizeof(float) * 4;
  547. VkDescriptorSetLayout set_layout[1] = { g_DescriptorSetLayout };
  548. VkPipelineLayoutCreateInfo layout_info = {};
  549. layout_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
  550. layout_info.setLayoutCount = 1;
  551. layout_info.pSetLayouts = set_layout;
  552. layout_info.pushConstantRangeCount = 1;
  553. layout_info.pPushConstantRanges = push_constants;
  554. err = vkCreatePipelineLayout(g_Device, &layout_info, g_Allocator, &g_PipelineLayout);
  555. check_vk_result(err);
  556. }
  557. VkPipelineShaderStageCreateInfo stage[2] = {};
  558. stage[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
  559. stage[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
  560. stage[0].module = vert_module;
  561. stage[0].pName = "main";
  562. stage[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
  563. stage[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
  564. stage[1].module = frag_module;
  565. stage[1].pName = "main";
  566. VkVertexInputBindingDescription binding_desc[1] = {};
  567. binding_desc[0].stride = sizeof(ImDrawVert);
  568. binding_desc[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
  569. VkVertexInputAttributeDescription attribute_desc[3] = {};
  570. attribute_desc[0].location = 0;
  571. attribute_desc[0].binding = binding_desc[0].binding;
  572. attribute_desc[0].format = VK_FORMAT_R32G32_SFLOAT;
  573. attribute_desc[0].offset = IM_OFFSETOF(ImDrawVert, pos);
  574. attribute_desc[1].location = 1;
  575. attribute_desc[1].binding = binding_desc[0].binding;
  576. attribute_desc[1].format = VK_FORMAT_R32G32_SFLOAT;
  577. attribute_desc[1].offset = IM_OFFSETOF(ImDrawVert, uv);
  578. attribute_desc[2].location = 2;
  579. attribute_desc[2].binding = binding_desc[0].binding;
  580. attribute_desc[2].format = VK_FORMAT_R8G8B8A8_UNORM;
  581. attribute_desc[2].offset = IM_OFFSETOF(ImDrawVert, col);
  582. VkPipelineVertexInputStateCreateInfo vertex_info = {};
  583. vertex_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
  584. vertex_info.vertexBindingDescriptionCount = 1;
  585. vertex_info.pVertexBindingDescriptions = binding_desc;
  586. vertex_info.vertexAttributeDescriptionCount = 3;
  587. vertex_info.pVertexAttributeDescriptions = attribute_desc;
  588. VkPipelineInputAssemblyStateCreateInfo ia_info = {};
  589. ia_info.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
  590. ia_info.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
  591. VkPipelineViewportStateCreateInfo viewport_info = {};
  592. viewport_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
  593. viewport_info.viewportCount = 1;
  594. viewport_info.scissorCount = 1;
  595. VkPipelineRasterizationStateCreateInfo raster_info = {};
  596. raster_info.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
  597. raster_info.polygonMode = VK_POLYGON_MODE_FILL;
  598. raster_info.cullMode = VK_CULL_MODE_NONE;
  599. raster_info.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
  600. raster_info.lineWidth = 1.0f;
  601. VkPipelineMultisampleStateCreateInfo ms_info = {};
  602. ms_info.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
  603. ms_info.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
  604. VkPipelineColorBlendAttachmentState color_attachment[1] = {};
  605. color_attachment[0].blendEnable = VK_TRUE;
  606. color_attachment[0].srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
  607. color_attachment[0].dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
  608. color_attachment[0].colorBlendOp = VK_BLEND_OP_ADD;
  609. color_attachment[0].srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
  610. color_attachment[0].dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
  611. color_attachment[0].alphaBlendOp = VK_BLEND_OP_ADD;
  612. color_attachment[0].colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
  613. VkPipelineDepthStencilStateCreateInfo depth_info = {};
  614. depth_info.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
  615. VkPipelineColorBlendStateCreateInfo blend_info = {};
  616. blend_info.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
  617. blend_info.attachmentCount = 1;
  618. blend_info.pAttachments = color_attachment;
  619. VkDynamicState dynamic_states[2] = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
  620. VkPipelineDynamicStateCreateInfo dynamic_state = {};
  621. dynamic_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
  622. dynamic_state.dynamicStateCount = (uint32_t)IM_ARRAYSIZE(dynamic_states);
  623. dynamic_state.pDynamicStates = dynamic_states;
  624. VkGraphicsPipelineCreateInfo info = {};
  625. info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
  626. info.flags = g_PipelineCreateFlags;
  627. info.stageCount = 2;
  628. info.pStages = stage;
  629. info.pVertexInputState = &vertex_info;
  630. info.pInputAssemblyState = &ia_info;
  631. info.pViewportState = &viewport_info;
  632. info.pRasterizationState = &raster_info;
  633. info.pMultisampleState = &ms_info;
  634. info.pDepthStencilState = &depth_info;
  635. info.pColorBlendState = &blend_info;
  636. info.pDynamicState = &dynamic_state;
  637. info.layout = g_PipelineLayout;
  638. info.renderPass = g_RenderPass;
  639. err = vkCreateGraphicsPipelines(g_Device, g_PipelineCache, 1, &info, g_Allocator, &g_Pipeline);
  640. check_vk_result(err);
  641. vkDestroyShaderModule(g_Device, vert_module, g_Allocator);
  642. vkDestroyShaderModule(g_Device, frag_module, g_Allocator);
  643. return true;
  644. }
  645. void ImGui_ImplVulkan_InvalidateFontUploadObjects()
  646. {
  647. if (g_UploadBuffer)
  648. {
  649. vkDestroyBuffer(g_Device, g_UploadBuffer, g_Allocator);
  650. g_UploadBuffer = VK_NULL_HANDLE;
  651. }
  652. if (g_UploadBufferMemory)
  653. {
  654. vkFreeMemory(g_Device, g_UploadBufferMemory, g_Allocator);
  655. g_UploadBufferMemory = VK_NULL_HANDLE;
  656. }
  657. }
  658. void ImGui_ImplVulkan_InvalidateDeviceObjects()
  659. {
  660. ImGui_ImplVulkan_InvalidateFontUploadObjects();
  661. if (g_FontView) { vkDestroyImageView(g_Device, g_FontView, g_Allocator); g_FontView = VK_NULL_HANDLE; }
  662. if (g_FontImage) { vkDestroyImage(g_Device, g_FontImage, g_Allocator); g_FontImage = VK_NULL_HANDLE; }
  663. if (g_FontMemory) { vkFreeMemory(g_Device, g_FontMemory, g_Allocator); g_FontMemory = VK_NULL_HANDLE; }
  664. if (g_FontSampler) { vkDestroySampler(g_Device, g_FontSampler, g_Allocator); g_FontSampler = VK_NULL_HANDLE; }
  665. if (g_DescriptorSetLayout) { vkDestroyDescriptorSetLayout(g_Device, g_DescriptorSetLayout, g_Allocator); g_DescriptorSetLayout = VK_NULL_HANDLE; }
  666. if (g_PipelineLayout) { vkDestroyPipelineLayout(g_Device, g_PipelineLayout, g_Allocator); g_PipelineLayout = VK_NULL_HANDLE; }
  667. if (g_Pipeline) { vkDestroyPipeline(g_Device, g_Pipeline, g_Allocator); g_Pipeline = VK_NULL_HANDLE; }
  668. }
  669. void ImGui_ImplVulkan_InvalidateFrameDeviceObjects()
  670. {
  671. for (int i = 0; i < g_FramesDataBuffers.size(); i++)
  672. {
  673. FrameDataForRender* fd = &g_FramesDataBuffers[i];
  674. if (fd->VertexBuffer) { vkDestroyBuffer(g_Device, fd->VertexBuffer, g_Allocator); fd->VertexBuffer = VK_NULL_HANDLE; }
  675. if (fd->VertexBufferMemory) { vkFreeMemory (g_Device, fd->VertexBufferMemory, g_Allocator); fd->VertexBufferMemory = VK_NULL_HANDLE; }
  676. if (fd->IndexBuffer) { vkDestroyBuffer(g_Device, fd->IndexBuffer, g_Allocator); fd->IndexBuffer = VK_NULL_HANDLE; }
  677. if (fd->IndexBufferMemory) { vkFreeMemory (g_Device, fd->IndexBufferMemory, g_Allocator); fd->IndexBufferMemory = VK_NULL_HANDLE; }
  678. }
  679. }
  680. bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass render_pass)
  681. {
  682. ImGuiIO& io = ImGui::GetIO();
  683. io.BackendRendererName = "imgui_impl_vulkan";
  684. IM_ASSERT(info->Instance != VK_NULL_HANDLE);
  685. IM_ASSERT(info->PhysicalDevice != VK_NULL_HANDLE);
  686. IM_ASSERT(info->Device != VK_NULL_HANDLE);
  687. IM_ASSERT(info->Queue != VK_NULL_HANDLE);
  688. IM_ASSERT(info->DescriptorPool != VK_NULL_HANDLE);
  689. IM_ASSERT(render_pass != VK_NULL_HANDLE);
  690. g_Instance = info->Instance;
  691. g_PhysicalDevice = info->PhysicalDevice;
  692. g_Device = info->Device;
  693. g_QueueFamily = info->QueueFamily;
  694. g_Queue = info->Queue;
  695. g_RenderPass = render_pass;
  696. g_PipelineCache = info->PipelineCache;
  697. g_DescriptorPool = info->DescriptorPool;
  698. g_Allocator = info->Allocator;
  699. g_CheckVkResultFn = info->CheckVkResultFn;
  700. g_FramesDataBuffers.resize(info->QueuedFrames);
  701. for (int i = 0; i < g_FramesDataBuffers.size(); i++)
  702. g_FramesDataBuffers[i] = FrameDataForRender();
  703. ImGui_ImplVulkan_CreateDeviceObjects();
  704. return true;
  705. }
  706. void ImGui_ImplVulkan_Shutdown()
  707. {
  708. ImGui_ImplVulkan_InvalidateFrameDeviceObjects();
  709. ImGui_ImplVulkan_InvalidateDeviceObjects();
  710. }
  711. void ImGui_ImplVulkan_NewFrame()
  712. {
  713. }
  714. void ImGui_ImplVulkan_SetQueuedFramesCount(uint32_t count)
  715. {
  716. if (count == g_FramesDataBuffers.size())
  717. return;
  718. ImGui_ImplVulkan_InvalidateFrameDeviceObjects();
  719. uint32_t old_size = g_FramesDataBuffers.size();
  720. g_FramesDataBuffers.resize(count);
  721. for (uint32_t i = old_size; i < count; i++)
  722. g_FramesDataBuffers[i] = FrameDataForRender();
  723. g_FrameIndex = 0;
  724. }
  725. //-------------------------------------------------------------------------
  726. // Internal / Miscellaneous Vulkan Helpers
  727. //-------------------------------------------------------------------------
  728. // You probably do NOT need to use or care about those functions.
  729. // Those functions only exist because:
  730. // 1) they facilitate the readability and maintenance of the multiple main.cpp examples files.
  731. // 2) the upcoming multi-viewport feature will need them internally.
  732. // Generally we avoid exposing any kind of superfluous high-level helpers in the bindings,
  733. // but it is too much code to duplicate everywhere so we exceptionally expose them.
  734. // Your application/engine will likely already have code to setup all that stuff (swap chain, render pass, frame buffers, etc.).
  735. // You may read this code to learn about Vulkan, but it is recommended you use you own custom tailored code to do equivalent work.
  736. // (those functions do not interact with any of the state used by the regular ImGui_ImplVulkan_XXX functions)
  737. //-------------------------------------------------------------------------
  738. #include <stdlib.h> // malloc
  739. ImGui_ImplVulkanH_FrameData::ImGui_ImplVulkanH_FrameData()
  740. {
  741. BackbufferIndex = 0;
  742. CommandPool = VK_NULL_HANDLE;
  743. CommandBuffer = VK_NULL_HANDLE;
  744. Fence = VK_NULL_HANDLE;
  745. ImageAcquiredSemaphore = VK_NULL_HANDLE;
  746. RenderCompleteSemaphore = VK_NULL_HANDLE;
  747. }
  748. ImGui_ImplVulkanH_WindowData::ImGui_ImplVulkanH_WindowData()
  749. {
  750. Width = Height = 0;
  751. Swapchain = VK_NULL_HANDLE;
  752. Surface = VK_NULL_HANDLE;
  753. memset(&SurfaceFormat, 0, sizeof(SurfaceFormat));
  754. PresentMode = VK_PRESENT_MODE_MAX_ENUM_KHR;
  755. RenderPass = VK_NULL_HANDLE;
  756. ClearEnable = true;
  757. memset(&ClearValue, 0, sizeof(ClearValue));
  758. BackBufferCount = 0;
  759. memset(&BackBuffer, 0, sizeof(BackBuffer));
  760. memset(&BackBufferView, 0, sizeof(BackBufferView));
  761. memset(&Framebuffer, 0, sizeof(Framebuffer));
  762. FrameIndex = 0;
  763. }
  764. VkSurfaceFormatKHR ImGui_ImplVulkanH_SelectSurfaceFormat(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkFormat* request_formats, int request_formats_count, VkColorSpaceKHR request_color_space)
  765. {
  766. IM_ASSERT(request_formats != NULL);
  767. IM_ASSERT(request_formats_count > 0);
  768. // Per Spec Format and View Format are expected to be the same unless VK_IMAGE_CREATE_MUTABLE_BIT was set at image creation
  769. // Assuming that the default behavior is without setting this bit, there is no need for separate Swapchain image and image view format
  770. // Additionally several new color spaces were introduced with Vulkan Spec v1.0.40,
  771. // hence we must make sure that a format with the mostly available color space, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR, is found and used.
  772. uint32_t avail_count;
  773. vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface, &avail_count, NULL);
  774. ImVector<VkSurfaceFormatKHR> avail_format;
  775. avail_format.resize((int)avail_count);
  776. vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface, &avail_count, avail_format.Data);
  777. // First check if only one format, VK_FORMAT_UNDEFINED, is available, which would imply that any format is available
  778. if (avail_count == 1)
  779. {
  780. if (avail_format[0].format == VK_FORMAT_UNDEFINED)
  781. {
  782. VkSurfaceFormatKHR ret;
  783. ret.format = request_formats[0];
  784. ret.colorSpace = request_color_space;
  785. return ret;
  786. }
  787. else
  788. {
  789. // No point in searching another format
  790. return avail_format[0];
  791. }
  792. }
  793. else
  794. {
  795. // Request several formats, the first found will be used
  796. for (int request_i = 0; request_i < request_formats_count; request_i++)
  797. for (uint32_t avail_i = 0; avail_i < avail_count; avail_i++)
  798. if (avail_format[avail_i].format == request_formats[request_i] && avail_format[avail_i].colorSpace == request_color_space)
  799. return avail_format[avail_i];
  800. // If none of the requested image formats could be found, use the first available
  801. return avail_format[0];
  802. }
  803. }
  804. VkPresentModeKHR ImGui_ImplVulkanH_SelectPresentMode(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkPresentModeKHR* request_modes, int request_modes_count)
  805. {
  806. IM_ASSERT(request_modes != NULL);
  807. IM_ASSERT(request_modes_count > 0);
  808. // Request a certain mode and confirm that it is available. If not use VK_PRESENT_MODE_FIFO_KHR which is mandatory
  809. uint32_t avail_count = 0;
  810. vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface, &avail_count, NULL);
  811. ImVector<VkPresentModeKHR> avail_modes;
  812. avail_modes.resize((int)avail_count);
  813. vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface, &avail_count, avail_modes.Data);
  814. //for (uint32_t avail_i = 0; avail_i < avail_count; avail_i++)
  815. // printf("[vulkan] avail_modes[%d] = %d\n", avail_i, avail_modes[avail_i]);
  816. for (int request_i = 0; request_i < request_modes_count; request_i++)
  817. for (uint32_t avail_i = 0; avail_i < avail_count; avail_i++)
  818. if (request_modes[request_i] == avail_modes[avail_i])
  819. return request_modes[request_i];
  820. return VK_PRESENT_MODE_FIFO_KHR; // Always available
  821. }
  822. void ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(VkPhysicalDevice physical_device, VkDevice device, uint32_t queue_family, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator)
  823. {
  824. IM_ASSERT(physical_device != VK_NULL_HANDLE && device != VK_NULL_HANDLE);
  825. (void)physical_device;
  826. (void)allocator;
  827. // Create Command Buffers
  828. VkResult err;
  829. for (int i = 0; i < wd->Frames.size(); i++)
  830. {
  831. ImGui_ImplVulkanH_FrameData* fd = &wd->Frames[i];
  832. {
  833. VkCommandPoolCreateInfo info = {};
  834. info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
  835. info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
  836. info.queueFamilyIndex = queue_family;
  837. err = vkCreateCommandPool(device, &info, allocator, &fd->CommandPool);
  838. check_vk_result(err);
  839. }
  840. {
  841. VkCommandBufferAllocateInfo info = {};
  842. info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
  843. info.commandPool = fd->CommandPool;
  844. info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
  845. info.commandBufferCount = 1;
  846. err = vkAllocateCommandBuffers(device, &info, &fd->CommandBuffer);
  847. check_vk_result(err);
  848. }
  849. {
  850. VkFenceCreateInfo info = {};
  851. info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
  852. info.flags = VK_FENCE_CREATE_SIGNALED_BIT;
  853. err = vkCreateFence(device, &info, allocator, &fd->Fence);
  854. check_vk_result(err);
  855. }
  856. {
  857. VkSemaphoreCreateInfo info = {};
  858. info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
  859. err = vkCreateSemaphore(device, &info, allocator, &fd->ImageAcquiredSemaphore);
  860. check_vk_result(err);
  861. err = vkCreateSemaphore(device, &info, allocator, &fd->RenderCompleteSemaphore);
  862. check_vk_result(err);
  863. }
  864. }
  865. }
  866. int ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(VkPresentModeKHR present_mode)
  867. {
  868. if (present_mode == VK_PRESENT_MODE_MAILBOX_KHR)
  869. return 3;
  870. if (present_mode == VK_PRESENT_MODE_FIFO_KHR || present_mode == VK_PRESENT_MODE_FIFO_RELAXED_KHR)
  871. return 2;
  872. if (present_mode == VK_PRESENT_MODE_IMMEDIATE_KHR)
  873. return 1;
  874. IM_ASSERT(0);
  875. return 1;
  876. }
  877. void ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count)
  878. {
  879. VkResult err;
  880. VkSwapchainKHR old_swapchain = wd->Swapchain;
  881. err = vkDeviceWaitIdle(device);
  882. check_vk_result(err);
  883. // Destroy old Framebuffer
  884. for (uint32_t i = 0; i < wd->BackBufferCount; i++)
  885. {
  886. if (wd->BackBufferView[i])
  887. vkDestroyImageView(device, wd->BackBufferView[i], allocator);
  888. if (wd->Framebuffer[i])
  889. vkDestroyFramebuffer(device, wd->Framebuffer[i], allocator);
  890. }
  891. wd->BackBufferCount = 0;
  892. if (wd->RenderPass)
  893. vkDestroyRenderPass(device, wd->RenderPass, allocator);
  894. // If min image count was not specified, request different count of images dependent on selected present mode
  895. if (min_image_count == 0)
  896. min_image_count = ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(wd->PresentMode);
  897. // Create Swapchain
  898. {
  899. VkSwapchainCreateInfoKHR info = {};
  900. info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
  901. info.surface = wd->Surface;
  902. info.minImageCount = min_image_count;
  903. info.imageFormat = wd->SurfaceFormat.format;
  904. info.imageColorSpace = wd->SurfaceFormat.colorSpace;
  905. info.imageArrayLayers = 1;
  906. info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
  907. info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; // Assume that graphics family == present family
  908. info.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
  909. info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
  910. info.presentMode = wd->PresentMode;
  911. info.clipped = VK_TRUE;
  912. info.oldSwapchain = old_swapchain;
  913. VkSurfaceCapabilitiesKHR cap;
  914. err = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physical_device, wd->Surface, &cap);
  915. check_vk_result(err);
  916. if (info.minImageCount < cap.minImageCount)
  917. info.minImageCount = cap.minImageCount;
  918. else if (cap.maxImageCount != 0 && info.minImageCount > cap.maxImageCount)
  919. info.minImageCount = cap.maxImageCount;
  920. if (cap.currentExtent.width == 0xffffffff)
  921. {
  922. info.imageExtent.width = wd->Width = w;
  923. info.imageExtent.height = wd->Height = h;
  924. }
  925. else
  926. {
  927. info.imageExtent.width = wd->Width = cap.currentExtent.width;
  928. info.imageExtent.height = wd->Height = cap.currentExtent.height;
  929. }
  930. err = vkCreateSwapchainKHR(device, &info, allocator, &wd->Swapchain);
  931. check_vk_result(err);
  932. err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->BackBufferCount, NULL);
  933. check_vk_result(err);
  934. err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->BackBufferCount, wd->BackBuffer);
  935. check_vk_result(err);
  936. for (uint32_t i = 0; i < wd->Frames.size(); i++)
  937. ImGui_ImplVulkanH_DestroyFrameData(g_Instance, device, &wd->Frames[i], allocator);
  938. uint32_t old_size = wd->Frames.size();
  939. wd->Frames.resize(wd->BackBufferCount);
  940. for (uint32_t i = 0; i < wd->Frames.size(); i++)
  941. wd->Frames[i] = ImGui_ImplVulkanH_FrameData();
  942. }
  943. if (old_swapchain)
  944. vkDestroySwapchainKHR(device, old_swapchain, allocator);
  945. // Create the Render Pass
  946. {
  947. VkAttachmentDescription attachment = {};
  948. attachment.format = wd->SurfaceFormat.format;
  949. attachment.samples = VK_SAMPLE_COUNT_1_BIT;
  950. attachment.loadOp = wd->ClearEnable ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_DONT_CARE;
  951. attachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
  952. attachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
  953. attachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
  954. attachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  955. attachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
  956. VkAttachmentReference color_attachment = {};
  957. color_attachment.attachment = 0;
  958. color_attachment.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  959. VkSubpassDescription subpass = {};
  960. subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
  961. subpass.colorAttachmentCount = 1;
  962. subpass.pColorAttachments = &color_attachment;
  963. VkSubpassDependency dependency = {};
  964. dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
  965. dependency.dstSubpass = 0;
  966. dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
  967. dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
  968. dependency.srcAccessMask = 0;
  969. dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
  970. VkRenderPassCreateInfo info = {};
  971. info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
  972. info.attachmentCount = 1;
  973. info.pAttachments = &attachment;
  974. info.subpassCount = 1;
  975. info.pSubpasses = &subpass;
  976. info.dependencyCount = 1;
  977. info.pDependencies = &dependency;
  978. err = vkCreateRenderPass(device, &info, allocator, &wd->RenderPass);
  979. check_vk_result(err);
  980. }
  981. // Create The Image Views
  982. {
  983. VkImageViewCreateInfo info = {};
  984. info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
  985. info.viewType = VK_IMAGE_VIEW_TYPE_2D;
  986. info.format = wd->SurfaceFormat.format;
  987. info.components.r = VK_COMPONENT_SWIZZLE_R;
  988. info.components.g = VK_COMPONENT_SWIZZLE_G;
  989. info.components.b = VK_COMPONENT_SWIZZLE_B;
  990. info.components.a = VK_COMPONENT_SWIZZLE_A;
  991. VkImageSubresourceRange image_range = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
  992. info.subresourceRange = image_range;
  993. for (uint32_t i = 0; i < wd->BackBufferCount; i++)
  994. {
  995. info.image = wd->BackBuffer[i];
  996. err = vkCreateImageView(device, &info, allocator, &wd->BackBufferView[i]);
  997. check_vk_result(err);
  998. }
  999. }
  1000. // Create Framebuffer
  1001. {
  1002. VkImageView attachment[1];
  1003. VkFramebufferCreateInfo info = {};
  1004. info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
  1005. info.renderPass = wd->RenderPass;
  1006. info.attachmentCount = 1;
  1007. info.pAttachments = attachment;
  1008. info.width = wd->Width;
  1009. info.height = wd->Height;
  1010. info.layers = 1;
  1011. for (uint32_t i = 0; i < wd->BackBufferCount; i++)
  1012. {
  1013. attachment[0] = wd->BackBufferView[i];
  1014. err = vkCreateFramebuffer(device, &info, allocator, &wd->Framebuffer[i]);
  1015. check_vk_result(err);
  1016. }
  1017. }
  1018. }
  1019. void ImGui_ImplVulkanH_DestroyWindowData(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator)
  1020. {
  1021. vkDeviceWaitIdle(device); // FIXME: We could wait on the Queue if we had the queue in wd-> (otherwise VulkanH functions can't use globals)
  1022. //vkQueueWaitIdle(g_Queue);
  1023. for (int i = 0; i < wd->Frames.size(); i++)
  1024. {
  1025. ImGui_ImplVulkanH_FrameData* fd = &wd->Frames[i];
  1026. ImGui_ImplVulkanH_DestroyFrameData(instance, device, fd, allocator);
  1027. }
  1028. for (uint32_t i = 0; i < wd->BackBufferCount; i++)
  1029. {
  1030. vkDestroyImageView(device, wd->BackBufferView[i], allocator);
  1031. vkDestroyFramebuffer(device, wd->Framebuffer[i], allocator);
  1032. }
  1033. vkDestroyRenderPass(device, wd->RenderPass, allocator);
  1034. vkDestroySwapchainKHR(device, wd->Swapchain, allocator);
  1035. vkDestroySurfaceKHR(instance, wd->Surface, allocator);
  1036. *wd = ImGui_ImplVulkanH_WindowData();
  1037. }
  1038. void ImGui_ImplVulkanH_DestroyFrameData(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_FrameData* fd, const VkAllocationCallbacks* allocator)
  1039. {
  1040. vkDestroyFence(device, fd->Fence, allocator);
  1041. vkFreeCommandBuffers(device, fd->CommandPool, 1, &fd->CommandBuffer);
  1042. vkDestroyCommandPool(device, fd->CommandPool, allocator);
  1043. vkDestroySemaphore(device, fd->ImageAcquiredSemaphore, allocator);
  1044. vkDestroySemaphore(device, fd->RenderCompleteSemaphore, allocator);
  1045. }