-
Notifications
You must be signed in to change notification settings - Fork 44
/
07_image_view.rs
223 lines (194 loc) · 7.46 KB
/
07_image_view.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
use vulkan_tutorial_rust::{
utility, // the mod define some fixed functions that have been learned before.
utility::constants::*,
utility::debug::*,
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::ptr;
// Constants
const WINDOW_TITLE: &'static str = "07.Image View";
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,
swapchain_loader: ash::extensions::khr::Swapchain,
swapchain: vk::SwapchainKHR,
_swapchain_images: Vec<vk::Image>,
_swapchain_format: vk::Format,
_swapchain_extent: vk::Extent2D,
swapchain_imageviews: Vec<vk::ImageView>,
}
impl VulkanApp {
pub fn new(window: &winit::window::Window) -> VulkanApp {
// init vulkan stuff
let entry = ash::Entry::new().unwrap();
let instance = share::create_instance(
&entry,
WINDOW_TITLE,
VALIDATION.is_enable,
&VALIDATION.required_validation_layers.to_vec(),
);
let surface_stuff =
share::create_surface(&entry, &instance, &window, WINDOW_WIDTH, WINDOW_HEIGHT);
let (debug_utils_loader, debug_merssager) =
setup_debug_utils(VALIDATION.is_enable, &entry, &instance);
let physical_device =
share::pick_physical_device(&instance, &surface_stuff, &DEVICE_EXTENSIONS);
let (device, family_indices) = share::create_logical_device(
&instance,
physical_device,
&VALIDATION,
&DEVICE_EXTENSIONS,
&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) };
let swapchain_stuff = share::create_swapchain(
&instance,
&device,
physical_device,
&window,
&surface_stuff,
&family_indices,
);
let swapchain_imageviews = VulkanApp::create_image_views(
&device,
swapchain_stuff.swapchain_format,
&swapchain_stuff.swapchain_images,
);
// 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,
swapchain_loader: swapchain_stuff.swapchain_loader,
swapchain: swapchain_stuff.swapchain,
_swapchain_format: swapchain_stuff.swapchain_format,
_swapchain_images: swapchain_stuff.swapchain_images,
_swapchain_extent: swapchain_stuff.swapchain_extent,
swapchain_imageviews,
}
}
fn create_image_views(
device: &ash::Device,
surface_format: vk::Format,
images: &Vec<vk::Image>,
) -> Vec<vk::ImageView> {
let mut swapchain_imageviews = vec![];
for &image in images.iter() {
let imageview_create_info = vk::ImageViewCreateInfo {
s_type: vk::StructureType::IMAGE_VIEW_CREATE_INFO,
p_next: ptr::null(),
flags: vk::ImageViewCreateFlags::empty(),
view_type: vk::ImageViewType::TYPE_2D,
format: surface_format,
components: vk::ComponentMapping {
r: vk::ComponentSwizzle::IDENTITY,
g: vk::ComponentSwizzle::IDENTITY,
b: vk::ComponentSwizzle::IDENTITY,
a: vk::ComponentSwizzle::IDENTITY,
},
subresource_range: vk::ImageSubresourceRange {
aspect_mask: vk::ImageAspectFlags::COLOR,
base_mip_level: 0,
level_count: 1,
base_array_layer: 0,
layer_count: 1,
},
image,
};
let imageview = unsafe {
device
.create_image_view(&imageview_create_info, None)
.expect("Failed to create Image View!")
};
swapchain_imageviews.push(imageview);
}
swapchain_imageviews
}
fn draw_frame(&mut self) {
// Drawing will be here
}
}
impl Drop for VulkanApp {
fn drop(&mut self) {
unsafe {
for &imageview in self.swapchain_imageviews.iter() {
self.device.destroy_image_view(imageview, None);
}
self.swapchain_loader
.destroy_swapchain(self.swapchain, None);
self.device.destroy_device(None);
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);
}
// -------------------------------------------------------------------------------------------