ashen-aetna icon indicating copy to clipboard operation
ashen-aetna copied to clipboard

Handle 0 case for surface_capabilities.max_image_count

Open benjcollins opened this issue 1 year ago • 0 comments

Hi,

The section of code where you create the swap chain was causing the program to crash on my system:

let queuefamilies = [qfamindices.0];
let swapchain_create_info = vk::SwapchainCreateInfoKHR::builder()
    .surface(surface)
    .min_image_count(
        3.max(surface_capabilities.min_image_count)
            .min(surface_capabilities.max_image_count),
    )
    .image_format(surface_formats.first().unwrap().format)
    .image_color_space(surface_formats.first().unwrap().color_space)
    .image_extent(surface_capabilities.current_extent)
    .image_array_layers(1)
    .image_usage(vk::ImageUsageFlags::COLOR_ATTACHMENT)
    .image_sharing_mode(vk::SharingMode::EXCLUSIVE)
    .queue_family_indices(&queuefamilies)
    .pre_transform(surface_capabilities.current_transform)
    .composite_alpha(vk::CompositeAlphaFlagsKHR::OPAQUE)
    .present_mode(vk::PresentModeKHR::FIFO);
let swapchain_loader = ash::extensions::khr::Swapchain::new(&instance, &logical_device);
let swapchain = unsafe { swapchain_loader.create_swapchain(&swapchain_create_info, None)? };

This was due to the GPU on my system returning 0 for max_image_count which meant the code would try to create a swap chain with zero images. In the spec a zero for max_image_count means there is no maximum so the code to select the number of images should account for this.

As well as the current_extent can be set to 0xffffffff to show that the surface size is determined by the parameter passed in which needs to be handled differently as well. This is probably due to using Wayland instead of Xlib since the window is not created until you start drawing to it.

My modified version of this section of the code:

let mut min_image_count = 3.max(surface_capabilities.min_image_count);
if surface_capabilities.max_image_count != 0 {
    min_image_count = min_image_count.max(surface_capabilities.max_image_count);
}

let image_extent = if surface_capabilities.current_extent.width == u32::MAX
    && surface_capabilities.current_extent.height == u32::MAX
{
    vk::Extent2D {
        width: 800,
        height: 600,
    }
} else {
    surface_capabilities.current_extent
};

let queue_families = [queue_index];
let swapchain_create_info = vk::SwapchainCreateInfoKHR::builder()
    .surface(surface.surface)
    .min_image_count(min_image_count)
    .image_format(surface_format.format)
    .image_color_space(surface_format.color_space)
    .image_extent(image_extent)
    .image_array_layers(1)
    .image_usage(vk::ImageUsageFlags::COLOR_ATTACHMENT)
    .image_sharing_mode(vk::SharingMode::EXCLUSIVE)
    .queue_family_indices(&queue_families)
    .pre_transform(surface_capabilities.current_transform)
    .composite_alpha(vk::CompositeAlphaFlagsKHR::OPAQUE)
    .present_mode(vk::PresentModeKHR::FIFO);

benjcollins avatar Sep 18 '23 14:09 benjcollins