Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify the same texture in temporal accumulation #189

Merged
merged 1 commit into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions blade-render/code/a-trous.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ var t_flat_normal: texture_2d<f32>;
var t_prev_flat_normal: texture_2d<f32>;
var t_motion: texture_2d<f32>;
var input: texture_2d<f32>;
var prev_input: texture_2d<f32>;
var output: texture_storage_2d<rgba16float, write>;
var output: texture_storage_2d<rgba16float, read_write>;

const LUMA: vec3<f32> = vec3<f32>(0.2126, 0.7152, 0.0722);
const MIN_WEIGHT: f32 = 0.01;
Expand Down Expand Up @@ -80,34 +79,37 @@ fn temporal_accum(@builtin(global_invocation_id) global_id: vec3<u32>) {

var sum_weight = 0.0;
var sum_ilm = vec4<f32>(0.0);
//TODO: optimize depth load with a gather operation
for (var i = 0; i < 4; i += 1) {
let prev_pixel = prev_pixels[i];
if (all(prev_pixel >= vec2<i32>(0)) && all(prev_pixel < params.extent)) {
let prev_surface = read_prev_surface(prev_pixel);
if (compare_flat_normals(surface.flat_normal, prev_surface.flat_normal) < 0.5) {
continue;
}
let projected_distance = length(pos_world - prev_camera.position);
if (compare_depths(prev_surface.depth, projected_distance) < 0.5) {
continue;
if (params.temporal_weight != 1.0) {
//TODO: optimize depth load with a gather operation
for (var i = 0; i < 4; i += 1) {
let prev_pixel = prev_pixels[i];
if (all(prev_pixel >= vec2<i32>(0)) && all(prev_pixel < params.extent)) {
let prev_surface = read_prev_surface(prev_pixel);
if (compare_flat_normals(surface.flat_normal, prev_surface.flat_normal) < 0.5) {
continue;
}
let projected_distance = length(pos_world - prev_camera.position);
if (compare_depths(prev_surface.depth, projected_distance) < 0.5) {
continue;
}
let w = prev_weights[i];
sum_weight += w;
let illumination = w * textureLoad(input, prev_pixel, 0).xyz;
let luminocity = dot(illumination, LUMA);
sum_ilm += vec4<f32>(illumination, luminocity * luminocity);
}
let w = prev_weights[i];
sum_weight += w;
let illumination = w * textureLoad(prev_input, prev_pixel, 0).xyz;
let luminocity = dot(illumination, LUMA);
sum_ilm += vec4<f32>(illumination, luminocity * luminocity);
}
}

let cur_illumination = textureLoad(input, pixel, 0).xyz;
let cur_illumination = textureLoad(output, pixel).xyz;
let cur_luminocity = dot(cur_illumination, LUMA);
var mixed_ilm = vec4<f32>(cur_illumination, cur_luminocity * cur_luminocity);
if (sum_weight > MIN_WEIGHT) {
let prev_ilm = sum_ilm / vec4(vec3<f32>(sum_weight), max(0.001, sum_weight*sum_weight));
mixed_ilm = mix(mixed_ilm, prev_ilm, sum_weight * (1.0 - params.temporal_weight));
}
textureStore(output, global_id.xy, mixed_ilm);
//Note: could also use HW blending for this
textureStore(output, pixel, mixed_ilm);
}

const GAUSSIAN_WEIGHTS = vec2<f32>(0.44198, 0.27901);
Expand Down
11 changes: 3 additions & 8 deletions blade-render/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,6 @@ struct TemporalAccumData {
prev_camera: CameraParams,
params: BlurParams,
input: blade_graphics::TextureView,
prev_input: blade_graphics::TextureView,
t_depth: blade_graphics::TextureView,
t_prev_depth: blade_graphics::TextureView,
t_flat_normal: blade_graphics::TextureView,
Expand Down Expand Up @@ -1229,7 +1228,6 @@ impl Renderer {
pad: 0,
};
let (cur, prev) = self.work_indices();
let temp = 2;

if denoiser_config.temporal_weight < 1.0 {
let mut pass = command_encoder.compute("temporal-accum");
Expand All @@ -1244,23 +1242,20 @@ impl Renderer {
camera: self.targets.camera_params[cur],
prev_camera: self.targets.camera_params[prev],
params,
input: self.targets.light_diffuse.views[cur],
prev_input: self.targets.light_diffuse.views[prev],
input: self.targets.light_diffuse.views[prev],
t_depth: self.targets.depth.views[cur],
t_prev_depth: self.targets.depth.views[prev],
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[temp],
output: self.targets.light_diffuse.views[cur],
},
);
pc.dispatch(groups);
//Note: making `cur` contain the latest reprojection output
self.targets.light_diffuse.views.swap(cur, temp);
}

assert_eq!(cur, self.post_proc_input_index);
let mut ping_pong = [temp, if self.is_frozen { cur } else { prev }];
let mut ping_pong = [2, if self.is_frozen { cur } else { prev }];
for _ in 0..denoiser_config.num_passes {
let mut pass = command_encoder.compute("a-trous");
let mut pc = pass.with(&self.blur.a_trous_pipeline);
Expand Down
1 change: 1 addition & 0 deletions examples/scene/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ impl Example {
window,
gpu::ContextDesc {
validation: cfg!(debug_assertions),
capture: true,
..Default::default()
},
)
Expand Down
Loading