From 10b2d1be01978261ee0c452fdfabe2ec68ad1642 Mon Sep 17 00:00:00 2001
From: Dave Parks <davep@lindenlab.com>
Date: Thu, 29 Sep 2022 18:56:21 -0500
Subject: [PATCH] SL-18190 End of linear space odyssey -- just eat the
 conversions in atmosphericsFuncsF for sunlight and additive, but leave linear
 ambient in tact.

---
 .../shaders/class1/deferred/deferredUtil.glsl |  2 +-
 .../shaders/class2/deferred/pbralphaF.glsl    |  6 +++---
 .../class2/deferred/reflectionProbeF.glsl     |  2 +-
 .../class2/windlight/atmosphericsF.glsl       | 11 ++---------
 .../class2/windlight/atmosphericsFuncs.glsl   | 19 ++++++++++++++-----
 5 files changed, 21 insertions(+), 19 deletions(-)

diff --git a/indra/newview/app_settings/shaders/class1/deferred/deferredUtil.glsl b/indra/newview/app_settings/shaders/class1/deferred/deferredUtil.glsl
index 50bf4976051..bca4771c273 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/deferredUtil.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/deferredUtil.glsl
@@ -370,7 +370,7 @@ vec3 pbrIbl(vec3 diffuseColor,
 	vec3 diffuse = diffuseLight * diffuseColor;
 	vec3 specular = specularLight * (specularColor * brdf.x + brdf.y);
 
-	return (diffuse + specular) * ao * 0.5;  //reduce by half to place in appropriate color space for atmospherics
+	return (diffuse + specular*0.5) * ao;  //reduce by half to place in appropriate color space for atmospherics
 }
 
 // Encapsulate the various inputs used by the various functions in the shading equation
diff --git a/indra/newview/app_settings/shaders/class2/deferred/pbralphaF.glsl b/indra/newview/app_settings/shaders/class2/deferred/pbralphaF.glsl
index e6cb8f37c73..13544af0b10 100644
--- a/indra/newview/app_settings/shaders/class2/deferred/pbralphaF.glsl
+++ b/indra/newview/app_settings/shaders/class2/deferred/pbralphaF.glsl
@@ -196,7 +196,7 @@ void main()
     vec3  irradiance = vec3(0);
     vec3  radiance  = vec3(0);
     sampleReflectionProbes(irradiance, radiance, pos.xyz, norm.xyz, gloss);
-    irradiance       = max(amblit*2.0,irradiance);
+    irradiance       = max(amblit*0.75,irradiance);
 
     vec3 f0 = vec3(0.04);
     
@@ -209,8 +209,8 @@ void main()
     float NdotV = clamp(abs(dot(norm.xyz, v)), 0.001, 1.0);
 
     color += pbrIbl(diffuseColor, specularColor, radiance, irradiance, ao, NdotV, perceptualRoughness);
-    color += pbrPunctual(diffuseColor, specularColor, perceptualRoughness, metallic, norm.xyz, v, light_dir) * sunlit*2.75 * scol;
-    color += colorEmissive;
+    color += pbrPunctual(diffuseColor, specularColor, perceptualRoughness, metallic, norm.xyz, v, light_dir) * sunlit * scol;
+    color += colorEmissive*0.5;
     
     color = atmosFragLightingLinear(color, additive, atten);
     color  = scaleSoftClipFragLinear(color);
diff --git a/indra/newview/app_settings/shaders/class2/deferred/reflectionProbeF.glsl b/indra/newview/app_settings/shaders/class2/deferred/reflectionProbeF.glsl
index 878724982f5..0236680ac0e 100644
--- a/indra/newview/app_settings/shaders/class2/deferred/reflectionProbeF.glsl
+++ b/indra/newview/app_settings/shaders/class2/deferred/reflectionProbeF.glsl
@@ -63,6 +63,6 @@ void applyGlossEnv(inout vec3 color, vec3 glossenv, vec4 spec, vec3 pos, vec3 no
 
 void applyLegacyEnv(inout vec3 color, vec3 legacyenv, vec4 spec, vec3 pos, vec3 norm, float envIntensity)
 {
-    color = mix(color.rgb, legacyenv*0.5, envIntensity);
+    color = mix(color.rgb, legacyenv*1.5, envIntensity);
 }
 
diff --git a/indra/newview/app_settings/shaders/class2/windlight/atmosphericsF.glsl b/indra/newview/app_settings/shaders/class2/windlight/atmosphericsF.glsl
index 20b1e3513e7..1463d507bc8 100644
--- a/indra/newview/app_settings/shaders/class2/windlight/atmosphericsF.glsl
+++ b/indra/newview/app_settings/shaders/class2/windlight/atmosphericsF.glsl
@@ -33,7 +33,7 @@ vec3 srgb_to_linear(vec3 col);
 vec3 linear_to_srgb(vec3 col);
 
 vec3 atmosFragLighting(vec3 light, vec3 additive, vec3 atten)
-{
+{ 
     if (no_atmo == 1)
     {
         return light;
@@ -45,14 +45,7 @@ vec3 atmosFragLighting(vec3 light, vec3 additive, vec3 atten)
 
 vec3 atmosFragLightingLinear(vec3 light, vec3 additive, vec3 atten)
 {
-    if (no_atmo == 1)
-    {
-        return light;
-    }
-    
-    light *= atten.r;
-    light += additive;
-    return light*2.0;
+    return atmosFragLighting(light, additive, atten);
 }
 
 vec3 atmosLighting(vec3 light)
diff --git a/indra/newview/app_settings/shaders/class2/windlight/atmosphericsFuncs.glsl b/indra/newview/app_settings/shaders/class2/windlight/atmosphericsFuncs.glsl
index 739f00a8b0c..0093e8a31e2 100644
--- a/indra/newview/app_settings/shaders/class2/windlight/atmosphericsFuncs.glsl
+++ b/indra/newview/app_settings/shaders/class2/windlight/atmosphericsFuncs.glsl
@@ -50,6 +50,8 @@ uniform float sun_moon_glow_factor;
 
 float getAmbientClamp() { return 1.0f; }
 
+vec3 srgb_to_linear(vec3 col);
+
 // return colors in sRGB space
 void calcAtmosphericVars(vec3 inPositionEye, vec3 light_dir, float ambFactor, out vec3 sunlit, out vec3 amblit, out vec3 additive,
                          out vec3 atten, bool use_ao)
@@ -148,6 +150,14 @@ vec3 srgb_to_linear(vec3 col);
 void calcAtmosphericVarsLinear(vec3 inPositionEye, vec3 light_dir, float ambFactor, out vec3 sunlit, out vec3 amblit, out vec3 additive,
                          out vec3 atten, bool use_ao)
 {
+#if 1
+    calcAtmosphericVars(inPositionEye, light_dir, 1.0, sunlit, amblit, additive, atten, false);
+    sunlit = srgb_to_linear(sunlit);
+    additive = srgb_to_linear(additive);
+    amblit = ambient_linear;
+#else 
+
+    //EXPERIMENTAL -- attempt to factor out srgb_to_linear conversions above
     vec3 rel_pos = inPositionEye;
 
     //(TERRAIN) limit altitude
@@ -217,12 +227,11 @@ void calcAtmosphericVarsLinear(vec3 inPositionEye, vec3 light_dir, float ambFact
     additive = (blue_horizon.rgb * blue_weight.rgb) * (cs + tmpAmbient.rgb) + (haze_horizon * haze_weight.rgb) * (cs * haze_glow + tmpAmbient.rgb);
 
     // brightness of surface both sunlight and ambient
-    sunlit = sunlight.rgb;
+    sunlit = min(sunlight.rgb, vec3(1));
     amblit = tmpAmbient.rgb;
     additive *= vec3(1.0 - combined_haze);
 
-    //sunlit = srgb_to_linear(sunlit);
-    amblit = ambient_linear;
-    additive = srgb_to_linear(additive*1.5);
-
+    //sunlit = sunlight_linear;
+    amblit = ambient_linear*0.8;
+#endif
 }
-- 
GitLab