Skip to content

Commit

Permalink
Merge pull request #1274 from hannobraun/msaa
Browse files Browse the repository at this point in the history
Enable multisample anti-aliasing
  • Loading branch information
hannobraun authored Oct 26, 2022
2 parents 8bad590 + ad3e2c5 commit 0494fe7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
1 change: 1 addition & 0 deletions crates/fj-viewer/src/graphics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ pub use self::{
};

const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
const SAMPLE_COUNT: u32 = 4;
6 changes: 3 additions & 3 deletions crates/fj-viewer/src/graphics/pipelines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::mem::size_of;
use super::{
shaders::{Shader, Shaders},
vertices::Vertex,
DEPTH_FORMAT,
DEPTH_FORMAT, SAMPLE_COUNT,
};

#[derive(Debug)]
Expand Down Expand Up @@ -108,9 +108,9 @@ impl Pipeline {
bias: wgpu::DepthBiasState::default(),
}),
multisample: wgpu::MultisampleState {
count: 1,
count: SAMPLE_COUNT,
mask: !0,
alpha_to_coverage_enabled: false,
alpha_to_coverage_enabled: true,
},
fragment: Some(wgpu::FragmentState {
module: shader.module,
Expand Down
39 changes: 32 additions & 7 deletions crates/fj-viewer/src/graphics/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -158,6 +160,7 @@ impl Renderer {
queue,

surface_config,
frame_buffer,
depth_view,

uniform_buffer,
Expand Down Expand Up @@ -187,9 +190,10 @@ impl Renderer {

self.surface.configure(&self.device, &self.surface_config);

let depth_view =
self.frame_buffer =
Self::create_frame_buffer(&self.device, &self.surface_config);
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.
Expand Down Expand Up @@ -243,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,
},
},
)],
Expand Down Expand Up @@ -304,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,
Expand All @@ -316,7 +341,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,
Expand Down

0 comments on commit 0494fe7

Please sign in to comment.