mirror of
https://github.com/ocornut/imgui.git
synced 2025-09-07 03:48:25 +00:00
Examples: Add Win32+Vulkan example, amends. (#8180)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// Dear ImGui: standalone example application for Vulkan
|
||||
// Dear ImGui: standalone example application for Win32 + Vulkan
|
||||
|
||||
// Learn about Dear ImGui:
|
||||
// - FAQ https://dearimgui.com/faq
|
||||
@@ -74,35 +74,6 @@ static bool IsExtensionAvailable(const ImVector<VkExtensionProperties>& properti
|
||||
return false;
|
||||
}
|
||||
|
||||
static VkPhysicalDevice SetupVulkan_SelectPhysicalDevice()
|
||||
{
|
||||
uint32_t gpu_count;
|
||||
VkResult err = vkEnumeratePhysicalDevices(g_Instance, &gpu_count, nullptr);
|
||||
check_vk_result(err);
|
||||
IM_ASSERT(gpu_count > 0);
|
||||
|
||||
ImVector<VkPhysicalDevice> gpus;
|
||||
gpus.resize(gpu_count);
|
||||
err = vkEnumeratePhysicalDevices(g_Instance, &gpu_count, gpus.Data);
|
||||
check_vk_result(err);
|
||||
|
||||
// If a number >1 of GPUs got reported, find discrete GPU if present, or use first one available. This covers
|
||||
// most common cases (multi-gpu/integrated+dedicated graphics). Handling more complicated setups (multiple
|
||||
// dedicated GPUs) is out of scope of this sample.
|
||||
for (VkPhysicalDevice& device : gpus)
|
||||
{
|
||||
VkPhysicalDeviceProperties properties;
|
||||
vkGetPhysicalDeviceProperties(device, &properties);
|
||||
if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU)
|
||||
return device;
|
||||
}
|
||||
|
||||
// Use first GPU (Integrated) is a Discrete one is not available.
|
||||
if (gpu_count > 0)
|
||||
return gpus[0];
|
||||
return VK_NULL_HANDLE;
|
||||
}
|
||||
|
||||
static void SetupVulkan(ImVector<const char*> instance_extensions)
|
||||
{
|
||||
VkResult err;
|
||||
@@ -166,23 +137,12 @@ static void SetupVulkan(ImVector<const char*> instance_extensions)
|
||||
}
|
||||
|
||||
// Select Physical Device (GPU)
|
||||
g_PhysicalDevice = SetupVulkan_SelectPhysicalDevice();
|
||||
g_PhysicalDevice = ImGui_ImplVulkanH_SelectPhysicalDevice(g_Instance);
|
||||
IM_ASSERT(g_PhysicalDevice != VK_NULL_HANDLE);
|
||||
|
||||
// Select graphics queue family
|
||||
{
|
||||
uint32_t count;
|
||||
vkGetPhysicalDeviceQueueFamilyProperties(g_PhysicalDevice, &count, nullptr);
|
||||
VkQueueFamilyProperties* queues = (VkQueueFamilyProperties*)malloc(sizeof(VkQueueFamilyProperties) * count);
|
||||
vkGetPhysicalDeviceQueueFamilyProperties(g_PhysicalDevice, &count, queues);
|
||||
for (uint32_t i = 0; i < count; i++)
|
||||
if (queues[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
|
||||
{
|
||||
g_QueueFamily = i;
|
||||
break;
|
||||
}
|
||||
free(queues);
|
||||
IM_ASSERT(g_QueueFamily != (uint32_t)-1);
|
||||
}
|
||||
g_QueueFamily = ImGui_ImplVulkanH_SelectQueueFamilyIndex(g_PhysicalDevice);
|
||||
IM_ASSERT(g_QueueFamily != (uint32_t)-1);
|
||||
|
||||
// Create Logical Device (with 1 queue)
|
||||
{
|
||||
@@ -257,7 +217,7 @@ static void SetupVulkanWindow(ImGui_ImplVulkanH_Window* wd, VkSurfaceKHR surface
|
||||
wd->SurfaceFormat = ImGui_ImplVulkanH_SelectSurfaceFormat(g_PhysicalDevice, wd->Surface, requestSurfaceImageFormat, (size_t)IM_ARRAYSIZE(requestSurfaceImageFormat), requestSurfaceColorSpace);
|
||||
|
||||
// Select Present Mode
|
||||
#ifdef APP_UNLIMITED_FRAME_RATE
|
||||
#ifdef APP_USE_UNLIMITED_FRAME_RATE
|
||||
VkPresentModeKHR present_modes[] = { VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_FIFO_KHR };
|
||||
#else
|
||||
VkPresentModeKHR present_modes[] = { VK_PRESENT_MODE_FIFO_KHR };
|
||||
@@ -387,7 +347,7 @@ int main(int, char**)
|
||||
//ImGui_ImplWin32_EnableDpiAwareness();
|
||||
WNDCLASSEXW wc = { sizeof(wc), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(nullptr), nullptr, nullptr, nullptr, nullptr, L"ImGui Example", nullptr };
|
||||
::RegisterClassExW(&wc);
|
||||
HWND hwnd = ::CreateWindowW(wc.lpszClassName, L"Dear ImGui Vulkan Example", WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, nullptr, nullptr, wc.hInstance, nullptr);
|
||||
HWND hwnd = ::CreateWindowW(wc.lpszClassName, L"Dear ImGui Win32+Vulkan Example", WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, nullptr, nullptr, wc.hInstance, nullptr);
|
||||
|
||||
ImVector<const char*> extensions;
|
||||
extensions.push_back("VK_KHR_surface");
|
||||
@@ -400,7 +360,7 @@ int main(int, char**)
|
||||
VkWin32SurfaceCreateInfoKHR createInfo = {};
|
||||
createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
|
||||
createInfo.hwnd = hwnd;
|
||||
createInfo.hinstance = GetModuleHandle(nullptr);
|
||||
createInfo.hinstance = ::GetModuleHandle(nullptr);
|
||||
if (vkCreateWin32SurfaceKHR(g_Instance, &createInfo, nullptr, &surface) != VK_SUCCESS)
|
||||
{
|
||||
printf("Failed to create Vulkan surface.\n");
|
||||
@@ -408,6 +368,7 @@ int main(int, char**)
|
||||
}
|
||||
|
||||
// Show the window
|
||||
// FIXME: Retrieve client size from window itself.
|
||||
ImGui_ImplVulkanH_Window* wd = &g_MainWindowData;
|
||||
SetupVulkanWindow(wd, surface, 1280, 800);
|
||||
::ShowWindow(hwnd, SW_SHOWDEFAULT);
|
||||
@@ -525,7 +486,6 @@ int main(int, char**)
|
||||
|
||||
// Rendering
|
||||
ImGui::Render();
|
||||
|
||||
ImDrawData* draw_data = ImGui::GetDrawData();
|
||||
const bool is_minimized = (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f);
|
||||
if (!is_minimized)
|
||||
@@ -539,10 +499,9 @@ int main(int, char**)
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
err = vkDeviceWaitIdle(g_Device);
|
||||
check_vk_result(err);
|
||||
|
||||
// Cleanup
|
||||
ImGui_ImplVulkan_Shutdown();
|
||||
ImGui_ImplWin32_Shutdown();
|
||||
ImGui::DestroyContext();
|
||||
|
Reference in New Issue
Block a user