1 // ImGui - standalone example application for Glfw + Vulkan
2 // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
3
4 #include "imgui.h"
5 #include "imgui_impl_glfw.h"
6 #include "imgui_impl_vulkan.h"
7 #include <stdio.h> // printf, fprintf
8 #include <stdlib.h> // abort
9 #define GLFW_INCLUDE_NONE
10 #define GLFW_INCLUDE_VULKAN
11 #include <GLFW/glfw3.h>
12 #include <vulkan/vulkan.h>
13
14 //#define IMGUI_UNLIMITED_FRAME_RATE
15 #ifdef _DEBUG
16 #define IMGUI_VULKAN_DEBUG_REPORT
17 #endif
18
19 static VkAllocationCallbacks* g_Allocator = NULL;
20 static VkInstance g_Instance = VK_NULL_HANDLE;
21 static VkPhysicalDevice g_PhysicalDevice = VK_NULL_HANDLE;
22 static VkDevice g_Device = VK_NULL_HANDLE;
23 static uint32_t g_QueueFamily = (uint32_t)-1;
24 static VkQueue g_Queue = VK_NULL_HANDLE;
25 static VkDebugReportCallbackEXT g_DebugReport = VK_NULL_HANDLE;
26 static VkPipelineCache g_PipelineCache = VK_NULL_HANDLE;
27 static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE;
28
29 static ImGui_ImplVulkanH_WindowData g_WindowData;
30 static bool g_ResizeWanted = false;
31 static int g_ResizeWidth = 0, g_ResizeHeight = 0;
32
check_vk_result(VkResult err)33 static void check_vk_result(VkResult err)
34 {
35 if (err == 0) return;
36 printf("VkResult %d\n", err);
37 if (err < 0)
38 abort();
39 }
40
41 #ifdef IMGUI_VULKAN_DEBUG_REPORT
debug_report(VkDebugReportFlagsEXT flags,VkDebugReportObjectTypeEXT objectType,uint64_t object,size_t location,int32_t messageCode,const char * pLayerPrefix,const char * pMessage,void * pUserData)42 static VKAPI_ATTR VkBool32 VKAPI_CALL debug_report(VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT objectType, uint64_t object, size_t location, int32_t messageCode, const char* pLayerPrefix, const char* pMessage, void* pUserData)
43 {
44 (void)flags; (void)object; (void)location; (void)messageCode; (void)pUserData; (void)pLayerPrefix; // Unused arguments
45 fprintf(stderr, "[vulkan] ObjectType: %i\nMessage: %s\n\n", objectType, pMessage);
46 return VK_FALSE;
47 }
48 #endif // IMGUI_VULKAN_DEBUG_REPORT
49
SetupVulkan(const char ** extensions,uint32_t extensions_count)50 static void SetupVulkan(const char** extensions, uint32_t extensions_count)
51 {
52 VkResult err;
53
54 // Create Vulkan Instance
55 {
56 VkInstanceCreateInfo create_info = {};
57 create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
58 create_info.enabledExtensionCount = extensions_count;
59 create_info.ppEnabledExtensionNames = extensions;
60
61 #ifdef IMGUI_VULKAN_DEBUG_REPORT
62 // Enabling multiple validation layers grouped as LunarG standard validation
63 const char* layers[] = { "VK_LAYER_LUNARG_standard_validation" };
64 create_info.enabledLayerCount = 1;
65 create_info.ppEnabledLayerNames = layers;
66
67 // Enable debug report extension (we need additional storage, so we duplicate the user array to add our new extension to it)
68 const char** extensions_ext = (const char**)malloc(sizeof(const char*) * (extensions_count + 1));
69 memcpy(extensions_ext, extensions, extensions_count * sizeof(const char*));
70 extensions_ext[extensions_count] = "VK_EXT_debug_report";
71 create_info.enabledExtensionCount = extensions_count + 1;
72 create_info.ppEnabledExtensionNames = extensions_ext;
73
74 // Create Vulkan Instance
75 err = vkCreateInstance(&create_info, g_Allocator, &g_Instance);
76 check_vk_result(err);
77 free(extensions_ext);
78
79 // Get the function pointer (required for any extensions)
80 auto vkCreateDebugReportCallbackEXT = (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(g_Instance, "vkCreateDebugReportCallbackEXT");
81 IM_ASSERT(vkCreateDebugReportCallbackEXT != NULL);
82
83 // Setup the debug report callback
84 VkDebugReportCallbackCreateInfoEXT debug_report_ci = {};
85 debug_report_ci.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT;
86 debug_report_ci.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT | VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
87 debug_report_ci.pfnCallback = debug_report;
88 debug_report_ci.pUserData = NULL;
89 err = vkCreateDebugReportCallbackEXT(g_Instance, &debug_report_ci, g_Allocator, &g_DebugReport);
90 check_vk_result(err);
91 #else
92 // Create Vulkan Instance without any debug feature
93 err = vkCreateInstance(&create_info, g_Allocator, &g_Instance);
94 check_vk_result(err);
95 #endif
96 }
97
98 // Select GPU
99 {
100 uint32_t gpu_count;
101 err = vkEnumeratePhysicalDevices(g_Instance, &gpu_count, NULL);
102 check_vk_result(err);
103
104 VkPhysicalDevice* gpus = (VkPhysicalDevice*)malloc(sizeof(VkPhysicalDevice) * gpu_count);
105 err = vkEnumeratePhysicalDevices(g_Instance, &gpu_count, gpus);
106 check_vk_result(err);
107
108 // If a number >1 of GPUs got reported, you should find the best fit GPU for your purpose
109 // e.g. VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU if available, or with the greatest memory available, etc.
110 // for sake of simplicity we'll just take the first one, assuming it has a graphics queue family.
111 g_PhysicalDevice = gpus[0];
112 free(gpus);
113 }
114
115 // Select graphics queue family
116 {
117 uint32_t count;
118 vkGetPhysicalDeviceQueueFamilyProperties(g_PhysicalDevice, &count, NULL);
119 VkQueueFamilyProperties* queues = (VkQueueFamilyProperties*)malloc(sizeof(VkQueueFamilyProperties) * count);
120 vkGetPhysicalDeviceQueueFamilyProperties(g_PhysicalDevice, &count, queues);
121 for (uint32_t i = 0; i < count; i++)
122 if (queues[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
123 {
124 g_QueueFamily = i;
125 break;
126 }
127 free(queues);
128 IM_ASSERT(g_QueueFamily != -1);
129 }
130
131 // Create Logical Device (with 1 queue)
132 {
133 int device_extension_count = 1;
134 const char* device_extensions[] = { "VK_KHR_swapchain" };
135 const float queue_priority[] = { 1.0f };
136 VkDeviceQueueCreateInfo queue_info[1] = {};
137 queue_info[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
138 queue_info[0].queueFamilyIndex = g_QueueFamily;
139 queue_info[0].queueCount = 1;
140 queue_info[0].pQueuePriorities = queue_priority;
141 VkDeviceCreateInfo create_info = {};
142 create_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
143 create_info.queueCreateInfoCount = sizeof(queue_info) / sizeof(queue_info[0]);
144 create_info.pQueueCreateInfos = queue_info;
145 create_info.enabledExtensionCount = device_extension_count;
146 create_info.ppEnabledExtensionNames = device_extensions;
147 err = vkCreateDevice(g_PhysicalDevice, &create_info, g_Allocator, &g_Device);
148 check_vk_result(err);
149 vkGetDeviceQueue(g_Device, g_QueueFamily, 0, &g_Queue);
150 }
151
152 // Create Descriptor Pool
153 {
154 VkDescriptorPoolSize pool_sizes[] =
155 {
156 { VK_DESCRIPTOR_TYPE_SAMPLER, 1000 },
157 { VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1000 },
158 { VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1000 },
159 { VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1000 },
160 { VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1000 },
161 { VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1000 },
162 { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1000 },
163 { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1000 },
164 { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1000 },
165 { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1000 },
166 { VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 1000 }
167 };
168 VkDescriptorPoolCreateInfo pool_info = {};
169 pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
170 pool_info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
171 pool_info.maxSets = 1000 * IM_ARRAYSIZE(pool_sizes);
172 pool_info.poolSizeCount = (uint32_t)IM_ARRAYSIZE(pool_sizes);
173 pool_info.pPoolSizes = pool_sizes;
174 err = vkCreateDescriptorPool(g_Device, &pool_info, g_Allocator, &g_DescriptorPool);
175 check_vk_result(err);
176 }
177 }
178
SetupVulkanWindowData(ImGui_ImplVulkanH_WindowData * wd,VkSurfaceKHR surface,int width,int height)179 static void SetupVulkanWindowData(ImGui_ImplVulkanH_WindowData* wd, VkSurfaceKHR surface, int width, int height)
180 {
181 wd->Surface = surface;
182
183 // Check for WSI support
184 VkBool32 res;
185 vkGetPhysicalDeviceSurfaceSupportKHR(g_PhysicalDevice, g_QueueFamily, wd->Surface, &res);
186 if (res != VK_TRUE)
187 {
188 fprintf(stderr, "Error no WSI support on physical device 0\n");
189 exit(-1);
190 }
191
192 // Get Surface Format
193 const VkFormat requestSurfaceImageFormat[] = { VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_B8G8R8_UNORM, VK_FORMAT_R8G8B8_UNORM };
194 const VkColorSpaceKHR requestSurfaceColorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
195 wd->SurfaceFormat = ImGui_ImplVulkanH_SelectSurfaceFormat(g_PhysicalDevice, wd->Surface, requestSurfaceImageFormat, (size_t)IM_ARRAYSIZE(requestSurfaceImageFormat), requestSurfaceColorSpace);
196
197 // Get Present Mode
198 #ifdef IMGUI_UNLIMITED_FRAME_RATE
199 VkPresentModeKHR present_modes[] = { VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_FIFO_KHR };
200 #else
201 VkPresentModeKHR present_modes[] = { VK_PRESENT_MODE_FIFO_KHR };
202 #endif
203 wd->PresentMode = ImGui_ImplVulkanH_SelectPresentMode(g_PhysicalDevice, wd->Surface, &present_modes[0], IM_ARRAYSIZE(present_modes));
204 //printf("[vulkan] Selected PresentMode = %d\n", wd->PresentMode);
205
206 // Create SwapChain, RenderPass, Framebuffer, etc.
207 ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, wd, g_Allocator);
208 ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, wd, g_Allocator, width, height);
209 }
210
CleanupVulkan()211 static void CleanupVulkan()
212 {
213 ImGui_ImplVulkanH_WindowData* wd = &g_WindowData;
214 ImGui_ImplVulkanH_DestroyWindowData(g_Instance, g_Device, wd, g_Allocator);
215 vkDestroyDescriptorPool(g_Device, g_DescriptorPool, g_Allocator);
216
217 #ifdef IMGUI_VULKAN_DEBUG_REPORT
218 // Remove the debug report callback
219 auto vkDestroyDebugReportCallbackEXT = (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(g_Instance, "vkDestroyDebugReportCallbackEXT");
220 vkDestroyDebugReportCallbackEXT(g_Instance, g_DebugReport, g_Allocator);
221 #endif // IMGUI_VULKAN_DEBUG_REPORT
222
223 vkDestroyDevice(g_Device, g_Allocator);
224 vkDestroyInstance(g_Instance, g_Allocator);
225 }
226
FrameRender(ImGui_ImplVulkanH_WindowData * wd)227 static void FrameRender(ImGui_ImplVulkanH_WindowData* wd)
228 {
229 VkResult err;
230
231 VkSemaphore& image_acquired_semaphore = wd->Frames[wd->FrameIndex].ImageAcquiredSemaphore;
232 err = vkAcquireNextImageKHR(g_Device, wd->Swapchain, UINT64_MAX, image_acquired_semaphore, VK_NULL_HANDLE, &wd->FrameIndex);
233 check_vk_result(err);
234
235 ImGui_ImplVulkanH_FrameData* fd = &wd->Frames[wd->FrameIndex];
236 {
237 err = vkWaitForFences(g_Device, 1, &fd->Fence, VK_TRUE, UINT64_MAX); // wait indefinitely instead of periodically checking
238 check_vk_result(err);
239
240 err = vkResetFences(g_Device, 1, &fd->Fence);
241 check_vk_result(err);
242 }
243 {
244 err = vkResetCommandPool(g_Device, fd->CommandPool, 0);
245 check_vk_result(err);
246 VkCommandBufferBeginInfo info = {};
247 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
248 info.flags |= VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
249 err = vkBeginCommandBuffer(fd->CommandBuffer, &info);
250 check_vk_result(err);
251 }
252 {
253 VkRenderPassBeginInfo info = {};
254 info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
255 info.renderPass = wd->RenderPass;
256 info.framebuffer = wd->Framebuffer[wd->FrameIndex];
257 info.renderArea.extent.width = wd->Width;
258 info.renderArea.extent.height = wd->Height;
259 info.clearValueCount = 1;
260 info.pClearValues = &wd->ClearValue;
261 vkCmdBeginRenderPass(fd->CommandBuffer, &info, VK_SUBPASS_CONTENTS_INLINE);
262 }
263
264 // Record Imgui Draw Data and draw funcs into command buffer
265 ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), fd->CommandBuffer);
266
267 // Submit command buffer
268 vkCmdEndRenderPass(fd->CommandBuffer);
269 {
270 VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
271 VkSubmitInfo info = {};
272 info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
273 info.waitSemaphoreCount = 1;
274 info.pWaitSemaphores = &image_acquired_semaphore;
275 info.pWaitDstStageMask = &wait_stage;
276 info.commandBufferCount = 1;
277 info.pCommandBuffers = &fd->CommandBuffer;
278 info.signalSemaphoreCount = 1;
279 info.pSignalSemaphores = &fd->RenderCompleteSemaphore;
280
281 err = vkEndCommandBuffer(fd->CommandBuffer);
282 check_vk_result(err);
283 err = vkQueueSubmit(g_Queue, 1, &info, fd->Fence);
284 check_vk_result(err);
285 }
286 }
287
FramePresent(ImGui_ImplVulkanH_WindowData * wd)288 static void FramePresent(ImGui_ImplVulkanH_WindowData* wd)
289 {
290 ImGui_ImplVulkanH_FrameData* fd = &wd->Frames[wd->FrameIndex];
291 VkPresentInfoKHR info = {};
292 info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
293 info.waitSemaphoreCount = 1;
294 info.pWaitSemaphores = &fd->RenderCompleteSemaphore;
295 info.swapchainCount = 1;
296 info.pSwapchains = &wd->Swapchain;
297 info.pImageIndices = &wd->FrameIndex;
298 VkResult err = vkQueuePresentKHR(g_Queue, &info);
299 check_vk_result(err);
300 }
301
glfw_error_callback(int error,const char * description)302 static void glfw_error_callback(int error, const char* description)
303 {
304 fprintf(stderr, "Glfw Error %d: %s\n", error, description);
305 }
306
glfw_resize_callback(GLFWwindow *,int w,int h)307 static void glfw_resize_callback(GLFWwindow*, int w, int h)
308 {
309 g_ResizeWanted = true;
310 g_ResizeWidth = w;
311 g_ResizeHeight = h;
312 }
313
main(int,char **)314 int main(int, char**)
315 {
316 // Setup window
317 glfwSetErrorCallback(glfw_error_callback);
318 if (!glfwInit())
319 return 1;
320
321 glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
322 GLFWwindow* window = glfwCreateWindow(1280, 720, "Dear ImGui GLFW+Vulkan example", NULL, NULL);
323
324 // Setup Vulkan
325 if (!glfwVulkanSupported())
326 {
327 printf("GLFW: Vulkan Not Supported\n");
328 return 1;
329 }
330 uint32_t extensions_count = 0;
331 const char** extensions = glfwGetRequiredInstanceExtensions(&extensions_count);
332 SetupVulkan(extensions, extensions_count);
333
334 // Create Window Surface
335 VkSurfaceKHR surface;
336 VkResult err = glfwCreateWindowSurface(g_Instance, window, g_Allocator, &surface);
337 check_vk_result(err);
338
339 // Create Framebuffers
340 int w, h;
341 glfwGetFramebufferSize(window, &w, &h);
342 glfwSetFramebufferSizeCallback(window, glfw_resize_callback);
343 ImGui_ImplVulkanH_WindowData* wd = &g_WindowData;
344 SetupVulkanWindowData(wd, surface, w, h);
345
346 // Setup Dear ImGui binding
347 IMGUI_CHECKVERSION();
348 ImGui::CreateContext();
349 ImGuiIO& io = ImGui::GetIO(); (void)io;
350 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
351 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
352
353 // Setup GLFW binding
354 ImGui_ImplGlfw_InitForVulkan(window, true);
355
356 // Setup Vulkan binding
357 ImGui_ImplVulkan_InitInfo init_info = {};
358 init_info.Instance = g_Instance;
359 init_info.PhysicalDevice = g_PhysicalDevice;
360 init_info.Device = g_Device;
361 init_info.QueueFamily = g_QueueFamily;
362 init_info.Queue = g_Queue;
363 init_info.PipelineCache = g_PipelineCache;
364 init_info.DescriptorPool = g_DescriptorPool;
365 init_info.Allocator = g_Allocator;
366 init_info.CheckVkResultFn = check_vk_result;
367 ImGui_ImplVulkan_Init(&init_info, wd->RenderPass);
368
369 // Setup style
370 ImGui::StyleColorsDark();
371 //ImGui::StyleColorsClassic();
372
373 // Load Fonts
374 // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
375 // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
376 // - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
377 // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
378 // - Read 'misc/fonts/README.txt' for more instructions and details.
379 // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
380 //io.Fonts->AddFontDefault();
381 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
382 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
383 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
384 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
385 //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
386 //IM_ASSERT(font != NULL);
387
388 // Upload Fonts
389 {
390 // Use any command queue
391 VkCommandPool command_pool = wd->Frames[wd->FrameIndex].CommandPool;
392 VkCommandBuffer command_buffer = wd->Frames[wd->FrameIndex].CommandBuffer;
393
394 err = vkResetCommandPool(g_Device, command_pool, 0);
395 check_vk_result(err);
396 VkCommandBufferBeginInfo begin_info = {};
397 begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
398 begin_info.flags |= VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
399 err = vkBeginCommandBuffer(command_buffer, &begin_info);
400 check_vk_result(err);
401
402 ImGui_ImplVulkan_CreateFontsTexture(command_buffer);
403
404 VkSubmitInfo end_info = {};
405 end_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
406 end_info.commandBufferCount = 1;
407 end_info.pCommandBuffers = &command_buffer;
408 err = vkEndCommandBuffer(command_buffer);
409 check_vk_result(err);
410 err = vkQueueSubmit(g_Queue, 1, &end_info, VK_NULL_HANDLE);
411 check_vk_result(err);
412
413 err = vkDeviceWaitIdle(g_Device);
414 check_vk_result(err);
415 ImGui_ImplVulkan_InvalidateFontUploadObjects();
416 }
417
418 bool show_demo_window = true;
419 bool show_another_window = false;
420 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
421
422 // Main loop
423 while (!glfwWindowShouldClose(window))
424 {
425 // Poll and handle events (inputs, window resize, etc.)
426 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
427 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
428 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
429 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
430 glfwPollEvents();
431 if (g_ResizeWanted)
432 {
433 ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, &g_WindowData, g_Allocator, g_ResizeWidth, g_ResizeHeight);
434 g_ResizeWanted = false;
435 }
436
437 // Start the Dear ImGui frame
438 ImGui_ImplVulkan_NewFrame();
439 ImGui_ImplGlfw_NewFrame();
440 ImGui::NewFrame();
441
442 // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
443 if (show_demo_window)
444 ImGui::ShowDemoWindow(&show_demo_window);
445
446 // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
447 {
448 static float f = 0.0f;
449 static int counter = 0;
450
451 ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
452
453 ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
454 ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
455 ImGui::Checkbox("Another Window", &show_another_window);
456
457 ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
458 ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
459
460 if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
461 counter++;
462 ImGui::SameLine();
463 ImGui::Text("counter = %d", counter);
464
465 ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
466 ImGui::End();
467 }
468
469 // 3. Show another simple window.
470 if (show_another_window)
471 {
472 ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
473 ImGui::Text("Hello from another window!");
474 if (ImGui::Button("Close Me"))
475 show_another_window = false;
476 ImGui::End();
477 }
478
479 // Rendering
480 ImGui::Render();
481 memcpy(&wd->ClearValue.color.float32[0], &clear_color, 4 * sizeof(float));
482 FrameRender(wd);
483
484 FramePresent(wd);
485 }
486
487 // Cleanup
488 err = vkDeviceWaitIdle(g_Device);
489 check_vk_result(err);
490 ImGui_ImplVulkan_Shutdown();
491 ImGui_ImplGlfw_Shutdown();
492 ImGui::DestroyContext();
493 CleanupVulkan();
494
495 glfwDestroyWindow(window);
496 glfwTerminate();
497
498 return 0;
499 }
500