forked from bwasty/vulkan-tutorial-rs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
02_validation_layers.rs.diff
107 lines (100 loc) · 3.46 KB
/
02_validation_layers.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
--- a/01_instance_creation.rs
+++ b/02_validation_layers.rs
@@ -11,24 +11,41 @@ use vulkano::instance::{
InstanceExtensions,
ApplicationInfo,
Version,
+ layers_list,
};
+use vulkano::instance::debug::{DebugCallback, MessageTypes};
const WIDTH: u32 = 800;
const HEIGHT: u32 = 600;
+const VALIDATION_LAYERS: &[&str] = &[
+ "VK_LAYER_LUNARG_standard_validation"
+];
+
+#[cfg(all(debug_assertions))]
+const ENABLE_VALIDATION_LAYERS: bool = true;
+#[cfg(not(debug_assertions))]
+const ENABLE_VALIDATION_LAYERS: bool = false;
+
#[allow(unused)]
struct HelloTriangleApplication {
instance: Arc<Instance>,
+ debug_callback: Option<DebugCallback>,
+
events_loop: EventsLoop,
}
impl HelloTriangleApplication {
pub fn initialize() -> Self {
let instance = Self::create_instance();
+ let debug_callback = Self::setup_debug_callback(&instance);
+
let events_loop = Self::init_window();
Self {
instance,
+ debug_callback,
+
events_loop,
}
}
@@ -43,6 +60,10 @@ impl HelloTriangleApplication {
}
fn create_instance() -> Arc<Instance> {
+ if ENABLE_VALIDATION_LAYERS && !Self::check_validation_layer_support() {
+ println!("Validation layers requested, but not available!")
+ }
+
let supported_extensions = InstanceExtensions::supported_by_core()
.expect("failed to retrieve supported extensions");
println!("Supported extensions: {:?}", supported_extensions);
@@ -54,9 +75,48 @@ impl HelloTriangleApplication {
engine_version: Some(Version { major: 1, minor: 0, patch: 0 }),
};
- let required_extensions = vulkano_win::required_extensions();
- Instance::new(Some(&app_info), &required_extensions, None)
- .expect("failed to create Vulkan instance")
+ let required_extensions = Self::get_required_extensions();
+
+ if ENABLE_VALIDATION_LAYERS && Self::check_validation_layer_support() {
+ Instance::new(Some(&app_info), &required_extensions, VALIDATION_LAYERS.iter().map(|s| *s))
+ .expect("failed to create Vulkan instance")
+ } else {
+ Instance::new(Some(&app_info), &required_extensions, None)
+ .expect("failed to create Vulkan instance")
+ }
+ }
+
+ fn check_validation_layer_support() -> bool {
+ let layers: Vec<_> = layers_list().unwrap().map(|l| l.name().to_owned()).collect();
+ VALIDATION_LAYERS.iter()
+ .all(|layer_name| layers.contains(&layer_name.to_string()))
+ }
+
+ fn get_required_extensions() -> InstanceExtensions {
+ let mut extensions = vulkano_win::required_extensions();
+ if ENABLE_VALIDATION_LAYERS {
+ // TODO!: this should be ext_debug_utils (_report is deprecated), but that doesn't exist yet in vulkano
+ extensions.ext_debug_report = true;
+ }
+
+ extensions
+ }
+
+ fn setup_debug_callback(instance: &Arc<Instance>) -> Option<DebugCallback> {
+ if !ENABLE_VALIDATION_LAYERS {
+ return None;
+ }
+
+ let msg_types = MessageTypes {
+ error: true,
+ warning: true,
+ performance_warning: true,
+ information: false,
+ debug: true,
+ };
+ DebugCallback::new(&instance, msg_types, |msg| {
+ println!("validation layer: {:?}", msg.description);
+ }).ok()
}
#[allow(unused)]