forked from bwasty/vulkan-tutorial-rs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
02_validation_layers.rs
142 lines (117 loc) · 4.21 KB
/
02_validation_layers.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
extern crate vulkano;
extern crate vulkano_win;
extern crate winit;
use std::sync::Arc;
use winit::{EventsLoop, WindowBuilder, dpi::LogicalSize, Event, WindowEvent};
use vulkano::instance::{
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,
}
}
fn init_window() -> EventsLoop {
let events_loop = EventsLoop::new();
let _window_builder = WindowBuilder::new()
.with_title("Vulkan")
.with_dimensions(LogicalSize::new(f64::from(WIDTH), f64::from(HEIGHT)));
// .build(&self.events_loop.as_ref().unwrap());
events_loop
}
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);
let app_info = ApplicationInfo {
application_name: Some("Hello Triangle".into()),
application_version: Some(Version { major: 1, minor: 0, patch: 0 }),
engine_name: Some("No Engine".into()),
engine_version: Some(Version { major: 1, minor: 0, patch: 0 }),
};
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)]
fn main_loop(&mut self) {
loop {
let mut done = false;
self.events_loop.poll_events(|ev| {
match ev {
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => done = true,
_ => ()
}
});
if done {
return;
}
}
}
}
fn main() {
let mut _app = HelloTriangleApplication::initialize();
// app.main_loop();
}