forked from unknownue/vulkan-tutorial-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_physical_device_selection.rs
271 lines (231 loc) · 8.66 KB
/
03_physical_device_selection.rs
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
use vulkan_tutorial_rust::{
utility, // the mod define some fixed functions that have been learned before.
utility::constants::*,
utility::share,
};
use ash::version::InstanceV1_0;
use ash::vk;
use ash::{vk_version_major, vk_version_minor, vk_version_patch};
use winit::event::{Event, VirtualKeyCode, ElementState, KeyboardInput, WindowEvent};
use winit::event_loop::{EventLoop, ControlFlow};
// Constants
const WINDOW_TITLE: &'static str = "03.Physical Device Selection";
struct QueueFamilyIndices {
graphics_family: Option<u32>,
}
impl QueueFamilyIndices {
pub fn is_complete(&self) -> bool {
self.graphics_family.is_some()
}
}
struct VulkanApp {
// vulkan stuff
_entry: ash::Entry,
instance: ash::Instance,
debug_utils_loader: ash::extensions::ext::DebugUtils,
debug_merssager: vk::DebugUtilsMessengerEXT,
_physical_device: vk::PhysicalDevice,
}
impl VulkanApp {
pub fn new() -> VulkanApp {
// init vulkan stuff
let entry = ash::Entry::new().unwrap();
let instance = share::create_instance(
&entry,
WINDOW_TITLE,
VALIDATION.is_enable,
&VALIDATION.required_validation_layers.to_vec(),
);
let (debug_utils_loader, debug_merssager) =
utility::debug::setup_debug_utils(VALIDATION.is_enable, &entry, &instance);
let physical_device = VulkanApp::pick_physical_device(&instance);
// cleanup(); the 'drop' function will take care of it.
VulkanApp {
_entry: entry,
instance,
debug_utils_loader,
debug_merssager,
_physical_device: physical_device,
}
}
fn pick_physical_device(instance: &ash::Instance) -> vk::PhysicalDevice {
let physical_devices = unsafe {
instance
.enumerate_physical_devices()
.expect("Failed to enumerate Physical Devices!")
};
println!(
"{} devices (GPU) found with vulkan support.",
physical_devices.len()
);
let mut result = None;
for &physical_device in physical_devices.iter() {
if VulkanApp::is_physical_device_suitable(instance, physical_device) {
if result.is_none() {
result = Some(physical_device)
}
}
}
match result {
None => panic!("Failed to find a suitable GPU!"),
Some(physical_device) => physical_device,
}
}
fn is_physical_device_suitable(
instance: &ash::Instance,
physical_device: vk::PhysicalDevice,
) -> bool {
let device_properties = unsafe { instance.get_physical_device_properties(physical_device) };
let device_features = unsafe { instance.get_physical_device_features(physical_device) };
let device_queue_families =
unsafe { instance.get_physical_device_queue_family_properties(physical_device) };
let device_type = match device_properties.device_type {
vk::PhysicalDeviceType::CPU => "Cpu",
vk::PhysicalDeviceType::INTEGRATED_GPU => "Integrated GPU",
vk::PhysicalDeviceType::DISCRETE_GPU => "Discrete GPU",
vk::PhysicalDeviceType::VIRTUAL_GPU => "Virtual GPU",
vk::PhysicalDeviceType::OTHER => "Unknown",
_ => panic!(),
};
let device_name = utility::tools::vk_to_string(&device_properties.device_name);
println!(
"\tDevice Name: {}, id: {}, type: {}",
device_name, device_properties.device_id, device_type
);
let major_version = vk_version_major!(device_properties.api_version);
let minor_version = vk_version_minor!(device_properties.api_version);
let patch_version = vk_version_patch!(device_properties.api_version);
println!(
"\tAPI Version: {}.{}.{}",
major_version, minor_version, patch_version
);
println!("\tSupport Queue Family: {}", device_queue_families.len());
println!("\t\tQueue Count | Graphics, Compute, Transfer, Sparse Binding");
for queue_family in device_queue_families.iter() {
let is_graphics_support = if queue_family.queue_flags.contains(vk::QueueFlags::GRAPHICS)
{
"support"
} else {
"unsupport"
};
let is_compute_support = if queue_family.queue_flags.contains(vk::QueueFlags::COMPUTE) {
"support"
} else {
"unsupport"
};
let is_transfer_support = if queue_family.queue_flags.contains(vk::QueueFlags::TRANSFER)
{
"support"
} else {
"unsupport"
};
let is_sparse_support = if queue_family
.queue_flags
.contains(vk::QueueFlags::SPARSE_BINDING)
{
"support"
} else {
"unsupport"
};
println!(
"\t\t{}\t | {}, {}, {}, {}",
queue_family.queue_count,
is_graphics_support,
is_compute_support,
is_transfer_support,
is_sparse_support
);
}
// there are plenty of features
println!(
"\tGeometry Shader support: {}",
if device_features.geometry_shader == 1 {
"Support"
} else {
"Unsupport"
}
);
let indices = VulkanApp::find_queue_family(instance, physical_device);
return indices.is_complete();
}
fn find_queue_family(
instance: &ash::Instance,
physical_device: vk::PhysicalDevice,
) -> QueueFamilyIndices {
let queue_families =
unsafe { instance.get_physical_device_queue_family_properties(physical_device) };
let mut queue_family_indices = QueueFamilyIndices {
graphics_family: None,
};
let mut index = 0;
for queue_family in queue_families.iter() {
if queue_family.queue_count > 0
&& queue_family.queue_flags.contains(vk::QueueFlags::GRAPHICS)
{
queue_family_indices.graphics_family = Some(index);
}
if queue_family_indices.is_complete() {
break;
}
index += 1;
}
queue_family_indices
}
fn draw_frame(&mut self) {
// Drawing will be here
}
}
impl Drop for VulkanApp {
fn drop(&mut self) {
unsafe {
if VALIDATION.is_enable {
self.debug_utils_loader
.destroy_debug_utils_messenger(self.debug_merssager, None);
}
self.instance.destroy_instance(None);
}
}
}
// Fix content -------------------------------------------------------------------------------
impl VulkanApp {
pub fn main_loop(mut self, event_loop: EventLoop<()>, window: winit::window::Window) {
event_loop.run(move |event, _, control_flow| {
match event {
| Event::WindowEvent { event, .. } => {
match event {
| WindowEvent::CloseRequested => {
*control_flow = ControlFlow::Exit
},
| WindowEvent::KeyboardInput { input, .. } => {
match input {
| KeyboardInput { virtual_keycode, state, .. } => {
match (virtual_keycode, state) {
| (Some(VirtualKeyCode::Escape), ElementState::Pressed) => {
*control_flow = ControlFlow::Exit
},
| _ => {},
}
},
}
},
| _ => {},
}
},
| Event::MainEventsCleared => {
window.request_redraw();
},
| Event::RedrawRequested(_window_id) => {
self.draw_frame();
},
_ => (),
}
})
}
}
fn main() {
let event_loop = EventLoop::new();
let window = utility::window::init_window(&event_loop, WINDOW_TITLE, WINDOW_WIDTH, WINDOW_HEIGHT);
let vulkan_app = VulkanApp::new();
vulkan_app.main_loop(event_loop, window);
}
// -------------------------------------------------------------------------------------------