From c07817c3d26095de569e9193ade2b9040b7be8c0 Mon Sep 17 00:00:00 2001 From: William Weaver Date: Fri, 11 Apr 2025 02:23:52 +0300 Subject: Fix(Tonemap): Correct blend logic to preserve HDR detail The blending operation for the `tonemap_mix` uniform in `postDeferredTonemap.glsl` incorrectly used a prematurely clamped color value as the source for the linear mix target. Specifically, the exposed HDR input color was clamped to the [0, 1] LDR range before being used in the `mix()` function when `tonemap_mix < 1.0`. This premature clamping resulted in the loss of High Dynamic Range (HDR) detail in highlights during the blend operation. As `tonemap_mix` was reduced, instead of smoothly blending towards the linear scene representation, clipped highlights were incorrectly reintroduced. This commit modifies the `toneMap` and `toneMapNoExposure` functions to correct this logic: 1. The original linear input color is preserved before exposure/processing. 2. The appropriate exposure factor is calculated and applied separately. 3. The chosen tone mapping operator is applied to the exposed color, storing the result. 4. The `mix()` function now correctly blends between the appropriately scaled, *unclamped* linear input color and the fully tone-mapped result. 5. The final clamp to the [0, 1] LDR range is applied *after* the blend operation. This change ensures that HDR information is preserved throughout the blending process, resulting in a smoother, more perceptually correct visual transition as `tonemap_mix` is adjusted. While the effect is nuanced, it is noticeable in bright highlights; with the legacy code, these highlights appeared visibly clipped and less intense during the blend, whereas the corrected code allows them to retain their peak brightness and detail more accurately. This makes the `tonemap_mix` control more intuitive, behaving as a true intensity blend for the tone mapping effect without introducing clipping artifacts. The computational cost is negligible. --- .../shaders/class1/deferred/tonemapUtilF.glsl | 37 ++++++++++++++-------- 1 file changed, 24 insertions(+), 13 deletions(-) (limited to 'indra') diff --git a/indra/newview/app_settings/shaders/class1/deferred/tonemapUtilF.glsl b/indra/newview/app_settings/shaders/class1/deferred/tonemapUtilF.glsl index a63b8d7c2b..774ccb6baf 100644 --- a/indra/newview/app_settings/shaders/class1/deferred/tonemapUtilF.glsl +++ b/indra/newview/app_settings/shaders/class1/deferred/tonemapUtilF.glsl @@ -117,27 +117,34 @@ uniform float exposure; uniform float tonemap_mix; uniform int tonemap_type; + vec3 toneMap(vec3 color) { #ifndef NO_POST - float exp_scale = texture(exposureMap, vec2(0.5,0.5)).r; - - color *= exposure * exp_scale; + vec3 linear_input_color = color; - vec3 clamped_color = clamp(color.rgb, vec3(0.0), vec3(1.0)); + float exp_scale = texture(exposureMap, vec2(0.5,0.5)).r; + float final_exposure = exposure * exp_scale; + vec3 exposed_color = color * final_exposure; + vec3 tonemapped_color = exposed_color; switch(tonemap_type) { case 0: - color = PBRNeutralToneMapping(color); + tonemapped_color = PBRNeutralToneMapping(exposed_color); break; case 1: - color = toneMapACES_Hill(color); + tonemapped_color = toneMapACES_Hill(exposed_color); break; } - // mix tonemapped and linear here to provide adjustment - color = mix(clamped_color, color, tonemap_mix); + vec3 exposed_linear_input = linear_input_color * final_exposure; + color = mix(exposed_linear_input, tonemapped_color, tonemap_mix); + + color = clamp(color, 0.0, 1.0); +#else + color *= exposure * texture(exposureMap, vec2(0.5,0.5)).r; + color = clamp(color, 0.0, 1.0); #endif return color; @@ -147,20 +154,24 @@ vec3 toneMap(vec3 color) vec3 toneMapNoExposure(vec3 color) { #ifndef NO_POST - vec3 clamped_color = clamp(color.rgb, vec3(0.0), vec3(1.0)); + vec3 linear_input_color = color; + vec3 tonemapped_color = color; switch(tonemap_type) { case 0: - color = PBRNeutralToneMapping(color); + tonemapped_color = PBRNeutralToneMapping(color); break; case 1: - color = toneMapACES_Hill(color); + tonemapped_color = toneMapACES_Hill(color); break; } - // mix tonemapped and linear here to provide adjustment - color = mix(clamped_color, color, tonemap_mix); + color = mix(linear_input_color, tonemapped_color, tonemap_mix); + + color = clamp(color, 0.0, 1.0); +#else + color = clamp(color, 0.0, 1.0); #endif return color; -- cgit v1.2.3