diff --git a/src/core/app.rs b/src/core/app.rs index d80d5c6..93ef9bc 100644 --- a/src/core/app.rs +++ b/src/core/app.rs @@ -8,6 +8,8 @@ use crate::core::scene::SceneManager; use crate::core::timer::Timer; use crate::game::main_scene::MainScene; use egui_winit_vulkano::{Gui, GuiConfig}; +use vulkano::format::Format; +use vulkano::image::ImageUsage; use vulkano::swapchain::PresentMode; use vulkano_util::context::VulkanoContext; use vulkano_util::window::{VulkanoWindows, WindowDescriptor}; @@ -16,6 +18,8 @@ use winit::event::WindowEvent; use winit::event_loop::ActiveEventLoop; use winit::window::WindowId; +pub const DEPTH_IMAGE_ID: usize = 0; + pub struct App { vulkan_context: Arc, vulkano_windows: Arc, @@ -56,8 +60,14 @@ impl ApplicationHandler for App { |_| {}, ); + let renderer = vulkano_windows.get_renderer_mut(window_id).unwrap(); + renderer.add_additional_image_view( + DEPTH_IMAGE_ID, + Format::D16_UNORM, + ImageUsage::DEPTH_STENCIL_ATTACHMENT, + ); + let gui = { - let renderer = vulkano_windows.get_renderer_mut(window_id).unwrap(); Gui::new( event_loop, renderer.surface(), @@ -124,8 +134,8 @@ impl ApplicationHandler for App { None => log::error!("Failed to get a mutable reference to the timer"), } - let scene_context = SceneContext::from(( - &*renderer, + let mut scene_context = SceneContext::from(( + &mut *renderer, &self.vulkan_context, &self.input_manager, &self.timer, @@ -139,14 +149,10 @@ impl ApplicationHandler for App { scene.update(&scene_context).unwrap(); let acquire_future = renderer.acquire(None, |_| {}).unwrap(); - let acquire_future = scene - .render( - &renderer.swapchain_image_view(), - acquire_future, - &scene_context, - gui, - ) - .unwrap(); + // Update the swapchain image view to the current one after acquiring the next image + scene_context.swapchain_image_view = renderer.swapchain_image_view(); + + let acquire_future = scene.render(acquire_future, &scene_context, gui).unwrap(); renderer.present(acquire_future, true); } } diff --git a/src/core/render/primitives/camera.rs b/src/core/render/primitives/camera.rs index 5ea52e7..e8cdf1e 100644 --- a/src/core/render/primitives/camera.rs +++ b/src/core/render/primitives/camera.rs @@ -55,7 +55,7 @@ impl Camera3D { // Process camera rotation let camera_delta = camera_sensitivity * timer.delta_time(); self.rotation += Vec3::new( - -(input_manager.get_virtual_input_state("mouse_y") * camera_delta).to_radians(), + (input_manager.get_virtual_input_state("mouse_y") * camera_delta).to_radians(), (input_manager.get_virtual_input_state("mouse_x") * camera_delta).to_radians(), 0.0, ); diff --git a/src/core/scene/context.rs b/src/core/scene/context.rs index 028d612..545f39c 100644 --- a/src/core/scene/context.rs +++ b/src/core/scene/context.rs @@ -4,13 +4,15 @@ use vulkano::{ command_buffer::allocator::StandardCommandBufferAllocator, descriptor_set::allocator::StandardDescriptorSetAllocator, device::{Device, Queue}, - format::Format, + image::view::ImageView, instance::Instance, memory::allocator::StandardMemoryAllocator, }; use vulkano_util::renderer::VulkanoWindowRenderer; -use crate::core::{input::InputManager, render::vulkan_context::VulkanContext, timer::Timer}; +use crate::core::{ + app::DEPTH_IMAGE_ID, input::InputManager, render::vulkan_context::VulkanContext, timer::Timer, +}; pub struct SceneContext { pub instance: Arc, @@ -23,14 +25,15 @@ pub struct SceneContext { pub descriptor_set_allocator: Arc, pub window_size: [f32; 2], pub aspect_ratio: f32, - pub swapchain_format: Format, + pub swapchain_image_view: Arc, + pub depth_stencil_image_view: Arc, pub input_manager: Arc, pub timer: Arc, } impl From<( - &VulkanoWindowRenderer, + &mut VulkanoWindowRenderer, &Arc, &Arc, &Arc, @@ -38,7 +41,7 @@ impl { fn from( (renderer, vulkan_context, input_manager, timer): ( - &VulkanoWindowRenderer, + &mut VulkanoWindowRenderer, &Arc, &Arc, &Arc, @@ -74,7 +77,8 @@ impl descriptor_set_allocator, window_size: renderer.window_size(), aspect_ratio: renderer.aspect_ratio(), - swapchain_format: renderer.swapchain_format(), + swapchain_image_view: renderer.swapchain_image_view(), + depth_stencil_image_view: renderer.get_additional_image_view(DEPTH_IMAGE_ID), input_manager: input_manager.clone(), timer: timer.clone(), } diff --git a/src/core/scene/mod.rs b/src/core/scene/mod.rs index a71bfbb..8642224 100644 --- a/src/core/scene/mod.rs +++ b/src/core/scene/mod.rs @@ -15,7 +15,6 @@ pub trait Scene { fn update(&mut self, scene_context: &SceneContext) -> Result<(), Box>; fn render( &mut self, - image_view: &Arc, acquire_future: Box, scene_context: &SceneContext, gui: &mut Gui, diff --git a/src/game/main_scene.rs b/src/game/main_scene.rs index b1638c6..22c851b 100644 --- a/src/game/main_scene.rs +++ b/src/game/main_scene.rs @@ -12,7 +12,6 @@ use glam::Vec3; use vulkano::format::Format; use vulkano::image::Image; use vulkano::image::ImageCreateInfo; -use vulkano::image::ImageLayout; use vulkano::image::ImageUsage; use vulkano::memory::allocator::AllocationCreateInfo; use vulkano::{ @@ -34,7 +33,6 @@ pub struct MainSceneState { camera: Camera3D, texture: Texture, speed: f32, - depth_image_view: Arc, } #[derive(Default)] @@ -48,27 +46,11 @@ impl Scene for MainScene { } fn load(&mut self, scene_context: &SceneContext) -> Result<(), Box> { - let depth_image = Image::new( - scene_context.memory_allocator.clone(), - ImageCreateInfo { - format: Format::D16_UNORM, - extent: [ - scene_context.window_size[0] as u32, - scene_context.window_size[1] as u32, - 1, - ], - usage: ImageUsage::DEPTH_STENCIL_ATTACHMENT, - ..Default::default() - }, - AllocationCreateInfo::default(), - )?; - let depth_image_view = ImageView::new_default(depth_image)?; - let square = Square::new( &scene_context.device, &scene_context.memory_allocator, - scene_context.swapchain_format, - Format::D16_UNORM, + scene_context.swapchain_image_view.format(), + scene_context.depth_stencil_image_view.format(), )?; let num_instances = 100; @@ -122,7 +104,6 @@ impl Scene for MainScene { camera, texture, speed: 50.0, - depth_image_view, }); Ok(()) @@ -143,7 +124,6 @@ impl Scene for MainScene { fn render( &mut self, - image_view: &Arc, acquire_future: Box, scene_context: &SceneContext, gui: &mut Gui, @@ -169,13 +149,17 @@ impl Scene for MainScene { load_op: AttachmentLoadOp::Clear, store_op: AttachmentStoreOp::Store, clear_value: Some([0.0, 0.0, 0.0, 1.0].into()), - ..RenderingAttachmentInfo::image_view(image_view.clone()) + ..RenderingAttachmentInfo::image_view( + scene_context.swapchain_image_view.clone(), + ) })], depth_attachment: Some(RenderingAttachmentInfo { load_op: AttachmentLoadOp::Clear, store_op: AttachmentStoreOp::DontCare, clear_value: Some([1.0].into()), - ..RenderingAttachmentInfo::image_view(state.depth_image_view.clone()) + ..RenderingAttachmentInfo::image_view( + scene_context.depth_stencil_image_view.clone(), + ) }), ..Default::default() })? @@ -230,7 +214,8 @@ impl Scene for MainScene { }); }); - let render_future = gui.draw_on_image(render_future, image_view.clone()); + let render_future = + gui.draw_on_image(render_future, scene_context.swapchain_image_view.clone()); Ok(render_future) }