This commit is contained in:
Carter Anderson 2020-01-13 02:11:30 -08:00
parent a798cf3346
commit 019cad9e04
5 changed files with 90 additions and 16 deletions

View File

@ -52,6 +52,7 @@ impl AppBuilder {
} }
pub fn add_default_passes(mut self) -> Self { pub fn add_default_passes(mut self) -> Self {
let msaa_samples = 8;
let render_graph = &mut self.render_graph; let render_graph = &mut self.render_graph;
render_graph render_graph
.add_render_resource_manager(Box::new(render_resources::MaterialResourceManager)); .add_render_resource_manager(Box::new(render_resources::MaterialResourceManager));
@ -62,14 +63,14 @@ impl AppBuilder {
.add_render_resource_manager(Box::new(render_resources::Global2dResourceManager)); .add_render_resource_manager(Box::new(render_resources::Global2dResourceManager));
let depth_format = wgpu::TextureFormat::Depth32Float; let depth_format = wgpu::TextureFormat::Depth32Float;
render_graph.set_pass("forward", Box::new(ForwardPass::new(depth_format))); render_graph.set_pass("forward", Box::new(ForwardPass::new(depth_format, msaa_samples)));
render_graph.set_pipeline("forward", "forward", Box::new(ForwardPipeline::new())); render_graph.set_pipeline("forward", "forward", Box::new(ForwardPipeline::new(msaa_samples)));
render_graph.set_pipeline( render_graph.set_pipeline(
"forward", "forward",
"forward_instanced", "forward_instanced",
Box::new(ForwardInstancedPipeline::new(depth_format)), Box::new(ForwardInstancedPipeline::new(depth_format, msaa_samples)),
); );
render_graph.set_pipeline("forward", "ui", Box::new(UiPipeline::new())); render_graph.set_pipeline("forward", "ui", Box::new(UiPipeline::new(msaa_samples)));
self self
} }

View File

@ -12,11 +12,15 @@ pub struct ForwardUniforms {
pub struct ForwardPass { pub struct ForwardPass {
pub depth_format: wgpu::TextureFormat, pub depth_format: wgpu::TextureFormat,
pub msaa_samples: usize,
} }
impl ForwardPass { impl ForwardPass {
pub fn new(depth_format: wgpu::TextureFormat) -> Self { pub fn new(depth_format: wgpu::TextureFormat, msaa_samples: usize) -> Self {
ForwardPass { depth_format } ForwardPass {
depth_format,
msaa_samples,
}
} }
fn get_depth_texture( fn get_depth_texture(
&self, &self,
@ -31,7 +35,7 @@ impl ForwardPass {
}, },
array_layer_count: 1, array_layer_count: 1,
mip_level_count: 1, mip_level_count: 1,
sample_count: 1, sample_count: self.msaa_samples as u32,
dimension: wgpu::TextureDimension::D2, dimension: wgpu::TextureDimension::D2,
format: self.depth_format, format: self.depth_format,
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT, usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
@ -39,16 +43,53 @@ impl ForwardPass {
texture.create_default_view() texture.create_default_view()
} }
fn get_multisampled_framebuffer(
&self,
device: &Device,
swap_chain_descriptor: &SwapChainDescriptor,
sample_count: usize,
) -> wgpu::TextureView {
let multisampled_texture_extent = wgpu::Extent3d {
width: swap_chain_descriptor.width,
height: swap_chain_descriptor.height,
depth: 1,
};
let multisampled_frame_descriptor = &wgpu::TextureDescriptor {
size: multisampled_texture_extent,
array_layer_count: 1,
mip_level_count: 1,
sample_count: sample_count as u32,
dimension: wgpu::TextureDimension::D2,
format: swap_chain_descriptor.format,
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
};
device
.create_texture(multisampled_frame_descriptor)
.create_default_view()
}
} }
const DEPTH_TEXTURE_NAME: &str = "forward_depth"; const DEPTH_TEXTURE_NAME: &str = "forward_depth";
const MULTISAMPLED_FRAMEBUFFER_TEXTURE_NAME: &str = "forward_multisampled_framebuffer";
impl Pass for ForwardPass { impl Pass for ForwardPass {
fn initialize(&self, render_graph: &mut RenderGraphData) { fn initialize(&self, render_graph: &mut RenderGraphData) {
let depth_texture = let depth_texture =
self.get_depth_texture(&render_graph.device, &render_graph.swap_chain_descriptor); self.get_depth_texture(&render_graph.device, &render_graph.swap_chain_descriptor);
render_graph.set_texture(DEPTH_TEXTURE_NAME, depth_texture); render_graph.set_texture(DEPTH_TEXTURE_NAME, depth_texture);
if self.msaa_samples > 1 {
let multisampled_framebuffer = self.get_multisampled_framebuffer(
&render_graph.device,
&render_graph.swap_chain_descriptor,
self.msaa_samples,
);
render_graph.set_texture(MULTISAMPLED_FRAMEBUFFER_TEXTURE_NAME, multisampled_framebuffer);
}
} }
fn begin<'a>( fn begin<'a>(
&mut self, &mut self,
render_graph: &mut RenderGraphData, render_graph: &mut RenderGraphData,
@ -57,8 +98,8 @@ impl Pass for ForwardPass {
frame: &'a wgpu::SwapChainOutput, frame: &'a wgpu::SwapChainOutput,
) -> Option<wgpu::RenderPass<'a>> { ) -> Option<wgpu::RenderPass<'a>> {
let depth_texture = render_graph.get_texture(DEPTH_TEXTURE_NAME); let depth_texture = render_graph.get_texture(DEPTH_TEXTURE_NAME);
Some(encoder.begin_render_pass(&wgpu::RenderPassDescriptor { let color_attachment = if self.msaa_samples == 1 {
color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { wgpu::RenderPassColorAttachmentDescriptor {
attachment: &frame.view, attachment: &frame.view,
resolve_target: None, resolve_target: None,
load_op: wgpu::LoadOp::Clear, load_op: wgpu::LoadOp::Clear,
@ -69,7 +110,24 @@ impl Pass for ForwardPass {
b: 0.5, b: 0.5,
a: 1.0, a: 1.0,
}, },
}], }
} else {
let multisampled_framebuffer = render_graph.get_texture(MULTISAMPLED_FRAMEBUFFER_TEXTURE_NAME).unwrap();
wgpu::RenderPassColorAttachmentDescriptor {
attachment: multisampled_framebuffer,
resolve_target: Some(&frame.view),
load_op: wgpu::LoadOp::Clear,
store_op: wgpu::StoreOp::Store,
clear_color: wgpu::Color {
r: 0.3,
g: 0.4,
b: 0.5,
a: 1.0,
},
}
};
Some(encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
color_attachments: &[color_attachment],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachmentDescriptor { depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachmentDescriptor {
attachment: depth_texture.unwrap(), attachment: depth_texture.unwrap(),
depth_load_op: wgpu::LoadOp::Clear, depth_load_op: wgpu::LoadOp::Clear,
@ -86,6 +144,15 @@ impl Pass for ForwardPass {
let depth_texture = let depth_texture =
self.get_depth_texture(&render_graph.device, &render_graph.swap_chain_descriptor); self.get_depth_texture(&render_graph.device, &render_graph.swap_chain_descriptor);
render_graph.set_texture(DEPTH_TEXTURE_NAME, depth_texture); render_graph.set_texture(DEPTH_TEXTURE_NAME, depth_texture);
if self.msaa_samples > 1 {
let multisampled_framebuffer = self.get_multisampled_framebuffer(
&render_graph.device,
&render_graph.swap_chain_descriptor,
self.msaa_samples,
);
render_graph.set_texture(MULTISAMPLED_FRAMEBUFFER_TEXTURE_NAME, multisampled_framebuffer);
}
} }
fn should_repeat(&self) -> bool { fn should_repeat(&self) -> bool {

View File

@ -6,13 +6,15 @@ pub struct ForwardPipeline {
pub pipeline: Option<wgpu::RenderPipeline>, pub pipeline: Option<wgpu::RenderPipeline>,
pub depth_format: wgpu::TextureFormat, pub depth_format: wgpu::TextureFormat,
pub bind_group: Option<wgpu::BindGroup>, pub bind_group: Option<wgpu::BindGroup>,
pub msaa_samples: usize,
} }
impl ForwardPipeline { impl ForwardPipeline {
pub fn new() -> Self { pub fn new(msaa_samples: usize) -> Self {
ForwardPipeline { ForwardPipeline {
pipeline: None, pipeline: None,
bind_group: None, bind_group: None,
msaa_samples,
depth_format: wgpu::TextureFormat::Depth32Float, depth_format: wgpu::TextureFormat::Depth32Float,
} }
} }
@ -120,7 +122,7 @@ impl Pipeline for ForwardPipeline {
}), }),
index_format: wgpu::IndexFormat::Uint16, index_format: wgpu::IndexFormat::Uint16,
vertex_buffers: &[vertex_buffer_descriptor], vertex_buffers: &[vertex_buffer_descriptor],
sample_count: 1, sample_count: self.msaa_samples as u32,
sample_mask: !0, sample_mask: !0,
alpha_to_coverage_enabled: false, alpha_to_coverage_enabled: false,
}, },

View File

@ -14,13 +14,15 @@ pub struct ForwardInstancedPipeline {
pub depth_format: wgpu::TextureFormat, pub depth_format: wgpu::TextureFormat,
pub local_bind_group: Option<wgpu::BindGroup>, pub local_bind_group: Option<wgpu::BindGroup>,
pub instance_buffer_infos: Option<Vec<InstanceBufferInfo>>, pub instance_buffer_infos: Option<Vec<InstanceBufferInfo>>,
pub msaa_samples: usize,
} }
impl ForwardInstancedPipeline { impl ForwardInstancedPipeline {
pub fn new(depth_format: wgpu::TextureFormat) -> Self { pub fn new(depth_format: wgpu::TextureFormat, msaa_samples: usize) -> Self {
ForwardInstancedPipeline { ForwardInstancedPipeline {
pipeline: None, pipeline: None,
depth_format, depth_format,
msaa_samples,
local_bind_group: None, local_bind_group: None,
instance_buffer_infos: None, instance_buffer_infos: None,
} }
@ -255,7 +257,7 @@ impl Pipeline for ForwardInstancedPipeline {
}), }),
index_format: wgpu::IndexFormat::Uint16, index_format: wgpu::IndexFormat::Uint16,
vertex_buffers: &[vertex_buffer_descriptor, instance_buffer_descriptor], vertex_buffers: &[vertex_buffer_descriptor, instance_buffer_descriptor],
sample_count: 1, sample_count: self.msaa_samples as u32,
sample_mask: !0, sample_mask: !0,
alpha_to_coverage_enabled: false, alpha_to_coverage_enabled: false,
}, },

View File

@ -24,13 +24,15 @@ pub struct UiPipeline {
pub depth_format: wgpu::TextureFormat, pub depth_format: wgpu::TextureFormat,
pub quad: Option<Handle<Mesh>>, pub quad: Option<Handle<Mesh>>,
pub bind_group: Option<wgpu::BindGroup>, pub bind_group: Option<wgpu::BindGroup>,
pub msaa_samples: usize,
} }
impl UiPipeline { impl UiPipeline {
pub fn new() -> Self { pub fn new(msaa_samples: usize) -> Self {
UiPipeline { UiPipeline {
pipeline: None, pipeline: None,
bind_group: None, bind_group: None,
msaa_samples,
quad: None, quad: None,
depth_format: wgpu::TextureFormat::Depth32Float, depth_format: wgpu::TextureFormat::Depth32Float,
} }
@ -220,7 +222,7 @@ impl Pipeline for UiPipeline {
}), }),
index_format: wgpu::IndexFormat::Uint16, index_format: wgpu::IndexFormat::Uint16,
vertex_buffers: &[vertex_buffer_descriptor, instance_buffer_descriptor], vertex_buffers: &[vertex_buffer_descriptor, instance_buffer_descriptor],
sample_count: 1, sample_count: self.msaa_samples as u32,
sample_mask: !0, sample_mask: !0,
alpha_to_coverage_enabled: false, alpha_to_coverage_enabled: false,
}, },