Skip to content

Commit d1095b0

Browse files
authored
Solari: Improve spatial sample finding (#22617)
Pink is visualizing pixels without a valid spatial sample (for ReSTIR GI). Before: <img width="3206" height="1875" alt="image" src="https://github.com/user-attachments/assets/0d336bd8-a20d-420c-97d8-50e3bb1309ab" /> After: <img width="3206" height="1875" alt="image" src="https://github.com/user-attachments/assets/50352242-b658-4a47-a5b8-1b061388a8de" />
1 parent 98fcc7e commit d1095b0

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

crates/bevy_solari/src/realtime/restir_di.wgsl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,15 @@ fn load_temporal_reservoir_inner(temporal_pixel_id: vec2<u32>, depth: f32, world
184184
}
185185

186186
fn load_spatial_reservoir(pixel_id: vec2<u32>, depth: f32, world_position: vec3<f32>, world_normal: vec3<f32>, rng: ptr<function, u32>) -> NeighborInfo {
187+
var search_radius = SPATIAL_REUSE_RADIUS_PIXELS;
187188
for (var i = 0u; i < 5u; i++) {
188-
let spatial_pixel_id = get_neighbor_pixel_id(pixel_id, rng);
189+
let spatial_pixel_id = get_neighbor_pixel_id(pixel_id, search_radius, rng);
189190

190191
let spatial_depth = textureLoad(depth_buffer, spatial_pixel_id, 0);
191192
let spatial_surface = gpixel_resolve(textureLoad(gbuffer, spatial_pixel_id, 0), spatial_depth, spatial_pixel_id, view.main_pass_viewport.zw, view.world_from_clip);
192193
let spatial_diffuse_brdf = spatial_surface.material.base_color / PI;
193194
if pixel_dissimilar(depth, world_position, spatial_surface.world_position, world_normal, spatial_surface.world_normal, view) {
195+
search_radius /= 2.0;
194196
continue;
195197
}
196198

@@ -201,8 +203,8 @@ fn load_spatial_reservoir(pixel_id: vec2<u32>, depth: f32, world_position: vec3<
201203
return NeighborInfo(empty_reservoir(), world_position, world_normal, vec3(0.0));
202204
}
203205

204-
fn get_neighbor_pixel_id(center_pixel_id: vec2<u32>, rng: ptr<function, u32>) -> vec2<u32> {
205-
var spatial_id = vec2<f32>(center_pixel_id) + sample_disk(SPATIAL_REUSE_RADIUS_PIXELS, rng);
206+
fn get_neighbor_pixel_id(center_pixel_id: vec2<u32>, search_radius: f32, rng: ptr<function, u32>) -> vec2<u32> {
207+
var spatial_id = vec2<f32>(center_pixel_id) + sample_disk(search_radius, rng);
206208
spatial_id = clamp(spatial_id, vec2(0.0), view.main_pass_viewport.zw - 1.0);
207209
return vec2<u32>(spatial_id);
208210
}

crates/bevy_solari/src/realtime/restir_gi.wgsl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,15 @@ fn load_temporal_reservoir_inner(temporal_pixel_id: vec2<u32>, depth: f32, world
150150
}
151151

152152
fn load_spatial_reservoir(pixel_id: vec2<u32>, depth: f32, world_position: vec3<f32>, world_normal: vec3<f32>, rng: ptr<function, u32>) -> NeighborInfo {
153+
var search_radius = SPATIAL_REUSE_RADIUS_PIXELS;
153154
for (var i = 0u; i < 5u; i++) {
154-
let spatial_pixel_id = get_neighbor_pixel_id(pixel_id, rng);
155+
let spatial_pixel_id = get_neighbor_pixel_id(pixel_id, search_radius, rng);
155156

156157
let spatial_depth = textureLoad(depth_buffer, spatial_pixel_id, 0);
157158
let spatial_surface = gpixel_resolve(textureLoad(gbuffer, spatial_pixel_id, 0), spatial_depth, spatial_pixel_id, view.main_pass_viewport.zw, view.world_from_clip);
158159
let spatial_diffuse_brdf = spatial_surface.material.base_color / PI;
159160
if pixel_dissimilar(depth, world_position, spatial_surface.world_position, world_normal, spatial_surface.world_normal, view) {
161+
search_radius /= 2.0;
160162
continue;
161163
}
162164

@@ -168,8 +170,8 @@ fn load_spatial_reservoir(pixel_id: vec2<u32>, depth: f32, world_position: vec3<
168170
return NeighborInfo(empty_reservoir(), world_position, world_normal, vec3(0.0));
169171
}
170172

171-
fn get_neighbor_pixel_id(center_pixel_id: vec2<u32>, rng: ptr<function, u32>) -> vec2<u32> {
172-
var spatial_id = vec2<f32>(center_pixel_id) + sample_disk(SPATIAL_REUSE_RADIUS_PIXELS, rng);
173+
fn get_neighbor_pixel_id(center_pixel_id: vec2<u32>, search_radius: f32, rng: ptr<function, u32>) -> vec2<u32> {
174+
var spatial_id = vec2<f32>(center_pixel_id) + sample_disk(search_radius, rng);
173175
spatial_id = clamp(spatial_id, vec2(0.0), view.main_pass_viewport.zw - 1.0);
174176
return vec2<u32>(spatial_id);
175177
}

0 commit comments

Comments
 (0)