imgui_impl_vulkan.cpp 40 KB

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