xo-imgui: refactor: streamline pick_physical_device()

This commit is contained in:
Roland Conybeare 2025-11-11 00:56:29 -05:00
commit 7495206e79
2 changed files with 52 additions and 35 deletions

View file

@ -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<VkPhysicalDevice> 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<VkQueueFamilyProperties> 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 */

View file

@ -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<VkPhysicalDevice> 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<VkQueueFamilyProperties> 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.
*/