forked from bwasty/vulkan-tutorial-rs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
03_physical_device_selection.rs.diff
88 lines (79 loc) · 2.26 KB
/
03_physical_device_selection.rs.diff
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
--- a/02_validation_layers.rs
+++ b/03_physical_device_selection.rs
@@ -12,6 +12,7 @@ use vulkano::instance::{
ApplicationInfo,
Version,
layers_list,
+ PhysicalDevice,
};
use vulkano::instance::debug::{DebugCallback, MessageTypes};
@@ -27,12 +28,27 @@ const ENABLE_VALIDATION_LAYERS: bool = true;
#[cfg(not(debug_assertions))]
const ENABLE_VALIDATION_LAYERS: bool = false;
+struct QueueFamilyIndices {
+ graphics_family: i32,
+}
+impl QueueFamilyIndices {
+ fn new() -> Self {
+ Self { graphics_family: -1 }
+ }
+
+ fn is_complete(&self) -> bool {
+ self.graphics_family >= 0
+ }
+}
+
#[allow(unused)]
struct HelloTriangleApplication {
instance: Arc<Instance>,
debug_callback: Option<DebugCallback>,
events_loop: EventsLoop,
+
+ physical_device_index: usize, // can't store PhysicalDevice directly (lifetime issues)
}
impl HelloTriangleApplication {
@@ -42,11 +58,15 @@ impl HelloTriangleApplication {
let events_loop = Self::init_window();
+ let physical_device_index = Self::pick_physical_device(&instance);
+
Self {
instance,
debug_callback,
events_loop,
+
+ physical_device_index,
}
}
@@ -119,6 +139,33 @@ impl HelloTriangleApplication {
}).ok()
}
+ fn pick_physical_device(instance: &Arc<Instance>) -> usize {
+ PhysicalDevice::enumerate(&instance)
+ .position(|device| Self::is_device_suitable(&device))
+ .expect("failed to find a suitable GPU!")
+ }
+
+ fn is_device_suitable(device: &PhysicalDevice) -> bool {
+ let indices = Self::find_queue_families(device);
+ indices.is_complete()
+ }
+
+ fn find_queue_families(device: &PhysicalDevice) -> QueueFamilyIndices {
+ let mut indices = QueueFamilyIndices::new();
+ // TODO: replace index with id to simplify?
+ for (i, queue_family) in device.queue_families().enumerate() {
+ if queue_family.supports_graphics() {
+ indices.graphics_family = i as i32;
+ }
+
+ if indices.is_complete() {
+ break;
+ }
+ }
+
+ indices
+ }
+
#[allow(unused)]
fn main_loop(&mut self) {
loop {