Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 26m52s
102 lines
3.3 KiB
Rust
102 lines
3.3 KiB
Rust
use std::sync::Arc;
|
|
use vulkano::device::Device;
|
|
use vulkano::image::view::ImageView;
|
|
use vulkano::image::{Image, ImageUsage};
|
|
use vulkano::pipeline::graphics::viewport::Viewport;
|
|
use vulkano::swapchain::{Surface, Swapchain, SwapchainCreateInfo};
|
|
use vulkano::sync::GpuFuture;
|
|
use vulkano::{Validated, VulkanError, sync};
|
|
use winit::window::Window;
|
|
|
|
pub struct WindowRenderContext {
|
|
pub window: Arc<Window>,
|
|
pub swapchain: Arc<Swapchain>,
|
|
pub attachment_image_views: Vec<Arc<ImageView>>,
|
|
pub viewport: Viewport,
|
|
pub recreate_swapchain: bool,
|
|
pub previous_frame_end: Option<Box<dyn GpuFuture>>,
|
|
}
|
|
|
|
impl WindowRenderContext {
|
|
pub fn new(window: Arc<Window>, surface: Arc<Surface>, device: &Arc<Device>) -> Self {
|
|
let window_size = window.inner_size();
|
|
|
|
let (swapchain, images) = {
|
|
let surface_capabilities = device
|
|
.physical_device()
|
|
.surface_capabilities(&surface, Default::default())
|
|
.unwrap();
|
|
|
|
let (image_format, _) = device
|
|
.physical_device()
|
|
.surface_formats(&surface, Default::default())
|
|
.unwrap()[0];
|
|
|
|
Swapchain::new(
|
|
device.clone(),
|
|
surface,
|
|
SwapchainCreateInfo {
|
|
// 2 because with some graphics driver, it crash on fullscreen because fullscreen need to min image to works.
|
|
min_image_count: surface_capabilities.min_image_count.max(2),
|
|
image_format,
|
|
image_extent: window_size.into(),
|
|
image_usage: ImageUsage::COLOR_ATTACHMENT,
|
|
composite_alpha: surface_capabilities
|
|
.supported_composite_alpha
|
|
.into_iter()
|
|
.next()
|
|
.unwrap(),
|
|
|
|
..Default::default()
|
|
},
|
|
)
|
|
.unwrap()
|
|
};
|
|
|
|
let attachment_image_views = window_size_dependent_setup(&images);
|
|
|
|
let viewport = Viewport {
|
|
offset: [0.0, 0.0],
|
|
extent: window_size.into(),
|
|
depth_range: 0.0..=1.0,
|
|
};
|
|
|
|
let recreate_swapchain = false;
|
|
let previous_frame_end = Some(sync::now(device.clone()).boxed());
|
|
|
|
Self {
|
|
window,
|
|
swapchain,
|
|
attachment_image_views,
|
|
viewport,
|
|
recreate_swapchain,
|
|
previous_frame_end,
|
|
}
|
|
}
|
|
|
|
pub fn update_swapchain(&mut self) -> Result<(), Validated<VulkanError>> {
|
|
if !self.recreate_swapchain {
|
|
return Ok(());
|
|
}
|
|
|
|
let window_size = self.window.inner_size();
|
|
let (new_swapchain, new_images) = self.swapchain.recreate(SwapchainCreateInfo {
|
|
image_extent: window_size.into(),
|
|
..self.swapchain.create_info()
|
|
})?;
|
|
|
|
self.swapchain = new_swapchain;
|
|
self.attachment_image_views = window_size_dependent_setup(&new_images);
|
|
self.viewport.extent = window_size.into();
|
|
self.recreate_swapchain = false;
|
|
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
fn window_size_dependent_setup(images: &[Arc<Image>]) -> Vec<Arc<ImageView>> {
|
|
images
|
|
.iter()
|
|
.map(|image| ImageView::new_default(image.clone()).unwrap())
|
|
.collect::<Vec<_>>()
|
|
}
|