imgui_impl_vulkan.cpp 87 KB

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