1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use ash::extensions::khr::Surface;
use ash::vk;
use ash::vk::{PhysicalDevice, PhysicalDeviceProperties, PhysicalDeviceType};
use std::os::raw::c_char;
use tracing::info;
/// A struct holding a physical device, a queue family index and a surface.
pub struct DeviceQueueFamilies {
pub physical_device: vk::PhysicalDevice,
//We might want to add support for multiple queue families in case no queue family
// supports both graphics and presenting, but it seems no hardware actually works that way currently,
// so we're sticking to the simpler API for now.
pub queue_family_index: u32,
pub surface_loader: Surface,
}
/// Function with a default implementation to get a suitable queue family;
/// selects the first discrete gpu unless an index is specified,
/// in which case the candidate device with the given index is used.
/// Devices without support for graphics or the given surface are ignored.
/// # Safety
/// The given instance must be valid and compatible with the given surface.
pub unsafe fn select(
entry: &ash::Entry,
instance: &ash::Instance,
surface: vk::SurfaceKHR,
index: Option<usize>,
) -> DeviceQueueFamilies {
let physical_devices = instance
.enumerate_physical_devices()
.expect("Physical device error");
let surface_loader = Surface::new(entry, instance);
struct Candidate {
physical_device: PhysicalDevice,
queue_family_index: usize,
physical_device_properties: PhysicalDeviceProperties,
}
// Select device with given index that supports graphics;
// default to the first device with the highest queue count.
let devices_and_queues = physical_devices
.iter()
.filter_map(|physical_device| {
instance
.get_physical_device_queue_family_properties(*physical_device)
.iter()
.enumerate()
.find_map(|(queue_family_index, ref info)| {
let supports_graphic_and_surface =
info.queue_flags.contains(vk::QueueFlags::GRAPHICS)
&& surface_loader
.get_physical_device_surface_support(
*physical_device,
queue_family_index as u32,
surface,
)
.expect("Get physical device surface support failed.");
if supports_graphic_and_surface {
let props = instance.get_physical_device_properties(*physical_device);
let name = std::str::from_utf8(
&*(&props.device_name
[..props.device_name.iter().position(|&x| x == 0).unwrap()]
as *const [c_char] as *const [u8]),
)
.unwrap();
info!("device name: {:?} info: {:?}", name, info);
Some(Candidate {
physical_device: *physical_device,
queue_family_index,
physical_device_properties: props,
})
} else {
None
}
})
})
.collect::<Vec<_>>();
let index = index.unwrap_or_else(|| {
devices_and_queues
.iter()
.enumerate()
.find(
|(
_,
Candidate {
physical_device_properties,
..
},
)| {
physical_device_properties.device_type == PhysicalDeviceType::DISCRETE_GPU
},
)
.map(|(i, _)| i)
.unwrap_or(0)
});
let selected = &devices_and_queues[index];
DeviceQueueFamilies {
physical_device: selected.physical_device,
queue_family_index: selected.queue_family_index as _,
surface_loader,
}
}