diff --git a/src/core/app.rs b/src/core/app.rs index 33a5dc1..d80d5c6 100644 --- a/src/core/app.rs +++ b/src/core/app.rs @@ -130,26 +130,24 @@ impl ApplicationHandler for App { &self.input_manager, &self.timer, )); - self.scene_manager.load_scene_if_not_loaded(&scene_context); + + self.scene_manager + .load_scene_if_not_loaded(&scene_context) + .unwrap(); if let Some(scene) = self.scene_manager.current_scene_mut() { - if let Err(e) = scene.update(&scene_context) { - log::error!("Error updating scene: {}", e); - } + 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, - ); - match acquire_future { - Ok(future) => renderer.present(future, true), - Err(e) => { - log::error!("Error rendering scene: {}", e); - } - } + let acquire_future = scene + .render( + &renderer.swapchain_image_view(), + acquire_future, + &scene_context, + gui, + ) + .unwrap(); + renderer.present(acquire_future, true); } } _ => {} diff --git a/src/core/scene/manager.rs b/src/core/scene/manager.rs index e2043f2..e44765d 100644 --- a/src/core/scene/manager.rs +++ b/src/core/scene/manager.rs @@ -1,3 +1,5 @@ +use std::error::Error; + use super::{Scene, SceneContext}; pub struct SceneManager { @@ -11,14 +13,16 @@ impl SceneManager { } } - pub fn load_scene_if_not_loaded(&mut self, scene_context: &SceneContext) { + pub fn load_scene_if_not_loaded( + &mut self, + scene_context: &SceneContext, + ) -> Result<(), Box> { if let Some(current_scene) = self.current_scene.as_mut() { if !current_scene.loaded() { - if let Err(e) = current_scene.load(scene_context) { - log::error!("Error loading scene: {}", e); - } + current_scene.load(scene_context)?; } } + Ok(()) } pub fn load_scene(&mut self, scene: Box) { diff --git a/src/game/assets/square.rs b/src/game/assets/square.rs index ca16b93..80a24e2 100644 --- a/src/game/assets/square.rs +++ b/src/game/assets/square.rs @@ -17,7 +17,8 @@ use vulkano::{ graphics::{ GraphicsPipelineCreateInfo, color_blend::{ColorBlendAttachmentState, ColorBlendState}, - input_assembly::{InputAssemblyState, PrimitiveTopology}, + depth_stencil::{DepthState, DepthStencilState}, + input_assembly::InputAssemblyState, multisample::MultisampleState, rasterization::RasterizationState, subpass::PipelineRenderingCreateInfo, @@ -84,6 +85,7 @@ impl Square { device: &Arc, memory_allocator: &Arc, swapchain_format: Format, + depth_format: Format, ) -> Result> { let vertex_buffer = Buffer::from_iter( memory_allocator.clone(), @@ -173,6 +175,7 @@ impl Square { let subpass = PipelineRenderingCreateInfo { color_attachment_formats: vec![Some(swapchain_format)], + depth_attachment_format: Some(depth_format), ..Default::default() }; @@ -190,6 +193,10 @@ impl Square { subpass.color_attachment_formats.len() as u32, ColorBlendAttachmentState::default(), )), + depth_stencil_state: Some(DepthStencilState { + depth: Some(DepthState::simple()), + ..Default::default() + }), dynamic_state: [DynamicState::Viewport].into_iter().collect(), subpass: Some(subpass.into()), ..GraphicsPipelineCreateInfo::layout(layout) diff --git a/src/game/main_scene.rs b/src/game/main_scene.rs index 5fd757a..b1638c6 100644 --- a/src/game/main_scene.rs +++ b/src/game/main_scene.rs @@ -9,6 +9,12 @@ use egui_winit_vulkano::{Gui, egui}; use glam::EulerRot; use glam::Quat; 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::{ command_buffer::{ AutoCommandBufferBuilder, CommandBufferUsage, PrimaryCommandBufferAbstract, @@ -28,6 +34,7 @@ pub struct MainSceneState { camera: Camera3D, texture: Texture, speed: f32, + depth_image_view: Arc, } #[derive(Default)] @@ -41,10 +48,27 @@ 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, )?; let num_instances = 100; @@ -98,6 +122,7 @@ impl Scene for MainScene { camera, texture, speed: 50.0, + depth_image_view, }); Ok(()) @@ -146,6 +171,12 @@ impl Scene for MainScene { clear_value: Some([0.0, 0.0, 0.0, 1.0].into()), ..RenderingAttachmentInfo::image_view(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()) + }), ..Default::default() })? .set_viewport(0, [viewport].into_iter().collect())?;