From 7495206e79c685e2346f2daf6ac6dbeafe880349 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Tue, 11 Nov 2025 00:56:29 -0500 Subject: [PATCH] xo-imgui: refactor: streamline pick_physical_device() --- xo-imgui/example/ex4a/VulkanApp.cpp | 33 ++++++++++++++++++ xo-imgui/example/ex4a/VulkanApp.hpp | 54 ++++++++++------------------- 2 files changed, 52 insertions(+), 35 deletions(-) diff --git a/xo-imgui/example/ex4a/VulkanApp.cpp b/xo-imgui/example/ex4a/VulkanApp.cpp index ec88c867..029c0447 100644 --- a/xo-imgui/example/ex4a/VulkanApp.cpp +++ b/xo-imgui/example/ex4a/VulkanApp.cpp @@ -103,4 +103,37 @@ MinimalImGuiVulkan::create_surface() } } +void +MinimalImGuiVulkan::pick_physical_device() +{ + uint32_t n_device = 0; + vkEnumeratePhysicalDevices(instance_, &n_device, nullptr); + + if (n_device == 0) { + throw std::runtime_error("Failed to find GPUs with Vulkan support!"); + } + + std::vector devices(n_device); + vkEnumeratePhysicalDevices(instance_, &n_device, devices.data()); + + this->physical_device_ = devices[0]; // Just pick the first one for simplicity + + // Find graphics queue family + uint32_t n_queue_family = 0; + vkGetPhysicalDeviceQueueFamilyProperties(physical_device_, &n_queue_family, nullptr); + std::vector queue_families(n_queue_family); + vkGetPhysicalDeviceQueueFamilyProperties(physical_device_, &n_queue_family, queue_families.data()); + + for (uint32_t i = 0; i < queue_families.size(); i++) { + if (queue_families[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) { + VkBool32 present_support = false; + vkGetPhysicalDeviceSurfaceSupportKHR(physical_device_, i, surface_, &present_support); + if (present_support) { + this->graphics_queue_family_ = i; + break; + } + } + } +} + /* end VulkanApp.cpp */ diff --git a/xo-imgui/example/ex4a/VulkanApp.hpp b/xo-imgui/example/ex4a/VulkanApp.hpp index aa5e43db..f9b7a91f 100644 --- a/xo-imgui/example/ex4a/VulkanApp.hpp +++ b/xo-imgui/example/ex4a/VulkanApp.hpp @@ -36,41 +36,18 @@ private: */ void create_surface(); - void pick_physical_device() { - uint32_t deviceCount = 0; - vkEnumeratePhysicalDevices(instance_, &deviceCount, nullptr); - - if (deviceCount == 0) { - throw std::runtime_error("Failed to find GPUs with Vulkan support!"); - } - - std::vector devices(deviceCount); - vkEnumeratePhysicalDevices(instance_, &deviceCount, devices.data()); - - this->physical_device_ = devices[0]; // Just pick the first one for simplicity - - // Find graphics queue family - uint32_t queueFamilyCount = 0; - vkGetPhysicalDeviceQueueFamilyProperties(physical_device_, &queueFamilyCount, nullptr); - std::vector queueFamilies(queueFamilyCount); - vkGetPhysicalDeviceQueueFamilyProperties(physical_device_, &queueFamilyCount, queueFamilies.data()); - - for (uint32_t i = 0; i < queueFamilies.size(); i++) { - if (queueFamilies[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) { - VkBool32 presentSupport = false; - vkGetPhysicalDeviceSurfaceSupportKHR(physical_device_, i, surface_, &presentSupport); - if (presentSupport) { - graphicsQueueFamily = i; - break; - } - } - } - } + /* choose physical device (1:1 with graphics card, presumably) + * populates @ref physical_device_, @ref graphics_queue_family_ + */ + void pick_physical_device(); + /* + * require: pick_physical_device() has run successfully + */ void create_logical_device() { VkDeviceQueueCreateInfo queueCreateInfo{}; queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; - queueCreateInfo.queueFamilyIndex = graphicsQueueFamily; + queueCreateInfo.queueFamilyIndex = graphics_queue_family_; queueCreateInfo.queueCount = 1; float queuePriority = 1.0f; queueCreateInfo.pQueuePriorities = &queuePriority; @@ -91,7 +68,7 @@ private: throw std::runtime_error("Failed to create logical device!"); } - vkGetDeviceQueue(device, graphicsQueueFamily, 0, &graphicsQueue); + vkGetDeviceQueue(device, graphics_queue_family_, 0, &graphicsQueue); } void create_swapchain() { @@ -231,7 +208,7 @@ private: VkCommandPoolCreateInfo poolInfo{}; poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; poolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; - poolInfo.queueFamilyIndex = graphicsQueueFamily; + poolInfo.queueFamilyIndex = graphics_queue_family_; if (vkCreateCommandPool(device, &poolInfo, nullptr, &commandPool) != VK_SUCCESS) { throw std::runtime_error("Failed to create command pool!"); @@ -315,7 +292,7 @@ private: init_info.Instance = instance_; init_info.PhysicalDevice = physical_device_; init_info.Device = device; - init_info.QueueFamily = graphicsQueueFamily; + init_info.QueueFamily = graphics_queue_family_; init_info.Queue = graphicsQueue; init_info.PipelineCache = VK_NULL_HANDLE; init_info.DescriptorPool = descriptorPool; @@ -593,9 +570,17 @@ private: SDL_Window* window_ = nullptr; VkInstance instance_; + + /* abstraction for presentation area (?) */ VkSurfaceKHR surface_; + + /* physical device (graphics card) */ VkPhysicalDevice physical_device_; + uint32_t graphics_queue_family_ = 0; + + /* logical device (graphics card, abstract api (?)) */ VkDevice device; + VkQueue graphicsQueue; VkSwapchainKHR swapchain; VkFormat swapchainImageFormat; @@ -613,7 +598,6 @@ private: uint32_t currentFrame = 0; const int MAX_FRAMES_IN_FLIGHT = 2; - uint32_t graphicsQueueFamily = 0; /* true when window resize behavior detected, * until swapchain in consistent state. */