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

Use prev acceleration structure for pairwise MIS in motion #141

Merged
merged 2 commits into from
Jul 16, 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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ gltf = { version = "1.1", default-features = false }
log = "0.4"
mint = "0.5"
#TODO: switch to crates
naga = { git = "https://github.com/gfx-rs/wgpu", features = ["wgsl-in"], rev = "425526828f738c95ec50b016c6a761bc00d2fb25" }
naga = { git = "https://github.com/gfx-rs/wgpu", features = ["wgsl-in"], rev = "f44f52a85ddb3e7b93fe195fb98a8990b05575d8" }
profiling = "1"
slab = "0.4"
strum = { version = "0.25", features = ["derive"] }
Expand Down
6 changes: 6 additions & 0 deletions blade-helpers/src/hud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ impl ExposeHud for blade_render::RayConfig {
&mut self.environment_importance_sampling,
"Env importance sampling",
);
ui.checkbox(&mut self.temporal_tap, "Temporal tap");
ui.add(
egui::widgets::Slider::new(&mut self.temporal_history, 0..=50).text("Temporal history"),
);
Expand All @@ -25,6 +26,11 @@ impl ExposeHud for blade_render::RayConfig {
egui::widgets::Slider::new(&mut self.spatial_radius, 1..=50)
.text("Spatial radius (px)"),
);
ui.add(
egui::widgets::Slider::new(&mut self.t_start, 0.001..=0.5)
.text("T min")
.logarithmic(true),
);
}
}

Expand Down
18 changes: 10 additions & 8 deletions blade-render/code/ray-trace.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct MainParams {
frame_index: u32,
num_environment_samples: u32,
environment_importance_sampling: u32,
temporal_tap: u32,
temporal_history: u32,
spatial_taps: u32,
spatial_tap_history: u32,
Expand All @@ -40,6 +41,7 @@ var<uniform> prev_camera: CameraParams;
var<uniform> parameters: MainParams;
var<uniform> debug: DebugParams;
var acc_struct: acceleration_structure;
var prev_acc_struct: acceleration_structure;
var env_map: texture_2d<f32>;
var sampler_linear: sampler;
var sampler_nearest: sampler;
Expand Down Expand Up @@ -222,10 +224,10 @@ fn evaluate_brdf(surface: Surface, dir: vec3<f32>) -> f32 {
return lambert_brdf * max(0.0, lambert_term);
}

fn check_ray_occluded(position: vec3<f32>, direction: vec3<f32>, debug_len: f32) -> bool {
fn check_ray_occluded(acs: acceleration_structure, position: vec3<f32>, direction: vec3<f32>, debug_len: f32) -> bool {
var rq: ray_query;
let flags = RAY_FLAG_TERMINATE_ON_FIRST_HIT | RAY_FLAG_CULL_NO_OPAQUE;
rayQueryInitialize(&rq, acc_struct,
rayQueryInitialize(&rq, acs,
RayDesc(flags, 0xFFu, parameters.t_start, camera.depth, position, direction)
);
rayQueryProceed(&rq);
Expand Down Expand Up @@ -272,7 +274,7 @@ fn make_target_score(color: vec3<f32>) -> TargetScore {
}

fn estimate_target_score_with_occlusion(
surface: Surface, position: vec3<f32>, light_index: u32, light_uv: vec2<f32>, debug_len: f32
surface: Surface, position: vec3<f32>, light_index: u32, light_uv: vec2<f32>, acs: acceleration_structure, debug_len: f32
) -> TargetScore {
if (light_index != 0u) {
return TargetScore();
Expand All @@ -286,7 +288,7 @@ fn estimate_target_score_with_occlusion(
return TargetScore();
}

if (check_ray_occluded(position, direction, debug_len)) {
if (check_ray_occluded(acs, position, direction, debug_len)) {
return TargetScore();
} else {
//Note: same as `evaluate_reflected_light`
Expand All @@ -311,7 +313,7 @@ fn evaluate_sample(ls: LightSample, surface: Surface, start_pos: vec3<f32>, debu
return 0.0;
}

if (check_ray_occluded(start_pos, dir, debug_len)) {
if (check_ray_occluded(acc_struct, start_pos, dir, debug_len)) {
return 0.0;
}

Expand Down Expand Up @@ -385,7 +387,7 @@ fn compute_restir(surface: Surface, pixel: vec2<i32>, rng: ptr<function, RandomS
let r0 = max(prev_pixel - vec2<i32>(parameters.spatial_radius), vec2<i32>(0));
let r1 = min(prev_pixel + vec2<i32>(parameters.spatial_radius + 1), vec2<i32>(prev_camera.target_size));
other_pixel = vec2<i32>(mix(vec2<f32>(r0), vec2<f32>(r1), vec2<f32>(random_gen(rng), random_gen(rng))));
} else if (parameters.temporal_history == 0u)
} else if (parameters.temporal_tap == 0u)
{
continue;
}
Expand Down Expand Up @@ -434,7 +436,7 @@ fn compute_restir(surface: Surface, pixel: vec2<i32>, rng: ptr<function, RandomS
let neighbor_position = prev_camera.position + neighbor_surface.depth * neighbor_dir;

let t_canonical_at_neighbor = estimate_target_score_with_occlusion(
neighbor_surface, neighbor_position, canonical.selected_light_index, canonical.selected_uv, debug_len);
neighbor_surface, neighbor_position, canonical.selected_light_index, canonical.selected_uv, prev_acc_struct, debug_len);
let mis_sub_canonical = balance_heuristic(
t_canonical_at_neighbor.score, canonical.selected_target_score,
neighbor_history * f32(accepted_count), canonical.history);
Expand All @@ -447,7 +449,7 @@ fn compute_restir(surface: Surface, pixel: vec2<i32>, rng: ptr<function, RandomS
// 2. we can use the cached target score, and there is no use of the target color
//let t_neighbor_at_neighbor = estimate_target_pdf(neighbor_surface, neighbor_position, neighbor.selected_dir);
let t_neighbor_at_canonical = estimate_target_score_with_occlusion(
surface, position, neighbor.light_index, neighbor.light_uv, debug_len);
surface, position, neighbor.light_index, neighbor.light_uv, acc_struct, debug_len);
let mis_neighbor = balance_heuristic(
neighbor.target_score, t_neighbor_at_canonical.score,
neighbor_history * f32(accepted_count), canonical.history);
Expand Down
22 changes: 20 additions & 2 deletions blade-render/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pub struct DebugConfig {
pub struct RayConfig {
pub num_environment_samples: u32,
pub environment_importance_sampling: bool,
pub temporal_tap: bool,
pub temporal_history: u32,
pub spatial_taps: u32,
pub spatial_tap_history: u32,
Expand Down Expand Up @@ -310,6 +311,7 @@ pub struct Renderer {
post_proc_pipeline: blade_graphics::RenderPipeline,
blur: Blur,
acceleration_structure: blade_graphics::AccelerationStructure,
prev_acceleration_structure: blade_graphics::AccelerationStructure,
env_map: EnvironmentMap,
dummy: DummyResources,
hit_buffer: blade_graphics::Buffer,
Expand Down Expand Up @@ -355,6 +357,7 @@ struct MainParams {
frame_index: u32,
num_environment_samples: u32,
environment_importance_sampling: u32,
temporal_tap: u32,
temporal_history: u32,
spatial_taps: u32,
spatial_tap_history: u32,
Expand Down Expand Up @@ -390,6 +393,7 @@ struct MainData {
debug: DebugParams,
parameters: MainParams,
acc_struct: blade_graphics::AccelerationStructure,
prev_acc_struct: blade_graphics::AccelerationStructure,
sampler_linear: blade_graphics::Sampler,
sampler_nearest: blade_graphics::Sampler,
env_map: blade_graphics::TextureView,
Expand Down Expand Up @@ -699,6 +703,7 @@ impl Renderer {
atrous_pipeline: sp.atrous,
},
acceleration_structure: blade_graphics::AccelerationStructure::default(),
prev_acceleration_structure: blade_graphics::AccelerationStructure::default(),
env_map: EnvironmentMap::with_pipeline(&dummy, sp.env_prepare),
dummy,
hit_buffer: blade_graphics::Buffer::default(),
Expand All @@ -724,6 +729,9 @@ impl Renderer {
gpu.destroy_buffer(self.hit_buffer);
}
gpu.destroy_acceleration_structure(self.acceleration_structure);
if self.prev_acceleration_structure != blade_graphics::AccelerationStructure::default() {
gpu.destroy_acceleration_structure(self.prev_acceleration_structure);
}
// env map, dummy, and debug
self.env_map.destroy(gpu);
self.dummy.destroy(gpu);
Expand Down Expand Up @@ -847,10 +855,11 @@ impl Renderer {
self.env_map
.assign(env_view, env_extent, command_encoder, gpu);

if self.acceleration_structure != blade_graphics::AccelerationStructure::default() {
if self.prev_acceleration_structure != blade_graphics::AccelerationStructure::default() {
temp.acceleration_structures
.push(self.acceleration_structure);
.push(self.prev_acceleration_structure);
}
self.prev_acceleration_structure = self.acceleration_structure;

let geometry_count = objects
.iter()
Expand Down Expand Up @@ -1142,6 +1151,7 @@ impl Renderer {
num_environment_samples: ray_config.num_environment_samples,
environment_importance_sampling: ray_config.environment_importance_sampling
as u32,
temporal_tap: ray_config.temporal_tap as u32,
temporal_history: ray_config.temporal_history,
spatial_taps: ray_config.spatial_taps,
spatial_tap_history: ray_config.spatial_tap_history,
Expand All @@ -1150,6 +1160,14 @@ impl Renderer {
use_motion_vectors: (self.frame_scene_built == self.frame_index) as u32,
},
acc_struct: self.acceleration_structure,
prev_acc_struct: if self.frame_scene_built < self.frame_index
|| self.prev_acceleration_structure
== blade_graphics::AccelerationStructure::default()
{
self.acceleration_structure
} else {
self.prev_acceleration_structure
},
sampler_linear: self.samplers.linear,
sampler_nearest: self.samplers.nearest,
env_map: self.env_map.main_view,
Expand Down
1 change: 1 addition & 0 deletions examples/scene/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ impl Example {
ray_config: blade_render::RayConfig {
num_environment_samples: 1,
environment_importance_sampling: false,
temporal_tap: true,
temporal_history: 10,
spatial_taps: 1,
spatial_tap_history: 5,
Expand Down
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ impl Engine {
ray_config: blade_render::RayConfig {
num_environment_samples: 1,
environment_importance_sampling: false,
temporal_tap: true,
temporal_history: 10,
spatial_taps: 1,
spatial_tap_history: 5,
Expand Down Expand Up @@ -532,7 +533,7 @@ impl Engine {
scale_factor: f32,
) {
if self.track_hot_reloads {
self.frame_config.reset_reservoirs |= self.renderer.hot_reload(
self.renderer.hot_reload(
&self.asset_hub,
&self.gpu_context,
self.pacer.last_sync_point().unwrap(),
Expand Down Expand Up @@ -727,9 +728,7 @@ impl Engine {
egui::CollapsingHeader::new("Rendering")
.default_open(false)
.show(ui, |ui| {
let old_config = self.ray_config;
self.ray_config.populate_hud(ui);
self.frame_config.reset_reservoirs |= self.ray_config != old_config;
self.frame_config.reset_reservoirs |= ui.button("Reset Accumulation").clicked();
ui.checkbox(&mut self.denoiser_enabled, "Enable Denoiser");
self.denoiser_config.populate_hud(ui);
Expand Down
Loading