-
Notifications
You must be signed in to change notification settings - Fork 44
/
01_instance_creation.rs
138 lines (114 loc) · 4.24 KB
/
01_instance_creation.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
use vulkan_tutorial_rust::utility;
use vulkan_tutorial_rust::utility::constants::*;
use ash::version::EntryV1_0;
use ash::version::InstanceV1_0;
use ash::vk;
use std::ffi::CString;
use std::ptr;
use winit::event::{Event, VirtualKeyCode, ElementState, KeyboardInput, WindowEvent};
use winit::event_loop::{EventLoop, ControlFlow};
use winit::window::Window;
// Constants
const WINDOW_TITLE: &'static str = "01.Instance Creation";
struct VulkanApp {
_entry: ash::Entry,
instance: ash::Instance,
}
impl VulkanApp {
pub fn new() -> VulkanApp {
// init vulkan stuff
let entry = ash::Entry::new().unwrap();
let instance = VulkanApp::create_instance(&entry);
// cleanup(); the 'drop' function will take care of it.
VulkanApp {
_entry: entry,
instance,
}
}
fn init_window(event_loop: &EventLoop<()>) -> winit::window::Window {
winit::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 {
let app_name = CString::new(WINDOW_TITLE).unwrap();
let engine_name = CString::new("Vulkan Engine").unwrap();
let app_info = vk::ApplicationInfo {
s_type: vk::StructureType::APPLICATION_INFO,
p_next: ptr::null(),
p_application_name: app_name.as_ptr(),
application_version: APPLICATION_VERSION,
p_engine_name: engine_name.as_ptr(),
engine_version: ENGINE_VERSION,
api_version: API_VERSION,
};
let extension_names = utility::platforms::required_extension_names();
let create_info = vk::InstanceCreateInfo {
s_type: vk::StructureType::INSTANCE_CREATE_INFO,
p_next: ptr::null(),
flags: vk::InstanceCreateFlags::empty(),
p_application_info: &app_info,
pp_enabled_layer_names: ptr::null(),
enabled_layer_count: 0,
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 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();
},
_ => (),
}
})
}
}
impl Drop for VulkanApp {
fn drop(&mut self) {
unsafe {
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);
}