imgui_impl_vulkan.cpp 70 KB

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