Add more syncing to make sure we aren't wastefully rendering

This commit is contained in:
Timothy Warren 2023-04-07 12:18:44 -04:00
parent 61bd125414
commit d0871fa4c5
2 changed files with 39 additions and 1 deletions

View File

@ -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<vk::Semaphore>,
render_finished_semaphores: Vec<vk::Semaphore>,
in_flight_fences: Vec<vk::Fence>,
images_in_flight: Vec<vk::Fence>,
}
impl AppData {}

View File

@ -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(())
}