render: add depth buffer
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 8m13s

This commit is contained in:
Florian RICHER 2025-05-29 17:44:00 +02:00
parent 77c717f90b
commit 650b61e3ae
Signed by: florian.richer
GPG key ID: C73D37CBED7BFC77
4 changed files with 61 additions and 21 deletions

View file

@ -130,26 +130,24 @@ impl ApplicationHandler for App {
&self.input_manager, &self.input_manager,
&self.timer, &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 Some(scene) = self.scene_manager.current_scene_mut() {
if let Err(e) = scene.update(&scene_context) { scene.update(&scene_context).unwrap();
log::error!("Error updating scene: {}", e);
}
let acquire_future = renderer.acquire(None, |_| {}).unwrap(); let acquire_future = renderer.acquire(None, |_| {}).unwrap();
let acquire_future = scene.render( let acquire_future = scene
&renderer.swapchain_image_view(), .render(
acquire_future, &renderer.swapchain_image_view(),
&scene_context, acquire_future,
gui, &scene_context,
); gui,
match acquire_future { )
Ok(future) => renderer.present(future, true), .unwrap();
Err(e) => { renderer.present(acquire_future, true);
log::error!("Error rendering scene: {}", e);
}
}
} }
} }
_ => {} _ => {}

View file

@ -1,3 +1,5 @@
use std::error::Error;
use super::{Scene, SceneContext}; use super::{Scene, SceneContext};
pub struct SceneManager { 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<dyn Error>> {
if let Some(current_scene) = self.current_scene.as_mut() { if let Some(current_scene) = self.current_scene.as_mut() {
if !current_scene.loaded() { if !current_scene.loaded() {
if let Err(e) = current_scene.load(scene_context) { current_scene.load(scene_context)?;
log::error!("Error loading scene: {}", e);
}
} }
} }
Ok(())
} }
pub fn load_scene(&mut self, scene: Box<dyn Scene>) { pub fn load_scene(&mut self, scene: Box<dyn Scene>) {

View file

@ -17,7 +17,8 @@ use vulkano::{
graphics::{ graphics::{
GraphicsPipelineCreateInfo, GraphicsPipelineCreateInfo,
color_blend::{ColorBlendAttachmentState, ColorBlendState}, color_blend::{ColorBlendAttachmentState, ColorBlendState},
input_assembly::{InputAssemblyState, PrimitiveTopology}, depth_stencil::{DepthState, DepthStencilState},
input_assembly::InputAssemblyState,
multisample::MultisampleState, multisample::MultisampleState,
rasterization::RasterizationState, rasterization::RasterizationState,
subpass::PipelineRenderingCreateInfo, subpass::PipelineRenderingCreateInfo,
@ -84,6 +85,7 @@ impl Square {
device: &Arc<Device>, device: &Arc<Device>,
memory_allocator: &Arc<StandardMemoryAllocator>, memory_allocator: &Arc<StandardMemoryAllocator>,
swapchain_format: Format, swapchain_format: Format,
depth_format: Format,
) -> Result<Self, Box<dyn Error>> { ) -> Result<Self, Box<dyn Error>> {
let vertex_buffer = Buffer::from_iter( let vertex_buffer = Buffer::from_iter(
memory_allocator.clone(), memory_allocator.clone(),
@ -173,6 +175,7 @@ impl Square {
let subpass = PipelineRenderingCreateInfo { let subpass = PipelineRenderingCreateInfo {
color_attachment_formats: vec![Some(swapchain_format)], color_attachment_formats: vec![Some(swapchain_format)],
depth_attachment_format: Some(depth_format),
..Default::default() ..Default::default()
}; };
@ -190,6 +193,10 @@ impl Square {
subpass.color_attachment_formats.len() as u32, subpass.color_attachment_formats.len() as u32,
ColorBlendAttachmentState::default(), ColorBlendAttachmentState::default(),
)), )),
depth_stencil_state: Some(DepthStencilState {
depth: Some(DepthState::simple()),
..Default::default()
}),
dynamic_state: [DynamicState::Viewport].into_iter().collect(), dynamic_state: [DynamicState::Viewport].into_iter().collect(),
subpass: Some(subpass.into()), subpass: Some(subpass.into()),
..GraphicsPipelineCreateInfo::layout(layout) ..GraphicsPipelineCreateInfo::layout(layout)

View file

@ -9,6 +9,12 @@ use egui_winit_vulkano::{Gui, egui};
use glam::EulerRot; use glam::EulerRot;
use glam::Quat; use glam::Quat;
use glam::Vec3; 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::{ use vulkano::{
command_buffer::{ command_buffer::{
AutoCommandBufferBuilder, CommandBufferUsage, PrimaryCommandBufferAbstract, AutoCommandBufferBuilder, CommandBufferUsage, PrimaryCommandBufferAbstract,
@ -28,6 +34,7 @@ pub struct MainSceneState {
camera: Camera3D, camera: Camera3D,
texture: Texture, texture: Texture,
speed: f32, speed: f32,
depth_image_view: Arc<ImageView>,
} }
#[derive(Default)] #[derive(Default)]
@ -41,10 +48,27 @@ impl Scene for MainScene {
} }
fn load(&mut self, scene_context: &SceneContext) -> Result<(), Box<dyn Error>> { fn load(&mut self, scene_context: &SceneContext) -> Result<(), Box<dyn Error>> {
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( let square = Square::new(
&scene_context.device, &scene_context.device,
&scene_context.memory_allocator, &scene_context.memory_allocator,
scene_context.swapchain_format, scene_context.swapchain_format,
Format::D16_UNORM,
)?; )?;
let num_instances = 100; let num_instances = 100;
@ -98,6 +122,7 @@ impl Scene for MainScene {
camera, camera,
texture, texture,
speed: 50.0, speed: 50.0,
depth_image_view,
}); });
Ok(()) Ok(())
@ -146,6 +171,12 @@ impl Scene for MainScene {
clear_value: Some([0.0, 0.0, 0.0, 1.0].into()), clear_value: Some([0.0, 0.0, 0.0, 1.0].into()),
..RenderingAttachmentInfo::image_view(image_view.clone()) ..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() ..Default::default()
})? })?
.set_viewport(0, [viewport].into_iter().collect())?; .set_viewport(0, [viewport].into_iter().collect())?;