veloren/voxygen/src/render/pipelines/postprocess.rs

206 lines
7.1 KiB
Rust
Raw Normal View History

2020-09-13 10:03:42 +00:00
use super::super::{AaMode, GlobalsLayouts, Mesh, Tri};
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)]
#[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],
}
2020-10-21 21:05:25 +00:00
impl Default for Locals {
fn default() -> Self { Self::new(Mat4::identity(), Mat4::identity()) }
}
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(),
}
}
}
2020-08-24 21:17:40 +00:00
#[repr(C)]
2020-09-26 15:43:59 +00:00
#[derive(Copy, Clone, Debug, Zeroable, Pod)]
2020-08-24 21:17:40 +00:00
pub struct Vertex {
pub pos: [f32; 2],
}
2020-09-13 10:03:42 +00:00
impl Vertex {
fn desc<'a>() -> wgpu::VertexBufferDescriptor<'a> {
use std::mem;
const ATTRIBUTES: [wgpu::VertexAttributeDescriptor; 1] =
wgpu::vertex_attr_array![0 => Float2];
2020-09-13 10:03:42 +00:00
wgpu::VertexBufferDescriptor {
stride: mem::size_of::<Self>() as wgpu::BufferAddress,
step_mode: wgpu::InputStepMode::Vertex,
attributes: &ATTRIBUTES,
2020-09-13 10:03:42 +00:00
}
}
}
2020-08-24 21:17:40 +00:00
pub fn create_mesh() -> Mesh<Vertex> {
let mut mesh = Mesh::new();
#[rustfmt::skip]
mesh.push_tri(Tri::new(
Vertex { pos: [ 1.0, -1.0] },
Vertex { pos: [-1.0, 1.0] },
Vertex { pos: [-1.0, -1.0] },
));
#[rustfmt::skip]
mesh.push_tri(Tri::new(
Vertex { pos: [1.0, -1.0] },
Vertex { pos: [1.0, 1.0] },
Vertex { pos: [-1.0, 1.0] },
));
mesh
}
2020-09-13 10:03:42 +00:00
pub struct PostProcessLayout {
pub src_color: wgpu::BindGroupLayout,
}
impl PostProcessLayout {
pub fn new(device: &wgpu::Device) -> Self {
Self {
src_color: device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: None,
entries: &[
// src color
2020-09-13 10:03:42 +00:00
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::SampledTexture {
component_type: wgpu::TextureComponentType::Float,
dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::Sampler { comparison: false },
count: None,
},
// src depth
wgpu::BindGroupLayoutEntry {
binding: 2,
visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::SampledTexture {
component_type: wgpu::TextureComponentType::Float,
dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 3,
visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::Sampler { comparison: false },
count: None,
},
// Locals
wgpu::BindGroupLayoutEntry {
binding: 4,
visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::UniformBuffer {
dynamic: false,
min_binding_size: None,
},
count: None,
},
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 {
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: &[],
bind_group_layouts: &[&global_layout.globals, &layout.src_color],
});
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",
}),
rasterization_state: Some(wgpu::RasterizationStateDescriptor {
front_face: wgpu::FrontFace::Ccw,
cull_mode: wgpu::CullMode::Back,
polygon_mode: wgpu::PolygonMode::Fill,
2020-09-13 10:03:42 +00:00
clamp_depth: false,
depth_bias: 0,
depth_bias_slope_scale: 0.0,
depth_bias_clamp: 0.0,
}),
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,
}],
depth_stencil_state: Some(wgpu::DepthStencilStateDescriptor {
format: wgpu::TextureFormat::Depth24Plus,
depth_write_enabled: false,
depth_compare: wgpu::CompareFunction::Always,
stencil: wgpu::StencilStateDescriptor {
front: wgpu::StencilStateFaceDescriptor::IGNORE,
back: wgpu::StencilStateFaceDescriptor::IGNORE,
read_mask: !0,
write_mask: !0,
},
}),
vertex_state: wgpu::VertexStateDescriptor {
index_format: wgpu::IndexFormat::Uint16,
vertex_buffers: &[Vertex::desc()],
},
sample_count: samples,
sample_mask: !0,
alpha_to_coverage_enabled: false,
});
Self {
pipeline: render_pipeline,
}
}
}