Skip to content

Commit 3d6e838

Browse files
authored
Tweak Solari world cache (#20622)
# Objective - Make the world cache more responsive to changes in lighting - Make the world cache switch LODs slower to make the transition less noticeable - Make the base cell size smaller to improve close-up lighting
1 parent ad8dd57 commit 3d6e838

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

crates/bevy_solari/src/realtime/restir_gi.wgsl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#import bevy_core_pipeline::tonemapping::tonemapping_luminance as luminance
44
#import bevy_pbr::pbr_deferred_types::unpack_24bit_normal
55
#import bevy_pbr::prepass_bindings::PreviousViewUniforms
6-
#import bevy_pbr::rgb9e5::rgb9e5_to_vec3_
76
#import bevy_pbr::utils::{rand_f, sample_uniform_hemisphere, uniform_hemisphere_inverse_pdf, sample_disk, octahedral_decode}
87
#import bevy_render::maths::PI
98
#import bevy_render::view::View
@@ -82,6 +81,10 @@ fn spatial_and_shade(@builtin(global_invocation_id) global_id: vec3<u32>) {
8281
var pixel_color = textureLoad(view_output, global_id.xy);
8382
pixel_color += vec4(merge_result.selected_sample_radiance * combined_reservoir.unbiased_contribution_weight * view.exposure, 0.0);
8483
textureStore(view_output, global_id.xy, pixel_color);
84+
85+
#ifdef VISUALIZE_WORLD_CACHE
86+
textureStore(view_output, global_id.xy, vec4(query_world_cache(world_position, world_normal, view.world_position) * view.exposure, 1.0));
87+
#endif
8588
}
8689

8790
fn generate_initial_reservoir(world_position: vec3<f32>, world_normal: vec3<f32>, rng: ptr<function, u32>) -> Reservoir {

crates/bevy_solari/src/realtime/world_cache_query.wgsl

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
#define_import_path bevy_solari::world_cache
22

3-
/// Controls how response the world cache is to changes in lighting
4-
const WORLD_CACHE_MAX_TEMPORAL_SAMPLES: f32 = 30.0;
3+
/// How responsive the world cache is to changes in lighting (higher is less responsive, lower is more responsive)
4+
const WORLD_CACHE_MAX_TEMPORAL_SAMPLES: f32 = 10.0;
55
/// Maximum amount of frames a cell can live for without being queried
66
const WORLD_CACHE_CELL_LIFETIME: u32 = 30u;
77
/// Maximum amount of attempts to find a cache entry after a hash collision
88
const WORLD_CACHE_MAX_SEARCH_STEPS: u32 = 3u;
99

10-
/// Controls the base size of each cache cell
11-
const WORLD_CACHE_POSITION_BASE_CELL_SIZE: f32 = 0.4;
10+
/// The size of a cache cell at the lowest LOD in meters
11+
const WORLD_CACHE_POSITION_BASE_CELL_SIZE: f32 = 0.25;
12+
/// How fast the world cache transitions between LODs as a function of distance to the camera
13+
const WORLD_CACHE_POSITION_LOD_SCALE: f32 = 30.0;
1214

1315
/// Marker value for an empty cell
1416
const WORLD_CACHE_EMPTY_CELL: u32 = 0u;
@@ -36,9 +38,9 @@ struct WorldCacheGeometryData {
3638

3739
#ifndef WORLD_CACHE_NON_ATOMIC_LIFE_BUFFER
3840
fn query_world_cache(world_position: vec3<f32>, world_normal: vec3<f32>, view_position: vec3<f32>) -> vec3<f32> {
39-
let world_position_quantized = bitcast<vec3<u32>>(quantize_position(world_position, view_position));
41+
let cell_size = get_cell_size(world_position, view_position);
42+
let world_position_quantized = bitcast<vec3<u32>>(quantize_position(world_position, cell_size));
4043
let world_normal_quantized = bitcast<vec3<u32>>(quantize_normal(world_normal));
41-
4244
var key = compute_key(world_position_quantized, world_normal_quantized);
4345
let checksum = compute_checksum(world_position_quantized, world_normal_quantized);
4446

@@ -64,12 +66,13 @@ fn query_world_cache(world_position: vec3<f32>, world_normal: vec3<f32>, view_po
6466
}
6567
#endif
6668

67-
fn quantize_position(world_position: vec3<f32>, view_position: vec3<f32>) -> vec3<f32> {
68-
let base_size = WORLD_CACHE_POSITION_BASE_CELL_SIZE;
69-
let d = distance(view_position, world_position);
70-
let step = max((d * base_size) / 7.0, base_size);
71-
let quantization_factor = exp2(floor(log2(step)));
69+
fn get_cell_size(world_position: vec3<f32>, view_position: vec3<f32>) -> f32 {
70+
let camera_distance = distance(view_position, world_position) / WORLD_CACHE_POSITION_LOD_SCALE;
71+
let lod = exp2(floor(log2(1.0 + camera_distance)));
72+
return WORLD_CACHE_POSITION_BASE_CELL_SIZE * lod;
73+
}
7274

75+
fn quantize_position(world_position: vec3<f32>, quantization_factor: f32) -> vec3<f32> {
7376
return floor(world_position / quantization_factor + 0.0001);
7477
}
7578

release-content/release-notes/bevy_solari.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Initial raytraced lighting progress (bevy_solari)
33
authors: ["@JMS55", "@SparkyPotato"]
4-
pull_requests: [19058, 19620, 19790, 20020, 20113, 20156, 20213, 20242, 20259, 20406, 20457, 20580, 20596]
4+
pull_requests: [19058, 19620, 19790, 20020, 20113, 20156, 20213, 20242, 20259, 20406, 20457, 20580, 20596, 20622]
55
---
66

77
## Overview

0 commit comments

Comments
 (0)