imgui_impl_vulkan.cpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  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. {
  703. g_FramesDataBuffers[i] = FrameDataForRender();
  704. }
  705. ImGui_ImplVulkan_CreateDeviceObjects();
  706. return true;
  707. }
  708. void ImGui_ImplVulkan_Shutdown()
  709. {
  710. ImGui_ImplVulkan_InvalidateFrameDeviceObjects();
  711. ImGui_ImplVulkan_InvalidateDeviceObjects();
  712. }
  713. void ImGui_ImplVulkan_NewFrame()
  714. {
  715. }
  716. void ImGui_ImplVulkan_SetQueuedFramesCount(uint32_t count)
  717. {
  718. if (count == g_FramesDataBuffers.size())
  719. {
  720. return;
  721. }
  722. ImGui_ImplVulkan_InvalidateFrameDeviceObjects();
  723. uint32_t old_size = g_FramesDataBuffers.size();
  724. g_FramesDataBuffers.resize(count);
  725. for (uint32_t i = old_size; i < count; i++)
  726. {
  727. g_FramesDataBuffers[i] = FrameDataForRender();
  728. }
  729. g_FrameIndex = 0;
  730. }
  731. //-------------------------------------------------------------------------
  732. // Internal / Miscellaneous Vulkan Helpers
  733. //-------------------------------------------------------------------------
  734. // You probably do NOT need to use or care about those functions.
  735. // Those functions only exist because:
  736. // 1) they facilitate the readability and maintenance of the multiple main.cpp examples files.
  737. // 2) the upcoming multi-viewport feature will need them internally.
  738. // Generally we avoid exposing any kind of superfluous high-level helpers in the bindings,
  739. // but it is too much code to duplicate everywhere so we exceptionally expose them.
  740. // Your application/engine will likely already have code to setup all that stuff (swap chain, render pass, frame buffers, etc.).
  741. // You may read this code to learn about Vulkan, but it is recommended you use you own custom tailored code to do equivalent work.
  742. // (those functions do not interact with any of the state used by the regular ImGui_ImplVulkan_XXX functions)
  743. //-------------------------------------------------------------------------
  744. #include <stdlib.h> // malloc
  745. ImGui_ImplVulkanH_FrameData::ImGui_ImplVulkanH_FrameData()
  746. {
  747. BackbufferIndex = 0;
  748. CommandPool = VK_NULL_HANDLE;
  749. CommandBuffer = VK_NULL_HANDLE;
  750. Fence = VK_NULL_HANDLE;
  751. ImageAcquiredSemaphore = VK_NULL_HANDLE;
  752. RenderCompleteSemaphore = VK_NULL_HANDLE;
  753. }
  754. ImGui_ImplVulkanH_WindowData::ImGui_ImplVulkanH_WindowData()
  755. {
  756. Width = Height = 0;
  757. Swapchain = VK_NULL_HANDLE;
  758. Surface = VK_NULL_HANDLE;
  759. memset(&SurfaceFormat, 0, sizeof(SurfaceFormat));
  760. PresentMode = VK_PRESENT_MODE_MAX_ENUM_KHR;
  761. RenderPass = VK_NULL_HANDLE;
  762. ClearEnable = true;
  763. memset(&ClearValue, 0, sizeof(ClearValue));
  764. BackBufferCount = 0;
  765. memset(&BackBuffer, 0, sizeof(BackBuffer));
  766. memset(&BackBufferView, 0, sizeof(BackBufferView));
  767. memset(&Framebuffer, 0, sizeof(Framebuffer));
  768. FrameIndex = 0;
  769. }
  770. VkSurfaceFormatKHR ImGui_ImplVulkanH_SelectSurfaceFormat(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkFormat* request_formats, int request_formats_count, VkColorSpaceKHR request_color_space)
  771. {
  772. IM_ASSERT(request_formats != NULL);
  773. IM_ASSERT(request_formats_count > 0);
  774. // Per Spec Format and View Format are expected to be the same unless VK_IMAGE_CREATE_MUTABLE_BIT was set at image creation
  775. // Assuming that the default behavior is without setting this bit, there is no need for separate Swapchain image and image view format
  776. // Additionally several new color spaces were introduced with Vulkan Spec v1.0.40,
  777. // hence we must make sure that a format with the mostly available color space, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR, is found and used.
  778. uint32_t avail_count;
  779. vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface, &avail_count, NULL);
  780. ImVector<VkSurfaceFormatKHR> avail_format;
  781. avail_format.resize((int)avail_count);
  782. vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface, &avail_count, avail_format.Data);
  783. // First check if only one format, VK_FORMAT_UNDEFINED, is available, which would imply that any format is available
  784. if (avail_count == 1)
  785. {
  786. if (avail_format[0].format == VK_FORMAT_UNDEFINED)
  787. {
  788. VkSurfaceFormatKHR ret;
  789. ret.format = request_formats[0];
  790. ret.colorSpace = request_color_space;
  791. return ret;
  792. }
  793. else
  794. {
  795. // No point in searching another format
  796. return avail_format[0];
  797. }
  798. }
  799. else
  800. {
  801. // Request several formats, the first found will be used
  802. for (int request_i = 0; request_i < request_formats_count; request_i++)
  803. for (uint32_t avail_i = 0; avail_i < avail_count; avail_i++)
  804. if (avail_format[avail_i].format == request_formats[request_i] && avail_format[avail_i].colorSpace == request_color_space)
  805. return avail_format[avail_i];
  806. // If none of the requested image formats could be found, use the first available
  807. return avail_format[0];
  808. }
  809. }
  810. VkPresentModeKHR ImGui_ImplVulkanH_SelectPresentMode(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkPresentModeKHR* request_modes, int request_modes_count)
  811. {
  812. IM_ASSERT(request_modes != NULL);
  813. IM_ASSERT(request_modes_count > 0);
  814. // Request a certain mode and confirm that it is available. If not use VK_PRESENT_MODE_FIFO_KHR which is mandatory
  815. uint32_t avail_count = 0;
  816. vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface, &avail_count, NULL);
  817. ImVector<VkPresentModeKHR> avail_modes;
  818. avail_modes.resize((int)avail_count);
  819. vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface, &avail_count, avail_modes.Data);
  820. //for (uint32_t avail_i = 0; avail_i < avail_count; avail_i++)
  821. // printf("[vulkan] avail_modes[%d] = %d\n", avail_i, avail_modes[avail_i]);
  822. for (int request_i = 0; request_i < request_modes_count; request_i++)
  823. for (uint32_t avail_i = 0; avail_i < avail_count; avail_i++)
  824. if (request_modes[request_i] == avail_modes[avail_i])
  825. return request_modes[request_i];
  826. return VK_PRESENT_MODE_FIFO_KHR; // Always available
  827. }
  828. void ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(VkPhysicalDevice physical_device, VkDevice device, uint32_t queue_family, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator)
  829. {
  830. IM_ASSERT(physical_device != VK_NULL_HANDLE && device != VK_NULL_HANDLE);
  831. (void)physical_device;
  832. (void)allocator;
  833. // Create Command Buffers
  834. VkResult err;
  835. for (int i = 0; i < wd->Frames.size(); i++)
  836. {
  837. ImGui_ImplVulkanH_FrameData* fd = &wd->Frames[i];
  838. {
  839. VkCommandPoolCreateInfo info = {};
  840. info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
  841. info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
  842. info.queueFamilyIndex = queue_family;
  843. err = vkCreateCommandPool(device, &info, allocator, &fd->CommandPool);
  844. check_vk_result(err);
  845. }
  846. {
  847. VkCommandBufferAllocateInfo info = {};
  848. info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
  849. info.commandPool = fd->CommandPool;
  850. info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
  851. info.commandBufferCount = 1;
  852. err = vkAllocateCommandBuffers(device, &info, &fd->CommandBuffer);
  853. check_vk_result(err);
  854. }
  855. {
  856. VkFenceCreateInfo info = {};
  857. info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
  858. info.flags = VK_FENCE_CREATE_SIGNALED_BIT;
  859. err = vkCreateFence(device, &info, allocator, &fd->Fence);
  860. check_vk_result(err);
  861. }
  862. {
  863. VkSemaphoreCreateInfo info = {};
  864. info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
  865. err = vkCreateSemaphore(device, &info, allocator, &fd->ImageAcquiredSemaphore);
  866. check_vk_result(err);
  867. err = vkCreateSemaphore(device, &info, allocator, &fd->RenderCompleteSemaphore);
  868. check_vk_result(err);
  869. }
  870. }
  871. }
  872. int ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(VkPresentModeKHR present_mode)
  873. {
  874. if (present_mode == VK_PRESENT_MODE_MAILBOX_KHR)
  875. return 3;
  876. if (present_mode == VK_PRESENT_MODE_FIFO_KHR || present_mode == VK_PRESENT_MODE_FIFO_RELAXED_KHR)
  877. return 2;
  878. if (present_mode == VK_PRESENT_MODE_IMMEDIATE_KHR)
  879. return 1;
  880. IM_ASSERT(0);
  881. return 1;
  882. }
  883. 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)
  884. {
  885. VkResult err;
  886. VkSwapchainKHR old_swapchain = wd->Swapchain;
  887. err = vkDeviceWaitIdle(device);
  888. check_vk_result(err);
  889. // Destroy old Framebuffer
  890. for (uint32_t i = 0; i < wd->BackBufferCount; i++)
  891. {
  892. if (wd->BackBufferView[i])
  893. vkDestroyImageView(device, wd->BackBufferView[i], allocator);
  894. if (wd->Framebuffer[i])
  895. vkDestroyFramebuffer(device, wd->Framebuffer[i], allocator);
  896. }
  897. wd->BackBufferCount = 0;
  898. if (wd->RenderPass)
  899. vkDestroyRenderPass(device, wd->RenderPass, allocator);
  900. // If min image count was not specified, request different count of images dependent on selected present mode
  901. if (min_image_count == 0)
  902. min_image_count = ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(wd->PresentMode);
  903. // Create Swapchain
  904. {
  905. VkSwapchainCreateInfoKHR info = {};
  906. info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
  907. info.surface = wd->Surface;
  908. info.minImageCount = min_image_count;
  909. info.imageFormat = wd->SurfaceFormat.format;
  910. info.imageColorSpace = wd->SurfaceFormat.colorSpace;
  911. info.imageArrayLayers = 1;
  912. info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
  913. info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; // Assume that graphics family == present family
  914. info.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
  915. info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
  916. info.presentMode = wd->PresentMode;
  917. info.clipped = VK_TRUE;
  918. info.oldSwapchain = old_swapchain;
  919. VkSurfaceCapabilitiesKHR cap;
  920. err = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physical_device, wd->Surface, &cap);
  921. check_vk_result(err);
  922. if (info.minImageCount < cap.minImageCount)
  923. info.minImageCount = cap.minImageCount;
  924. else if (cap.maxImageCount != 0 && info.minImageCount > cap.maxImageCount)
  925. info.minImageCount = cap.maxImageCount;
  926. if (cap.currentExtent.width == 0xffffffff)
  927. {
  928. info.imageExtent.width = wd->Width = w;
  929. info.imageExtent.height = wd->Height = h;
  930. }
  931. else
  932. {
  933. info.imageExtent.width = wd->Width = cap.currentExtent.width;
  934. info.imageExtent.height = wd->Height = cap.currentExtent.height;
  935. }
  936. err = vkCreateSwapchainKHR(device, &info, allocator, &wd->Swapchain);
  937. check_vk_result(err);
  938. err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->BackBufferCount, NULL);
  939. check_vk_result(err);
  940. err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->BackBufferCount, wd->BackBuffer);
  941. check_vk_result(err);
  942. for (uint32_t i = 0; i < wd->Frames.size(); i++)
  943. {
  944. ImGui_ImplVulkanH_DestroyFrameData(g_Instance, device, &wd->Frames[i], allocator);
  945. }
  946. uint32_t old_size = wd->Frames.size();
  947. wd->Frames.resize(wd->BackBufferCount);
  948. for (uint32_t i = 0; i < wd->Frames.size(); i++)
  949. {
  950. wd->Frames[i] = ImGui_ImplVulkanH_FrameData();
  951. }
  952. }
  953. if (old_swapchain)
  954. vkDestroySwapchainKHR(device, old_swapchain, allocator);
  955. // Create the Render Pass
  956. {
  957. VkAttachmentDescription attachment = {};
  958. attachment.format = wd->SurfaceFormat.format;
  959. attachment.samples = VK_SAMPLE_COUNT_1_BIT;
  960. attachment.loadOp = wd->ClearEnable ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_DONT_CARE;
  961. attachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
  962. attachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
  963. attachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
  964. attachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  965. attachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
  966. VkAttachmentReference color_attachment = {};
  967. color_attachment.attachment = 0;
  968. color_attachment.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  969. VkSubpassDescription subpass = {};
  970. subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
  971. subpass.colorAttachmentCount = 1;
  972. subpass.pColorAttachments = &color_attachment;
  973. VkSubpassDependency dependency = {};
  974. dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
  975. dependency.dstSubpass = 0;
  976. dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
  977. dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
  978. dependency.srcAccessMask = 0;
  979. dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
  980. VkRenderPassCreateInfo info = {};
  981. info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
  982. info.attachmentCount = 1;
  983. info.pAttachments = &attachment;
  984. info.subpassCount = 1;
  985. info.pSubpasses = &subpass;
  986. info.dependencyCount = 1;
  987. info.pDependencies = &dependency;
  988. err = vkCreateRenderPass(device, &info, allocator, &wd->RenderPass);
  989. check_vk_result(err);
  990. }
  991. // Create The Image Views
  992. {
  993. VkImageViewCreateInfo info = {};
  994. info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
  995. info.viewType = VK_IMAGE_VIEW_TYPE_2D;
  996. info.format = wd->SurfaceFormat.format;
  997. info.components.r = VK_COMPONENT_SWIZZLE_R;
  998. info.components.g = VK_COMPONENT_SWIZZLE_G;
  999. info.components.b = VK_COMPONENT_SWIZZLE_B;
  1000. info.components.a = VK_COMPONENT_SWIZZLE_A;
  1001. VkImageSubresourceRange image_range = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
  1002. info.subresourceRange = image_range;
  1003. for (uint32_t i = 0; i < wd->BackBufferCount; i++)
  1004. {
  1005. info.image = wd->BackBuffer[i];
  1006. err = vkCreateImageView(device, &info, allocator, &wd->BackBufferView[i]);
  1007. check_vk_result(err);
  1008. }
  1009. }
  1010. // Create Framebuffer
  1011. {
  1012. VkImageView attachment[1];
  1013. VkFramebufferCreateInfo info = {};
  1014. info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
  1015. info.renderPass = wd->RenderPass;
  1016. info.attachmentCount = 1;
  1017. info.pAttachments = attachment;
  1018. info.width = wd->Width;
  1019. info.height = wd->Height;
  1020. info.layers = 1;
  1021. for (uint32_t i = 0; i < wd->BackBufferCount; i++)
  1022. {
  1023. attachment[0] = wd->BackBufferView[i];
  1024. err = vkCreateFramebuffer(device, &info, allocator, &wd->Framebuffer[i]);
  1025. check_vk_result(err);
  1026. }
  1027. }
  1028. }
  1029. void ImGui_ImplVulkanH_DestroyWindowData(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_WindowData* wd, const VkAllocationCallbacks* allocator)
  1030. {
  1031. vkDeviceWaitIdle(device); // FIXME: We could wait on the Queue if we had the queue in wd-> (otherwise VulkanH functions can't use globals)
  1032. //vkQueueWaitIdle(g_Queue);
  1033. for (int i = 0; i < wd->Frames.size(); i++)
  1034. {
  1035. ImGui_ImplVulkanH_FrameData* fd = &wd->Frames[i];
  1036. ImGui_ImplVulkanH_DestroyFrameData(instance, device, fd, allocator);
  1037. }
  1038. for (uint32_t i = 0; i < wd->BackBufferCount; i++)
  1039. {
  1040. vkDestroyImageView(device, wd->BackBufferView[i], allocator);
  1041. vkDestroyFramebuffer(device, wd->Framebuffer[i], allocator);
  1042. }
  1043. vkDestroyRenderPass(device, wd->RenderPass, allocator);
  1044. vkDestroySwapchainKHR(device, wd->Swapchain, allocator);
  1045. vkDestroySurfaceKHR(instance, wd->Surface, allocator);
  1046. *wd = ImGui_ImplVulkanH_WindowData();
  1047. }
  1048. void ImGui_ImplVulkanH_DestroyFrameData(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_FrameData* fd, const VkAllocationCallbacks* allocator)
  1049. {
  1050. vkDestroyFence(device, fd->Fence, allocator);
  1051. vkFreeCommandBuffers(device, fd->CommandPool, 1, &fd->CommandBuffer);
  1052. vkDestroyCommandPool(device, fd->CommandPool, allocator);
  1053. vkDestroySemaphore(device, fd->ImageAcquiredSemaphore, allocator);
  1054. vkDestroySemaphore(device, fd->RenderCompleteSemaphore, allocator);
  1055. }