From 8c2d55b1c92b35e11d4e6fb8072e9b86c4117546 Mon Sep 17 00:00:00 2001 From: itycodes Date: Mon, 31 Mar 2025 14:11:55 +0200 Subject: [PATCH] Broken: Fixed crash, but a race condition in SDL results in VUID violation SDL2 does not finish resizing the surface before it emits WindowEvent::Resized. This means that I cannot actually properly implement resizing. --- src/main.rs | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4081884..ee497da 100644 --- a/src/main.rs +++ b/src/main.rs @@ -533,15 +533,15 @@ fn setup_swapchain( surface_loader: &surface::Instance, swapchain_loader: &ash::khr::swapchain::Device, instance: &Instance, -) -> vk::SwapchainKHR { + width: u32, + height: u32, +) -> (vk::SwapchainKHR, vk::Extent2D) { let caps = unsafe { surface_loader .get_physical_device_surface_capabilities(*pdev, *surface) .expect("Failed to get surface capabilities") }; - println!("Extents: {:?}", caps.current_extent); - let formats = unsafe { surface_loader .get_physical_device_surface_formats(*pdev, *surface) @@ -557,12 +557,20 @@ fn setup_swapchain( .next() .expect("No suitable format found"); + // When the window is resized fast, then the caps goes out of sync of the target extents + // Thus we are using the passed-in (target) extents instead of the cap extents. + // assert_eq!(caps.current_extent.width, width); + // assert_eq!(caps.current_extent.height, height); + let swap_create = vk::SwapchainCreateInfoKHR::default() .surface(*surface) .min_image_count(caps.min_image_count) .image_format(format.format) .image_color_space(format.color_space) - .image_extent(caps.current_extent) + .image_extent(vk::Extent2D { + width: std::cmp::max(caps.current_extent.width, width), + height: std::cmp::max(caps.current_extent.height, height), + }) .image_array_layers(1) .image_usage(vk::ImageUsageFlags::COLOR_ATTACHMENT) .image_sharing_mode(vk::SharingMode::EXCLUSIVE) @@ -577,7 +585,7 @@ fn setup_swapchain( .expect("Failed to create swapchain") }; println!("Created swapchain"); - swapchain + (swapchain, caps.current_extent) } fn setup_desc_pool(dev: &Device) -> vk::DescriptorPool { @@ -1093,13 +1101,15 @@ fn main() { let surface_loader = surface::Instance::new(&entry, &instance); let swapchain_loader = swapchain::Device::new(&instance, &dev); - let mut swapchain = setup_swapchain( + let (mut swapchain, mut extents) = setup_swapchain( &pdev, &dev, &surface, &surface_loader, &swapchain_loader, &instance, + width, + height, ); let desc_pool = setup_desc_pool(&dev); @@ -1145,13 +1155,19 @@ fn main() { } => { width = w as u32; height = h as u32; - swapchain = setup_swapchain( + unsafe { + dev.device_wait_idle().expect("Failed to wait for device idle"); + swapchain_loader.destroy_swapchain(swapchain, None); + } + (swapchain, extents) = setup_swapchain( &pdev, &dev, &surface, &surface_loader, &swapchain_loader, &instance, + width, + height, ); swap_images = make_swap_images(&dev, &swapchain, &swapchain_loader); swap_views = make_swap_views(&dev, &swap_images, vk::Format::B8G8R8A8_UNORM);