2020-12-07 15:47:44 +00:00
|
|
|
use super::super::{AaMode, Consts, GlobalsLayouts};
|
2020-09-26 15:43:59 +00:00
|
|
|
use bytemuck::{Pod, Zeroable};
|
2020-10-21 21:05:25 +00:00
|
|
|
use vek::*;
|
2020-08-24 21:17:40 +00:00
|
|
|
|
|
|
|
#[repr(C)]
|
2020-11-29 02:14:22 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Zeroable, Pod)]
|
2020-08-24 21:17:40 +00:00
|
|
|
pub struct Locals {
|
|
|
|
proj_mat_inv: [[f32; 4]; 4],
|
|
|
|
view_mat_inv: [[f32; 4]; 4],
|
2019-05-06 08:22:47 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 21:05:25 +00:00
|
|
|
impl Default for Locals {
|
|
|
|
fn default() -> Self { Self::new(Mat4::identity(), Mat4::identity()) }
|
|
|
|
}
|
|
|
|
|
2019-05-06 08:22:47 +00:00
|
|
|
impl Locals {
|
2020-10-21 21:05:25 +00:00
|
|
|
pub fn new(proj_mat_inv: Mat4<f32>, view_mat_inv: Mat4<f32>) -> Self {
|
|
|
|
Self {
|
|
|
|
proj_mat_inv: proj_mat_inv.into_col_arrays(),
|
|
|
|
view_mat_inv: view_mat_inv.into_col_arrays(),
|
|
|
|
}
|
|
|
|
}
|
2019-05-06 08:22:47 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 09:31:35 +00:00
|
|
|
pub struct BindGroup {
|
|
|
|
pub(in super::super) bind_group: wgpu::BindGroup,
|
2019-05-06 08:22:47 +00:00
|
|
|
}
|
2020-09-13 10:03:42 +00:00
|
|
|
|
|
|
|
pub struct PostProcessLayout {
|
2020-12-05 09:31:35 +00:00
|
|
|
pub layout: wgpu::BindGroupLayout,
|
2020-09-13 10:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PostProcessLayout {
|
|
|
|
pub fn new(device: &wgpu::Device) -> Self {
|
|
|
|
Self {
|
2020-12-05 09:31:35 +00:00
|
|
|
layout: device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
2020-09-13 10:03:42 +00:00
|
|
|
label: None,
|
|
|
|
entries: &[
|
2020-11-29 02:14:22 +00:00
|
|
|
// src color
|
2020-09-13 10:03:42 +00:00
|
|
|
wgpu::BindGroupLayoutEntry {
|
|
|
|
binding: 0,
|
|
|
|
visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT,
|
2020-12-05 12:50:06 +00:00
|
|
|
ty: wgpu::BindingType::Texture {
|
|
|
|
sample_type: wgpu::TextureSampleType::Float { filterable: true },
|
|
|
|
view_dimension: wgpu::TextureViewDimension::D2,
|
2020-09-13 10:03:42 +00:00
|
|
|
multisampled: false,
|
|
|
|
},
|
|
|
|
count: None,
|
|
|
|
},
|
|
|
|
wgpu::BindGroupLayoutEntry {
|
|
|
|
binding: 1,
|
|
|
|
visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT,
|
2020-12-05 22:04:29 +00:00
|
|
|
ty: wgpu::BindingType::Sampler {
|
|
|
|
filtering: true,
|
|
|
|
comparison: false,
|
|
|
|
},
|
2020-09-13 10:03:42 +00:00
|
|
|
count: None,
|
|
|
|
},
|
2020-11-29 02:14:22 +00:00
|
|
|
// Locals
|
|
|
|
wgpu::BindGroupLayoutEntry {
|
2020-12-05 20:20:56 +00:00
|
|
|
binding: 2,
|
2020-11-29 02:14:22 +00:00
|
|
|
visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT,
|
2020-12-05 12:50:06 +00:00
|
|
|
ty: wgpu::BindingType::Buffer {
|
|
|
|
ty: wgpu::BufferBindingType::Uniform,
|
|
|
|
has_dynamic_offset: false,
|
2020-11-29 02:14:22 +00:00
|
|
|
min_binding_size: None,
|
|
|
|
},
|
|
|
|
count: None,
|
|
|
|
},
|
2020-09-13 10:03:42 +00:00
|
|
|
],
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
2020-12-05 09:31:35 +00:00
|
|
|
|
|
|
|
pub fn bind(
|
|
|
|
&self,
|
|
|
|
device: &wgpu::Device,
|
|
|
|
src_color: &wgpu::TextureView,
|
|
|
|
sampler: &wgpu::Sampler,
|
|
|
|
locals: &Consts<Locals>,
|
|
|
|
) -> BindGroup {
|
|
|
|
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
|
|
|
label: None,
|
|
|
|
layout: &self.layout,
|
|
|
|
entries: &[
|
|
|
|
wgpu::BindGroupEntry {
|
|
|
|
binding: 0,
|
|
|
|
resource: wgpu::BindingResource::TextureView(src_color),
|
|
|
|
},
|
|
|
|
wgpu::BindGroupEntry {
|
|
|
|
binding: 1,
|
|
|
|
resource: wgpu::BindingResource::Sampler(sampler),
|
|
|
|
},
|
|
|
|
wgpu::BindGroupEntry {
|
|
|
|
binding: 2,
|
|
|
|
resource: locals.buf().as_entire_binding(),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
BindGroup { bind_group }
|
|
|
|
}
|
2020-09-13 10:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct PostProcessPipeline {
|
|
|
|
pub pipeline: wgpu::RenderPipeline,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PostProcessPipeline {
|
|
|
|
pub fn new(
|
|
|
|
device: &wgpu::Device,
|
|
|
|
vs_module: &wgpu::ShaderModule,
|
|
|
|
fs_module: &wgpu::ShaderModule,
|
|
|
|
sc_desc: &wgpu::SwapChainDescriptor,
|
|
|
|
global_layout: &GlobalsLayouts,
|
|
|
|
layout: &PostProcessLayout,
|
|
|
|
aa_mode: AaMode,
|
|
|
|
) -> Self {
|
2020-11-30 04:22:13 +00:00
|
|
|
common::span!(_guard, "PostProcessPipeline::new");
|
2020-09-13 10:03:42 +00:00
|
|
|
let render_pipeline_layout =
|
|
|
|
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
|
|
|
label: Some("Post process pipeline layout"),
|
|
|
|
push_constant_ranges: &[],
|
2020-12-05 09:31:35 +00:00
|
|
|
bind_group_layouts: &[&global_layout.globals, &layout.layout],
|
2020-09-13 10:03:42 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let samples = match aa_mode {
|
|
|
|
AaMode::None | AaMode::Fxaa => 1,
|
|
|
|
// TODO: Ensure sampling in the shader is exactly between the 4 texels
|
|
|
|
AaMode::MsaaX4 => 4,
|
|
|
|
AaMode::MsaaX8 => 8,
|
|
|
|
AaMode::MsaaX16 => 16,
|
|
|
|
};
|
|
|
|
|
|
|
|
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
|
|
|
label: Some("Post process pipeline"),
|
|
|
|
layout: Some(&render_pipeline_layout),
|
|
|
|
vertex_stage: wgpu::ProgrammableStageDescriptor {
|
|
|
|
module: vs_module,
|
|
|
|
entry_point: "main",
|
|
|
|
},
|
|
|
|
fragment_stage: Some(wgpu::ProgrammableStageDescriptor {
|
|
|
|
module: fs_module,
|
|
|
|
entry_point: "main",
|
|
|
|
}),
|
2020-12-06 02:23:50 +00:00
|
|
|
rasterization_state: None,
|
2020-09-13 10:03:42 +00:00
|
|
|
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
|
|
|
|
color_states: &[wgpu::ColorStateDescriptor {
|
|
|
|
format: sc_desc.format,
|
|
|
|
color_blend: wgpu::BlendDescriptor::REPLACE,
|
|
|
|
alpha_blend: wgpu::BlendDescriptor::REPLACE,
|
|
|
|
write_mask: wgpu::ColorWrite::ALL,
|
|
|
|
}],
|
2020-12-05 20:20:56 +00:00
|
|
|
depth_stencil_state: None,
|
2020-09-13 10:03:42 +00:00
|
|
|
vertex_state: wgpu::VertexStateDescriptor {
|
2020-12-05 12:50:06 +00:00
|
|
|
index_format: None,
|
2020-12-07 15:47:44 +00:00
|
|
|
vertex_buffers: &[],
|
2020-09-13 10:03:42 +00:00
|
|
|
},
|
|
|
|
sample_count: samples,
|
|
|
|
sample_mask: !0,
|
|
|
|
alpha_to_coverage_enabled: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
Self {
|
|
|
|
pipeline: render_pipeline,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|