From 28c6cc35f366ff8e3c2c454e65842a82ed574637 Mon Sep 17 00:00:00 2001 From: Janis Date: Sat, 21 Dec 2024 14:10:00 +0100 Subject: [PATCH] optional extent when recreating swapchain --- crates/game/src/main.rs | 4 ++-- crates/renderer/src/lib.rs | 31 +++++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/crates/game/src/main.rs b/crates/game/src/main.rs index e8c0534..297c44c 100644 --- a/crates/game/src/main.rs +++ b/crates/game/src/main.rs @@ -40,10 +40,10 @@ impl WindowState { _ = (window_id, new_size); info!("TODO: implement resize events"); if let Some(ctx) = self.renderer.window_contexts.get_mut(&window_id) { - ctx.recreate_with(renderer::Extent2D { + ctx.recreate_with(Some(renderer::Extent2D { width: new_size.width, height: new_size.height, - }) + })) .expect("swapchain recreation"); } } diff --git a/crates/renderer/src/lib.rs b/crates/renderer/src/lib.rs index 0171025..32d1a2c 100644 --- a/crates/renderer/src/lib.rs +++ b/crates/renderer/src/lib.rs @@ -541,6 +541,15 @@ impl Device { .for_each(|q| unsafe { q.0.force_unlock() }); } + fn wait_queue_idle(&self, queue: &Queue) -> VkResult<()> { + tracing::warn!("locking queue {queue:?} and waiting for idle"); + + queue.with_locked(|q| unsafe { self.dev().queue_wait_idle(q) })?; + + tracing::warn!("finished waiting: unlocking queue {queue:?}."); + Ok(()) + } + fn wait_idle(&self) -> VkResult<()> { tracing::warn!("locking all queues and waiting for device to idle"); unsafe { @@ -639,13 +648,15 @@ pub struct Swapchain { fences: Vec>, current_frame: AtomicU32, + + // for khr_present_id/khr_present_wait present_id: AtomicU64, } impl Drop for Swapchain { fn drop(&mut self) { unsafe { - self.device.wait_idle(); + self.device.wait_queue_idle(self.device.present_queue()); info!("dropping swapchain {:?}", self.swapchain); for view in &self.image_views { self.device.dev().destroy_image_view(*view, None); @@ -723,7 +734,7 @@ impl Swapchain { instance: &Arc, surface: vk::SurfaceKHR, pdev: vk::PhysicalDevice, - requested_extent: vk::Extent2D, + requested_extent: Option, ) -> Result { let caps = unsafe { instance @@ -770,7 +781,11 @@ impl Swapchain { + Self::PREFERRED_IMAGES_IN_FLIGHT) .min(max_image_count); - let extent = current_extent_or_clamped(&caps, requested_extent); + let extent = current_extent_or_clamped( + &caps, + requested_extent + .unwrap_or(vk::Extent2D::default().width(1).height(1)), + ); Ok(SwapchainParams { present_mode, @@ -789,7 +804,7 @@ impl Swapchain { pdev: vk::PhysicalDevice, extent: vk::Extent2D, ) -> Result { - Self::create(instance, device, surface, pdev, extent, None) + Self::create(instance, device, surface, pdev, Some(extent), None) } fn create( @@ -797,7 +812,7 @@ impl Swapchain { device: Device, surface: Arc, pdev: vk::PhysicalDevice, - extent: vk::Extent2D, + extent: Option, old_swapchain: Option<&SwapchainHandle>, ) -> Result { let SwapchainParams { @@ -962,7 +977,7 @@ impl Swapchain { self.images.len() as u32 } - fn recreate(&self, extent: vk::Extent2D) -> Result { + fn recreate(&self, extent: Option) -> Result { Self::create( self.instance.clone(), self.device.clone(), @@ -1975,14 +1990,14 @@ impl WindowContext { let mut lock = self.current_swapchain.write(); // only recreate our swapchain if it is still same, or else it might have already been recreated. if Arc::ptr_eq(&swapchain, &lock) { - *lock = Arc::new(lock.recreate(lock.extent)?); + *lock = Arc::new(lock.recreate(None)?); } } Ok(frame) } - pub fn recreate_with(&self, extent: vk::Extent2D) -> Result<()> { + pub fn recreate_with(&self, extent: Option) -> Result<()> { let mut swapchain = self.current_swapchain.write(); *swapchain = Arc::new(swapchain.recreate(extent)?);