From d0871fa4c5aa35b1b0fcf3be988b1dba842b2ea7 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 7 Apr 2023 12:18:44 -0400 Subject: [PATCH] Add more syncing to make sure we aren't wastefully rendering --- src/app.rs | 30 +++++++++++++++++++++++++++++- src/app/functions.rs | 10 ++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/app.rs b/src/app.rs index f1d8efa..51ee29c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -78,6 +78,12 @@ impl App { /// # Safety /// Here be Dragons pub unsafe fn render(&mut self, _window: &Window) -> Result<()> { + self.device.wait_for_fences( + &[self.data.in_flight_fences[self.frame]], + true, + u64::max_value(), + )?; + let image_index = self .device .acquire_next_image_khr( @@ -88,6 +94,16 @@ impl App { )? .0 as usize; + if !self.data.images_in_flight[image_index as usize].is_null() { + self.device.wait_for_fences( + &[self.data.images_in_flight[image_index as usize]], + true, + u64::max_value(), + )?; + } + + self.data.images_in_flight[image_index as usize] = self.data.in_flight_fences[self.frame]; + let wait_semaphores = &[self.data.image_available_semaphores[self.frame]]; let wait_stages = &[vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT]; let command_buffers = &[self.data.command_buffers[image_index as usize]]; @@ -99,7 +115,13 @@ impl App { .signal_semaphores(signal_semaphores); self.device - .queue_submit(self.data.graphics_queue, &[submit_info], vk::Fence::null())?; + .reset_fences(&[self.data.in_flight_fences[self.frame]])?; + + self.device.queue_submit( + self.data.graphics_queue, + &[submit_info], + self.data.in_flight_fences[self.frame], + )?; let swapchains = &[self.data.swapchain]; let image_indices = &[image_index as u32]; @@ -121,6 +143,10 @@ impl App { /// # Safety /// Here be Dragons pub unsafe fn destroy(&mut self) { + self.data + .in_flight_fences + .iter() + .for_each(|f| self.device.destroy_fence(*f, None)); self.data .render_finished_semaphores .iter() @@ -186,6 +212,8 @@ pub struct AppData { // Sync Objects image_available_semaphores: Vec, render_finished_semaphores: Vec, + in_flight_fences: Vec, + images_in_flight: Vec, } impl AppData {} diff --git a/src/app/functions.rs b/src/app/functions.rs index 6394cd0..b34377b 100644 --- a/src/app/functions.rs +++ b/src/app/functions.rs @@ -629,13 +629,23 @@ pub(super) unsafe fn create_command_buffers(device: &Device, data: &mut AppData) pub(super) unsafe fn create_sync_objects(device: &Device, data: &mut AppData) -> Result<()> { let semaphore_info = vk::SemaphoreCreateInfo::builder(); + let fence_info = vk::FenceCreateInfo::builder().flags(vk::FenceCreateFlags::SIGNALED); for _ in 0..MAX_FRAMES_IN_FLIGHT { data.image_available_semaphores .push(device.create_semaphore(&semaphore_info, None)?); data.render_finished_semaphores .push(device.create_semaphore(&semaphore_info, None)?); + + data.in_flight_fences + .push(device.create_fence(&fence_info, None)?); } + data.images_in_flight = data + .swapchain_images + .iter() + .map(|_| vk::Fence::null()) + .collect(); + Ok(()) }