imgui_impl_glfw_vulkan.cpp 43 KB

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