Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 0s
71 lines
No EOL
2.3 KiB
Rust
71 lines
No EOL
2.3 KiB
Rust
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,
|
|
desired_image_count: u32,
|
|
surface_format: SurfaceFormatKHR,
|
|
surface_resolution: Extent2D,
|
|
present_mode: PresentModeKHR,
|
|
pre_transform: SurfaceTransformFlagsKHR,
|
|
) -> Self {
|
|
Self {
|
|
swapchain_loader,
|
|
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 {
|
|
fn drop(&mut self) {
|
|
if let Some(swapchain) = self.swapchain {
|
|
unsafe {
|
|
self.swapchain_loader.destroy_swapchain(swapchain, None);
|
|
}
|
|
}
|
|
}
|
|
} |