| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | #include "imgui.h" |
| 17 | #include "imgui_impl_win32.h" |
| 18 | #define VK_USE_PLATFORM_WIN32_KHR |
| 19 | #include "imgui_impl_vulkan.h" |
| 20 | #include <windows.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <tchar.h> |
| 24 | |
| 25 | |
| 26 | #ifdef IMGUI_IMPL_VULKAN_USE_VOLK |
| 27 | #define VOLK_IMPLEMENTATION |
| 28 | #include <volk.h> |
| 29 | #endif |
| 30 | |
| 31 | |
| 32 | #ifdef _DEBUG |
| 33 | #define APP_USE_VULKAN_DEBUG_REPORT |
| 34 | #endif |
| 35 | |
| 36 | |
| 37 | static VkAllocationCallbacks* g_Allocator = nullptr; |
| 38 | static VkInstance g_Instance = VK_NULL_HANDLE; |
| 39 | static VkPhysicalDevice g_PhysicalDevice = VK_NULL_HANDLE; |
| 40 | static VkDevice g_Device = VK_NULL_HANDLE; |
| 41 | static uint32_t g_QueueFamily = (uint32_t)-1; |
| 42 | static VkQueue g_Queue = VK_NULL_HANDLE; |
| 43 | static VkDebugReportCallbackEXT g_DebugReport = VK_NULL_HANDLE; |
| 44 | static VkPipelineCache g_PipelineCache = VK_NULL_HANDLE; |
| 45 | static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE; |
| 46 | |
| 47 | static ImGui_ImplVulkanH_Window g_MainWindowData; |
| 48 | static uint32_t g_MinImageCount = 2; |
| 49 | static bool g_SwapChainRebuild = false; |
| 50 | |
| 51 | static void check_vk_result(VkResult err) |
| 52 | { |
| 53 | if (err == VK_SUCCESS) |
| 54 | return; |
| 55 | fprintf(stderr, "[vulkan] Error: VkResult = %d\n", err); |
| 56 | if (err < 0) |
| 57 | abort(); |
| 58 | } |
| 59 | |
| 60 | #ifdef APP_USE_VULKAN_DEBUG_REPORT |
| 61 | 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) |
| 62 | { |
| 63 | (void)flags; (void)object; (void)location; (void)messageCode; (void)pUserData; (void)pLayerPrefix; |
| 64 | fprintf(stderr, "[vulkan] Debug report from ObjectType: %i\nMessage: %s\n\n", objectType, pMessage); |
| 65 | return VK_FALSE; |
| 66 | } |
| 67 | #endif |
| 68 | |
| 69 | static bool IsExtensionAvailable(const ImVector<VkExtensionProperties>& properties, const char* extension) |
| 70 | { |
| 71 | for (const VkExtensionProperties& p : properties) |
| 72 | if (strcmp(p.extensionName, extension) == 0) |
| 73 | return true; |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | static void SetupVulkan(ImVector<const char*> instance_extensions) |
| 78 | { |
| 79 | VkResult err; |
| 80 | #ifdef IMGUI_IMPL_VULKAN_USE_VOLK |
| 81 | volkInitialize(); |
| 82 | #endif |
| 83 | |
| 84 | |
| 85 | { |
| 86 | VkInstanceCreateInfo create_info = {}; |
| 87 | create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; |
| 88 | |
| 89 | |
| 90 | uint32_t properties_count; |
| 91 | ImVector<VkExtensionProperties> properties; |
| 92 | vkEnumerateInstanceExtensionProperties(nullptr, &properties_count, nullptr); |
| 93 | properties.resize(properties_count); |
| 94 | err = vkEnumerateInstanceExtensionProperties(nullptr, &properties_count, properties.Data); |
| 95 | check_vk_result(err); |
| 96 | |
| 97 | |
| 98 | if (IsExtensionAvailable(properties, VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME)) |
| 99 | instance_extensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME); |
| 100 | #ifdef VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME |
| 101 | if (IsExtensionAvailable(properties, VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME)) |
| 102 | { |
| 103 | instance_extensions.push_back(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME); |
| 104 | create_info.flags |= VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR; |
| 105 | } |
| 106 | #endif |
| 107 | |
| 108 | |
| 109 | #ifdef APP_USE_VULKAN_DEBUG_REPORT |
| 110 | const char* layers[] = { "VK_LAYER_KHRONOS_validation" }; |
| 111 | create_info.enabledLayerCount = 1; |
| 112 | create_info.ppEnabledLayerNames = layers; |
| 113 | instance_extensions.push_back("VK_EXT_debug_report"); |
| 114 | #endif |
| 115 | |
| 116 | |
| 117 | create_info.enabledExtensionCount = (uint32_t)instance_extensions.Size; |
| 118 | create_info.ppEnabledExtensionNames = instance_extensions.Data; |
| 119 | err = vkCreateInstance(&create_info, g_Allocator, &g_Instance); |
| 120 | check_vk_result(err); |
| 121 | #ifdef IMGUI_IMPL_VULKAN_USE_VOLK |
| 122 | volkLoadInstance(g_Instance); |
| 123 | #endif |
| 124 | |
| 125 | |
| 126 | #ifdef APP_USE_VULKAN_DEBUG_REPORT |
| 127 | auto f_vkCreateDebugReportCallbackEXT = (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(g_Instance, "vkCreateDebugReportCallbackEXT"); |
| 128 | IM_ASSERT(f_vkCreateDebugReportCallbackEXT != nullptr); |
| 129 | VkDebugReportCallbackCreateInfoEXT debug_report_ci = {}; |
| 130 | debug_report_ci.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT; |
| 131 | debug_report_ci.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT | VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT; |
| 132 | debug_report_ci.pfnCallback = debug_report; |
| 133 | debug_report_ci.pUserData = nullptr; |
| 134 | err = f_vkCreateDebugReportCallbackEXT(g_Instance, &debug_report_ci, g_Allocator, &g_DebugReport); |
| 135 | check_vk_result(err); |
| 136 | #endif |
| 137 | } |
| 138 | |
| 139 | |
| 140 | g_PhysicalDevice = ImGui_ImplVulkanH_SelectPhysicalDevice(g_Instance); |
| 141 | IM_ASSERT(g_PhysicalDevice != VK_NULL_HANDLE); |
| 142 | |
| 143 | |
| 144 | g_QueueFamily = ImGui_ImplVulkanH_SelectQueueFamilyIndex(g_PhysicalDevice); |
| 145 | IM_ASSERT(g_QueueFamily != (uint32_t)-1); |
| 146 | |
| 147 | |
| 148 | { |
| 149 | ImVector<const char*> device_extensions; |
| 150 | device_extensions.push_back("VK_KHR_swapchain"); |
| 151 | |
| 152 | |
| 153 | uint32_t properties_count; |
| 154 | ImVector<VkExtensionProperties> properties; |
| 155 | vkEnumerateDeviceExtensionProperties(g_PhysicalDevice, nullptr, &properties_count, nullptr); |
| 156 | properties.resize(properties_count); |
| 157 | vkEnumerateDeviceExtensionProperties(g_PhysicalDevice, nullptr, &properties_count, properties.Data); |
| 158 | #ifdef VK_KHR_PORTABILITY_SUBSET_EXTENSION_NAME |
| 159 | if (IsExtensionAvailable(properties, VK_KHR_PORTABILITY_SUBSET_EXTENSION_NAME)) |
| 160 | device_extensions.push_back(VK_KHR_PORTABILITY_SUBSET_EXTENSION_NAME); |
| 161 | #endif |
| 162 | |
| 163 | const float queue_priority[] = { 1.0f }; |
| 164 | VkDeviceQueueCreateInfo queue_info[1] = {}; |
| 165 | queue_info[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; |
| 166 | queue_info[0].queueFamilyIndex = g_QueueFamily; |
| 167 | queue_info[0].queueCount = 1; |
| 168 | queue_info[0].pQueuePriorities = queue_priority; |
| 169 | VkDeviceCreateInfo create_info = {}; |
| 170 | create_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; |
| 171 | create_info.queueCreateInfoCount = sizeof(queue_info) / sizeof(queue_info[0]); |
| 172 | create_info.pQueueCreateInfos = queue_info; |
| 173 | create_info.enabledExtensionCount = (uint32_t)device_extensions.Size; |
| 174 | create_info.ppEnabledExtensionNames = device_extensions.Data; |
| 175 | err = vkCreateDevice(g_PhysicalDevice, &create_info, g_Allocator, &g_Device); |
| 176 | check_vk_result(err); |
| 177 | vkGetDeviceQueue(g_Device, g_QueueFamily, 0, &g_Queue); |
| 178 | } |
| 179 | |
| 180 | |
| 181 | |
| 182 | { |
| 183 | VkDescriptorPoolSize pool_sizes[] = |
| 184 | { |
| 185 | { VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, IMGUI_IMPL_VULKAN_MINIMUM_SAMPLED_IMAGE_POOL_SIZE }, |
| 186 | { VK_DESCRIPTOR_TYPE_SAMPLER, IMGUI_IMPL_VULKAN_MINIMUM_SAMPLER_POOL_SIZE }, |
| 187 | }; |
| 188 | VkDescriptorPoolCreateInfo pool_info = {}; |
| 189 | pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; |
| 190 | pool_info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT; |
| 191 | pool_info.maxSets = 0; |
| 192 | for (VkDescriptorPoolSize& pool_size : pool_sizes) |
| 193 | pool_info.maxSets += pool_size.descriptorCount; |
| 194 | pool_info.poolSizeCount = (uint32_t)IM_COUNTOF(pool_sizes); |
| 195 | pool_info.pPoolSizes = pool_sizes; |
| 196 | err = vkCreateDescriptorPool(g_Device, &pool_info, g_Allocator, &g_DescriptorPool); |
| 197 | check_vk_result(err); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | |
| 202 | |
| 203 | static void SetupVulkanWindow(ImGui_ImplVulkanH_Window* wd, VkSurfaceKHR surface, int width, int height) |
| 204 | { |
| 205 | |
| 206 | VkBool32 res; |
| 207 | vkGetPhysicalDeviceSurfaceSupportKHR(g_PhysicalDevice, g_QueueFamily, surface, &res); |
| 208 | if (res != VK_TRUE) |
| 209 | { |
| 210 | fprintf(stderr, "Error no WSI support on physical device 0\n"); |
| 211 | exit(-1); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | const VkFormat requestSurfaceImageFormat[] = { VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_B8G8R8_UNORM, VK_FORMAT_R8G8B8_UNORM }; |
| 216 | const VkColorSpaceKHR requestSurfaceColorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR; |
| 217 | wd->Surface = surface; |
| 218 | wd->SurfaceFormat = ImGui_ImplVulkanH_SelectSurfaceFormat(g_PhysicalDevice, wd->Surface, requestSurfaceImageFormat, (size_t)IM_COUNTOF(requestSurfaceImageFormat), requestSurfaceColorSpace); |
| 219 | |
| 220 | |
| 221 | #ifdef APP_USE_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_COUNTOF(present_modes)); |
| 227 | |
| 228 | |
| 229 | |
| 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, 0); |
| 232 | } |
| 233 | |
| 234 | static void CleanupVulkan() |
| 235 | { |
| 236 | vkDestroyDescriptorPool(g_Device, g_DescriptorPool, g_Allocator); |
| 237 | |
| 238 | #ifdef APP_USE_VULKAN_DEBUG_REPORT |
| 239 | |
| 240 | auto f_vkDestroyDebugReportCallbackEXT = (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(g_Instance, "vkDestroyDebugReportCallbackEXT"); |
| 241 | f_vkDestroyDebugReportCallbackEXT(g_Instance, g_DebugReport, g_Allocator); |
| 242 | #endif |
| 243 | |
| 244 | vkDestroyDevice(g_Device, g_Allocator); |
| 245 | vkDestroyInstance(g_Instance, g_Allocator); |
| 246 | } |
| 247 | |
| 248 | static void CleanupVulkanWindow(ImGui_ImplVulkanH_Window* wd) |
| 249 | { |
| 250 | ImGui_ImplVulkanH_DestroyWindow(g_Instance, g_Device, wd, g_Allocator); |
| 251 | vkDestroySurfaceKHR(g_Instance, wd->Surface, g_Allocator); |
| 252 | } |
| 253 | |
| 254 | static void FrameRender(ImGui_ImplVulkanH_Window* wd, ImDrawData* draw_data) |
| 255 | { |
| 256 | VkSemaphore image_acquired_semaphore = wd->FrameSemaphores[wd->SemaphoreIndex].ImageAcquiredSemaphore; |
| 257 | VkSemaphore render_complete_semaphore = wd->FrameSemaphores[wd->SemaphoreIndex].RenderCompleteSemaphore; |
| 258 | VkResult err = vkAcquireNextImageKHR(g_Device, wd->Swapchain, UINT64_MAX, image_acquired_semaphore, VK_NULL_HANDLE, &wd->FrameIndex); |
| 259 | if (err == VK_ERROR_OUT_OF_DATE_KHR || err == VK_SUBOPTIMAL_KHR) |
| 260 | g_SwapChainRebuild = true; |
| 261 | if (err == VK_ERROR_OUT_OF_DATE_KHR) |
| 262 | return; |
| 263 | if (err != VK_SUBOPTIMAL_KHR) |
| 264 | check_vk_result(err); |
| 265 | |
| 266 | ImGui_ImplVulkanH_Frame* fd = &wd->Frames[wd->FrameIndex]; |
| 267 | { |
| 268 | err = vkWaitForFences(g_Device, 1, &fd->Fence, VK_TRUE, UINT64_MAX); |
| 269 | check_vk_result(err); |
| 270 | |
| 271 | err = vkResetFences(g_Device, 1, &fd->Fence); |
| 272 | check_vk_result(err); |
| 273 | } |
| 274 | { |
| 275 | err = vkResetCommandPool(g_Device, fd->CommandPool, 0); |
| 276 | check_vk_result(err); |
| 277 | VkCommandBufferBeginInfo info = {}; |
| 278 | info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 279 | info.flags |= VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; |
| 280 | err = vkBeginCommandBuffer(fd->CommandBuffer, &info); |
| 281 | check_vk_result(err); |
| 282 | } |
| 283 | { |
| 284 | VkRenderPassBeginInfo info = {}; |
| 285 | info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; |
| 286 | info.renderPass = wd->RenderPass; |
| 287 | info.framebuffer = fd->Framebuffer; |
| 288 | info.renderArea.extent.width = wd->Width; |
| 289 | info.renderArea.extent.height = wd->Height; |
| 290 | info.clearValueCount = 1; |
| 291 | info.pClearValues = &wd->ClearValue; |
| 292 | vkCmdBeginRenderPass(fd->CommandBuffer, &info, VK_SUBPASS_CONTENTS_INLINE); |
| 293 | } |
| 294 | |
| 295 | |
| 296 | ImGui_ImplVulkan_RenderDrawData(draw_data, fd->CommandBuffer); |
| 297 | |
| 298 | |
| 299 | vkCmdEndRenderPass(fd->CommandBuffer); |
| 300 | { |
| 301 | VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; |
| 302 | VkSubmitInfo info = {}; |
| 303 | info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 304 | info.waitSemaphoreCount = 1; |
| 305 | info.pWaitSemaphores = &image_acquired_semaphore; |
| 306 | info.pWaitDstStageMask = &wait_stage; |
| 307 | info.commandBufferCount = 1; |
| 308 | info.pCommandBuffers = &fd->CommandBuffer; |
| 309 | info.signalSemaphoreCount = 1; |
| 310 | info.pSignalSemaphores = &render_complete_semaphore; |
| 311 | |
| 312 | err = vkEndCommandBuffer(fd->CommandBuffer); |
| 313 | check_vk_result(err); |
| 314 | err = vkQueueSubmit(g_Queue, 1, &info, fd->Fence); |
| 315 | check_vk_result(err); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | static void FramePresent(ImGui_ImplVulkanH_Window* wd) |
| 320 | { |
| 321 | if (g_SwapChainRebuild) |
| 322 | return; |
| 323 | VkSemaphore render_complete_semaphore = wd->FrameSemaphores[wd->SemaphoreIndex].RenderCompleteSemaphore; |
| 324 | VkPresentInfoKHR info = {}; |
| 325 | info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; |
| 326 | info.waitSemaphoreCount = 1; |
| 327 | info.pWaitSemaphores = &render_complete_semaphore; |
| 328 | info.swapchainCount = 1; |
| 329 | info.pSwapchains = &wd->Swapchain; |
| 330 | info.pImageIndices = &wd->FrameIndex; |
| 331 | VkResult err = vkQueuePresentKHR(g_Queue, &info); |
| 332 | if (err == VK_ERROR_OUT_OF_DATE_KHR || err == VK_SUBOPTIMAL_KHR) |
| 333 | g_SwapChainRebuild = true; |
| 334 | if (err == VK_ERROR_OUT_OF_DATE_KHR) |
| 335 | return; |
| 336 | if (err != VK_SUBOPTIMAL_KHR) |
| 337 | check_vk_result(err); |
| 338 | wd->SemaphoreIndex = (wd->SemaphoreIndex + 1) % wd->SemaphoreCount; |
| 339 | } |
| 340 | |
| 341 | LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); |
| 342 | |
| 343 | |
| 344 | static int ImGui_ImplWin32_CreateVkSurface(ImGuiViewport* viewport, ImU64 vk_instance, const void* vk_allocator, ImU64* out_vk_surface) |
| 345 | { |
| 346 | VkWin32SurfaceCreateInfoKHR createInfo = {}; |
| 347 | createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR; |
| 348 | createInfo.hwnd = (HWND)viewport->PlatformHandleRaw; |
| 349 | createInfo.hinstance = ::GetModuleHandle(nullptr); |
| 350 | return (int)vkCreateWin32SurfaceKHR((VkInstance)vk_instance, &createInfo, (VkAllocationCallbacks*)vk_allocator, (VkSurfaceKHR*)out_vk_surface); |
| 351 | } |
| 352 | |
| 353 | |
| 354 | int main(int, char**) |
| 355 | { |
| 356 | |
| 357 | ImGui_ImplWin32_EnableDpiAwareness(); |
| 358 | float main_scale = ImGui_ImplWin32_GetDpiScaleForMonitor(::MonitorFromPoint(POINT{ 0, 0 }, MONITOR_DEFAULTTOPRIMARY)); |
| 359 | |
| 360 | |
| 361 | WNDCLASSEXW wc = { sizeof(wc), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(nullptr), nullptr, nullptr, nullptr, nullptr, L"ImGui Example", nullptr }; |
| 362 | ::RegisterClassExW(&wc); |
| 363 | HWND hwnd = ::CreateWindowW(wc.lpszClassName, L"Dear ImGui Win32+Vulkan Example", WS_OVERLAPPEDWINDOW, 100, 100, (int)(1280 * main_scale), (int)(800 * main_scale), nullptr, nullptr, wc.hInstance, nullptr); |
| 364 | |
| 365 | ImVector<const char*> extensions; |
| 366 | extensions.push_back("VK_KHR_surface"); |
| 367 | extensions.push_back("VK_KHR_win32_surface"); |
| 368 | SetupVulkan(extensions); |
| 369 | |
| 370 | |
| 371 | VkSurfaceKHR surface; |
| 372 | VkResult err; |
| 373 | VkWin32SurfaceCreateInfoKHR createInfo = {}; |
| 374 | createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR; |
| 375 | createInfo.hwnd = hwnd; |
| 376 | createInfo.hinstance = ::GetModuleHandle(nullptr); |
| 377 | if (vkCreateWin32SurfaceKHR(g_Instance, &createInfo, nullptr, &surface) != VK_SUCCESS) |
| 378 | { |
| 379 | printf("Failed to create Vulkan surface.\n"); |
| 380 | return 1; |
| 381 | } |
| 382 | |
| 383 | |
| 384 | |
| 385 | ImGui_ImplVulkanH_Window* wd = &g_MainWindowData; |
| 386 | SetupVulkanWindow(wd, surface, (int)(1280 * main_scale), (int)(800 * main_scale)); |
| 387 | ::ShowWindow(hwnd, SW_SHOWDEFAULT); |
| 388 | ::UpdateWindow(hwnd); |
| 389 | |
| 390 | |
| 391 | IMGUI_CHECKVERSION(); |
| 392 | ImGui::CreateContext(); |
| 393 | ImGuiIO& io = ImGui::GetIO(); (void)io; |
| 394 | io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; |
| 395 | io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; |
| 396 | io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; |
| 397 | io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | ImGui::StyleColorsDark(); |
| 403 | |
| 404 | |
| 405 | |
| 406 | ImGuiStyle& style = ImGui::GetStyle(); |
| 407 | style.ScaleAllSizes(main_scale); |
| 408 | style.FontScaleDpi = main_scale; |
| 409 | io.ConfigDpiScaleFonts = true; |
| 410 | io.ConfigDpiScaleViewports = true; |
| 411 | |
| 412 | |
| 413 | if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) |
| 414 | { |
| 415 | style.WindowRounding = 0.0f; |
| 416 | style.Colors[ImGuiCol_WindowBg].w = 1.0f; |
| 417 | } |
| 418 | |
| 419 | |
| 420 | ImGui_ImplWin32_Init(hwnd); |
| 421 | ImGui::GetPlatformIO().Platform_CreateVkSurface = ImGui_ImplWin32_CreateVkSurface; |
| 422 | |
| 423 | ImGui_ImplVulkan_InitInfo init_info = {}; |
| 424 | |
| 425 | init_info.Instance = g_Instance; |
| 426 | init_info.PhysicalDevice = g_PhysicalDevice; |
| 427 | init_info.Device = g_Device; |
| 428 | init_info.QueueFamily = g_QueueFamily; |
| 429 | init_info.Queue = g_Queue; |
| 430 | init_info.PipelineCache = g_PipelineCache; |
| 431 | init_info.DescriptorPool = g_DescriptorPool; |
| 432 | init_info.MinImageCount = g_MinImageCount; |
| 433 | init_info.ImageCount = wd->ImageCount; |
| 434 | init_info.Allocator = g_Allocator; |
| 435 | init_info.PipelineInfoMain.RenderPass = wd->RenderPass; |
| 436 | init_info.PipelineInfoMain.Subpass = 0; |
| 437 | init_info.PipelineInfoMain.MSAASamples = VK_SAMPLE_COUNT_1_BIT; |
| 438 | init_info.CheckVkResultFn = check_vk_result; |
| 439 | ImGui_ImplVulkan_Init(&init_info); |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | bool show_demo_window = true; |
| 461 | bool show_another_window = false; |
| 462 | ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); |
| 463 | |
| 464 | |
| 465 | bool done = false; |
| 466 | while (!done) |
| 467 | { |
| 468 | |
| 469 | |
| 470 | MSG msg; |
| 471 | while (::PeekMessage(&msg, nullptr, 0U, 0U, PM_REMOVE)) |
| 472 | { |
| 473 | ::TranslateMessage(&msg); |
| 474 | ::DispatchMessage(&msg); |
| 475 | if (msg.message == WM_QUIT) |
| 476 | done = true; |
| 477 | } |
| 478 | if (done) |
| 479 | break; |
| 480 | |
| 481 | |
| 482 | ImGui_ImplVulkan_NewFrame(); |
| 483 | ImGui_ImplWin32_NewFrame(); |
| 484 | ImGui::NewFrame(); |
| 485 | |
| 486 | |
| 487 | if (show_demo_window) |
| 488 | ImGui::ShowDemoWindow(&show_demo_window); |
| 489 | |
| 490 | |
| 491 | { |
| 492 | static float f = 0.0f; |
| 493 | static int counter = 0; |
| 494 | |
| 495 | ImGui::Begin("Hello, world!"); |
| 496 | |
| 497 | ImGui::Text("This is some useful text."); |
| 498 | ImGui::Checkbox("Demo Window", &show_demo_window); |
| 499 | ImGui::Checkbox("Another Window", &show_another_window); |
| 500 | |
| 501 | ImGui::SliderFloat("float", &f, 0.0f, 1.0f); |
| 502 | ImGui::ColorEdit3("clear color", (float*)&clear_color); |
| 503 | |
| 504 | if (ImGui::Button("Button")) |
| 505 | counter++; |
| 506 | ImGui::SameLine(); |
| 507 | ImGui::Text("counter = %d", counter); |
| 508 | |
| 509 | ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); |
| 510 | ImGui::End(); |
| 511 | } |
| 512 | |
| 513 | |
| 514 | if (show_another_window) |
| 515 | { |
| 516 | ImGui::Begin("Another Window", &show_another_window); |
| 517 | ImGui::Text("Hello from another window!"); |
| 518 | if (ImGui::Button("Close Me")) |
| 519 | show_another_window = false; |
| 520 | ImGui::End(); |
| 521 | } |
| 522 | |
| 523 | |
| 524 | ImGui::Render(); |
| 525 | ImDrawData* main_draw_data = ImGui::GetDrawData(); |
| 526 | const bool main_is_minimized = (main_draw_data->DisplaySize.x <= 0.0f || main_draw_data->DisplaySize.y <= 0.0f); |
| 527 | wd->ClearValue.color.float32[0] = clear_color.x * clear_color.w; |
| 528 | wd->ClearValue.color.float32[1] = clear_color.y * clear_color.w; |
| 529 | wd->ClearValue.color.float32[2] = clear_color.z * clear_color.w; |
| 530 | wd->ClearValue.color.float32[3] = clear_color.w; |
| 531 | if (!main_is_minimized) |
| 532 | FrameRender(wd, main_draw_data); |
| 533 | |
| 534 | |
| 535 | if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) |
| 536 | { |
| 537 | ImGui::UpdatePlatformWindows(); |
| 538 | ImGui::RenderPlatformWindowsDefault(); |
| 539 | } |
| 540 | |
| 541 | |
| 542 | if (!main_is_minimized) |
| 543 | FramePresent(wd); |
| 544 | } |
| 545 | |
| 546 | |
| 547 | err = vkDeviceWaitIdle(g_Device); |
| 548 | check_vk_result(err); |
| 549 | ImGui_ImplVulkan_Shutdown(); |
| 550 | ImGui_ImplWin32_Shutdown(); |
| 551 | ImGui::DestroyContext(); |
| 552 | |
| 553 | CleanupVulkanWindow(&g_MainWindowData); |
| 554 | CleanupVulkan(); |
| 555 | |
| 556 | ::DestroyWindow(hwnd); |
| 557 | ::UnregisterClassW(wc.lpszClassName, wc.hInstance); |
| 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) |
| 573 | { |
| 574 | if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam)) |
| 575 | return true; |
| 576 | |
| 577 | switch (msg) |
| 578 | { |
| 579 | case WM_SIZE: |
| 580 | if (g_Device != VK_NULL_HANDLE && wParam != SIZE_MINIMIZED) |
| 581 | { |
| 582 | |
| 583 | int fb_width = (UINT)LOWORD(lParam); |
| 584 | int fb_height = (UINT)HIWORD(lParam); |
| 585 | if (fb_width > 0 && fb_height > 0 && (g_SwapChainRebuild || g_MainWindowData.Width != fb_width || g_MainWindowData.Height != fb_height)) |
| 586 | { |
| 587 | ImGui_ImplVulkan_SetMinImageCount(g_MinImageCount); |
| 588 | ImGui_ImplVulkanH_CreateOrResizeWindow(g_Instance, g_PhysicalDevice, g_Device, &g_MainWindowData, g_QueueFamily, g_Allocator, fb_width, fb_height, g_MinImageCount, 0); |
| 589 | g_MainWindowData.FrameIndex = 0; |
| 590 | g_SwapChainRebuild = false; |
| 591 | } |
| 592 | } |
| 593 | return 0; |
| 594 | case WM_SYSCOMMAND: |
| 595 | if ((wParam & 0xfff0) == SC_KEYMENU) |
| 596 | return 0; |
| 597 | break; |
| 598 | case WM_DESTROY: |
| 599 | ::PostQuitMessage(0); |
| 600 | return 0; |
| 601 | } |
| 602 | return ::DefWindowProcW(hWnd, msg, wParam, lParam); |
| 603 | } |