19 static GLFWwindow* g_Window = NULL;
20 static double g_Time = 0.0f;
23 static VkAllocationCallbacks* g_Allocator = NULL;
24 static VkPhysicalDevice g_Gpu = VK_NULL_HANDLE;
25 static VkDevice g_Device = VK_NULL_HANDLE;
26 static VkRenderPass g_RenderPass = VK_NULL_HANDLE;
27 static VkPipelineCache g_PipelineCache = VK_NULL_HANDLE;
28 static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE;
29 static void(*g_CheckVkResult)(VkResult err) = NULL;
31 static VkCommandBuffer g_CommandBuffer = VK_NULL_HANDLE;
32 static size_t g_BufferMemoryAlignment = 256;
33 static VkPipelineCreateFlags g_PipelineCreateFlags = 0;
34 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;
41 static VkSampler g_FontSampler = VK_NULL_HANDLE;
42 static VkDeviceMemory g_FontMemory = VK_NULL_HANDLE;
43 static VkImage g_FontImage = VK_NULL_HANDLE;
44 static VkImageView g_FontView = VK_NULL_HANDLE;
46 static VkDeviceMemory g_VertexBufferMemory[IMGUI_VK_QUEUED_FRAMES] = {};
47 static VkDeviceMemory g_IndexBufferMemory[IMGUI_VK_QUEUED_FRAMES] = {};
48 static size_t g_VertexBufferSize[IMGUI_VK_QUEUED_FRAMES] = {};
49 static size_t g_IndexBufferSize[IMGUI_VK_QUEUED_FRAMES] = {};
50 static VkBuffer g_VertexBuffer[IMGUI_VK_QUEUED_FRAMES] = {};
51 static VkBuffer g_IndexBuffer[IMGUI_VK_QUEUED_FRAMES] = {};
53 static VkDeviceMemory g_UploadBufferMemory = VK_NULL_HANDLE;
54 static VkBuffer g_UploadBuffer = VK_NULL_HANDLE;
56 static uint32_t __glsl_shader_vert_spv[] =
58 0x07230203,0x00010000,0x00080001,0x0000002e,0x00000000,0x00020011,0x00000001,0x0006000b,
59 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001,
60 0x000a000f,0x00000000,0x00000004,0x6e69616d,0x00000000,0x0000000b,0x0000000f,0x00000015,
61 0x0000001b,0x0000001c,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d,
62 0x00000000,0x00030005,0x00000009,0x00000000,0x00050006,0x00000009,0x00000000,0x6f6c6f43,
63 0x00000072,0x00040006,0x00000009,0x00000001,0x00005655,0x00030005,0x0000000b,0x0074754f,
64 0x00040005,0x0000000f,0x6c6f4361,0x0000726f,0x00030005,0x00000015,0x00565561,0x00060005,
65 0x00000019,0x505f6c67,0x65567265,0x78657472,0x00000000,0x00060006,0x00000019,0x00000000,
66 0x505f6c67,0x7469736f,0x006e6f69,0x00030005,0x0000001b,0x00000000,0x00040005,0x0000001c,
67 0x736f5061,0x00000000,0x00060005,0x0000001e,0x73755075,0x6e6f4368,0x6e617473,0x00000074,
68 0x00050006,0x0000001e,0x00000000,0x61635375,0x0000656c,0x00060006,0x0000001e,0x00000001,
69 0x61725475,0x616c736e,0x00006574,0x00030005,0x00000020,0x00006370,0x00040047,0x0000000b,
70 0x0000001e,0x00000000,0x00040047,0x0000000f,0x0000001e,0x00000002,0x00040047,0x00000015,
71 0x0000001e,0x00000001,0x00050048,0x00000019,0x00000000,0x0000000b,0x00000000,0x00030047,
72 0x00000019,0x00000002,0x00040047,0x0000001c,0x0000001e,0x00000000,0x00050048,0x0000001e,
73 0x00000000,0x00000023,0x00000000,0x00050048,0x0000001e,0x00000001,0x00000023,0x00000008,
74 0x00030047,0x0000001e,0x00000002,0x00020013,0x00000002,0x00030021,0x00000003,0x00000002,
75 0x00030016,0x00000006,0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,0x00040017,
76 0x00000008,0x00000006,0x00000002,0x0004001e,0x00000009,0x00000007,0x00000008,0x00040020,
77 0x0000000a,0x00000003,0x00000009,0x0004003b,0x0000000a,0x0000000b,0x00000003,0x00040015,
78 0x0000000c,0x00000020,0x00000001,0x0004002b,0x0000000c,0x0000000d,0x00000000,0x00040020,
79 0x0000000e,0x00000001,0x00000007,0x0004003b,0x0000000e,0x0000000f,0x00000001,0x00040020,
80 0x00000011,0x00000003,0x00000007,0x0004002b,0x0000000c,0x00000013,0x00000001,0x00040020,
81 0x00000014,0x00000001,0x00000008,0x0004003b,0x00000014,0x00000015,0x00000001,0x00040020,
82 0x00000017,0x00000003,0x00000008,0x0003001e,0x00000019,0x00000007,0x00040020,0x0000001a,
83 0x00000003,0x00000019,0x0004003b,0x0000001a,0x0000001b,0x00000003,0x0004003b,0x00000014,
84 0x0000001c,0x00000001,0x0004001e,0x0000001e,0x00000008,0x00000008,0x00040020,0x0000001f,
85 0x00000009,0x0000001e,0x0004003b,0x0000001f,0x00000020,0x00000009,0x00040020,0x00000021,
86 0x00000009,0x00000008,0x0004002b,0x00000006,0x00000028,0x00000000,0x0004002b,0x00000006,
87 0x00000029,0x3f800000,0x00050036,0x00000002,0x00000004,0x00000000,0x00000003,0x000200f8,
88 0x00000005,0x0004003d,0x00000007,0x00000010,0x0000000f,0x00050041,0x00000011,0x00000012,
89 0x0000000b,0x0000000d,0x0003003e,0x00000012,0x00000010,0x0004003d,0x00000008,0x00000016,
90 0x00000015,0x00050041,0x00000017,0x00000018,0x0000000b,0x00000013,0x0003003e,0x00000018,
91 0x00000016,0x0004003d,0x00000008,0x0000001d,0x0000001c,0x00050041,0x00000021,0x00000022,
92 0x00000020,0x0000000d,0x0004003d,0x00000008,0x00000023,0x00000022,0x00050085,0x00000008,
93 0x00000024,0x0000001d,0x00000023,0x00050041,0x00000021,0x00000025,0x00000020,0x00000013,
94 0x0004003d,0x00000008,0x00000026,0x00000025,0x00050081,0x00000008,0x00000027,0x00000024,
95 0x00000026,0x00050051,0x00000006,0x0000002a,0x00000027,0x00000000,0x00050051,0x00000006,
96 0x0000002b,0x00000027,0x00000001,0x00070050,0x00000007,0x0000002c,0x0000002a,0x0000002b,
97 0x00000028,0x00000029,0x00050041,0x00000011,0x0000002d,0x0000001b,0x0000000d,0x0003003e,
98 0x0000002d,0x0000002c,0x000100fd,0x00010038
101 static uint32_t __glsl_shader_frag_spv[] =
103 0x07230203,0x00010000,0x00080001,0x0000001e,0x00000000,0x00020011,0x00000001,0x0006000b,
104 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001,
105 0x0007000f,0x00000004,0x00000004,0x6e69616d,0x00000000,0x00000009,0x0000000d,0x00030010,
106 0x00000004,0x00000007,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d,
107 0x00000000,0x00040005,0x00000009,0x6c6f4366,0x0000726f,0x00030005,0x0000000b,0x00000000,
108 0x00050006,0x0000000b,0x00000000,0x6f6c6f43,0x00000072,0x00040006,0x0000000b,0x00000001,
109 0x00005655,0x00030005,0x0000000d,0x00006e49,0x00050005,0x00000016,0x78655473,0x65727574,
110 0x00000000,0x00040047,0x00000009,0x0000001e,0x00000000,0x00040047,0x0000000d,0x0000001e,
111 0x00000000,0x00040047,0x00000016,0x00000022,0x00000000,0x00040047,0x00000016,0x00000021,
112 0x00000000,0x00020013,0x00000002,0x00030021,0x00000003,0x00000002,0x00030016,0x00000006,
113 0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,0x00040020,0x00000008,0x00000003,
114 0x00000007,0x0004003b,0x00000008,0x00000009,0x00000003,0x00040017,0x0000000a,0x00000006,
115 0x00000002,0x0004001e,0x0000000b,0x00000007,0x0000000a,0x00040020,0x0000000c,0x00000001,
116 0x0000000b,0x0004003b,0x0000000c,0x0000000d,0x00000001,0x00040015,0x0000000e,0x00000020,
117 0x00000001,0x0004002b,0x0000000e,0x0000000f,0x00000000,0x00040020,0x00000010,0x00000001,
118 0x00000007,0x00090019,0x00000013,0x00000006,0x00000001,0x00000000,0x00000000,0x00000000,
119 0x00000001,0x00000000,0x0003001b,0x00000014,0x00000013,0x00040020,0x00000015,0x00000000,
120 0x00000014,0x0004003b,0x00000015,0x00000016,0x00000000,0x0004002b,0x0000000e,0x00000018,
121 0x00000001,0x00040020,0x00000019,0x00000001,0x0000000a,0x00050036,0x00000002,0x00000004,
122 0x00000000,0x00000003,0x000200f8,0x00000005,0x00050041,0x00000010,0x00000011,0x0000000d,
123 0x0000000f,0x0004003d,0x00000007,0x00000012,0x00000011,0x0004003d,0x00000014,0x00000017,
124 0x00000016,0x00050041,0x00000019,0x0000001a,0x0000000d,0x00000018,0x0004003d,0x0000000a,
125 0x0000001b,0x0000001a,0x00050057,0x00000007,0x0000001c,0x00000017,0x0000001b,0x00050085,
126 0x00000007,0x0000001d,0x00000012,0x0000001c,0x0003003e,0x00000009,0x0000001d,0x000100fd,
130 static uint32_t ImGui_ImplGlfwVulkan_MemoryType(VkMemoryPropertyFlags properties, uint32_t type_bits)
132 VkPhysicalDeviceMemoryProperties prop;
133 vkGetPhysicalDeviceMemoryProperties(g_Gpu, &prop);
134 for (uint32_t i = 0; i < prop.memoryTypeCount; i++)
135 if ((prop.memoryTypes[i].propertyFlags & properties) == properties && type_bits & (1 << i))
140 static void ImGui_ImplGlfwVulkan_VkResult(VkResult err)
143 g_CheckVkResult(err);
147 void ImGui_ImplGlfwVulkan_RenderDrawLists(
ImDrawData* draw_data)
154 if (!g_VertexBuffer[g_FrameIndex] || g_VertexBufferSize[g_FrameIndex] < vertex_size)
156 if (g_VertexBuffer[g_FrameIndex])
157 vkDestroyBuffer(g_Device, g_VertexBuffer[g_FrameIndex], g_Allocator);
158 if (g_VertexBufferMemory[g_FrameIndex])
159 vkFreeMemory(g_Device, g_VertexBufferMemory[g_FrameIndex], g_Allocator);
160 size_t vertex_buffer_size = ((vertex_size - 1) / g_BufferMemoryAlignment + 1) * g_BufferMemoryAlignment;
161 VkBufferCreateInfo buffer_info = {};
162 buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
163 buffer_info.size = vertex_buffer_size;
164 buffer_info.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
165 buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
166 err = vkCreateBuffer(g_Device, &buffer_info, g_Allocator, &g_VertexBuffer[g_FrameIndex]);
167 ImGui_ImplGlfwVulkan_VkResult(err);
168 VkMemoryRequirements req;
169 vkGetBufferMemoryRequirements(g_Device, g_VertexBuffer[g_FrameIndex], &req);
170 g_BufferMemoryAlignment = (g_BufferMemoryAlignment > req.alignment) ? g_BufferMemoryAlignment : req.alignment;
171 VkMemoryAllocateInfo alloc_info = {};
172 alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
173 alloc_info.allocationSize = req.size;
174 alloc_info.memoryTypeIndex = ImGui_ImplGlfwVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
175 err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &g_VertexBufferMemory[g_FrameIndex]);
176 ImGui_ImplGlfwVulkan_VkResult(err);
177 err = vkBindBufferMemory(g_Device, g_VertexBuffer[g_FrameIndex], g_VertexBufferMemory[g_FrameIndex], 0);
178 ImGui_ImplGlfwVulkan_VkResult(err);
179 g_VertexBufferSize[g_FrameIndex] = vertex_buffer_size;
184 if (!g_IndexBuffer[g_FrameIndex] || g_IndexBufferSize[g_FrameIndex] < index_size)
186 if (g_IndexBuffer[g_FrameIndex])
187 vkDestroyBuffer(g_Device, g_IndexBuffer[g_FrameIndex], g_Allocator);
188 if (g_IndexBufferMemory[g_FrameIndex])
189 vkFreeMemory(g_Device, g_IndexBufferMemory[g_FrameIndex], g_Allocator);
190 size_t index_buffer_size = ((index_size - 1) / g_BufferMemoryAlignment + 1) * g_BufferMemoryAlignment;
191 VkBufferCreateInfo buffer_info = {};
192 buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
193 buffer_info.size = index_buffer_size;
194 buffer_info.usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
195 buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
196 err = vkCreateBuffer(g_Device, &buffer_info, g_Allocator, &g_IndexBuffer[g_FrameIndex]);
197 ImGui_ImplGlfwVulkan_VkResult(err);
198 VkMemoryRequirements req;
199 vkGetBufferMemoryRequirements(g_Device, g_IndexBuffer[g_FrameIndex], &req);
200 g_BufferMemoryAlignment = (g_BufferMemoryAlignment > req.alignment) ? g_BufferMemoryAlignment : req.alignment;
201 VkMemoryAllocateInfo alloc_info = {};
202 alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
203 alloc_info.allocationSize = req.size;
204 alloc_info.memoryTypeIndex = ImGui_ImplGlfwVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
205 err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &g_IndexBufferMemory[g_FrameIndex]);
206 ImGui_ImplGlfwVulkan_VkResult(err);
207 err = vkBindBufferMemory(g_Device, g_IndexBuffer[g_FrameIndex], g_IndexBufferMemory[g_FrameIndex], 0);
208 ImGui_ImplGlfwVulkan_VkResult(err);
209 g_IndexBufferSize[g_FrameIndex] = index_buffer_size;
216 err = vkMapMemory(g_Device, g_VertexBufferMemory[g_FrameIndex], 0, vertex_size, 0, (
void**)(&vtx_dst));
217 ImGui_ImplGlfwVulkan_VkResult(err);
218 err = vkMapMemory(g_Device, g_IndexBufferMemory[g_FrameIndex], 0, index_size, 0, (
void**)(&idx_dst));
219 ImGui_ImplGlfwVulkan_VkResult(err);
228 VkMappedMemoryRange range[2] = {};
229 range[0].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
230 range[0].memory = g_VertexBufferMemory[g_FrameIndex];
231 range[0].size = VK_WHOLE_SIZE;
232 range[1].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
233 range[1].memory = g_IndexBufferMemory[g_FrameIndex];
234 range[1].size = VK_WHOLE_SIZE;
235 err = vkFlushMappedMemoryRanges(g_Device, 2, range);
236 ImGui_ImplGlfwVulkan_VkResult(err);
237 vkUnmapMemory(g_Device, g_VertexBufferMemory[g_FrameIndex]);
238 vkUnmapMemory(g_Device, g_IndexBufferMemory[g_FrameIndex]);
243 vkCmdBindPipeline(g_CommandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, g_Pipeline);
244 VkDescriptorSet desc_set[1] = { g_DescriptorSet };
245 vkCmdBindDescriptorSets(g_CommandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, g_PipelineLayout, 0, 1, desc_set, 0, NULL);
250 VkBuffer vertex_buffers[1] = { g_VertexBuffer[g_FrameIndex] };
251 VkDeviceSize vertex_offset[1] = { 0 };
252 vkCmdBindVertexBuffers(g_CommandBuffer, 0, 1, vertex_buffers, vertex_offset);
253 vkCmdBindIndexBuffer(g_CommandBuffer, g_IndexBuffer[g_FrameIndex], 0, VK_INDEX_TYPE_UINT16);
263 viewport.minDepth = 0.0f;
264 viewport.maxDepth = 1.0f;
265 vkCmdSetViewport(g_CommandBuffer, 0, 1, &viewport);
274 translate[0] = -1.0f;
275 translate[1] = -1.0f;
276 vkCmdPushConstants(g_CommandBuffer, g_PipelineLayout, VK_SHADER_STAGE_VERTEX_BIT,
sizeof(
float) * 0,
sizeof(
float) * 2, scale);
277 vkCmdPushConstants(g_CommandBuffer, g_PipelineLayout, VK_SHADER_STAGE_VERTEX_BIT,
sizeof(
float) * 2,
sizeof(
float) * 2, translate);
286 for (
int cmd_i = 0; cmd_i < cmd_list->
CmdBuffer.
Size; cmd_i++)
300 vkCmdSetScissor(g_CommandBuffer, 0, 1, &scissor);
301 vkCmdDrawIndexed(g_CommandBuffer, pcmd->
ElemCount, 1, idx_offset, vtx_offset, 0);
309 static const char* ImGui_ImplGlfwVulkan_GetClipboardText(
void* user_data)
311 return glfwGetClipboardString((GLFWwindow*)user_data);
314 static void ImGui_ImplGlfwVulkan_SetClipboardText(
void* user_data,
const char* text)
316 glfwSetClipboardString((GLFWwindow*)user_data, text);
319 bool ImGui_ImplGlfwVulkan_CreateFontsTexture(VkCommandBuffer command_buffer)
323 unsigned char* pixels;
326 size_t upload_size = width*height * 4 *
sizeof(char);
332 VkImageCreateInfo info = {};
333 info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
334 info.imageType = VK_IMAGE_TYPE_2D;
335 info.format = VK_FORMAT_R8G8B8A8_UNORM;
336 info.extent.width = width;
337 info.extent.height = height;
338 info.extent.depth = 1;
340 info.arrayLayers = 1;
341 info.samples = VK_SAMPLE_COUNT_1_BIT;
342 info.tiling = VK_IMAGE_TILING_OPTIMAL;
343 info.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
344 info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
345 info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
346 err = vkCreateImage(g_Device, &info, g_Allocator, &g_FontImage);
347 ImGui_ImplGlfwVulkan_VkResult(err);
348 VkMemoryRequirements req;
349 vkGetImageMemoryRequirements(g_Device, g_FontImage, &req);
350 VkMemoryAllocateInfo alloc_info = {};
351 alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
352 alloc_info.allocationSize = req.size;
353 alloc_info.memoryTypeIndex = ImGui_ImplGlfwVulkan_MemoryType(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, req.memoryTypeBits);
354 err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &g_FontMemory);
355 ImGui_ImplGlfwVulkan_VkResult(err);
356 err = vkBindImageMemory(g_Device, g_FontImage, g_FontMemory, 0);
357 ImGui_ImplGlfwVulkan_VkResult(err);
362 VkImageViewCreateInfo info = {};
363 info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
364 info.image = g_FontImage;
365 info.viewType = VK_IMAGE_VIEW_TYPE_2D;
366 info.format = VK_FORMAT_R8G8B8A8_UNORM;
367 info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
368 info.subresourceRange.levelCount = 1;
369 info.subresourceRange.layerCount = 1;
370 err = vkCreateImageView(g_Device, &info, g_Allocator, &g_FontView);
371 ImGui_ImplGlfwVulkan_VkResult(err);
376 VkDescriptorImageInfo desc_image[1] = {};
377 desc_image[0].sampler = g_FontSampler;
378 desc_image[0].imageView = g_FontView;
379 desc_image[0].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
380 VkWriteDescriptorSet write_desc[1] = {};
381 write_desc[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
382 write_desc[0].dstSet = g_DescriptorSet;
383 write_desc[0].descriptorCount = 1;
384 write_desc[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
385 write_desc[0].pImageInfo = desc_image;
386 vkUpdateDescriptorSets(g_Device, 1, write_desc, 0, NULL);
391 VkBufferCreateInfo buffer_info = {};
392 buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
393 buffer_info.size = upload_size;
394 buffer_info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
395 buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
396 err = vkCreateBuffer(g_Device, &buffer_info, g_Allocator, &g_UploadBuffer);
397 ImGui_ImplGlfwVulkan_VkResult(err);
398 VkMemoryRequirements req;
399 vkGetBufferMemoryRequirements(g_Device, g_UploadBuffer, &req);
400 g_BufferMemoryAlignment = (g_BufferMemoryAlignment > req.alignment) ? g_BufferMemoryAlignment : req.alignment;
401 VkMemoryAllocateInfo alloc_info = {};
402 alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
403 alloc_info.allocationSize = req.size;
404 alloc_info.memoryTypeIndex = ImGui_ImplGlfwVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
405 err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &g_UploadBufferMemory);
406 ImGui_ImplGlfwVulkan_VkResult(err);
407 err = vkBindBufferMemory(g_Device, g_UploadBuffer, g_UploadBufferMemory, 0);
408 ImGui_ImplGlfwVulkan_VkResult(err);
414 err = vkMapMemory(g_Device, g_UploadBufferMemory, 0, upload_size, 0, (
void**)(&map));
415 ImGui_ImplGlfwVulkan_VkResult(err);
416 memcpy(map, pixels, upload_size);
417 VkMappedMemoryRange range[1] = {};
418 range[0].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
419 range[0].memory = g_UploadBufferMemory;
420 range[0].size = upload_size;
421 err = vkFlushMappedMemoryRanges(g_Device, 1, range);
422 ImGui_ImplGlfwVulkan_VkResult(err);
423 vkUnmapMemory(g_Device, g_UploadBufferMemory);
427 VkImageMemoryBarrier copy_barrier[1] = {};
428 copy_barrier[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
429 copy_barrier[0].dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
430 copy_barrier[0].oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
431 copy_barrier[0].newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
432 copy_barrier[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
433 copy_barrier[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
434 copy_barrier[0].image = g_FontImage;
435 copy_barrier[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
436 copy_barrier[0].subresourceRange.levelCount = 1;
437 copy_barrier[0].subresourceRange.layerCount = 1;
438 vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 0, NULL, 1, copy_barrier);
440 VkBufferImageCopy region = {};
441 region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
442 region.imageSubresource.layerCount = 1;
443 region.imageExtent.width = width;
444 region.imageExtent.height = height;
445 region.imageExtent.depth = 1;
446 vkCmdCopyBufferToImage(command_buffer, g_UploadBuffer, g_FontImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion);
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);
464 io.
Fonts->
TexID = (
void *)(intptr_t)g_FontImage;
469 bool ImGui_ImplGlfwVulkan_CreateDeviceObjects()
472 VkShaderModule vert_module;
473 VkShaderModule frag_module;
477 VkShaderModuleCreateInfo vert_info = {};
478 vert_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
479 vert_info.codeSize =
sizeof(__glsl_shader_vert_spv);
480 vert_info.pCode = (uint32_t*)__glsl_shader_vert_spv;
481 err = vkCreateShaderModule(g_Device, &vert_info, g_Allocator, &vert_module);
482 ImGui_ImplGlfwVulkan_VkResult(err);
483 VkShaderModuleCreateInfo frag_info = {};
484 frag_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
485 frag_info.codeSize =
sizeof(__glsl_shader_frag_spv);
486 frag_info.pCode = (uint32_t*)__glsl_shader_frag_spv;
487 err = vkCreateShaderModule(g_Device, &frag_info, g_Allocator, &frag_module);
488 ImGui_ImplGlfwVulkan_VkResult(err);
493 VkSamplerCreateInfo info = {};
494 info.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
495 info.magFilter = VK_FILTER_LINEAR;
496 info.minFilter = VK_FILTER_LINEAR;
497 info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
498 info.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
499 info.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
500 info.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
503 info.maxAnisotropy = 1.0f;
504 err = vkCreateSampler(g_Device, &info, g_Allocator, &g_FontSampler);
505 ImGui_ImplGlfwVulkan_VkResult(err);
508 if (!g_DescriptorSetLayout)
510 VkSampler sampler[1] = { g_FontSampler };
511 VkDescriptorSetLayoutBinding binding[1] = {};
512 binding[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
513 binding[0].descriptorCount = 1;
514 binding[0].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
515 binding[0].pImmutableSamplers = sampler;
516 VkDescriptorSetLayoutCreateInfo info = {};
517 info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
518 info.bindingCount = 1;
519 info.pBindings = binding;
520 err = vkCreateDescriptorSetLayout(g_Device, &info, g_Allocator, &g_DescriptorSetLayout);
521 ImGui_ImplGlfwVulkan_VkResult(err);
526 VkDescriptorSetAllocateInfo alloc_info = {};
527 alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
528 alloc_info.descriptorPool = g_DescriptorPool;
529 alloc_info.descriptorSetCount = 1;
530 alloc_info.pSetLayouts = &g_DescriptorSetLayout;
531 err = vkAllocateDescriptorSets(g_Device, &alloc_info, &g_DescriptorSet);
532 ImGui_ImplGlfwVulkan_VkResult(err);
535 if (!g_PipelineLayout)
537 VkPushConstantRange push_constants[1] = {};
538 push_constants[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
539 push_constants[0].offset =
sizeof(float) * 0;
540 push_constants[0].size =
sizeof(float) * 4;
541 VkDescriptorSetLayout set_layout[1] = { g_DescriptorSetLayout };
542 VkPipelineLayoutCreateInfo layout_info = {};
543 layout_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
544 layout_info.setLayoutCount = 1;
545 layout_info.pSetLayouts = set_layout;
546 layout_info.pushConstantRangeCount = 1;
547 layout_info.pPushConstantRanges = push_constants;
548 err = vkCreatePipelineLayout(g_Device, &layout_info, g_Allocator, &g_PipelineLayout);
549 ImGui_ImplGlfwVulkan_VkResult(err);
552 VkPipelineShaderStageCreateInfo stage[2] = {};
553 stage[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
554 stage[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
555 stage[0].module = vert_module;
556 stage[0].pName =
"main";
557 stage[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
558 stage[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
559 stage[1].module = frag_module;
560 stage[1].pName =
"main";
562 VkVertexInputBindingDescription binding_desc[1] = {};
564 binding_desc[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
566 VkVertexInputAttributeDescription attribute_desc[3] = {};
567 attribute_desc[0].location = 0;
568 attribute_desc[0].binding = binding_desc[0].binding;
569 attribute_desc[0].format = VK_FORMAT_R32G32_SFLOAT;
570 attribute_desc[0].offset = (size_t)(&((
ImDrawVert*)0)->pos);
571 attribute_desc[1].location = 1;
572 attribute_desc[1].binding = binding_desc[0].binding;
573 attribute_desc[1].format = VK_FORMAT_R32G32_SFLOAT;
574 attribute_desc[1].offset = (size_t)(&((
ImDrawVert*)0)->uv);
575 attribute_desc[2].location = 2;
576 attribute_desc[2].binding = binding_desc[0].binding;
577 attribute_desc[2].format = VK_FORMAT_R8G8B8A8_UNORM;
578 attribute_desc[2].offset = (size_t)(&((
ImDrawVert*)0)->col);
580 VkPipelineVertexInputStateCreateInfo vertex_info = {};
581 vertex_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
582 vertex_info.vertexBindingDescriptionCount = 1;
583 vertex_info.pVertexBindingDescriptions = binding_desc;
584 vertex_info.vertexAttributeDescriptionCount = 3;
585 vertex_info.pVertexAttributeDescriptions = attribute_desc;
587 VkPipelineInputAssemblyStateCreateInfo ia_info = {};
588 ia_info.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
589 ia_info.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
591 VkPipelineViewportStateCreateInfo viewport_info = {};
592 viewport_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
593 viewport_info.viewportCount = 1;
594 viewport_info.scissorCount = 1;
596 VkPipelineRasterizationStateCreateInfo raster_info = {};
597 raster_info.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
598 raster_info.polygonMode = VK_POLYGON_MODE_FILL;
599 raster_info.cullMode = VK_CULL_MODE_NONE;
600 raster_info.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
601 raster_info.lineWidth = 1.0f;
603 VkPipelineMultisampleStateCreateInfo ms_info = {};
604 ms_info.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
605 ms_info.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
607 VkPipelineColorBlendAttachmentState color_attachment[1] = {};
608 color_attachment[0].blendEnable = VK_TRUE;
609 color_attachment[0].srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
610 color_attachment[0].dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
611 color_attachment[0].colorBlendOp = VK_BLEND_OP_ADD;
612 color_attachment[0].srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
613 color_attachment[0].dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
614 color_attachment[0].alphaBlendOp = VK_BLEND_OP_ADD;
615 color_attachment[0].colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
617 VkPipelineDepthStencilStateCreateInfo depth_info = {};
618 depth_info.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
620 VkPipelineColorBlendStateCreateInfo blend_info = {};
621 blend_info.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
622 blend_info.attachmentCount = 1;
623 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;
631 VkGraphicsPipelineCreateInfo info = {};
632 info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
633 info.flags = g_PipelineCreateFlags;
635 info.pStages = stage;
636 info.pVertexInputState = &vertex_info;
637 info.pInputAssemblyState = &ia_info;
638 info.pViewportState = &viewport_info;
639 info.pRasterizationState = &raster_info;
640 info.pMultisampleState = &ms_info;
641 info.pDepthStencilState = &depth_info;
642 info.pColorBlendState = &blend_info;
643 info.pDynamicState = &dynamic_state;
644 info.layout = g_PipelineLayout;
645 info.renderPass = g_RenderPass;
646 err = vkCreateGraphicsPipelines(g_Device, g_PipelineCache, 1, &info, g_Allocator, &g_Pipeline);
647 ImGui_ImplGlfwVulkan_VkResult(err);
649 vkDestroyShaderModule(g_Device, vert_module, g_Allocator);
650 vkDestroyShaderModule(g_Device, frag_module, g_Allocator);
655 void ImGui_ImplGlfwVulkan_InvalidateFontUploadObjects()
659 vkDestroyBuffer(g_Device, g_UploadBuffer, g_Allocator);
660 g_UploadBuffer = VK_NULL_HANDLE;
662 if (g_UploadBufferMemory)
664 vkFreeMemory(g_Device, g_UploadBufferMemory, g_Allocator);
665 g_UploadBufferMemory = VK_NULL_HANDLE;
669 void ImGui_ImplGlfwVulkan_InvalidateDeviceObjects()
671 ImGui_ImplGlfwVulkan_InvalidateFontUploadObjects();
673 for (
int i = 0; i < IMGUI_VK_QUEUED_FRAMES; i++)
675 if (g_VertexBuffer[i]) { vkDestroyBuffer(g_Device, g_VertexBuffer[i], g_Allocator); g_VertexBuffer[i] = VK_NULL_HANDLE; }
676 if (g_VertexBufferMemory[i]) { vkFreeMemory(g_Device, g_VertexBufferMemory[i], g_Allocator); g_VertexBufferMemory[i] = VK_NULL_HANDLE; }
677 if (g_IndexBuffer[i]) { vkDestroyBuffer(g_Device, g_IndexBuffer[i], g_Allocator); g_IndexBuffer[i] = VK_NULL_HANDLE; }
678 if (g_IndexBufferMemory[i]) { vkFreeMemory(g_Device, g_IndexBufferMemory[i], g_Allocator); g_IndexBufferMemory[i] = VK_NULL_HANDLE; }
681 if (g_FontView) { vkDestroyImageView(g_Device, g_FontView, g_Allocator); g_FontView = VK_NULL_HANDLE; }
682 if (g_FontImage) { vkDestroyImage(g_Device, g_FontImage, g_Allocator); g_FontImage = VK_NULL_HANDLE; }
683 if (g_FontMemory) { vkFreeMemory(g_Device, g_FontMemory, g_Allocator); g_FontMemory = VK_NULL_HANDLE; }
684 if (g_FontSampler) { vkDestroySampler(g_Device, g_FontSampler, g_Allocator); g_FontSampler = VK_NULL_HANDLE; }
685 if (g_DescriptorSetLayout) { vkDestroyDescriptorSetLayout(g_Device, g_DescriptorSetLayout, g_Allocator); g_DescriptorSetLayout = VK_NULL_HANDLE; }
686 if (g_PipelineLayout) { vkDestroyPipelineLayout(g_Device, g_PipelineLayout, g_Allocator); g_PipelineLayout = VK_NULL_HANDLE; }
687 if (g_Pipeline) { vkDestroyPipeline(g_Device, g_Pipeline, g_Allocator); g_Pipeline = VK_NULL_HANDLE; }
690 bool ImGui_ImplGlfwVulkan_Init(GLFWwindow* window, ImGui_ImplGlfwVulkan_Init_Data *init_data)
692 g_Allocator = init_data->allocator;
693 g_Gpu = init_data->gpu;
694 g_Device = init_data->device;
695 g_RenderPass = init_data->render_pass;
696 g_PipelineCache = init_data->pipeline_cache;
697 g_DescriptorPool = init_data->descriptor_pool;
698 g_CheckVkResult = init_data->check_vk_result;
712 ImGui_ImplGlfwVulkan_CreateDeviceObjects();
717 void ImGui_ImplGlfwVulkan_Shutdown()
719 ImGui_ImplGlfwVulkan_InvalidateDeviceObjects();
723 void ImGui_ImplGlfwVulkan_NewFrame()
726 glfwSetInputMode(g_Window, GLFW_CURSOR, io.
MouseDrawCursor ? GLFW_CURSOR_HIDDEN : GLFW_CURSOR_NORMAL);
738 void ImGui_ImplGlfwVulkan_Render(VkCommandBuffer command_buffer)
740 g_CommandBuffer = command_buffer;
742 g_CommandBuffer = VK_NULL_HANDLE;
743 g_FrameIndex = (g_FrameIndex + 1) % IMGUI_VK_QUEUED_FRAMES;
745 #endif // USING_VULKAN const char *(* GetClipboardTextFn)(void *user_data)
void(* SetClipboardTextFn)(void *user_data, const char *text)
IMGUI_API void NewFrame()
ImVector< ImDrawCmd > CmdBuffer
IMGUI_API ImGuiIO & GetIO()
IMGUI_API void GetTexDataAsRGBA32(unsigned char **out_pixels, int *out_width, int *out_height, int *out_bytes_per_pixel=NULL)
ImDrawCallback UserCallback
IMGUI_API void Shutdown()
void(* RenderDrawListsFn)(ImDrawData *data)
ImVector< ImDrawVert > VtxBuffer
static eastl::weak_ptr< Engine > GetEngine() noexcept
This method allows you to get the instance of the Engine. This method will automatically initialize t...
ImVector< ImDrawIdx > IdxBuffer