imgui_impl_glfw_vulkan.cpp 44 KB

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