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

This commit is contained in:
Florian RICHER 2024-11-13 13:56:22 +01:00
parent ee8b886aec
commit 96cb003174
3 changed files with 65 additions and 32 deletions

View file

@ -5,7 +5,7 @@ pub struct VkRenderContext {
instance: VkInstance,
surface: VkSurface,
device: VkDevice,
swapchain: VkSwapchain
swapchain: VkSwapchain,
}
impl VkRenderContext {
@ -28,11 +28,11 @@ impl VkRenderContext {
let device = VkDevice::new_graphics_device(&instance, &physical_device, queue_family_index)
.expect("Unable to create device");
let swapchain = surface.create_swapchain(
let mut swapchain = surface.create_swapchain(
&window,
&instance,
&device,
&physical_device
&physical_device,
).expect("Unable to create swapchain");
// let present_queue = device.get_device_queue(0);
@ -41,7 +41,7 @@ impl VkRenderContext {
instance,
surface,
device,
swapchain
swapchain,
})
}
}

View file

@ -1,11 +1,11 @@
use crate::display::Window;
use crate::vulkan::{VkDevice, VkInstance, VkPhysicalDevice, VkSwapchain, LOG_TARGET};
use ash::prelude::VkResult;
use ash::vk;
use crate::display::Window;
pub struct VkSurface {
surface_loader: ash::khr::surface::Instance,
surface: vk::SurfaceKHR,
pub(super) surface: vk::SurfaceKHR,
}
impl VkSurface {
@ -68,7 +68,9 @@ impl VkSurface {
let surface_formats = self.get_physical_device_surface_formats(physical_device)?;
log::debug!(target: LOG_TARGET, "Supported surface formats by physical device: {surface_formats:#?}");
let surface_format = surface_formats.first()
let surface_format = surface_formats
.first()
.and_then(|f| Some(*f))
.ok_or_else(|| anyhow::anyhow!("No available surface formats"))?;
log::debug!(target: LOG_TARGET, "Selected surface format: {surface_format:?}");
@ -116,30 +118,18 @@ impl VkSurface {
.unwrap_or(vk::PresentModeKHR::FIFO);
let swapchain_loader = ash::khr::swapchain::Device::new(&instance.handle, &device.handle);
let swapchain_create_info = vk::SwapchainCreateInfoKHR::default()
.surface(self.surface)
.min_image_count(desired_image_count)
.image_color_space(surface_format.color_space)
.image_format(surface_format.format)
.image_extent(surface_resolution)
.image_usage(vk::ImageUsageFlags::COLOR_ATTACHMENT)
.image_sharing_mode(vk::SharingMode::EXCLUSIVE)
.pre_transform(pre_transform)
.composite_alpha(vk::CompositeAlphaFlagsKHR::OPAQUE)
.present_mode(present_mode)
.clipped(true)
.image_array_layers(1);
log::debug!(target: LOG_TARGET, "Swapchain info: {swapchain_create_info:#?}");
let swapchain = unsafe {
swapchain_loader.create_swapchain(&swapchain_create_info, None)?
};
log::debug!(target: LOG_TARGET, "Swapchain created");
Ok(VkSwapchain::new(
let mut swapchain = VkSwapchain::new(
swapchain_loader,
Some(swapchain)
))
desired_image_count,
surface_format,
surface_resolution,
present_mode,
pre_transform,
);
swapchain.create_swapchain(&self)?;
Ok(swapchain)
}
}

View file

@ -1,20 +1,63 @@
use crate::vulkan::{VkSurface, LOG_TARGET};
use ash::prelude::VkResult;
use ash::vk;
use ash::vk::{Extent2D, PresentModeKHR, SurfaceFormatKHR, SurfaceTransformFlagsKHR, SwapchainKHR};
pub struct VkSwapchain {
swapchain_loader: ash::khr::swapchain::Device,
swapchain: Option<vk::SwapchainKHR>,
desired_image_count: u32,
surface_format: SurfaceFormatKHR,
surface_resolution: Extent2D,
present_mode: PresentModeKHR,
pre_transform: SurfaceTransformFlagsKHR,
}
impl VkSwapchain {
pub fn new(
swapchain_loader: ash::khr::swapchain::Device,
swapchain: Option<vk::SwapchainKHR>,
desired_image_count: u32,
surface_format: SurfaceFormatKHR,
surface_resolution: Extent2D,
present_mode: PresentModeKHR,
pre_transform: SurfaceTransformFlagsKHR,
) -> Self {
Self {
swapchain_loader,
swapchain
desired_image_count,
surface_format,
surface_resolution,
present_mode,
pre_transform,
swapchain: None,
}
}
pub fn create_swapchain(&mut self, surface: &VkSurface) -> VkResult<()> {
let swapchain_create_info = vk::SwapchainCreateInfoKHR::default()
.surface(surface.surface)
.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_usage(vk::ImageUsageFlags::COLOR_ATTACHMENT)
.image_sharing_mode(vk::SharingMode::EXCLUSIVE)
.pre_transform(self.pre_transform)
.composite_alpha(vk::CompositeAlphaFlagsKHR::OPAQUE)
.present_mode(self.present_mode)
.clipped(true)
.image_array_layers(1);
let swapchain = unsafe {
self.swapchain_loader.create_swapchain(&swapchain_create_info, None)?
};
log::debug!(target: LOG_TARGET, "Swapchain created : {swapchain_create_info:#?}");
self.swapchain = Some(swapchain);
Ok(())
}
}
impl Drop for VkSwapchain {