render_plugin: Autocreate swapchain and update
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 9m45s
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 9m45s
This commit is contained in:
parent
ae0a2be097
commit
62d12f2ab8
5 changed files with 201 additions and 147 deletions
|
@ -1,7 +1,6 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use bevy_ecs::resource::Resource;
|
||||
use engine_window::raw_handle::WindowWrapper;
|
||||
use utils::{device::create_and_insert_device, instance::create_and_insert_instance};
|
||||
use vulkano::{
|
||||
command_buffer::allocator::StandardCommandBufferAllocator,
|
||||
|
@ -10,12 +9,10 @@ use vulkano::{
|
|||
instance::Instance,
|
||||
memory::allocator::StandardMemoryAllocator,
|
||||
};
|
||||
use window_render_context::WindowRenderContext;
|
||||
|
||||
use bevy_app::{App, Plugin};
|
||||
|
||||
mod utils;
|
||||
mod window_render_context;
|
||||
|
||||
#[derive(Resource, Clone)]
|
||||
pub struct VulkanInstance(pub Arc<Instance>);
|
||||
|
|
|
@ -1,125 +0,0 @@
|
|||
use bevy_app::App;
|
||||
use bevy_ecs::resource::Resource;
|
||||
use engine_window::raw_handle::WindowWrapper;
|
||||
use std::sync::Arc;
|
||||
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::{self, GpuFuture};
|
||||
use vulkano::{Validated, VulkanError};
|
||||
use winit::window::Window;
|
||||
|
||||
use crate::{VulkanDevice, VulkanInstance};
|
||||
|
||||
#[derive(Resource)]
|
||||
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 + Send + Sync>>,
|
||||
}
|
||||
|
||||
impl From<&App> for WindowRenderContext {
|
||||
fn from(app: &App) -> Self {
|
||||
let world = app.world();
|
||||
let window_handle = world
|
||||
.get_resource::<WindowWrapper>()
|
||||
.expect("Failed to find window handle");
|
||||
let vulkan_instance = world
|
||||
.get_resource::<VulkanInstance>()
|
||||
.expect("Failed to find vulkan instance");
|
||||
let vulkan_device = world
|
||||
.get_resource::<VulkanDevice>()
|
||||
.expect("Failed to find vulkan device");
|
||||
|
||||
let window_size = window_handle.0.inner_size();
|
||||
|
||||
let surface = Surface::from_window(vulkan_instance.0.clone(), window_handle.0.clone())
|
||||
.expect("Failed to create surface");
|
||||
|
||||
let (swapchain, images) = {
|
||||
let surface_capabilities = vulkan_device
|
||||
.0
|
||||
.physical_device()
|
||||
.surface_capabilities(&surface, Default::default())
|
||||
.unwrap();
|
||||
|
||||
let (image_format, _) = vulkan_device
|
||||
.0
|
||||
.physical_device()
|
||||
.surface_formats(&surface, Default::default())
|
||||
.unwrap()[0];
|
||||
|
||||
Swapchain::new(
|
||||
vulkan_device.0.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(vulkan_device.0.clone()).boxed_send_sync());
|
||||
|
||||
Self {
|
||||
window: window_handle.0.clone(),
|
||||
swapchain,
|
||||
attachment_image_views,
|
||||
viewport,
|
||||
recreate_swapchain,
|
||||
previous_frame_end,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl WindowRenderContext {
|
||||
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<_>>()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue