forked from unknownue/vulkan-tutorial-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
02_validation_layers.rs
283 lines (242 loc) · 10 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
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
272
273
274
275
276
277
278
279
280
281
282
283
use vulkan_tutorial_rust::{utility, utility::constants::*, utility::debug::ValidationInfo};
use ash::version::EntryV1_0;
use ash::version::InstanceV1_0;
use ash::vk;
use std::ffi::{CStr, CString};
use std::os::raw::c_void;
use std::ptr;
use winit::event::{Event, VirtualKeyCode, ElementState, KeyboardInput, WindowEvent};
use winit::event_loop::{EventLoop, ControlFlow};
use winit::window::{Window, WindowBuilder};
// Constants
const WINDOW_TITLE: &'static str = "02.Validation Layers";
const VALIDATION: ValidationInfo = ValidationInfo {
is_enable: true,
required_validation_layers: ["VK_LAYER_KHRONOS_validation"],
};
/// the callback function used in Debug Utils.
unsafe extern "system" fn vulkan_debug_utils_callback(
message_severity: vk::DebugUtilsMessageSeverityFlagsEXT,
message_type: vk::DebugUtilsMessageTypeFlagsEXT,
p_callback_data: *const vk::DebugUtilsMessengerCallbackDataEXT,
_p_user_data: *mut c_void,
) -> vk::Bool32 {
let severity = match message_severity {
vk::DebugUtilsMessageSeverityFlagsEXT::VERBOSE => "[Verbose]",
vk::DebugUtilsMessageSeverityFlagsEXT::WARNING => "[Warning]",
vk::DebugUtilsMessageSeverityFlagsEXT::ERROR => "[Error]",
vk::DebugUtilsMessageSeverityFlagsEXT::INFO => "[Info]",
_ => "[Unknown]",
};
let types = match message_type {
vk::DebugUtilsMessageTypeFlagsEXT::GENERAL => "[General]",
vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE => "[Performance]",
vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION => "[Validation]",
_ => "[Unknown]",
};
let message = CStr::from_ptr((*p_callback_data).p_message);
println!("[Debug]{}{}{:?}", severity, types, message);
vk::FALSE
}
struct VulkanApp {
_entry: ash::Entry,
instance: ash::Instance,
debug_utils_loader: ash::extensions::ext::DebugUtils,
debug_merssager: vk::DebugUtilsMessengerEXT,
}
impl VulkanApp {
pub fn new() -> VulkanApp {
// init vulkan stuff
let entry = ash::Entry::new().unwrap();
let instance = VulkanApp::create_instance(&entry);
let (debug_utils_loader, debug_merssager) = VulkanApp::setup_debug_utils(&entry, &instance);
// cleanup(); the 'drop' function will take care of it.
VulkanApp {
_entry: entry,
instance,
debug_utils_loader,
debug_merssager,
}
}
fn init_window(event_loop: &EventLoop<()>) -> Window {
WindowBuilder::new()
.with_title(WINDOW_TITLE)
.with_inner_size(winit::dpi::LogicalSize::new(WINDOW_WIDTH, WINDOW_HEIGHT))
.build(event_loop)
.expect("Failed to create window.")
}
fn create_instance(entry: &ash::Entry) -> ash::Instance {
if VALIDATION.is_enable && VulkanApp::check_validation_layer_support(entry) == false {
panic!("Validation layers requested, but not available!");
}
let app_name = CString::new(WINDOW_TITLE).unwrap();
let engine_name = CString::new("Vulkan Engine").unwrap();
let app_info = vk::ApplicationInfo {
p_application_name: app_name.as_ptr(),
s_type: vk::StructureType::APPLICATION_INFO,
p_next: ptr::null(),
application_version: APPLICATION_VERSION,
p_engine_name: engine_name.as_ptr(),
engine_version: ENGINE_VERSION,
api_version: API_VERSION, // set api_version to vk_make_version!(2, 0, 92) to test if the p_next field in vk::InstanceCreateInfo works.
};
// This create info used to debug issues in vk::createInstance and vk::destroyInstance.
let debug_utils_create_info = populate_debug_messenger_create_info();
// VK_EXT debug utils has been requested here.
let extension_names = utility::platforms::required_extension_names();
let requred_validation_layer_raw_names: Vec<CString> = VALIDATION
.required_validation_layers
.iter()
.map(|layer_name| CString::new(*layer_name).unwrap())
.collect();
let enable_layer_names: Vec<*const i8> = requred_validation_layer_raw_names
.iter()
.map(|layer_name| layer_name.as_ptr())
.collect();
let create_info = vk::InstanceCreateInfo {
s_type: vk::StructureType::INSTANCE_CREATE_INFO,
p_next: if VALIDATION.is_enable {
&debug_utils_create_info as *const vk::DebugUtilsMessengerCreateInfoEXT
as *const c_void
} else {
ptr::null()
},
flags: vk::InstanceCreateFlags::empty(),
p_application_info: &app_info,
pp_enabled_layer_names: if VALIDATION.is_enable {
enable_layer_names.as_ptr()
} else {
ptr::null()
},
enabled_layer_count: if VALIDATION.is_enable {
enable_layer_names.len()
} else {
0
} as u32,
pp_enabled_extension_names: extension_names.as_ptr(),
enabled_extension_count: extension_names.len() as u32,
};
let instance: ash::Instance = unsafe {
entry
.create_instance(&create_info, None)
.expect("Failed to create Instance!")
};
instance
}
fn check_validation_layer_support(entry: &ash::Entry) -> bool {
// if support validation layer, then return true
let layer_properties = entry
.enumerate_instance_layer_properties()
.expect("Failed to enumerate Instance Layers Properties!");
if layer_properties.len() <= 0 {
eprintln!("No available layers.");
return false;
} else {
println!("Instance Available Layers: ");
for layer in layer_properties.iter() {
let layer_name = utility::tools::vk_to_string(&layer.layer_name);
println!("\t{}", layer_name);
}
}
for required_layer_name in VALIDATION.required_validation_layers.iter() {
let mut is_layer_found = false;
for layer_property in layer_properties.iter() {
let test_layer_name = utility::tools::vk_to_string(&layer_property.layer_name);
if (*required_layer_name) == test_layer_name {
is_layer_found = true;
break;
}
}
if is_layer_found == false {
return false;
}
}
true
}
fn setup_debug_utils(
entry: &ash::Entry,
instance: &ash::Instance,
) -> (ash::extensions::ext::DebugUtils, vk::DebugUtilsMessengerEXT) {
let debug_utils_loader = ash::extensions::ext::DebugUtils::new(entry, instance);
if VALIDATION.is_enable == false {
(debug_utils_loader, ash::vk::DebugUtilsMessengerEXT::null())
} else {
let messenger_ci = populate_debug_messenger_create_info();
let utils_messenger = unsafe {
debug_utils_loader
.create_debug_utils_messenger(&messenger_ci, None)
.expect("Debug Utils Callback")
};
(debug_utils_loader, utils_messenger)
}
}
fn draw_frame(&mut self) {
// Drawing will be here
}
pub fn main_loop(mut self, event_loop: EventLoop<()>, 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 populate_debug_messenger_create_info() -> vk::DebugUtilsMessengerCreateInfoEXT {
vk::DebugUtilsMessengerCreateInfoEXT {
s_type: vk::StructureType::DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
p_next: ptr::null(),
flags: vk::DebugUtilsMessengerCreateFlagsEXT::empty(),
message_severity: vk::DebugUtilsMessageSeverityFlagsEXT::WARNING |
// vk::DebugUtilsMessageSeverityFlagsEXT::VERBOSE |
// vk::DebugUtilsMessageSeverityFlagsEXT::INFO |
vk::DebugUtilsMessageSeverityFlagsEXT::ERROR,
message_type: vk::DebugUtilsMessageTypeFlagsEXT::GENERAL
| vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE
| vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION,
pfn_user_callback: Some(vulkan_debug_utils_callback),
p_user_data: ptr::null_mut(),
}
}
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);
}
}
}
fn main() {
let event_loop = EventLoop::new();
let window = VulkanApp::init_window(&event_loop);
let vulkan_app = VulkanApp::new();
vulkan_app.main_loop(event_loop, window);
}