forked from bwasty/vulkan-tutorial-rs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
16_swap_chain_recreation.rs.diff
156 lines (138 loc) · 5.93 KB
/
16_swap_chain_recreation.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
--- a/15_hello_triangle.rs
+++ b/16_swap_chain_recreation.rs
@@ -31,10 +31,11 @@ use vulkano::swapchain::{
Swapchain,
CompositeAlpha,
acquire_next_image,
+ AcquireError,
};
use vulkano::format::Format;
use vulkano::image::{ImageUsage, swapchain::SwapchainImage};
-use vulkano::sync::{SharingMode, GpuFuture};
+use vulkano::sync::{self, SharingMode, GpuFuture};
use vulkano::pipeline::{
GraphicsPipeline,
vertex::BufferlessDefinition,
@@ -90,9 +91,9 @@ impl QueueFamilyIndices {
type ConcreteGraphicsPipeline = GraphicsPipeline<BufferlessDefinition, Box<PipelineLayoutAbstract + Send + Sync + 'static>, Arc<RenderPassAbstract + Send + Sync + 'static>>;
-#[allow(unused)]
struct HelloTriangleApplication {
instance: Arc<Instance>,
+ #[allow(unused)]
debug_callback: Option<DebugCallback>,
events_loop: EventsLoop,
@@ -117,6 +118,9 @@ struct HelloTriangleApplication {
swap_chain_framebuffers: Vec<Arc<FramebufferAbstract + Send + Sync>>,
command_buffers: Vec<Arc<AutoCommandBuffer>>,
+
+ previous_frame_end: Option<Box<GpuFuture>>,
+ recreate_swap_chain: bool,
}
impl HelloTriangleApplication {
@@ -130,13 +134,15 @@ impl HelloTriangleApplication {
&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);
+ &device, &graphics_queue, &present_queue, None);
let render_pass = Self::create_render_pass(&device, swap_chain.format());
let graphics_pipeline = Self::create_graphics_pipeline(&device, swap_chain.dimensions(), &render_pass);
let swap_chain_framebuffers = Self::create_framebuffers(&swap_chain_images, &render_pass);
+ let previous_frame_end = Some(Self::create_sync_objects(&device));
+
let mut app = Self {
instance,
debug_callback,
@@ -159,6 +165,9 @@ impl HelloTriangleApplication {
swap_chain_framebuffers,
command_buffers: vec![],
+
+ previous_frame_end,
+ recreate_swap_chain: false,
};
app.create_command_buffers();
@@ -293,6 +302,7 @@ impl HelloTriangleApplication {
device: &Arc<Device>,
graphics_queue: &Arc<Queue>,
present_queue: &Arc<Queue>,
+ old_swapchain: Option<Arc<Swapchain<Window>>>,
) -> (Arc<Swapchain<Window>>, Vec<Arc<SwapchainImage<Window>>>) {
let physical_device = PhysicalDevice::from_index(&instance, physical_device_index).unwrap();
let capabilities = surface.capabilities(physical_device)
@@ -333,7 +343,7 @@ impl HelloTriangleApplication {
CompositeAlpha::Opaque,
present_mode,
true, // clipped
- None,
+ old_swapchain.as_ref()
).expect("failed to create swap chain!");
(swap_chain, images)
@@ -444,6 +454,10 @@ impl HelloTriangleApplication {
.collect();
}
+ fn create_sync_objects(device: &Arc<Device>) -> Box<GpuFuture> {
+ Box::new(sync::now(device.clone())) as Box<GpuFuture>
+ }
+
fn find_queue_families(surface: &Arc<Surface<Window>>, device: &PhysicalDevice) -> QueueFamilyIndices {
let mut indices = QueueFamilyIndices::new();
// TODO: replace index with id to simplify?
@@ -524,18 +538,61 @@ impl HelloTriangleApplication {
}
fn draw_frame(&mut self) {
- let (image_index, acquire_future) = acquire_next_image(self.swap_chain.clone(), None).unwrap();
+ self.previous_frame_end.as_mut().unwrap().cleanup_finished();
+
+ if self.recreate_swap_chain {
+ self.recreate_swap_chain();
+ self.recreate_swap_chain = false;
+ }
+
+ let (image_index, acquire_future) = match acquire_next_image(self.swap_chain.clone(), None) {
+ Ok(r) => r,
+ Err(AcquireError::OutOfDate) => {
+ self.recreate_swap_chain = true;
+ return;
+ },
+ Err(err) => panic!("{:?}", err)
+ };
let command_buffer = self.command_buffers[image_index].clone();
- let future = acquire_future
+ let future = self.previous_frame_end.take().unwrap()
+ .join(acquire_future)
.then_execute(self.graphics_queue.clone(), command_buffer)
.unwrap()
.then_swapchain_present(self.present_queue.clone(), self.swap_chain.clone(), image_index)
- .then_signal_fence_and_flush()
- .unwrap();
+ .then_signal_fence_and_flush();
+
+ match future {
+ Ok(future) => {
+ self.previous_frame_end = Some(Box::new(future) as Box<_>);
+ }
+ Err(vulkano::sync::FlushError::OutOfDate) => {
+ self.recreate_swap_chain = true;
+ self.previous_frame_end
+ = Some(Box::new(vulkano::sync::now(self.device.clone())) as Box<_>);
+ }
+ Err(e) => {
+ println!("{:?}", e);
+ self.previous_frame_end
+ = Some(Box::new(vulkano::sync::now(self.device.clone())) as Box<_>);
+ }
+ }
+ }
+
+ fn recreate_swap_chain(&mut self) {
+ let (swap_chain, images) = Self::create_swap_chain(&self.instance, &self.surface, self.physical_device_index,
+ &self.device, &self.graphics_queue, &self.present_queue, Some(self.swap_chain.clone()));
+ self.swap_chain = swap_chain;
+ self.swap_chain_images = images;
- future.wait(None).unwrap();
+ self.render_pass = Self::create_render_pass(&self.device, self.swap_chain.format());
+ self.graphics_pipeline = Self::create_graphics_pipeline(&self.device, self.swap_chain.dimensions(),
+ &self.render_pass);
+ self.swap_chain_framebuffers = Self::create_framebuffers(&self.swap_chain_images, &self.render_pass);
+ self.create_command_buffers();
}
}