forked from bwasty/vulkan-tutorial-rs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
06_swap_chain_creation.rs.diff
185 lines (174 loc) · 6.73 KB
/
06_swap_chain_creation.rs.diff
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
--- a/05_window_surface.rs
+++ b/06_swap_chain_creation.rs
@@ -21,7 +21,16 @@ use vulkano::instance::debug::{DebugCallback, MessageTypes};
use vulkano::device::{Device, DeviceExtensions, Queue};
use vulkano::swapchain::{
Surface,
+ Capabilities,
+ ColorSpace,
+ SupportedPresentModes,
+ PresentMode,
+ Swapchain,
+ CompositeAlpha,
};
+use vulkano::format::Format;
+use vulkano::image::{ImageUsage, swapchain::SwapchainImage};
+use vulkano::sync::SharingMode;
const WIDTH: u32 = 800;
const HEIGHT: u32 = 600;
@@ -30,6 +39,14 @@ const VALIDATION_LAYERS: &[&str] = &[
"VK_LAYER_LUNARG_standard_validation"
];
+/// Required device extensions
+fn device_extensions() -> DeviceExtensions {
+ DeviceExtensions {
+ khr_swapchain: true,
+ .. vulkano::device::DeviceExtensions::none()
+ }
+}
+
#[cfg(all(debug_assertions))]
const ENABLE_VALIDATION_LAYERS: bool = true;
#[cfg(not(debug_assertions))]
@@ -62,6 +79,9 @@ struct HelloTriangleApplication {
graphics_queue: Arc<Queue>,
present_queue: Arc<Queue>,
+
+ swap_chain: Arc<Swapchain<Window>>,
+ swap_chain_images: Vec<Arc<SwapchainImage<Window>>>,
}
impl HelloTriangleApplication {
@@ -74,6 +94,9 @@ impl HelloTriangleApplication {
let (device, graphics_queue, present_queue) = Self::create_logical_device(
&instance, &surface, physical_device_index);
+ let (swap_chain, swap_chain_images) = Self::create_swap_chain(&instance, &surface, physical_device_index,
+ &device, &graphics_queue, &present_queue);
+
Self {
instance,
debug_callback,
@@ -86,6 +109,9 @@ impl HelloTriangleApplication {
graphics_queue,
present_queue,
+
+ swap_chain,
+ swap_chain_images,
}
}
@@ -157,7 +183,110 @@ impl HelloTriangleApplication {
fn is_device_suitable(surface: &Arc<Surface<Window>>, device: &PhysicalDevice) -> bool {
let indices = Self::find_queue_families(surface, device);
- indices.is_complete()
+ let extensions_supported = Self::check_device_extension_support(device);
+
+ let swap_chain_adequate = if extensions_supported {
+ let capabilities = surface.capabilities(*device)
+ .expect("failed to get surface capabilities");
+ !capabilities.supported_formats.is_empty() &&
+ capabilities.present_modes.iter().next().is_some()
+ } else {
+ false
+ };
+
+ indices.is_complete() && extensions_supported && swap_chain_adequate
+ }
+
+ fn check_device_extension_support(device: &PhysicalDevice) -> bool {
+ let available_extensions = DeviceExtensions::supported_by_device(*device);
+ let device_extensions = device_extensions();
+ available_extensions.intersection(&device_extensions) == device_extensions
+ }
+
+ fn choose_swap_surface_format(available_formats: &[(Format, ColorSpace)]) -> (Format, ColorSpace) {
+ // NOTE: the 'preferred format' mentioned in the tutorial doesn't seem to be
+ // queryable in Vulkano (no VK_FORMAT_UNDEFINED enum)
+ *available_formats.iter()
+ .find(|(format, color_space)|
+ *format == Format::B8G8R8A8Unorm && *color_space == ColorSpace::SrgbNonLinear
+ )
+ .unwrap_or_else(|| &available_formats[0])
+ }
+
+ fn choose_swap_present_mode(available_present_modes: SupportedPresentModes) -> PresentMode {
+ if available_present_modes.mailbox {
+ PresentMode::Mailbox
+ } else if available_present_modes.immediate {
+ PresentMode::Immediate
+ } else {
+ PresentMode::Fifo
+ }
+ }
+
+ fn choose_swap_extent(capabilities: &Capabilities) -> [u32; 2] {
+ if let Some(current_extent) = capabilities.current_extent {
+ return current_extent
+ } else {
+ let mut actual_extent = [WIDTH, HEIGHT];
+ actual_extent[0] = capabilities.min_image_extent[0]
+ .max(capabilities.max_image_extent[0].min(actual_extent[0]));
+ actual_extent[1] = capabilities.min_image_extent[1]
+ .max(capabilities.max_image_extent[1].min(actual_extent[1]));
+ actual_extent
+ }
+ }
+
+ fn create_swap_chain(
+ instance: &Arc<Instance>,
+ surface: &Arc<Surface<Window>>,
+ physical_device_index: usize,
+ device: &Arc<Device>,
+ graphics_queue: &Arc<Queue>,
+ present_queue: &Arc<Queue>,
+ ) -> (Arc<Swapchain<Window>>, Vec<Arc<SwapchainImage<Window>>>) {
+ let physical_device = PhysicalDevice::from_index(&instance, physical_device_index).unwrap();
+ let capabilities = surface.capabilities(physical_device)
+ .expect("failed to get surface capabilities");
+
+ let surface_format = Self::choose_swap_surface_format(&capabilities.supported_formats);
+ let present_mode = Self::choose_swap_present_mode(capabilities.present_modes);
+ let extent = Self::choose_swap_extent(&capabilities);
+
+ let mut image_count = capabilities.min_image_count + 1;
+ if capabilities.max_image_count.is_some() && image_count > capabilities.max_image_count.unwrap() {
+ image_count = capabilities.max_image_count.unwrap();
+ }
+
+ let image_usage = ImageUsage {
+ color_attachment: true,
+ .. ImageUsage::none()
+ };
+
+ let indices = Self::find_queue_families(&surface, &physical_device);
+
+ let sharing: SharingMode = if indices.graphics_family != indices.present_family {
+ vec![graphics_queue, present_queue].as_slice().into()
+ } else {
+ graphics_queue.into()
+ };
+
+ let (swap_chain, images) = Swapchain::new(
+ device.clone(),
+ surface.clone(),
+ image_count,
+ surface_format.0, // TODO: color space?
+ extent,
+ 1, // layers
+ image_usage,
+ sharing,
+ capabilities.current_transform,
+ CompositeAlpha::Opaque,
+ present_mode,
+ true, // clipped
+ None,
+ ).expect("failed to create swap chain!");
+
+ (swap_chain, images)
}
fn find_queue_families(surface: &Arc<Surface<Window>>, device: &PhysicalDevice) -> QueueFamilyIndices {
@@ -202,7 +331,7 @@ impl HelloTriangleApplication {
// for us internally.
let (device, mut queues) = Device::new(physical_device, &Features::none(),
- &DeviceExtensions::none(), queue_families)
+ &device_extensions(), queue_families)
.expect("failed to create logical device!");
let graphics_queue = queues.next().unwrap();