imgui_impl_vulkan.cpp 57 KB

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