From d81eebe423fd3e00df5ff035ec24fe7fb37f2c62 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 7 Jan 2025 12:25:37 +0100 Subject: albedo texture works --- client/src/scene_render.rs | 109 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 84 insertions(+), 25 deletions(-) (limited to 'client/src/scene_render.rs') diff --git a/client/src/scene_render.rs b/client/src/scene_render.rs index b4965e1..8703c9f 100644 --- a/client/src/scene_render.rs +++ b/client/src/scene_render.rs @@ -1,21 +1,24 @@ use glam::{EulerRot, Mat3, Mat4, Vec3, vec3}; -use std::collections::{HashMap, HashSet}; +use std::sync::Arc; use weareshared::{packets::Resource, tree::SceneTree}; use wgpu::{ - BindGroup, BindGroupDescriptor, BindGroupLayoutDescriptor, BlendState, Color, ColorTargetState, - ColorWrites, CommandEncoder, Device, FragmentState, FrontFace, IndexFormat, LoadOp, + BindGroupLayout, BindGroupLayoutDescriptor, BindGroupLayoutEntry, BindingType, BlendState, + Color, ColorTargetState, ColorWrites, CommandEncoder, CompareFunction, DepthBiasState, + DepthStencilState, Device, Extent3d, FragmentState, FrontFace, IndexFormat, LoadOp, MultisampleState, Operations, PipelineCompilationOptions, PipelineLayoutDescriptor, PolygonMode, PrimitiveState, PrimitiveTopology, PushConstantRange, RenderPassColorAttachment, - RenderPassDescriptor, RenderPipeline, RenderPipelineDescriptor, ShaderStages, StoreOp, - TextureFormat, TextureView, VertexAttribute, VertexBufferLayout, VertexFormat, VertexState, - VertexStepMode, include_wgsl, + RenderPassDepthStencilAttachment, RenderPassDescriptor, RenderPipeline, + RenderPipelineDescriptor, SamplerBindingType, ShaderStages, StencilState, StoreOp, + TextureDescriptor, TextureDimension, TextureFormat, TextureSampleType, TextureUsages, + TextureView, TextureViewDescriptor, TextureViewDimension, VertexAttribute, VertexBufferLayout, + VertexFormat, VertexState, VertexStepMode, include_wgsl, }; -use crate::scene_prepare::RPrefab; +use crate::scene_prepare::{DemandMap, RPrefab}; pub struct ScenePipeline { pipeline: RenderPipeline, - bind_group: BindGroup, + depth: TextureView, } macro_rules! v_attr { @@ -33,17 +36,45 @@ macro_rules! v_attr { } impl ScenePipeline { - pub fn new(device: &Device, format: TextureFormat) -> Self { + pub fn new(device: &Device, format: TextureFormat) -> (Self, BindGroupLayout) { let module = device.create_shader_module(include_wgsl!("shader.wgsl")); - let bind_group_layout = device.create_bind_group_layout(&BindGroupLayoutDescriptor { - entries: &[], + let depth = device.create_texture(&TextureDescriptor { label: None, + size: Extent3d { + height: 256, + width: 256, + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: TextureDimension::D2, + format: TextureFormat::Depth32Float, + usage: TextureUsages::RENDER_ATTACHMENT, + view_formats: &[], }); - let bind_group = device.create_bind_group(&BindGroupDescriptor { + let depth = depth.create_view(&TextureViewDescriptor::default()); + + let bind_group_layout = device.create_bind_group_layout(&BindGroupLayoutDescriptor { + entries: &[ + BindGroupLayoutEntry { + binding: 0, + count: None, + visibility: ShaderStages::FRAGMENT, + ty: BindingType::Texture { + sample_type: TextureSampleType::Float { filterable: true }, + view_dimension: TextureViewDimension::D2, + multisampled: false, + }, + }, + BindGroupLayoutEntry { + binding: 1, + count: None, + visibility: ShaderStages::FRAGMENT, + ty: BindingType::Sampler(SamplerBindingType::Filtering), + }, + ], label: None, - layout: &bind_group_layout, - entries: &[], }); let pipeline_layout = device.create_pipeline_layout(&PipelineLayoutDescriptor { label: None, @@ -79,23 +110,45 @@ impl ScenePipeline { polygon_mode: PolygonMode::Fill, ..Default::default() }, - depth_stencil: Default::default(), + depth_stencil: Some(DepthStencilState { + depth_compare: CompareFunction::Greater, + depth_write_enabled: true, + format: TextureFormat::Depth32Float, + bias: DepthBiasState::default(), + stencil: StencilState::default(), + }), multisample: MultisampleState::default(), multiview: None, cache: None, }); - Self { - bind_group, - pipeline, - } + (Self { pipeline, depth }, bind_group_layout) + } + + pub fn resize(&mut self, device: &Device, width: u32, height: u32) { + self.depth = device + .create_texture(&TextureDescriptor { + label: None, + size: Extent3d { + height, + width, + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: TextureDimension::D2, + format: TextureFormat::Depth32Float, + usage: TextureUsages::RENDER_ATTACHMENT, + view_formats: &[], + }) + .create_view(&TextureViewDescriptor::default()); } + pub fn draw( &mut self, commands: &mut CommandEncoder, target: &TextureView, scene: &SceneTree, - prefabs: &HashMap, - prefabs_needed: &mut HashSet, + prefabs: &mut DemandMap>, ) { let mut rpass = commands.begin_render_pass(&RenderPassDescriptor { label: None, @@ -112,6 +165,14 @@ impl ScenePipeline { }), }, })], + depth_stencil_attachment: Some(RenderPassDepthStencilAttachment { + view: &self.depth, + depth_ops: Some(Operations { + load: LoadOp::Clear(0.), + store: StoreOp::Store, + }), + stencil_ops: None, + }), ..Default::default() }); @@ -127,14 +188,14 @@ impl ScenePipeline { ob.rot.z, )) * Mat4::from_translation(ob.pos.into()); - if let Some(prefab) = prefabs.get(&ob.res) { + if let Some(prefab) = prefabs.try_get(ob.res) { for (affine, part) in &prefab.0 { let part_projection = prefab_projection * Mat4::from_mat3a(affine.matrix3) * Mat4::from_translation(affine.translation.into()); let projection = part_projection.to_cols_array().map(|v| v.to_le_bytes()); - rpass.set_bind_group(0, &self.bind_group, &[]); + rpass.set_bind_group(0, &*part.texture, &[]); rpass.set_pipeline(&self.pipeline); rpass.set_push_constants(ShaderStages::VERTEX, 0, projection.as_flattened()); rpass.set_index_buffer(part.index.slice(..), IndexFormat::Uint16); @@ -148,8 +209,6 @@ impl ScenePipeline { rpass.set_vertex_buffer(7, part.texcoord[1].slice(..)); rpass.draw_indexed(0..part.index_count, 0, 0..1); } - } else { - prefabs_needed.insert(ob.res); } } } -- cgit v1.2.3-70-g09d2