imgui_impl_glfw_vulkan.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. // ImGui GLFW binding with Vulkan + shaders
  2. // Missing features:
  3. // [ ] User texture binding. Changes of ImTextureID aren't supported by this binding! See https://github.com/ocornut/imgui/pull/914
  4. // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
  5. // If you use this binding you'll need to call 5 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXX_CreateFontsTexture(), ImGui_ImplXXXX_NewFrame(), ImGui_ImplXXXX_Render() and ImGui_ImplXXXX_Shutdown().
  6. // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
  7. // https://github.com/ocornut/imgui
  8. // CHANGELOG
  9. // (minor and older changes stripped away, please see git history for details)
  10. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  11. // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
  12. // 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
  13. // 2018-01-18: Inputs: Added mapping for ImGuiKey_Insert.
  14. // 2017-08-25: Inputs: MousePos set to -FLT_MAX,-FLT_MAX when mouse is unavailable/missing (instead of -1,-1).
  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-10-15: Misc: Added a void* user_data parameter to Clipboard function handlers.
  19. // 2016-08-27: Vulkan: Fix Vulkan example for use when a depth buffer is active.
  20. #include "imgui.h"
  21. #include "imgui_impl_glfw_vulkan.h"
  22. // GLFW
  23. #define GLFW_INCLUDE_NONE
  24. #define GLFW_INCLUDE_VULKAN
  25. #include <GLFW/glfw3.h>
  26. #ifdef _WIN32
  27. #undef APIENTRY
  28. #define GLFW_EXPOSE_NATIVE_WIN32
  29. #define GLFW_EXPOSE_NATIVE_WGL
  30. #include <GLFW/glfw3native.h>
  31. #endif
  32. // GLFW Data
  33. static GLFWwindow* g_Window = NULL;
  34. static double g_Time = 0.0f;
  35. static bool g_MouseJustPressed[3] = { false, false, false };
  36. // Vulkan Data
  37. static VkAllocationCallbacks* g_Allocator = NULL;
  38. static VkPhysicalDevice g_Gpu = VK_NULL_HANDLE;
  39. static VkDevice g_Device = VK_NULL_HANDLE;
  40. static VkRenderPass g_RenderPass = VK_NULL_HANDLE;
  41. static VkPipelineCache g_PipelineCache = VK_NULL_HANDLE;
  42. static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE;
  43. static void (*g_CheckVkResult)(VkResult err) = NULL;
  44. static VkCommandBuffer g_CommandBuffer = VK_NULL_HANDLE;
  45. static VkDeviceSize g_BufferMemoryAlignment = 256;
  46. static VkPipelineCreateFlags g_PipelineCreateFlags = 0;
  47. static int g_FrameIndex = 0;
  48. static VkDescriptorSetLayout g_DescriptorSetLayout = VK_NULL_HANDLE;
  49. static VkPipelineLayout g_PipelineLayout = VK_NULL_HANDLE;
  50. static VkDescriptorSet g_DescriptorSet = VK_NULL_HANDLE;
  51. static VkPipeline g_Pipeline = VK_NULL_HANDLE;
  52. static VkSampler g_FontSampler = VK_NULL_HANDLE;
  53. static VkDeviceMemory g_FontMemory = VK_NULL_HANDLE;
  54. static VkImage g_FontImage = VK_NULL_HANDLE;
  55. static VkImageView g_FontView = VK_NULL_HANDLE;
  56. static VkDeviceMemory g_VertexBufferMemory[IMGUI_VK_QUEUED_FRAMES] = {};
  57. static VkDeviceMemory g_IndexBufferMemory[IMGUI_VK_QUEUED_FRAMES] = {};
  58. static VkDeviceSize g_VertexBufferSize[IMGUI_VK_QUEUED_FRAMES] = {};
  59. static VkDeviceSize g_IndexBufferSize[IMGUI_VK_QUEUED_FRAMES] = {};
  60. static VkBuffer g_VertexBuffer[IMGUI_VK_QUEUED_FRAMES] = {};
  61. static VkBuffer g_IndexBuffer[IMGUI_VK_QUEUED_FRAMES] = {};
  62. static VkDeviceMemory g_UploadBufferMemory = VK_NULL_HANDLE;
  63. static VkBuffer g_UploadBuffer = VK_NULL_HANDLE;
  64. static uint32_t __glsl_shader_vert_spv[] =
  65. {
  66. 0x07230203,0x00010000,0x00080001,0x0000002e,0x00000000,0x00020011,0x00000001,0x0006000b,
  67. 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001,
  68. 0x000a000f,0x00000000,0x00000004,0x6e69616d,0x00000000,0x0000000b,0x0000000f,0x00000015,
  69. 0x0000001b,0x0000001c,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d,
  70. 0x00000000,0x00030005,0x00000009,0x00000000,0x00050006,0x00000009,0x00000000,0x6f6c6f43,
  71. 0x00000072,0x00040006,0x00000009,0x00000001,0x00005655,0x00030005,0x0000000b,0x0074754f,
  72. 0x00040005,0x0000000f,0x6c6f4361,0x0000726f,0x00030005,0x00000015,0x00565561,0x00060005,
  73. 0x00000019,0x505f6c67,0x65567265,0x78657472,0x00000000,0x00060006,0x00000019,0x00000000,
  74. 0x505f6c67,0x7469736f,0x006e6f69,0x00030005,0x0000001b,0x00000000,0x00040005,0x0000001c,
  75. 0x736f5061,0x00000000,0x00060005,0x0000001e,0x73755075,0x6e6f4368,0x6e617473,0x00000074,
  76. 0x00050006,0x0000001e,0x00000000,0x61635375,0x0000656c,0x00060006,0x0000001e,0x00000001,
  77. 0x61725475,0x616c736e,0x00006574,0x00030005,0x00000020,0x00006370,0x00040047,0x0000000b,
  78. 0x0000001e,0x00000000,0x00040047,0x0000000f,0x0000001e,0x00000002,0x00040047,0x00000015,
  79. 0x0000001e,0x00000001,0x00050048,0x00000019,0x00000000,0x0000000b,0x00000000,0x00030047,
  80. 0x00000019,0x00000002,0x00040047,0x0000001c,0x0000001e,0x00000000,0x00050048,0x0000001e,
  81. 0x00000000,0x00000023,0x00000000,0x00050048,0x0000001e,0x00000001,0x00000023,0x00000008,
  82. 0x00030047,0x0000001e,0x00000002,0x00020013,0x00000002,0x00030021,0x00000003,0x00000002,
  83. 0x00030016,0x00000006,0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,0x00040017,
  84. 0x00000008,0x00000006,0x00000002,0x0004001e,0x00000009,0x00000007,0x00000008,0x00040020,
  85. 0x0000000a,0x00000003,0x00000009,0x0004003b,0x0000000a,0x0000000b,0x00000003,0x00040015,
  86. 0x0000000c,0x00000020,0x00000001,0x0004002b,0x0000000c,0x0000000d,0x00000000,0x00040020,
  87. 0x0000000e,0x00000001,0x00000007,0x0004003b,0x0000000e,0x0000000f,0x00000001,0x00040020,
  88. 0x00000011,0x00000003,0x00000007,0x0004002b,0x0000000c,0x00000013,0x00000001,0x00040020,
  89. 0x00000014,0x00000001,0x00000008,0x0004003b,0x00000014,0x00000015,0x00000001,0x00040020,
  90. 0x00000017,0x00000003,0x00000008,0x0003001e,0x00000019,0x00000007,0x00040020,0x0000001a,
  91. 0x00000003,0x00000019,0x0004003b,0x0000001a,0x0000001b,0x00000003,0x0004003b,0x00000014,
  92. 0x0000001c,0x00000001,0x0004001e,0x0000001e,0x00000008,0x00000008,0x00040020,0x0000001f,
  93. 0x00000009,0x0000001e,0x0004003b,0x0000001f,0x00000020,0x00000009,0x00040020,0x00000021,
  94. 0x00000009,0x00000008,0x0004002b,0x00000006,0x00000028,0x00000000,0x0004002b,0x00000006,
  95. 0x00000029,0x3f800000,0x00050036,0x00000002,0x00000004,0x00000000,0x00000003,0x000200f8,
  96. 0x00000005,0x0004003d,0x00000007,0x00000010,0x0000000f,0x00050041,0x00000011,0x00000012,
  97. 0x0000000b,0x0000000d,0x0003003e,0x00000012,0x00000010,0x0004003d,0x00000008,0x00000016,
  98. 0x00000015,0x00050041,0x00000017,0x00000018,0x0000000b,0x00000013,0x0003003e,0x00000018,
  99. 0x00000016,0x0004003d,0x00000008,0x0000001d,0x0000001c,0x00050041,0x00000021,0x00000022,
  100. 0x00000020,0x0000000d,0x0004003d,0x00000008,0x00000023,0x00000022,0x00050085,0x00000008,
  101. 0x00000024,0x0000001d,0x00000023,0x00050041,0x00000021,0x00000025,0x00000020,0x00000013,
  102. 0x0004003d,0x00000008,0x00000026,0x00000025,0x00050081,0x00000008,0x00000027,0x00000024,
  103. 0x00000026,0x00050051,0x00000006,0x0000002a,0x00000027,0x00000000,0x00050051,0x00000006,
  104. 0x0000002b,0x00000027,0x00000001,0x00070050,0x00000007,0x0000002c,0x0000002a,0x0000002b,
  105. 0x00000028,0x00000029,0x00050041,0x00000011,0x0000002d,0x0000001b,0x0000000d,0x0003003e,
  106. 0x0000002d,0x0000002c,0x000100fd,0x00010038
  107. };
  108. static uint32_t __glsl_shader_frag_spv[] =
  109. {
  110. 0x07230203,0x00010000,0x00080001,0x0000001e,0x00000000,0x00020011,0x00000001,0x0006000b,
  111. 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001,
  112. 0x0007000f,0x00000004,0x00000004,0x6e69616d,0x00000000,0x00000009,0x0000000d,0x00030010,
  113. 0x00000004,0x00000007,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d,
  114. 0x00000000,0x00040005,0x00000009,0x6c6f4366,0x0000726f,0x00030005,0x0000000b,0x00000000,
  115. 0x00050006,0x0000000b,0x00000000,0x6f6c6f43,0x00000072,0x00040006,0x0000000b,0x00000001,
  116. 0x00005655,0x00030005,0x0000000d,0x00006e49,0x00050005,0x00000016,0x78655473,0x65727574,
  117. 0x00000000,0x00040047,0x00000009,0x0000001e,0x00000000,0x00040047,0x0000000d,0x0000001e,
  118. 0x00000000,0x00040047,0x00000016,0x00000022,0x00000000,0x00040047,0x00000016,0x00000021,
  119. 0x00000000,0x00020013,0x00000002,0x00030021,0x00000003,0x00000002,0x00030016,0x00000006,
  120. 0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,0x00040020,0x00000008,0x00000003,
  121. 0x00000007,0x0004003b,0x00000008,0x00000009,0x00000003,0x00040017,0x0000000a,0x00000006,
  122. 0x00000002,0x0004001e,0x0000000b,0x00000007,0x0000000a,0x00040020,0x0000000c,0x00000001,
  123. 0x0000000b,0x0004003b,0x0000000c,0x0000000d,0x00000001,0x00040015,0x0000000e,0x00000020,
  124. 0x00000001,0x0004002b,0x0000000e,0x0000000f,0x00000000,0x00040020,0x00000010,0x00000001,
  125. 0x00000007,0x00090019,0x00000013,0x00000006,0x00000001,0x00000000,0x00000000,0x00000000,
  126. 0x00000001,0x00000000,0x0003001b,0x00000014,0x00000013,0x00040020,0x00000015,0x00000000,
  127. 0x00000014,0x0004003b,0x00000015,0x00000016,0x00000000,0x0004002b,0x0000000e,0x00000018,
  128. 0x00000001,0x00040020,0x00000019,0x00000001,0x0000000a,0x00050036,0x00000002,0x00000004,
  129. 0x00000000,0x00000003,0x000200f8,0x00000005,0x00050041,0x00000010,0x00000011,0x0000000d,
  130. 0x0000000f,0x0004003d,0x00000007,0x00000012,0x00000011,0x0004003d,0x00000014,0x00000017,
  131. 0x00000016,0x00050041,0x00000019,0x0000001a,0x0000000d,0x00000018,0x0004003d,0x0000000a,
  132. 0x0000001b,0x0000001a,0x00050057,0x00000007,0x0000001c,0x00000017,0x0000001b,0x00050085,
  133. 0x00000007,0x0000001d,0x00000012,0x0000001c,0x0003003e,0x00000009,0x0000001d,0x000100fd,
  134. 0x00010038
  135. };
  136. static uint32_t ImGui_ImplGlfwVulkan_MemoryType(VkMemoryPropertyFlags properties, uint32_t type_bits)
  137. {
  138. VkPhysicalDeviceMemoryProperties prop;
  139. vkGetPhysicalDeviceMemoryProperties(g_Gpu, &prop);
  140. for (uint32_t i = 0; i < prop.memoryTypeCount; i++)
  141. if ((prop.memoryTypes[i].propertyFlags & properties) == properties && type_bits & (1<<i))
  142. return i;
  143. return 0xffffffff; // Unable to find memoryType
  144. }
  145. static void ImGui_ImplGlfwVulkan_VkResult(VkResult err)
  146. {
  147. if (g_CheckVkResult)
  148. g_CheckVkResult(err);
  149. }
  150. // This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
  151. void ImGui_ImplGlfwVulkan_RenderDrawLists(ImDrawData* draw_data)
  152. {
  153. VkResult err;
  154. ImGuiIO& io = ImGui::GetIO();
  155. // Create the Vertex Buffer:
  156. size_t vertex_size = draw_data->TotalVtxCount * sizeof(ImDrawVert);
  157. if (!g_VertexBuffer[g_FrameIndex] || g_VertexBufferSize[g_FrameIndex] < vertex_size)
  158. {
  159. if (g_VertexBuffer[g_FrameIndex])
  160. vkDestroyBuffer(g_Device, g_VertexBuffer[g_FrameIndex], g_Allocator);
  161. if (g_VertexBufferMemory[g_FrameIndex])
  162. vkFreeMemory(g_Device, g_VertexBufferMemory[g_FrameIndex], g_Allocator);
  163. VkDeviceSize vertex_buffer_size = ((vertex_size-1) / g_BufferMemoryAlignment+1) * g_BufferMemoryAlignment;
  164. VkBufferCreateInfo buffer_info = {};
  165. buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  166. buffer_info.size = vertex_buffer_size;
  167. buffer_info.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
  168. buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  169. err = vkCreateBuffer(g_Device, &buffer_info, g_Allocator, &g_VertexBuffer[g_FrameIndex]);
  170. ImGui_ImplGlfwVulkan_VkResult(err);
  171. VkMemoryRequirements req;
  172. vkGetBufferMemoryRequirements(g_Device, g_VertexBuffer[g_FrameIndex], &req);
  173. g_BufferMemoryAlignment = (g_BufferMemoryAlignment > req.alignment) ? g_BufferMemoryAlignment : req.alignment;
  174. VkMemoryAllocateInfo alloc_info = {};
  175. alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  176. alloc_info.allocationSize = req.size;
  177. alloc_info.memoryTypeIndex = ImGui_ImplGlfwVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
  178. err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &g_VertexBufferMemory[g_FrameIndex]);
  179. ImGui_ImplGlfwVulkan_VkResult(err);
  180. err = vkBindBufferMemory(g_Device, g_VertexBuffer[g_FrameIndex], g_VertexBufferMemory[g_FrameIndex], 0);
  181. ImGui_ImplGlfwVulkan_VkResult(err);
  182. g_VertexBufferSize[g_FrameIndex] = vertex_buffer_size;
  183. }
  184. // Create the Index Buffer:
  185. size_t index_size = draw_data->TotalIdxCount * sizeof(ImDrawIdx);
  186. if (!g_IndexBuffer[g_FrameIndex] || g_IndexBufferSize[g_FrameIndex] < index_size)
  187. {
  188. if (g_IndexBuffer[g_FrameIndex])
  189. vkDestroyBuffer(g_Device, g_IndexBuffer[g_FrameIndex], g_Allocator);
  190. if (g_IndexBufferMemory[g_FrameIndex])
  191. vkFreeMemory(g_Device, g_IndexBufferMemory[g_FrameIndex], g_Allocator);
  192. VkDeviceSize index_buffer_size = ((index_size-1) / g_BufferMemoryAlignment+1) * g_BufferMemoryAlignment;
  193. VkBufferCreateInfo buffer_info = {};
  194. buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  195. buffer_info.size = index_buffer_size;
  196. buffer_info.usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
  197. buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  198. err = vkCreateBuffer(g_Device, &buffer_info, g_Allocator, &g_IndexBuffer[g_FrameIndex]);
  199. ImGui_ImplGlfwVulkan_VkResult(err);
  200. VkMemoryRequirements req;
  201. vkGetBufferMemoryRequirements(g_Device, g_IndexBuffer[g_FrameIndex], &req);
  202. g_BufferMemoryAlignment = (g_BufferMemoryAlignment > req.alignment) ? g_BufferMemoryAlignment : req.alignment;
  203. VkMemoryAllocateInfo alloc_info = {};
  204. alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  205. alloc_info.allocationSize = req.size;
  206. alloc_info.memoryTypeIndex = ImGui_ImplGlfwVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
  207. err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &g_IndexBufferMemory[g_FrameIndex]);
  208. ImGui_ImplGlfwVulkan_VkResult(err);
  209. err = vkBindBufferMemory(g_Device, g_IndexBuffer[g_FrameIndex], g_IndexBufferMemory[g_FrameIndex], 0);
  210. ImGui_ImplGlfwVulkan_VkResult(err);
  211. g_IndexBufferSize[g_FrameIndex] = index_buffer_size;
  212. }
  213. // Upload Vertex and index Data:
  214. {
  215. ImDrawVert* vtx_dst;
  216. ImDrawIdx* idx_dst;
  217. err = vkMapMemory(g_Device, g_VertexBufferMemory[g_FrameIndex], 0, vertex_size, 0, (void**)(&vtx_dst));
  218. ImGui_ImplGlfwVulkan_VkResult(err);
  219. err = vkMapMemory(g_Device, g_IndexBufferMemory[g_FrameIndex], 0, index_size, 0, (void**)(&idx_dst));
  220. ImGui_ImplGlfwVulkan_VkResult(err);
  221. for (int n = 0; n < draw_data->CmdListsCount; n++)
  222. {
  223. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  224. memcpy(vtx_dst, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert));
  225. memcpy(idx_dst, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx));
  226. vtx_dst += cmd_list->VtxBuffer.Size;
  227. idx_dst += cmd_list->IdxBuffer.Size;
  228. }
  229. VkMappedMemoryRange range[2] = {};
  230. range[0].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
  231. range[0].memory = g_VertexBufferMemory[g_FrameIndex];
  232. range[0].size = VK_WHOLE_SIZE;
  233. range[1].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
  234. range[1].memory = g_IndexBufferMemory[g_FrameIndex];
  235. range[1].size = VK_WHOLE_SIZE;
  236. err = vkFlushMappedMemoryRanges(g_Device, 2, range);
  237. ImGui_ImplGlfwVulkan_VkResult(err);
  238. vkUnmapMemory(g_Device, g_VertexBufferMemory[g_FrameIndex]);
  239. vkUnmapMemory(g_Device, g_IndexBufferMemory[g_FrameIndex]);
  240. }
  241. // Bind pipeline and descriptor sets:
  242. {
  243. vkCmdBindPipeline(g_CommandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, g_Pipeline);
  244. VkDescriptorSet desc_set[1] = {g_DescriptorSet};
  245. vkCmdBindDescriptorSets(g_CommandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, g_PipelineLayout, 0, 1, desc_set, 0, NULL);
  246. }
  247. // Bind Vertex And Index Buffer:
  248. {
  249. VkBuffer vertex_buffers[1] = {g_VertexBuffer[g_FrameIndex]};
  250. VkDeviceSize vertex_offset[1] = {0};
  251. vkCmdBindVertexBuffers(g_CommandBuffer, 0, 1, vertex_buffers, vertex_offset);
  252. vkCmdBindIndexBuffer(g_CommandBuffer, g_IndexBuffer[g_FrameIndex], 0, VK_INDEX_TYPE_UINT16);
  253. }
  254. // Setup viewport:
  255. {
  256. VkViewport viewport;
  257. viewport.x = 0;
  258. viewport.y = 0;
  259. viewport.width = ImGui::GetIO().DisplaySize.x;
  260. viewport.height = ImGui::GetIO().DisplaySize.y;
  261. viewport.minDepth = 0.0f;
  262. viewport.maxDepth = 1.0f;
  263. vkCmdSetViewport(g_CommandBuffer, 0, 1, &viewport);
  264. }
  265. // Setup scale and translation:
  266. {
  267. float scale[2];
  268. scale[0] = 2.0f/io.DisplaySize.x;
  269. scale[1] = 2.0f/io.DisplaySize.y;
  270. float translate[2];
  271. translate[0] = -1.0f;
  272. translate[1] = -1.0f;
  273. vkCmdPushConstants(g_CommandBuffer, g_PipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, sizeof(float) * 0, sizeof(float) * 2, scale);
  274. vkCmdPushConstants(g_CommandBuffer, g_PipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, sizeof(float) * 2, sizeof(float) * 2, translate);
  275. }
  276. // Render the command lists:
  277. int vtx_offset = 0;
  278. int idx_offset = 0;
  279. for (int n = 0; n < draw_data->CmdListsCount; n++)
  280. {
  281. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  282. for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
  283. {
  284. const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
  285. if (pcmd->UserCallback)
  286. {
  287. pcmd->UserCallback(cmd_list, pcmd);
  288. }
  289. else
  290. {
  291. VkRect2D scissor;
  292. scissor.offset.x = (int32_t)(pcmd->ClipRect.x) > 0 ? (int32_t)(pcmd->ClipRect.x) : 0;
  293. scissor.offset.y = (int32_t)(pcmd->ClipRect.y) > 0 ? (int32_t)(pcmd->ClipRect.y) : 0;
  294. scissor.extent.width = (uint32_t)(pcmd->ClipRect.z - pcmd->ClipRect.x);
  295. scissor.extent.height = (uint32_t)(pcmd->ClipRect.w - pcmd->ClipRect.y + 1); // FIXME: Why +1 here?
  296. vkCmdSetScissor(g_CommandBuffer, 0, 1, &scissor);
  297. vkCmdDrawIndexed(g_CommandBuffer, pcmd->ElemCount, 1, idx_offset, vtx_offset, 0);
  298. }
  299. idx_offset += pcmd->ElemCount;
  300. }
  301. vtx_offset += cmd_list->VtxBuffer.Size;
  302. }
  303. }
  304. static const char* ImGui_ImplGlfwVulkan_GetClipboardText(void* user_data)
  305. {
  306. return glfwGetClipboardString((GLFWwindow*)user_data);
  307. }
  308. static void ImGui_ImplGlfwVulkan_SetClipboardText(void* user_data, const char* text)
  309. {
  310. glfwSetClipboardString((GLFWwindow*)user_data, text);
  311. }
  312. void ImGui_ImplGlfwVulkan_MouseButtonCallback(GLFWwindow*, int button, int action, int /*mods*/)
  313. {
  314. if (action == GLFW_PRESS && button >= 0 && button < 3)
  315. g_MouseJustPressed[button] = true;
  316. }
  317. void ImGui_ImplGlfwVulkan_ScrollCallback(GLFWwindow*, double xoffset, double yoffset)
  318. {
  319. ImGuiIO& io = ImGui::GetIO();
  320. io.MouseWheelH += (float)xoffset;
  321. io.MouseWheel += (float)yoffset;
  322. }
  323. void ImGui_ImplGlfwVulkan_KeyCallback(GLFWwindow*, int key, int, int action, int mods)
  324. {
  325. ImGuiIO& io = ImGui::GetIO();
  326. if (action == GLFW_PRESS)
  327. io.KeysDown[key] = true;
  328. if (action == GLFW_RELEASE)
  329. io.KeysDown[key] = false;
  330. (void)mods; // Modifiers are not reliable across systems
  331. io.KeyCtrl = io.KeysDown[GLFW_KEY_LEFT_CONTROL] || io.KeysDown[GLFW_KEY_RIGHT_CONTROL];
  332. io.KeyShift = io.KeysDown[GLFW_KEY_LEFT_SHIFT] || io.KeysDown[GLFW_KEY_RIGHT_SHIFT];
  333. io.KeyAlt = io.KeysDown[GLFW_KEY_LEFT_ALT] || io.KeysDown[GLFW_KEY_RIGHT_ALT];
  334. io.KeySuper = io.KeysDown[GLFW_KEY_LEFT_SUPER] || io.KeysDown[GLFW_KEY_RIGHT_SUPER];
  335. }
  336. void ImGui_ImplGlfwVulkan_CharCallback(GLFWwindow*, unsigned int c)
  337. {
  338. ImGuiIO& io = ImGui::GetIO();
  339. if (c > 0 && c < 0x10000)
  340. io.AddInputCharacter((unsigned short)c);
  341. }
  342. bool ImGui_ImplGlfwVulkan_CreateFontsTexture(VkCommandBuffer command_buffer)
  343. {
  344. ImGuiIO& io = ImGui::GetIO();
  345. unsigned char* pixels;
  346. int width, height;
  347. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
  348. size_t upload_size = width*height*4*sizeof(char);
  349. VkResult err;
  350. // Create the Image:
  351. {
  352. VkImageCreateInfo info = {};
  353. info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
  354. info.imageType = VK_IMAGE_TYPE_2D;
  355. info.format = VK_FORMAT_R8G8B8A8_UNORM;
  356. info.extent.width = width;
  357. info.extent.height = height;
  358. info.extent.depth = 1;
  359. info.mipLevels = 1;
  360. info.arrayLayers = 1;
  361. info.samples = VK_SAMPLE_COUNT_1_BIT;
  362. info.tiling = VK_IMAGE_TILING_OPTIMAL;
  363. info.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
  364. info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  365. info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  366. err = vkCreateImage(g_Device, &info, g_Allocator, &g_FontImage);
  367. ImGui_ImplGlfwVulkan_VkResult(err);
  368. VkMemoryRequirements req;
  369. vkGetImageMemoryRequirements(g_Device, g_FontImage, &req);
  370. VkMemoryAllocateInfo alloc_info = {};
  371. alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  372. alloc_info.allocationSize = req.size;
  373. alloc_info.memoryTypeIndex = ImGui_ImplGlfwVulkan_MemoryType(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, req.memoryTypeBits);
  374. err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &g_FontMemory);
  375. ImGui_ImplGlfwVulkan_VkResult(err);
  376. err = vkBindImageMemory(g_Device, g_FontImage, g_FontMemory, 0);
  377. ImGui_ImplGlfwVulkan_VkResult(err);
  378. }
  379. // Create the Image View:
  380. {
  381. VkImageViewCreateInfo info = {};
  382. info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
  383. info.image = g_FontImage;
  384. info.viewType = VK_IMAGE_VIEW_TYPE_2D;
  385. info.format = VK_FORMAT_R8G8B8A8_UNORM;
  386. info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  387. info.subresourceRange.levelCount = 1;
  388. info.subresourceRange.layerCount = 1;
  389. err = vkCreateImageView(g_Device, &info, g_Allocator, &g_FontView);
  390. ImGui_ImplGlfwVulkan_VkResult(err);
  391. }
  392. // Update the Descriptor Set:
  393. {
  394. VkDescriptorImageInfo desc_image[1] = {};
  395. desc_image[0].sampler = g_FontSampler;
  396. desc_image[0].imageView = g_FontView;
  397. desc_image[0].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  398. VkWriteDescriptorSet write_desc[1] = {};
  399. write_desc[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  400. write_desc[0].dstSet = g_DescriptorSet;
  401. write_desc[0].descriptorCount = 1;
  402. write_desc[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
  403. write_desc[0].pImageInfo = desc_image;
  404. vkUpdateDescriptorSets(g_Device, 1, write_desc, 0, NULL);
  405. }
  406. // Create the Upload Buffer:
  407. {
  408. VkBufferCreateInfo buffer_info = {};
  409. buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  410. buffer_info.size = upload_size;
  411. buffer_info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
  412. buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  413. err = vkCreateBuffer(g_Device, &buffer_info, g_Allocator, &g_UploadBuffer);
  414. ImGui_ImplGlfwVulkan_VkResult(err);
  415. VkMemoryRequirements req;
  416. vkGetBufferMemoryRequirements(g_Device, g_UploadBuffer, &req);
  417. g_BufferMemoryAlignment = (g_BufferMemoryAlignment > req.alignment) ? g_BufferMemoryAlignment : req.alignment;
  418. VkMemoryAllocateInfo alloc_info = {};
  419. alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  420. alloc_info.allocationSize = req.size;
  421. alloc_info.memoryTypeIndex = ImGui_ImplGlfwVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
  422. err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &g_UploadBufferMemory);
  423. ImGui_ImplGlfwVulkan_VkResult(err);
  424. err = vkBindBufferMemory(g_Device, g_UploadBuffer, g_UploadBufferMemory, 0);
  425. ImGui_ImplGlfwVulkan_VkResult(err);
  426. }
  427. // Upload to Buffer:
  428. {
  429. char* map = NULL;
  430. err = vkMapMemory(g_Device, g_UploadBufferMemory, 0, upload_size, 0, (void**)(&map));
  431. ImGui_ImplGlfwVulkan_VkResult(err);
  432. memcpy(map, pixels, upload_size);
  433. VkMappedMemoryRange range[1] = {};
  434. range[0].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
  435. range[0].memory = g_UploadBufferMemory;
  436. range[0].size = upload_size;
  437. err = vkFlushMappedMemoryRanges(g_Device, 1, range);
  438. ImGui_ImplGlfwVulkan_VkResult(err);
  439. vkUnmapMemory(g_Device, g_UploadBufferMemory);
  440. }
  441. // Copy to Image:
  442. {
  443. VkImageMemoryBarrier copy_barrier[1] = {};
  444. copy_barrier[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
  445. copy_barrier[0].dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
  446. copy_barrier[0].oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  447. copy_barrier[0].newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
  448. copy_barrier[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  449. copy_barrier[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  450. copy_barrier[0].image = g_FontImage;
  451. copy_barrier[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  452. copy_barrier[0].subresourceRange.levelCount = 1;
  453. copy_barrier[0].subresourceRange.layerCount = 1;
  454. vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 0, NULL, 1, copy_barrier);
  455. VkBufferImageCopy region = {};
  456. region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  457. region.imageSubresource.layerCount = 1;
  458. region.imageExtent.width = width;
  459. region.imageExtent.height = height;
  460. region.imageExtent.depth = 1;
  461. vkCmdCopyBufferToImage(command_buffer, g_UploadBuffer, g_FontImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
  462. VkImageMemoryBarrier use_barrier[1] = {};
  463. use_barrier[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
  464. use_barrier[0].srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
  465. use_barrier[0].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
  466. use_barrier[0].oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
  467. use_barrier[0].newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  468. use_barrier[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  469. use_barrier[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  470. use_barrier[0].image = g_FontImage;
  471. use_barrier[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  472. use_barrier[0].subresourceRange.levelCount = 1;
  473. use_barrier[0].subresourceRange.layerCount = 1;
  474. vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, NULL, 0, NULL, 1, use_barrier);
  475. }
  476. // Store our identifier
  477. io.Fonts->TexID = (void *)(intptr_t)g_FontImage;
  478. return true;
  479. }
  480. bool ImGui_ImplGlfwVulkan_CreateDeviceObjects()
  481. {
  482. VkResult err;
  483. VkShaderModule vert_module;
  484. VkShaderModule frag_module;
  485. // Create The Shader Modules:
  486. {
  487. VkShaderModuleCreateInfo vert_info = {};
  488. vert_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
  489. vert_info.codeSize = sizeof(__glsl_shader_vert_spv);
  490. vert_info.pCode = (uint32_t*)__glsl_shader_vert_spv;
  491. err = vkCreateShaderModule(g_Device, &vert_info, g_Allocator, &vert_module);
  492. ImGui_ImplGlfwVulkan_VkResult(err);
  493. VkShaderModuleCreateInfo frag_info = {};
  494. frag_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
  495. frag_info.codeSize = sizeof(__glsl_shader_frag_spv);
  496. frag_info.pCode = (uint32_t*)__glsl_shader_frag_spv;
  497. err = vkCreateShaderModule(g_Device, &frag_info, g_Allocator, &frag_module);
  498. ImGui_ImplGlfwVulkan_VkResult(err);
  499. }
  500. if (!g_FontSampler)
  501. {
  502. VkSamplerCreateInfo info = {};
  503. info.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
  504. info.magFilter = VK_FILTER_LINEAR;
  505. info.minFilter = VK_FILTER_LINEAR;
  506. info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
  507. info.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
  508. info.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
  509. info.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
  510. info.minLod = -1000;
  511. info.maxLod = 1000;
  512. info.maxAnisotropy = 1.0f;
  513. err = vkCreateSampler(g_Device, &info, g_Allocator, &g_FontSampler);
  514. ImGui_ImplGlfwVulkan_VkResult(err);
  515. }
  516. if (!g_DescriptorSetLayout)
  517. {
  518. VkSampler sampler[1] = {g_FontSampler};
  519. VkDescriptorSetLayoutBinding binding[1] = {};
  520. binding[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
  521. binding[0].descriptorCount = 1;
  522. binding[0].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
  523. binding[0].pImmutableSamplers = sampler;
  524. VkDescriptorSetLayoutCreateInfo info = {};
  525. info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
  526. info.bindingCount = 1;
  527. info.pBindings = binding;
  528. err = vkCreateDescriptorSetLayout(g_Device, &info, g_Allocator, &g_DescriptorSetLayout);
  529. ImGui_ImplGlfwVulkan_VkResult(err);
  530. }
  531. // Create Descriptor Set:
  532. {
  533. VkDescriptorSetAllocateInfo alloc_info = {};
  534. alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
  535. alloc_info.descriptorPool = g_DescriptorPool;
  536. alloc_info.descriptorSetCount = 1;
  537. alloc_info.pSetLayouts = &g_DescriptorSetLayout;
  538. err = vkAllocateDescriptorSets(g_Device, &alloc_info, &g_DescriptorSet);
  539. ImGui_ImplGlfwVulkan_VkResult(err);
  540. }
  541. if (!g_PipelineLayout)
  542. {
  543. VkPushConstantRange push_constants[1] = {};
  544. push_constants[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
  545. push_constants[0].offset = sizeof(float) * 0;
  546. push_constants[0].size = sizeof(float) * 4;
  547. VkDescriptorSetLayout set_layout[1] = {g_DescriptorSetLayout};
  548. VkPipelineLayoutCreateInfo layout_info = {};
  549. layout_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
  550. layout_info.setLayoutCount = 1;
  551. layout_info.pSetLayouts = set_layout;
  552. layout_info.pushConstantRangeCount = 1;
  553. layout_info.pPushConstantRanges = push_constants;
  554. err = vkCreatePipelineLayout(g_Device, &layout_info, g_Allocator, &g_PipelineLayout);
  555. ImGui_ImplGlfwVulkan_VkResult(err);
  556. }
  557. VkPipelineShaderStageCreateInfo stage[2] = {};
  558. stage[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
  559. stage[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
  560. stage[0].module = vert_module;
  561. stage[0].pName = "main";
  562. stage[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
  563. stage[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
  564. stage[1].module = frag_module;
  565. stage[1].pName = "main";
  566. VkVertexInputBindingDescription binding_desc[1] = {};
  567. binding_desc[0].stride = sizeof(ImDrawVert);
  568. binding_desc[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
  569. VkVertexInputAttributeDescription attribute_desc[3] = {};
  570. attribute_desc[0].location = 0;
  571. attribute_desc[0].binding = binding_desc[0].binding;
  572. attribute_desc[0].format = VK_FORMAT_R32G32_SFLOAT;
  573. attribute_desc[0].offset = (size_t)(&((ImDrawVert*)0)->pos);
  574. attribute_desc[1].location = 1;
  575. attribute_desc[1].binding = binding_desc[0].binding;
  576. attribute_desc[1].format = VK_FORMAT_R32G32_SFLOAT;
  577. attribute_desc[1].offset = (size_t)(&((ImDrawVert*)0)->uv);
  578. attribute_desc[2].location = 2;
  579. attribute_desc[2].binding = binding_desc[0].binding;
  580. attribute_desc[2].format = VK_FORMAT_R8G8B8A8_UNORM;
  581. attribute_desc[2].offset = (size_t)(&((ImDrawVert*)0)->col);
  582. VkPipelineVertexInputStateCreateInfo vertex_info = {};
  583. vertex_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
  584. vertex_info.vertexBindingDescriptionCount = 1;
  585. vertex_info.pVertexBindingDescriptions = binding_desc;
  586. vertex_info.vertexAttributeDescriptionCount = 3;
  587. vertex_info.pVertexAttributeDescriptions = attribute_desc;
  588. VkPipelineInputAssemblyStateCreateInfo ia_info = {};
  589. ia_info.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
  590. ia_info.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
  591. VkPipelineViewportStateCreateInfo viewport_info = {};
  592. viewport_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
  593. viewport_info.viewportCount = 1;
  594. viewport_info.scissorCount = 1;
  595. VkPipelineRasterizationStateCreateInfo raster_info = {};
  596. raster_info.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
  597. raster_info.polygonMode = VK_POLYGON_MODE_FILL;
  598. raster_info.cullMode = VK_CULL_MODE_NONE;
  599. raster_info.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
  600. raster_info.lineWidth = 1.0f;
  601. VkPipelineMultisampleStateCreateInfo ms_info = {};
  602. ms_info.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
  603. ms_info.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
  604. VkPipelineColorBlendAttachmentState color_attachment[1] = {};
  605. color_attachment[0].blendEnable = VK_TRUE;
  606. color_attachment[0].srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
  607. color_attachment[0].dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
  608. color_attachment[0].colorBlendOp = VK_BLEND_OP_ADD;
  609. color_attachment[0].srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
  610. color_attachment[0].dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
  611. color_attachment[0].alphaBlendOp = VK_BLEND_OP_ADD;
  612. color_attachment[0].colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
  613. VkPipelineDepthStencilStateCreateInfo depth_info = {};
  614. depth_info.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
  615. VkPipelineColorBlendStateCreateInfo blend_info = {};
  616. blend_info.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
  617. blend_info.attachmentCount = 1;
  618. blend_info.pAttachments = color_attachment;
  619. VkDynamicState dynamic_states[2] = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
  620. VkPipelineDynamicStateCreateInfo dynamic_state = {};
  621. dynamic_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
  622. dynamic_state.dynamicStateCount = 2;
  623. dynamic_state.pDynamicStates = dynamic_states;
  624. VkGraphicsPipelineCreateInfo info = {};
  625. info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
  626. info.flags = g_PipelineCreateFlags;
  627. info.stageCount = 2;
  628. info.pStages = stage;
  629. info.pVertexInputState = &vertex_info;
  630. info.pInputAssemblyState = &ia_info;
  631. info.pViewportState = &viewport_info;
  632. info.pRasterizationState = &raster_info;
  633. info.pMultisampleState = &ms_info;
  634. info.pDepthStencilState = &depth_info;
  635. info.pColorBlendState = &blend_info;
  636. info.pDynamicState = &dynamic_state;
  637. info.layout = g_PipelineLayout;
  638. info.renderPass = g_RenderPass;
  639. err = vkCreateGraphicsPipelines(g_Device, g_PipelineCache, 1, &info, g_Allocator, &g_Pipeline);
  640. ImGui_ImplGlfwVulkan_VkResult(err);
  641. vkDestroyShaderModule(g_Device, vert_module, g_Allocator);
  642. vkDestroyShaderModule(g_Device, frag_module, g_Allocator);
  643. return true;
  644. }
  645. void ImGui_ImplGlfwVulkan_InvalidateFontUploadObjects()
  646. {
  647. if (g_UploadBuffer)
  648. {
  649. vkDestroyBuffer(g_Device, g_UploadBuffer, g_Allocator);
  650. g_UploadBuffer = VK_NULL_HANDLE;
  651. }
  652. if (g_UploadBufferMemory)
  653. {
  654. vkFreeMemory(g_Device, g_UploadBufferMemory, g_Allocator);
  655. g_UploadBufferMemory = VK_NULL_HANDLE;
  656. }
  657. }
  658. void ImGui_ImplGlfwVulkan_InvalidateDeviceObjects()
  659. {
  660. ImGui_ImplGlfwVulkan_InvalidateFontUploadObjects();
  661. for (int i = 0; i < IMGUI_VK_QUEUED_FRAMES; i++)
  662. {
  663. if (g_VertexBuffer[i]) { vkDestroyBuffer(g_Device, g_VertexBuffer[i], g_Allocator); g_VertexBuffer[i] = VK_NULL_HANDLE; }
  664. if (g_VertexBufferMemory[i]) { vkFreeMemory(g_Device, g_VertexBufferMemory[i], g_Allocator); g_VertexBufferMemory[i] = VK_NULL_HANDLE; }
  665. if (g_IndexBuffer[i]) { vkDestroyBuffer(g_Device, g_IndexBuffer[i], g_Allocator); g_IndexBuffer[i] = VK_NULL_HANDLE; }
  666. if (g_IndexBufferMemory[i]) { vkFreeMemory(g_Device, g_IndexBufferMemory[i], g_Allocator); g_IndexBufferMemory[i] = VK_NULL_HANDLE; }
  667. }
  668. if (g_FontView) { vkDestroyImageView(g_Device, g_FontView, g_Allocator); g_FontView = VK_NULL_HANDLE; }
  669. if (g_FontImage) { vkDestroyImage(g_Device, g_FontImage, g_Allocator); g_FontImage = VK_NULL_HANDLE; }
  670. if (g_FontMemory) { vkFreeMemory(g_Device, g_FontMemory, g_Allocator); g_FontMemory = VK_NULL_HANDLE; }
  671. if (g_FontSampler) { vkDestroySampler(g_Device, g_FontSampler, g_Allocator); g_FontSampler = VK_NULL_HANDLE; }
  672. if (g_DescriptorSetLayout) { vkDestroyDescriptorSetLayout(g_Device, g_DescriptorSetLayout, g_Allocator); g_DescriptorSetLayout = VK_NULL_HANDLE; }
  673. if (g_PipelineLayout) { vkDestroyPipelineLayout(g_Device, g_PipelineLayout, g_Allocator); g_PipelineLayout = VK_NULL_HANDLE; }
  674. if (g_Pipeline) { vkDestroyPipeline(g_Device, g_Pipeline, g_Allocator); g_Pipeline = VK_NULL_HANDLE; }
  675. }
  676. bool ImGui_ImplGlfwVulkan_Init(GLFWwindow* window, bool install_callbacks, ImGui_ImplGlfwVulkan_Init_Data *init_data)
  677. {
  678. g_Allocator = init_data->allocator;
  679. g_Gpu = init_data->gpu;
  680. g_Device = init_data->device;
  681. g_RenderPass = init_data->render_pass;
  682. g_PipelineCache = init_data->pipeline_cache;
  683. g_DescriptorPool = init_data->descriptor_pool;
  684. g_CheckVkResult = init_data->check_vk_result;
  685. g_Window = window;
  686. ImGuiIO& io = ImGui::GetIO();
  687. io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array.
  688. io.KeyMap[ImGuiKey_LeftArrow] = GLFW_KEY_LEFT;
  689. io.KeyMap[ImGuiKey_RightArrow] = GLFW_KEY_RIGHT;
  690. io.KeyMap[ImGuiKey_UpArrow] = GLFW_KEY_UP;
  691. io.KeyMap[ImGuiKey_DownArrow] = GLFW_KEY_DOWN;
  692. io.KeyMap[ImGuiKey_PageUp] = GLFW_KEY_PAGE_UP;
  693. io.KeyMap[ImGuiKey_PageDown] = GLFW_KEY_PAGE_DOWN;
  694. io.KeyMap[ImGuiKey_Home] = GLFW_KEY_HOME;
  695. io.KeyMap[ImGuiKey_End] = GLFW_KEY_END;
  696. io.KeyMap[ImGuiKey_Insert] = GLFW_KEY_INSERT;
  697. io.KeyMap[ImGuiKey_Delete] = GLFW_KEY_DELETE;
  698. io.KeyMap[ImGuiKey_Backspace] = GLFW_KEY_BACKSPACE;
  699. io.KeyMap[ImGuiKey_Space] = GLFW_KEY_SPACE;
  700. io.KeyMap[ImGuiKey_Enter] = GLFW_KEY_ENTER;
  701. io.KeyMap[ImGuiKey_Escape] = GLFW_KEY_ESCAPE;
  702. io.KeyMap[ImGuiKey_A] = GLFW_KEY_A;
  703. io.KeyMap[ImGuiKey_C] = GLFW_KEY_C;
  704. io.KeyMap[ImGuiKey_V] = GLFW_KEY_V;
  705. io.KeyMap[ImGuiKey_X] = GLFW_KEY_X;
  706. io.KeyMap[ImGuiKey_Y] = GLFW_KEY_Y;
  707. io.KeyMap[ImGuiKey_Z] = GLFW_KEY_Z;
  708. io.RenderDrawListsFn = ImGui_ImplGlfwVulkan_RenderDrawLists; // Alternatively you can set this to NULL and call ImGui::GetDrawData() after ImGui::Render() to get the same ImDrawData pointer.
  709. io.SetClipboardTextFn = ImGui_ImplGlfwVulkan_SetClipboardText;
  710. io.GetClipboardTextFn = ImGui_ImplGlfwVulkan_GetClipboardText;
  711. io.ClipboardUserData = g_Window;
  712. #ifdef _WIN32
  713. io.ImeWindowHandle = glfwGetWin32Window(g_Window);
  714. #endif
  715. if (install_callbacks)
  716. {
  717. glfwSetMouseButtonCallback(window, ImGui_ImplGlfwVulkan_MouseButtonCallback);
  718. glfwSetScrollCallback(window, ImGui_ImplGlfwVulkan_ScrollCallback);
  719. glfwSetKeyCallback(window, ImGui_ImplGlfwVulkan_KeyCallback);
  720. glfwSetCharCallback(window, ImGui_ImplGlfwVulkan_CharCallback);
  721. }
  722. ImGui_ImplGlfwVulkan_CreateDeviceObjects();
  723. return true;
  724. }
  725. void ImGui_ImplGlfwVulkan_Shutdown()
  726. {
  727. ImGui_ImplGlfwVulkan_InvalidateDeviceObjects();
  728. }
  729. void ImGui_ImplGlfwVulkan_NewFrame()
  730. {
  731. ImGuiIO& io = ImGui::GetIO();
  732. // Setup display size (every frame to accommodate for window resizing)
  733. int w, h;
  734. int display_w, display_h;
  735. glfwGetWindowSize(g_Window, &w, &h);
  736. glfwGetFramebufferSize(g_Window, &display_w, &display_h);
  737. io.DisplaySize = ImVec2((float)w, (float)h);
  738. io.DisplayFramebufferScale = ImVec2(w > 0 ? ((float)display_w / w) : 0, h > 0 ? ((float)display_h / h) : 0);
  739. // Setup time step
  740. double current_time = glfwGetTime();
  741. io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f);
  742. g_Time = current_time;
  743. // Setup inputs
  744. // (we already got mouse wheel, keyboard keys & characters from glfw callbacks polled in glfwPollEvents())
  745. if (glfwGetWindowAttrib(g_Window, GLFW_FOCUSED))
  746. {
  747. double mouse_x, mouse_y;
  748. glfwGetCursorPos(g_Window, &mouse_x, &mouse_y);
  749. io.MousePos = ImVec2((float)mouse_x, (float)mouse_y);
  750. }
  751. else
  752. {
  753. io.MousePos = ImVec2(-FLT_MAX,-FLT_MAX);
  754. }
  755. for (int i = 0; i < 3; i++)
  756. {
  757. io.MouseDown[i] = g_MouseJustPressed[i] || glfwGetMouseButton(g_Window, i) != 0; // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
  758. g_MouseJustPressed[i] = false;
  759. }
  760. // Hide OS mouse cursor if ImGui is drawing it
  761. glfwSetInputMode(g_Window, GLFW_CURSOR, io.MouseDrawCursor ? GLFW_CURSOR_HIDDEN : GLFW_CURSOR_NORMAL);
  762. // Start the frame. This call will update the io.WantCaptureMouse, io.WantCaptureKeyboard flag that you can use to dispatch inputs (or not) to your application.
  763. ImGui::NewFrame();
  764. }
  765. void ImGui_ImplGlfwVulkan_Render(VkCommandBuffer command_buffer)
  766. {
  767. g_CommandBuffer = command_buffer;
  768. ImGui::Render();
  769. g_CommandBuffer = VK_NULL_HANDLE;
  770. g_FrameIndex = (g_FrameIndex + 1) % IMGUI_VK_QUEUED_FRAMES;
  771. }