imgui_impl_vulkan.cpp 55 KB

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