Refactor
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 0s

This commit is contained in:
Florian RICHER 2024-11-17 14:07:37 +01:00
parent e9ce480f96
commit 210a5504ab
Signed by: florian.richer
GPG key ID: C73D37CBED7BFC77
6 changed files with 162 additions and 90 deletions

View file

@ -1,4 +1,5 @@
use crate::display::Window;
use crate::vulkan::vk_surface::SwapchainSupportDetails;
use crate::vulkan::{VkDevice, VkPhysicalDevice, VkSurface, LOG_TARGET};
use ash::prelude::VkResult;
use ash::vk;
@ -8,10 +9,11 @@ pub struct VkSwapchain {
surface: Arc<VkSurface>,
device: Arc<VkDevice>,
swapchain: Option<vk::SwapchainKHR>,
swapchain_support_details: SwapchainSupportDetails,
pub(super) desired_image_count: u32,
pub(super) surface_format: vk::SurfaceFormatKHR,
pub(super) surface_resolution: vk::Extent2D,
pub(super) swapchain_extent: vk::Extent2D,
pub(super) present_mode: vk::PresentModeKHR,
pub(super) pre_transform: vk::SurfaceTransformFlagsKHR,
@ -28,64 +30,42 @@ impl VkSwapchain {
) -> anyhow::Result<Self> {
log::debug!(target: LOG_TARGET, "Creating swapchain");
let (surface_formats, surface_capabilities, present_modes) =
surface.get_physical_device_surface_infos(physical_device)?;
let window_size = window
.physical_size::<u32>()
.and_then(|size| {
Some(vk::Extent2D {
width: size.width,
height: size.height,
})
})
.ok_or_else(|| anyhow::anyhow!("Failed to get swapchain extent"))?;
log::debug!(target: LOG_TARGET, "Window size ({}x{})", window_size.width, window_size.height);
let swapchain_support_details =
surface.get_physical_device_swapchain_support_details(physical_device)?;
let SwapchainSupportDetails(surface_formats, surface_capabilities, present_modes) =
&swapchain_support_details;
log::debug!(target: LOG_TARGET, "Supported surface formats by physical device: {surface_formats:#?}");
log::debug!(target: LOG_TARGET, "Surface capabilities: {surface_capabilities:#?}");
log::debug!(target: LOG_TARGET, "Present modes: {present_modes:#?}");
let surface_format = surface_formats
.first()
.and_then(|f| Some(*f))
let surface_format = Self::choose_surface_format(surface_formats)
.ok_or_else(|| anyhow::anyhow!("No available surface formats"))?;
log::debug!(target: LOG_TARGET, "Selected surface format: {surface_format:?}");
let mut desired_image_count = surface_capabilities.min_image_count + 1;
if surface_capabilities.max_image_count > 0
&& desired_image_count > surface_capabilities.max_image_count
{
desired_image_count = surface_capabilities.max_image_count;
}
log::debug!(target: LOG_TARGET, "Selected surface image count: {desired_image_count}");
let window_size = window
.size()
.ok_or_else(|| anyhow::anyhow!("Window size is not valid"))?
.to_physical::<u32>(1.0);
log::debug!(target: LOG_TARGET, "Window size: {window_size:?}");
let surface_resolution = match surface_capabilities.current_extent.width {
u32::MAX => vk::Extent2D {
width: window_size.width,
height: window_size.height,
},
_ => surface_capabilities.current_extent,
};
log::debug!(target: LOG_TARGET, "Surface resolution: {surface_resolution:?}");
let pre_transform = if surface_capabilities
.supported_transforms
.contains(vk::SurfaceTransformFlagsKHR::IDENTITY)
{
vk::SurfaceTransformFlagsKHR::IDENTITY
} else {
surface_capabilities.current_transform
};
let present_mode = present_modes
.iter()
.cloned()
.find(|&mode| mode == vk::PresentModeKHR::MAILBOX)
.unwrap_or(vk::PresentModeKHR::FIFO);
let desired_image_count = Self::choose_desired_image_count(surface_capabilities);
let swapchain_extent = Self::choose_swapchain_extent(window_size, surface_capabilities);
let pre_transform = Self::choose_pre_transform(surface_capabilities);
let present_mode = Self::choose_present_mode(present_modes);
let mut swapchain = Self {
surface,
device,
swapchain: None,
swapchain_support_details,
desired_image_count,
surface_format,
surface_resolution,
swapchain_extent,
present_mode,
pre_transform,
swapchain: None,
present_images: None,
present_image_views: None,
};
@ -138,10 +118,22 @@ impl VkSwapchain {
}
pub(super) fn update_resolution(&mut self, width: u32, height: u32) -> VkResult<()> {
log::debug!(target: LOG_TARGET, "New resolution requested for swapchain {width}x{height}");
self.surface_resolution = vk::Extent2D { width, height };
log::debug!(target: LOG_TARGET, "New resolution requested ({width}x{height})");
self.create_swapchain()?;
let chosen_extent = Self::choose_swapchain_extent(
vk::Extent2D { width, height },
&self.swapchain_support_details.1,
);
if chosen_extent.width != self.swapchain_extent.width
|| chosen_extent.height != self.swapchain_extent.height
{
self.swapchain_extent = chosen_extent;
log::debug!(target: LOG_TARGET, "New resolution applied ({}x{})", chosen_extent.width, chosen_extent.height);
self.create_swapchain()?;
} else {
log::debug!(target: LOG_TARGET, "New resolution skipped ({width}x{height}) : Same resolution");
}
Ok(())
}
@ -152,7 +144,7 @@ impl VkSwapchain {
.min_image_count(self.desired_image_count)
.image_color_space(self.surface_format.color_space)
.image_format(self.surface_format.format)
.image_extent(self.surface_resolution)
.image_extent(self.swapchain_extent)
.image_usage(vk::ImageUsageFlags::COLOR_ATTACHMENT)
.image_sharing_mode(vk::SharingMode::EXCLUSIVE)
.pre_transform(self.pre_transform)
@ -161,6 +153,65 @@ impl VkSwapchain {
.clipped(true)
.image_array_layers(1)
}
fn choose_swapchain_extent(
window_size: vk::Extent2D,
surface_capabilities: &vk::SurfaceCapabilitiesKHR,
) -> vk::Extent2D {
vk::Extent2D {
width: window_size
.width
.max(surface_capabilities.min_image_extent.width)
.min(surface_capabilities.max_image_extent.width),
height: window_size
.height
.max(surface_capabilities.min_image_extent.height)
.min(surface_capabilities.max_image_extent.height),
}
}
fn choose_surface_format(
surface_formats: &Vec<vk::SurfaceFormatKHR>
) -> Option<vk::SurfaceFormatKHR> {
surface_formats
.first()
.and_then(|f| Some(*f))
}
fn choose_desired_image_count(
surface_capabilities: &vk::SurfaceCapabilitiesKHR
) -> u32 {
let mut desired_image_count = surface_capabilities.min_image_count + 1;
if surface_capabilities.max_image_count > 0
&& desired_image_count > surface_capabilities.max_image_count
{
desired_image_count = surface_capabilities.max_image_count;
}
desired_image_count
}
fn choose_pre_transform(
surface_capabilities: &vk::SurfaceCapabilitiesKHR
) -> vk::SurfaceTransformFlagsKHR {
if surface_capabilities
.supported_transforms
.contains(vk::SurfaceTransformFlagsKHR::IDENTITY)
{
vk::SurfaceTransformFlagsKHR::IDENTITY
} else {
surface_capabilities.current_transform
}
}
fn choose_present_mode(
present_modes: &Vec<vk::PresentModeKHR>
) -> vk::PresentModeKHR {
present_modes
.iter()
.cloned()
.find(|&mode| mode == vk::PresentModeKHR::MAILBOX)
.unwrap_or(vk::PresentModeKHR::FIFO)
}
}
impl Drop for VkSwapchain {