use super::history::CameraFrame;
use super::targets::{SharedTargets, Surface};
use crate::shading;
use glam::{Mat4, Vec4};
use sgl_post_fx::post_fx_context::{self, FrameDesc, PostFXContext};
use sgl_post_fx::screen_space_reflection::{self, FeatureFlags, ScreenSpaceReflection};
use sgl_post_fx::temporal_anti_aliasing::{self, TemporalAntiAliasing};
use sgl_post_fx::{CameraAttribs, ScreenSpaceReflectionAttribs, TemporalAntiAliasingAttribs};
pub(crate) const FAR_PLANE: f32 = 10_000.;
struct Inputs {
size: [u32; 2],
normal: wgpu::TextureView,
material: wgpu::TextureView,
motion: wgpu::TextureView,
depth: [wgpu::TextureView; 2],
}
impl Inputs {
fn new(device: &wgpu::Device, size: [u32; 2]) -> Self {
let texture = |label, format, usage| {
device.create_texture(&wgpu::TextureDescriptor {
label: Some(label),
size: wgpu::Extent3d {
width: size[0],
height: size[1],
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format,
usage: wgpu::TextureUsages::TEXTURE_BINDING | usage,
view_formats: &[],
})
};
let target = |label, format| {
texture(label, format, wgpu::TextureUsages::RENDER_ATTACHMENT)
.create_view(&Default::default())
};
Self {
size,
normal: target("DiligentFX input normal", NORMAL_FORMAT),
material: target("DiligentFX input material parameters", MATERIAL_FORMAT),
motion: target("DiligentFX input motion vectors", MOTION_FORMAT),
depth: std::array::from_fn(|_| {
target("DiligentFX input depth", wgpu::TextureFormat::Depth32Float)
}),
}
}
}
const NORMAL_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba16Float;
const MATERIAL_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::R32Float;
const MOTION_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rg16Float;
pub(crate) fn ssr_attribs() -> ScreenSpaceReflectionAttribs {
ScreenSpaceReflectionAttribs {
is_roughness_perceptual: 1,
roughness_channel: 0,
max_traversal_intersections: 64,
ggx_importance_sample_bias: 1.,
..Default::default()
}
}
pub(crate) const TAA_MIP_BIAS: f32 = -0.5;
const TAA_FEATURE_FLAGS: temporal_anti_aliasing::FeatureFlags =
temporal_anti_aliasing::FeatureFlags::BICUBIC_FILTER;
struct History {
frame_index: u32,
}
pub(crate) static INPUTS: shading::Module = shading::Module {
name: "post_fx_inputs",
source: include_str!("post_fx_inputs.wgsl"),
deps: &[&shading::GBUFFER, &shading::FULLSCREEN_VS],
};
pub(crate) const FS_MAIN_ENTRY: &str = "fs_main";
pub(crate) struct PostFx {
context: PostFXContext,
ssr: Option<ScreenSpaceReflection>,
taa: Option<TemporalAntiAliasing>,
history: Option<History>,
inputs: Option<Inputs>,
prepare: wgpu::RenderPipeline,
planes: wgpu::Buffer,
frame_index: u32,
jitter: [f32; 2],
reset_accumulation: bool,
}
fn left_handed(view: Mat4, projection: Mat4) -> (Mat4, Mat4) {
let mirror = Mat4::from_diagonal(Vec4::new(1., 1., -1., 1.));
(mirror * view, projection * mirror)
}
fn camera_attribs(
view: Mat4,
projection: Mat4,
jitter: [f32; 2],
size: [u32; 2],
frame_index: u32,
) -> CameraAttribs {
let far = FAR_PLANE;
let (view, mut projection) = left_handed(view, projection);
let near = projection.w_axis.z;
projection.z_axis.z = -near / (far - near);
projection.w_axis.z = near * far / (far - near);
let view_projection = projection * view;
let columns = Mat4::to_cols_array;
let mut attribs = CameraAttribs {
f4_position: view.inverse().w_axis.to_array(),
f4_viewport_size: [
size[0] as f32,
size[1] as f32,
1. / size[0] as f32,
1. / size[1] as f32,
],
f_handness: -1.,
ui_frame_index: frame_index,
f2_jitter: jitter,
m_view: columns(&view),
m_proj: columns(&projection),
m_view_proj: columns(&view_projection),
m_view_inv: columns(&view.inverse()),
m_proj_inv: columns(&projection.inverse()),
m_view_proj_inv: columns(&view_projection.inverse()),
..Default::default()
};
attribs.set_clip_planes(far, near);
attribs
}
fn pass_group(pass: &str) -> &'static str {
match pass {
"ComputeBlueNoiseTexture" => "DiligentFX blue noise",
"ComputeReprojectedDepth" => "DiligentFX reprojected depth",
"ComputePreviousDepth" => "DiligentFX previous depth",
"ComputeHierarchicalDepthBuffer" => "SSR depth hierarchy",
"ComputeStencilMaskAndExtractRoughness" => "SSR mask and roughness",
"ComputeDownsampledStencilMask" => "SSR half-resolution mask",
"ComputeIntersection" => "SSR intersection",
"SpatialReconstruction" => "SSR spatial reconstruction",
"ComputeTemporalAccumulation" => "SSR temporal accumulation",
"ComputeBilateralCleanup" => "SSR bilateral cleanup",
"TemporalAccumulation" => "TAA",
_ => "DiligentFX",
}
}
impl PostFx {
pub fn new(device: &wgpu::Device, queue: &wgpu::Queue, taa: bool) -> Self {
let module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("DiligentFX inputs"),
source: wgpu::ShaderSource::Wgsl(shading::compose(&[&INPUTS]).into()),
});
let target = |format| {
Some(wgpu::ColorTargetState {
format,
blend: None,
write_mask: wgpu::ColorWrites::ALL,
})
};
Self {
context: PostFXContext::new(
device,
queue,
post_fx_context::CreateInfo {
transition_duration: 0.,
},
),
ssr: None,
taa: taa.then(|| TemporalAntiAliasing::new(device)),
history: None,
inputs: None,
prepare: device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("DiligentFX inputs from the G-buffer"),
layout: None,
vertex: wgpu::VertexState {
module: &module,
entry_point: Some(shading::FULLSCREEN_VS_ENTRY),
compilation_options: Default::default(),
buffers: &[],
},
fragment: Some(wgpu::FragmentState {
module: &module,
entry_point: Some(FS_MAIN_ENTRY),
compilation_options: Default::default(),
targets: &[
target(NORMAL_FORMAT),
target(MATERIAL_FORMAT),
target(MOTION_FORMAT),
],
}),
primitive: Default::default(),
depth_stencil: Some(wgpu::DepthStencilState {
format: wgpu::TextureFormat::Depth32Float,
depth_write_enabled: Some(true),
depth_compare: Some(wgpu::CompareFunction::Always),
stencil: Default::default(),
bias: Default::default(),
}),
multisample: Default::default(),
multiview_mask: None,
cache: None,
}),
planes: crate::counters::buffer(
device,
&wgpu::BufferDescriptor {
label: Some("DiligentFX input planes"),
size: 16,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
},
),
frame_index: 0,
jitter: [0.; 2],
reset_accumulation: true,
}
}
pub fn taa(&self) -> bool {
self.taa.is_some()
}
pub fn release_screen_space_reflections(&mut self) {
self.ssr = None;
}
pub fn prepare(
&mut self,
device: &wgpu::Device,
encoder: &mut wgpu::CommandEncoder,
size: [u32; 2],
frame_index: u32,
history_valid: bool,
) -> [f32; 2] {
self.reset_accumulation = !history_valid
|| self
.history
.as_ref()
.is_none_or(|history| frame_index != history.frame_index.wrapping_add(1));
self.frame_index = frame_index;
self.context.prepare_resources(
device,
&FrameDesc {
index: frame_index,
width: size[0],
height: size[1],
output_width: size[0],
output_height: size[1],
},
post_fx_context::FeatureFlags::REVERSED_DEPTH,
);
self.jitter = match &mut self.taa {
Some(taa) => {
taa.prepare_resources(device, encoder, &self.context, TAA_FEATURE_FLAGS, 0);
if self.reset_accumulation {
[0.; 2]
} else {
taa.get_jitter_offset(0)
}
}
None => [0.; 2],
};
self.jitter
}
pub fn set_jitter(&mut self, jitter: [f32; 2]) {
self.jitter = jitter;
}
#[allow(clippy::too_many_arguments)]
pub fn begin(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
encoder: &mut wgpu::CommandEncoder,
t: &SharedTargets,
surface: Surface<'_>,
size: [u32; 2],
view: Mat4,
projection: Mat4,
previous: Option<CameraFrame>,
timing: Option<&crate::timing::GpuTiming>,
) {
if self
.inputs
.as_ref()
.is_none_or(|inputs| inputs.size != size)
{
self.inputs = Some(Inputs::new(device, size));
}
let inputs = self.inputs.as_ref().unwrap();
let near = projection.w_axis.z;
crate::counters::write_buffer(
queue,
&self.planes,
0,
bytemuck::cast_slice(&[near, FAR_PLANE, 0., 0.]),
);
let resource = |binding, view| wgpu::BindGroupEntry {
binding,
resource: wgpu::BindingResource::TextureView(view),
};
{
let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("DiligentFX inputs"),
layout: &self.prepare.get_bind_group_layout(0),
entries: &[
resource(0, &t.normal),
resource(1, &t.material),
resource(2, &t.f0),
resource(3, &t.motion),
resource(4, surface.depth),
wgpu::BindGroupEntry {
binding: 5,
resource: self.planes.as_entire_binding(),
},
resource(6, &t.depth),
resource(7, surface.receivers),
],
});
let attachment = |view| {
Some(wgpu::RenderPassColorAttachment {
view,
depth_slice: None,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
store: wgpu::StoreOp::Store,
},
})
};
let depth = &inputs.depth[(self.frame_index & 1) as usize];
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("DiligentFX inputs"),
color_attachments: &[
attachment(&inputs.normal),
attachment(&inputs.material),
attachment(&inputs.motion),
],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: depth,
depth_ops: Some(wgpu::Operations {
load: wgpu::LoadOp::Clear(0.),
store: wgpu::StoreOp::Store,
}),
stencil_ops: None,
}),
timestamp_writes: timing.and_then(|t| t.render_pass("DiligentFX inputs")),
..Default::default()
});
pass.set_pipeline(&self.prepare);
pass.set_bind_group(0, &group, &[]);
pass.draw(0..3, 0..1);
}
let depth_view = &inputs.depth[(self.frame_index & 1) as usize];
let previous_depth = &inputs.depth[(self.frame_index.wrapping_add(1) & 1) as usize];
let camera = camera_attribs(view, projection, self.jitter, size, self.frame_index);
let reset_accumulation = self.reset_accumulation;
if reset_accumulation {
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("DiligentFX previous depth reset"),
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: previous_depth,
depth_ops: Some(wgpu::Operations {
load: wgpu::LoadOp::Clear(0.),
store: wgpu::StoreOp::Store,
}),
stencil_ops: None,
}),
..Default::default()
});
}
let previous_camera = match (&self.history, previous) {
(Some(history), Some(previous)) if !reset_accumulation => camera_attribs(
previous.view,
previous.jittered_projection(),
previous.jitter,
size,
history.frame_index,
),
_ => camera,
};
let timestamps =
|pass: &'static str| timing.and_then(|timing| timing.render_pass(pass_group(pass)));
self.context
.execute(&mut post_fx_context::RenderAttributes {
device,
queue,
device_context: encoder,
curr_depth_buffer_srv: depth_view,
prev_depth_buffer_srv: previous_depth,
curr_camera: Some(&camera),
prev_camera: Some(&previous_camera),
camera_attribs_cb: None,
pass_timestamps: Some(×tamps),
});
self.history = Some(History {
frame_index: self.frame_index,
});
}
#[allow(clippy::too_many_arguments)]
pub fn screen_space_reflections(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
encoder: &mut wgpu::CommandEncoder,
frame_time: f32,
half_resolution: bool,
radiance: &wgpu::TextureView,
timing: Option<&crate::timing::GpuTiming>,
) -> &wgpu::TextureView {
let inputs = self.inputs.as_ref().expect("PostFx::begin");
let feature_flags = if half_resolution {
FeatureFlags::HALF_RESOLUTION
} else {
FeatureFlags::NONE
};
let ssr = self
.ssr
.get_or_insert_with(|| ScreenSpaceReflection::new(device));
ssr.prepare_resources(device, encoder, &mut self.context, feature_flags);
let depth_view = &inputs.depth[(self.frame_index & 1) as usize];
let timestamps =
|pass: &'static str| timing.and_then(|timing| timing.render_pass(pass_group(pass)));
ssr.execute(&mut screen_space_reflection::RenderAttributes {
device,
queue,
device_context: encoder,
post_fx_context: &mut self.context,
color_buffer_srv: radiance,
depth_buffer_srv: depth_view,
normal_buffer_srv: &inputs.normal,
material_buffer_srv: &inputs.material,
motion_vectors_srv: &inputs.motion,
ssr_attribs: &ssr_attribs(),
pass_timestamps: Some(×tamps),
reset_accumulation: self.reset_accumulation,
frame_time,
});
ssr.get_ssr_radiance_srv()
}
pub fn taa_output(&self) -> &wgpu::TextureView {
self.taa
.as_ref()
.expect("PostFx::new with TAA")
.get_accumulated_frame_srv(false, 0)
}
pub fn temporal_anti_aliasing(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
encoder: &mut wgpu::CommandEncoder,
color: &wgpu::TextureView,
timing: Option<&crate::timing::GpuTiming>,
) -> &wgpu::TextureView {
let taa = self.taa.as_mut().expect("PostFx::new with TAA");
let inputs = self.inputs.as_ref().expect("PostFx::begin");
let timestamps =
|pass: &'static str| timing.and_then(|timing| timing.render_pass(pass_group(pass)));
taa.execute(&mut temporal_anti_aliasing::RenderAttributes {
device,
queue,
device_context: encoder,
post_fx_context: &mut self.context,
color_buffer_srv: color,
depth_buffer_srv: &inputs.depth[(self.frame_index & 1) as usize],
motion_vectors_srv: &inputs.motion,
taa_attribs: &TemporalAntiAliasingAttribs {
reset_accumulation: u32::from(self.reset_accumulation),
..Default::default()
},
accumulation_buffer_idx: 0,
pass_timestamps: Some(×tamps),
});
taa.get_accumulated_frame_srv(false, 0)
}
}