From dbfb01fa7b5b05686dcbdd9d73643d7866810668 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Fri, 10 Jan 2025 14:33:57 +0100 Subject: fix egui rendering --- client/src/ui.rs | 73 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 31 deletions(-) (limited to 'client/src/ui.rs') 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::() as u64, step_mode: VertexStepMode::Vertex, - attributes: &[ - VertexAttribute { - format: VertexFormat::Float32x2, - offset: 0, - shader_location: 0, - }, - VertexAttribute { - format: VertexFormat::Float32x2, - offset: size_of::() as u64 * 2, - shader_location: 1, - }, - VertexAttribute { - format: VertexFormat::Uint32, - offset: size_of::() 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_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); -- cgit v1.2.3-70-g09d2