Skip to content

Commit

Permalink
Ensure previous light diffuse isn't used if not initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Oct 13, 2024
1 parent 543e082 commit c42b043
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
13 changes: 7 additions & 6 deletions blade-render/code/ray-trace.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ fn produce_canonical(surface: Surface, position: vec3<f32>, rng: ptr<function, R
bump_reservoir(&reservoir, 1.0);
}
}

normalize_reservoir(&reservoir, 1.0);
return reservoir;
}

Expand All @@ -361,7 +363,7 @@ struct Neighborhood {
fn gather_neighborhood_temporal(
surface: Surface, position: vec3<f32>, pixel: vec2<i32>, rng: ptr<function, RandomState>
) -> Neighborhood {
if (surface.depth == 0.0) {
if (surface.depth == 0.0 || parameters.temporal_tap == 0u) {
return Neighborhood();
}

Expand Down Expand Up @@ -409,7 +411,7 @@ fn gather_neighborhood_temporal(
fn gather_neighborhood_spatial(
surface: Surface, pixel: vec2<i32>, rng: ptr<function, RandomState>
) -> Neighborhood {
if (surface.depth == 0.0) {
if (surface.depth == 0.0 || parameters.spatial_taps == 0u) {
return Neighborhood();
}

Expand Down Expand Up @@ -574,9 +576,9 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
let neighborhood = gather_neighborhood_temporal(surface, position, pixel, &rng);
let canonical = produce_canonical(surface, position, &rng, enable_debug);
let ro = compute_restir(surface, ray_dir, canonical, neighborhood, parameters.temporal_history, &rng, enable_debug);

let pixel_index = get_reservoir_index(pixel, camera);
reservoirs[pixel_index] = ro.reservoir;

textureStore(out_diffuse, pixel, vec4<f32>(ro.radiance, 1.0));
}

Expand All @@ -586,9 +588,6 @@ fn main_spatial(@builtin(global_invocation_id) global_id: vec3<u32>) {
return;
}

let global_index = global_id.y * camera.target_size.x + global_id.x;
var rng = random_init(global_index, parameters.frame_index * 2u);

let surface = read_surface(vec2<i32>(global_id.xy));
let pixel = vec2<i32>(global_id.xy);
let pixel_index = get_reservoir_index(pixel, camera);
Expand All @@ -601,6 +600,8 @@ fn main_spatial(@builtin(global_invocation_id) global_id: vec3<u32>) {
}

let enable_debug = DEBUG_MODE && all(global_id.xy == debug.mouse_pos);
let global_index = global_id.y * camera.target_size.x + global_id.x;
var rng = random_init(global_index, parameters.frame_index * 2u);

let neighborhood = gather_neighborhood_spatial(surface, pixel, &rng);
let old_reservoir = prev_reservoirs[pixel_index];
Expand Down
44 changes: 31 additions & 13 deletions blade-render/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ pub struct Renderer {
surface_info: blade_graphics::SurfaceInfo,
frame_index: usize,
frame_scene_built: usize,
frame_resize: usize,
is_frozen: bool,
//TODO: refactor `ResourceArray` to not carry the freelist logic
// This way we can embed user info into the allocator.
Expand Down Expand Up @@ -752,6 +753,7 @@ impl Renderer {
surface_info: config.surface_info,
frame_index: 0,
frame_scene_built: 0,
frame_resize: 0,
is_frozen: false,
texture_resource_lookup: HashMap::default(),
}
Expand Down Expand Up @@ -877,6 +879,7 @@ impl Renderer {
self.surface_size = size;
self.targets.destroy(gpu);
self.targets = RestirTargets::new(size, self.reservoir_size, encoder, gpu);
self.frame_resize = self.frame_index + 1;
}

#[profiling::function]
Expand Down Expand Up @@ -1117,7 +1120,7 @@ impl Renderer {
}
self.debug.update_entry(&mut transfer);

if config.reset_reservoirs {
if config.reset_reservoirs || self.frame_resize == self.frame_index {
if !config.debug_draw {
self.debug.reset_lines(&mut transfer);
}
Expand All @@ -1143,7 +1146,7 @@ impl Renderer {
/// The result is stored internally in an HDR render target.
#[profiling::function]
pub fn ray_trace(
&self,
&mut self,
command_encoder: &mut blade_graphics::CommandEncoder,
debug_config: DebugConfig,
ray_config: RayConfig,
Expand Down Expand Up @@ -1233,9 +1236,10 @@ impl Renderer {
);
pc.dispatch(groups);
}
//Note: output reservoirs and lighting is in [1]

//TODO: control by `ray_config.spatial_pass`
if let mut pass = command_encoder.compute("ray-trace-spatial") {
if ray_config.spatial_pass {
let mut pass = command_encoder.compute("ray-trace-spatial");
let mut pc = pass.with(&self.spatial_pipeline);
let groups = self.spatial_pipeline.get_dispatch_for(self.surface_size);
pc.bind(
Expand Down Expand Up @@ -1267,19 +1271,27 @@ impl Renderer {
},
);
pc.dispatch(groups);
} else {
self.targets.reservoir_buf.swap(0, 1);
self.targets.light_diffuse.views.swap(0, 1);
}
//Note: output reservoirs and lighting is in [0]
}

/// Perform noise reduction using SVGF.
#[profiling::function]
pub fn denoise(
&mut self, //TODO: borrow immutably
&mut self,
command_encoder: &mut blade_graphics::CommandEncoder,
denoiser_config: DenoiserConfig,
) {
let mut params = BlurParams {
extent: [self.surface_size.width, self.surface_size.height],
temporal_weight: denoiser_config.temporal_weight,
temporal_weight: if self.frame_resize < self.frame_index {
denoiser_config.temporal_weight
} else {
1.0
},
iteration: 0,
use_motion_vectors: (self.frame_scene_built >= self.frame_index) as u32,
pad: 0,
Expand All @@ -1305,16 +1317,18 @@ impl Renderer {
t_flat_normal: self.targets.flat_normal.views[cur],
t_prev_flat_normal: self.targets.flat_normal.views[prev],
t_motion: self.targets.motion.views[0],
output: self.targets.light_diffuse.views[1],
output: self.targets.light_diffuse.views[0],
},
);
pc.dispatch(groups);
//Note: making `2` contain the latest reprojection output
self.targets.light_diffuse.views.swap(1, 2);
}

// Make sure the accumulated result is in [2]
self.targets.light_diffuse.views.swap(0, 2);

let mut input_index = 2;
for _ in 0..denoiser_config.num_passes {
self.targets.light_diffuse.views.swap(0, 1);
let output_index = if input_index == 0 { 1 } else { 0 };
let mut pass = command_encoder.compute("a-trous");
let mut pc = pass.with(&self.blur.a_trous_pipeline);
let groups = self
Expand All @@ -1325,16 +1339,20 @@ impl Renderer {
0,
&ATrousData {
params,
input: self.targets.light_diffuse.views
[if params.iteration == 0 { 2 } else { 1 }],
input: self.targets.light_diffuse.views[input_index],
t_depth: self.targets.depth.views[cur],
t_flat_normal: self.targets.flat_normal.views[cur],
output: self.targets.light_diffuse.views[0],
output: self.targets.light_diffuse.views[output_index],
},
);
pc.dispatch(groups);
params.iteration += 1;
input_index = output_index;
}
if input_index != 0 {
self.targets.light_diffuse.views.swap(0, input_index);
}
//Note: output lighting is in [0]
}

/// Blit the rendering result into a specified render pass.
Expand Down

0 comments on commit c42b043

Please sign in to comment.