Split pick_graphics_device
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 26m24s

This commit is contained in:
Florian RICHER 2025-04-03 21:10:08 +02:00
parent 15c273b93d
commit 2fbf4e6ce2
Signed by: florian.richer
GPG key ID: C73D37CBED7BFC77

View file

@ -13,7 +13,8 @@ use vulkano::{
descriptor_set::allocator::StandardDescriptorSetAllocator,
device::{
Device, DeviceCreateInfo, DeviceExtensions, DeviceFeatures, Queue, QueueCreateInfo,
QueueFlags, physical::PhysicalDeviceType,
QueueFlags,
physical::{PhysicalDevice, PhysicalDeviceType},
},
instance::{Instance, InstanceCreateFlags, InstanceCreateInfo, InstanceExtensions},
memory::allocator::{MemoryTypeFilter, StandardMemoryAllocator},
@ -133,6 +134,53 @@ fn create_instance(
.unwrap()
}
fn find_physical_device_queue_family_indexes(
physical_device: &Arc<PhysicalDevice>,
event_loop: &EventLoop<()>,
) -> Option<u32> {
let mut graphic_queue_family_index = None;
for (i, queue_family_property) in physical_device.queue_family_properties().iter().enumerate() {
if queue_family_property
.queue_flags
.intersects(QueueFlags::GRAPHICS)
&& physical_device
.presentation_support(i as u32, event_loop)
.unwrap()
{
graphic_queue_family_index = Some(i as u32);
}
}
graphic_queue_family_index
}
fn pick_physical_device_and_queue_family_indexes(
instance: &Arc<Instance>,
event_loop: &EventLoop<()>,
device_extensions: &DeviceExtensions,
) -> Option<(Arc<PhysicalDevice>, u32)> {
instance
.enumerate_physical_devices()
.unwrap()
.filter(|p| {
p.api_version() >= Version::V1_3 || p.supported_extensions().khr_dynamic_rendering
})
.filter(|p| p.supported_extensions().contains(device_extensions))
.filter_map(|p| {
find_physical_device_queue_family_indexes(&p, event_loop)
.and_then(|indexes| Some((p, indexes)))
})
.min_by_key(|(p, _)| match p.properties().device_type {
PhysicalDeviceType::DiscreteGpu => 0,
PhysicalDeviceType::IntegratedGpu => 1,
PhysicalDeviceType::VirtualGpu => 2,
PhysicalDeviceType::Cpu => 3,
PhysicalDeviceType::Other => 4,
_ => 5,
})
}
fn pick_graphics_device(
instance: &Arc<Instance>,
event_loop: &EventLoop<()>,
@ -145,32 +193,9 @@ fn pick_graphics_device(
..DeviceExtensions::empty()
};
let (physical_device, queue_family_index) = instance
.enumerate_physical_devices()
.unwrap()
.filter(|p| {
p.api_version() >= Version::V1_3 || p.supported_extensions().khr_dynamic_rendering
})
.filter(|p| p.supported_extensions().contains(&device_extensions))
.filter_map(|p| {
p.queue_family_properties()
.iter()
.enumerate()
.position(|(i, q)| {
q.queue_flags.intersects(QueueFlags::GRAPHICS)
&& p.presentation_support(i as u32, event_loop).unwrap()
})
.map(|i| (p, i as u32))
})
.min_by_key(|(p, _)| match p.properties().device_type {
PhysicalDeviceType::DiscreteGpu => 0,
PhysicalDeviceType::IntegratedGpu => 1,
PhysicalDeviceType::VirtualGpu => 2,
PhysicalDeviceType::Cpu => 3,
PhysicalDeviceType::Other => 4,
_ => 5,
})
.expect("no suitable physical device found");
let (physical_device, graphics_family_index) =
pick_physical_device_and_queue_family_indexes(instance, event_loop, &device_extensions)
.unwrap();
log::debug!(
"Using device: {} (type: {:?})",
@ -188,7 +213,7 @@ fn pick_graphics_device(
physical_device,
DeviceCreateInfo {
queue_create_infos: vec![QueueCreateInfo {
queue_family_index,
queue_family_index: graphics_family_index,
..Default::default()
}],
enabled_extensions: device_extensions,