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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use {
super::WindowSurface,
crate::graphics::vulkan_api::Queue,
ash::vk,
ccthw_ash_instance::{LogicalDevice, PhysicalDevice, QueueFamilyInfo},
indoc::indoc,
std::collections::HashMap,
};
pub(super) struct QueueFinder {
graphics_queue_family_index: usize,
presentation_queue_family_index: usize,
families: HashMap<usize, QueueFamilyInfo>,
}
impl QueueFinder {
pub fn device_has_required_queues(
device: &PhysicalDevice,
window_surface: &WindowSurface,
) -> bool {
let has_graphics_queue =
Self::find_graphics_queue_family_index(device).is_some();
let has_present_queue =
Self::find_presentation_queue_family_index(window_surface, device)
.is_some();
has_graphics_queue && has_present_queue
}
pub fn new(
device: &PhysicalDevice,
window_surface: &WindowSurface,
) -> Self {
let mut families = HashMap::<usize, QueueFamilyInfo>::new();
let graphics_queue_family_index =
Self::find_graphics_queue_family_index(device).unwrap();
families
.entry(graphics_queue_family_index)
.or_insert_with_key(|&index| QueueFamilyInfo::new(index as u32))
.add_queue_priority(1.0);
let presentation_queue_family_index =
Self::find_presentation_queue_family_index(window_surface, device)
.unwrap();
if presentation_queue_family_index != graphics_queue_family_index {
families
.entry(presentation_queue_family_index)
.or_insert_with_key(|&index| QueueFamilyInfo::new(index as u32))
.add_queue_priority(1.0)
}
Self {
graphics_queue_family_index,
presentation_queue_family_index,
families,
}
}
pub fn get_queues_from_device(
&self,
logical_device: &LogicalDevice,
) -> (Queue, Queue) {
let mut current_indices = HashMap::<usize, usize>::new();
let mut next_index = |family_index| {
let index_ref = current_indices.entry(family_index).or_insert(0);
let index = *index_ref;
*index_ref = index + 1;
index
};
let graphics_queue = Queue::new(
logical_device,
self.graphics_queue_family_index,
next_index(self.graphics_queue_family_index),
);
let presentation_queue = if self.graphics_queue_family_index
!= self.presentation_queue_family_index
{
Queue::new(
logical_device,
self.presentation_queue_family_index,
next_index(self.presentation_queue_family_index),
)
} else {
Queue::new(
logical_device,
graphics_queue.family_index() as usize,
graphics_queue.index() as usize,
)
};
(graphics_queue, presentation_queue)
}
pub fn queue_family_infos(&self) -> Vec<QueueFamilyInfo> {
self.families.values().cloned().collect()
}
}
impl QueueFinder {
fn find_graphics_queue_family_index(
device: &PhysicalDevice,
) -> Option<usize> {
device
.queue_family_properties()
.iter()
.enumerate()
.find(|(_queue_family_index, props)| {
props.queue_flags.contains(vk::QueueFlags::GRAPHICS)
})
.map(|(queue_family_index, _)| queue_family_index)
}
pub fn find_presentation_queue_family_index(
window_surface: &WindowSurface,
device: &PhysicalDevice,
) -> Option<usize> {
device
.queue_family_properties()
.iter()
.enumerate()
.find(|(queue_family_index, props)| {
let result = unsafe {
window_surface.get_physical_device_surface_support(
device,
*queue_family_index,
)
};
if let Err(err) = result {
log::warn!(
indoc!(
"
Error checking for surface support
- device {}
- queue {} [{:?}]
- error {:?}"
),
device,
queue_family_index,
props.queue_flags,
err,
);
false
} else {
result.unwrap()
}
})
.map(|(queue_family_index, _properties)| queue_family_index)
}
}