forked from unknownue/vulkan-tutorial-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
05_window_surface.rs
329 lines (285 loc) · 11 KB
/
05_window_surface.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
use vulkan_tutorial_rust::{
utility, // the mod define some fixed functions that have been learned before.
utility::constants::*,
utility::debug::ValidationInfo,
utility::share,
};
use ash::version::DeviceV1_0;
use ash::version::InstanceV1_0;
use ash::vk;
use winit::event::{Event, VirtualKeyCode, ElementState, KeyboardInput, WindowEvent};
use winit::event_loop::{EventLoop, ControlFlow};
use std::ffi::CString;
use std::os::raw::c_char;
use std::ptr;
// Constants
const WINDOW_TITLE: &'static str = "05.Window Surface";
struct QueueFamilyIndices {
graphics_family: Option<u32>,
present_family: Option<u32>,
}
impl QueueFamilyIndices {
pub fn new() -> QueueFamilyIndices {
QueueFamilyIndices {
graphics_family: None,
present_family: None,
}
}
pub fn is_complete(&self) -> bool {
self.graphics_family.is_some() && self.present_family.is_some()
}
}
struct SurfaceStuff {
surface_loader: ash::extensions::khr::Surface,
surface: vk::SurfaceKHR,
}
struct VulkanApp {
_entry: ash::Entry,
instance: ash::Instance,
surface_loader: ash::extensions::khr::Surface,
surface: vk::SurfaceKHR,
debug_utils_loader: ash::extensions::ext::DebugUtils,
debug_merssager: vk::DebugUtilsMessengerEXT,
_physical_device: vk::PhysicalDevice,
device: ash::Device,
_graphics_queue: vk::Queue,
_present_queue: vk::Queue,
}
impl VulkanApp {
pub fn new(window: &winit::window::Window) -> VulkanApp {
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 surface_stuff = VulkanApp::create_surface(&entry, &instance, &window);
let physical_device = VulkanApp::pick_physical_device(&instance, &surface_stuff);
let (device, family_indices) = VulkanApp::create_logical_device(
&instance,
physical_device,
&VALIDATION,
&surface_stuff,
);
let graphics_queue =
unsafe { device.get_device_queue(family_indices.graphics_family.unwrap(), 0) };
let present_queue =
unsafe { device.get_device_queue(family_indices.present_family.unwrap(), 0) };
// cleanup(); the 'drop' function will take care of it.
VulkanApp {
_entry: entry,
instance,
surface: surface_stuff.surface,
surface_loader: surface_stuff.surface_loader,
debug_utils_loader,
debug_merssager,
_physical_device: physical_device,
device,
_graphics_queue: graphics_queue,
_present_queue: present_queue,
}
}
fn create_surface(
entry: &ash::Entry,
instance: &ash::Instance,
window: &winit::window::Window,
) -> SurfaceStuff {
let surface = unsafe {
utility::platforms::create_surface(entry, instance, window)
.expect("Failed to create surface.")
};
let surface_loader = ash::extensions::khr::Surface::new(entry, instance);
SurfaceStuff {
surface_loader,
surface,
}
}
fn pick_physical_device(
instance: &ash::Instance,
surface_stuff: &SurfaceStuff,
) -> vk::PhysicalDevice {
let physical_devices = unsafe {
instance
.enumerate_physical_devices()
.expect("Failed to enumerate Physical Devices!")
};
let result = physical_devices.iter().find(|physical_device| {
VulkanApp::is_physical_device_suitable(instance, **physical_device, surface_stuff)
});
match result {
Some(p_physical_device) => *p_physical_device,
None => panic!("Failed to find a suitable GPU!"),
}
}
fn is_physical_device_suitable(
instance: &ash::Instance,
physical_device: vk::PhysicalDevice,
surface_stuff: &SurfaceStuff,
) -> bool {
let _device_properties =
unsafe { instance.get_physical_device_properties(physical_device) };
let _device_features = unsafe { instance.get_physical_device_features(physical_device) };
let indices = VulkanApp::find_queue_family(instance, physical_device, surface_stuff);
return indices.is_complete();
}
fn create_logical_device(
instance: &ash::Instance,
physical_device: vk::PhysicalDevice,
validation: &ValidationInfo,
surface_stuff: &SurfaceStuff,
) -> (ash::Device, QueueFamilyIndices) {
let indices = VulkanApp::find_queue_family(instance, physical_device, surface_stuff);
use std::collections::HashSet;
let mut unique_queue_families = HashSet::new();
unique_queue_families.insert(indices.graphics_family.unwrap());
unique_queue_families.insert(indices.present_family.unwrap());
let queue_priorities = [1.0_f32];
let mut queue_create_infos = vec![];
for &queue_family in unique_queue_families.iter() {
let queue_create_info = vk::DeviceQueueCreateInfo {
s_type: vk::StructureType::DEVICE_QUEUE_CREATE_INFO,
p_next: ptr::null(),
flags: vk::DeviceQueueCreateFlags::empty(),
queue_family_index: queue_family,
p_queue_priorities: queue_priorities.as_ptr(),
queue_count: queue_priorities.len() as u32,
};
queue_create_infos.push(queue_create_info);
}
let physical_device_features = vk::PhysicalDeviceFeatures {
..Default::default() // default just enable no feature.
};
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 c_char> = requred_validation_layer_raw_names
.iter()
.map(|layer_name| layer_name.as_ptr())
.collect();
let device_create_info = vk::DeviceCreateInfo {
s_type: vk::StructureType::DEVICE_CREATE_INFO,
p_next: ptr::null(),
flags: vk::DeviceCreateFlags::empty(),
queue_create_info_count: queue_create_infos.len() as u32,
p_queue_create_infos: queue_create_infos.as_ptr(),
enabled_layer_count: if validation.is_enable {
enable_layer_names.len()
} else {
0
} as u32,
pp_enabled_layer_names: if validation.is_enable {
enable_layer_names.as_ptr()
} else {
ptr::null()
},
enabled_extension_count: 0,
pp_enabled_extension_names: ptr::null(),
p_enabled_features: &physical_device_features,
};
let device: ash::Device = unsafe {
instance
.create_device(physical_device, &device_create_info, None)
.expect("Failed to create logical device!")
};
(device, indices)
}
fn find_queue_family(
instance: &ash::Instance,
physical_device: vk::PhysicalDevice,
surface_stuff: &SurfaceStuff,
) -> QueueFamilyIndices {
let queue_families =
unsafe { instance.get_physical_device_queue_family_properties(physical_device) };
let mut queue_family_indices = QueueFamilyIndices::new();
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);
}
let is_present_support = unsafe {
surface_stuff
.surface_loader
.get_physical_device_surface_support(
physical_device,
index as u32,
surface_stuff.surface,
)
};
if queue_family.queue_count > 0 && is_present_support {
queue_family_indices.present_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 {
self.device.destroy_device(None);
// FIXME: The program crash here.
self.surface_loader.destroy_surface(self.surface, None);
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(&window);
vulkan_app.main_loop(event_loop, window);
}
// -------------------------------------------------------------------------------------------