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
|
#![deny(unsafe_code)]
use crate::error::*;
use crate::graphics_permanent::{
PermanentGraphicsState, GraphicsStateForReinit
};
use crate::graphics_window_dressing::{
WindowDressing, N_SIMULTANEOUS_FRAMES
};
use crate::linear_algebra::{ Vec3, Vec4, Mat4, Transformation, UniformBlock };
use std::cell::RefCell;
use std::f32::consts::{ FRAC_PI_2, FRAC_PI_4, TAU };
use std::ptr::copy_nonoverlapping;
use std::time::Instant;
use vulkanalia::Device;
use vulkanalia::vk::{ self, Handle, HasBuilder, DeviceV1_0,
KhrSwapchainExtensionDeviceCommands };
use winit::application::ApplicationHandler;
use winit::event::WindowEvent;
use winit::event_loop::{ ActiveEventLoop, EventLoop };
use winit::window::WindowId;
mod error;
mod graphics_permanent;
mod graphics_window_dressing;
mod linear_algebra;
struct Surreality {
permanent: RefCell<Option<PermanentGraphicsState>>,
for_reinit: RefCell<Option<GraphicsStateForReinit>>,
window_dressing: RefCell<Option<WindowDressing>>,
is_minimized: bool,
is_reinit_queued: bool,
frame_index: usize,
simulation_start: Instant,
}
impl Surreality {
fn new() -> Self {
Surreality {
permanent: RefCell::new(None),
for_reinit: RefCell::new(None),
window_dressing: RefCell::new(None),
is_minimized: false,
is_reinit_queued: false,
frame_index: 0,
simulation_start: Instant::now(),
}
}
#[allow(unsafe_code)]
fn init(&mut self, event_loop: &ActiveEventLoop) -> Result<()> {
let (permanent, for_reinit, enable_swapchain)
= PermanentGraphicsState::new(event_loop)?;
if enable_swapchain.0 {
*self.window_dressing.get_mut()
= Some(WindowDressing::new(&permanent, &for_reinit)?);
}
*self.permanent.get_mut() = Some(permanent);
*self.for_reinit.get_mut() = Some(for_reinit);
Ok(())
}
fn reinit(&mut self) -> Result<()> {
if let Some(permanent) = self.permanent.borrow().as_ref()
&& let Some(for_reinit) = self.for_reinit.borrow().as_ref()
&& let Some(window_dressing)
= self.window_dressing.borrow_mut().as_mut()
{
window_dressing.reinit(permanent, for_reinit)?;
}
Ok(())
}
fn do_frame(&mut self, window_id: WindowId) -> Result<()> {
if !self.is_minimized {
if self.render(window_id)? || self.is_reinit_queued {
self.is_reinit_queued = false;
self.reinit()?;
}
}
Ok(())
}
// This returns true if, and only if, the window dressing should be
// reinitialized.
#[allow(unsafe_code)]
fn render(&mut self, window_id: WindowId) -> Result<bool> {
if let Some(permanent) = self.permanent.borrow().as_ref()
&& let Some(window_dressing)
= self.window_dressing.borrow_mut().as_mut()
&& window_id == permanent.window.id()
{
let device = &permanent.device;
let frame_index = self.frame_index;
let concurrency = &mut window_dressing.concurrency;
let image_available_semaphore
= &concurrency.image_available_semaphores[frame_index];
let rendering_finished_semaphore
= &concurrency.rendering_finished_semaphores[frame_index];
let frame_fence = &concurrency.frame_fences[frame_index];
unsafe { device.wait_for_fences(&[*frame_fence], true, u64::MAX) }?;
let image_index = match unsafe {
device.acquire_next_image_khr(window_dressing.swapchain.swapchain,
u64::MAX, *image_available_semaphore,
vk::Fence::null())
} {
Ok((image_index, _)) => image_index as usize,
Err(vk::ErrorCode::OUT_OF_DATE_KHR) => return Ok(true),
Err(e) => return Err(Error::from(e)),
};
let image_fence = &concurrency.image_fences[image_index];
if !image_fence.is_null() {
unsafe { device.wait_for_fences(&[*image_fence], true, u64::MAX) }?;
}
concurrency.image_fences[image_index] = *frame_fence;
render_uniforms(device,
&window_dressing.uniform_buffer_memory[image_index],
&window_dressing.swapchain.extent,
self.simulation_start)?;
let first_semaphores = [*image_available_semaphore];
let second_semaphores = [*rendering_finished_semaphore];
let wait_stages = [vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT];
let command_buffers = [window_dressing.command_buffers[image_index]];
let submit_info = vk::SubmitInfo::builder()
.wait_semaphores(&first_semaphores)
.wait_dst_stage_mask(&wait_stages)
.command_buffers(&command_buffers)
.signal_semaphores(&second_semaphores);
unsafe { device.reset_fences(&[*frame_fence]) }?;
unsafe {
device.queue_submit(permanent.graphics_queue,
&[submit_info],
*frame_fence)
}?;
let swapchains = [window_dressing.swapchain.swapchain];
let image_indices = [image_index as u32];
let present_info = vk::PresentInfoKHR::builder()
.wait_semaphores(&second_semaphores)
.swapchains(&swapchains)
.image_indices(&image_indices);
match unsafe {
device.queue_present_khr(permanent.presentation_queue, &present_info)
} {
Ok(vk::SuccessCode::SUBOPTIMAL_KHR) => return Ok(true),
Err(vk::ErrorCode::OUT_OF_DATE_KHR) => return Ok(true),
Err(e) => return Err(Error::from(e)),
Ok(_) => (),
}
self.frame_index = (frame_index + 1) % N_SIMULTANEOUS_FRAMES;
}
Ok(false)
}
}
impl Drop for Surreality {
#[allow(unsafe_code)]
fn drop(&mut self) {
if let Some(permanent) = self.permanent.replace(None) {
unsafe { permanent.device.device_wait_idle() }.unwrap();
if let Some(window_dressing) = self.window_dressing.replace(None) {
window_dressing.destroy(&permanent.device);
}
if let Some(for_reinit) = self.for_reinit.replace(None) {
for_reinit.destroy(&permanent.device);
}
permanent.destroy();
}
}
}
impl ApplicationHandler for Surreality {
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
ignore_errors(move || {
self.init(event_loop)?;
Ok(())
});
}
fn window_event(&mut self, event_loop: &ActiveEventLoop,
window_id: WindowId, event: WindowEvent)
{
match event {
WindowEvent::RedrawRequested => {
if !event_loop.exiting() {
if let Err(e) = self.do_frame(window_id) {
eprintln!("Error: {}", e);
}
}
}
WindowEvent::CloseRequested => {
event_loop.exit();
}
WindowEvent::Resized(size) => {
if size.width == 0 || size.height == 0 {
self.is_minimized = true;
} else {
self.is_minimized = false;
self.is_reinit_queued = true;
if let Err(e) = self.do_frame(window_id) {
eprintln!("Error: {}", e);
}
}
}
_ => { }
}
}
fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) {
if let Some(permanent) = self.permanent.borrow().as_ref() {
permanent.window.request_redraw();
}
}
}
fn main() -> std::process::ExitCode {
let body: fn() -> Result<()> = || {
let event_loop = EventLoop::new()?;
let mut surreality = Surreality::new();
event_loop.run_app(&mut surreality)?;
Ok(())
};
match body() {
Ok(()) => std::process::ExitCode::SUCCESS,
Err(e) => {
eprintln!("Error: {}", e);
std::process::ExitCode::from(1)
}
}
}
#[allow(unsafe_code)]
fn render_uniforms(device: &Device, device_memory: &vk::DeviceMemory,
extent: &vk::Extent2D, simulation_start: Instant)
-> Result<()>
{
let time = simulation_start.elapsed().as_secs_f32();
let scale = Vec3::new(1.0, 1.0, 1.0);
let model = Transformation {
rotation: Vec4::rotation_quaternion(&Vec3::new(-1.0, 0.0, 0.0), FRAC_PI_2)
.rotate(&Vec3::new(0.0, 1.0, 0.0), time % TAU),
translation: Vec3::new(0.0, 0.0, 0.0),
};
let view = Transformation {
rotation: Vec4::rotation_quaternion(&Vec3::new(1.0, 0.0, 0.0), 0.1),
translation: Vec3::new(0.0, 0.0, 2.0),
};
let aspect_ratio = extent.width as f32 / extent.height as f32;
let projection = Mat4::perspective(FRAC_PI_4, aspect_ratio, 0.1, 10.0);
let block = UniformBlock::<f32> { scale, model, view, projection };
let size = size_of::<UniformBlock<f32>>() as u64;
let host_memory = unsafe {
device.map_memory(*device_memory, 0, size, vk::MemoryMapFlags::empty())
}?;
unsafe {
copy_nonoverlapping(&block, host_memory.cast(), 1)
};
unsafe { device.unmap_memory(*device_memory) };
Ok(())
}
|