use crate::post_fx_context::{self, BlueNoiseDimension, PostFXContext, TextureOperationAttribs};
use crate::render_technique::{
DepthStencilStateDesc, PostFXRenderTechnique, Resource, create_shader,
};
use crate::structures::ScreenSpaceReflectionAttribs;
use std::collections::HashMap;
pub const SSR_DEPTH_HIERARCHY_MAX_MIP: u32 = 6;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct FeatureFlags(pub u32);
impl FeatureFlags {
pub const NONE: Self = Self(0);
pub const PREVIOUS_FRAME: Self = Self(1 << 0);
pub const HALF_RESOLUTION: Self = Self(1 << 1);
pub fn contains(self, flag: Self) -> bool {
self.0 & flag.0 != 0
}
}
impl std::ops::BitOr for FeatureFlags {
type Output = Self;
fn bitor(self, rhs: Self) -> Self {
Self(self.0 | rhs.0)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PostFxExecutionStatus {
Ready,
Pending,
}
pub struct RenderAttributes<'a, 'p> {
pub device: &'a wgpu::Device,
pub queue: &'a wgpu::Queue,
pub device_context: &'a mut wgpu::CommandEncoder,
pub post_fx_context: &'a mut PostFXContext,
pub color_buffer_srv: &'a wgpu::TextureView,
pub depth_buffer_srv: &'a wgpu::TextureView,
pub normal_buffer_srv: &'a wgpu::TextureView,
pub material_buffer_srv: &'a wgpu::TextureView,
pub motion_vectors_srv: &'a wgpu::TextureView,
pub ssr_attribs: &'a ScreenSpaceReflectionAttribs,
pub pass_timestamps: Option<&'a post_fx_context::PassTimestamps<'p>>,
pub reset_accumulation: bool,
pub frame_time: f32,
}
#[allow(clippy::enum_variant_names)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
enum RenderTech {
ComputeHierarchicalDepthBuffer,
ComputeStencilMaskAndExtractRoughness,
ComputeDownsampledStencilMask,
ComputeIntersection,
ClassifyDenoiserTiles,
DilateDenoiserTiles,
ComputeSpatialReconstruction,
ComputeTemporalAccumulation,
ComputeBilateralCleanup,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
struct RenderTechniqueKey {
render_tech: RenderTech,
feature_flags: FeatureFlags,
use_reverse_depth: bool,
}
const DEPTH_HIERARCHY_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::R32Float;
const ROUGHNESS_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::R8Unorm;
const DENOISER_TILE_SIZE: u32 = 8;
const DENOISER_BLOCK_SIZE: u32 = DENOISER_TILE_SIZE / 2;
const DENOISER_TILE_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::R8Unorm;
const DEPTH_STENCIL_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth16Unorm;
const RADIANCE_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba16Float;
const VARIANCE_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::R16Float;
struct Resources {
depth_hierarchy: wgpu::TextureView,
depth_stencil_mask: wgpu::TextureView,
depth_stencil_mask_half_res: Option<wgpu::TextureView>,
roughness: wgpu::TextureView,
radiance: wgpu::TextureView,
ray_direction_pdf: wgpu::TextureView,
denoiser_hits: wgpu::TextureView,
denoiser_tiles: wgpu::TextureView,
resolved_radiance: wgpu::TextureView,
resolved_variance: wgpu::TextureView,
resolved_depth: wgpu::TextureView,
radiance_history: [wgpu::TextureView; 2],
variance_history: [wgpu::TextureView; 2],
output: wgpu::TextureView,
}
pub struct ScreenSpaceReflection {
render_tech: HashMap<RenderTechniqueKey, PostFXRenderTechnique>,
ssr_attribs: ScreenSpaceReflectionAttribs,
constant_buffer: wgpu::Buffer,
resources: Option<Resources>,
hierarchical_depth_mip_map_rtv: Vec<wgpu::TextureView>,
hierarchical_depth_mip_map_srv: Vec<wgpu::TextureView>,
back_buffer_width: u32,
back_buffer_height: u32,
feature_flags: FeatureFlags,
use_reverse_depth: bool,
frame_timer: Option<f32>,
linear_clamp: wgpu::Sampler,
last_frame_idx: Option<u32>,
history_reset: bool,
history_reset_buffer: wgpu::Buffer,
}
fn compute_mip_levels_count(width: u32, height: u32) -> u32 {
32 - width.max(height).leading_zeros()
}
fn texture_2d(
device: &wgpu::Device,
name: &str,
width: u32,
height: u32,
format: wgpu::TextureFormat,
mip_levels: u32,
) -> wgpu::Texture {
device.create_texture(&wgpu::TextureDescriptor {
label: Some(name),
size: wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
},
mip_level_count: mip_levels,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format,
usage: wgpu::TextureUsages::TEXTURE_BINDING
| wgpu::TextureUsages::RENDER_ATTACHMENT
| wgpu::TextureUsages::COPY_SRC,
view_formats: &[],
})
}
fn view(texture: &wgpu::Texture) -> wgpu::TextureView {
texture.create_view(&Default::default())
}
fn texture(view: &wgpu::TextureView) -> wgpu::BindingResource<'_> {
wgpu::BindingResource::TextureView(view)
}
enum DepthStencil<'a> {
Clear(&'a wgpu::TextureView),
ReadOnly(&'a wgpu::TextureView),
}
#[allow(clippy::too_many_arguments)]
fn draw_pass(
device_context: &mut wgpu::CommandEncoder,
label: &str,
rtvs: &[&wgpu::TextureView],
clear: Option<&[wgpu::Color]>,
dsv: Option<DepthStencil<'_>>,
tech: &PostFXRenderTechnique,
group: &wgpu::BindGroup,
timestamp_writes: Option<wgpu::RenderPassTimestampWrites<'_>>,
) {
let attachments: Vec<_> = rtvs
.iter()
.enumerate()
.map(|(index, view)| {
Some(wgpu::RenderPassColorAttachment {
view,
depth_slice: None,
resolve_target: None,
ops: wgpu::Operations {
load: clear.map_or(wgpu::LoadOp::Load, |colors| {
wgpu::LoadOp::Clear(colors[index])
}),
store: wgpu::StoreOp::Store,
},
})
})
.collect();
let mut pass = device_context.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some(label),
color_attachments: &attachments,
depth_stencil_attachment: dsv.map(|dsv| match dsv {
DepthStencil::Clear(view) => wgpu::RenderPassDepthStencilAttachment {
view,
depth_ops: Some(wgpu::Operations {
load: wgpu::LoadOp::Clear(0.0),
store: wgpu::StoreOp::Store,
}),
stencil_ops: None,
},
DepthStencil::ReadOnly(view) => wgpu::RenderPassDepthStencilAttachment {
view,
depth_ops: None,
stencil_ops: None,
},
}),
timestamp_writes,
..Default::default()
});
pass.set_pipeline(tech.pso.as_ref().expect("initialized technique"));
pass.set_bind_group(0, group, &[]);
pass.draw(0..3, 0..1);
}
impl ScreenSpaceReflection {
pub fn new(device: &wgpu::Device) -> Self {
let ssr_attribs = ScreenSpaceReflectionAttribs::default();
let constant_buffer = {
use wgpu::util::DeviceExt;
device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("ScreenSpaceReflection::ConstantBuffer"),
contents: bytemuck::bytes_of(&ssr_attribs),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
})
};
Self {
render_tech: HashMap::new(),
ssr_attribs,
constant_buffer,
resources: None,
hierarchical_depth_mip_map_rtv: Vec::new(),
hierarchical_depth_mip_map_srv: Vec::new(),
back_buffer_width: 0,
back_buffer_height: 0,
feature_flags: FeatureFlags::NONE,
use_reverse_depth: false,
frame_timer: None,
last_frame_idx: None,
history_reset: false,
history_reset_buffer: device.create_buffer(&wgpu::BufferDescriptor {
label: Some("ScreenSpaceReflection::HistoryReset"),
size: 16,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
}),
linear_clamp: device.create_sampler(&wgpu::SamplerDescriptor {
label: Some("Sam_LinearClamp"),
mag_filter: wgpu::FilterMode::Linear,
min_filter: wgpu::FilterMode::Linear,
mipmap_filter: wgpu::MipmapFilterMode::Linear,
..Default::default()
}),
}
}
pub fn prepare_resources(
&mut self,
device: &wgpu::Device,
device_context: &mut wgpu::CommandEncoder,
post_fx_context: &mut PostFXContext,
feature_flags: FeatureFlags,
) {
let frame_desc = *post_fx_context.get_frame_desc();
let use_reverse_depth = post_fx_context
.get_feature_flags()
.contains(post_fx_context::FeatureFlags::REVERSED_DEPTH);
if self.feature_flags != feature_flags || self.use_reverse_depth != use_reverse_depth {
if self.feature_flags.contains(FeatureFlags::HALF_RESOLUTION)
!= feature_flags.contains(FeatureFlags::HALF_RESOLUTION)
|| self.use_reverse_depth != use_reverse_depth
{
self.back_buffer_width = 0;
self.back_buffer_height = 0;
}
self.feature_flags = feature_flags;
self.use_reverse_depth = use_reverse_depth;
}
if self.back_buffer_width == frame_desc.width
&& self.back_buffer_height == frame_desc.height
{
return;
}
self.back_buffer_width = frame_desc.width;
self.back_buffer_height = frame_desc.height;
self.last_frame_idx = None;
let (width, height) = (self.back_buffer_width, self.back_buffer_height);
let half_res = feature_flags.contains(FeatureFlags::HALF_RESOLUTION);
let (trace_width, trace_height) = if half_res {
((width / 2).max(1), (height / 2).max(1))
} else {
(width, height)
};
let depth_hierarchy_mip_count = SSR_DEPTH_HIERARCHY_MAX_MIP + 1;
let depth_hierarchy = texture_2d(
device,
"ScreenSpaceReflection::DepthHierarchy",
width,
height,
DEPTH_HIERARCHY_FORMAT,
compute_mip_levels_count(width, height).min(depth_hierarchy_mip_count),
);
let mip_view = |mip_level| {
depth_hierarchy.create_view(&wgpu::TextureViewDescriptor {
base_mip_level: mip_level,
mip_level_count: Some(1),
..Default::default()
})
};
self.hierarchical_depth_mip_map_rtv = (0..depth_hierarchy.mip_level_count())
.map(mip_view)
.collect();
self.hierarchical_depth_mip_map_srv = (0..depth_hierarchy.mip_level_count())
.map(mip_view)
.collect();
let mut history = |name| {
let history_texture =
view(&texture_2d(device, name, width, height, RADIANCE_FORMAT, 1));
post_fx_context.clear_render_target(
&mut TextureOperationAttribs {
device,
device_context,
timestamp_writes: None,
},
&history_texture,
[0.0, 0.0, 0.0, 0.0],
);
history_texture
};
let radiance_history = [
history("ScreenSpaceReflection::RadianceHistory"),
history("ScreenSpaceReflection::RadianceHistory"),
];
let variance_history = std::array::from_fn(|_| {
let history_texture = view(&texture_2d(
device,
"ScreenSpaceReflection::VarianceHistory",
width,
height,
VARIANCE_FORMAT,
1,
));
post_fx_context.clear_render_target(
&mut TextureOperationAttribs {
device,
device_context,
timestamp_writes: None,
},
&history_texture,
[0.0, 0.0, 0.0, 0.0],
);
history_texture
});
let output = view(&texture_2d(
device,
"ScreenSpaceReflection::Output",
width,
height,
RADIANCE_FORMAT,
1,
));
post_fx_context.clear_render_target(
&mut TextureOperationAttribs {
device,
device_context,
timestamp_writes: None,
},
&output,
[0.0, 0.0, 0.0, 0.0],
);
self.resources = Some(Resources {
depth_hierarchy: view(&depth_hierarchy),
roughness: view(&texture_2d(
device,
"ScreenSpaceReflection::Roughness",
width,
height,
ROUGHNESS_FORMAT,
1,
)),
depth_stencil_mask: view(&texture_2d(
device,
"ScreenSpaceReflection::DepthStencilMask",
width,
height,
DEPTH_STENCIL_FORMAT,
1,
)),
depth_stencil_mask_half_res: half_res.then(|| {
view(&texture_2d(
device,
"ScreenSpaceReflection::DepthStencilMaskHalfRes",
trace_width,
trace_height,
DEPTH_STENCIL_FORMAT,
1,
))
}),
radiance: view(&texture_2d(
device,
"ScreenSpaceReflection::Radiance",
trace_width,
trace_height,
RADIANCE_FORMAT,
1,
)),
ray_direction_pdf: view(&texture_2d(
device,
"ScreenSpaceReflection::RayDirectionPDF",
trace_width,
trace_height,
RADIANCE_FORMAT,
1,
)),
denoiser_hits: view(&texture_2d(
device,
"ScreenSpaceReflection::DenoiserHits",
width.div_ceil(DENOISER_BLOCK_SIZE),
height.div_ceil(DENOISER_BLOCK_SIZE),
DENOISER_TILE_FORMAT,
1,
)),
denoiser_tiles: view(&texture_2d(
device,
"ScreenSpaceReflection::DenoiserTiles",
width.div_ceil(DENOISER_TILE_SIZE),
height.div_ceil(DENOISER_TILE_SIZE),
DENOISER_TILE_FORMAT,
1,
)),
resolved_radiance: view(&texture_2d(
device,
"ScreenSpaceReflection::ResolvedRadiance",
width,
height,
RADIANCE_FORMAT,
1,
)),
resolved_variance: view(&texture_2d(
device,
"ScreenSpaceReflection::ResolvedVariance",
width,
height,
VARIANCE_FORMAT,
1,
)),
resolved_depth: view(&texture_2d(
device,
"ScreenSpaceReflection::ResolvedDepth",
width,
height,
self.resolved_depth_format(),
1,
)),
radiance_history,
variance_history,
output,
});
}
pub fn execute(
&mut self,
render_attribs: &mut RenderAttributes<'_, '_>,
) -> PostFxExecutionStatus {
let all_psos_ready = self.prepare_shaders_and_pso(render_attribs.device)
&& render_attribs.post_fx_context.is_psos_ready();
self.update_constant_buffer(render_attribs, !all_psos_ready);
self.reset_history(render_attribs);
if all_psos_ready {
self.compute_hierarchical_depth_buffer(render_attribs);
self.compute_stencil_mask_and_extract_roughness(render_attribs);
self.compute_downsampled_stencil_mask(render_attribs);
self.compute_intersection(render_attribs);
self.classify_denoiser_tiles(render_attribs);
self.compute_spatial_reconstruction(render_attribs);
self.compute_temporal_accumulation(render_attribs);
self.compute_bilateral_cleanup(render_attribs);
} else {
self.compute_placeholder_texture(render_attribs);
}
if all_psos_ready {
PostFxExecutionStatus::Ready
} else {
PostFxExecutionStatus::Pending
}
}
pub fn get_ssr_radiance_srv(&self) -> &wgpu::TextureView {
&self.resources().output
}
fn resources(&self) -> &Resources {
self.resources
.as_ref()
.expect("ScreenSpaceReflection::PrepareResources")
}
fn resolved_depth_format(&self) -> wgpu::TextureFormat {
if self.use_reverse_depth {
wgpu::TextureFormat::R16Float
} else {
wgpu::TextureFormat::R32Float
}
}
fn render_technique(&mut self, render_tech: RenderTech) -> &mut PostFXRenderTechnique {
self.render_tech
.entry(RenderTechniqueKey {
render_tech,
feature_flags: self.feature_flags,
use_reverse_depth: self.use_reverse_depth,
})
.or_default()
}
fn technique(&self, render_tech: RenderTech) -> &PostFXRenderTechnique {
&self.render_tech[&RenderTechniqueKey {
render_tech,
feature_flags: self.feature_flags,
use_reverse_depth: self.use_reverse_depth,
}]
}
fn prepare_shaders_and_pso(&mut self, device: &wgpu::Device) -> bool {
let flag = |set: bool| if set { "1" } else { "0" };
let tile_size = DENOISER_TILE_SIZE.to_string();
let macros = [
("SUPPORTED_SHADER_SRV", "1"),
("SSR_OPTION_INVERTED_DEPTH", flag(self.use_reverse_depth)),
(
"SSR_OPTION_PREVIOUS_FRAME",
flag(self.feature_flags.contains(FeatureFlags::PREVIOUS_FRAME)),
),
(
"SSR_OPTION_HALF_RESOLUTION",
flag(self.feature_flags.contains(FeatureFlags::HALF_RESOLUTION)),
),
("SSR_DENOISER_TILE_SIZE", tile_size.as_str()),
];
let previous_frame = self.feature_flags.contains(FeatureFlags::PREVIOUS_FRAME);
let triangle_depth_05 = [("TRIANGLE_DEPTH", "0.5")];
let triangle_depth_10 = [("TRIANGLE_DEPTH", "1.0")];
let vs = |macros: &[(&str, &str)]| {
create_shader(
device,
"FullScreenTriangleVS.fx",
"FullScreenTriangleVS",
macros,
)
};
let ps = |file: &str, entry: &'static str| create_shader(device, file, entry, ¯os);
{
let tech = self.render_technique(RenderTech::ComputeHierarchicalDepthBuffer);
if !tech.is_initialized_pso() {
tech.initialize_pso(
device,
"ScreenSpaceReflection::ComputeHierarchicalDepthBuffer",
&vs(&[]),
&ps(
"SSR_ComputeHierarchicalDepthBuffer.fx",
"ComputeHierarchicalDepthBufferPS",
),
&[(0, Resource::Texture)],
&[DEPTH_HIERARCHY_FORMAT],
None,
DepthStencilStateDesc::DisableDepth,
);
}
}
{
let tech = self.render_technique(RenderTech::ComputeStencilMaskAndExtractRoughness);
if !tech.is_initialized_pso() {
tech.initialize_pso(
device,
"ScreenSpaceReflection::ComputeStencilMaskAndExtractRoughness",
&vs(&triangle_depth_10),
&ps(
"SSR_ComputeStencilMaskAndExtractRoughness.fx",
"ComputeStencilMaskAndExtractRoughnessPS",
),
&[
(0, Resource::ConstantBuffer),
(1, Resource::Texture),
(2, Resource::DepthTexture),
],
&[ROUGHNESS_FORMAT],
Some(DEPTH_STENCIL_FORMAT),
DepthStencilStateDesc::WriteAlways,
);
}
}
{
let tech = self.render_technique(RenderTech::ComputeDownsampledStencilMask);
if !tech.is_initialized_pso() {
tech.initialize_pso(
device,
"ScreenSpaceReflection::ComputeDownsampledStencilMask",
&vs(&triangle_depth_10),
&ps(
"SSR_ComputeDownsampledStencilMask.fx",
"ComputeDownsampledStencilMaskPS",
),
&[
(0, Resource::ConstantBuffer),
(1, Resource::Texture),
(2, Resource::DepthTexture),
],
&[],
Some(DEPTH_STENCIL_FORMAT),
DepthStencilStateDesc::WriteAlways,
);
}
}
{
let mut layout = vec![
(0, Resource::ConstantBuffer),
(1, Resource::ConstantBuffer),
(2, Resource::Texture),
(3, Resource::Texture),
(4, Resource::Texture),
(6, Resource::Texture),
(7, Resource::Texture),
];
if previous_frame {
layout.push((5, Resource::Texture));
}
let tech = self.render_technique(RenderTech::ComputeIntersection);
if !tech.is_initialized_pso() {
tech.initialize_pso(
device,
"ScreenSpaceReflection::ComputeIntersection",
&vs(&triangle_depth_05),
&ps("SSR_ComputeIntersection.fx", "ComputeIntersectionPS"),
&layout,
&[RADIANCE_FORMAT, RADIANCE_FORMAT],
Some(DEPTH_STENCIL_FORMAT),
DepthStencilStateDesc::EnableDepthNoWrites,
);
}
}
{
let classify: &[_] = &[
(1, Resource::FilterableTexture),
(3, Resource::Sampler { filtering: true }),
];
let dilate: &[_] = &[
(0, Resource::ConstantBuffer),
(2, Resource::FilterableTexture),
(3, Resource::Sampler { filtering: true }),
];
for (render_tech, entry, layout) in [
(
RenderTech::ClassifyDenoiserTiles,
"ClassifyDenoiserTilesPS",
classify,
),
(
RenderTech::DilateDenoiserTiles,
"DilateDenoiserTilesPS",
dilate,
),
] {
let tech = self.render_technique(render_tech);
if !tech.is_initialized_pso() {
tech.initialize_pso(
device,
"ScreenSpaceReflection::DenoiserTiles",
&vs(&[]),
&ps("SSR_ComputeDenoiserTiles.fx", entry),
layout,
&[DENOISER_TILE_FORMAT],
None,
DepthStencilStateDesc::DisableDepth,
);
}
}
}
{
let resolved_depth_format = self.resolved_depth_format();
let tech = self.render_technique(RenderTech::ComputeSpatialReconstruction);
if !tech.is_initialized_pso() {
tech.initialize_pso(
device,
"ScreenSpaceReflection::ComputeSpatialReconstruction",
&vs(&triangle_depth_05),
&ps(
"SSR_ComputeSpatialReconstruction.fx",
"ComputeSpatialReconstructionPS",
),
&[
(0, Resource::ConstantBuffer),
(1, Resource::ConstantBuffer),
(2, Resource::Texture),
(3, Resource::Texture),
(4, Resource::DepthTexture),
(5, Resource::Texture),
(6, Resource::Texture),
(7, Resource::Texture),
],
&[RADIANCE_FORMAT, VARIANCE_FORMAT, resolved_depth_format],
Some(DEPTH_STENCIL_FORMAT),
DepthStencilStateDesc::EnableDepthNoWrites,
);
}
}
{
let tech = self.render_technique(RenderTech::ComputeTemporalAccumulation);
if !tech.is_initialized_pso() {
tech.initialize_pso(
device,
"ScreenSpaceReflection::ComputeTemporalAccumulation",
&vs(&triangle_depth_05),
&ps(
"SSR_ComputeTemporalAccumulation.fx",
"ComputeTemporalAccumulationPS",
),
&[
(0, Resource::ConstantBuffer),
(1, Resource::ConstantBuffer),
(2, Resource::Texture),
(3, Resource::Texture),
(4, Resource::Texture),
(5, Resource::Texture),
(6, Resource::Texture),
(7, Resource::Texture),
(8, Resource::FilterableTexture),
(9, Resource::FilterableTexture),
(10, Resource::Sampler { filtering: true }),
(11, Resource::Sampler { filtering: true }),
(12, Resource::Texture),
(13, Resource::ConstantBuffer),
],
&[RADIANCE_FORMAT, VARIANCE_FORMAT],
Some(DEPTH_STENCIL_FORMAT),
DepthStencilStateDesc::EnableDepthNoWrites,
);
}
}
{
let tech = self.render_technique(RenderTech::ComputeBilateralCleanup);
if !tech.is_initialized_pso() {
tech.initialize_pso(
device,
"ScreenSpaceReflection::ComputeBilateralCleanup",
&vs(&triangle_depth_05),
&ps(
"SSR_ComputeBilateralCleanup.fx",
"ComputeBilateralCleanupPS",
),
&[
(0, Resource::ConstantBuffer),
(1, Resource::ConstantBuffer),
(2, Resource::DepthTexture),
(3, Resource::Texture),
(4, Resource::Texture),
(5, Resource::Texture),
(6, Resource::Texture),
(7, Resource::Texture),
],
&[RADIANCE_FORMAT],
Some(DEPTH_STENCIL_FORMAT),
DepthStencilStateDesc::EnableDepthNoWrites,
);
}
}
true
}
fn reset_history(&mut self, render_attribs: &mut RenderAttributes<'_, '_>) {
let current_frame_idx = render_attribs.post_fx_context.get_frame_desc().index;
let reset_accumulation = self.last_frame_idx.is_none_or(|last| {
current_frame_idx != last.wrapping_add(1)
}) || render_attribs.reset_accumulation; self.last_frame_idx = Some(current_frame_idx);
if reset_accumulation != self.history_reset {
self.history_reset = reset_accumulation;
render_attribs.queue.write_buffer(
&self.history_reset_buffer,
0,
bytemuck::bytes_of(&u32::from(reset_accumulation)),
);
}
if !reset_accumulation {
return;
}
let r = self.resources();
for history in r.radiance_history.iter().chain(&r.variance_history) {
render_attribs.post_fx_context.clear_render_target(
&mut TextureOperationAttribs {
device: render_attribs.device,
device_context: render_attribs.device_context,
timestamp_writes: None,
},
history,
[0.0, 0.0, 0.0, 0.0],
);
}
}
fn update_constant_buffer(
&mut self,
render_attribs: &RenderAttributes<'_, '_>,
reset_timer: bool,
) {
let elapsed = match self.frame_timer {
Some(elapsed) if !reset_timer => elapsed + render_attribs.frame_time,
_ => 0.0,
};
self.frame_timer = Some(elapsed);
let alpha = render_attribs.post_fx_context.get_transition_alpha(elapsed);
let mut attribs = *render_attribs.ssr_attribs;
attribs.alpha_interpolation = alpha;
let top_mip = self.hierarchical_depth_mip_map_srv.len().saturating_sub(1) as u32;
attribs.most_detailed_mip = attribs.most_detailed_mip.min(top_mip);
let update_required = bytemuck::bytes_of(&attribs) != bytemuck::bytes_of(&self.ssr_attribs);
if update_required {
self.ssr_attribs = attribs;
render_attribs.queue.write_buffer(
&self.constant_buffer,
0,
bytemuck::bytes_of(&self.ssr_attribs),
);
}
}
fn compute_hierarchical_depth_buffer(&mut self, render_attribs: &mut RenderAttributes<'_, '_>) {
render_attribs.post_fx_context.copy_texture_depth(
&mut TextureOperationAttribs {
device: render_attribs.device,
device_context: render_attribs.device_context,
timestamp_writes: render_attribs
.pass_timestamps
.and_then(|timestamps| timestamps("ComputeHierarchicalDepthBuffer")),
},
render_attribs.depth_buffer_srv,
&self.hierarchical_depth_mip_map_rtv[0],
DEPTH_HIERARCHY_FORMAT,
);
let tech = self.technique(RenderTech::ComputeHierarchicalDepthBuffer);
for mip_level in 1..self.hierarchical_depth_mip_map_rtv.len() {
let group = tech.bind_group(
render_attribs.device,
"ComputeHierarchicalDepthBuffer",
&[wgpu::BindGroupEntry {
binding: 0,
resource: texture(&self.hierarchical_depth_mip_map_srv[mip_level - 1]),
}],
);
draw_pass(
render_attribs.device_context,
"ComputeHierarchicalDepthBuffer",
&[&self.hierarchical_depth_mip_map_rtv[mip_level]],
None,
None,
tech,
&group,
render_attribs
.pass_timestamps
.and_then(|timestamps| timestamps("ComputeHierarchicalDepthBuffer")),
);
}
}
fn compute_stencil_mask_and_extract_roughness(
&mut self,
render_attribs: &mut RenderAttributes<'_, '_>,
) {
let tech = self.technique(RenderTech::ComputeStencilMaskAndExtractRoughness);
let r = self.resources();
let group = tech.bind_group(
render_attribs.device,
"ComputeStencilMaskAndExtractRoughness",
&[
wgpu::BindGroupEntry {
binding: 0,
resource: self.constant_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: texture(render_attribs.material_buffer_srv),
},
wgpu::BindGroupEntry {
binding: 2,
resource: texture(render_attribs.depth_buffer_srv),
},
],
);
draw_pass(
render_attribs.device_context,
"ComputeStencilMaskAndExtractRoughness",
&[&r.roughness],
None,
Some(DepthStencil::Clear(&r.depth_stencil_mask)),
tech,
&group,
render_attribs
.pass_timestamps
.and_then(|timestamps| timestamps("ComputeStencilMaskAndExtractRoughness")),
);
}
fn compute_downsampled_stencil_mask(&mut self, render_attribs: &mut RenderAttributes<'_, '_>) {
if !self.feature_flags.contains(FeatureFlags::HALF_RESOLUTION) {
return;
}
let tech = self.technique(RenderTech::ComputeDownsampledStencilMask);
let r = self.resources();
let group = tech.bind_group(
render_attribs.device,
"ComputeDownsampledStencilMask",
&[
wgpu::BindGroupEntry {
binding: 0,
resource: self.constant_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: texture(&r.roughness),
},
wgpu::BindGroupEntry {
binding: 2,
resource: texture(render_attribs.depth_buffer_srv),
},
],
);
draw_pass(
render_attribs.device_context,
"ComputeDownsampledStencilMask",
&[],
None,
Some(DepthStencil::Clear(
r.depth_stencil_mask_half_res.as_ref().unwrap(),
)),
tech,
&group,
render_attribs
.pass_timestamps
.and_then(|timestamps| timestamps("ComputeDownsampledStencilMask")),
);
}
fn compute_intersection(&mut self, render_attribs: &mut RenderAttributes<'_, '_>) {
let tech = self.technique(RenderTech::ComputeIntersection);
let r = self.resources();
let ctx = &*render_attribs.post_fx_context;
let mut entries = vec![
wgpu::BindGroupEntry {
binding: 0,
resource: ctx.get_camera_attribs_cb().as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: self.constant_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: texture(render_attribs.color_buffer_srv),
},
wgpu::BindGroupEntry {
binding: 3,
resource: texture(render_attribs.normal_buffer_srv),
},
wgpu::BindGroupEntry {
binding: 4,
resource: texture(&r.roughness),
},
wgpu::BindGroupEntry {
binding: 6,
resource: texture(ctx.get_2d_blue_noise_srv(BlueNoiseDimension::Xy)),
},
wgpu::BindGroupEntry {
binding: 7,
resource: texture(&r.depth_hierarchy),
},
];
if self.feature_flags.contains(FeatureFlags::PREVIOUS_FRAME) {
entries.push(wgpu::BindGroupEntry {
binding: 5,
resource: texture(render_attribs.motion_vectors_srv),
});
}
let group = tech.bind_group(render_attribs.device, "ComputeIntersection", &entries);
let dsv = if self.feature_flags.contains(FeatureFlags::HALF_RESOLUTION) {
r.depth_stencil_mask_half_res.as_ref().unwrap()
} else {
&r.depth_stencil_mask
};
draw_pass(
render_attribs.device_context,
"ComputeIntersection",
&[&r.radiance, &r.ray_direction_pdf],
Some(&[wgpu::Color::TRANSPARENT; 2]),
Some(DepthStencil::ReadOnly(dsv)),
tech,
&group,
render_attribs
.pass_timestamps
.and_then(|timestamps| timestamps("ComputeIntersection")),
);
}
fn classify_denoiser_tiles(&mut self, render_attribs: &mut RenderAttributes<'_, '_>) {
let r = self.resources();
let sampler = wgpu::BindGroupEntry {
binding: 3,
resource: wgpu::BindingResource::Sampler(&self.linear_clamp),
};
for (render_tech, entries, output) in [
(
RenderTech::ClassifyDenoiserTiles,
vec![
wgpu::BindGroupEntry {
binding: 1,
resource: texture(&r.radiance),
},
sampler.clone(),
],
&r.denoiser_hits,
),
(
RenderTech::DilateDenoiserTiles,
vec![
wgpu::BindGroupEntry {
binding: 0,
resource: self.constant_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: texture(&r.denoiser_hits),
},
sampler.clone(),
],
&r.denoiser_tiles,
),
] {
let tech = self.technique(render_tech);
let group = tech.bind_group(render_attribs.device, "DenoiserTiles", &entries);
draw_pass(
render_attribs.device_context,
"DenoiserTiles",
&[output],
None,
None,
tech,
&group,
render_attribs
.pass_timestamps
.and_then(|timestamps| timestamps("SpatialReconstruction")),
);
}
}
fn compute_spatial_reconstruction(&mut self, render_attribs: &mut RenderAttributes<'_, '_>) {
let tech = self.technique(RenderTech::ComputeSpatialReconstruction);
let r = self.resources();
let ctx = &*render_attribs.post_fx_context;
let group = tech.bind_group(
render_attribs.device,
"SpatialReconstruction",
&[
wgpu::BindGroupEntry {
binding: 0,
resource: ctx.get_camera_attribs_cb().as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: self.constant_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: texture(&r.roughness),
},
wgpu::BindGroupEntry {
binding: 3,
resource: texture(render_attribs.normal_buffer_srv),
},
wgpu::BindGroupEntry {
binding: 4,
resource: texture(render_attribs.depth_buffer_srv),
},
wgpu::BindGroupEntry {
binding: 5,
resource: texture(&r.ray_direction_pdf),
},
wgpu::BindGroupEntry {
binding: 6,
resource: texture(&r.radiance),
},
wgpu::BindGroupEntry {
binding: 7,
resource: texture(&r.denoiser_tiles),
},
],
);
draw_pass(
render_attribs.device_context,
"SpatialReconstruction",
&[
&r.resolved_radiance,
&r.resolved_variance,
&r.resolved_depth,
],
Some(&[wgpu::Color::TRANSPARENT; 3]),
Some(DepthStencil::ReadOnly(&r.depth_stencil_mask)),
tech,
&group,
render_attribs
.pass_timestamps
.and_then(|timestamps| timestamps("SpatialReconstruction")),
);
}
fn compute_temporal_accumulation(&mut self, render_attribs: &mut RenderAttributes<'_, '_>) {
let tech = self.technique(RenderTech::ComputeTemporalAccumulation);
let r = self.resources();
let ctx = &*render_attribs.post_fx_context;
let frame_index = ctx.get_frame_desc().index;
let curr_frame_idx = (frame_index & 0x01) as usize;
let prev_frame_idx = (frame_index.wrapping_add(1) & 0x01) as usize;
let group = tech.bind_group(
render_attribs.device,
"ComputeTemporalAccumulation",
&[
wgpu::BindGroupEntry {
binding: 0,
resource: ctx.get_camera_attribs_cb().as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: self.constant_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: texture(render_attribs.motion_vectors_srv),
},
wgpu::BindGroupEntry {
binding: 3,
resource: texture(&r.resolved_depth),
},
wgpu::BindGroupEntry {
binding: 4,
resource: texture(ctx.get_reprojected_depth()),
},
wgpu::BindGroupEntry {
binding: 5,
resource: texture(&r.resolved_radiance),
},
wgpu::BindGroupEntry {
binding: 6,
resource: texture(&r.resolved_variance),
},
wgpu::BindGroupEntry {
binding: 7,
resource: texture(ctx.get_previous_depth()),
},
wgpu::BindGroupEntry {
binding: 8,
resource: texture(&r.radiance_history[prev_frame_idx]),
},
wgpu::BindGroupEntry {
binding: 9,
resource: texture(&r.variance_history[prev_frame_idx]),
},
wgpu::BindGroupEntry {
binding: 10,
resource: wgpu::BindingResource::Sampler(&self.linear_clamp),
},
wgpu::BindGroupEntry {
binding: 11,
resource: wgpu::BindingResource::Sampler(&self.linear_clamp),
},
wgpu::BindGroupEntry {
binding: 12,
resource: texture(&r.denoiser_tiles),
},
wgpu::BindGroupEntry {
binding: 13,
resource: self.history_reset_buffer.as_entire_binding(),
},
],
);
draw_pass(
render_attribs.device_context,
"ComputeTemporalAccumulation",
&[
&r.radiance_history[curr_frame_idx],
&r.variance_history[curr_frame_idx],
],
Some(&[wgpu::Color::TRANSPARENT, wgpu::Color::WHITE]),
Some(DepthStencil::ReadOnly(&r.depth_stencil_mask)),
tech,
&group,
render_attribs
.pass_timestamps
.and_then(|timestamps| timestamps("ComputeTemporalAccumulation")),
);
}
fn compute_bilateral_cleanup(&mut self, render_attribs: &mut RenderAttributes<'_, '_>) {
let tech = self.technique(RenderTech::ComputeBilateralCleanup);
let r = self.resources();
let ctx = &*render_attribs.post_fx_context;
let curr_frame_idx = (ctx.get_frame_desc().index & 0x1) as usize;
let group = tech.bind_group(
render_attribs.device,
"ComputeBilateralCleanup",
&[
wgpu::BindGroupEntry {
binding: 0,
resource: ctx.get_camera_attribs_cb().as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: self.constant_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: texture(render_attribs.depth_buffer_srv),
},
wgpu::BindGroupEntry {
binding: 3,
resource: texture(render_attribs.normal_buffer_srv),
},
wgpu::BindGroupEntry {
binding: 4,
resource: texture(&r.roughness),
},
wgpu::BindGroupEntry {
binding: 5,
resource: texture(&r.radiance_history[curr_frame_idx]),
},
wgpu::BindGroupEntry {
binding: 6,
resource: texture(&r.variance_history[curr_frame_idx]),
},
wgpu::BindGroupEntry {
binding: 7,
resource: texture(&r.denoiser_tiles),
},
],
);
draw_pass(
render_attribs.device_context,
"ComputeBilateralCleanup",
&[&r.output],
Some(&[wgpu::Color::TRANSPARENT]),
Some(DepthStencil::ReadOnly(&r.depth_stencil_mask)),
tech,
&group,
render_attribs
.pass_timestamps
.and_then(|timestamps| timestamps("ComputeBilateralCleanup")),
);
}
fn compute_placeholder_texture(&mut self, render_attribs: &mut RenderAttributes<'_, '_>) {
let output = &self.resources().output;
render_attribs.post_fx_context.clear_render_target(
&mut TextureOperationAttribs {
device: render_attribs.device,
device_context: render_attribs.device_context,
timestamp_writes: None,
},
output,
[0.0, 0.0, 0.0, 0.0],
);
}
}
#[cfg(all(test, not(target_arch = "wasm32")))]
#[path = "ssr_roughness_tests.rs"]
mod roughness_tests;
#[cfg(all(test, not(target_arch = "wasm32")))]
#[path = "ssr_temporal_neighborhood_tests.rs"]
mod temporal_neighborhood_tests;
#[cfg(all(test, not(target_arch = "wasm32")))]
#[path = "ssr_intersection_tests.rs"]
mod intersection_tests;