imgui_impl_vulkan.cpp 70 KB

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