imgui_impl_vulkan.cpp 73 KB

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