Skip to content

Commit 9dd6c4d

Browse files
committed
Merge pull request #112927 from allenwp/fix_clamping_in_tonemap_mobile
Fix inconsistent color clamping between Mobile and Forward+.
2 parents f34c270 + 2c6749a commit 9dd6c4d

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

servers/rendering/renderer_rd/shaders/effects/tonemap.glsl

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -862,17 +862,14 @@ void main() {
862862
}
863863

864864
if (bool(params.flags & FLAG_USE_GLOW) && params.glow_mode != GLOW_MODE_SOFTLIGHT) {
865-
vec3 glow = gather_glow(source_glow, uv_interp);
865+
vec3 glow = gather_glow(source_glow, uv_interp) * params.glow_intensity;
866+
if (params.glow_map_strength > 0.001) {
867+
glow = mix(glow, texture(glow_map, uv_interp).rgb * glow, params.glow_map_strength);
868+
}
869+
866870
if (params.glow_mode == GLOW_MODE_MIX) {
867-
if (params.glow_map_strength > 0.001) {
868-
glow = mix(glow, texture(glow_map, uv_interp).rgb * glow, params.glow_map_strength);
869-
}
870-
color.rgb = mix(color.rgb, glow, params.glow_intensity);
871+
color.rgb = color.rgb * (1.0 - params.glow_intensity) + glow;
871872
} else {
872-
glow = glow * params.glow_intensity;
873-
if (params.glow_map_strength > 0.001) {
874-
glow = mix(glow, texture(glow_map, uv_interp).rgb * glow, params.glow_map_strength);
875-
}
876873
color.rgb = apply_glow(color.rgb, glow, params.white);
877874
}
878875
}
@@ -887,8 +884,7 @@ void main() {
887884
// Apply soft light after tonemapping to mitigate the issue of discontinuity
888885
// at 1.0 and higher. This makes the issue only appear with HDR output that
889886
// can exceed a 1.0 output value.
890-
vec3 glow = gather_glow(source_glow, uv_interp);
891-
glow = glow * params.glow_intensity;
887+
vec3 glow = gather_glow(source_glow, uv_interp) * params.glow_intensity;
892888
if (params.glow_map_strength > 0.001) {
893889
glow = mix(glow, texture(glow_map, uv_interp).rgb * glow, params.glow_map_strength);
894890
}

servers/rendering/renderer_rd/shaders/effects/tonemap_mobile.glsl

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,6 @@ vec3 tonemap_agx(vec3 color) {
225225
}
226226

227227
vec3 linear_to_srgb(vec3 color) {
228-
// Clamping is not strictly necessary for floating point nonlinear sRGB encoding,
229-
// but many cases that call this function need the result clamped.
230-
color = clamp(color, vec3(0.0), vec3(1.0));
231228
const vec3 a = vec3(0.055f);
232229
return mix((vec3(1.0f) + a) * pow(color.rgb, vec3(1.0f / 2.4f)) - a, 12.92f * color.rgb, lessThan(color.rgb, vec3(0.0031308f)));
233230
}
@@ -248,20 +245,20 @@ vec3 apply_tonemapping(vec3 color, float white) { // inputs are LINEAR
248245
return tonemap_filmic(max(vec3(0.0f), color), white);
249246
} else if (tonemapper_aces) {
250247
return tonemap_aces(max(vec3(0.0f), color), white);
251-
} else { // FLAG_TONEMAPPER_AGX
248+
} else { // tonemapper_agx
252249
return tonemap_agx(color);
253250
}
254251
}
255252

256253
#ifdef USE_MULTIVIEW
257254
vec3 gather_glow() {
258255
vec2 uv = gl_FragCoord.xy * params.dest_pixel_size;
259-
return textureLod(source_glow, vec3(uv, ViewIndex), 0.0).rgb;
256+
return textureLod(source_glow, vec3(uv, ViewIndex), 0.0).rgb * params.luminance_multiplier;
260257
}
261258
#else
262259
vec3 gather_glow() {
263260
vec2 uv = gl_FragCoord.xy * params.dest_pixel_size;
264-
return textureLod(source_glow, uv, 0.0).rgb;
261+
return textureLod(source_glow, uv, 0.0).rgb * params.luminance_multiplier;
265262
}
266263
#endif // !USE_MULTIVIEW
267264

@@ -728,15 +725,15 @@ void main() {
728725

729726
color.rgb *= params.exposure;
730727

731-
// Early Tonemap & SRGB Conversion
732728
#ifndef SUBPASS
729+
// Single-pass FXAA and pre-tonemap glow.
733730
if (use_fxaa) {
734731
// FXAA must be performed before glow to preserve the "bleed" effect of glow.
735732
color.rgb = do_fxaa(color.rgb, params.exposure, uv_interp);
736733
}
737734

738735
if (use_glow && !glow_mode_softlight) {
739-
vec3 glow = gather_glow() * params.luminance_multiplier * params.glow_intensity;
736+
vec3 glow = gather_glow() * params.glow_intensity;
740737
if (use_glow_map) {
741738
glow = mix(glow, texture(glow_map, uv_interp).rgb * glow, params.glow_map_strength);
742739
}
@@ -749,25 +746,27 @@ void main() {
749746
}
750747
#endif
751748

749+
// Tonemap to lower dynamic range.
750+
752751
color.rgb = apply_tonemapping(color.rgb, params.white);
753752

754753
#ifndef SUBPASS
755-
// Glow
754+
// Post-tonemap glow.
755+
756756
if (use_glow && glow_mode_softlight) {
757757
// Apply soft light after tonemapping to mitigate the issue of discontinuity
758758
// at 1.0 and higher. This makes the issue only appear with HDR output that
759759
// can exceed a 1.0 output value.
760-
vec3 glow = gather_glow() * params.glow_intensity * params.luminance_multiplier;
760+
vec3 glow = gather_glow() * params.glow_intensity;
761761
if (use_glow_map) {
762762
glow = mix(glow, texture(glow_map, uv_interp).rgb * glow, params.glow_map_strength);
763763
}
764-
765764
glow = apply_tonemapping(glow, params.white);
766765
color.rgb = apply_glow(color.rgb, glow, params.white);
767766
}
768767
#endif
769768

770-
// Additional effects
769+
// Additional effects.
771770

772771
if (use_bcs) {
773772
// Apply brightness:

0 commit comments

Comments
 (0)