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.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);
}
}
_ => {}

View file

@ -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<dyn Error>> {
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<dyn Scene>) {

View file

@ -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<Device>,
memory_allocator: &Arc<StandardMemoryAllocator>,
swapchain_format: Format,
depth_format: Format,
) -> Result<Self, Box<dyn Error>> {
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)

View file

@ -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<ImageView>,
}
#[derive(Default)]
@ -41,10 +48,27 @@ impl Scene for MainScene {
}
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(
&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())?;