imgui_impl_vulkan.cpp 71 KB

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