From 51cdd7873336e5b19bec2f52f415f7fac006ab11 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 26 Oct 2022 12:09:55 +0200 Subject: [PATCH 1/3] Refactor --- crates/fj-viewer/src/graphics/renderer.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/fj-viewer/src/graphics/renderer.rs b/crates/fj-viewer/src/graphics/renderer.rs index efdf76adc..8a19d2e00 100644 --- a/crates/fj-viewer/src/graphics/renderer.rs +++ b/crates/fj-viewer/src/graphics/renderer.rs @@ -187,9 +187,8 @@ impl Renderer { self.surface.configure(&self.device, &self.surface_config); - let depth_view = + self.depth_view = Self::create_depth_buffer(&self.device, &self.surface_config); - self.depth_view = depth_view; } /// Draws the renderer, camera, and config state to the window. From aabe37bf5f0b2225fbda919000ac3e2c6081acea Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 26 Oct 2022 12:14:54 +0200 Subject: [PATCH 2/3] Add `SAMPLE_COUNT` constant --- crates/fj-viewer/src/graphics/mod.rs | 1 + crates/fj-viewer/src/graphics/pipelines.rs | 4 ++-- crates/fj-viewer/src/graphics/renderer.rs | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/crates/fj-viewer/src/graphics/mod.rs b/crates/fj-viewer/src/graphics/mod.rs index 6179b129a..60711a990 100644 --- a/crates/fj-viewer/src/graphics/mod.rs +++ b/crates/fj-viewer/src/graphics/mod.rs @@ -16,3 +16,4 @@ pub use self::{ }; const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float; +const SAMPLE_COUNT: u32 = 1; diff --git a/crates/fj-viewer/src/graphics/pipelines.rs b/crates/fj-viewer/src/graphics/pipelines.rs index 0eef9ef49..76cee8e40 100644 --- a/crates/fj-viewer/src/graphics/pipelines.rs +++ b/crates/fj-viewer/src/graphics/pipelines.rs @@ -3,7 +3,7 @@ use std::mem::size_of; use super::{ shaders::{Shader, Shaders}, vertices::Vertex, - DEPTH_FORMAT, + DEPTH_FORMAT, SAMPLE_COUNT, }; #[derive(Debug)] @@ -108,7 +108,7 @@ impl Pipeline { bias: wgpu::DepthBiasState::default(), }), multisample: wgpu::MultisampleState { - count: 1, + count: SAMPLE_COUNT, mask: !0, alpha_to_coverage_enabled: false, }, diff --git a/crates/fj-viewer/src/graphics/renderer.rs b/crates/fj-viewer/src/graphics/renderer.rs index 8a19d2e00..2fbff0183 100644 --- a/crates/fj-viewer/src/graphics/renderer.rs +++ b/crates/fj-viewer/src/graphics/renderer.rs @@ -14,7 +14,7 @@ use crate::{ use super::{ draw_config::DrawConfig, drawables::Drawables, geometries::Geometries, pipelines::Pipelines, transform::Transform, uniforms::Uniforms, - vertices::Vertices, DEPTH_FORMAT, + vertices::Vertices, DEPTH_FORMAT, SAMPLE_COUNT, }; /// Graphics rendering state and target abstraction @@ -315,7 +315,7 @@ impl Renderer { depth_or_array_layers: 1, }, mip_level_count: 1, - sample_count: 1, + sample_count: SAMPLE_COUNT, dimension: wgpu::TextureDimension::D2, format: DEPTH_FORMAT, usage: wgpu::TextureUsages::RENDER_ATTACHMENT, From ad3e2c52678dfa48d29e5ce4d58b5abdf9e6d31e Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 26 Oct 2022 12:16:21 +0200 Subject: [PATCH 3/3] Enable multisampling --- crates/fj-viewer/src/graphics/mod.rs | 2 +- crates/fj-viewer/src/graphics/pipelines.rs | 2 +- crates/fj-viewer/src/graphics/renderer.rs | 32 ++++++++++++++++++++-- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/crates/fj-viewer/src/graphics/mod.rs b/crates/fj-viewer/src/graphics/mod.rs index 60711a990..06109fe5c 100644 --- a/crates/fj-viewer/src/graphics/mod.rs +++ b/crates/fj-viewer/src/graphics/mod.rs @@ -16,4 +16,4 @@ pub use self::{ }; const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float; -const SAMPLE_COUNT: u32 = 1; +const SAMPLE_COUNT: u32 = 4; diff --git a/crates/fj-viewer/src/graphics/pipelines.rs b/crates/fj-viewer/src/graphics/pipelines.rs index 76cee8e40..fb64e3cb8 100644 --- a/crates/fj-viewer/src/graphics/pipelines.rs +++ b/crates/fj-viewer/src/graphics/pipelines.rs @@ -110,7 +110,7 @@ impl Pipeline { multisample: wgpu::MultisampleState { count: SAMPLE_COUNT, mask: !0, - alpha_to_coverage_enabled: false, + alpha_to_coverage_enabled: true, }, fragment: Some(wgpu::FragmentState { module: shader.module, diff --git a/crates/fj-viewer/src/graphics/renderer.rs b/crates/fj-viewer/src/graphics/renderer.rs index 2fbff0183..cea4a85ce 100644 --- a/crates/fj-viewer/src/graphics/renderer.rs +++ b/crates/fj-viewer/src/graphics/renderer.rs @@ -26,6 +26,7 @@ pub struct Renderer { queue: wgpu::Queue, surface_config: wgpu::SurfaceConfiguration, + frame_buffer: wgpu::TextureView, depth_view: wgpu::TextureView, uniform_buffer: wgpu::Buffer, @@ -106,6 +107,7 @@ impl Renderer { }; surface.configure(&device, &surface_config); + let frame_buffer = Self::create_frame_buffer(&device, &surface_config); let depth_view = Self::create_depth_buffer(&device, &surface_config); let uniform_buffer = @@ -158,6 +160,7 @@ impl Renderer { queue, surface_config, + frame_buffer, depth_view, uniform_buffer, @@ -187,6 +190,8 @@ impl Renderer { self.surface.configure(&self.device, &self.surface_config); + self.frame_buffer = + Self::create_frame_buffer(&self.device, &self.surface_config); self.depth_view = Self::create_depth_buffer(&self.device, &self.surface_config); } @@ -242,11 +247,12 @@ impl Renderer { label: None, color_attachments: &[Some( wgpu::RenderPassColorAttachment { - view: &color_view, - resolve_target: None, + view: &self.frame_buffer, + resolve_target: Some(&color_view), ops: wgpu::Operations { load: wgpu::LoadOp::Clear(wgpu::Color::WHITE), - store: true, + // Not necessary, due to MSAA being enabled. + store: false, }, }, )], @@ -303,6 +309,26 @@ impl Renderer { Ok(()) } + fn create_frame_buffer( + device: &wgpu::Device, + surface_config: &wgpu::SurfaceConfiguration, + ) -> wgpu::TextureView { + let texture = device.create_texture(&wgpu::TextureDescriptor { + label: None, + size: wgpu::Extent3d { + width: surface_config.width, + height: surface_config.height, + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: SAMPLE_COUNT, + dimension: wgpu::TextureDimension::D2, + format: surface_config.format, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT, + }); + texture.create_view(&wgpu::TextureViewDescriptor::default()) + } + fn create_depth_buffer( device: &wgpu::Device, surface_config: &wgpu::SurfaceConfiguration,