imgui_impl_vulkan.cpp 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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-03: Vulkan: Various refactor, created a couple of ImGui_ImplVulkanH_XXX helper that the example can use and that viewport support will use.
  12. // 2018-03-01: Vulkan: Renamed ImGui_ImplVulkan_Init_Info to ImGui_ImplVulkan_InitInfo and fields to match more closely Vulkan terminology.
  13. // 2018-02-18: Vulkan: Offset projection matrix and clipping rectangle by io.DisplayPos (which will be non-zero for multi-viewport applications).
  14. // 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback, ImGui_ImplVulkan_Render() calls ImGui_ImplVulkan_RenderDrawData() itself.
  15. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  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-08-27: Vulkan: Fix Vulkan example for use when a depth buffer is active.
  20. #include "imgui.h"
  21. #include "imgui_impl_vulkan.h"
  22. // Vulkan data
  23. static const VkAllocationCallbacks* g_Allocator = NULL;
  24. static VkPhysicalDevice g_PhysicalDevice = VK_NULL_HANDLE;
  25. static VkInstance g_Instance = VK_NULL_HANDLE;
  26. static VkDevice g_Device = VK_NULL_HANDLE;
  27. static VkPipelineCache g_PipelineCache = VK_NULL_HANDLE;
  28. static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE;
  29. static VkRenderPass g_RenderPass = VK_NULL_HANDLE;
  30. static void (*g_CheckVkResultFn)(VkResult err) = NULL;
  31. static VkDeviceSize g_BufferMemoryAlignment = 256;
  32. static VkPipelineCreateFlags g_PipelineCreateFlags = 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. // Frame data
  38. struct FrameDataForRender
  39. {
  40. VkDeviceMemory VertexBufferMemory;
  41. VkDeviceMemory IndexBufferMemory;
  42. VkDeviceSize VertexBufferSize;
  43. VkDeviceSize IndexBufferSize;
  44. VkBuffer VertexBuffer;
  45. VkBuffer IndexBuffer;
  46. };
  47. static int g_FrameIndex = 0;
  48. static FrameDataForRender g_FramesDataBuffers[IMGUI_VK_QUEUED_FRAMES];
  49. // Font data
  50. static VkSampler g_FontSampler = VK_NULL_HANDLE;
  51. static VkDeviceMemory g_FontMemory = VK_NULL_HANDLE;
  52. static VkImage g_FontImage = VK_NULL_HANDLE;
  53. static VkImageView g_FontView = VK_NULL_HANDLE;
  54. static VkDeviceMemory g_UploadBufferMemory = VK_NULL_HANDLE;
  55. static VkBuffer g_UploadBuffer = VK_NULL_HANDLE;
  56. // Forward Declarations
  57. static void ImGui_ImplVulkan_InitPlatformInterface();
  58. static void ImGui_ImplVulkan_ShutdownPlatformInterface();
  59. // glsl_shader.vert, compiled with:
  60. // # glslangValidator -V -x -o glsl_shader.vert.u32 glsl_shader.vert
  61. static uint32_t __glsl_shader_vert_spv[] =
  62. {
  63. 0x07230203,0x00010000,0x00080001,0x0000002e,0x00000000,0x00020011,0x00000001,0x0006000b,
  64. 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001,
  65. 0x000a000f,0x00000000,0x00000004,0x6e69616d,0x00000000,0x0000000b,0x0000000f,0x00000015,
  66. 0x0000001b,0x0000001c,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d,
  67. 0x00000000,0x00030005,0x00000009,0x00000000,0x00050006,0x00000009,0x00000000,0x6f6c6f43,
  68. 0x00000072,0x00040006,0x00000009,0x00000001,0x00005655,0x00030005,0x0000000b,0x0074754f,
  69. 0x00040005,0x0000000f,0x6c6f4361,0x0000726f,0x00030005,0x00000015,0x00565561,0x00060005,
  70. 0x00000019,0x505f6c67,0x65567265,0x78657472,0x00000000,0x00060006,0x00000019,0x00000000,
  71. 0x505f6c67,0x7469736f,0x006e6f69,0x00030005,0x0000001b,0x00000000,0x00040005,0x0000001c,
  72. 0x736f5061,0x00000000,0x00060005,0x0000001e,0x73755075,0x6e6f4368,0x6e617473,0x00000074,
  73. 0x00050006,0x0000001e,0x00000000,0x61635375,0x0000656c,0x00060006,0x0000001e,0x00000001,
  74. 0x61725475,0x616c736e,0x00006574,0x00030005,0x00000020,0x00006370,0x00040047,0x0000000b,
  75. 0x0000001e,0x00000000,0x00040047,0x0000000f,0x0000001e,0x00000002,0x00040047,0x00000015,
  76. 0x0000001e,0x00000001,0x00050048,0x00000019,0x00000000,0x0000000b,0x00000000,0x00030047,
  77. 0x00000019,0x00000002,0x00040047,0x0000001c,0x0000001e,0x00000000,0x00050048,0x0000001e,
  78. 0x00000000,0x00000023,0x00000000,0x00050048,0x0000001e,0x00000001,0x00000023,0x00000008,
  79. 0x00030047,0x0000001e,0x00000002,0x00020013,0x00000002,0x00030021,0x00000003,0x00000002,
  80. 0x00030016,0x00000006,0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,0x00040017,
  81. 0x00000008,0x00000006,0x00000002,0x0004001e,0x00000009,0x00000007,0x00000008,0x00040020,
  82. 0x0000000a,0x00000003,0x00000009,0x0004003b,0x0000000a,0x0000000b,0x00000003,0x00040015,
  83. 0x0000000c,0x00000020,0x00000001,0x0004002b,0x0000000c,0x0000000d,0x00000000,0x00040020,
  84. 0x0000000e,0x00000001,0x00000007,0x0004003b,0x0000000e,0x0000000f,0x00000001,0x00040020,
  85. 0x00000011,0x00000003,0x00000007,0x0004002b,0x0000000c,0x00000013,0x00000001,0x00040020,
  86. 0x00000014,0x00000001,0x00000008,0x0004003b,0x00000014,0x00000015,0x00000001,0x00040020,
  87. 0x00000017,0x00000003,0x00000008,0x0003001e,0x00000019,0x00000007,0x00040020,0x0000001a,
  88. 0x00000003,0x00000019,0x0004003b,0x0000001a,0x0000001b,0x00000003,0x0004003b,0x00000014,
  89. 0x0000001c,0x00000001,0x0004001e,0x0000001e,0x00000008,0x00000008,0x00040020,0x0000001f,
  90. 0x00000009,0x0000001e,0x0004003b,0x0000001f,0x00000020,0x00000009,0x00040020,0x00000021,
  91. 0x00000009,0x00000008,0x0004002b,0x00000006,0x00000028,0x00000000,0x0004002b,0x00000006,
  92. 0x00000029,0x3f800000,0x00050036,0x00000002,0x00000004,0x00000000,0x00000003,0x000200f8,
  93. 0x00000005,0x0004003d,0x00000007,0x00000010,0x0000000f,0x00050041,0x00000011,0x00000012,
  94. 0x0000000b,0x0000000d,0x0003003e,0x00000012,0x00000010,0x0004003d,0x00000008,0x00000016,
  95. 0x00000015,0x00050041,0x00000017,0x00000018,0x0000000b,0x00000013,0x0003003e,0x00000018,
  96. 0x00000016,0x0004003d,0x00000008,0x0000001d,0x0000001c,0x00050041,0x00000021,0x00000022,
  97. 0x00000020,0x0000000d,0x0004003d,0x00000008,0x00000023,0x00000022,0x00050085,0x00000008,
  98. 0x00000024,0x0000001d,0x00000023,0x00050041,0x00000021,0x00000025,0x00000020,0x00000013,
  99. 0x0004003d,0x00000008,0x00000026,0x00000025,0x00050081,0x00000008,0x00000027,0x00000024,
  100. 0x00000026,0x00050051,0x00000006,0x0000002a,0x00000027,0x00000000,0x00050051,0x00000006,
  101. 0x0000002b,0x00000027,0x00000001,0x00070050,0x00000007,0x0000002c,0x0000002a,0x0000002b,
  102. 0x00000028,0x00000029,0x00050041,0x00000011,0x0000002d,0x0000001b,0x0000000d,0x0003003e,
  103. 0x0000002d,0x0000002c,0x000100fd,0x00010038
  104. };
  105. // glsl_shader.frag, compiled with:
  106. // # glslangValidator -V -x -o glsl_shader.frag.u32 glsl_shader.frag
  107. static uint32_t __glsl_shader_frag_spv[] =
  108. {
  109. 0x07230203,0x00010000,0x00080001,0x0000001e,0x00000000,0x00020011,0x00000001,0x0006000b,
  110. 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001,
  111. 0x0007000f,0x00000004,0x00000004,0x6e69616d,0x00000000,0x00000009,0x0000000d,0x00030010,
  112. 0x00000004,0x00000007,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d,
  113. 0x00000000,0x00040005,0x00000009,0x6c6f4366,0x0000726f,0x00030005,0x0000000b,0x00000000,
  114. 0x00050006,0x0000000b,0x00000000,0x6f6c6f43,0x00000072,0x00040006,0x0000000b,0x00000001,
  115. 0x00005655,0x00030005,0x0000000d,0x00006e49,0x00050005,0x00000016,0x78655473,0x65727574,
  116. 0x00000000,0x00040047,0x00000009,0x0000001e,0x00000000,0x00040047,0x0000000d,0x0000001e,
  117. 0x00000000,0x00040047,0x00000016,0x00000022,0x00000000,0x00040047,0x00000016,0x00000021,
  118. 0x00000000,0x00020013,0x00000002,0x00030021,0x00000003,0x00000002,0x00030016,0x00000006,
  119. 0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,0x00040020,0x00000008,0x00000003,
  120. 0x00000007,0x0004003b,0x00000008,0x00000009,0x00000003,0x00040017,0x0000000a,0x00000006,
  121. 0x00000002,0x0004001e,0x0000000b,0x00000007,0x0000000a,0x00040020,0x0000000c,0x00000001,
  122. 0x0000000b,0x0004003b,0x0000000c,0x0000000d,0x00000001,0x00040015,0x0000000e,0x00000020,
  123. 0x00000001,0x0004002b,0x0000000e,0x0000000f,0x00000000,0x00040020,0x00000010,0x00000001,
  124. 0x00000007,0x00090019,0x00000013,0x00000006,0x00000001,0x00000000,0x00000000,0x00000000,
  125. 0x00000001,0x00000000,0x0003001b,0x00000014,0x00000013,0x00040020,0x00000015,0x00000000,
  126. 0x00000014,0x0004003b,0x00000015,0x00000016,0x00000000,0x0004002b,0x0000000e,0x00000018,
  127. 0x00000001,0x00040020,0x00000019,0x00000001,0x0000000a,0x00050036,0x00000002,0x00000004,
  128. 0x00000000,0x00000003,0x000200f8,0x00000005,0x00050041,0x00000010,0x00000011,0x0000000d,
  129. 0x0000000f,0x0004003d,0x00000007,0x00000012,0x00000011,0x0004003d,0x00000014,0x00000017,
  130. 0x00000016,0x00050041,0x00000019,0x0000001a,0x0000000d,0x00000018,0x0004003d,0x0000000a,
  131. 0x0000001b,0x0000001a,0x00050057,0x00000007,0x0000001c,0x00000017,0x0000001b,0x00050085,
  132. 0x00000007,0x0000001d,0x00000012,0x0000001c,0x0003003e,0x00000009,0x0000001d,0x000100fd,
  133. 0x00010038
  134. };
  135. static uint32_t ImGui_ImplVulkan_MemoryType(VkMemoryPropertyFlags properties, uint32_t type_bits)
  136. {
  137. VkPhysicalDeviceMemoryProperties prop;
  138. vkGetPhysicalDeviceMemoryProperties(g_PhysicalDevice, &prop);
  139. for (uint32_t i = 0; i < prop.memoryTypeCount; i++)
  140. if ((prop.memoryTypes[i].propertyFlags & properties) == properties && type_bits & (1<<i))
  141. return i;
  142. return 0xFFFFFFFF; // Unable to find memoryType
  143. }
  144. static void check_vk_result(VkResult err)
  145. {
  146. if (g_CheckVkResultFn)
  147. g_CheckVkResultFn(err);
  148. }
  149. static void CreateOrResizeBuffer(VkBuffer& buffer, VkDeviceMemory& buffer_memory, VkDeviceSize& p_buffer_size, size_t new_size, VkBufferUsageFlagBits usage)
  150. {
  151. VkResult err;
  152. if (buffer != NULL)
  153. vkDestroyBuffer(g_Device, buffer, g_Allocator);
  154. if (buffer_memory)
  155. vkFreeMemory(g_Device, buffer_memory, g_Allocator);
  156. VkDeviceSize vertex_buffer_size_aligned = ((new_size - 1) / g_BufferMemoryAlignment + 1) * g_BufferMemoryAlignment;
  157. VkBufferCreateInfo buffer_info = {};
  158. buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  159. buffer_info.size = vertex_buffer_size_aligned;
  160. buffer_info.usage = usage;
  161. buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  162. err = vkCreateBuffer(g_Device, &buffer_info, g_Allocator, &buffer);
  163. check_vk_result(err);
  164. VkMemoryRequirements req;
  165. vkGetBufferMemoryRequirements(g_Device, buffer, &req);
  166. g_BufferMemoryAlignment = (g_BufferMemoryAlignment > req.alignment) ? g_BufferMemoryAlignment : req.alignment;
  167. VkMemoryAllocateInfo alloc_info = {};
  168. alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  169. alloc_info.allocationSize = req.size;
  170. alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
  171. err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &buffer_memory);
  172. check_vk_result(err);
  173. err = vkBindBufferMemory(g_Device, buffer, buffer_memory, 0);
  174. check_vk_result(err);
  175. p_buffer_size = new_size;
  176. }
  177. // This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
  178. void ImGui_ImplVulkan_RenderDrawData(VkCommandBuffer command_buffer, ImDrawData* draw_data)
  179. {
  180. VkResult err;
  181. ImGuiIO& io = ImGui::GetIO();
  182. if (draw_data->TotalVtxCount == 0)
  183. return;
  184. FrameDataForRender* fd = &g_FramesDataBuffers[g_FrameIndex];
  185. // Create the Vertex and Index buffers:
  186. size_t vertex_size = draw_data->TotalVtxCount * sizeof(ImDrawVert);
  187. size_t index_size = draw_data->TotalIdxCount * sizeof(ImDrawIdx);
  188. if (!fd->VertexBuffer || fd->VertexBufferSize < vertex_size)
  189. CreateOrResizeBuffer(fd->VertexBuffer, fd->VertexBufferMemory, fd->VertexBufferSize, vertex_size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
  190. if (!fd->IndexBuffer || fd->IndexBufferSize < index_size)
  191. CreateOrResizeBuffer(fd->IndexBuffer, fd->IndexBufferMemory, fd->IndexBufferSize, index_size, VK_BUFFER_USAGE_INDEX_BUFFER_BIT);
  192. // Upload Vertex and index Data:
  193. {
  194. ImDrawVert* vtx_dst;
  195. ImDrawIdx* idx_dst;
  196. err = vkMapMemory(g_Device, fd->VertexBufferMemory, 0, vertex_size, 0, (void**)(&vtx_dst));
  197. check_vk_result(err);
  198. err = vkMapMemory(g_Device, fd->IndexBufferMemory, 0, index_size, 0, (void**)(&idx_dst));
  199. check_vk_result(err);
  200. for (int n = 0; n < draw_data->CmdListsCount; n++)
  201. {
  202. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  203. memcpy(vtx_dst, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert));
  204. memcpy(idx_dst, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx));
  205. vtx_dst += cmd_list->VtxBuffer.Size;
  206. idx_dst += cmd_list->IdxBuffer.Size;
  207. }
  208. VkMappedMemoryRange range[2] = {};
  209. range[0].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
  210. range[0].memory = fd->VertexBufferMemory;
  211. range[0].size = VK_WHOLE_SIZE;
  212. range[1].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
  213. range[1].memory = fd->IndexBufferMemory;
  214. range[1].size = VK_WHOLE_SIZE;
  215. err = vkFlushMappedMemoryRanges(g_Device, 2, range);
  216. check_vk_result(err);
  217. vkUnmapMemory(g_Device, fd->VertexBufferMemory);
  218. vkUnmapMemory(g_Device, fd->IndexBufferMemory);
  219. }
  220. // Bind pipeline and descriptor sets:
  221. {
  222. vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, g_Pipeline);
  223. VkDescriptorSet desc_set[1] = { g_DescriptorSet };
  224. vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, g_PipelineLayout, 0, 1, desc_set, 0, NULL);
  225. }
  226. // Bind Vertex And Index Buffer:
  227. {
  228. VkBuffer vertex_buffers[1] = { fd->VertexBuffer };
  229. VkDeviceSize vertex_offset[1] = { 0 };
  230. vkCmdBindVertexBuffers(command_buffer, 0, 1, vertex_buffers, vertex_offset);
  231. vkCmdBindIndexBuffer(command_buffer, fd->IndexBuffer, 0, VK_INDEX_TYPE_UINT16);
  232. }
  233. // Setup viewport:
  234. {
  235. VkViewport viewport;
  236. viewport.x = 0;
  237. viewport.y = 0;
  238. viewport.width = io.DisplaySize.x;
  239. viewport.height = io.DisplaySize.y;
  240. viewport.minDepth = 0.0f;
  241. viewport.maxDepth = 1.0f;
  242. vkCmdSetViewport(command_buffer, 0, 1, &viewport);
  243. }
  244. // Setup scale and translation:
  245. // (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.)
  246. {
  247. float scale[2];
  248. scale[0] = 2.0f / io.DisplaySize.x;
  249. scale[1] = 2.0f / io.DisplaySize.y;
  250. float translate[2];
  251. translate[0] = -1.0f - io.DisplayPos.x * scale[0];
  252. translate[1] = -1.0f - io.DisplayPos.y * scale[1];
  253. vkCmdPushConstants(command_buffer, g_PipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, sizeof(float) * 0, sizeof(float) * 2, scale);
  254. vkCmdPushConstants(command_buffer, g_PipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, sizeof(float) * 2, sizeof(float) * 2, translate);
  255. }
  256. // Render the command lists:
  257. int vtx_offset = 0;
  258. int idx_offset = 0;
  259. for (int n = 0; n < draw_data->CmdListsCount; n++)
  260. {
  261. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  262. for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
  263. {
  264. const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
  265. if (pcmd->UserCallback)
  266. {
  267. pcmd->UserCallback(cmd_list, pcmd);
  268. }
  269. else
  270. {
  271. // Apply scissor/clipping rectangle
  272. // FIXME: We could clamp width/height based on clamped min/max values.
  273. VkRect2D scissor;
  274. scissor.offset.x = (int32_t)(pcmd->ClipRect.x - io.DisplayPos.x) > 0 ? (int32_t)(pcmd->ClipRect.x - io.DisplayPos.y) : 0;
  275. scissor.offset.y = (int32_t)(pcmd->ClipRect.y - io.DisplayPos.y) > 0 ? (int32_t)(pcmd->ClipRect.y - io.DisplayPos.y) : 0;
  276. scissor.extent.width = (uint32_t)(pcmd->ClipRect.z - pcmd->ClipRect.x);
  277. scissor.extent.height = (uint32_t)(pcmd->ClipRect.w - pcmd->ClipRect.y + 1); // FIXME: Why +1 here?
  278. vkCmdSetScissor(command_buffer, 0, 1, &scissor);
  279. // Draw
  280. vkCmdDrawIndexed(command_buffer, pcmd->ElemCount, 1, idx_offset, vtx_offset, 0);
  281. }
  282. idx_offset += pcmd->ElemCount;
  283. }
  284. vtx_offset += cmd_list->VtxBuffer.Size;
  285. }
  286. }
  287. bool ImGui_ImplVulkan_CreateFontsTexture(VkCommandBuffer command_buffer)
  288. {
  289. ImGuiIO& io = ImGui::GetIO();
  290. unsigned char* pixels;
  291. int width, height;
  292. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
  293. size_t upload_size = width*height*4*sizeof(char);
  294. VkResult err;
  295. // Create the Image:
  296. {
  297. VkImageCreateInfo info = {};
  298. info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
  299. info.imageType = VK_IMAGE_TYPE_2D;
  300. info.format = VK_FORMAT_R8G8B8A8_UNORM;
  301. info.extent.width = width;
  302. info.extent.height = height;
  303. info.extent.depth = 1;
  304. info.mipLevels = 1;
  305. info.arrayLayers = 1;
  306. info.samples = VK_SAMPLE_COUNT_1_BIT;
  307. info.tiling = VK_IMAGE_TILING_OPTIMAL;
  308. info.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
  309. info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  310. info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  311. err = vkCreateImage(g_Device, &info, g_Allocator, &g_FontImage);
  312. check_vk_result(err);
  313. VkMemoryRequirements req;
  314. vkGetImageMemoryRequirements(g_Device, g_FontImage, &req);
  315. VkMemoryAllocateInfo alloc_info = {};
  316. alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  317. alloc_info.allocationSize = req.size;
  318. alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, req.memoryTypeBits);
  319. err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &g_FontMemory);
  320. check_vk_result(err);
  321. err = vkBindImageMemory(g_Device, g_FontImage, g_FontMemory, 0);
  322. check_vk_result(err);
  323. }
  324. // Create the Image View:
  325. {
  326. VkImageViewCreateInfo info = {};
  327. info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
  328. info.image = g_FontImage;
  329. info.viewType = VK_IMAGE_VIEW_TYPE_2D;
  330. info.format = VK_FORMAT_R8G8B8A8_UNORM;
  331. info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  332. info.subresourceRange.levelCount = 1;
  333. info.subresourceRange.layerCount = 1;
  334. err = vkCreateImageView(g_Device, &info, g_Allocator, &g_FontView);
  335. check_vk_result(err);
  336. }
  337. // Update the Descriptor Set:
  338. {
  339. VkDescriptorImageInfo desc_image[1] = {};
  340. desc_image[0].sampler = g_FontSampler;
  341. desc_image[0].imageView = g_FontView;
  342. desc_image[0].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  343. VkWriteDescriptorSet write_desc[1] = {};
  344. write_desc[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  345. write_desc[0].dstSet = g_DescriptorSet;
  346. write_desc[0].descriptorCount = 1;
  347. write_desc[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
  348. write_desc[0].pImageInfo = desc_image;
  349. vkUpdateDescriptorSets(g_Device, 1, write_desc, 0, NULL);
  350. }
  351. // Create the Upload Buffer:
  352. {
  353. VkBufferCreateInfo buffer_info = {};
  354. buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  355. buffer_info.size = upload_size;
  356. buffer_info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
  357. buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  358. err = vkCreateBuffer(g_Device, &buffer_info, g_Allocator, &g_UploadBuffer);
  359. check_vk_result(err);
  360. VkMemoryRequirements req;
  361. vkGetBufferMemoryRequirements(g_Device, g_UploadBuffer, &req);
  362. g_BufferMemoryAlignment = (g_BufferMemoryAlignment > req.alignment) ? g_BufferMemoryAlignment : req.alignment;
  363. VkMemoryAllocateInfo alloc_info = {};
  364. alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  365. alloc_info.allocationSize = req.size;
  366. alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
  367. err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &g_UploadBufferMemory);
  368. check_vk_result(err);
  369. err = vkBindBufferMemory(g_Device, g_UploadBuffer, g_UploadBufferMemory, 0);
  370. check_vk_result(err);
  371. }
  372. // Upload to Buffer:
  373. {
  374. char* map = NULL;
  375. err = vkMapMemory(g_Device, g_UploadBufferMemory, 0, upload_size, 0, (void**)(&map));
  376. check_vk_result(err);
  377. memcpy(map, pixels, upload_size);
  378. VkMappedMemoryRange range[1] = {};
  379. range[0].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
  380. range[0].memory = g_UploadBufferMemory;
  381. range[0].size = upload_size;
  382. err = vkFlushMappedMemoryRanges(g_Device, 1, range);
  383. check_vk_result(err);
  384. vkUnmapMemory(g_Device, g_UploadBufferMemory);
  385. }
  386. // Copy to Image:
  387. {
  388. VkImageMemoryBarrier copy_barrier[1] = {};
  389. copy_barrier[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
  390. copy_barrier[0].dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
  391. copy_barrier[0].oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  392. copy_barrier[0].newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
  393. copy_barrier[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  394. copy_barrier[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  395. copy_barrier[0].image = g_FontImage;
  396. copy_barrier[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  397. copy_barrier[0].subresourceRange.levelCount = 1;
  398. copy_barrier[0].subresourceRange.layerCount = 1;
  399. vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 0, NULL, 1, copy_barrier);
  400. VkBufferImageCopy region = {};
  401. region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  402. region.imageSubresource.layerCount = 1;
  403. region.imageExtent.width = width;
  404. region.imageExtent.height = height;
  405. region.imageExtent.depth = 1;
  406. vkCmdCopyBufferToImage(command_buffer, g_UploadBuffer, g_FontImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
  407. VkImageMemoryBarrier use_barrier[1] = {};
  408. use_barrier[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
  409. use_barrier[0].srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
  410. use_barrier[0].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
  411. use_barrier[0].oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
  412. use_barrier[0].newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  413. use_barrier[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  414. use_barrier[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  415. use_barrier[0].image = g_FontImage;
  416. use_barrier[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  417. use_barrier[0].subresourceRange.levelCount = 1;
  418. use_barrier[0].subresourceRange.layerCount = 1;
  419. vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, NULL, 0, NULL, 1, use_barrier);
  420. }
  421. // Store our identifier
  422. io.Fonts->TexID = (void *)(intptr_t)g_FontImage;
  423. return true;
  424. }
  425. bool ImGui_ImplVulkan_CreateDeviceObjects()
  426. {
  427. VkResult err;
  428. VkShaderModule vert_module;
  429. VkShaderModule frag_module;
  430. // Create The Shader Modules:
  431. {
  432. VkShaderModuleCreateInfo vert_info = {};
  433. vert_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
  434. vert_info.codeSize = sizeof(__glsl_shader_vert_spv);
  435. vert_info.pCode = (uint32_t*)__glsl_shader_vert_spv;
  436. err = vkCreateShaderModule(g_Device, &vert_info, g_Allocator, &vert_module);
  437. check_vk_result(err);
  438. VkShaderModuleCreateInfo frag_info = {};
  439. frag_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
  440. frag_info.codeSize = sizeof(__glsl_shader_frag_spv);
  441. frag_info.pCode = (uint32_t*)__glsl_shader_frag_spv;
  442. err = vkCreateShaderModule(g_Device, &frag_info, g_Allocator, &frag_module);
  443. check_vk_result(err);
  444. }
  445. if (!g_FontSampler)
  446. {
  447. VkSamplerCreateInfo info = {};
  448. info.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
  449. info.magFilter = VK_FILTER_LINEAR;
  450. info.minFilter = VK_FILTER_LINEAR;
  451. info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
  452. info.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
  453. info.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
  454. info.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
  455. info.minLod = -1000;
  456. info.maxLod = 1000;
  457. info.maxAnisotropy = 1.0f;
  458. err = vkCreateSampler(g_Device, &info, g_Allocator, &g_FontSampler);
  459. check_vk_result(err);
  460. }
  461. if (!g_DescriptorSetLayout)
  462. {
  463. VkSampler sampler[1] = {g_FontSampler};
  464. VkDescriptorSetLayoutBinding binding[1] = {};
  465. binding[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
  466. binding[0].descriptorCount = 1;
  467. binding[0].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
  468. binding[0].pImmutableSamplers = sampler;
  469. VkDescriptorSetLayoutCreateInfo info = {};
  470. info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
  471. info.bindingCount = 1;
  472. info.pBindings = binding;
  473. err = vkCreateDescriptorSetLayout(g_Device, &info, g_Allocator, &g_DescriptorSetLayout);
  474. check_vk_result(err);
  475. }
  476. // Create Descriptor Set:
  477. {
  478. VkDescriptorSetAllocateInfo alloc_info = {};
  479. alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
  480. alloc_info.descriptorPool = g_DescriptorPool;
  481. alloc_info.descriptorSetCount = 1;
  482. alloc_info.pSetLayouts = &g_DescriptorSetLayout;
  483. err = vkAllocateDescriptorSets(g_Device, &alloc_info, &g_DescriptorSet);
  484. check_vk_result(err);
  485. }
  486. if (!g_PipelineLayout)
  487. {
  488. // Constants: we are using 'vec2 offset' and 'vec2 scale' instead of a full 3d projection matrix
  489. VkPushConstantRange push_constants[1] = {};
  490. push_constants[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
  491. push_constants[0].offset = sizeof(float) * 0;
  492. push_constants[0].size = sizeof(float) * 4;
  493. VkDescriptorSetLayout set_layout[1] = { g_DescriptorSetLayout };
  494. VkPipelineLayoutCreateInfo layout_info = {};
  495. layout_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
  496. layout_info.setLayoutCount = 1;
  497. layout_info.pSetLayouts = set_layout;
  498. layout_info.pushConstantRangeCount = 1;
  499. layout_info.pPushConstantRanges = push_constants;
  500. err = vkCreatePipelineLayout(g_Device, &layout_info, g_Allocator, &g_PipelineLayout);
  501. check_vk_result(err);
  502. }
  503. VkPipelineShaderStageCreateInfo stage[2] = {};
  504. stage[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
  505. stage[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
  506. stage[0].module = vert_module;
  507. stage[0].pName = "main";
  508. stage[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
  509. stage[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
  510. stage[1].module = frag_module;
  511. stage[1].pName = "main";
  512. VkVertexInputBindingDescription binding_desc[1] = {};
  513. binding_desc[0].stride = sizeof(ImDrawVert);
  514. binding_desc[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
  515. VkVertexInputAttributeDescription attribute_desc[3] = {};
  516. attribute_desc[0].location = 0;
  517. attribute_desc[0].binding = binding_desc[0].binding;
  518. attribute_desc[0].format = VK_FORMAT_R32G32_SFLOAT;
  519. attribute_desc[0].offset = IM_OFFSETOF(ImDrawVert, pos);
  520. attribute_desc[1].location = 1;
  521. attribute_desc[1].binding = binding_desc[0].binding;
  522. attribute_desc[1].format = VK_FORMAT_R32G32_SFLOAT;
  523. attribute_desc[1].offset = IM_OFFSETOF(ImDrawVert, uv);
  524. attribute_desc[2].location = 2;
  525. attribute_desc[2].binding = binding_desc[0].binding;
  526. attribute_desc[2].format = VK_FORMAT_R8G8B8A8_UNORM;
  527. attribute_desc[2].offset = IM_OFFSETOF(ImDrawVert, col);
  528. VkPipelineVertexInputStateCreateInfo vertex_info = {};
  529. vertex_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
  530. vertex_info.vertexBindingDescriptionCount = 1;
  531. vertex_info.pVertexBindingDescriptions = binding_desc;
  532. vertex_info.vertexAttributeDescriptionCount = 3;
  533. vertex_info.pVertexAttributeDescriptions = attribute_desc;
  534. VkPipelineInputAssemblyStateCreateInfo ia_info = {};
  535. ia_info.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
  536. ia_info.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
  537. VkPipelineViewportStateCreateInfo viewport_info = {};
  538. viewport_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
  539. viewport_info.viewportCount = 1;
  540. viewport_info.scissorCount = 1;
  541. VkPipelineRasterizationStateCreateInfo raster_info = {};
  542. raster_info.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
  543. raster_info.polygonMode = VK_POLYGON_MODE_FILL;
  544. raster_info.cullMode = VK_CULL_MODE_NONE;
  545. raster_info.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
  546. raster_info.lineWidth = 1.0f;
  547. VkPipelineMultisampleStateCreateInfo ms_info = {};
  548. ms_info.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
  549. ms_info.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
  550. VkPipelineColorBlendAttachmentState color_attachment[1] = {};
  551. color_attachment[0].blendEnable = VK_TRUE;
  552. color_attachment[0].srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
  553. color_attachment[0].dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
  554. color_attachment[0].colorBlendOp = VK_BLEND_OP_ADD;
  555. color_attachment[0].srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
  556. color_attachment[0].dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
  557. color_attachment[0].alphaBlendOp = VK_BLEND_OP_ADD;
  558. color_attachment[0].colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
  559. VkPipelineDepthStencilStateCreateInfo depth_info = {};
  560. depth_info.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
  561. VkPipelineColorBlendStateCreateInfo blend_info = {};
  562. blend_info.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
  563. blend_info.attachmentCount = 1;
  564. blend_info.pAttachments = color_attachment;
  565. VkDynamicState dynamic_states[2] = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
  566. VkPipelineDynamicStateCreateInfo dynamic_state = {};
  567. dynamic_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
  568. dynamic_state.dynamicStateCount = 2;
  569. dynamic_state.pDynamicStates = dynamic_states;
  570. VkGraphicsPipelineCreateInfo info = {};
  571. info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
  572. info.flags = g_PipelineCreateFlags;
  573. info.stageCount = 2;
  574. info.pStages = stage;
  575. info.pVertexInputState = &vertex_info;
  576. info.pInputAssemblyState = &ia_info;
  577. info.pViewportState = &viewport_info;
  578. info.pRasterizationState = &raster_info;
  579. info.pMultisampleState = &ms_info;
  580. info.pDepthStencilState = &depth_info;
  581. info.pColorBlendState = &blend_info;
  582. info.pDynamicState = &dynamic_state;
  583. info.layout = g_PipelineLayout;
  584. info.renderPass = g_RenderPass;
  585. err = vkCreateGraphicsPipelines(g_Device, g_PipelineCache, 1, &info, g_Allocator, &g_Pipeline);
  586. check_vk_result(err);
  587. vkDestroyShaderModule(g_Device, vert_module, g_Allocator);
  588. vkDestroyShaderModule(g_Device, frag_module, g_Allocator);
  589. return true;
  590. }
  591. void ImGui_ImplVulkan_InvalidateFontUploadObjects()
  592. {
  593. if (g_UploadBuffer)
  594. {
  595. vkDestroyBuffer(g_Device, g_UploadBuffer, g_Allocator);
  596. g_UploadBuffer = VK_NULL_HANDLE;
  597. }
  598. if (g_UploadBufferMemory)
  599. {
  600. vkFreeMemory(g_Device, g_UploadBufferMemory, g_Allocator);
  601. g_UploadBufferMemory = VK_NULL_HANDLE;
  602. }
  603. }
  604. void ImGui_ImplVulkan_InvalidateDeviceObjects()
  605. {
  606. ImGui_ImplVulkan_InvalidateFontUploadObjects();
  607. for (int i = 0; i < IMGUI_VK_QUEUED_FRAMES; i++)
  608. {
  609. FrameDataForRender* fd = &g_FramesDataBuffers[i];
  610. if (fd->VertexBuffer) { vkDestroyBuffer (g_Device, fd->VertexBuffer, g_Allocator); fd->VertexBuffer = VK_NULL_HANDLE; }
  611. if (fd->VertexBufferMemory) { vkFreeMemory (g_Device, fd->VertexBufferMemory, g_Allocator); fd->VertexBufferMemory = VK_NULL_HANDLE; }
  612. if (fd->IndexBuffer) { vkDestroyBuffer (g_Device, fd->IndexBuffer, g_Allocator); fd->IndexBuffer = VK_NULL_HANDLE; }
  613. if (fd->IndexBufferMemory) { vkFreeMemory (g_Device, fd->IndexBufferMemory, g_Allocator); fd->IndexBufferMemory = VK_NULL_HANDLE; }
  614. }
  615. if (g_FontView) { vkDestroyImageView(g_Device, g_FontView, g_Allocator); g_FontView = VK_NULL_HANDLE; }
  616. if (g_FontImage) { vkDestroyImage(g_Device, g_FontImage, g_Allocator); g_FontImage = VK_NULL_HANDLE; }
  617. if (g_FontMemory) { vkFreeMemory(g_Device, g_FontMemory, g_Allocator); g_FontMemory = VK_NULL_HANDLE; }
  618. if (g_FontSampler) { vkDestroySampler(g_Device, g_FontSampler, g_Allocator); g_FontSampler = VK_NULL_HANDLE; }
  619. if (g_DescriptorSetLayout) { vkDestroyDescriptorSetLayout(g_Device, g_DescriptorSetLayout, g_Allocator); g_DescriptorSetLayout = VK_NULL_HANDLE; }
  620. if (g_PipelineLayout) { vkDestroyPipelineLayout(g_Device, g_PipelineLayout, g_Allocator); g_PipelineLayout = VK_NULL_HANDLE; }
  621. if (g_Pipeline) { vkDestroyPipeline(g_Device, g_Pipeline, g_Allocator); g_Pipeline = VK_NULL_HANDLE; }
  622. }
  623. bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass render_pass)
  624. {
  625. IM_ASSERT(info->Instance != NULL);
  626. IM_ASSERT(info->PhysicalDevice != NULL);
  627. IM_ASSERT(info->Device != NULL);
  628. g_Allocator = info->Allocator;
  629. g_PhysicalDevice = info->PhysicalDevice;
  630. g_Device = info->Device;
  631. g_RenderPass = render_pass;
  632. g_PipelineCache = info->PipelineCache;
  633. g_DescriptorPool = info->DescriptorPool;
  634. g_CheckVkResultFn = info->CheckVkResultFn;
  635. ImGuiIO& io = ImGui::GetIO();
  636. ImGui_ImplVulkan_CreateDeviceObjects();
  637. io.ConfigFlags |= ImGuiConfigFlags_RendererHasViewports;
  638. if (io.ConfigFlags & ImGuiConfigFlags_EnableViewports)
  639. ImGui_ImplVulkan_InitPlatformInterface();
  640. return true;
  641. }
  642. void ImGui_ImplVulkan_Shutdown()
  643. {
  644. ImGui_ImplVulkan_ShutdownPlatformInterface();
  645. ImGui_ImplVulkan_InvalidateDeviceObjects();
  646. }
  647. void ImGui_ImplVulkan_NewFrame()
  648. {
  649. }
  650. void ImGui_ImplVulkan_Render(VkCommandBuffer command_buffer)
  651. {
  652. ImGui_ImplVulkan_RenderDrawData(command_buffer, ImGui::GetDrawData());
  653. g_FrameIndex = (g_FrameIndex + 1) % IMGUI_VK_QUEUED_FRAMES;
  654. }
  655. //-------------------------------------------------------------------------
  656. // Miscellaneous Vulkan Helpers
  657. // (Those are currently not strictly needed by the binding, but will be once if we support multi-viewports)
  658. //-------------------------------------------------------------------------
  659. #include <stdlib.h> // malloc
  660. ImGui_ImplVulkan_FrameData::ImGui_ImplVulkan_FrameData()
  661. {
  662. BackbufferIndex = 0;
  663. CommandPool = VK_NULL_HANDLE;
  664. CommandBuffer = VK_NULL_HANDLE;
  665. Fence = VK_NULL_HANDLE;
  666. PresentCompleteSemaphore = VK_NULL_HANDLE;
  667. RenderCompleteSemaphore = VK_NULL_HANDLE;
  668. }
  669. ImGui_ImplVulkan_WindowData::ImGui_ImplVulkan_WindowData()
  670. {
  671. Width = Height = 0;
  672. Swapchain = VK_NULL_HANDLE;
  673. Surface = VK_NULL_HANDLE;
  674. memset(&SurfaceFormat, 0, sizeof(SurfaceFormat));
  675. PresentMode = VK_PRESENT_MODE_MAX_ENUM_KHR;
  676. RenderPass = VK_NULL_HANDLE;
  677. memset(&ClearValue, 0, sizeof(ClearValue));
  678. BackBufferCount = 0;
  679. memset(&BackBuffer, 0, sizeof(BackBuffer));
  680. memset(&BackBufferView, 0, sizeof(BackBufferView));
  681. memset(&Framebuffer, 0, sizeof(Framebuffer));
  682. FrameIndex = 0;
  683. }
  684. VkSurfaceFormatKHR ImGui_ImplVulkanH_SelectSurfaceFormat(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkFormat* request_formats, int request_formats_count, VkColorSpaceKHR request_color_space)
  685. {
  686. IM_ASSERT(request_formats != NULL);
  687. IM_ASSERT(request_formats_count > 0);
  688. // Per Spec Format and View Format are expected to be the same unless VK_IMAGE_CREATE_MUTABLE_BIT was set at image creation
  689. // Assuming that the default behavior is without setting this bit, there is no need for separate Swapchain image and image view format
  690. // Additionally several new color spaces were introduced with Vulkan Spec v1.0.40,
  691. // hence we must make sure that a format with the mostly available color space, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR, is found and used.
  692. uint32_t avail_count;
  693. vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface, &avail_count, NULL);
  694. ImVector<VkSurfaceFormatKHR> avail_format;
  695. avail_format.resize((int)avail_count);
  696. vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface, &avail_count, avail_format.Data);
  697. // First check if only one format, VK_FORMAT_UNDEFINED, is available, which would imply that any format is available
  698. if (avail_count == 1)
  699. {
  700. if (avail_format[0].format == VK_FORMAT_UNDEFINED)
  701. {
  702. VkSurfaceFormatKHR ret;
  703. ret.format = request_formats[0];
  704. ret.colorSpace = request_color_space;
  705. return ret;
  706. }
  707. else
  708. {
  709. // No point in searching another format
  710. return avail_format[0];
  711. }
  712. }
  713. else
  714. {
  715. // Request several formats, the first found will be used
  716. for (int request_i = 0; request_i < request_formats_count; request_i++)
  717. for (uint32_t avail_i = 0; avail_i < avail_count; avail_i++)
  718. if (avail_format[avail_i].format == request_formats[request_i] && avail_format[avail_i].colorSpace == request_color_space)
  719. return avail_format[avail_i];
  720. // If none of the requested image formats could be found, use the first available
  721. return avail_format[0];
  722. }
  723. }
  724. VkPresentModeKHR ImGui_ImplVulkanH_SelectPresentMode(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkPresentModeKHR* request_modes, int request_modes_count)
  725. {
  726. IM_ASSERT(request_modes != NULL);
  727. IM_ASSERT(request_modes_count > 0);
  728. // Request a certain mode and confirm that it is available. If not use VK_PRESENT_MODE_FIFO_KHR which is mandatory
  729. uint32_t avail_count = 0;
  730. vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface, &avail_count, NULL);
  731. ImVector<VkPresentModeKHR> avail_modes;
  732. avail_modes.resize((int)avail_count);
  733. vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface, &avail_count, avail_modes.Data);
  734. for (int request_i = 0; request_i < request_modes_count; request_i++)
  735. for (uint32_t avail_i = 0; avail_i < avail_count; avail_i++)
  736. if (request_modes[request_i] == avail_modes[avail_i])
  737. return request_modes[request_i];
  738. return VK_PRESENT_MODE_FIFO_KHR; // Always available
  739. }
  740. void ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(VkPhysicalDevice physical_device, VkDevice device, uint32_t queue_family, ImGui_ImplVulkan_WindowData* wd, const VkAllocationCallbacks* allocator)
  741. {
  742. IM_ASSERT(physical_device != NULL && device != NULL);
  743. (void)allocator;
  744. // Create Command Buffers
  745. VkResult err;
  746. for (int i = 0; i < IMGUI_VK_QUEUED_FRAMES; i++)
  747. {
  748. ImGui_ImplVulkan_FrameData* fd = &wd->Frames[i];
  749. {
  750. VkCommandPoolCreateInfo info = {};
  751. info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
  752. info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
  753. info.queueFamilyIndex = queue_family;
  754. err = vkCreateCommandPool(device, &info, allocator, &fd->CommandPool);
  755. check_vk_result(err);
  756. }
  757. {
  758. VkCommandBufferAllocateInfo info = {};
  759. info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
  760. info.commandPool = fd->CommandPool;
  761. info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
  762. info.commandBufferCount = 1;
  763. err = vkAllocateCommandBuffers(device, &info, &fd->CommandBuffer);
  764. check_vk_result(err);
  765. }
  766. {
  767. VkFenceCreateInfo info = {};
  768. info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
  769. info.flags = VK_FENCE_CREATE_SIGNALED_BIT;
  770. err = vkCreateFence(device, &info, allocator, &fd->Fence);
  771. check_vk_result(err);
  772. }
  773. {
  774. VkSemaphoreCreateInfo info = {};
  775. info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
  776. err = vkCreateSemaphore(device, &info, allocator, &fd->PresentCompleteSemaphore);
  777. check_vk_result(err);
  778. err = vkCreateSemaphore(device, &info, allocator, &fd->RenderCompleteSemaphore);
  779. check_vk_result(err);
  780. }
  781. }
  782. }
  783. void ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkan_WindowData* wd, const VkAllocationCallbacks* allocator, int w, int h)
  784. {
  785. VkResult err;
  786. VkSwapchainKHR old_swapchain = wd->Swapchain;
  787. err = vkDeviceWaitIdle(device);
  788. check_vk_result(err);
  789. // Destroy old Framebuffer
  790. for (uint32_t i = 0; i < wd->BackBufferCount; i++)
  791. {
  792. if (wd->BackBufferView[i])
  793. vkDestroyImageView(device, wd->BackBufferView[i], allocator);
  794. if (wd->Framebuffer[i])
  795. vkDestroyFramebuffer(device, wd->Framebuffer[i], allocator);
  796. }
  797. wd->BackBufferCount = 0;
  798. if (wd->RenderPass)
  799. vkDestroyRenderPass(device, wd->RenderPass, allocator);
  800. // Create Swapchain
  801. {
  802. VkSwapchainCreateInfoKHR info = {};
  803. info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
  804. info.surface = wd->Surface;
  805. info.imageFormat = wd->SurfaceFormat.format;
  806. info.imageColorSpace = wd->SurfaceFormat.colorSpace;
  807. info.imageArrayLayers = 1;
  808. info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
  809. info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; // Assume that graphics family == present family
  810. info.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
  811. info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
  812. info.presentMode = wd->PresentMode;
  813. info.clipped = VK_TRUE;
  814. info.oldSwapchain = old_swapchain;
  815. VkSurfaceCapabilitiesKHR cap;
  816. err = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physical_device, wd->Surface, &cap);
  817. check_vk_result(err);
  818. if (cap.maxImageCount > 0)
  819. info.minImageCount = (cap.minImageCount + 2 < cap.maxImageCount) ? (cap.minImageCount + 2) : cap.maxImageCount;
  820. else
  821. info.minImageCount = cap.minImageCount + 2;
  822. if (cap.currentExtent.width == 0xffffffff)
  823. {
  824. info.imageExtent.width = wd->Width = w;
  825. info.imageExtent.height = wd->Height = h;
  826. }
  827. else
  828. {
  829. info.imageExtent.width = wd->Width = cap.currentExtent.width;
  830. info.imageExtent.height = wd->Height = cap.currentExtent.height;
  831. }
  832. err = vkCreateSwapchainKHR(device, &info, allocator, &wd->Swapchain);
  833. check_vk_result(err);
  834. err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->BackBufferCount, NULL);
  835. check_vk_result(err);
  836. err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->BackBufferCount, wd->BackBuffer);
  837. check_vk_result(err);
  838. }
  839. if (old_swapchain)
  840. vkDestroySwapchainKHR(device, old_swapchain, allocator);
  841. // Create the Render Pass
  842. {
  843. VkAttachmentDescription attachment = {};
  844. attachment.format = wd->SurfaceFormat.format;
  845. attachment.samples = VK_SAMPLE_COUNT_1_BIT;
  846. attachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
  847. attachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
  848. attachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
  849. attachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
  850. attachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  851. attachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
  852. VkAttachmentReference color_attachment = {};
  853. color_attachment.attachment = 0;
  854. color_attachment.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  855. VkSubpassDescription subpass = {};
  856. subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
  857. subpass.colorAttachmentCount = 1;
  858. subpass.pColorAttachments = &color_attachment;
  859. VkRenderPassCreateInfo info = {};
  860. info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
  861. info.attachmentCount = 1;
  862. info.pAttachments = &attachment;
  863. info.subpassCount = 1;
  864. info.pSubpasses = &subpass;
  865. err = vkCreateRenderPass(device, &info, allocator, &wd->RenderPass);
  866. check_vk_result(err);
  867. }
  868. // Create The Image Views
  869. {
  870. VkImageViewCreateInfo info = {};
  871. info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
  872. info.viewType = VK_IMAGE_VIEW_TYPE_2D;
  873. info.format = wd->SurfaceFormat.format;
  874. info.components.r = VK_COMPONENT_SWIZZLE_R;
  875. info.components.g = VK_COMPONENT_SWIZZLE_G;
  876. info.components.b = VK_COMPONENT_SWIZZLE_B;
  877. info.components.a = VK_COMPONENT_SWIZZLE_A;
  878. VkImageSubresourceRange image_range = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
  879. info.subresourceRange = image_range;
  880. for (uint32_t i = 0; i < wd->BackBufferCount; i++)
  881. {
  882. info.image = wd->BackBuffer[i];
  883. err = vkCreateImageView(device, &info, allocator, &wd->BackBufferView[i]);
  884. check_vk_result(err);
  885. }
  886. }
  887. // Create Framebuffer
  888. {
  889. VkImageView attachment[1];
  890. VkFramebufferCreateInfo info = {};
  891. info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
  892. info.renderPass = wd->RenderPass;
  893. info.attachmentCount = 1;
  894. info.pAttachments = attachment;
  895. info.width = wd->Width;
  896. info.height = wd->Height;
  897. info.layers = 1;
  898. for (uint32_t i = 0; i < wd->BackBufferCount; i++)
  899. {
  900. attachment[0] = wd->BackBufferView[i];
  901. err = vkCreateFramebuffer(device, &info, allocator, &wd->Framebuffer[i]);
  902. check_vk_result(err);
  903. }
  904. }
  905. }
  906. void ImGui_ImplVulkanH_DestroyWindowData(VkInstance instance, VkDevice device, ImGui_ImplVulkan_WindowData* wd, const VkAllocationCallbacks* allocator)
  907. {
  908. for (int i = 0; i < IMGUI_VK_QUEUED_FRAMES; i++)
  909. {
  910. ImGui_ImplVulkan_FrameData* fd = &wd->Frames[i];
  911. vkDestroyFence(device, fd->Fence, allocator);
  912. vkFreeCommandBuffers(device, fd->CommandPool, 1, &fd->CommandBuffer);
  913. vkDestroyCommandPool(device, fd->CommandPool, allocator);
  914. vkDestroySemaphore(device, fd->PresentCompleteSemaphore, allocator);
  915. vkDestroySemaphore(device, fd->RenderCompleteSemaphore, allocator);
  916. }
  917. for (uint32_t i = 0; i < wd->BackBufferCount; i++)
  918. {
  919. vkDestroyImageView(device, wd->BackBufferView[i], allocator);
  920. vkDestroyFramebuffer(device, wd->Framebuffer[i], allocator);
  921. }
  922. vkDestroyRenderPass(device, wd->RenderPass, allocator);
  923. vkDestroySwapchainKHR(device, wd->Swapchain, allocator);
  924. vkDestroySurfaceKHR(instance, wd->Surface, allocator);
  925. *wd = ImGui_ImplVulkan_WindowData();
  926. }
  927. //--------------------------------------------------------------------------------------------------------
  928. // Platform Windows (OPTIONAL/EXPERIMENTAL)
  929. //--------------------------------------------------------------------------------------------------------
  930. #include "imgui_internal.h" // ImGuiViewport
  931. struct ImGuiPlatformDataVulkan
  932. {
  933. ImGui_ImplVulkan_WindowData Wd;
  934. ImGuiPlatformDataVulkan() { }
  935. ~ImGuiPlatformDataVulkan() { }
  936. };
  937. static void ImGui_ImplVulkan_CreateViewport(ImGuiViewport* viewport)
  938. {
  939. ImGuiPlatformDataVulkan* data = IM_NEW(ImGuiPlatformDataVulkan)();
  940. viewport->RendererUserData = data;
  941. // FIXME-PLATFORM
  942. //HWND hwnd = (HWND)viewport->PlatformHandle;
  943. //IM_ASSERT(hwnd != 0);
  944. //...
  945. }
  946. static void ImGui_ImplVulkan_DestroyViewport(ImGuiViewport* viewport)
  947. {
  948. if (ImGuiPlatformDataVulkan* data = (ImGuiPlatformDataVulkan*)viewport->RendererUserData)
  949. {
  950. //...
  951. IM_DELETE(data);
  952. }
  953. viewport->RendererUserData = NULL;
  954. }
  955. static void ImGui_ImplVulkan_ResizeViewport(ImGuiViewport* viewport, int w, int h)
  956. {
  957. ImGuiPlatformDataVulkan* data = (ImGuiPlatformDataVulkan*)viewport->RendererUserData;
  958. //...
  959. (void)data;
  960. (void)w;
  961. (void)h;
  962. }
  963. static void ImGui_ImplVulkan_RenderViewport(ImGuiViewport* viewport)
  964. {
  965. ImGuiPlatformDataVulkan* data = (ImGuiPlatformDataVulkan*)viewport->RendererUserData;
  966. ImVec4 clear_color = ImGui::GetStyle().Colors[ImGuiCol_WindowBg]; // FIXME-PLATFORM
  967. clear_color.w = 1.0f;
  968. (void)data;
  969. // clear
  970. // call ImGui_ImplVulkan_RenderDrawData(&viewport->DrawData)
  971. }
  972. static void ImGui_ImplVulkan_SwapBuffers(ImGuiViewport* viewport)
  973. {
  974. ImGuiPlatformDataVulkan* data = (ImGuiPlatformDataVulkan*)viewport->RendererUserData;
  975. (void)data;
  976. //...
  977. }
  978. void ImGui_ImplVulkan_InitPlatformInterface()
  979. {
  980. ImGuiIO& io = ImGui::GetIO();
  981. io.RendererInterface.CreateViewport = ImGui_ImplVulkan_CreateViewport;
  982. io.RendererInterface.DestroyViewport = ImGui_ImplVulkan_DestroyViewport;
  983. io.RendererInterface.ResizeViewport = ImGui_ImplVulkan_ResizeViewport;
  984. io.RendererInterface.RenderViewport = ImGui_ImplVulkan_RenderViewport;
  985. io.RendererInterface.SwapBuffers = ImGui_ImplVulkan_SwapBuffers;
  986. }
  987. void ImGui_ImplVulkan_ShutdownPlatformInterface()
  988. {
  989. ImGuiIO& io = ImGui::GetIO();
  990. memset(&io.RendererInterface, 0, sizeof(io.RendererInterface));
  991. }