Enable multisampling

This commit is contained in:
numzero 2024-09-26 00:47:29 +03:00
parent ec6f2e3c57
commit 45ed4dff90
2 changed files with 50 additions and 6 deletions

View File

@ -162,7 +162,8 @@ impl<'a> State<'a> {
.await .await
.unwrap(); .unwrap();
let viewport = viewport::Viewport::new(&adapter, surface, uvec2(size.width, size.height)); let viewport =
viewport::Viewport::new(&adapter, &device, surface, uvec2(size.width, size.height));
let kbd = keyctl::Keyboard::new(); let kbd = keyctl::Keyboard::new();
let cam_loc = camctl::CameraLocation::new(); let cam_loc = camctl::CameraLocation::new();
@ -170,7 +171,7 @@ impl<'a> State<'a> {
let depth = None; let depth = None;
let msaa = wgpu::MultisampleState { let msaa = wgpu::MultisampleState {
count: 1, count: viewport.sample_count(),
mask: !0, mask: !0,
alpha_to_coverage_enabled: false, alpha_to_coverage_enabled: false,
}; };

View File

@ -3,10 +3,16 @@ use glam::{uvec2, UVec2};
pub struct Viewport<'a> { pub struct Viewport<'a> {
surface: wgpu::Surface<'a>, surface: wgpu::Surface<'a>,
config: wgpu::SurfaceConfiguration, config: wgpu::SurfaceConfiguration,
multisample: Multisample,
} }
impl<'a> Viewport<'a> { impl<'a> Viewport<'a> {
pub fn new(adapter: &wgpu::Adapter, surface: wgpu::Surface<'a>, size: UVec2) -> Self { pub fn new(
adapter: &wgpu::Adapter,
device: &wgpu::Device,
surface: wgpu::Surface<'a>,
size: UVec2,
) -> Self {
let caps = surface.get_capabilities(adapter); let caps = surface.get_capabilities(adapter);
let format = wgpu::TextureFormat::Bgra8Unorm; let format = wgpu::TextureFormat::Bgra8Unorm;
let config = wgpu::SurfaceConfiguration { let config = wgpu::SurfaceConfiguration {
@ -19,11 +25,17 @@ impl<'a> Viewport<'a> {
view_formats: vec![], view_formats: vec![],
desired_maximum_frame_latency: 2, desired_maximum_frame_latency: 2,
}; };
Self { surface, config } let multisample = Multisample::new(device, format, size);
Self {
surface,
config,
multisample,
}
} }
pub fn configure(&mut self, device: &wgpu::Device) { pub fn configure(&mut self, device: &wgpu::Device) {
self.surface.configure(&device, &self.config); self.surface.configure(&device, &self.config);
self.multisample = Multisample::new(device, self.format(), self.size());
} }
pub fn resize(&mut self, device: &wgpu::Device, size: UVec2) { pub fn resize(&mut self, device: &wgpu::Device, size: UVec2) {
@ -40,6 +52,10 @@ impl<'a> Viewport<'a> {
self.config.format self.config.format
} }
pub fn sample_count(&self) -> u32 {
Multisample::SAMPLE_COUNT
}
pub fn render_single_pass( pub fn render_single_pass(
&mut self, &mut self,
device: &wgpu::Device, device: &wgpu::Device,
@ -56,8 +72,8 @@ impl<'a> Viewport<'a> {
let render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { let render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("RenderPass"), label: Some("RenderPass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment { color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &view, view: &self.multisample.view,
resolve_target: None, resolve_target: Some(&view),
ops: wgpu::Operations { ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color { load: wgpu::LoadOp::Clear(wgpu::Color {
r: 0., r: 0.,
@ -78,3 +94,30 @@ impl<'a> Viewport<'a> {
Ok(()) Ok(())
} }
} }
struct Multisample {
view: wgpu::TextureView,
}
impl Multisample {
const SAMPLE_COUNT: u32 = 4;
fn new(device: &wgpu::Device, format: wgpu::TextureFormat, size: UVec2) -> Multisample {
let tex = device.create_texture(&wgpu::TextureDescriptor {
label: Some("Multisample texture"),
size: wgpu::Extent3d {
width: size.x,
height: size.y,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: Self::SAMPLE_COUNT,
dimension: wgpu::TextureDimension::D2,
format,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
view_formats: &[],
});
let view = tex.create_view(&wgpu::TextureViewDescriptor::default());
Multisample { view }
}
}