imgui_impl_vulkan.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. // 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. // [ ] 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 use this binding you'll need to call 5 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXX_CreateFontsTexture(), ImGui_ImplXXXX_NewFrame(), ImGui_ImplXXXX_Render() and ImGui_ImplXXXX_Shutdown().
  7. // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
  8. // https://github.com/ocornut/imgui
  9. // CHANGELOG
  10. // (minor and older changes stripped away, please see git history for details)
  11. // 2018-03-01: Vulkan: Renamed ImGui_ImplVulkan_Init_Info to ImGui_ImplVulkan_InitInfo and fields to match more closely Vulkan terminology.
  12. // 2018-02-18: Vulkan: Offset projection matrix and clipping rectangle by io.DisplayPos (which will be non-zero for multi-viewport applications).
  13. // 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback, ImGui_ImplVulkan_Render() calls ImGui_ImplVulkan_RenderDrawData() itself.
  14. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  15. // 2017-05-15: Vulkan: Fix scissor offset being negative. Fix new Vulkan validation warnings. Set required depth member for buffer image copy.
  16. // 2016-11-13: Vulkan: Fix validation layer warnings and errors and redeclare gl_PerVertex.
  17. // 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.
  18. // 2016-08-27: Vulkan: Fix Vulkan example for use when a depth buffer is active.
  19. #include "imgui.h"
  20. #include "imgui_impl_vulkan.h"
  21. // Vulkan data
  22. static const VkAllocationCallbacks* g_Allocator = NULL;
  23. static VkPhysicalDevice g_PhysicalDevice = VK_NULL_HANDLE;
  24. static VkDevice g_Device = VK_NULL_HANDLE;
  25. static VkRenderPass g_RenderPass = VK_NULL_HANDLE;
  26. static VkPipelineCache g_PipelineCache = VK_NULL_HANDLE;
  27. static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE;
  28. static void (*g_CheckVkResult)(VkResult err) = NULL;
  29. static VkDeviceSize g_BufferMemoryAlignment = 256;
  30. static VkPipelineCreateFlags g_PipelineCreateFlags = 0;
  31. static VkDescriptorSetLayout g_DescriptorSetLayout = VK_NULL_HANDLE;
  32. static VkPipelineLayout g_PipelineLayout = VK_NULL_HANDLE;
  33. static VkDescriptorSet g_DescriptorSet = VK_NULL_HANDLE;
  34. static VkPipeline g_Pipeline = VK_NULL_HANDLE;
  35. // Frame data
  36. struct FrameDataForRender
  37. {
  38. VkDeviceMemory VertexBufferMemory;
  39. VkDeviceMemory IndexBufferMemory;
  40. VkDeviceSize VertexBufferSize;
  41. VkDeviceSize IndexBufferSize;
  42. VkBuffer VertexBuffer;
  43. VkBuffer IndexBuffer;
  44. };
  45. static int g_FrameIndex = 0;
  46. static FrameDataForRender g_FramesDataBuffers[IMGUI_VK_QUEUED_FRAMES];
  47. // Font data
  48. static VkSampler g_FontSampler = VK_NULL_HANDLE;
  49. static VkDeviceMemory g_FontMemory = VK_NULL_HANDLE;
  50. static VkImage g_FontImage = VK_NULL_HANDLE;
  51. static VkImageView g_FontView = VK_NULL_HANDLE;
  52. static VkDeviceMemory g_UploadBufferMemory = VK_NULL_HANDLE;
  53. static VkBuffer g_UploadBuffer = VK_NULL_HANDLE;
  54. // Forward Declarations
  55. static void ImGui_ImplVulkan_InitPlatformInterface();
  56. static void ImGui_ImplVulkan_ShutdownPlatformInterface();
  57. // glsl_shader.vert, compiled with:
  58. // # glslangValidator -V -x -o glsl_shader.vert.u32 glsl_shader.vert
  59. static uint32_t __glsl_shader_vert_spv[] =
  60. {
  61. 0x07230203,0x00010000,0x00080001,0x0000002e,0x00000000,0x00020011,0x00000001,0x0006000b,
  62. 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001,
  63. 0x000a000f,0x00000000,0x00000004,0x6e69616d,0x00000000,0x0000000b,0x0000000f,0x00000015,
  64. 0x0000001b,0x0000001c,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d,
  65. 0x00000000,0x00030005,0x00000009,0x00000000,0x00050006,0x00000009,0x00000000,0x6f6c6f43,
  66. 0x00000072,0x00040006,0x00000009,0x00000001,0x00005655,0x00030005,0x0000000b,0x0074754f,
  67. 0x00040005,0x0000000f,0x6c6f4361,0x0000726f,0x00030005,0x00000015,0x00565561,0x00060005,
  68. 0x00000019,0x505f6c67,0x65567265,0x78657472,0x00000000,0x00060006,0x00000019,0x00000000,
  69. 0x505f6c67,0x7469736f,0x006e6f69,0x00030005,0x0000001b,0x00000000,0x00040005,0x0000001c,
  70. 0x736f5061,0x00000000,0x00060005,0x0000001e,0x73755075,0x6e6f4368,0x6e617473,0x00000074,
  71. 0x00050006,0x0000001e,0x00000000,0x61635375,0x0000656c,0x00060006,0x0000001e,0x00000001,
  72. 0x61725475,0x616c736e,0x00006574,0x00030005,0x00000020,0x00006370,0x00040047,0x0000000b,
  73. 0x0000001e,0x00000000,0x00040047,0x0000000f,0x0000001e,0x00000002,0x00040047,0x00000015,
  74. 0x0000001e,0x00000001,0x00050048,0x00000019,0x00000000,0x0000000b,0x00000000,0x00030047,
  75. 0x00000019,0x00000002,0x00040047,0x0000001c,0x0000001e,0x00000000,0x00050048,0x0000001e,
  76. 0x00000000,0x00000023,0x00000000,0x00050048,0x0000001e,0x00000001,0x00000023,0x00000008,
  77. 0x00030047,0x0000001e,0x00000002,0x00020013,0x00000002,0x00030021,0x00000003,0x00000002,
  78. 0x00030016,0x00000006,0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,0x00040017,
  79. 0x00000008,0x00000006,0x00000002,0x0004001e,0x00000009,0x00000007,0x00000008,0x00040020,
  80. 0x0000000a,0x00000003,0x00000009,0x0004003b,0x0000000a,0x0000000b,0x00000003,0x00040015,
  81. 0x0000000c,0x00000020,0x00000001,0x0004002b,0x0000000c,0x0000000d,0x00000000,0x00040020,
  82. 0x0000000e,0x00000001,0x00000007,0x0004003b,0x0000000e,0x0000000f,0x00000001,0x00040020,
  83. 0x00000011,0x00000003,0x00000007,0x0004002b,0x0000000c,0x00000013,0x00000001,0x00040020,
  84. 0x00000014,0x00000001,0x00000008,0x0004003b,0x00000014,0x00000015,0x00000001,0x00040020,
  85. 0x00000017,0x00000003,0x00000008,0x0003001e,0x00000019,0x00000007,0x00040020,0x0000001a,
  86. 0x00000003,0x00000019,0x0004003b,0x0000001a,0x0000001b,0x00000003,0x0004003b,0x00000014,
  87. 0x0000001c,0x00000001,0x0004001e,0x0000001e,0x00000008,0x00000008,0x00040020,0x0000001f,
  88. 0x00000009,0x0000001e,0x0004003b,0x0000001f,0x00000020,0x00000009,0x00040020,0x00000021,
  89. 0x00000009,0x00000008,0x0004002b,0x00000006,0x00000028,0x00000000,0x0004002b,0x00000006,
  90. 0x00000029,0x3f800000,0x00050036,0x00000002,0x00000004,0x00000000,0x00000003,0x000200f8,
  91. 0x00000005,0x0004003d,0x00000007,0x00000010,0x0000000f,0x00050041,0x00000011,0x00000012,
  92. 0x0000000b,0x0000000d,0x0003003e,0x00000012,0x00000010,0x0004003d,0x00000008,0x00000016,
  93. 0x00000015,0x00050041,0x00000017,0x00000018,0x0000000b,0x00000013,0x0003003e,0x00000018,
  94. 0x00000016,0x0004003d,0x00000008,0x0000001d,0x0000001c,0x00050041,0x00000021,0x00000022,
  95. 0x00000020,0x0000000d,0x0004003d,0x00000008,0x00000023,0x00000022,0x00050085,0x00000008,
  96. 0x00000024,0x0000001d,0x00000023,0x00050041,0x00000021,0x00000025,0x00000020,0x00000013,
  97. 0x0004003d,0x00000008,0x00000026,0x00000025,0x00050081,0x00000008,0x00000027,0x00000024,
  98. 0x00000026,0x00050051,0x00000006,0x0000002a,0x00000027,0x00000000,0x00050051,0x00000006,
  99. 0x0000002b,0x00000027,0x00000001,0x00070050,0x00000007,0x0000002c,0x0000002a,0x0000002b,
  100. 0x00000028,0x00000029,0x00050041,0x00000011,0x0000002d,0x0000001b,0x0000000d,0x0003003e,
  101. 0x0000002d,0x0000002c,0x000100fd,0x00010038
  102. };
  103. // glsl_shader.frag, compiled with:
  104. // # glslangValidator -V -x -o glsl_shader.frag.u32 glsl_shader.frag
  105. static uint32_t __glsl_shader_frag_spv[] =
  106. {
  107. 0x07230203,0x00010000,0x00080001,0x0000001e,0x00000000,0x00020011,0x00000001,0x0006000b,
  108. 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001,
  109. 0x0007000f,0x00000004,0x00000004,0x6e69616d,0x00000000,0x00000009,0x0000000d,0x00030010,
  110. 0x00000004,0x00000007,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d,
  111. 0x00000000,0x00040005,0x00000009,0x6c6f4366,0x0000726f,0x00030005,0x0000000b,0x00000000,
  112. 0x00050006,0x0000000b,0x00000000,0x6f6c6f43,0x00000072,0x00040006,0x0000000b,0x00000001,
  113. 0x00005655,0x00030005,0x0000000d,0x00006e49,0x00050005,0x00000016,0x78655473,0x65727574,
  114. 0x00000000,0x00040047,0x00000009,0x0000001e,0x00000000,0x00040047,0x0000000d,0x0000001e,
  115. 0x00000000,0x00040047,0x00000016,0x00000022,0x00000000,0x00040047,0x00000016,0x00000021,
  116. 0x00000000,0x00020013,0x00000002,0x00030021,0x00000003,0x00000002,0x00030016,0x00000006,
  117. 0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,0x00040020,0x00000008,0x00000003,
  118. 0x00000007,0x0004003b,0x00000008,0x00000009,0x00000003,0x00040017,0x0000000a,0x00000006,
  119. 0x00000002,0x0004001e,0x0000000b,0x00000007,0x0000000a,0x00040020,0x0000000c,0x00000001,
  120. 0x0000000b,0x0004003b,0x0000000c,0x0000000d,0x00000001,0x00040015,0x0000000e,0x00000020,
  121. 0x00000001,0x0004002b,0x0000000e,0x0000000f,0x00000000,0x00040020,0x00000010,0x00000001,
  122. 0x00000007,0x00090019,0x00000013,0x00000006,0x00000001,0x00000000,0x00000000,0x00000000,
  123. 0x00000001,0x00000000,0x0003001b,0x00000014,0x00000013,0x00040020,0x00000015,0x00000000,
  124. 0x00000014,0x0004003b,0x00000015,0x00000016,0x00000000,0x0004002b,0x0000000e,0x00000018,
  125. 0x00000001,0x00040020,0x00000019,0x00000001,0x0000000a,0x00050036,0x00000002,0x00000004,
  126. 0x00000000,0x00000003,0x000200f8,0x00000005,0x00050041,0x00000010,0x00000011,0x0000000d,
  127. 0x0000000f,0x0004003d,0x00000007,0x00000012,0x00000011,0x0004003d,0x00000014,0x00000017,
  128. 0x00000016,0x00050041,0x00000019,0x0000001a,0x0000000d,0x00000018,0x0004003d,0x0000000a,
  129. 0x0000001b,0x0000001a,0x00050057,0x00000007,0x0000001c,0x00000017,0x0000001b,0x00050085,
  130. 0x00000007,0x0000001d,0x00000012,0x0000001c,0x0003003e,0x00000009,0x0000001d,0x000100fd,
  131. 0x00010038
  132. };
  133. static uint32_t ImGui_ImplVulkan_MemoryType(VkMemoryPropertyFlags properties, uint32_t type_bits)
  134. {
  135. VkPhysicalDeviceMemoryProperties prop;
  136. vkGetPhysicalDeviceMemoryProperties(g_PhysicalDevice, &prop);
  137. for (uint32_t i = 0; i < prop.memoryTypeCount; i++)
  138. if ((prop.memoryTypes[i].propertyFlags & properties) == properties && type_bits & (1<<i))
  139. return i;
  140. return 0xffffffff; // Unable to find memoryType
  141. }
  142. static void ImGui_ImplVulkan_VkResult(VkResult err)
  143. {
  144. if (g_CheckVkResult)
  145. g_CheckVkResult(err);
  146. }
  147. static void CreateOrResizeBuffer(VkBuffer& buffer, VkDeviceMemory& buffer_memory, VkDeviceSize& p_buffer_size, size_t new_size, VkBufferUsageFlagBits usage)
  148. {
  149. VkResult err;
  150. if (buffer != NULL)
  151. vkDestroyBuffer(g_Device, buffer, g_Allocator);
  152. if (buffer_memory)
  153. vkFreeMemory(g_Device, buffer_memory, g_Allocator);
  154. VkDeviceSize vertex_buffer_size_aligned = ((new_size - 1) / g_BufferMemoryAlignment + 1) * g_BufferMemoryAlignment;
  155. VkBufferCreateInfo buffer_info = {};
  156. buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  157. buffer_info.size = vertex_buffer_size_aligned;
  158. buffer_info.usage = usage;
  159. buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  160. err = vkCreateBuffer(g_Device, &buffer_info, g_Allocator, &buffer);
  161. ImGui_ImplVulkan_VkResult(err);
  162. VkMemoryRequirements req;
  163. vkGetBufferMemoryRequirements(g_Device, buffer, &req);
  164. g_BufferMemoryAlignment = (g_BufferMemoryAlignment > req.alignment) ? g_BufferMemoryAlignment : req.alignment;
  165. VkMemoryAllocateInfo alloc_info = {};
  166. alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  167. alloc_info.allocationSize = req.size;
  168. alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
  169. err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &buffer_memory);
  170. ImGui_ImplVulkan_VkResult(err);
  171. err = vkBindBufferMemory(g_Device, buffer, buffer_memory, 0);
  172. ImGui_ImplVulkan_VkResult(err);
  173. p_buffer_size = new_size;
  174. }
  175. // This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
  176. void ImGui_ImplVulkan_RenderDrawData(VkCommandBuffer command_buffer, ImDrawData* draw_data)
  177. {
  178. VkResult err;
  179. ImGuiIO& io = ImGui::GetIO();
  180. if (draw_data->TotalVtxCount == 0)
  181. return;
  182. FrameDataForRender* fd = &g_FramesDataBuffers[g_FrameIndex];
  183. // Create the Vertex and Index buffers:
  184. size_t vertex_size = draw_data->TotalVtxCount * sizeof(ImDrawVert);
  185. size_t index_size = draw_data->TotalIdxCount * sizeof(ImDrawIdx);
  186. if (!fd->VertexBuffer || fd->VertexBufferSize < vertex_size)
  187. CreateOrResizeBuffer(fd->VertexBuffer, fd->VertexBufferMemory, fd->VertexBufferSize, vertex_size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
  188. if (!fd->IndexBuffer || fd->IndexBufferSize < index_size)
  189. CreateOrResizeBuffer(fd->IndexBuffer, fd->IndexBufferMemory, fd->IndexBufferSize, index_size, VK_BUFFER_USAGE_INDEX_BUFFER_BIT);
  190. // Upload Vertex and index Data:
  191. {
  192. ImDrawVert* vtx_dst;
  193. ImDrawIdx* idx_dst;
  194. err = vkMapMemory(g_Device, fd->VertexBufferMemory, 0, vertex_size, 0, (void**)(&vtx_dst));
  195. ImGui_ImplVulkan_VkResult(err);
  196. err = vkMapMemory(g_Device, fd->IndexBufferMemory, 0, index_size, 0, (void**)(&idx_dst));
  197. ImGui_ImplVulkan_VkResult(err);
  198. for (int n = 0; n < draw_data->CmdListsCount; n++)
  199. {
  200. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  201. memcpy(vtx_dst, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert));
  202. memcpy(idx_dst, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx));
  203. vtx_dst += cmd_list->VtxBuffer.Size;
  204. idx_dst += cmd_list->IdxBuffer.Size;
  205. }
  206. VkMappedMemoryRange range[2] = {};
  207. range[0].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
  208. range[0].memory = fd->VertexBufferMemory;
  209. range[0].size = VK_WHOLE_SIZE;
  210. range[1].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
  211. range[1].memory = fd->IndexBufferMemory;
  212. range[1].size = VK_WHOLE_SIZE;
  213. err = vkFlushMappedMemoryRanges(g_Device, 2, range);
  214. ImGui_ImplVulkan_VkResult(err);
  215. vkUnmapMemory(g_Device, fd->VertexBufferMemory);
  216. vkUnmapMemory(g_Device, fd->IndexBufferMemory);
  217. }
  218. // Bind pipeline and descriptor sets:
  219. {
  220. vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, g_Pipeline);
  221. VkDescriptorSet desc_set[1] = { g_DescriptorSet };
  222. vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, g_PipelineLayout, 0, 1, desc_set, 0, NULL);
  223. }
  224. // Bind Vertex And Index Buffer:
  225. {
  226. VkBuffer vertex_buffers[1] = { fd->VertexBuffer };
  227. VkDeviceSize vertex_offset[1] = { 0 };
  228. vkCmdBindVertexBuffers(command_buffer, 0, 1, vertex_buffers, vertex_offset);
  229. vkCmdBindIndexBuffer(command_buffer, fd->IndexBuffer, 0, VK_INDEX_TYPE_UINT16);
  230. }
  231. // Setup viewport:
  232. {
  233. VkViewport viewport;
  234. viewport.x = 0;
  235. viewport.y = 0;
  236. viewport.width = io.DisplaySize.x;
  237. viewport.height = io.DisplaySize.y;
  238. viewport.minDepth = 0.0f;
  239. viewport.maxDepth = 1.0f;
  240. vkCmdSetViewport(command_buffer, 0, 1, &viewport);
  241. }
  242. // Setup scale and translation:
  243. // (Our visible imgui space lies from io.DisplayPos (top left) to io.DisplayPos+io.DisplaySize (bottom right). io.DisplayPos is typically (0,0) for single viewport applications.)
  244. {
  245. float scale[2];
  246. scale[0] = 2.0f / io.DisplaySize.x;
  247. scale[1] = 2.0f / io.DisplaySize.y;
  248. float translate[2];
  249. translate[0] = -1.0f - io.DisplayPos.x * scale[0];
  250. translate[1] = -1.0f - io.DisplayPos.y * scale[1];
  251. vkCmdPushConstants(command_buffer, g_PipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, sizeof(float) * 0, sizeof(float) * 2, scale);
  252. vkCmdPushConstants(command_buffer, g_PipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, sizeof(float) * 2, sizeof(float) * 2, translate);
  253. }
  254. // Render the command lists:
  255. int vtx_offset = 0;
  256. int idx_offset = 0;
  257. for (int n = 0; n < draw_data->CmdListsCount; n++)
  258. {
  259. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  260. for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
  261. {
  262. const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
  263. if (pcmd->UserCallback)
  264. {
  265. pcmd->UserCallback(cmd_list, pcmd);
  266. }
  267. else
  268. {
  269. // Apply scissor/clipping rectangle
  270. // FIXME: We could clamp width/height based on clamped min/max values.
  271. VkRect2D scissor;
  272. scissor.offset.x = (int32_t)(pcmd->ClipRect.x - io.DisplayPos.x) > 0 ? (int32_t)(pcmd->ClipRect.x - io.DisplayPos.y) : 0;
  273. scissor.offset.y = (int32_t)(pcmd->ClipRect.y - io.DisplayPos.y) > 0 ? (int32_t)(pcmd->ClipRect.y - io.DisplayPos.y) : 0;
  274. scissor.extent.width = (uint32_t)(pcmd->ClipRect.z - pcmd->ClipRect.x);
  275. scissor.extent.height = (uint32_t)(pcmd->ClipRect.w - pcmd->ClipRect.y + 1); // FIXME: Why +1 here?
  276. vkCmdSetScissor(command_buffer, 0, 1, &scissor);
  277. // Draw
  278. vkCmdDrawIndexed(command_buffer, pcmd->ElemCount, 1, idx_offset, vtx_offset, 0);
  279. }
  280. idx_offset += pcmd->ElemCount;
  281. }
  282. vtx_offset += cmd_list->VtxBuffer.Size;
  283. }
  284. }
  285. bool ImGui_ImplVulkan_CreateFontsTexture(VkCommandBuffer command_buffer)
  286. {
  287. ImGuiIO& io = ImGui::GetIO();
  288. unsigned char* pixels;
  289. int width, height;
  290. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
  291. size_t upload_size = width*height*4*sizeof(char);
  292. VkResult err;
  293. // Create the Image:
  294. {
  295. VkImageCreateInfo info = {};
  296. info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
  297. info.imageType = VK_IMAGE_TYPE_2D;
  298. info.format = VK_FORMAT_R8G8B8A8_UNORM;
  299. info.extent.width = width;
  300. info.extent.height = height;
  301. info.extent.depth = 1;
  302. info.mipLevels = 1;
  303. info.arrayLayers = 1;
  304. info.samples = VK_SAMPLE_COUNT_1_BIT;
  305. info.tiling = VK_IMAGE_TILING_OPTIMAL;
  306. info.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
  307. info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  308. info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  309. err = vkCreateImage(g_Device, &info, g_Allocator, &g_FontImage);
  310. ImGui_ImplVulkan_VkResult(err);
  311. VkMemoryRequirements req;
  312. vkGetImageMemoryRequirements(g_Device, g_FontImage, &req);
  313. VkMemoryAllocateInfo alloc_info = {};
  314. alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  315. alloc_info.allocationSize = req.size;
  316. alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, req.memoryTypeBits);
  317. err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &g_FontMemory);
  318. ImGui_ImplVulkan_VkResult(err);
  319. err = vkBindImageMemory(g_Device, g_FontImage, g_FontMemory, 0);
  320. ImGui_ImplVulkan_VkResult(err);
  321. }
  322. // Create the Image View:
  323. {
  324. VkImageViewCreateInfo info = {};
  325. info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
  326. info.image = g_FontImage;
  327. info.viewType = VK_IMAGE_VIEW_TYPE_2D;
  328. info.format = VK_FORMAT_R8G8B8A8_UNORM;
  329. info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  330. info.subresourceRange.levelCount = 1;
  331. info.subresourceRange.layerCount = 1;
  332. err = vkCreateImageView(g_Device, &info, g_Allocator, &g_FontView);
  333. ImGui_ImplVulkan_VkResult(err);
  334. }
  335. // Update the Descriptor Set:
  336. {
  337. VkDescriptorImageInfo desc_image[1] = {};
  338. desc_image[0].sampler = g_FontSampler;
  339. desc_image[0].imageView = g_FontView;
  340. desc_image[0].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  341. VkWriteDescriptorSet write_desc[1] = {};
  342. write_desc[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  343. write_desc[0].dstSet = g_DescriptorSet;
  344. write_desc[0].descriptorCount = 1;
  345. write_desc[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
  346. write_desc[0].pImageInfo = desc_image;
  347. vkUpdateDescriptorSets(g_Device, 1, write_desc, 0, NULL);
  348. }
  349. // Create the Upload Buffer:
  350. {
  351. VkBufferCreateInfo buffer_info = {};
  352. buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  353. buffer_info.size = upload_size;
  354. buffer_info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
  355. buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  356. err = vkCreateBuffer(g_Device, &buffer_info, g_Allocator, &g_UploadBuffer);
  357. ImGui_ImplVulkan_VkResult(err);
  358. VkMemoryRequirements req;
  359. vkGetBufferMemoryRequirements(g_Device, g_UploadBuffer, &req);
  360. g_BufferMemoryAlignment = (g_BufferMemoryAlignment > req.alignment) ? g_BufferMemoryAlignment : req.alignment;
  361. VkMemoryAllocateInfo alloc_info = {};
  362. alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  363. alloc_info.allocationSize = req.size;
  364. alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
  365. err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &g_UploadBufferMemory);
  366. ImGui_ImplVulkan_VkResult(err);
  367. err = vkBindBufferMemory(g_Device, g_UploadBuffer, g_UploadBufferMemory, 0);
  368. ImGui_ImplVulkan_VkResult(err);
  369. }
  370. // Upload to Buffer:
  371. {
  372. char* map = NULL;
  373. err = vkMapMemory(g_Device, g_UploadBufferMemory, 0, upload_size, 0, (void**)(&map));
  374. ImGui_ImplVulkan_VkResult(err);
  375. memcpy(map, pixels, upload_size);
  376. VkMappedMemoryRange range[1] = {};
  377. range[0].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
  378. range[0].memory = g_UploadBufferMemory;
  379. range[0].size = upload_size;
  380. err = vkFlushMappedMemoryRanges(g_Device, 1, range);
  381. ImGui_ImplVulkan_VkResult(err);
  382. vkUnmapMemory(g_Device, g_UploadBufferMemory);
  383. }
  384. // Copy to Image:
  385. {
  386. VkImageMemoryBarrier copy_barrier[1] = {};
  387. copy_barrier[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
  388. copy_barrier[0].dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
  389. copy_barrier[0].oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  390. copy_barrier[0].newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
  391. copy_barrier[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  392. copy_barrier[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  393. copy_barrier[0].image = g_FontImage;
  394. copy_barrier[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  395. copy_barrier[0].subresourceRange.levelCount = 1;
  396. copy_barrier[0].subresourceRange.layerCount = 1;
  397. vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 0, NULL, 1, copy_barrier);
  398. VkBufferImageCopy region = {};
  399. region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  400. region.imageSubresource.layerCount = 1;
  401. region.imageExtent.width = width;
  402. region.imageExtent.height = height;
  403. region.imageExtent.depth = 1;
  404. vkCmdCopyBufferToImage(command_buffer, g_UploadBuffer, g_FontImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
  405. VkImageMemoryBarrier use_barrier[1] = {};
  406. use_barrier[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
  407. use_barrier[0].srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
  408. use_barrier[0].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
  409. use_barrier[0].oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
  410. use_barrier[0].newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  411. use_barrier[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  412. use_barrier[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  413. use_barrier[0].image = g_FontImage;
  414. use_barrier[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  415. use_barrier[0].subresourceRange.levelCount = 1;
  416. use_barrier[0].subresourceRange.layerCount = 1;
  417. vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, NULL, 0, NULL, 1, use_barrier);
  418. }
  419. // Store our identifier
  420. io.Fonts->TexID = (void *)(intptr_t)g_FontImage;
  421. return true;
  422. }
  423. bool ImGui_ImplVulkan_CreateDeviceObjects()
  424. {
  425. VkResult err;
  426. VkShaderModule vert_module;
  427. VkShaderModule frag_module;
  428. // Create The Shader Modules:
  429. {
  430. VkShaderModuleCreateInfo vert_info = {};
  431. vert_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
  432. vert_info.codeSize = sizeof(__glsl_shader_vert_spv);
  433. vert_info.pCode = (uint32_t*)__glsl_shader_vert_spv;
  434. err = vkCreateShaderModule(g_Device, &vert_info, g_Allocator, &vert_module);
  435. ImGui_ImplVulkan_VkResult(err);
  436. VkShaderModuleCreateInfo frag_info = {};
  437. frag_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
  438. frag_info.codeSize = sizeof(__glsl_shader_frag_spv);
  439. frag_info.pCode = (uint32_t*)__glsl_shader_frag_spv;
  440. err = vkCreateShaderModule(g_Device, &frag_info, g_Allocator, &frag_module);
  441. ImGui_ImplVulkan_VkResult(err);
  442. }
  443. if (!g_FontSampler)
  444. {
  445. VkSamplerCreateInfo info = {};
  446. info.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
  447. info.magFilter = VK_FILTER_LINEAR;
  448. info.minFilter = VK_FILTER_LINEAR;
  449. info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
  450. info.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
  451. info.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
  452. info.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
  453. info.minLod = -1000;
  454. info.maxLod = 1000;
  455. info.maxAnisotropy = 1.0f;
  456. err = vkCreateSampler(g_Device, &info, g_Allocator, &g_FontSampler);
  457. ImGui_ImplVulkan_VkResult(err);
  458. }
  459. if (!g_DescriptorSetLayout)
  460. {
  461. VkSampler sampler[1] = {g_FontSampler};
  462. VkDescriptorSetLayoutBinding binding[1] = {};
  463. binding[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
  464. binding[0].descriptorCount = 1;
  465. binding[0].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
  466. binding[0].pImmutableSamplers = sampler;
  467. VkDescriptorSetLayoutCreateInfo info = {};
  468. info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
  469. info.bindingCount = 1;
  470. info.pBindings = binding;
  471. err = vkCreateDescriptorSetLayout(g_Device, &info, g_Allocator, &g_DescriptorSetLayout);
  472. ImGui_ImplVulkan_VkResult(err);
  473. }
  474. // Create Descriptor Set:
  475. {
  476. VkDescriptorSetAllocateInfo alloc_info = {};
  477. alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
  478. alloc_info.descriptorPool = g_DescriptorPool;
  479. alloc_info.descriptorSetCount = 1;
  480. alloc_info.pSetLayouts = &g_DescriptorSetLayout;
  481. err = vkAllocateDescriptorSets(g_Device, &alloc_info, &g_DescriptorSet);
  482. ImGui_ImplVulkan_VkResult(err);
  483. }
  484. if (!g_PipelineLayout)
  485. {
  486. // Constants: we are using 'vec2 offset' and 'vec2 scale' instead of a full 3d projection matrix
  487. VkPushConstantRange push_constants[1] = {};
  488. push_constants[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
  489. push_constants[0].offset = sizeof(float) * 0;
  490. push_constants[0].size = sizeof(float) * 4;
  491. VkDescriptorSetLayout set_layout[1] = { g_DescriptorSetLayout };
  492. VkPipelineLayoutCreateInfo layout_info = {};
  493. layout_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
  494. layout_info.setLayoutCount = 1;
  495. layout_info.pSetLayouts = set_layout;
  496. layout_info.pushConstantRangeCount = 1;
  497. layout_info.pPushConstantRanges = push_constants;
  498. err = vkCreatePipelineLayout(g_Device, &layout_info, g_Allocator, &g_PipelineLayout);
  499. ImGui_ImplVulkan_VkResult(err);
  500. }
  501. VkPipelineShaderStageCreateInfo stage[2] = {};
  502. stage[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
  503. stage[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
  504. stage[0].module = vert_module;
  505. stage[0].pName = "main";
  506. stage[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
  507. stage[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
  508. stage[1].module = frag_module;
  509. stage[1].pName = "main";
  510. VkVertexInputBindingDescription binding_desc[1] = {};
  511. binding_desc[0].stride = sizeof(ImDrawVert);
  512. binding_desc[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
  513. VkVertexInputAttributeDescription attribute_desc[3] = {};
  514. attribute_desc[0].location = 0;
  515. attribute_desc[0].binding = binding_desc[0].binding;
  516. attribute_desc[0].format = VK_FORMAT_R32G32_SFLOAT;
  517. attribute_desc[0].offset = IM_OFFSETOF(ImDrawVert, pos);
  518. attribute_desc[1].location = 1;
  519. attribute_desc[1].binding = binding_desc[0].binding;
  520. attribute_desc[1].format = VK_FORMAT_R32G32_SFLOAT;
  521. attribute_desc[1].offset = IM_OFFSETOF(ImDrawVert, uv);
  522. attribute_desc[2].location = 2;
  523. attribute_desc[2].binding = binding_desc[0].binding;
  524. attribute_desc[2].format = VK_FORMAT_R8G8B8A8_UNORM;
  525. attribute_desc[2].offset = IM_OFFSETOF(ImDrawVert, col);
  526. VkPipelineVertexInputStateCreateInfo vertex_info = {};
  527. vertex_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
  528. vertex_info.vertexBindingDescriptionCount = 1;
  529. vertex_info.pVertexBindingDescriptions = binding_desc;
  530. vertex_info.vertexAttributeDescriptionCount = 3;
  531. vertex_info.pVertexAttributeDescriptions = attribute_desc;
  532. VkPipelineInputAssemblyStateCreateInfo ia_info = {};
  533. ia_info.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
  534. ia_info.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
  535. VkPipelineViewportStateCreateInfo viewport_info = {};
  536. viewport_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
  537. viewport_info.viewportCount = 1;
  538. viewport_info.scissorCount = 1;
  539. VkPipelineRasterizationStateCreateInfo raster_info = {};
  540. raster_info.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
  541. raster_info.polygonMode = VK_POLYGON_MODE_FILL;
  542. raster_info.cullMode = VK_CULL_MODE_NONE;
  543. raster_info.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
  544. raster_info.lineWidth = 1.0f;
  545. VkPipelineMultisampleStateCreateInfo ms_info = {};
  546. ms_info.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
  547. ms_info.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
  548. VkPipelineColorBlendAttachmentState color_attachment[1] = {};
  549. color_attachment[0].blendEnable = VK_TRUE;
  550. color_attachment[0].srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
  551. color_attachment[0].dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
  552. color_attachment[0].colorBlendOp = VK_BLEND_OP_ADD;
  553. color_attachment[0].srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
  554. color_attachment[0].dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
  555. color_attachment[0].alphaBlendOp = VK_BLEND_OP_ADD;
  556. color_attachment[0].colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
  557. VkPipelineDepthStencilStateCreateInfo depth_info = {};
  558. depth_info.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
  559. VkPipelineColorBlendStateCreateInfo blend_info = {};
  560. blend_info.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
  561. blend_info.attachmentCount = 1;
  562. blend_info.pAttachments = color_attachment;
  563. VkDynamicState dynamic_states[2] = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
  564. VkPipelineDynamicStateCreateInfo dynamic_state = {};
  565. dynamic_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
  566. dynamic_state.dynamicStateCount = 2;
  567. dynamic_state.pDynamicStates = dynamic_states;
  568. VkGraphicsPipelineCreateInfo info = {};
  569. info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
  570. info.flags = g_PipelineCreateFlags;
  571. info.stageCount = 2;
  572. info.pStages = stage;
  573. info.pVertexInputState = &vertex_info;
  574. info.pInputAssemblyState = &ia_info;
  575. info.pViewportState = &viewport_info;
  576. info.pRasterizationState = &raster_info;
  577. info.pMultisampleState = &ms_info;
  578. info.pDepthStencilState = &depth_info;
  579. info.pColorBlendState = &blend_info;
  580. info.pDynamicState = &dynamic_state;
  581. info.layout = g_PipelineLayout;
  582. info.renderPass = g_RenderPass;
  583. err = vkCreateGraphicsPipelines(g_Device, g_PipelineCache, 1, &info, g_Allocator, &g_Pipeline);
  584. ImGui_ImplVulkan_VkResult(err);
  585. vkDestroyShaderModule(g_Device, vert_module, g_Allocator);
  586. vkDestroyShaderModule(g_Device, frag_module, g_Allocator);
  587. return true;
  588. }
  589. void ImGui_ImplVulkan_InvalidateFontUploadObjects()
  590. {
  591. if (g_UploadBuffer)
  592. {
  593. vkDestroyBuffer(g_Device, g_UploadBuffer, g_Allocator);
  594. g_UploadBuffer = VK_NULL_HANDLE;
  595. }
  596. if (g_UploadBufferMemory)
  597. {
  598. vkFreeMemory(g_Device, g_UploadBufferMemory, g_Allocator);
  599. g_UploadBufferMemory = VK_NULL_HANDLE;
  600. }
  601. }
  602. void ImGui_ImplVulkan_InvalidateDeviceObjects()
  603. {
  604. ImGui_ImplVulkan_InvalidateFontUploadObjects();
  605. for (int i = 0; i < IMGUI_VK_QUEUED_FRAMES; i++)
  606. {
  607. FrameDataForRender* fd = &g_FramesDataBuffers[i];
  608. if (fd->VertexBuffer) { vkDestroyBuffer (g_Device, fd->VertexBuffer, g_Allocator); fd->VertexBuffer = VK_NULL_HANDLE; }
  609. if (fd->VertexBufferMemory) { vkFreeMemory (g_Device, fd->VertexBufferMemory, g_Allocator); fd->VertexBufferMemory = VK_NULL_HANDLE; }
  610. if (fd->IndexBuffer) { vkDestroyBuffer (g_Device, fd->IndexBuffer, g_Allocator); fd->IndexBuffer = VK_NULL_HANDLE; }
  611. if (fd->IndexBufferMemory) { vkFreeMemory (g_Device, fd->IndexBufferMemory, g_Allocator); fd->IndexBufferMemory = VK_NULL_HANDLE; }
  612. }
  613. if (g_FontView) { vkDestroyImageView(g_Device, g_FontView, g_Allocator); g_FontView = VK_NULL_HANDLE; }
  614. if (g_FontImage) { vkDestroyImage(g_Device, g_FontImage, g_Allocator); g_FontImage = VK_NULL_HANDLE; }
  615. if (g_FontMemory) { vkFreeMemory(g_Device, g_FontMemory, g_Allocator); g_FontMemory = VK_NULL_HANDLE; }
  616. if (g_FontSampler) { vkDestroySampler(g_Device, g_FontSampler, g_Allocator); g_FontSampler = VK_NULL_HANDLE; }
  617. if (g_DescriptorSetLayout) { vkDestroyDescriptorSetLayout(g_Device, g_DescriptorSetLayout, g_Allocator); g_DescriptorSetLayout = VK_NULL_HANDLE; }
  618. if (g_PipelineLayout) { vkDestroyPipelineLayout(g_Device, g_PipelineLayout, g_Allocator); g_PipelineLayout = VK_NULL_HANDLE; }
  619. if (g_Pipeline) { vkDestroyPipeline(g_Device, g_Pipeline, g_Allocator); g_Pipeline = VK_NULL_HANDLE; }
  620. }
  621. bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo *init_data)
  622. {
  623. g_Allocator = init_data->Allocator;
  624. g_PhysicalDevice = init_data->PhysicalDevice;
  625. g_Device = init_data->Device;
  626. g_RenderPass = init_data->RenderPass;
  627. g_PipelineCache = init_data->PipelineCache;
  628. g_DescriptorPool = init_data->DescriptorPool;
  629. g_CheckVkResult = init_data->CheckVkResultFn;
  630. ImGuiIO& io = ImGui::GetIO();
  631. ImGui_ImplVulkan_CreateDeviceObjects();
  632. if (io.ConfigFlags & ImGuiConfigFlags_MultiViewports)
  633. ImGui_ImplVulkan_InitPlatformInterface();
  634. return true;
  635. }
  636. void ImGui_ImplVulkan_Shutdown()
  637. {
  638. ImGui_ImplVulkan_ShutdownPlatformInterface();
  639. ImGui_ImplVulkan_InvalidateDeviceObjects();
  640. }
  641. void ImGui_ImplVulkan_NewFrame()
  642. {
  643. }
  644. void ImGui_ImplVulkan_Render(VkCommandBuffer command_buffer)
  645. {
  646. ImGui::Render();
  647. ImGui_ImplVulkan_RenderDrawData(command_buffer, ImGui::GetDrawData());
  648. g_FrameIndex = (g_FrameIndex + 1) % IMGUI_VK_QUEUED_FRAMES;
  649. }
  650. //-------------------------------------------------------------------------
  651. // Miscellaneous Vulkan Helpers
  652. // (Those are currently not strictly needed by the binding, but will be once if we support multi-viewports)
  653. //-------------------------------------------------------------------------
  654. #include <stdlib.h> // malloc
  655. VkSurfaceFormatKHR ImGui_ImplVulkan_SelectSurfaceFormat(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkFormat* request_formats, int request_formats_count, VkColorSpaceKHR request_color_space)
  656. {
  657. IM_ASSERT(request_formats != NULL);
  658. IM_ASSERT(request_formats_count > 0);
  659. // Per Spec Format and View Format are expected to be the same unless VK_IMAGE_CREATE_MUTABLE_BIT was set at image creation
  660. // Assuming that the default behavior is without setting this bit, there is no need for separate Swapchain image and image view format
  661. // Additionally several new color spaces were introduced with Vulkan Spec v1.0.40,
  662. // hence we must make sure that a format with the mostly available color space, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR, is found and used.
  663. uint32_t avail_count;
  664. vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface, &avail_count, NULL);
  665. ImVector<VkSurfaceFormatKHR> avail_format;
  666. avail_format.resize((int)avail_count);
  667. vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface, &avail_count, avail_format.Data);
  668. // First check if only one format, VK_FORMAT_UNDEFINED, is available, which would imply that any format is available
  669. if (avail_count == 1)
  670. {
  671. if (avail_format[0].format == VK_FORMAT_UNDEFINED)
  672. {
  673. VkSurfaceFormatKHR ret;
  674. ret.format = request_formats[0];
  675. ret.colorSpace = request_color_space;
  676. return ret;
  677. }
  678. else
  679. {
  680. // No point in searching another format
  681. return avail_format[0];
  682. }
  683. }
  684. else
  685. {
  686. // Request several formats, the first found will be used
  687. for (int request_i = 0; request_i < request_formats_count; request_i++)
  688. for (uint32_t avail_i = 0; avail_i < avail_count; avail_i++)
  689. if (avail_format[avail_i].format == request_formats[request_i] && avail_format[avail_i].colorSpace == request_color_space)
  690. return avail_format[avail_i];
  691. // If none of the requested image formats could be found, use the first available
  692. return avail_format[0];
  693. }
  694. }
  695. VkPresentModeKHR ImGui_ImplVulkan_SelectPresentMode(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkPresentModeKHR* request_modes, int request_modes_count)
  696. {
  697. IM_ASSERT(request_modes != NULL);
  698. IM_ASSERT(request_modes_count > 0);
  699. // Request a certain mode and confirm that it is available. If not use VK_PRESENT_MODE_FIFO_KHR which is mandatory
  700. uint32_t avail_count = 0;
  701. vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface, &avail_count, NULL);
  702. ImVector<VkPresentModeKHR> avail_modes;
  703. avail_modes.resize((int)avail_count);
  704. vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface, &avail_count, avail_modes.Data);
  705. for (int request_i = 0; request_i < request_modes_count; request_i++)
  706. for (uint32_t avail_i = 0; avail_i < avail_count; avail_i++)
  707. if (request_modes[request_i] == avail_modes[avail_i])
  708. return request_modes[request_i];
  709. return VK_PRESENT_MODE_FIFO_KHR; // Always available
  710. }
  711. // --------------------------------------------------------------------------------------------------------
  712. // Platform Windows (OPTIONAL/EXPERIMENTAL)
  713. // --------------------------------------------------------------------------------------------------------
  714. #include "imgui_internal.h" // ImGuiViewport
  715. struct ImGuiPlatformDataVulkan
  716. {
  717. // store swap chain, render target/frame buffer, etc.
  718. ImGuiPlatformDataVulkan() { }
  719. ~ImGuiPlatformDataVulkan() { }
  720. };
  721. static void ImGui_ImplVulkan_CreateViewport(ImGuiViewport* viewport)
  722. {
  723. ImGuiPlatformDataVulkan* data = IM_NEW(ImGuiPlatformDataVulkan)();
  724. viewport->RendererUserData = data;
  725. // FIXME-PLATFORM
  726. //HWND hwnd = (HWND)viewport->PlatformHandle;
  727. //IM_ASSERT(hwnd != 0);
  728. //...
  729. }
  730. static void ImGui_ImplVulkan_DestroyViewport(ImGuiViewport* viewport)
  731. {
  732. if (ImGuiPlatformDataVulkan* data = (ImGuiPlatformDataVulkan*)viewport->RendererUserData)
  733. {
  734. //...
  735. IM_DELETE(data);
  736. }
  737. viewport->RendererUserData = NULL;
  738. }
  739. static void ImGui_ImplVulkan_ResizeViewport(ImGuiViewport* viewport, int w, int h)
  740. {
  741. ImGuiPlatformDataVulkan* data = (ImGuiPlatformDataVulkan*)viewport->RendererUserData;
  742. //...
  743. (void)data;
  744. (void)w;
  745. (void)h;
  746. }
  747. static void ImGui_ImplVulkan_RenderViewport(ImGuiViewport* viewport)
  748. {
  749. ImGuiPlatformDataVulkan* data = (ImGuiPlatformDataVulkan*)viewport->RendererUserData;
  750. ImVec4 clear_color = ImGui::GetStyle().Colors[ImGuiCol_WindowBg]; // FIXME-PLATFORM
  751. clear_color.w = 1.0f;
  752. (void)data;
  753. // clear
  754. // call ImGui_ImplVulkan_RenderDrawData(&viewport->DrawData)
  755. }
  756. static void ImGui_ImplVulkan_SwapBuffers(ImGuiViewport* viewport)
  757. {
  758. ImGuiPlatformDataVulkan* data = (ImGuiPlatformDataVulkan*)viewport->RendererUserData;
  759. (void)data;
  760. //...
  761. }
  762. void ImGui_ImplVulkan_InitPlatformInterface()
  763. {
  764. ImGuiIO& io = ImGui::GetIO();
  765. io.RendererInterface.CreateViewport = ImGui_ImplVulkan_CreateViewport;
  766. io.RendererInterface.DestroyViewport = ImGui_ImplVulkan_DestroyViewport;
  767. io.RendererInterface.ResizeViewport = ImGui_ImplVulkan_ResizeViewport;
  768. io.RendererInterface.RenderViewport = ImGui_ImplVulkan_RenderViewport;
  769. io.RendererInterface.SwapBuffers = ImGui_ImplVulkan_SwapBuffers;
  770. }
  771. void ImGui_ImplVulkan_ShutdownPlatformInterface()
  772. {
  773. ImGuiIO& io = ImGui::GetIO();
  774. memset(&io.RendererInterface, 0, sizeof(io.RendererInterface));
  775. }