diff options
Diffstat (limited to 'client/src')
-rw-r--r-- | client/src/renderer.rs | 44 | ||||
-rw-r--r-- | client/src/scene_render.rs | 52 | ||||
-rw-r--r-- | client/src/ui.rs | 73 | ||||
-rw-r--r-- | client/src/ui.wgsl | 9 |
4 files changed, 95 insertions, 83 deletions
diff --git a/client/src/renderer.rs b/client/src/renderer.rs index 66f8c5e..7bd98f0 100644 --- a/client/src/renderer.rs +++ b/client/src/renderer.rs @@ -23,9 +23,10 @@ use pollster::FutureExt; use std::sync::Arc; use weareshared::tree::SceneTree; use wgpu::{ - Backends, CommandEncoderDescriptor, Device, DeviceDescriptor, Features, Instance, + Backends, CommandEncoderDescriptor, Device, DeviceDescriptor, Extent3d, Features, Instance, InstanceDescriptor, Limits, MaintainBase, PowerPreference, Queue, RequestAdapterOptions, - Surface, SurfaceConfiguration, TextureViewDescriptor, + Surface, SurfaceConfiguration, TextureDescriptor, TextureDimension, TextureFormat, + TextureUsages, TextureView, TextureViewDescriptor, }; use winit::window::Window; @@ -38,6 +39,7 @@ pub struct Renderer<'a> { ui_renderer: UiRenderer, pub scene_prepare: ScenePreparer, surface_needs_reconfigure: bool, + depth: TextureView, } impl<'a> Renderer<'a> { pub fn new(window: &'a Window) -> Result<Self> { @@ -87,10 +89,27 @@ impl<'a> Renderer<'a> { let ui_renderer = UiRenderer::new(&device, surface_configuration.format); + 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 depth = depth.create_view(&TextureViewDescriptor::default()); + Ok(Self { scene_pipeline, scene_prepare, surface, + depth, device, queue, surface_configuration, @@ -104,7 +123,24 @@ impl<'a> Renderer<'a> { self.surface_configuration.height = height; self.surface .configure(&self.device, &self.surface_configuration); - self.scene_pipeline.resize(&self.device, width, height); + + self.depth = self + .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, scene: &SceneTree, camera: &Camera) -> Result<()> { @@ -131,6 +167,7 @@ impl<'a> Renderer<'a> { self.scene_pipeline.draw( &mut commands, &target_view, + &self.depth, scene, &mut self.scene_prepare.prefabs, projection, @@ -141,6 +178,7 @@ impl<'a> Renderer<'a> { &self.queue, &mut commands, &target_view, + &self.depth, projection, ); diff --git a/client/src/scene_render.rs b/client/src/scene_render.rs index 9dc14d1..9aceaef 100644 --- a/client/src/scene_render.rs +++ b/client/src/scene_render.rs @@ -20,21 +20,19 @@ use weareshared::{packets::Resource, resources::Prefab, tree::SceneTree}; use wgpu::{ 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, + DepthStencilState, Device, FragmentState, FrontFace, IndexFormat, LoadOp, MultisampleState, + Operations, PipelineCompilationOptions, PipelineLayoutDescriptor, PolygonMode, PrimitiveState, + PrimitiveTopology, PushConstantRange, RenderPassColorAttachment, RenderPassDepthStencilAttachment, RenderPassDescriptor, RenderPipeline, RenderPipelineDescriptor, SamplerBindingType, ShaderStages, StencilState, StoreOp, - TextureDescriptor, TextureDimension, TextureFormat, TextureSampleType, TextureUsages, - TextureView, TextureViewDescriptor, TextureViewDimension, VertexAttribute, VertexBufferLayout, - VertexFormat, VertexState, VertexStepMode, include_wgsl, + TextureFormat, TextureSampleType, TextureView, TextureViewDimension, VertexAttribute, + VertexBufferLayout, VertexFormat, VertexState, VertexStepMode, include_wgsl, }; use crate::scene_prepare::{DemandMap, RPrefab}; pub struct ScenePipeline { pipeline: RenderPipeline, - depth: TextureView, } macro_rules! v_attr { @@ -55,22 +53,6 @@ impl ScenePipeline { pub fn new(device: &Device, format: TextureFormat) -> (Self, BindGroupLayout) { let module = device.create_shader_module(include_wgsl!("shader.wgsl")); - 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 depth = depth.create_view(&TextureViewDescriptor::default()); - let bind_group_layout = device.create_bind_group_layout(&BindGroupLayoutDescriptor { entries: &[ BindGroupLayoutEntry { @@ -137,32 +119,14 @@ impl ScenePipeline { multiview: None, cache: None, }); - (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()); + (Self { pipeline }, bind_group_layout) } pub fn draw( &mut self, commands: &mut CommandEncoder, target: &TextureView, + depth: &TextureView, scene: &SceneTree, prefabs: &mut DemandMap<Resource<Prefab>, Arc<RPrefab>>, projection: Mat4, @@ -183,7 +147,7 @@ impl ScenePipeline { }, })], depth_stencil_attachment: Some(RenderPassDepthStencilAttachment { - view: &self.depth, + view: &depth, depth_ops: Some(Operations { load: LoadOp::Clear(1.), store: StoreOp::Store, diff --git a/client/src/ui.rs b/client/src/ui.rs index 69f1081..c3e2e38 100644 --- a/client/src/ui.rs +++ b/client/src/ui.rs @@ -18,21 +18,23 @@ use egui::{ Context, ImageData, TextureId, epaint::{Primitive, Vertex}, }; -use glam::Mat4; +use glam::{Mat2, Mat3, Mat4}; use log::info; use std::{collections::HashMap, num::NonZeroU64}; use wgpu::{ - BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayout, BindGroupLayoutDescriptor, - BindGroupLayoutEntry, BindingResource, BindingType, BlendState, Buffer, BufferDescriptor, - BufferUsages, ColorTargetState, ColorWrites, CommandEncoder, Device, Extent3d, FragmentState, - FrontFace, IndexFormat, LoadOp, MultisampleState, Operations, PipelineCompilationOptions, + AddressMode, BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayout, + BindGroupLayoutDescriptor, BindGroupLayoutEntry, BindingResource, BindingType, BlendState, + Buffer, BufferDescriptor, BufferUsages, ColorTargetState, ColorWrites, CommandEncoder, + CompareFunction, DepthStencilState, Device, Extent3d, FilterMode, FragmentState, FrontFace, + IndexFormat, LoadOp, MultisampleState, Operations, PipelineCompilationOptions, PipelineLayoutDescriptor, PolygonMode, PrimitiveState, PrimitiveTopology, PushConstantRange, - Queue, RenderPassColorAttachment, RenderPassDescriptor, RenderPipeline, - RenderPipelineDescriptor, SamplerBindingType, SamplerDescriptor, ShaderStages, StoreOp, - Texture, TextureDescriptor, TextureDimension, TextureFormat, TextureSampleType, TextureUsages, - TextureView, TextureViewDescriptor, TextureViewDimension, VertexAttribute, VertexBufferLayout, - VertexFormat, VertexState, VertexStepMode, include_wgsl, + Queue, RenderPassColorAttachment, RenderPassDepthStencilAttachment, RenderPassDescriptor, + RenderPipeline, RenderPipelineDescriptor, SamplerBindingType, SamplerDescriptor, ShaderStages, + StoreOp, Texture, TextureDescriptor, TextureDimension, TextureFormat, TextureSampleType, + TextureUsages, TextureView, TextureViewDescriptor, TextureViewDimension, VertexBufferLayout, + VertexState, VertexStepMode, include_wgsl, util::{DeviceExt, TextureDataOrder}, + vertex_attr_array, }; pub struct UiRenderer { @@ -109,23 +111,7 @@ impl UiRenderer { buffers: &[VertexBufferLayout { array_stride: size_of::<Vertex>() as u64, step_mode: VertexStepMode::Vertex, - attributes: &[ - VertexAttribute { - format: VertexFormat::Float32x2, - offset: 0, - shader_location: 0, - }, - VertexAttribute { - format: VertexFormat::Float32x2, - offset: size_of::<f32>() as u64 * 2, - shader_location: 1, - }, - VertexAttribute { - format: VertexFormat::Uint32, - offset: size_of::<f32>() as u64 * 4, - shader_location: 2, - }, - ], + attributes: &vertex_attr_array![0 => Float32x2, 1 => Float32x2, 2 => Uint32], }], compilation_options: PipelineCompilationOptions::default(), }, @@ -136,7 +122,13 @@ impl UiRenderer { polygon_mode: PolygonMode::Fill, ..Default::default() }, - depth_stencil: None, + depth_stencil: Some(DepthStencilState { + format: TextureFormat::Depth32Float, + depth_write_enabled: false, + depth_compare: CompareFunction::Always, + stencil: Default::default(), + bias: Default::default(), + }), multisample: MultisampleState::default(), multiview: None, cache: None, @@ -159,6 +151,7 @@ impl UiRenderer { queue: &Queue, commands: &mut CommandEncoder, target: &TextureView, + depth: &TextureView, projection: Mat4, ) { let raw_input = egui::RawInput::default(); @@ -210,6 +203,10 @@ impl UiRenderer { ); let textureview = texture.create_view(&TextureViewDescriptor::default()); let sampler = device.create_sampler(&SamplerDescriptor { + address_mode_u: AddressMode::ClampToEdge, + address_mode_v: AddressMode::ClampToEdge, + mag_filter: FilterMode::Linear, + min_filter: FilterMode::Linear, ..Default::default() }); let bindgroup = device.create_bind_group(&BindGroupDescriptor { @@ -254,7 +251,7 @@ impl UiRenderer { .expect("ui index buffer overflow"); let mut mapped_vertex = queue .write_buffer_with( - &self.index, + &self.vertex, 0, NonZeroU64::new((size_of::<Vertex>() * vertex_count) as u64).unwrap(), ) @@ -281,25 +278,39 @@ impl UiRenderer { } } + assert_eq!(index_count, index_offset); + assert_eq!(vertex_count, vertex_offset); + let mut rpass = commands.begin_render_pass(&RenderPassDescriptor { label: None, color_attachments: &[Some(RenderPassColorAttachment { view: target, resolve_target: None, ops: Operations { - store: StoreOp::Store, load: LoadOp::Load, + store: StoreOp::Store, }, })], + depth_stencil_attachment: Some(RenderPassDepthStencilAttachment { + view: &depth, + depth_ops: Some(Operations { + load: LoadOp::Load, + store: StoreOp::Store, + }), + stencil_ops: None, + }), ..Default::default() }); + let projection = projection + * Mat4::from_mat3(Mat3::from_mat2(Mat2::from_cols_array(&[1., 0., 0., -1.]))); + let projection = projection.to_cols_array().map(|v| v.to_le_bytes()); rpass.set_pipeline(&self.pipeline); + rpass.set_push_constants(ShaderStages::VERTEX, 0, projection.as_flattened()); rpass.set_index_buffer(self.index.slice(..), IndexFormat::Uint32); rpass.set_vertex_buffer(0, self.vertex.slice(..)); - rpass.set_push_constants(ShaderStages::VERTEX, 0, projection.as_flattened()); for (index, base_vertex, texid) in slices { rpass.set_bind_group(0, &self.textures.get(&texid).unwrap().0, &[]); rpass.draw_indexed(index, base_vertex, 0..1); diff --git a/client/src/ui.wgsl b/client/src/ui.wgsl index 3dd9b62..6361782 100644 --- a/client/src/ui.wgsl +++ b/client/src/ui.wgsl @@ -38,14 +38,13 @@ fn unpack_color(color: u32) -> vec4<f32> { } @vertex -fn vs_main(vi: VertexIn) -> VertexOut { - // var clip = project * vec4(vi.pos, 0., 1.); - var clip = vec4(vi.pos / 200., 0., 1.); +fn vs_main(@builtin(vertex_index) vindex: u32, vi: VertexIn) -> VertexOut { + var clip = project * vec4(vi.pos / 10., 0., 1.); + // var clip = vec4(vi.pos / 100., 0., 1.); let vo = VertexOut(clip, vi.uv, unpack_color(vi.color)); return vo; } @fragment fn fs_main(vo: VertexOut) -> @location(0) vec4<f32> { - return vec4(1., 0., 1., 1.); - // return textureSample(texture, texture_sampler, vo.uv) * vec4(vo.color, 1.); + return textureSample(texture, texture_sampler, vo.uv) * vo.color; } |