imgui_impl_vulkan.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. // ImGui GLFW binding with Vulkan + shaders
  2. // Missing features:
  3. // [ ] User texture binding. Changes of ImTextureID aren't supported by this binding! See https://github.com/ocornut/imgui/pull/914
  4. // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
  5. // 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().
  6. // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
  7. // https://github.com/ocornut/imgui
  8. // CHANGELOG
  9. // (minor and older changes stripped away, please see git history for details)
  10. // 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback, ImGui_ImplVulkan_Render() calls ImGui_ImplVulkan_RenderDrawData() itself.
  11. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  12. // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
  13. // 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
  14. // 2018-01-18: Inputs: Added mapping for ImGuiKey_Insert.
  15. // 2017-08-25: Inputs: MousePos set to -FLT_MAX,-FLT_MAX when mouse is unavailable/missing (instead of -1,-1).
  16. // 2017-05-15: Vulkan: Fix scissor offset being negative. Fix new Vulkan validation warnings. Set required depth member for buffer image copy.
  17. // 2016-11-13: Vulkan: Fix validation layer warnings and errors and redeclare gl_PerVertex.
  18. // 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.
  19. // 2016-10-15: Misc: Added a void* user_data parameter to Clipboard function handlers.
  20. // 2016-08-27: Vulkan: Fix Vulkan example for use when a depth buffer is active.
  21. #include "imgui.h"
  22. #include "imgui_impl_vulkan.h"
  23. // Vulkan Data
  24. static VkAllocationCallbacks* g_Allocator = NULL;
  25. static VkPhysicalDevice g_Gpu = VK_NULL_HANDLE;
  26. static VkDevice g_Device = VK_NULL_HANDLE;
  27. static VkRenderPass g_RenderPass = VK_NULL_HANDLE;
  28. static VkPipelineCache g_PipelineCache = VK_NULL_HANDLE;
  29. static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE;
  30. static void (*g_CheckVkResult)(VkResult err) = NULL;
  31. static VkCommandBuffer g_CommandBuffer = VK_NULL_HANDLE;
  32. static VkDeviceSize g_BufferMemoryAlignment = 256;
  33. static VkPipelineCreateFlags g_PipelineCreateFlags = 0;
  34. static int g_FrameIndex = 0;
  35. static VkDescriptorSetLayout g_DescriptorSetLayout = VK_NULL_HANDLE;
  36. static VkPipelineLayout g_PipelineLayout = VK_NULL_HANDLE;
  37. static VkDescriptorSet g_DescriptorSet = VK_NULL_HANDLE;
  38. static VkPipeline g_Pipeline = VK_NULL_HANDLE;
  39. static VkSampler g_FontSampler = VK_NULL_HANDLE;
  40. static VkDeviceMemory g_FontMemory = VK_NULL_HANDLE;
  41. static VkImage g_FontImage = VK_NULL_HANDLE;
  42. static VkImageView g_FontView = VK_NULL_HANDLE;
  43. static VkDeviceMemory g_VertexBufferMemory[IMGUI_VK_QUEUED_FRAMES] = {};
  44. static VkDeviceMemory g_IndexBufferMemory[IMGUI_VK_QUEUED_FRAMES] = {};
  45. static VkDeviceSize g_VertexBufferSize[IMGUI_VK_QUEUED_FRAMES] = {};
  46. static VkDeviceSize g_IndexBufferSize[IMGUI_VK_QUEUED_FRAMES] = {};
  47. static VkBuffer g_VertexBuffer[IMGUI_VK_QUEUED_FRAMES] = {};
  48. static VkBuffer g_IndexBuffer[IMGUI_VK_QUEUED_FRAMES] = {};
  49. static VkDeviceMemory g_UploadBufferMemory = VK_NULL_HANDLE;
  50. static VkBuffer g_UploadBuffer = VK_NULL_HANDLE;
  51. static uint32_t __glsl_shader_vert_spv[] =
  52. {
  53. 0x07230203,0x00010000,0x00080001,0x0000002e,0x00000000,0x00020011,0x00000001,0x0006000b,
  54. 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001,
  55. 0x000a000f,0x00000000,0x00000004,0x6e69616d,0x00000000,0x0000000b,0x0000000f,0x00000015,
  56. 0x0000001b,0x0000001c,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d,
  57. 0x00000000,0x00030005,0x00000009,0x00000000,0x00050006,0x00000009,0x00000000,0x6f6c6f43,
  58. 0x00000072,0x00040006,0x00000009,0x00000001,0x00005655,0x00030005,0x0000000b,0x0074754f,
  59. 0x00040005,0x0000000f,0x6c6f4361,0x0000726f,0x00030005,0x00000015,0x00565561,0x00060005,
  60. 0x00000019,0x505f6c67,0x65567265,0x78657472,0x00000000,0x00060006,0x00000019,0x00000000,
  61. 0x505f6c67,0x7469736f,0x006e6f69,0x00030005,0x0000001b,0x00000000,0x00040005,0x0000001c,
  62. 0x736f5061,0x00000000,0x00060005,0x0000001e,0x73755075,0x6e6f4368,0x6e617473,0x00000074,
  63. 0x00050006,0x0000001e,0x00000000,0x61635375,0x0000656c,0x00060006,0x0000001e,0x00000001,
  64. 0x61725475,0x616c736e,0x00006574,0x00030005,0x00000020,0x00006370,0x00040047,0x0000000b,
  65. 0x0000001e,0x00000000,0x00040047,0x0000000f,0x0000001e,0x00000002,0x00040047,0x00000015,
  66. 0x0000001e,0x00000001,0x00050048,0x00000019,0x00000000,0x0000000b,0x00000000,0x00030047,
  67. 0x00000019,0x00000002,0x00040047,0x0000001c,0x0000001e,0x00000000,0x00050048,0x0000001e,
  68. 0x00000000,0x00000023,0x00000000,0x00050048,0x0000001e,0x00000001,0x00000023,0x00000008,
  69. 0x00030047,0x0000001e,0x00000002,0x00020013,0x00000002,0x00030021,0x00000003,0x00000002,
  70. 0x00030016,0x00000006,0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,0x00040017,
  71. 0x00000008,0x00000006,0x00000002,0x0004001e,0x00000009,0x00000007,0x00000008,0x00040020,
  72. 0x0000000a,0x00000003,0x00000009,0x0004003b,0x0000000a,0x0000000b,0x00000003,0x00040015,
  73. 0x0000000c,0x00000020,0x00000001,0x0004002b,0x0000000c,0x0000000d,0x00000000,0x00040020,
  74. 0x0000000e,0x00000001,0x00000007,0x0004003b,0x0000000e,0x0000000f,0x00000001,0x00040020,
  75. 0x00000011,0x00000003,0x00000007,0x0004002b,0x0000000c,0x00000013,0x00000001,0x00040020,
  76. 0x00000014,0x00000001,0x00000008,0x0004003b,0x00000014,0x00000015,0x00000001,0x00040020,
  77. 0x00000017,0x00000003,0x00000008,0x0003001e,0x00000019,0x00000007,0x00040020,0x0000001a,
  78. 0x00000003,0x00000019,0x0004003b,0x0000001a,0x0000001b,0x00000003,0x0004003b,0x00000014,
  79. 0x0000001c,0x00000001,0x0004001e,0x0000001e,0x00000008,0x00000008,0x00040020,0x0000001f,
  80. 0x00000009,0x0000001e,0x0004003b,0x0000001f,0x00000020,0x00000009,0x00040020,0x00000021,
  81. 0x00000009,0x00000008,0x0004002b,0x00000006,0x00000028,0x00000000,0x0004002b,0x00000006,
  82. 0x00000029,0x3f800000,0x00050036,0x00000002,0x00000004,0x00000000,0x00000003,0x000200f8,
  83. 0x00000005,0x0004003d,0x00000007,0x00000010,0x0000000f,0x00050041,0x00000011,0x00000012,
  84. 0x0000000b,0x0000000d,0x0003003e,0x00000012,0x00000010,0x0004003d,0x00000008,0x00000016,
  85. 0x00000015,0x00050041,0x00000017,0x00000018,0x0000000b,0x00000013,0x0003003e,0x00000018,
  86. 0x00000016,0x0004003d,0x00000008,0x0000001d,0x0000001c,0x00050041,0x00000021,0x00000022,
  87. 0x00000020,0x0000000d,0x0004003d,0x00000008,0x00000023,0x00000022,0x00050085,0x00000008,
  88. 0x00000024,0x0000001d,0x00000023,0x00050041,0x00000021,0x00000025,0x00000020,0x00000013,
  89. 0x0004003d,0x00000008,0x00000026,0x00000025,0x00050081,0x00000008,0x00000027,0x00000024,
  90. 0x00000026,0x00050051,0x00000006,0x0000002a,0x00000027,0x00000000,0x00050051,0x00000006,
  91. 0x0000002b,0x00000027,0x00000001,0x00070050,0x00000007,0x0000002c,0x0000002a,0x0000002b,
  92. 0x00000028,0x00000029,0x00050041,0x00000011,0x0000002d,0x0000001b,0x0000000d,0x0003003e,
  93. 0x0000002d,0x0000002c,0x000100fd,0x00010038
  94. };
  95. static uint32_t __glsl_shader_frag_spv[] =
  96. {
  97. 0x07230203,0x00010000,0x00080001,0x0000001e,0x00000000,0x00020011,0x00000001,0x0006000b,
  98. 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001,
  99. 0x0007000f,0x00000004,0x00000004,0x6e69616d,0x00000000,0x00000009,0x0000000d,0x00030010,
  100. 0x00000004,0x00000007,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d,
  101. 0x00000000,0x00040005,0x00000009,0x6c6f4366,0x0000726f,0x00030005,0x0000000b,0x00000000,
  102. 0x00050006,0x0000000b,0x00000000,0x6f6c6f43,0x00000072,0x00040006,0x0000000b,0x00000001,
  103. 0x00005655,0x00030005,0x0000000d,0x00006e49,0x00050005,0x00000016,0x78655473,0x65727574,
  104. 0x00000000,0x00040047,0x00000009,0x0000001e,0x00000000,0x00040047,0x0000000d,0x0000001e,
  105. 0x00000000,0x00040047,0x00000016,0x00000022,0x00000000,0x00040047,0x00000016,0x00000021,
  106. 0x00000000,0x00020013,0x00000002,0x00030021,0x00000003,0x00000002,0x00030016,0x00000006,
  107. 0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,0x00040020,0x00000008,0x00000003,
  108. 0x00000007,0x0004003b,0x00000008,0x00000009,0x00000003,0x00040017,0x0000000a,0x00000006,
  109. 0x00000002,0x0004001e,0x0000000b,0x00000007,0x0000000a,0x00040020,0x0000000c,0x00000001,
  110. 0x0000000b,0x0004003b,0x0000000c,0x0000000d,0x00000001,0x00040015,0x0000000e,0x00000020,
  111. 0x00000001,0x0004002b,0x0000000e,0x0000000f,0x00000000,0x00040020,0x00000010,0x00000001,
  112. 0x00000007,0x00090019,0x00000013,0x00000006,0x00000001,0x00000000,0x00000000,0x00000000,
  113. 0x00000001,0x00000000,0x0003001b,0x00000014,0x00000013,0x00040020,0x00000015,0x00000000,
  114. 0x00000014,0x0004003b,0x00000015,0x00000016,0x00000000,0x0004002b,0x0000000e,0x00000018,
  115. 0x00000001,0x00040020,0x00000019,0x00000001,0x0000000a,0x00050036,0x00000002,0x00000004,
  116. 0x00000000,0x00000003,0x000200f8,0x00000005,0x00050041,0x00000010,0x00000011,0x0000000d,
  117. 0x0000000f,0x0004003d,0x00000007,0x00000012,0x00000011,0x0004003d,0x00000014,0x00000017,
  118. 0x00000016,0x00050041,0x00000019,0x0000001a,0x0000000d,0x00000018,0x0004003d,0x0000000a,
  119. 0x0000001b,0x0000001a,0x00050057,0x00000007,0x0000001c,0x00000017,0x0000001b,0x00050085,
  120. 0x00000007,0x0000001d,0x00000012,0x0000001c,0x0003003e,0x00000009,0x0000001d,0x000100fd,
  121. 0x00010038
  122. };
  123. static uint32_t ImGui_ImplVulkan_MemoryType(VkMemoryPropertyFlags properties, uint32_t type_bits)
  124. {
  125. VkPhysicalDeviceMemoryProperties prop;
  126. vkGetPhysicalDeviceMemoryProperties(g_Gpu, &prop);
  127. for (uint32_t i = 0; i < prop.memoryTypeCount; i++)
  128. if ((prop.memoryTypes[i].propertyFlags & properties) == properties && type_bits & (1<<i))
  129. return i;
  130. return 0xffffffff; // Unable to find memoryType
  131. }
  132. static void ImGui_ImplVulkan_VkResult(VkResult err)
  133. {
  134. if (g_CheckVkResult)
  135. g_CheckVkResult(err);
  136. }
  137. // This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
  138. void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data)
  139. {
  140. VkResult err;
  141. ImGuiIO& io = ImGui::GetIO();
  142. if (draw_data->TotalVtxCount == 0)
  143. return;
  144. // Create the Vertex Buffer:
  145. size_t vertex_size = draw_data->TotalVtxCount * sizeof(ImDrawVert);
  146. if (!g_VertexBuffer[g_FrameIndex] || g_VertexBufferSize[g_FrameIndex] < vertex_size)
  147. {
  148. if (g_VertexBuffer[g_FrameIndex])
  149. vkDestroyBuffer(g_Device, g_VertexBuffer[g_FrameIndex], g_Allocator);
  150. if (g_VertexBufferMemory[g_FrameIndex])
  151. vkFreeMemory(g_Device, g_VertexBufferMemory[g_FrameIndex], g_Allocator);
  152. VkDeviceSize vertex_buffer_size = ((vertex_size-1) / g_BufferMemoryAlignment+1) * g_BufferMemoryAlignment;
  153. VkBufferCreateInfo buffer_info = {};
  154. buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  155. buffer_info.size = vertex_buffer_size;
  156. buffer_info.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
  157. buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  158. err = vkCreateBuffer(g_Device, &buffer_info, g_Allocator, &g_VertexBuffer[g_FrameIndex]);
  159. ImGui_ImplVulkan_VkResult(err);
  160. VkMemoryRequirements req;
  161. vkGetBufferMemoryRequirements(g_Device, g_VertexBuffer[g_FrameIndex], &req);
  162. g_BufferMemoryAlignment = (g_BufferMemoryAlignment > req.alignment) ? g_BufferMemoryAlignment : req.alignment;
  163. VkMemoryAllocateInfo alloc_info = {};
  164. alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  165. alloc_info.allocationSize = req.size;
  166. alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
  167. err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &g_VertexBufferMemory[g_FrameIndex]);
  168. ImGui_ImplVulkan_VkResult(err);
  169. err = vkBindBufferMemory(g_Device, g_VertexBuffer[g_FrameIndex], g_VertexBufferMemory[g_FrameIndex], 0);
  170. ImGui_ImplVulkan_VkResult(err);
  171. g_VertexBufferSize[g_FrameIndex] = vertex_buffer_size;
  172. }
  173. // Create the Index Buffer:
  174. size_t index_size = draw_data->TotalIdxCount * sizeof(ImDrawIdx);
  175. if (!g_IndexBuffer[g_FrameIndex] || g_IndexBufferSize[g_FrameIndex] < index_size)
  176. {
  177. if (g_IndexBuffer[g_FrameIndex])
  178. vkDestroyBuffer(g_Device, g_IndexBuffer[g_FrameIndex], g_Allocator);
  179. if (g_IndexBufferMemory[g_FrameIndex])
  180. vkFreeMemory(g_Device, g_IndexBufferMemory[g_FrameIndex], g_Allocator);
  181. VkDeviceSize index_buffer_size = ((index_size-1) / g_BufferMemoryAlignment+1) * g_BufferMemoryAlignment;
  182. VkBufferCreateInfo buffer_info = {};
  183. buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  184. buffer_info.size = index_buffer_size;
  185. buffer_info.usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
  186. buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  187. err = vkCreateBuffer(g_Device, &buffer_info, g_Allocator, &g_IndexBuffer[g_FrameIndex]);
  188. ImGui_ImplVulkan_VkResult(err);
  189. VkMemoryRequirements req;
  190. vkGetBufferMemoryRequirements(g_Device, g_IndexBuffer[g_FrameIndex], &req);
  191. g_BufferMemoryAlignment = (g_BufferMemoryAlignment > req.alignment) ? g_BufferMemoryAlignment : req.alignment;
  192. VkMemoryAllocateInfo alloc_info = {};
  193. alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  194. alloc_info.allocationSize = req.size;
  195. alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
  196. err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &g_IndexBufferMemory[g_FrameIndex]);
  197. ImGui_ImplVulkan_VkResult(err);
  198. err = vkBindBufferMemory(g_Device, g_IndexBuffer[g_FrameIndex], g_IndexBufferMemory[g_FrameIndex], 0);
  199. ImGui_ImplVulkan_VkResult(err);
  200. g_IndexBufferSize[g_FrameIndex] = index_buffer_size;
  201. }
  202. // Upload Vertex and index Data:
  203. {
  204. ImDrawVert* vtx_dst;
  205. ImDrawIdx* idx_dst;
  206. err = vkMapMemory(g_Device, g_VertexBufferMemory[g_FrameIndex], 0, vertex_size, 0, (void**)(&vtx_dst));
  207. ImGui_ImplVulkan_VkResult(err);
  208. err = vkMapMemory(g_Device, g_IndexBufferMemory[g_FrameIndex], 0, index_size, 0, (void**)(&idx_dst));
  209. ImGui_ImplVulkan_VkResult(err);
  210. for (int n = 0; n < draw_data->CmdListsCount; n++)
  211. {
  212. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  213. memcpy(vtx_dst, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert));
  214. memcpy(idx_dst, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx));
  215. vtx_dst += cmd_list->VtxBuffer.Size;
  216. idx_dst += cmd_list->IdxBuffer.Size;
  217. }
  218. VkMappedMemoryRange range[2] = {};
  219. range[0].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
  220. range[0].memory = g_VertexBufferMemory[g_FrameIndex];
  221. range[0].size = VK_WHOLE_SIZE;
  222. range[1].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
  223. range[1].memory = g_IndexBufferMemory[g_FrameIndex];
  224. range[1].size = VK_WHOLE_SIZE;
  225. err = vkFlushMappedMemoryRanges(g_Device, 2, range);
  226. ImGui_ImplVulkan_VkResult(err);
  227. vkUnmapMemory(g_Device, g_VertexBufferMemory[g_FrameIndex]);
  228. vkUnmapMemory(g_Device, g_IndexBufferMemory[g_FrameIndex]);
  229. }
  230. // Bind pipeline and descriptor sets:
  231. {
  232. vkCmdBindPipeline(g_CommandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, g_Pipeline);
  233. VkDescriptorSet desc_set[1] = {g_DescriptorSet};
  234. vkCmdBindDescriptorSets(g_CommandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, g_PipelineLayout, 0, 1, desc_set, 0, NULL);
  235. }
  236. // Bind Vertex And Index Buffer:
  237. {
  238. VkBuffer vertex_buffers[1] = {g_VertexBuffer[g_FrameIndex]};
  239. VkDeviceSize vertex_offset[1] = {0};
  240. vkCmdBindVertexBuffers(g_CommandBuffer, 0, 1, vertex_buffers, vertex_offset);
  241. vkCmdBindIndexBuffer(g_CommandBuffer, g_IndexBuffer[g_FrameIndex], 0, VK_INDEX_TYPE_UINT16);
  242. }
  243. // Setup viewport:
  244. {
  245. VkViewport viewport;
  246. viewport.x = 0;
  247. viewport.y = 0;
  248. viewport.width = ImGui::GetIO().DisplaySize.x;
  249. viewport.height = ImGui::GetIO().DisplaySize.y;
  250. viewport.minDepth = 0.0f;
  251. viewport.maxDepth = 1.0f;
  252. vkCmdSetViewport(g_CommandBuffer, 0, 1, &viewport);
  253. }
  254. // Setup scale and translation:
  255. {
  256. float scale[2];
  257. scale[0] = 2.0f/io.DisplaySize.x;
  258. scale[1] = 2.0f/io.DisplaySize.y;
  259. float translate[2];
  260. translate[0] = -1.0f;
  261. translate[1] = -1.0f;
  262. vkCmdPushConstants(g_CommandBuffer, g_PipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, sizeof(float) * 0, sizeof(float) * 2, scale);
  263. vkCmdPushConstants(g_CommandBuffer, g_PipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, sizeof(float) * 2, sizeof(float) * 2, translate);
  264. }
  265. // Render the command lists:
  266. int vtx_offset = 0;
  267. int idx_offset = 0;
  268. for (int n = 0; n < draw_data->CmdListsCount; n++)
  269. {
  270. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  271. for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
  272. {
  273. const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
  274. if (pcmd->UserCallback)
  275. {
  276. pcmd->UserCallback(cmd_list, pcmd);
  277. }
  278. else
  279. {
  280. VkRect2D scissor;
  281. scissor.offset.x = (int32_t)(pcmd->ClipRect.x) > 0 ? (int32_t)(pcmd->ClipRect.x) : 0;
  282. scissor.offset.y = (int32_t)(pcmd->ClipRect.y) > 0 ? (int32_t)(pcmd->ClipRect.y) : 0;
  283. scissor.extent.width = (uint32_t)(pcmd->ClipRect.z - pcmd->ClipRect.x);
  284. scissor.extent.height = (uint32_t)(pcmd->ClipRect.w - pcmd->ClipRect.y + 1); // FIXME: Why +1 here?
  285. vkCmdSetScissor(g_CommandBuffer, 0, 1, &scissor);
  286. vkCmdDrawIndexed(g_CommandBuffer, pcmd->ElemCount, 1, idx_offset, vtx_offset, 0);
  287. }
  288. idx_offset += pcmd->ElemCount;
  289. }
  290. vtx_offset += cmd_list->VtxBuffer.Size;
  291. }
  292. }
  293. bool ImGui_ImplVulkan_CreateFontsTexture(VkCommandBuffer command_buffer)
  294. {
  295. ImGuiIO& io = ImGui::GetIO();
  296. unsigned char* pixels;
  297. int width, height;
  298. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
  299. size_t upload_size = width*height*4*sizeof(char);
  300. VkResult err;
  301. // Create the Image:
  302. {
  303. VkImageCreateInfo info = {};
  304. info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
  305. info.imageType = VK_IMAGE_TYPE_2D;
  306. info.format = VK_FORMAT_R8G8B8A8_UNORM;
  307. info.extent.width = width;
  308. info.extent.height = height;
  309. info.extent.depth = 1;
  310. info.mipLevels = 1;
  311. info.arrayLayers = 1;
  312. info.samples = VK_SAMPLE_COUNT_1_BIT;
  313. info.tiling = VK_IMAGE_TILING_OPTIMAL;
  314. info.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
  315. info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  316. info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  317. err = vkCreateImage(g_Device, &info, g_Allocator, &g_FontImage);
  318. ImGui_ImplVulkan_VkResult(err);
  319. VkMemoryRequirements req;
  320. vkGetImageMemoryRequirements(g_Device, g_FontImage, &req);
  321. VkMemoryAllocateInfo alloc_info = {};
  322. alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  323. alloc_info.allocationSize = req.size;
  324. alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, req.memoryTypeBits);
  325. err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &g_FontMemory);
  326. ImGui_ImplVulkan_VkResult(err);
  327. err = vkBindImageMemory(g_Device, g_FontImage, g_FontMemory, 0);
  328. ImGui_ImplVulkan_VkResult(err);
  329. }
  330. // Create the Image View:
  331. {
  332. VkImageViewCreateInfo info = {};
  333. info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
  334. info.image = g_FontImage;
  335. info.viewType = VK_IMAGE_VIEW_TYPE_2D;
  336. info.format = VK_FORMAT_R8G8B8A8_UNORM;
  337. info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  338. info.subresourceRange.levelCount = 1;
  339. info.subresourceRange.layerCount = 1;
  340. err = vkCreateImageView(g_Device, &info, g_Allocator, &g_FontView);
  341. ImGui_ImplVulkan_VkResult(err);
  342. }
  343. // Update the Descriptor Set:
  344. {
  345. VkDescriptorImageInfo desc_image[1] = {};
  346. desc_image[0].sampler = g_FontSampler;
  347. desc_image[0].imageView = g_FontView;
  348. desc_image[0].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  349. VkWriteDescriptorSet write_desc[1] = {};
  350. write_desc[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  351. write_desc[0].dstSet = g_DescriptorSet;
  352. write_desc[0].descriptorCount = 1;
  353. write_desc[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
  354. write_desc[0].pImageInfo = desc_image;
  355. vkUpdateDescriptorSets(g_Device, 1, write_desc, 0, NULL);
  356. }
  357. // Create the Upload Buffer:
  358. {
  359. VkBufferCreateInfo buffer_info = {};
  360. buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  361. buffer_info.size = upload_size;
  362. buffer_info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
  363. buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  364. err = vkCreateBuffer(g_Device, &buffer_info, g_Allocator, &g_UploadBuffer);
  365. ImGui_ImplVulkan_VkResult(err);
  366. VkMemoryRequirements req;
  367. vkGetBufferMemoryRequirements(g_Device, g_UploadBuffer, &req);
  368. g_BufferMemoryAlignment = (g_BufferMemoryAlignment > req.alignment) ? g_BufferMemoryAlignment : req.alignment;
  369. VkMemoryAllocateInfo alloc_info = {};
  370. alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  371. alloc_info.allocationSize = req.size;
  372. alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
  373. err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &g_UploadBufferMemory);
  374. ImGui_ImplVulkan_VkResult(err);
  375. err = vkBindBufferMemory(g_Device, g_UploadBuffer, g_UploadBufferMemory, 0);
  376. ImGui_ImplVulkan_VkResult(err);
  377. }
  378. // Upload to Buffer:
  379. {
  380. char* map = NULL;
  381. err = vkMapMemory(g_Device, g_UploadBufferMemory, 0, upload_size, 0, (void**)(&map));
  382. ImGui_ImplVulkan_VkResult(err);
  383. memcpy(map, pixels, upload_size);
  384. VkMappedMemoryRange range[1] = {};
  385. range[0].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
  386. range[0].memory = g_UploadBufferMemory;
  387. range[0].size = upload_size;
  388. err = vkFlushMappedMemoryRanges(g_Device, 1, range);
  389. ImGui_ImplVulkan_VkResult(err);
  390. vkUnmapMemory(g_Device, g_UploadBufferMemory);
  391. }
  392. // Copy to Image:
  393. {
  394. VkImageMemoryBarrier copy_barrier[1] = {};
  395. copy_barrier[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
  396. copy_barrier[0].dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
  397. copy_barrier[0].oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  398. copy_barrier[0].newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
  399. copy_barrier[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  400. copy_barrier[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  401. copy_barrier[0].image = g_FontImage;
  402. copy_barrier[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  403. copy_barrier[0].subresourceRange.levelCount = 1;
  404. copy_barrier[0].subresourceRange.layerCount = 1;
  405. vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 0, NULL, 1, copy_barrier);
  406. VkBufferImageCopy region = {};
  407. region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  408. region.imageSubresource.layerCount = 1;
  409. region.imageExtent.width = width;
  410. region.imageExtent.height = height;
  411. region.imageExtent.depth = 1;
  412. vkCmdCopyBufferToImage(command_buffer, g_UploadBuffer, g_FontImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
  413. VkImageMemoryBarrier use_barrier[1] = {};
  414. use_barrier[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
  415. use_barrier[0].srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
  416. use_barrier[0].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
  417. use_barrier[0].oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
  418. use_barrier[0].newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  419. use_barrier[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  420. use_barrier[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  421. use_barrier[0].image = g_FontImage;
  422. use_barrier[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  423. use_barrier[0].subresourceRange.levelCount = 1;
  424. use_barrier[0].subresourceRange.layerCount = 1;
  425. vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, NULL, 0, NULL, 1, use_barrier);
  426. }
  427. // Store our identifier
  428. io.Fonts->TexID = (void *)(intptr_t)g_FontImage;
  429. return true;
  430. }
  431. bool ImGui_ImplVulkan_CreateDeviceObjects()
  432. {
  433. VkResult err;
  434. VkShaderModule vert_module;
  435. VkShaderModule frag_module;
  436. // Create The Shader Modules:
  437. {
  438. VkShaderModuleCreateInfo vert_info = {};
  439. vert_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
  440. vert_info.codeSize = sizeof(__glsl_shader_vert_spv);
  441. vert_info.pCode = (uint32_t*)__glsl_shader_vert_spv;
  442. err = vkCreateShaderModule(g_Device, &vert_info, g_Allocator, &vert_module);
  443. ImGui_ImplVulkan_VkResult(err);
  444. VkShaderModuleCreateInfo frag_info = {};
  445. frag_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
  446. frag_info.codeSize = sizeof(__glsl_shader_frag_spv);
  447. frag_info.pCode = (uint32_t*)__glsl_shader_frag_spv;
  448. err = vkCreateShaderModule(g_Device, &frag_info, g_Allocator, &frag_module);
  449. ImGui_ImplVulkan_VkResult(err);
  450. }
  451. if (!g_FontSampler)
  452. {
  453. VkSamplerCreateInfo info = {};
  454. info.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
  455. info.magFilter = VK_FILTER_LINEAR;
  456. info.minFilter = VK_FILTER_LINEAR;
  457. info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
  458. info.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
  459. info.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
  460. info.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
  461. info.minLod = -1000;
  462. info.maxLod = 1000;
  463. info.maxAnisotropy = 1.0f;
  464. err = vkCreateSampler(g_Device, &info, g_Allocator, &g_FontSampler);
  465. ImGui_ImplVulkan_VkResult(err);
  466. }
  467. if (!g_DescriptorSetLayout)
  468. {
  469. VkSampler sampler[1] = {g_FontSampler};
  470. VkDescriptorSetLayoutBinding binding[1] = {};
  471. binding[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
  472. binding[0].descriptorCount = 1;
  473. binding[0].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
  474. binding[0].pImmutableSamplers = sampler;
  475. VkDescriptorSetLayoutCreateInfo info = {};
  476. info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
  477. info.bindingCount = 1;
  478. info.pBindings = binding;
  479. err = vkCreateDescriptorSetLayout(g_Device, &info, g_Allocator, &g_DescriptorSetLayout);
  480. ImGui_ImplVulkan_VkResult(err);
  481. }
  482. // Create Descriptor Set:
  483. {
  484. VkDescriptorSetAllocateInfo alloc_info = {};
  485. alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
  486. alloc_info.descriptorPool = g_DescriptorPool;
  487. alloc_info.descriptorSetCount = 1;
  488. alloc_info.pSetLayouts = &g_DescriptorSetLayout;
  489. err = vkAllocateDescriptorSets(g_Device, &alloc_info, &g_DescriptorSet);
  490. ImGui_ImplVulkan_VkResult(err);
  491. }
  492. if (!g_PipelineLayout)
  493. {
  494. VkPushConstantRange push_constants[1] = {};
  495. push_constants[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
  496. push_constants[0].offset = sizeof(float) * 0;
  497. push_constants[0].size = sizeof(float) * 4;
  498. VkDescriptorSetLayout set_layout[1] = {g_DescriptorSetLayout};
  499. VkPipelineLayoutCreateInfo layout_info = {};
  500. layout_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
  501. layout_info.setLayoutCount = 1;
  502. layout_info.pSetLayouts = set_layout;
  503. layout_info.pushConstantRangeCount = 1;
  504. layout_info.pPushConstantRanges = push_constants;
  505. err = vkCreatePipelineLayout(g_Device, &layout_info, g_Allocator, &g_PipelineLayout);
  506. ImGui_ImplVulkan_VkResult(err);
  507. }
  508. VkPipelineShaderStageCreateInfo stage[2] = {};
  509. stage[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
  510. stage[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
  511. stage[0].module = vert_module;
  512. stage[0].pName = "main";
  513. stage[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
  514. stage[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
  515. stage[1].module = frag_module;
  516. stage[1].pName = "main";
  517. VkVertexInputBindingDescription binding_desc[1] = {};
  518. binding_desc[0].stride = sizeof(ImDrawVert);
  519. binding_desc[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
  520. VkVertexInputAttributeDescription attribute_desc[3] = {};
  521. attribute_desc[0].location = 0;
  522. attribute_desc[0].binding = binding_desc[0].binding;
  523. attribute_desc[0].format = VK_FORMAT_R32G32_SFLOAT;
  524. attribute_desc[0].offset = (size_t)(&((ImDrawVert*)0)->pos);
  525. attribute_desc[1].location = 1;
  526. attribute_desc[1].binding = binding_desc[0].binding;
  527. attribute_desc[1].format = VK_FORMAT_R32G32_SFLOAT;
  528. attribute_desc[1].offset = (size_t)(&((ImDrawVert*)0)->uv);
  529. attribute_desc[2].location = 2;
  530. attribute_desc[2].binding = binding_desc[0].binding;
  531. attribute_desc[2].format = VK_FORMAT_R8G8B8A8_UNORM;
  532. attribute_desc[2].offset = (size_t)(&((ImDrawVert*)0)->col);
  533. VkPipelineVertexInputStateCreateInfo vertex_info = {};
  534. vertex_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
  535. vertex_info.vertexBindingDescriptionCount = 1;
  536. vertex_info.pVertexBindingDescriptions = binding_desc;
  537. vertex_info.vertexAttributeDescriptionCount = 3;
  538. vertex_info.pVertexAttributeDescriptions = attribute_desc;
  539. VkPipelineInputAssemblyStateCreateInfo ia_info = {};
  540. ia_info.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
  541. ia_info.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
  542. VkPipelineViewportStateCreateInfo viewport_info = {};
  543. viewport_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
  544. viewport_info.viewportCount = 1;
  545. viewport_info.scissorCount = 1;
  546. VkPipelineRasterizationStateCreateInfo raster_info = {};
  547. raster_info.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
  548. raster_info.polygonMode = VK_POLYGON_MODE_FILL;
  549. raster_info.cullMode = VK_CULL_MODE_NONE;
  550. raster_info.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
  551. raster_info.lineWidth = 1.0f;
  552. VkPipelineMultisampleStateCreateInfo ms_info = {};
  553. ms_info.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
  554. ms_info.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
  555. VkPipelineColorBlendAttachmentState color_attachment[1] = {};
  556. color_attachment[0].blendEnable = VK_TRUE;
  557. color_attachment[0].srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
  558. color_attachment[0].dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
  559. color_attachment[0].colorBlendOp = VK_BLEND_OP_ADD;
  560. color_attachment[0].srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
  561. color_attachment[0].dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
  562. color_attachment[0].alphaBlendOp = VK_BLEND_OP_ADD;
  563. color_attachment[0].colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
  564. VkPipelineDepthStencilStateCreateInfo depth_info = {};
  565. depth_info.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
  566. VkPipelineColorBlendStateCreateInfo blend_info = {};
  567. blend_info.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
  568. blend_info.attachmentCount = 1;
  569. blend_info.pAttachments = color_attachment;
  570. VkDynamicState dynamic_states[2] = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
  571. VkPipelineDynamicStateCreateInfo dynamic_state = {};
  572. dynamic_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
  573. dynamic_state.dynamicStateCount = 2;
  574. dynamic_state.pDynamicStates = dynamic_states;
  575. VkGraphicsPipelineCreateInfo info = {};
  576. info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
  577. info.flags = g_PipelineCreateFlags;
  578. info.stageCount = 2;
  579. info.pStages = stage;
  580. info.pVertexInputState = &vertex_info;
  581. info.pInputAssemblyState = &ia_info;
  582. info.pViewportState = &viewport_info;
  583. info.pRasterizationState = &raster_info;
  584. info.pMultisampleState = &ms_info;
  585. info.pDepthStencilState = &depth_info;
  586. info.pColorBlendState = &blend_info;
  587. info.pDynamicState = &dynamic_state;
  588. info.layout = g_PipelineLayout;
  589. info.renderPass = g_RenderPass;
  590. err = vkCreateGraphicsPipelines(g_Device, g_PipelineCache, 1, &info, g_Allocator, &g_Pipeline);
  591. ImGui_ImplVulkan_VkResult(err);
  592. vkDestroyShaderModule(g_Device, vert_module, g_Allocator);
  593. vkDestroyShaderModule(g_Device, frag_module, g_Allocator);
  594. return true;
  595. }
  596. void ImGui_ImplVulkan_InvalidateFontUploadObjects()
  597. {
  598. if (g_UploadBuffer)
  599. {
  600. vkDestroyBuffer(g_Device, g_UploadBuffer, g_Allocator);
  601. g_UploadBuffer = VK_NULL_HANDLE;
  602. }
  603. if (g_UploadBufferMemory)
  604. {
  605. vkFreeMemory(g_Device, g_UploadBufferMemory, g_Allocator);
  606. g_UploadBufferMemory = VK_NULL_HANDLE;
  607. }
  608. }
  609. void ImGui_ImplVulkan_InvalidateDeviceObjects()
  610. {
  611. ImGui_ImplVulkan_InvalidateFontUploadObjects();
  612. for (int i = 0; i < IMGUI_VK_QUEUED_FRAMES; i++)
  613. {
  614. if (g_VertexBuffer[i]) { vkDestroyBuffer(g_Device, g_VertexBuffer[i], g_Allocator); g_VertexBuffer[i] = VK_NULL_HANDLE; }
  615. if (g_VertexBufferMemory[i]) { vkFreeMemory(g_Device, g_VertexBufferMemory[i], g_Allocator); g_VertexBufferMemory[i] = VK_NULL_HANDLE; }
  616. if (g_IndexBuffer[i]) { vkDestroyBuffer(g_Device, g_IndexBuffer[i], g_Allocator); g_IndexBuffer[i] = VK_NULL_HANDLE; }
  617. if (g_IndexBufferMemory[i]) { vkFreeMemory(g_Device, g_IndexBufferMemory[i], g_Allocator); g_IndexBufferMemory[i] = VK_NULL_HANDLE; }
  618. }
  619. if (g_FontView) { vkDestroyImageView(g_Device, g_FontView, g_Allocator); g_FontView = VK_NULL_HANDLE; }
  620. if (g_FontImage) { vkDestroyImage(g_Device, g_FontImage, g_Allocator); g_FontImage = VK_NULL_HANDLE; }
  621. if (g_FontMemory) { vkFreeMemory(g_Device, g_FontMemory, g_Allocator); g_FontMemory = VK_NULL_HANDLE; }
  622. if (g_FontSampler) { vkDestroySampler(g_Device, g_FontSampler, g_Allocator); g_FontSampler = VK_NULL_HANDLE; }
  623. if (g_DescriptorSetLayout) { vkDestroyDescriptorSetLayout(g_Device, g_DescriptorSetLayout, g_Allocator); g_DescriptorSetLayout = VK_NULL_HANDLE; }
  624. if (g_PipelineLayout) { vkDestroyPipelineLayout(g_Device, g_PipelineLayout, g_Allocator); g_PipelineLayout = VK_NULL_HANDLE; }
  625. if (g_Pipeline) { vkDestroyPipeline(g_Device, g_Pipeline, g_Allocator); g_Pipeline = VK_NULL_HANDLE; }
  626. }
  627. bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitData *init_data)
  628. {
  629. g_Allocator = init_data->allocator;
  630. g_Gpu = init_data->gpu;
  631. g_Device = init_data->device;
  632. g_RenderPass = init_data->render_pass;
  633. g_PipelineCache = init_data->pipeline_cache;
  634. g_DescriptorPool = init_data->descriptor_pool;
  635. g_CheckVkResult = init_data->check_vk_result;
  636. ImGui_ImplVulkan_CreateDeviceObjects();
  637. return true;
  638. }
  639. void ImGui_ImplVulkan_Shutdown()
  640. {
  641. ImGui_ImplVulkan_InvalidateDeviceObjects();
  642. }
  643. void ImGui_ImplVulkan_NewFrame()
  644. {
  645. }
  646. void ImGui_ImplVulkan_Render(VkCommandBuffer command_buffer)
  647. {
  648. g_CommandBuffer = command_buffer;
  649. ImGui::Render();
  650. ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData());
  651. g_CommandBuffer = VK_NULL_HANDLE;
  652. g_FrameIndex = (g_FrameIndex + 1) % IMGUI_VK_QUEUED_FRAMES;
  653. }