Skip to content

Commit

Permalink
Only apply the motion vectors if the scene is fresh
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Nov 21, 2023
1 parent a538edb commit 4204552
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 5 deletions.
13 changes: 11 additions & 2 deletions blade-render/code/blur.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct Params {
extent: vec2<i32>,
temporal_weight: f32,
iteration: u32,
use_motion_vectors: u32,
}

var<uniform> camera: CameraParams;
Expand Down Expand Up @@ -42,6 +43,15 @@ fn read_prev_surface(pixel: vec2<i32>) -> Surface {
return surface;
}

fn get_prev_pixel(pixel: vec2<i32>, pos_world: vec3<f32>) -> vec2<f32> {
if (USE_MOTION_VECTORS && params.use_motion_vectors != 0u) {
let motion = textureLoad(t_motion, pixel, 0).xy / MOTION_SCALE;
return vec2<f32>(pixel) + 0.5 + motion;
} else {
return get_projected_pixel_float(prev_camera, pos_world);
}
}

@compute @workgroup_size(8, 8)
fn temporal_accum(@builtin(global_invocation_id) global_id: vec3<u32>) {
let pixel = vec2<i32>(global_id.xy);
Expand All @@ -52,8 +62,7 @@ fn temporal_accum(@builtin(global_invocation_id) global_id: vec3<u32>) {
let surface = read_surface(pixel);
let pos_world = camera.position + surface.depth * get_ray_direction(camera, pixel);
// considering all samples in 2x2 quad, to help with edges
let motion = textureLoad(t_motion, pixel, 0).xy / MOTION_SCALE;
let center_pixel = vec2<f32>(pixel) + 0.5 + motion;
var center_pixel = get_prev_pixel(pixel, pos_world);
var prev_pixels = array<vec2<i32>, 4>(
vec2<i32>(vec2<f32>(center_pixel.x - 0.5, center_pixel.y - 0.5)),
vec2<i32>(vec2<f32>(center_pixel.x + 0.5, center_pixel.y - 0.5)),
Expand Down
1 change: 1 addition & 0 deletions blade-render/code/fill-gbuf.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
let prev_position = (entry.prev_object_to_world * position_object).xyz;
let prev_screen = get_projected_pixel_float(prev_camera, prev_position);
//TODO: consider just storing integers here?
//TODO: technically this "0.5" is just a waste compute on both packing and unpacking
motion = prev_screen - vec2<f32>(global_id.xy) - 0.5;
if (debug.view_mode == DebugMode_Motion) {
textureStore(out_debug, global_id.xy, vec4<f32>(motion * MOTION_SCALE + vec2<f32>(0.5), 0.0, 1.0));
Expand Down
1 change: 1 addition & 0 deletions blade-render/code/gbuf.inc.wgsl
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
const MOTION_SCALE: f32 = 0.02;
const USE_MOTION_VECTORS: bool = true;
13 changes: 11 additions & 2 deletions blade-render/code/ray-trace.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct MainParams {
spatial_taps: u32,
spatial_tap_history: u32,
spatial_radius: i32,
use_motion_vectors: u32,
};

var<uniform> camera: CameraParams;
Expand Down Expand Up @@ -252,6 +253,15 @@ fn evaluate_reflected_light(surface: Surface, light_index: u32, light_uv: vec2<f
return radiance * brdf;
}

fn get_prev_pixel(pixel: vec2<i32>, pos_world: vec3<f32>) -> vec2<f32> {
if (USE_MOTION_VECTORS && parameters.use_motion_vectors != 0u) {
let motion = textureLoad(t_motion, pixel, 0).xy / MOTION_SCALE;
return vec2<f32>(pixel) + 0.5 + motion;
} else {
return get_projected_pixel_float(prev_camera, pos_world);
}
}

struct TargetScore {
color: vec3<f32>,
score: f32,
Expand Down Expand Up @@ -364,9 +374,8 @@ fn compute_restir(surface: Surface, pixel: vec2<i32>, rng: ptr<function, RandomS
}
}

let motion = textureLoad(t_motion, pixel, 0).xy / MOTION_SCALE;
//TODO: find best match in a 2x2 grid
let prev_pixel = pixel + vec2<i32>(motion + vec2<f32>(0.5));
let prev_pixel = vec2<i32>(get_prev_pixel(pixel, position));

// First, gather the list of reservoirs to merge with
var accepted_reservoir_indices = array<i32, MAX_RESERVOIRS>();
Expand Down
9 changes: 9 additions & 0 deletions blade-render/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ pub struct Renderer {
screen_size: blade_graphics::Extent,
screen_format: blade_graphics::TextureFormat,
frame_index: usize,
frame_scene_built: usize,
//TODO: refactor `ResourceArray` to not carry the freelist logic
// This way we can embed user info into the allocator.
texture_resource_lookup:
Expand Down Expand Up @@ -357,6 +358,7 @@ struct MainParams {
spatial_taps: u32,
spatial_tap_history: u32,
spatial_radius: u32,
use_motion_vectors: u32,
}

#[derive(blade_macros::ShaderData)]
Expand Down Expand Up @@ -410,6 +412,8 @@ struct BlurParams {
extent: [u32; 2],
temporal_weight: f32,
iteration: i32,
use_motion_vectors: u32,
pad: u32,
}

#[derive(blade_macros::ShaderData)]
Expand Down Expand Up @@ -696,6 +700,7 @@ impl Renderer {
screen_size: config.screen_size,
screen_format: config.surface_format,
frame_index: 0,
frame_scene_built: 0,
texture_resource_lookup: HashMap::default(),
}
}
Expand Down Expand Up @@ -991,6 +996,7 @@ impl Renderer {

temp.buffers.push(instance_buf);
temp.buffers.push(scratch_buf);
self.frame_scene_built = self.frame_index + 1;
}

fn make_debug_params(&self, config: &DebugConfig) -> DebugParams {
Expand Down Expand Up @@ -1123,6 +1129,7 @@ impl Renderer {
spatial_taps: ray_config.spatial_taps,
spatial_tap_history: ray_config.spatial_tap_history,
spatial_radius: ray_config.spatial_radius,
use_motion_vectors: (self.frame_scene_built == self.frame_index) as u32,
},
acc_struct: self.acceleration_structure,
sampler_linear: self.samplers.linear,
Expand Down Expand Up @@ -1158,6 +1165,8 @@ impl Renderer {
extent: [self.screen_size.width, self.screen_size.height],
temporal_weight: denoiser_config.temporal_weight,
iteration: 0,
use_motion_vectors: (self.frame_scene_built == self.frame_index) as u32,
pad: 0,
};
let cur = self.frame_index % 2;
let prev = cur ^ 1;
Expand Down
1 change: 0 additions & 1 deletion examples/scene/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,6 @@ impl Example {
}
.to_blade();
if object.transform != t1 {
object.prev_transform = object.transform;
object.transform = t1;
self.have_objects_changed = true;
}
Expand Down

0 comments on commit 4204552

Please sign in to comment.