Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

Solari: SolariLighting::first_bounce_probability, Russian roulette on the first bounce by workgroup

shinyobjectz4cec0645fbd3
diff --git a/vendor/bevy_solari/src/realtime/bindings.wesl b/vendor/bevy_solari/src/realtime/bindings.wesl
index 86e83e8b..97ad5b96 100644
--- a/vendor/bevy_solari/src/realtime/bindings.wesl
+++ b/vendor/bevy_solari/src/realtime/bindings.wesl
@@ -55,6 +55,11 @@ struct SolariLightingSettings {
world_cache_position_lod_scale: f32,
frame_rng: u32,
reset: u32,
+ // raea: SolariLighting::first_bounce_probability (prepare.rs), and padding
+ first_bounce_probability: f32,
+ pad0: u32,
+ pad1: u32,
+ pad2: u32,
}
/// Don't adjust the size of this struct without also adjusting `prepare::RESOLVED_LIGHT_SAMPLE_STRUCT_SIZE`.
diff --git a/vendor/bevy_solari/src/realtime/initial_path.wesl b/vendor/bevy_solari/src/realtime/initial_path.wesl
index 9654fcdb..a0ca2da0 100644
--- a/vendor/bevy_solari/src/realtime/initial_path.wesl
+++ b/vendor/bevy_solari/src/realtime/initial_path.wesl
@@ -100,9 +100,18 @@ fn generate_initial_path(world_position: vec3<f32>, world_normal: vec3<f32>, mat
let di_samples = select(constants.secondary_di_samples, constants.primary_di_samples, bounce == 0u);
generate_nee_candidate(&path, F_ab, p_nee, di_samples, workgroup_id, bounce, rng);
+ // raea: Russian roulette on the first bounce (SolariLighting::first_bounce_probability): a workgroup (8 x 8
+ // pixels) traces it with probability q, and its light counts 1 / q (in the throughput every bounce-0 candidate
+ // is weighted by), so the estimate keeps its mean; a skipped pixel keeps its primary light sample and takes its
+ // bounce from reuse. The coin is the workgroup's, not each pixel's: a SIMD group waits for its slowest ray, so
+ // pixels skipped at random saved little (photo 3's forest, q 0.5: 10.25 -> 8.97 ms). At 1 nothing is drawn.
+ let q = constants.first_bounce_probability;
+ if bounce == 0u && q < 1.0 && first_bounce_coin(workgroup_id) >= q { break; }
+
// Sample the BRDF and trace the next ray
- let next_bounce = evaluate_and_sample_brdf(path.wo, path.normal, path.material, F_ab, rng);
+ var next_bounce = evaluate_and_sample_brdf(path.wo, path.normal, path.material, F_ab, rng);
if next_bounce.pdf == 0.0 { break; }
+ if bounce == 0u && q < 1.0 { next_bounce.throughput /= q; }
let ray = trace_ray(path.ray_origin, next_bounce.wi, RAY_T_MIN, RAY_T_MAX, RAY_FLAG_NONE);
if ray.kind == RAY_QUERY_INTERSECTION_NONE {
@@ -210,6 +219,13 @@ fn generate_initial_path(world_position: vec3<f32>, world_normal: vec3<f32>, mat
return result;
}
+// raea: one draw in [0, 1) for a whole workgroup this frame (first_bounce_probability's coin)
+fn first_bounce_coin(workgroup_id: vec2<u32>) -> f32 {
+ var wg_rng = (workgroup_id.x * 5782582u) ^ (workgroup_id.y * 1103515245u) ^ constants.frame_rng;
+ wg_rng = wg_rng * 747796405u + 2891336453u;
+ return rand_f(&wg_rng);
+}
+
fn generate_nee_candidate(
path: ptr<function, PathState>,
F_ab: vec2<f32>,
diff --git a/vendor/bevy_solari/src/realtime/mod.rs b/vendor/bevy_solari/src/realtime/mod.rs
index f8016e7c..99130a30 100644
--- a/vendor/bevy_solari/src/realtime/mod.rs
+++ b/vendor/bevy_solari/src/realtime/mod.rs
@@ -156,6 +156,13 @@ pub struct SolariLighting {
/// multi-bounce lighting for specular paths.
pub max_bounces: u32,
+ /// raea: the chance a pixel traces its first bounce this frame (Russian roulette on it). The bounce's light is
+ /// divided by it, so the picture keeps its light on average, and ReSTIR's reuse spreads what a pixel skipped from
+ /// its neighbours and last frames. The bounce is most of the frame's tracing in dense foliage: 0.5 halves it.
+ ///
+ /// Defaults to `1.0` (every pixel, every frame).
+ pub first_bounce_probability: f32,
+
/// How responsive the world cache is to changes in lighting.
///
/// Higher values accumulate more temporal history, giving more stable but
@@ -227,6 +234,7 @@ impl Default for SolariLighting {
primary_di_samples: 8,
secondary_di_samples: 4,
max_bounces: 3,
+ first_bounce_probability: 1.0,
world_cache_max_temporal_samples: 32.0,
world_cache_direct_light_sample_count: 32,
world_cache_max_gi_ray_distance: 50.0,
diff --git a/vendor/bevy_solari/src/realtime/prepare.rs b/vendor/bevy_solari/src/realtime/prepare.rs
index 95f52f64..43404865 100644
--- a/vendor/bevy_solari/src/realtime/prepare.rs
+++ b/vendor/bevy_solari/src/realtime/prepare.rs
@@ -75,6 +75,9 @@ struct SolariLightingUniforms {
world_cache_position_lod_scale: f32,
frame_rng: u32,
reset: u32,
+ // raea: SolariLighting::first_bounce_probability, padded to keep the uniform a multiple of 16 bytes
+ first_bounce_probability: f32,
+ pad: [u32; 3],
}
impl SolariLightingUniforms {
@@ -92,6 +95,8 @@ impl SolariLightingUniforms {
world_cache_position_lod_scale: settings.world_cache_position_lod_scale,
frame_rng: frame_count.wrapping_mul(5782582),
reset: (settings.reset || force_reset) as u32,
+ first_bounce_probability: settings.first_bounce_probability.clamp(0.05, 1.0),
+ pad: [0; 3],
}
}
}