From 9a01ecd6f2b06098697791b1955887cdfa317fd9 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Mon, 6 May 2024 16:17:01 +0200 Subject: [PATCH] depth, wip --- kubi/src/rendering.rs | 5 ++- kubi/src/rendering/depth.rs | 67 +++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 kubi/src/rendering/depth.rs diff --git a/kubi/src/rendering.rs b/kubi/src/rendering.rs index e42a91b..215e597 100644 --- a/kubi/src/rendering.rs +++ b/kubi/src/rendering.rs @@ -10,6 +10,8 @@ use self::camera::update_camera_unform_buffer; pub mod world; pub mod camera; +pub mod depth; + //pub mod primitives; //pub mod selection_box; //pub mod entities; @@ -107,9 +109,6 @@ pub fn render_master(storages: AllStoragesViewMut) { }; if storages.run(is_ingame) { - //XXX: probably should be in pre_update or sth - storages.run(update_camera_unform_buffer); - //TODO init world render state on demand storages.run_with_data(world::draw_world, &mut data); } diff --git a/kubi/src/rendering/depth.rs b/kubi/src/rendering/depth.rs new file mode 100644 index 0000000..6c0f840 --- /dev/null +++ b/kubi/src/rendering/depth.rs @@ -0,0 +1,67 @@ +use glam::{uvec2, UVec2}; + +use super::Renderer; + +pub struct DepthTexture { + pub depth_texture: wgpu::Texture, + pub depth_view: wgpu::TextureView, + pub depth_sampler: wgpu::Sampler, +} + +impl DepthTexture { + fn desc(size: UVec2) -> wgpu::TextureDescriptor<'static> { + wgpu::TextureDescriptor { + label: Some("depth_texture"), + size: wgpu::Extent3d { + width: size.x, + height: size.y, + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Depth32Float, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, + view_formats: &[wgpu::TextureFormat::Depth32Float], + } + } + + pub fn init(renderer: &Renderer) -> Self { + let size = uvec2(renderer.size().width, renderer.size().height); + let depth_texture_desc = Self::desc(size); + let depth_texture = renderer.device().create_texture(&depth_texture_desc); + let depth_view = depth_texture.create_view(&wgpu::TextureViewDescriptor::default()); + let depth_sampler = renderer.device().create_sampler(&wgpu::SamplerDescriptor { + label: Some("depth_sampler"), + address_mode_u: wgpu::AddressMode::ClampToEdge, + address_mode_v: wgpu::AddressMode::ClampToEdge, + address_mode_w: wgpu::AddressMode::ClampToEdge, + mag_filter: wgpu::FilterMode::Nearest, + min_filter: wgpu::FilterMode::Nearest, + mipmap_filter: wgpu::FilterMode::Nearest, + compare: Some(wgpu::CompareFunction::LessEqual), + ..Default::default() + }); + Self { depth_texture, depth_view, depth_sampler } + } + + pub fn resize(&mut self, renderer: &Renderer) { + let old_size = uvec2(self.depth_texture.size().width, self.depth_texture.size().height); + let new_size = uvec2(renderer.size().width, renderer.size().height); + if old_size == new_size { return } + let depth_texture_desc = Self::desc(new_size); + self.depth_texture = renderer.device().create_texture(&depth_texture_desc); + self.depth_view = self.depth_texture.create_view(&wgpu::TextureViewDescriptor::default()); + } +} + +pub fn init_depth_texture(renderer: &Renderer) -> DepthTexture { + DepthTexture::init(renderer) +} + +pub fn resize_depth_texture( + renderer: &Renderer, + depth_texture: &mut DepthTexture, +) { + depth_texture.resize(renderer); +}