Selecting a physical device

This commit is contained in:
Timothy Warren 2023-04-05 11:08:36 -04:00
parent 34fb477871
commit 469ca82914

View File

@ -1,6 +1,7 @@
use crate::create_instance; use crate::create_instance;
use crate::VALIDATION_ENABLED; use crate::VALIDATION_ENABLED;
use ::anyhow::{anyhow, Result}; use ::anyhow::{anyhow, Result};
use ::log::*;
use ::thiserror::Error; use ::thiserror::Error;
use ::vulkanalia::loader::{LibloadingLoader, LIBRARY}; use ::vulkanalia::loader::{LibloadingLoader, LIBRARY};
use ::vulkanalia::prelude::v1_0::*; use ::vulkanalia::prelude::v1_0::*;
@ -51,6 +52,7 @@ impl App {
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
pub struct AppData { pub struct AppData {
messenger: vk::DebugUtilsMessengerEXT, messenger: vk::DebugUtilsMessengerEXT,
physical_device: vk::PhysicalDevice,
} }
#[derive(Debug, Error)] #[derive(Debug, Error)]
@ -58,5 +60,58 @@ pub struct AppData {
pub struct SuitabilityError(pub &'static str); pub struct SuitabilityError(pub &'static str);
unsafe fn pick_physical_device(instance: &Instance, data: &mut AppData) -> Result<()> { unsafe fn pick_physical_device(instance: &Instance, data: &mut AppData) -> Result<()> {
for physical_device in instance.enumerate_physical_devices()? {
let properties = instance.get_physical_device_properties(physical_device);
if let Err(error) = check_physical_device(instance, data, physical_device) {
warn!(
"Skipping physical device (`{}`): {}",
properties.device_name, error
);
} else {
info!("Selected physical device (`{}`).", properties.device_name);
data.physical_device = physical_device;
return Ok(());
}
}
Err(anyhow!("Failed to find suitable physical device."))
}
unsafe fn check_physical_device(
instance: &Instance,
data: &AppData,
physical_device: vk::PhysicalDevice,
) -> Result<()> {
QueueFamilyIndicies::get(instance, data, physical_device)?;
Ok(()) Ok(())
} }
#[derive(Copy, Clone, Debug)]
struct QueueFamilyIndicies {
graphics: u32,
}
impl QueueFamilyIndicies {
unsafe fn get(
instance: &Instance,
data: &AppData,
physical_device: vk::PhysicalDevice,
) -> Result<Self> {
let properties = instance.get_physical_device_queue_family_properties(physical_device);
let graphics = properties
.iter()
.position(|p| p.queue_flags.contains(vk::QueueFlags::GRAPHICS))
.map(|i| i as u32);
if let Some(graphics) = graphics {
Ok(Self { graphics })
} else {
Err(anyhow!(SuitabilityError(
"Missing required queue families."
)))
}
}
}