main.cpp (24432B)
1 // Dear ImGui: standalone example application for SDL2 + Vulkan 2 // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp. 3 // Read online: https://github.com/ocornut/imgui/tree/master/docs 4 5 // Important note to the reader who wish to integrate imgui_impl_vulkan.cpp/.h in their own engine/app. 6 // - Common ImGui_ImplVulkan_XXX functions and structures are used to interface with imgui_impl_vulkan.cpp/.h. 7 // You will use those if you want to use this rendering backend in your engine/app. 8 // - Helper ImGui_ImplVulkanH_XXX functions and structures are only used by this example (main.cpp) and by 9 // the backend itself (imgui_impl_vulkan.cpp), but should PROBABLY NOT be used by your own engine/app code. 10 // Read comments in imgui_impl_vulkan.h. 11 12 #include "imgui.h" 13 #include "imgui_impl_sdl.h" 14 #include "imgui_impl_vulkan.h" 15 #include <stdio.h> // printf, fprintf 16 #include <stdlib.h> // abort 17 #include <SDL.h> 18 #include <SDL_vulkan.h> 19 #include <vulkan/vulkan.h> 20 21 //#define IMGUI_UNLIMITED_FRAME_RATE 22 #ifdef _DEBUG 23 #define IMGUI_VULKAN_DEBUG_REPORT 24 #endif 25 26 static VkAllocationCallbacks* g_Allocator = NULL; 27 static VkInstance g_Instance = VK_NULL_HANDLE; 28 static VkPhysicalDevice g_PhysicalDevice = VK_NULL_HANDLE; 29 static VkDevice g_Device = VK_NULL_HANDLE; 30 static uint32_t g_QueueFamily = (uint32_t)-1; 31 static VkQueue g_Queue = VK_NULL_HANDLE; 32 static VkDebugReportCallbackEXT g_DebugReport = VK_NULL_HANDLE; 33 static VkPipelineCache g_PipelineCache = VK_NULL_HANDLE; 34 static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE; 35 36 static ImGui_ImplVulkanH_Window g_MainWindowData; 37 static uint32_t g_MinImageCount = 2; 38 static bool g_SwapChainRebuild = false; 39 40 static void check_vk_result(VkResult err) 41 { 42 if (err == 0) 43 return; 44 fprintf(stderr, "[vulkan] Error: VkResult = %d\n", err); 45 if (err < 0) 46 abort(); 47 } 48 49 #ifdef IMGUI_VULKAN_DEBUG_REPORT 50 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) 51 { 52 (void)flags; (void)object; (void)location; (void)messageCode; (void)pUserData; (void)pLayerPrefix; // Unused arguments 53 fprintf(stderr, "[vulkan] Debug report from ObjectType: %i\nMessage: %s\n\n", objectType, pMessage); 54 return VK_FALSE; 55 } 56 #endif // IMGUI_VULKAN_DEBUG_REPORT 57 58 static void SetupVulkan(const char** extensions, uint32_t extensions_count) 59 { 60 VkResult err; 61 62 // Create Vulkan Instance 63 { 64 VkInstanceCreateInfo create_info = {}; 65 create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; 66 create_info.enabledExtensionCount = extensions_count; 67 create_info.ppEnabledExtensionNames = extensions; 68 #ifdef IMGUI_VULKAN_DEBUG_REPORT 69 // Enabling validation layers 70 const char* layers[] = { "VK_LAYER_KHRONOS_validation" }; 71 create_info.enabledLayerCount = 1; 72 create_info.ppEnabledLayerNames = layers; 73 74 // Enable debug report extension (we need additional storage, so we duplicate the user array to add our new extension to it) 75 const char** extensions_ext = (const char**)malloc(sizeof(const char*) * (extensions_count + 1)); 76 memcpy(extensions_ext, extensions, extensions_count * sizeof(const char*)); 77 extensions_ext[extensions_count] = "VK_EXT_debug_report"; 78 create_info.enabledExtensionCount = extensions_count + 1; 79 create_info.ppEnabledExtensionNames = extensions_ext; 80 81 // Create Vulkan Instance 82 err = vkCreateInstance(&create_info, g_Allocator, &g_Instance); 83 check_vk_result(err); 84 free(extensions_ext); 85 86 // Get the function pointer (required for any extensions) 87 auto vkCreateDebugReportCallbackEXT = (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(g_Instance, "vkCreateDebugReportCallbackEXT"); 88 IM_ASSERT(vkCreateDebugReportCallbackEXT != NULL); 89 90 // Setup the debug report callback 91 VkDebugReportCallbackCreateInfoEXT debug_report_ci = {}; 92 debug_report_ci.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT; 93 debug_report_ci.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT | VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT; 94 debug_report_ci.pfnCallback = debug_report; 95 debug_report_ci.pUserData = NULL; 96 err = vkCreateDebugReportCallbackEXT(g_Instance, &debug_report_ci, g_Allocator, &g_DebugReport); 97 check_vk_result(err); 98 #else 99 // Create Vulkan Instance without any debug feature 100 err = vkCreateInstance(&create_info, g_Allocator, &g_Instance); 101 check_vk_result(err); 102 IM_UNUSED(g_DebugReport); 103 #endif 104 } 105 106 // Select GPU 107 { 108 uint32_t gpu_count; 109 err = vkEnumeratePhysicalDevices(g_Instance, &gpu_count, NULL); 110 check_vk_result(err); 111 IM_ASSERT(gpu_count > 0); 112 113 VkPhysicalDevice* gpus = (VkPhysicalDevice*)malloc(sizeof(VkPhysicalDevice) * gpu_count); 114 err = vkEnumeratePhysicalDevices(g_Instance, &gpu_count, gpus); 115 check_vk_result(err); 116 117 // If a number >1 of GPUs got reported, find discrete GPU if present, or use first one available. This covers 118 // most common cases (multi-gpu/integrated+dedicated graphics). Handling more complicated setups (multiple 119 // dedicated GPUs) is out of scope of this sample. 120 int use_gpu = 0; 121 for (int i = 0; i < (int)gpu_count; i++) 122 { 123 VkPhysicalDeviceProperties properties; 124 vkGetPhysicalDeviceProperties(gpus[i], &properties); 125 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) 126 { 127 use_gpu = i; 128 break; 129 } 130 } 131 132 g_PhysicalDevice = gpus[use_gpu]; 133 free(gpus); 134 } 135 136 // Select graphics queue family 137 { 138 uint32_t count; 139 vkGetPhysicalDeviceQueueFamilyProperties(g_PhysicalDevice, &count, NULL); 140 VkQueueFamilyProperties* queues = (VkQueueFamilyProperties*)malloc(sizeof(VkQueueFamilyProperties) * count); 141 vkGetPhysicalDeviceQueueFamilyProperties(g_PhysicalDevice, &count, queues); 142 for (uint32_t i = 0; i < count; i++) 143 if (queues[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) 144 { 145 g_QueueFamily = i; 146 break; 147 } 148 free(queues); 149 IM_ASSERT(g_QueueFamily != (uint32_t)-1); 150 } 151 152 // Create Logical Device (with 1 queue) 153 { 154 int device_extension_count = 1; 155 const char* device_extensions[] = { "VK_KHR_swapchain" }; 156 const float queue_priority[] = { 1.0f }; 157 VkDeviceQueueCreateInfo queue_info[1] = {}; 158 queue_info[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; 159 queue_info[0].queueFamilyIndex = g_QueueFamily; 160 queue_info[0].queueCount = 1; 161 queue_info[0].pQueuePriorities = queue_priority; 162 VkDeviceCreateInfo create_info = {}; 163 create_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; 164 create_info.queueCreateInfoCount = sizeof(queue_info) / sizeof(queue_info[0]); 165 create_info.pQueueCreateInfos = queue_info; 166 create_info.enabledExtensionCount = device_extension_count; 167 create_info.ppEnabledExtensionNames = device_extensions; 168 err = vkCreateDevice(g_PhysicalDevice, &create_info, g_Allocator, &g_Device); 169 check_vk_result(err); 170 vkGetDeviceQueue(g_Device, g_QueueFamily, 0, &g_Queue); 171 } 172 173 // Create Descriptor Pool 174 { 175 VkDescriptorPoolSize pool_sizes[] = 176 { 177 { VK_DESCRIPTOR_TYPE_SAMPLER, 1000 }, 178 { VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1000 }, 179 { VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1000 }, 180 { VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1000 }, 181 { VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1000 }, 182 { VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1000 }, 183 { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1000 }, 184 { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1000 }, 185 { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1000 }, 186 { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1000 }, 187 { VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 1000 } 188 }; 189 VkDescriptorPoolCreateInfo pool_info = {}; 190 pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; 191 pool_info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT; 192 pool_info.maxSets = 1000 * IM_ARRAYSIZE(pool_sizes); 193 pool_info.poolSizeCount = (uint32_t)IM_ARRAYSIZE(pool_sizes); 194 pool_info.pPoolSizes = pool_sizes; 195 err = vkCreateDescriptorPool(g_Device, &pool_info, g_Allocator, &g_DescriptorPool); 196 check_vk_result(err); 197 } 198 } 199 200 // All the ImGui_ImplVulkanH_XXX structures/functions are optional helpers used by the demo. 201 // Your real engine/app may not use them. 202 static void SetupVulkanWindow(ImGui_ImplVulkanH_Window* wd, VkSurfaceKHR surface, int width, int height) 203 { 204 wd->Surface = surface; 205 206 // Check for WSI support 207 VkBool32 res; 208 vkGetPhysicalDeviceSurfaceSupportKHR(g_PhysicalDevice, g_QueueFamily, wd->Surface, &res); 209 if (res != VK_TRUE) 210 { 211 fprintf(stderr, "Error no WSI support on physical device 0\n"); 212 exit(-1); 213 } 214 215 // Select Surface Format 216 const VkFormat requestSurfaceImageFormat[] = { VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_B8G8R8_UNORM, VK_FORMAT_R8G8B8_UNORM }; 217 const VkColorSpaceKHR requestSurfaceColorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR; 218 wd->SurfaceFormat = ImGui_ImplVulkanH_SelectSurfaceFormat(g_PhysicalDevice, wd->Surface, requestSurfaceImageFormat, (size_t)IM_ARRAYSIZE(requestSurfaceImageFormat), requestSurfaceColorSpace); 219 220 // Select Present Mode 221 #ifdef IMGUI_UNLIMITED_FRAME_RATE 222 VkPresentModeKHR present_modes[] = { VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_FIFO_KHR }; 223 #else 224 VkPresentModeKHR present_modes[] = { VK_PRESENT_MODE_FIFO_KHR }; 225 #endif 226 wd->PresentMode = ImGui_ImplVulkanH_SelectPresentMode(g_PhysicalDevice, wd->Surface, &present_modes[0], IM_ARRAYSIZE(present_modes)); 227 //printf("[vulkan] Selected PresentMode = %d\n", wd->PresentMode); 228 229 // Create SwapChain, RenderPass, Framebuffer, etc. 230 IM_ASSERT(g_MinImageCount >= 2); 231 ImGui_ImplVulkanH_CreateOrResizeWindow(g_Instance, g_PhysicalDevice, g_Device, wd, g_QueueFamily, g_Allocator, width, height, g_MinImageCount); 232 } 233 234 static void CleanupVulkan() 235 { 236 vkDestroyDescriptorPool(g_Device, g_DescriptorPool, g_Allocator); 237 238 #ifdef IMGUI_VULKAN_DEBUG_REPORT 239 // Remove the debug report callback 240 auto vkDestroyDebugReportCallbackEXT = (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(g_Instance, "vkDestroyDebugReportCallbackEXT"); 241 vkDestroyDebugReportCallbackEXT(g_Instance, g_DebugReport, g_Allocator); 242 #endif // IMGUI_VULKAN_DEBUG_REPORT 243 244 vkDestroyDevice(g_Device, g_Allocator); 245 vkDestroyInstance(g_Instance, g_Allocator); 246 } 247 248 static void CleanupVulkanWindow() 249 { 250 ImGui_ImplVulkanH_DestroyWindow(g_Instance, g_Device, &g_MainWindowData, g_Allocator); 251 } 252 253 static void FrameRender(ImGui_ImplVulkanH_Window* wd, ImDrawData* draw_data) 254 { 255 VkResult err; 256 257 VkSemaphore image_acquired_semaphore = wd->FrameSemaphores[wd->SemaphoreIndex].ImageAcquiredSemaphore; 258 VkSemaphore render_complete_semaphore = wd->FrameSemaphores[wd->SemaphoreIndex].RenderCompleteSemaphore; 259 err = vkAcquireNextImageKHR(g_Device, wd->Swapchain, UINT64_MAX, image_acquired_semaphore, VK_NULL_HANDLE, &wd->FrameIndex); 260 if (err == VK_ERROR_OUT_OF_DATE_KHR || err == VK_SUBOPTIMAL_KHR) 261 { 262 g_SwapChainRebuild = true; 263 return; 264 } 265 check_vk_result(err); 266 267 ImGui_ImplVulkanH_Frame* fd = &wd->Frames[wd->FrameIndex]; 268 { 269 err = vkWaitForFences(g_Device, 1, &fd->Fence, VK_TRUE, UINT64_MAX); // wait indefinitely instead of periodically checking 270 check_vk_result(err); 271 272 err = vkResetFences(g_Device, 1, &fd->Fence); 273 check_vk_result(err); 274 } 275 { 276 err = vkResetCommandPool(g_Device, fd->CommandPool, 0); 277 check_vk_result(err); 278 VkCommandBufferBeginInfo info = {}; 279 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; 280 info.flags |= VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; 281 err = vkBeginCommandBuffer(fd->CommandBuffer, &info); 282 check_vk_result(err); 283 } 284 { 285 VkRenderPassBeginInfo info = {}; 286 info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; 287 info.renderPass = wd->RenderPass; 288 info.framebuffer = fd->Framebuffer; 289 info.renderArea.extent.width = wd->Width; 290 info.renderArea.extent.height = wd->Height; 291 info.clearValueCount = 1; 292 info.pClearValues = &wd->ClearValue; 293 vkCmdBeginRenderPass(fd->CommandBuffer, &info, VK_SUBPASS_CONTENTS_INLINE); 294 } 295 296 // Record dear imgui primitives into command buffer 297 ImGui_ImplVulkan_RenderDrawData(draw_data, fd->CommandBuffer); 298 299 // Submit command buffer 300 vkCmdEndRenderPass(fd->CommandBuffer); 301 { 302 VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; 303 VkSubmitInfo info = {}; 304 info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; 305 info.waitSemaphoreCount = 1; 306 info.pWaitSemaphores = &image_acquired_semaphore; 307 info.pWaitDstStageMask = &wait_stage; 308 info.commandBufferCount = 1; 309 info.pCommandBuffers = &fd->CommandBuffer; 310 info.signalSemaphoreCount = 1; 311 info.pSignalSemaphores = &render_complete_semaphore; 312 313 err = vkEndCommandBuffer(fd->CommandBuffer); 314 check_vk_result(err); 315 err = vkQueueSubmit(g_Queue, 1, &info, fd->Fence); 316 check_vk_result(err); 317 } 318 } 319 320 static void FramePresent(ImGui_ImplVulkanH_Window* wd) 321 { 322 if (g_SwapChainRebuild) 323 return; 324 VkSemaphore render_complete_semaphore = wd->FrameSemaphores[wd->SemaphoreIndex].RenderCompleteSemaphore; 325 VkPresentInfoKHR info = {}; 326 info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; 327 info.waitSemaphoreCount = 1; 328 info.pWaitSemaphores = &render_complete_semaphore; 329 info.swapchainCount = 1; 330 info.pSwapchains = &wd->Swapchain; 331 info.pImageIndices = &wd->FrameIndex; 332 VkResult err = vkQueuePresentKHR(g_Queue, &info); 333 if (err == VK_ERROR_OUT_OF_DATE_KHR || err == VK_SUBOPTIMAL_KHR) 334 { 335 g_SwapChainRebuild = true; 336 return; 337 } 338 check_vk_result(err); 339 wd->SemaphoreIndex = (wd->SemaphoreIndex + 1) % wd->ImageCount; // Now we can use the next set of semaphores 340 } 341 342 int main(int, char**) 343 { 344 // Setup SDL 345 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) 346 { 347 printf("Error: %s\n", SDL_GetError()); 348 return -1; 349 } 350 351 // Setup window 352 SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI); 353 SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+Vulkan example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags); 354 355 // Setup Vulkan 356 uint32_t extensions_count = 0; 357 SDL_Vulkan_GetInstanceExtensions(window, &extensions_count, NULL); 358 const char** extensions = new const char*[extensions_count]; 359 SDL_Vulkan_GetInstanceExtensions(window, &extensions_count, extensions); 360 SetupVulkan(extensions, extensions_count); 361 delete[] extensions; 362 363 // Create Window Surface 364 VkSurfaceKHR surface; 365 VkResult err; 366 if (SDL_Vulkan_CreateSurface(window, g_Instance, &surface) == 0) 367 { 368 printf("Failed to create Vulkan surface.\n"); 369 return 1; 370 } 371 372 // Create Framebuffers 373 int w, h; 374 SDL_GetWindowSize(window, &w, &h); 375 ImGui_ImplVulkanH_Window* wd = &g_MainWindowData; 376 SetupVulkanWindow(wd, surface, w, h); 377 378 // Setup Dear ImGui context 379 IMGUI_CHECKVERSION(); 380 ImGui::CreateContext(); 381 ImGuiIO& io = ImGui::GetIO(); (void)io; 382 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls 383 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls 384 385 // Setup Dear ImGui style 386 ImGui::StyleColorsDark(); 387 //ImGui::StyleColorsClassic(); 388 389 // Setup Platform/Renderer backends 390 ImGui_ImplSDL2_InitForVulkan(window); 391 ImGui_ImplVulkan_InitInfo init_info = {}; 392 init_info.Instance = g_Instance; 393 init_info.PhysicalDevice = g_PhysicalDevice; 394 init_info.Device = g_Device; 395 init_info.QueueFamily = g_QueueFamily; 396 init_info.Queue = g_Queue; 397 init_info.PipelineCache = g_PipelineCache; 398 init_info.DescriptorPool = g_DescriptorPool; 399 init_info.Allocator = g_Allocator; 400 init_info.MinImageCount = g_MinImageCount; 401 init_info.ImageCount = wd->ImageCount; 402 init_info.CheckVkResultFn = check_vk_result; 403 ImGui_ImplVulkan_Init(&init_info, wd->RenderPass); 404 405 // Load Fonts 406 // - 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. 407 // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple. 408 // - 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). 409 // - 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. 410 // - Read 'docs/FONTS.md' for more instructions and details. 411 // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ ! 412 //io.Fonts->AddFontDefault(); 413 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f); 414 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f); 415 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f); 416 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f); 417 //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese()); 418 //IM_ASSERT(font != NULL); 419 420 // Upload Fonts 421 { 422 // Use any command queue 423 VkCommandPool command_pool = wd->Frames[wd->FrameIndex].CommandPool; 424 VkCommandBuffer command_buffer = wd->Frames[wd->FrameIndex].CommandBuffer; 425 426 err = vkResetCommandPool(g_Device, command_pool, 0); 427 check_vk_result(err); 428 VkCommandBufferBeginInfo begin_info = {}; 429 begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; 430 begin_info.flags |= VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; 431 err = vkBeginCommandBuffer(command_buffer, &begin_info); 432 check_vk_result(err); 433 434 ImGui_ImplVulkan_CreateFontsTexture(command_buffer); 435 436 VkSubmitInfo end_info = {}; 437 end_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; 438 end_info.commandBufferCount = 1; 439 end_info.pCommandBuffers = &command_buffer; 440 err = vkEndCommandBuffer(command_buffer); 441 check_vk_result(err); 442 err = vkQueueSubmit(g_Queue, 1, &end_info, VK_NULL_HANDLE); 443 check_vk_result(err); 444 445 err = vkDeviceWaitIdle(g_Device); 446 check_vk_result(err); 447 ImGui_ImplVulkan_DestroyFontUploadObjects(); 448 } 449 450 // Our state 451 bool show_demo_window = true; 452 bool show_another_window = false; 453 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); 454 455 // Main loop 456 bool done = false; 457 while (!done) 458 { 459 // Poll and handle events (inputs, window resize, etc.) 460 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. 461 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application. 462 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application. 463 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. 464 SDL_Event event; 465 while (SDL_PollEvent(&event)) 466 { 467 ImGui_ImplSDL2_ProcessEvent(&event); 468 if (event.type == SDL_QUIT) 469 done = true; 470 if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window)) 471 done = true; 472 } 473 474 // Resize swap chain? 475 if (g_SwapChainRebuild) 476 { 477 int width, height; 478 SDL_GetWindowSize(window, &width, &height); 479 if (width > 0 && height > 0) 480 { 481 ImGui_ImplVulkan_SetMinImageCount(g_MinImageCount); 482 ImGui_ImplVulkanH_CreateOrResizeWindow(g_Instance, g_PhysicalDevice, g_Device, &g_MainWindowData, g_QueueFamily, g_Allocator, width, height, g_MinImageCount); 483 g_MainWindowData.FrameIndex = 0; 484 g_SwapChainRebuild = false; 485 } 486 } 487 488 // Start the Dear ImGui frame 489 ImGui_ImplVulkan_NewFrame(); 490 ImGui_ImplSDL2_NewFrame(window); 491 ImGui::NewFrame(); 492 493 // 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!). 494 if (show_demo_window) 495 ImGui::ShowDemoWindow(&show_demo_window); 496 497 // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window. 498 { 499 static float f = 0.0f; 500 static int counter = 0; 501 502 ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it. 503 504 ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too) 505 ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state 506 ImGui::Checkbox("Another Window", &show_another_window); 507 508 ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f 509 ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color 510 511 if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated) 512 counter++; 513 ImGui::SameLine(); 514 ImGui::Text("counter = %d", counter); 515 516 ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); 517 ImGui::End(); 518 } 519 520 // 3. Show another simple window. 521 if (show_another_window) 522 { 523 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) 524 ImGui::Text("Hello from another window!"); 525 if (ImGui::Button("Close Me")) 526 show_another_window = false; 527 ImGui::End(); 528 } 529 530 // Rendering 531 ImGui::Render(); 532 ImDrawData* draw_data = ImGui::GetDrawData(); 533 const bool is_minimized = (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f); 534 if (!is_minimized) 535 { 536 wd->ClearValue.color.float32[0] = clear_color.x * clear_color.w; 537 wd->ClearValue.color.float32[1] = clear_color.y * clear_color.w; 538 wd->ClearValue.color.float32[2] = clear_color.z * clear_color.w; 539 wd->ClearValue.color.float32[3] = clear_color.w; 540 FrameRender(wd, draw_data); 541 FramePresent(wd); 542 } 543 } 544 545 // Cleanup 546 err = vkDeviceWaitIdle(g_Device); 547 check_vk_result(err); 548 ImGui_ImplVulkan_Shutdown(); 549 ImGui_ImplSDL2_Shutdown(); 550 ImGui::DestroyContext(); 551 552 CleanupVulkanWindow(); 553 CleanupVulkan(); 554 555 SDL_DestroyWindow(window); 556 SDL_Quit(); 557 558 return 0; 559 }