use super::temporal_neighborhood_tests::{
SIZE, camera, depth, device, half, jittered, read_rgba16f, read_scalar, standing_at, texels,
texture,
};
use super::*;
use crate::{CameraAttribs, post_fx_context::FrameDesc};
fn device_depth(camera: &CameraAttribs, z: f32) -> f32 {
camera.m_proj[10] + camera.m_proj[14] / z
}
pub(super) struct Followed {
pub reversed: bool,
pub z: f32,
pub previous_eye: [f32; 3],
pub virtual_z: f32,
pub jitter: [[f32; 2]; 2],
pub motion: [f32; 4],
pub history: fn(u32, u32) -> f32,
pub current: fn(u32, u32) -> f32,
}
pub(super) fn followed(
device: &wgpu::Device,
queue: &wgpu::Queue,
f: &Followed,
) -> (Vec<[f32; 4]>, Vec<f32>) {
let mut ssr = ScreenSpaceReflection::new(device);
let mut context = PostFXContext::new(device, queue, Default::default());
let context_flags = if f.reversed {
post_fx_context::FeatureFlags::REVERSED_DEPTH
} else {
post_fx_context::FeatureFlags::NONE
};
let current = jittered(camera(f.reversed, 0), f.jitter[1]);
let plane = device_depth(¤t, f.z);
let mut histories = None;
for index in 0..2 {
let previous = if index == 0 {
current
} else {
jittered(
standing_at(camera(f.reversed, 0), f.previous_eye),
f.jitter[0],
)
};
let mut encoder = device.create_command_encoder(&Default::default());
let scene_depth = depth(device, &mut encoder, plane);
let previous_depth = depth(device, &mut encoder, plane);
let normal = texture(
device,
queue,
wgpu::TextureFormat::Rgba16Float,
&half(&[0., 0., -1., 0.]),
);
let material = texture(device, queue, wgpu::TextureFormat::R8Unorm, &[0]);
let motion = texture(
device,
queue,
wgpu::TextureFormat::Rgba16Float,
&half(&f.motion),
);
context.prepare_resources(
device,
&FrameDesc {
index,
width: SIZE[0],
height: SIZE[1],
output_width: SIZE[0],
output_height: SIZE[1],
},
context_flags,
);
ssr.prepare_resources(device, &mut encoder, &mut context, FeatureFlags::NONE);
ssr.prepare_shaders_and_pso(device);
context.execute(&mut post_fx_context::RenderAttributes {
device,
queue,
device_context: &mut encoder,
curr_depth_buffer_srv: &scene_depth,
prev_depth_buffer_srv: &previous_depth,
curr_camera: Some(¤t),
prev_camera: Some(&previous),
camera_attribs_cb: None,
pass_timestamps: None,
});
let radiance = if index == 0 { f.history } else { f.current };
let input: Vec<u8> = (0..SIZE[1])
.flat_map(|y| {
(0..SIZE[0]).flat_map(move |x| {
let value = radiance(x, y);
half(&[value, value, value, 1.])
})
})
.collect();
let resources = ssr.resources.as_mut().unwrap();
resources.resolved_radiance =
texels(device, queue, wgpu::TextureFormat::Rgba16Float, &input);
resources.resolved_variance =
texture(device, queue, wgpu::TextureFormat::R16Float, &half(&[0.25]));
resources.resolved_depth = texture(
device,
queue,
wgpu::TextureFormat::R32Float,
&device_depth(¤t, f.virtual_z).to_le_bytes(),
);
resources.denoiser_tiles = texture(device, queue, wgpu::TextureFormat::R8Unorm, &[255]);
let attribs = ScreenSpaceReflectionAttribs::default();
let mut render = RenderAttributes {
device,
queue,
device_context: &mut encoder,
post_fx_context: &mut context,
color_buffer_srv: &normal,
depth_buffer_srv: &scene_depth,
normal_buffer_srv: &normal,
material_buffer_srv: &material,
motion_vectors_srv: &motion,
ssr_attribs: &attribs,
pass_timestamps: None,
reset_accumulation: false,
frame_time: 1. / 60.,
};
ssr.update_constant_buffer(&render, false);
ssr.reset_history(&mut render);
ssr.compute_stencil_mask_and_extract_roughness(&mut render);
ssr.compute_temporal_accumulation(&mut render);
queue.submit([encoder.finish()]);
let r = ssr.resources();
let slot = (index & 1) as usize;
histories = Some((
read_rgba16f(device, queue, &r.radiance_history[slot]),
read_scalar(device, queue, &r.variance_history[slot]),
));
}
histories.unwrap()
}
pub(super) fn checkerboard(x: u32, y: u32) -> f32 {
((x + y) % 2) as f32
}
const RAMP: f32 = 0.01;
fn ramp(x: u32, _: u32) -> f32 {
0.25 + RAMP * x as f32
}
#[test]
fn a_surface_the_camera_follows_keeps_its_reflection_history() {
let Some((device, queue)) = device() else {
return;
};
for reversed in [false, true] {
let (radiance, variance) = followed(
&device,
&queue,
&Followed {
reversed,
z: 2.,
previous_eye: [0., 0., -10.],
virtual_z: 2.,
jitter: [[0.; 2]; 2],
motion: [0., 0., 0., 1.],
history: |_, _| 0.75,
current: |x, y| checkerboard(x, y),
},
);
let mut lost = 0;
for y in 0..SIZE[1] {
for x in 0..SIZE[0] {
let pixel = (y * SIZE[0] + x) as usize;
let expected = 0.05 * checkerboard(x, y) + 0.95 * 0.75;
if (radiance[pixel][0] - expected).abs() > 0.002
|| (variance[pixel] - 0.925).abs() > 0.002
{
lost += 1;
}
}
}
assert_eq!(
lost, 0,
"reversed={reversed}: {lost} pixels of the followed plane lost their history"
);
}
}
#[test]
fn the_fallback_search_keeps_a_followed_surfaces_history_in_place() {
let Some((device, queue)) = device() else {
return;
};
let (radiance, variance) = followed(
&device,
&queue,
&Followed {
reversed: false,
z: 8.,
previous_eye: [0., 0., -2.],
virtual_z: 8.,
jitter: [[0.; 2]; 2],
motion: [0., 0., 0., 0.],
history: ramp,
current: ramp,
},
);
let mut drifted = Vec::new();
for y in 1..SIZE[1] - 1 {
for x in 1..SIZE[0] - 1 {
if (x as f32 + 0.5 - SIZE[0] as f32 / 2.).abs() < 4. {
continue;
}
let pixel = (y * SIZE[0] + x) as usize;
assert!(
variance[pixel] < 1.,
"({x},{y}) kept no history: the search should accept the plane"
);
if (radiance[pixel][0] - ramp(x, y)).abs() > 0.4 * RAMP {
drifted.push((x, y, ramp(x, y), radiance[pixel][0]));
}
}
}
assert!(
drifted.is_empty(),
"{} pixels read history from another position, first (x, y, expected, got): {:?}",
drifted.len(),
drifted.first()
);
}