Skip to content

Commit 64cb1e7

Browse files
committed
LightmapGI: Pack L1 SH coefficients before denoising
1 parent 0fdbf05 commit 64cb1e7

File tree

3 files changed

+26
-31
lines changed

3 files changed

+26
-31
lines changed

modules/lightmapper_rd/lightmapper_rd.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,7 @@ LightmapperRD::BakeError LightmapperRD::_denoise(RenderingDevice *p_rd, Ref<RDSh
980980
denoise_params.normal_bandwidth = 0.1f;
981981
denoise_params.filter_strength = 10.0f;
982982
denoise_params.half_search_window = p_denoiser_range;
983+
denoise_params.slice_count = p_bake_sh ? 4 : 1;
983984
p_rd->buffer_update(denoise_params_buffer, 0, sizeof(DenoiseParams), &denoise_params);
984985

985986
Vector<RD::Uniform> uniforms = dilate_or_denoise_common_uniforms(p_source_light_tex, p_dest_light_tex);
@@ -2065,6 +2066,14 @@ LightmapperRD::BakeError LightmapperRD::bake(BakeQuality p_quality, bool p_use_d
20652066

20662067
/* DENOISE */
20672068

2069+
if (p_bake_sh) {
2070+
SWAP(light_accum_tex, light_accum_tex2);
2071+
BakeError error = _pack_l1(rd, compute_shader, compute_base_uniform_set, push_constant, light_accum_tex2, light_accum_tex, atlas_size, atlas_slices);
2072+
if (unlikely(error != BAKE_OK)) {
2073+
return error;
2074+
}
2075+
}
2076+
20682077
if (p_use_denoiser) {
20692078
if (p_step_function) {
20702079
if (p_step_function(0.8, RTR("Denoising"), p_bake_userdata, true)) {
@@ -2290,14 +2299,6 @@ LightmapperRD::BakeError LightmapperRD::bake(BakeQuality p_quality, bool p_use_d
22902299
}
22912300
}
22922301

2293-
if (p_bake_sh) {
2294-
SWAP(light_accum_tex, light_accum_tex2);
2295-
BakeError error = _pack_l1(rd, compute_shader, compute_base_uniform_set, push_constant, light_accum_tex2, light_accum_tex, atlas_size, atlas_slices);
2296-
if (unlikely(error != BAKE_OK)) {
2297-
return error;
2298-
}
2299-
}
2300-
23012302
#ifdef DEBUG_TEXTURES
23022303

23032304
for (int i = 0; i < atlas_slices * (p_bake_sh ? 4 : 1); i++) {

modules/lightmapper_rd/lightmapper_rd.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,8 @@ class LightmapperRD : public Lightmapper {
281281

282282
int half_search_window;
283283
float filter_strength;
284-
float pad[2];
284+
uint32_t slice_count;
285+
uint32_t pad;
285286
};
286287

287288
BakeError _blit_meshes_into_atlas(int p_max_texture_size, int p_denoiser_range, Vector<Ref<Image>> &albedo_images, Vector<Ref<Image>> &emission_images, AABB &bounds, Size2i &atlas_size, int &atlas_slices, float p_supersampling_factor, BakeStepFunc p_step_function, void *p_bake_userdata);

modules/lightmapper_rd/lm_compute.glsl

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ layout(set = 1, binding = 4) uniform DenoiseParams {
8282

8383
int half_search_window;
8484
float filter_strength;
85+
uint slice_count;
8586
}
8687
denoise_params;
8788
#endif
@@ -1193,13 +1194,9 @@ void main() {
11931194
const float FILTER_SQUARE_TWO_SIGMA_LIGHT_SQUARE = FILTER_VALUE * FILTER_VALUE * TWO_SIGMA_LIGHT_SQUARE;
11941195
const float EPSILON = 1e-6f;
11951196

1196-
#ifdef USE_SH_LIGHTMAPS
1197-
const uint slice_count = 4;
1197+
const uint slice_count = denoise_params.slice_count;
11981198
const uint slice_base = params.atlas_slice * slice_count;
1199-
#else
1200-
const uint slice_count = 1;
1201-
const uint slice_base = params.atlas_slice;
1202-
#endif
1199+
const bool is_directional = (slice_count == 4);
12031200

12041201
for (uint i = 0; i < slice_count; i++) {
12051202
uint lightmap_slice = slice_base + i;
@@ -1223,9 +1220,16 @@ void main() {
12231220
for (int offset_x = -HALF_PATCH_WINDOW; offset_x <= HALF_PATCH_WINDOW; offset_x++) {
12241221
ivec2 offset_input_pos = atlas_pos + ivec2(offset_x, offset_y);
12251222
ivec2 offset_search_pos = search_pos + ivec2(offset_x, offset_y);
1226-
vec3 offset_input_rgb = texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(offset_input_pos, lightmap_slice), 0).rgb;
1227-
vec3 offset_search_rgb = texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(offset_search_pos, lightmap_slice), 0).rgb;
1223+
vec3 offset_input_rgb = texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(offset_input_pos, slice_base), 0).rgb;
1224+
vec3 offset_search_rgb = texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(offset_search_pos, slice_base), 0).rgb;
12281225
vec3 offset_delta_rgb = offset_input_rgb - offset_search_rgb;
1226+
1227+
if (is_directional) {
1228+
// Since L0 data is 1/4 the value of a regular lightmap,
1229+
// we have to multiply it by 4.
1230+
offset_delta_rgb *= 4.0;
1231+
}
1232+
12291233
patch_square_dist += dot(offset_delta_rgb, offset_delta_rgb) - TWO_SIGMA_LIGHT_SQUARE;
12301234
}
12311235
}
@@ -1280,24 +1284,13 @@ void main() {
12801284

12811285
#ifdef MODE_PACK_L1_COEFFS
12821286
vec4 base_coeff = texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(atlas_pos, params.atlas_slice * 4), 0);
1287+
imageStore(dest_light, ivec3(atlas_pos, params.atlas_slice * 4), base_coeff);
12831288

12841289
for (int i = 1; i < 4; i++) {
12851290
vec4 c = texelFetch(sampler2DArray(source_light, linear_sampler), ivec3(atlas_pos, params.atlas_slice * 4 + i), 0);
1291+
c.rgb /= (base_coeff.rgb * 8.0 + vec3(1e-6f));
1292+
c.rgb = clamp(c.rgb + vec3(0.5), vec3(0.0), vec3(1.0));
12861293

1287-
if (abs(base_coeff.r) > 0.0) {
1288-
c.r /= (base_coeff.r * 8);
1289-
}
1290-
1291-
if (abs(base_coeff.g) > 0.0) {
1292-
c.g /= (base_coeff.g * 8);
1293-
}
1294-
1295-
if (abs(base_coeff.b) > 0.0) {
1296-
c.b /= (base_coeff.b * 8);
1297-
}
1298-
1299-
c.rgb += vec3(0.5);
1300-
c.rgb = clamp(c.rgb, vec3(0.0), vec3(1.0));
13011294
imageStore(dest_light, ivec3(atlas_pos, params.atlas_slice * 4 + i), c);
13021295
}
13031296
#endif

0 commit comments

Comments
 (0)