-
-
Notifications
You must be signed in to change notification settings - Fork 4k
bevy_solari ReSTIR DI #19790
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
base: main
Are you sure you want to change the base?
bevy_solari ReSTIR DI #19790
Changes from 7 commits
2ad21d3
82b75df
7df0224
2a927f6
6aba44d
9e40fd1
c03a349
0527f10
77fd784
2074f71
76ab6af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,9 @@ | |
struct PreviousViewUniforms { | ||
view_from_world: mat4x4<f32>, | ||
clip_from_world: mat4x4<f32>, | ||
clip_from_view: mat4x4<f32>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was this just missed? Not sure why it's here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah it was missed in a previous PR. |
||
world_from_clip: mat4x4<f32>, | ||
view_from_clip: mat4x4<f32>, | ||
} | ||
|
||
@group(0) @binding(0) var<uniform> view: View; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,9 @@ | |
|
||
#define_import_path bevy_solari::reservoir | ||
|
||
#import bevy_solari::sampling::LightSample | ||
#import bevy_core_pipeline::tonemapping::tonemapping_luminance as luminance | ||
#import bevy_pbr::utils::rand_f | ||
#import bevy_solari::sampling::{LightSample, calculate_light_contribution} | ||
|
||
const NULL_RESERVOIR_SAMPLE = 0xFFFFFFFFu; | ||
|
||
|
@@ -12,7 +14,7 @@ struct Reservoir { | |
weight_sum: f32, | ||
confidence_weight: f32, | ||
unbiased_contribution_weight: f32, | ||
_padding: f32, | ||
visibility: f32, | ||
} | ||
|
||
fn empty_reservoir() -> Reservoir { | ||
|
@@ -28,3 +30,59 @@ fn empty_reservoir() -> Reservoir { | |
fn reservoir_valid(reservoir: Reservoir) -> bool { | ||
return reservoir.sample.light_id.x != NULL_RESERVOIR_SAMPLE; | ||
} | ||
|
||
struct ReservoirMergeResult { | ||
merged_reservoir: Reservoir, | ||
selected_sample_radiance: vec3<f32>, | ||
} | ||
|
||
fn merge_reservoirs( | ||
canonical_reservoir: Reservoir, | ||
other_reservoir: Reservoir, | ||
world_position: vec3<f32>, | ||
world_normal: vec3<f32>, | ||
diffuse_brdf: vec3<f32>, | ||
rng: ptr<function, u32>, | ||
) -> ReservoirMergeResult { | ||
// TODO: Balance heuristic MIS weights | ||
let mis_weight_denominator = 1.0 / (canonical_reservoir.confidence_weight + other_reservoir.confidence_weight); | ||
|
||
let canonical_mis_weight = canonical_reservoir.confidence_weight * mis_weight_denominator; | ||
let canonical_target_function = reservoir_target_function(canonical_reservoir, world_position, world_normal, diffuse_brdf); | ||
let canonical_resampling_weight = canonical_mis_weight * (canonical_target_function.a * canonical_reservoir.unbiased_contribution_weight); | ||
|
||
let other_mis_weight = other_reservoir.confidence_weight * mis_weight_denominator; | ||
let other_target_function = reservoir_target_function(other_reservoir, world_position, world_normal, diffuse_brdf); | ||
let other_resampling_weight = other_mis_weight * (other_target_function.a * other_reservoir.unbiased_contribution_weight); | ||
|
||
var combined_reservoir = empty_reservoir(); | ||
combined_reservoir.weight_sum = canonical_resampling_weight + other_resampling_weight; | ||
combined_reservoir.confidence_weight = canonical_reservoir.confidence_weight + other_reservoir.confidence_weight; | ||
|
||
// https://yusuketokuyoshi.com/papers/2024/Efficient_Visibility_Reuse_for_Real-time_ReSTIR_(Supplementary_Document).pdf | ||
combined_reservoir.visibility = max(0.0, (canonical_reservoir.visibility * canonical_resampling_weight | ||
+ other_reservoir.visibility * other_resampling_weight) / combined_reservoir.weight_sum); | ||
|
||
if rand_f(rng) < other_resampling_weight / combined_reservoir.weight_sum { | ||
combined_reservoir.sample = other_reservoir.sample; | ||
|
||
let inverse_target_function = select(0.0, 1.0 / other_target_function.a, other_target_function.a > 0.0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe extract a safe_invert function? |
||
combined_reservoir.unbiased_contribution_weight = combined_reservoir.weight_sum * inverse_target_function; | ||
|
||
return ReservoirMergeResult(combined_reservoir, other_target_function.rgb); | ||
} else { | ||
combined_reservoir.sample = canonical_reservoir.sample; | ||
|
||
let inverse_target_function = select(0.0, 1.0 / canonical_target_function.a, canonical_target_function.a > 0.0); | ||
combined_reservoir.unbiased_contribution_weight = combined_reservoir.weight_sum * inverse_target_function; | ||
|
||
return ReservoirMergeResult(combined_reservoir, canonical_target_function.rgb); | ||
} | ||
} | ||
|
||
fn reservoir_target_function(reservoir: Reservoir, world_position: vec3<f32>, world_normal: vec3<f32>, diffuse_brdf: vec3<f32>) -> vec4<f32> { | ||
if !reservoir_valid(reservoir) { return vec4(0.0); } | ||
let light_contribution = calculate_light_contribution(reservoir.sample, world_position, world_normal).radiance; | ||
let target_function = luminance(light_contribution * diffuse_brdf); | ||
return vec4(light_contribution, target_function); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know you want to double buffer, but we should also just make these more easily configurable in general to avoid this kind of thing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. The problem is that it's configured per-render target, and not per camera, but all our components live on cameras. For now I don't have any better idea :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if enabling this has any performance impact on eg mobile.