Begin add Window Render Context
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 26m40s
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 26m40s
This commit is contained in:
parent
e2616a0ef5
commit
a04c769438
5 changed files with 163 additions and 14 deletions
|
@ -1,9 +1,11 @@
|
|||
use context::VulkanContext;
|
||||
use vulkan_context::VulkanContext;
|
||||
use window_render_context::WindowRenderContext;
|
||||
|
||||
use super::app::App;
|
||||
|
||||
mod context;
|
||||
mod utils;
|
||||
mod vulkan_context;
|
||||
mod window_render_context;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum VulkanError {
|
||||
|
@ -16,9 +18,11 @@ pub struct Vulkan;
|
|||
impl Vulkan {
|
||||
pub fn new(app: &mut App) -> Result<(), VulkanError> {
|
||||
let vulkan_context = VulkanContext::from(app as &App);
|
||||
|
||||
app.world_mut().insert_resource(vulkan_context);
|
||||
|
||||
let window_render_context = WindowRenderContext::from(app as &App);
|
||||
app.world_mut().insert_resource(window_render_context);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,14 +1,18 @@
|
|||
use std::sync::Arc;
|
||||
use std::{any::Any, sync::Arc};
|
||||
|
||||
use bevy_ecs::system::Resource;
|
||||
use vulkano::{
|
||||
command_buffer::allocator::StandardCommandBufferAllocator,
|
||||
command_buffer::{
|
||||
AutoCommandBufferBuilder, CommandBufferUsage, PrimaryAutoCommandBuffer,
|
||||
allocator::StandardCommandBufferAllocator,
|
||||
},
|
||||
descriptor_set::allocator::StandardDescriptorSetAllocator,
|
||||
device::{Device, Queue},
|
||||
instance::Instance,
|
||||
memory::allocator::StandardMemoryAllocator,
|
||||
swapchain::Surface,
|
||||
};
|
||||
use winit::raw_window_handle::{HasDisplayHandle, HasWindowHandle};
|
||||
|
||||
use crate::core::{app::App, window::raw_handle::DisplayHandleWrapper};
|
||||
|
||||
|
@ -25,6 +29,24 @@ pub struct VulkanContext {
|
|||
pub descriptor_set_allocator: Arc<StandardDescriptorSetAllocator>,
|
||||
}
|
||||
|
||||
impl VulkanContext {
|
||||
pub fn create_surface(
|
||||
&self,
|
||||
window: Arc<impl HasWindowHandle + HasDisplayHandle + Any + Send + Sync>,
|
||||
) -> Arc<Surface> {
|
||||
Surface::from_window(self.instance.clone(), window).unwrap()
|
||||
}
|
||||
|
||||
pub fn create_render_builder(&self) -> AutoCommandBufferBuilder<PrimaryAutoCommandBuffer> {
|
||||
AutoCommandBufferBuilder::primary(
|
||||
self.command_buffer_allocator.clone(),
|
||||
self.graphics_queue.queue_family_index(),
|
||||
CommandBufferUsage::OneTimeSubmit,
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&App> for VulkanContext {
|
||||
fn from(app: &App) -> Self {
|
||||
let library = utils::load_library();
|
117
src/core/vulkan/window_render_context.rs
Normal file
117
src/core/vulkan/window_render_context.rs
Normal file
|
@ -0,0 +1,117 @@
|
|||
use bevy_ecs::system::Resource;
|
||||
use std::sync::Arc;
|
||||
use vulkano::image::view::ImageView;
|
||||
use vulkano::image::{Image, ImageUsage};
|
||||
use vulkano::pipeline::graphics::viewport::Viewport;
|
||||
use vulkano::swapchain::{Swapchain, SwapchainCreateInfo};
|
||||
use vulkano::sync::{self, GpuFuture};
|
||||
use vulkano::{Validated, VulkanError};
|
||||
use winit::window::Window;
|
||||
|
||||
use crate::core::app::App;
|
||||
use crate::core::window::raw_handle::WindowWrapper;
|
||||
|
||||
use super::vulkan_context::VulkanContext;
|
||||
|
||||
#[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 vulkan_context = world.get_resource::<VulkanContext>().unwrap();
|
||||
let window_handle = world.get_resource::<WindowWrapper>().unwrap();
|
||||
let window_size = window_handle.0.inner_size();
|
||||
|
||||
let surface = vulkan_context.create_surface(window_handle.0.clone());
|
||||
|
||||
let (swapchain, images) = {
|
||||
let surface_capabilities = vulkan_context
|
||||
.device
|
||||
.physical_device()
|
||||
.surface_capabilities(&surface, Default::default())
|
||||
.unwrap();
|
||||
|
||||
let (image_format, _) = vulkan_context
|
||||
.device
|
||||
.physical_device()
|
||||
.surface_formats(&surface, Default::default())
|
||||
.unwrap()[0];
|
||||
|
||||
Swapchain::new(
|
||||
vulkan_context.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(vulkan_context.device.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<_>>()
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use bevy_ecs::system::Resource;
|
||||
use winit::event_loop::EventLoopProxy;
|
||||
use winit::{event_loop::EventLoopProxy, window::Window};
|
||||
|
||||
#[derive(Resource)]
|
||||
pub struct EventLoopProxyWrapper<T: 'static>(EventLoopProxy<T>);
|
||||
|
@ -16,3 +18,6 @@ impl<T: 'static> EventLoopProxyWrapper<T> {
|
|||
|
||||
#[derive(Resource)]
|
||||
pub struct DisplayHandleWrapper(pub winit::event_loop::OwnedDisplayHandle);
|
||||
|
||||
#[derive(Resource)]
|
||||
pub struct WindowWrapper(pub Arc<Window>);
|
||||
|
|
|
@ -1,23 +1,22 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use bevy_ecs::world::World;
|
||||
use winit::{
|
||||
application::ApplicationHandler,
|
||||
event::WindowEvent,
|
||||
event_loop::ActiveEventLoop,
|
||||
window::{Window, WindowId},
|
||||
application::ApplicationHandler, event::WindowEvent, event_loop::ActiveEventLoop,
|
||||
window::WindowId,
|
||||
};
|
||||
|
||||
use crate::core::app::App;
|
||||
|
||||
use super::config::WindowConfig;
|
||||
use super::{config::WindowConfig, raw_handle::WindowWrapper};
|
||||
|
||||
pub struct WindowState {
|
||||
app: App,
|
||||
window: Option<Window>,
|
||||
}
|
||||
|
||||
impl WindowState {
|
||||
pub fn new(app: App) -> Self {
|
||||
Self { app, window: None }
|
||||
Self { app }
|
||||
}
|
||||
|
||||
fn world(&self) -> &World {
|
||||
|
@ -30,7 +29,9 @@ impl ApplicationHandler for WindowState {
|
|||
let window_config = self.world().get_resource::<WindowConfig>().unwrap();
|
||||
|
||||
let window = event_loop.create_window(window_config.into()).unwrap();
|
||||
self.window = Some(window);
|
||||
self.app
|
||||
.world_mut()
|
||||
.insert_resource(WindowWrapper(Arc::new(window)));
|
||||
}
|
||||
|
||||
fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
|
||||
|
|
Loading…
Add table
Reference in a new issue