From e67fae00d6e6aa39991671f1fbca66acc5b1bb85 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Tue, 5 Mar 2024 13:52:25 +0900 Subject: [PATCH] WIP "Physical devices and queue families" TODO: "Queue families" https://vulkan-tutorial.com/en/Drawing_a_triangle/Setup/Physical_devices_and_queue_families --- src/main.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 14493cc..36aaa87 100644 --- a/src/main.rs +++ b/src/main.rs @@ -123,6 +123,24 @@ fn create_debug_messenger_create_info() -> ffi::VkDebugUtilsMessengerCreateInfoE } } +fn is_device_suitable(dev: ffi::VkPhysicalDevice) -> bool { + let mut dev_props: ffi::VkPhysicalDeviceProperties = unsafe { std::mem::zeroed() }; + unsafe { + ffi::vkGetPhysicalDeviceProperties(dev, std::ptr::addr_of_mut!(dev_props)); + } + + let mut dev_feat: ffi::VkPhysicalDeviceFeatures = unsafe { std::mem::zeroed() }; + unsafe { + ffi::vkGetPhysicalDeviceFeatures(dev, std::ptr::addr_of_mut!(dev_feat)); + } + + // dev_props.deviceType == ffi::VkPhysicalDeviceType_VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU + // && dev_feat.geometryShader != 0 + + // Use previous checks for specifics, but for now, accept any GPU. + true +} + struct VulkanApp { window: *mut ffi::GLFWwindow, vk_instance: ffi::VkInstance, @@ -223,7 +241,8 @@ impl VulkanApp { create_info.enabledLayerCount = VALIDATION_LAYERS.len() as u32; create_info.ppEnabledLayerNames = VALIDATION_LAYERS.as_ptr() as *const *const i8; - create_info.pNext = std::ptr::addr_of!(debug_messenger_create_info) as *const std::ffi::c_void; + create_info.pNext = + std::ptr::addr_of!(debug_messenger_create_info) as *const std::ffi::c_void; } let vk_result = unsafe { @@ -239,6 +258,7 @@ impl VulkanApp { } self.setup_debug_messenger(); + self.pick_physical_device(); } fn setup_debug_messenger(&mut self) { @@ -263,6 +283,45 @@ impl VulkanApp { } } + fn pick_physical_device(&mut self) { + let mut phys_device: ffi::VkPhysicalDevice = std::ptr::null_mut(); + + let mut dev_count: u32 = 0; + unsafe { + ffi::vkEnumeratePhysicalDevices( + self.vk_instance, + std::ptr::addr_of_mut!(dev_count), + std::ptr::null_mut(), + ); + } + + if dev_count == 0 { + panic!("Failed to find GPUs with Vulkan support!"); + } + + let mut phys_dev_handles_vec: Vec = + Vec::with_capacity(dev_count as usize); + phys_dev_handles_vec.resize(dev_count as usize, std::ptr::null_mut()); + unsafe { + ffi::vkEnumeratePhysicalDevices( + self.vk_instance, + std::ptr::addr_of_mut!(dev_count), + phys_dev_handles_vec.as_mut_ptr(), + ); + } + + for phys_dev in phys_dev_handles_vec { + if is_device_suitable(phys_dev) { + phys_device = phys_dev; + break; + } + } + + if phys_device.is_null() { + panic!("Failed to find a suitable GPU!"); + } + } + fn main_loop(&mut self) { if self.window.is_null() { panic!("ERROR: Cannot execute main loop if window is null!");