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 world cache: the lights' light sampled where a path ends, cells keyed by their surface's plane and size, a miss no longer a zero

shinyobjectzfc7edcdc49de
diff --git a/vendor/bevy_solari/src/realtime/initial_path.wesl b/vendor/bevy_solari/src/realtime/initial_path.wesl
index 71bba079..50a35f73 100644
--- a/vendor/bevy_solari/src/realtime/initial_path.wesl
+++ b/vendor/bevy_solari/src/realtime/initial_path.wesl
@@ -7,7 +7,7 @@ import package::realtime::presample_light_tiles::unpack_resolved_light_sample;
import package::realtime::bindings::{empty_reservoir, light_tile_resolved_samples, light_tile_samples, Reservoir, constants, view};
import package::scene::sampling::{calculate_resolved_light_contribution, isinf, LightSample, NULL_LIGHT_ID, power_heuristic, trace_visibility, sample_environment_map_light_and_sun};
import package::scene::bindings::{light_sources, MIRROR_ROUGHNESS_THRESHOLD, RAY_T_MAX, RAY_T_MIN, resolve_ray_hit_full, ResolvedMaterial, ResolvedRayHitFull, trace_ray};
-import package::realtime::world_cache_query::{get_cell_size, query_world_cache, WORLD_CACHE_CELL_LIFETIME};
+import package::realtime::world_cache_query::{get_cell_size, query_world_cache, query_world_cache_found, WORLD_CACHE_CELL_LIFETIME};
@if(DLSS_RR_GUIDE_BUFFERS)
import package::realtime::resolve_dlss_rr_textures::{is_delta_mirror, primary_surface_glossy, psr_bounce_reflectance, psr_chain_mirror, psr_environment_miss, psr_init, replace_primary_surface};
@@ -174,7 +174,7 @@ fn generate_initial_path(world_position: vec3<f32>, world_normal: vec3<f32>, mat
}
// Try terminating into the world cache
- if terminate_into_cache(&path, ray_hit, ray.t, p_brdf, bounce, rng) {
+ if terminate_into_cache(&path, ray_hit, next_bounce.wi, ray.t, p_brdf, bounce, workgroup_id, rng) {
break;
}
@@ -406,9 +406,11 @@ fn generate_environment_map_light_candidate(
fn terminate_into_cache(
path: ptr<function, PathState>,
ray_hit: ResolvedRayHitFull,
+ wi: vec3<f32>,
ray_t: f32,
p_brdf: f32,
bounce: u32,
+ workgroup_id: vec2<u32>,
rng: ptr<function, u32>,
) -> bool {
// Only terminate into the world cache when the bounce was from a wide-enough BRDF sample
@@ -425,9 +427,22 @@ fn terminate_into_cache(
let world_cache_cell_size = get_cell_size(ray_hit.world_position, view.world_position, ray_t, &rng_copy);
if ray_t <= sqrt(3.0) * world_cache_cell_size { return false; }
- let cached_radiance = query_world_cache(ray_hit.world_position, ray_hit.geometric_world_normal, view.world_position, ray_t, WORLD_CACHE_CELL_LIFETIME, rng);
-
- let cache_outgoing = (ray_hit.material.base_color / PI) * cached_radiance;
+ // raea: a miss continues the path rather than end it on a zero
+ let cached = query_world_cache_found(ray_hit.world_position, ray_hit.geometric_world_normal, view.world_position, ray_t, WORLD_CACHE_CELL_LIFETIME, rng);
+ if cached.w == 0.0 { return false; }
+ let cached_radiance = cached.rgb;
+
+ // raea: the cache holds only light that has bounced (and the sky's, which no light sample finds); the lights' own
+ // light on the hit is sampled at the hit with its full BRDF, alone since the path ends here (world_cache_update.wesl).
+ // With as many candidates as the cache took for it (world_cache_direct_light_sample_count): with secondary_di_samples
+ // (2) the sun on a sunlit desk, picked among photo 2's lights, came in spikes the denoiser dropped (0.79 of the path
+ // tracer's light, from 0.86)
+ let hit_wo = -wi;
+ let hit_F_ab = F_AB(ray_hit.material.perceptual_roughness, max(dot(ray_hit.world_normal, hit_wo), 0.0001));
+ let hit_origin = ray_hit.world_position + (ray_hit.geometric_world_normal * RAY_T_MIN);
+ let di = sample_light_ris(hit_origin, ray_hit.world_normal, hit_wo, ray_hit.material, hit_F_ab, max(constants.world_cache_direct_light_sample_count, 1u), workgroup_id, bounce + 1u, rng);
+
+ let cache_outgoing = di.brdf_radiance * di.unbiased_contribution_weight + (ray_hit.material.base_color / PI) * cached_radiance;
let cache_L_at_reconnection = (*path).throughput_past_first_hit * cache_outgoing;
@if(RESTIR) {
diff --git a/vendor/bevy_solari/src/realtime/world_cache_query.wesl b/vendor/bevy_solari/src/realtime/world_cache_query.wesl
index 02a6b8c0..a01d85ff 100644
--- a/vendor/bevy_solari/src/realtime/world_cache_query.wesl
+++ b/vendor/bevy_solari/src/realtime/world_cache_query.wesl
@@ -18,6 +18,14 @@ const WORLD_CACHE_EMPTY_CELL: u32 = 0u;
@if(!WORLD_CACHE_NON_ATOMIC_LIFE_BUFFER)
fn query_world_cache(world_position_in: vec3<f32>, world_normal: vec3<f32>, view_position: vec3<f32>, ray_t: f32, cell_lifetime: u32, rng: ptr<function, u32>) -> vec3<f32> {
+ return query_world_cache_found(world_position_in, world_normal, view_position, ray_t, cell_lifetime, rng).rgb;
+}
+
+/// raea: the cell's light, and in w 1 when the cell holds light, 0 when the query found none: the cell was made by this
+/// query, has had no update yet, or is lost to collisions. Read as light, such a miss is a zero, and a lamp shade's
+/// lining, whose bounces chain from cell to cell, lost a third of them (photo 1's desk, 0.82 of the path tracer's)
+@if(!WORLD_CACHE_NON_ATOMIC_LIFE_BUFFER)
+fn query_world_cache_found(world_position_in: vec3<f32>, world_normal: vec3<f32>, view_position: vec3<f32>, ray_t: f32, cell_lifetime: u32, rng: ptr<function, u32>) -> vec4<f32> {
var world_position = world_position_in;
var cell_size = get_cell_size(world_position, view_position, ray_t, rng);
@@ -32,8 +40,15 @@ fn query_world_cache(world_position_in: vec3<f32>, world_normal: vec3<f32>, view
let world_position_quantized = bitcast<vec3<u32>>(quantize_position(world_position, cell_size));
let world_normal_quantized = bitcast<vec3<u32>>(quantize_normal(world_normal));
- var key = compute_key(world_position_quantized, world_normal_quantized);
- let checksum = compute_checksum(world_position_quantized, world_normal_quantized);
+ // raea: the plane the surface lies in, along its normal, in quarter cells, and the cell's size, are part of a
+ // cell's key. Without the plane, surfaces facing the same way across a cavity the size of a cell (a lamp shade's
+ // lining on one side, its dark cladding on the other) shared cells, and the lining lost a sixth of its light
+ // (LightBench emitter.tsx cavity-8: 0.84 of the path tracer's at 15 cm cells). The jitter stays in the tangent
+ // plane, so a flat surface keeps one plane. Without the size, cells of two LOD levels could share a key
+ let plane = bitcast<u32>(i32(floor(dot(world_position, world_normal) / (cell_size * 0.25))));
+ let level = bitcast<u32>(cell_size);
+ var key = compute_key(world_position_quantized, world_normal_quantized, plane, level);
+ let checksum = compute_checksum(world_position_quantized, world_normal_quantized, plane, level);
for (var i = 0u; i < WORLD_CACHE_MAX_SEARCH_STEPS; i++) {
let cas = atomicCompareExchangeWeak(&world_cache.checksums[key], WORLD_CACHE_EMPTY_CELL, checksum);
@@ -49,19 +64,20 @@ fn query_world_cache(world_position_in: vec3<f32>, world_normal: vec3<f32>, view
if existing_checksum == checksum {
// Cache entry already exists - get radiance
- return world_cache.radiance[key].rgb;
+ let radiance = world_cache.radiance[key];
+ return vec4(radiance.rgb, select(0.0, 1.0, radiance.a > 0.0));
} else if existing_checksum == WORLD_CACHE_EMPTY_CELL && cas.exchanged {
// Cell is empty - initialize it
world_cache.geometry_data[key].world_position = world_position;
world_cache.geometry_data[key].world_normal = world_normal;
- return vec3(0.0);
+ return vec4(0.0);
} else {
// Collision - linear probe to next entry
key += 1u;
}
}
- return vec3(0.0);
+ return vec4(0.0);
}
fn get_cell_size(world_position: vec3<f32>, view_position: vec3<f32>, ray_t: f32, rng: ptr<function, u32>) -> f32 {
@@ -80,23 +96,27 @@ fn quantize_normal(world_normal: vec3<f32>) -> vec3<f32> {
return floor(world_normal + 0.0001);
}
-fn compute_key(world_position: vec3<u32>, world_normal: vec3<u32>) -> u32 {
+fn compute_key(world_position: vec3<u32>, world_normal: vec3<u32>, plane: u32, level: u32) -> u32 {
var key = pcg_hash(world_position.x);
key = pcg_hash(key + world_position.y);
key = pcg_hash(key + world_position.z);
key = pcg_hash(key + world_normal.x);
key = pcg_hash(key + world_normal.y);
key = pcg_hash(key + world_normal.z);
+ key = pcg_hash(key + plane);
+ key = pcg_hash(key + level);
return wrap_key(key);
}
-fn compute_checksum(world_position: vec3<u32>, world_normal: vec3<u32>) -> u32 {
+fn compute_checksum(world_position: vec3<u32>, world_normal: vec3<u32>, plane: u32, level: u32) -> u32 {
var key = iqint_hash(world_position.x);
key = iqint_hash(key + world_position.y);
key = iqint_hash(key + world_position.z);
key = iqint_hash(key + world_normal.x);
key = iqint_hash(key + world_normal.y);
key = iqint_hash(key + world_normal.z);
+ key = iqint_hash(key + plane);
+ key = iqint_hash(key + level);
return max(key, 1u); // 0u is reserved for WORLD_CACHE_EMPTY_CELL
}
diff --git a/vendor/bevy_solari/src/realtime/world_cache_update.wesl b/vendor/bevy_solari/src/realtime/world_cache_update.wesl
index d04b07f6..c749f9da 100644
--- a/vendor/bevy_solari/src/realtime/world_cache_update.wesl
+++ b/vendor/bevy_solari/src/realtime/world_cache_update.wesl
@@ -4,7 +4,7 @@ import bevy_render::maths::PI;
import package::realtime::presample_light_tiles::unpack_resolved_light_sample;
import package::scene::sampling::{calculate_resolved_light_contribution, trace_visibility, sample_environment_map_light};
import package::scene::bindings::{trace_ray, resolve_ray_hit_full, RAY_T_MIN};
-import package::realtime::world_cache_query::query_world_cache;
+import package::realtime::world_cache_query::query_world_cache_found;
import package::realtime::bindings::{
light_tile_resolved_samples,
view,
@@ -24,9 +24,11 @@ fn sample_di(@builtin(workgroup_id) workgroup_id: vec3<u32>, @builtin(global_inv
if rand_f(&rng) >= f32(constants.world_cache_cell_updates_soft_target) / f32(world_cache.active_cells_count) { return; }
- let new_radiance = sample_random_light_ris(geometry_data.world_position, geometry_data.world_normal, workgroup_id.xy, &rng);
-
- world_cache.active_cells_new_radiance[active_cell_id.x] = new_radiance;
+ // raea: a cell holds only the light that has bounced (and the sky's), so it starts from nothing here. What the lights
+ // put on a surface is sampled where a path ends (initial_path.wesl terminate_into_cache) and where sample_gi's ray
+ // lands, at the very point: taken at one point for a whole cell, it lost half the light a shade's liner throws
+ // from the bulb a few cm off (LightBench's emitter.tsx bounce test: 0.48 of the path tracer's at a 4 cm gap)
+ world_cache.active_cells_new_radiance[active_cell_id.x] = vec3(0.0);
}
@compute @workgroup_size(64, 1, 1)
@@ -44,8 +46,17 @@ fn sample_gi(@builtin(workgroup_id) workgroup_id: vec3<u32>, @builtin(global_inv
if ray.kind != RAY_QUERY_INTERSECTION_NONE {
let ray_hit = resolve_ray_hit_full(ray);
let cell_life = atomicLoad(&world_cache.life[cell_index]);
- let radiance = query_world_cache(ray_hit.world_position, ray_hit.geometric_world_normal, view.world_position, ray.t, cell_life, &rng);
- world_cache.active_cells_new_radiance[active_cell_id.x] += ray_hit.material.base_color * radiance;
+ let cached = query_world_cache_found(ray_hit.world_position, ray_hit.geometric_world_normal, view.world_position, ray.t, cell_life, &rng);
+ // raea: a hit whose cell holds no light yet is no sample for a cell that has light: this update is skipped
+ // (blend_new_samples), not blended in as a zero, which cut the chain of bounces inside a cavity. A cell with
+ // none yet takes it (the lights' light at the hit alone), or a room with no sky would never fill
+ if cached.w == 0.0 && world_cache.radiance[cell_index].a > 0.0 {
+ world_cache.active_cells_new_radiance[active_cell_id.x] = vec3(-1.0);
+ return;
+ }
+ // raea: the lights' own light on the surface the ray found, sampled there, with what has bounced onto it
+ let direct = sample_random_light_ris(ray_hit.world_position, ray_hit.geometric_world_normal, workgroup_id.xy, &rng);
+ world_cache.active_cells_new_radiance[active_cell_id.x] += ray_hit.material.base_color * (direct + cached.rgb);
} else {
// The ray is cut short at world_cache_max_gi_ray_distance to save on traversal costs.
// A miss here may actually have been a surface further away if we kept tracing, and not actually have been a miss.
@@ -65,6 +76,7 @@ fn blend_new_samples(@builtin(global_invocation_id) active_cell_id: vec3<u32>) {
let old_radiance = world_cache.radiance[cell_index];
let new_radiance = world_cache.active_cells_new_radiance[active_cell_id.x];
+ if new_radiance.x < 0.0 { return; } // raea: sample_gi's ray found no light in the cache; no sample this frame
let luminance_delta = world_cache.luminance_deltas[cell_index];
// https://bsky.app/profile/gboisse.bsky.social/post/3m5blga3ftk2a