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

199 lines
6.4 KiB
Rust
Raw Normal View History

use super::super::{Consts, GlobalsLayouts, PipelineModes};
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-12-05 09:31:35 +00:00
pub struct BindGroup {
pub(in super::super) bind_group: wgpu::BindGroup,
}
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, pipeline_modes: &PipelineModes) -> Self {
let mut bind_entries = vec![
// src color
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Float { filterable: true },
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::Sampler {
filtering: true,
comparison: false,
},
count: None,
},
// Locals
wgpu::BindGroupLayoutEntry {
binding: 2,
visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
];
if pipeline_modes.bloom.is_on() {
bind_entries.push(
// src bloom
wgpu::BindGroupLayoutEntry {
binding: 3,
visibility: wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Float { filterable: true },
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
count: None,
},
);
}
2020-09-13 10:03:42 +00:00
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: &bind_entries,
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,
src_bloom: Option<&wgpu::TextureView>,
2020-12-05 09:31:35 +00:00
sampler: &wgpu::Sampler,
locals: &Consts<Locals>,
) -> BindGroup {
let mut entries = vec![
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(),
},
];
// Optional bloom source
if let Some(src_bloom) = src_bloom {
entries.push(
2021-07-21 08:58:03 +00:00
// TODO: might be cheaper to premix bloom at lower resolution if we are doing
// extensive upscaling
// TODO: if there is no upscaling we can do the last bloom upsampling in post
// process to save a pass and the need for the final full size bloom render target
2020-12-05 09:31:35 +00:00
wgpu::BindGroupEntry {
2021-07-21 09:39:30 +00:00
binding: 3,
resource: wgpu::BindingResource::TextureView(src_bloom),
2020-12-05 09:31:35 +00:00
},
);
}
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &self.layout,
entries: &entries,
2020-12-05 09:31:35 +00:00
});
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,
) -> Self {
common_base::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 render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("Post process pipeline"),
layout: Some(&render_pipeline_layout),
vertex: wgpu::VertexState {
2020-09-13 10:03:42 +00:00
module: vs_module,
entry_point: "main",
buffers: &[],
},
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
strip_index_format: None,
front_face: wgpu::FrontFace::Ccw,
2021-02-28 04:09:30 +00:00
cull_mode: None,
clamp_depth: false,
polygon_mode: wgpu::PolygonMode::Fill,
2021-03-17 13:56:40 +00:00
conservative: false,
2020-09-13 10:03:42 +00:00
},
depth_stencil: None,
multisample: wgpu::MultisampleState {
count: 1,
mask: !0,
alpha_to_coverage_enabled: false,
},
fragment: Some(wgpu::FragmentState {
2020-09-13 10:03:42 +00:00
module: fs_module,
entry_point: "main",
targets: &[wgpu::ColorTargetState {
format: sc_desc.format,
2021-02-28 04:09:30 +00:00
blend: None,
write_mask: wgpu::ColorWrite::ALL,
}],
2020-09-13 10:03:42 +00:00
}),
});
Self {
pipeline: render_pipeline,
}
}
}