imgui_impl_vulkan.cpp 74 KB

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