Skip to content

Commit

Permalink
render: sample previous frame acceleration structure
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Jul 16, 2024
1 parent 2da5423 commit 3c1c893
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
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
15 changes: 8 additions & 7 deletions blade-render/code/ray-trace.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -41,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 @@ -223,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 @@ -273,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 @@ -287,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 @@ -312,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 @@ -435,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 @@ -448,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
19 changes: 17 additions & 2 deletions blade-render/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,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 @@ -392,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 @@ -701,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 @@ -726,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 @@ -849,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 @@ -1153,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

0 comments on commit 3c1c893

Please sign in to comment.