optional extent when recreating swapchain

This commit is contained in:
Janis 2024-12-21 14:10:00 +01:00
parent 9d132066d3
commit 28c6cc35f3
2 changed files with 25 additions and 10 deletions

View file

@ -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");
}
}

View file

@ -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<Arc<sync::Fence>>,
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<Instance>,
surface: vk::SurfaceKHR,
pdev: vk::PhysicalDevice,
requested_extent: vk::Extent2D,
requested_extent: Option<vk::Extent2D>,
) -> Result<SwapchainParams> {
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> {
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<Surface>,
pdev: vk::PhysicalDevice,
extent: vk::Extent2D,
extent: Option<vk::Extent2D>,
old_swapchain: Option<&SwapchainHandle>,
) -> Result<Self> {
let SwapchainParams {
@ -962,7 +977,7 @@ impl Swapchain {
self.images.len() as u32
}
fn recreate(&self, extent: vk::Extent2D) -> Result<Self> {
fn recreate(&self, extent: Option<vk::Extent2D>) -> Result<Self> {
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<vk::Extent2D>) -> Result<()> {
let mut swapchain = self.current_swapchain.write();
*swapchain = Arc::new(swapchain.recreate(extent)?);