imgui_impl_glfw_vulkan.cpp 40 KB

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