imgui_impl_glfw_vulkan.cpp 42 KB

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