From e5a2f85005bbf94f39ef048dbfe43276990f1154 Mon Sep 17 00:00:00 2001
From: Dave Parks <davep@lindenlab.com>
Date: Mon, 27 Feb 2023 16:53:56 -0600
Subject: [PATCH] SL-19226 Reimplement water fresnel offset/scale, exposure
 balance for midday, adjust reflections off, and decruft depth buffer error
 correction shenanigans that are no longer used.

---
 .../deferred/postDeferredGammaCorrect.glsl    |  3 +-
 .../class1/interface/reflectionmipF.glsl      | 20 +-------
 .../class2/deferred/reflectionProbeF.glsl     | 10 ++--
 .../class3/deferred/reflectionProbeF.glsl     | 49 +++++--------------
 .../shaders/class3/environment/waterF.glsl    | 25 ++++++----
 indra/newview/lldrawpoolwlsky.cpp             |  7 ++-
 indra/newview/llreflectionmapmanager.cpp      | 16 ++----
 7 files changed, 46 insertions(+), 84 deletions(-)

diff --git a/indra/newview/app_settings/shaders/class1/deferred/postDeferredGammaCorrect.glsl b/indra/newview/app_settings/shaders/class1/deferred/postDeferredGammaCorrect.glsl
index f0e940eb5fe..cc77712347a 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/postDeferredGammaCorrect.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/postDeferredGammaCorrect.glsl
@@ -134,6 +134,7 @@ vec3 toneMap(vec3 color)
     color *= exposure;
 
 #ifdef TONEMAP_ACES_NARKOWICZ
+    color *= 0.8;
     color = toneMapACES_Narkowicz(color);
 #endif
 
@@ -145,7 +146,7 @@ vec3 toneMap(vec3 color)
     // boost exposure as discussed in https://github.com/mrdoob/three.js/pull/19621
     // this factor is based on the exposure correction of Krzysztof Narkowicz in his
     // implemetation of ACES tone mapping
-    color /= 0.6;
+    color *= 0.85/0.6;
     color = toneMapACES_Hill(color);
 #endif
 
diff --git a/indra/newview/app_settings/shaders/class1/interface/reflectionmipF.glsl b/indra/newview/app_settings/shaders/class1/interface/reflectionmipF.glsl
index 9f7706fe365..45267e44037 100644
--- a/indra/newview/app_settings/shaders/class1/interface/reflectionmipF.glsl
+++ b/indra/newview/app_settings/shaders/class1/interface/reflectionmipF.glsl
@@ -26,29 +26,11 @@
 out vec4 frag_color;
 
 uniform sampler2D diffuseRect;
-uniform sampler2D depthMap;
-
-uniform float resScale;
-uniform float znear;
-uniform float zfar;
 
 in vec2 vary_texcoord0;
 
-// get linear depth value given a depth buffer sample d and znear and zfar values
-float linearDepth(float d, float znear, float zfar);
-
 void main() 
 {
-    float depth = texture(depthMap, vary_texcoord0.xy).r;
-    float dist = linearDepth(depth, znear, zfar);
-
-    // convert linear depth to distance
-    vec3 v;
-    v.xy = vary_texcoord0.xy / 512.0 * 2.0 - 1.0;
-    v.z = 1.0;
-    v = normalize(v);
-    dist /= v.z;
-
     vec3 col = texture(diffuseRect, vary_texcoord0.xy).rgb;
-    frag_color = vec4(col, dist/256.0);
+    frag_color = vec4(col, 0.0);
 }
diff --git a/indra/newview/app_settings/shaders/class2/deferred/reflectionProbeF.glsl b/indra/newview/app_settings/shaders/class2/deferred/reflectionProbeF.glsl
index f1ee4b46818..5fa53c374b6 100644
--- a/indra/newview/app_settings/shaders/class2/deferred/reflectionProbeF.glsl
+++ b/indra/newview/app_settings/shaders/class2/deferred/reflectionProbeF.glsl
@@ -34,7 +34,7 @@ uniform mat3 env_mat;
 vec3 srgb_to_linear(vec3 c);
 
 void sampleReflectionProbes(inout vec3 ambenv, inout vec3 glossenv,
-        vec2 tc, vec3 pos, vec3 norm, float glossiness, bool errorCorrect)
+        vec2 tc, vec3 pos, vec3 norm, float glossiness)
 {
     ambenv = vec3(reflection_probe_ambiance * 0.25);
     
@@ -43,11 +43,11 @@ void sampleReflectionProbes(inout vec3 ambenv, inout vec3 glossenv,
     glossenv = srgb_to_linear(textureCube(environmentMap, env_vec).rgb);
 }
 
-void sampleReflectionProbes(inout vec3 ambenv, inout vec3 glossenv,
-    vec2 tc, vec3 pos, vec3 norm, float glossiness)
+void sampleReflectionProbesWater(inout vec3 ambenv, inout vec3 glossenv,
+        vec2 tc, vec3 pos, vec3 norm, float glossiness)
 {
-    sampleReflectionProbes(ambenv, glossenv,
-        tc, pos, norm, glossiness, false);
+    sampleReflectionProbes(ambenv, glossenv, tc, pos, norm, glossiness);
+    glossenv *= 8.0;
 }
 
 vec4 sampleReflectionProbesDebug(vec3 pos)
diff --git a/indra/newview/app_settings/shaders/class3/deferred/reflectionProbeF.glsl b/indra/newview/app_settings/shaders/class3/deferred/reflectionProbeF.glsl
index 24539c3c3af..23c6f4d5ae8 100644
--- a/indra/newview/app_settings/shaders/class3/deferred/reflectionProbeF.glsl
+++ b/indra/newview/app_settings/shaders/class3/deferred/reflectionProbeF.glsl
@@ -542,7 +542,7 @@ vec3 tapIrradianceMap(vec3 pos, vec3 dir, out float w, out float dw, vec3 c, int
     }
 }
 
-vec3 sampleProbes(vec3 pos, vec3 dir, float lod, bool errorCorrect)
+vec3 sampleProbes(vec3 pos, vec3 dir, float lod)
 {
     float wsum[2];
     wsum[0] = 0;
@@ -573,29 +573,7 @@ vec3 sampleProbes(vec3 pos, vec3 dir, float lod, bool errorCorrect)
 
         
         {
-            if (errorCorrect && refIndex[i].w >= 0)
-            { // error correction is on and this probe is a sphere
-              //take a sample to get depth value, then error correct
-                refcol = tapRefMap(pos, dir, w, dw, vi, wi, abs(lod + 2), refSphere[i].xyz, i);
-
-                //adjust lookup by distance result
-                float d = length(vi - wi);
-                vi += dir * d;
-
-                vi -= refSphere[i].xyz;
-
-                vi = env_mat * vi;
-
-                refcol = textureLod(reflectionProbes, vec4(vi, refIndex[i].x), lod).rgb;
-
-                // weight by vector correctness
-                vec3 pi = normalize(wi - pos);
-                w *= max(dot(pi, dir), 0.1);
-            }
-            else
-            {
-                refcol = tapRefMap(pos, dir, w, dw, vi, wi, lod, refSphere[i].xyz, i);
-            }
+            refcol = tapRefMap(pos, dir, w, dw, vi, wi, lod, refSphere[i].xyz, i);
 
             col[p] += refcol.rgb*w;
             wsum[p] += w;
@@ -684,7 +662,7 @@ vec3 sampleProbeAmbient(vec3 pos, vec3 dir)
 }
 
 void sampleReflectionProbes(inout vec3 ambenv, inout vec3 glossenv,
-        vec2 tc, vec3 pos, vec3 norm, float glossiness, bool errorCorrect)
+        vec2 tc, vec3 pos, vec3 norm, float glossiness)
 {
     // TODO - don't hard code lods
     float reflection_lods = max_probe_lod;
@@ -695,13 +673,12 @@ void sampleReflectionProbes(inout vec3 ambenv, inout vec3 glossenv,
     ambenv = sampleProbeAmbient(pos, norm);
 
     float lod = (1.0-glossiness)*reflection_lods;
-    glossenv = sampleProbes(pos, normalize(refnormpersp), lod, errorCorrect);
+    glossenv = sampleProbes(pos, normalize(refnormpersp), lod);
 
 #if defined(SSR)
     if (cube_snapshot != 1 && glossiness >= 0.9)
     {
         vec4 ssr = vec4(0);
-        //float w = tapScreenSpaceReflection(errorCorrect ? 1 : 4, tc, pos, norm, ssr, sceneMap);
         float w = tapScreenSpaceReflection(1, tc, pos, norm, ssr, sceneMap);
 
         glossenv = mix(glossenv, ssr.rgb, w);
@@ -709,6 +686,12 @@ void sampleReflectionProbes(inout vec3 ambenv, inout vec3 glossenv,
 #endif
 }
 
+void sampleReflectionProbesWater(inout vec3 ambenv, inout vec3 glossenv,
+        vec2 tc, vec3 pos, vec3 norm, float glossiness)
+{
+    sampleReflectionProbes(ambenv, glossenv, tc, pos, norm, glossiness);
+}
+
 void debugTapRefMap(vec3 pos, vec3 dir, float depth, int i, inout vec4 col)
 {
     vec3 origin = vec3(0,0,0);
@@ -749,14 +732,6 @@ vec4 sampleReflectionProbesDebug(vec3 pos)
     return col;
 }
 
-void sampleReflectionProbes(inout vec3 ambenv, inout vec3 glossenv,
-    vec2 tc, vec3 pos, vec3 norm, float glossiness)
-{
-    sampleReflectionProbes(ambenv, glossenv,
-        tc, pos, norm, glossiness, false);
-}
-
-
 void sampleReflectionProbesLegacy(inout vec3 ambenv, inout vec3 glossenv, inout vec3 legacyenv,
         vec2 tc, vec3 pos, vec3 norm, float glossiness, float envIntensity)
 {
@@ -770,12 +745,12 @@ void sampleReflectionProbesLegacy(inout vec3 ambenv, inout vec3 glossenv, inout
     if (glossiness > 0.0)
     {
         float lod = (1.0-glossiness)*reflection_lods;
-        glossenv = sampleProbes(pos, normalize(refnormpersp), lod, false);
+        glossenv = sampleProbes(pos, normalize(refnormpersp), lod);
     }
     
     if (envIntensity > 0.0)
     {
-        legacyenv = sampleProbes(pos, normalize(refnormpersp), 0.0, false);
+        legacyenv = sampleProbes(pos, normalize(refnormpersp), 0.0);
     }
 
 #if defined(SSR)
diff --git a/indra/newview/app_settings/shaders/class3/environment/waterF.glsl b/indra/newview/app_settings/shaders/class3/environment/waterF.glsl
index 631d2c04d0b..a87682affb2 100644
--- a/indra/newview/app_settings/shaders/class3/environment/waterF.glsl
+++ b/indra/newview/app_settings/shaders/class3/environment/waterF.glsl
@@ -122,11 +122,8 @@ vec3 transform_normal(vec3 vNt)
     return normalize(vNt.x * vT + vNt.y * vB + vNt.z * vN);
 }
 
-void sampleReflectionProbes(inout vec3 ambenv, inout vec3 glossenv,
-    vec2 tc, vec3 pos, vec3 norm, float glossiness);
-
-void sampleReflectionProbes(inout vec3 ambenv, inout vec3 glossenv,
-     vec2 tc, vec3 pos, vec3 norm, float glossiness, bool errorCorrect);
+void sampleReflectionProbesWater(inout vec3 ambenv, inout vec3 glossenv,
+        vec2 tc, vec3 pos, vec3 norm, float glossiness);
 
 vec3 getPositionWithNDC(vec3 ndc);
 
@@ -219,16 +216,16 @@ void main()
 
     fb = applyWaterFogViewLinear(refPos, fb, sunlit);
 #else
-    vec4 fb = applyWaterFogViewLinear(viewVec*2048.0, vec4(0.5), sunlit);
+    vec4 fb = applyWaterFogViewLinear(viewVec*2048.0, vec4(1.0), sunlit);
 #endif
 
     float metallic = 0.0;
-    float perceptualRoughness = 0.1;
+    float perceptualRoughness = 0.05;
     float gloss      = 1.0 - perceptualRoughness;
     
     vec3  irradiance = vec3(0);
     vec3  radiance  = vec3(0);
-    sampleReflectionProbes(irradiance, radiance, distort2, pos.xyz, wave_ibl.xyz, gloss);
+    sampleReflectionProbesWater(irradiance, radiance, distort2, pos.xyz, wave_ibl.xyz, gloss);
 
     irradiance       = vec3(0);
 
@@ -265,10 +262,18 @@ void main()
     f *= 0.9;
     f *= f;
 
+    // incoming scale is [0, 1] with 0.5 being default
+    // shift to 0.5 to 1.5
+    f *= (fresnelScale - 0.5)+1.0;
+
+    // incoming offset is [0, 1] with 0.5 being default
+    // shift from -1 to 1
+    f += (fresnelOffset - 0.5) * 2.0;
+
     f = clamp(f, 0, 1);
-    //fb.rgb *= 1.;
-    
+
     color = mix(color, fb.rgb, f);
+
     float spec = min(max(max(punctual.r, punctual.g), punctual.b), 0.05);
     
     frag_color = vec4(color, spec); //*sunAngle2);
diff --git a/indra/newview/lldrawpoolwlsky.cpp b/indra/newview/lldrawpoolwlsky.cpp
index 59ed62f9955..acbc3495674 100644
--- a/indra/newview/lldrawpoolwlsky.cpp
+++ b/indra/newview/lldrawpoolwlsky.cpp
@@ -45,6 +45,8 @@
 #include "llvowlsky.h"
 #include "llsettingsvo.h"
 
+extern BOOL gCubeSnapshot;
+
 static LLStaticHashedString sCamPosLocal("camPosLocal");
 static LLStaticHashedString sCustomAlpha("custom_alpha");
 
@@ -434,7 +436,10 @@ void LLDrawPoolWLSky::renderDeferred(S32 pass)
     {
         renderSkyHazeDeferred(origin, camHeightLocal);
         renderHeavenlyBodies();
-        renderStarsDeferred(origin);
+        if (!gCubeSnapshot)
+        {
+            renderStarsDeferred(origin);
+        }
         renderSkyCloudsDeferred(origin, camHeightLocal, cloud_shader);
     }
     gGL.setColorMask(true, true);
diff --git a/indra/newview/llreflectionmapmanager.cpp b/indra/newview/llreflectionmapmanager.cpp
index acb36124164..77168983762 100644
--- a/indra/newview/llreflectionmapmanager.cpp
+++ b/indra/newview/llreflectionmapmanager.cpp
@@ -107,7 +107,7 @@ void LLReflectionMapManager::update()
         mMipChain.resize(count);
         for (int i = 0; i < count; ++i)
         {
-            mMipChain[i].allocate(res, res, GL_RGBA16);
+            mMipChain[i].allocate(res, res, GL_RGB16);
             res /= 2;
         }
     }
@@ -475,7 +475,7 @@ void LLReflectionMapManager::updateProbeFace(LLReflectionMap* probe, U32 face)
         
         //only render sky, water, terrain, and clouds
         gPipeline.andRenderTypeMask(LLPipeline::RENDER_TYPE_SKY, LLPipeline::RENDER_TYPE_WL_SKY,
-            LLPipeline::RENDER_TYPE_WATER, LLPipeline::RENDER_TYPE_CLOUDS, LLPipeline::RENDER_TYPE_TERRAIN, LLPipeline::END_RENDER_TYPES);
+            LLPipeline::RENDER_TYPE_WATER, LLPipeline::RENDER_TYPE_VOIDWATER, LLPipeline::RENDER_TYPE_CLOUDS, LLPipeline::RENDER_TYPE_TERRAIN, LLPipeline::END_RENDER_TYPES);
         
         probe->update(mRenderTarget.getWidth(), face);
 
@@ -519,7 +519,6 @@ void LLReflectionMapManager::updateProbeFace(LLReflectionMap* probe, U32 face)
         static LLStaticHashedString zfar("zfar");
 
         LLRenderTarget* screen_rt = &gPipeline.mAuxillaryRT.screen;
-        LLRenderTarget* depth_rt = &gPipeline.mAuxillaryRT.deferredScreen;
 
         // perform a gaussian blur on the super sampled render before downsampling
         {
@@ -549,7 +548,6 @@ void LLReflectionMapManager::updateProbeFace(LLReflectionMap* probe, U32 face)
 
         gReflectionMipProgram.bind();
         S32 diffuseChannel = gReflectionMipProgram.enableTexture(LLShaderMgr::DEFERRED_DIFFUSE, LLTexUnit::TT_TEXTURE);
-        S32 depthChannel   = gReflectionMipProgram.enableTexture(LLShaderMgr::DEFERRED_DEPTH, LLTexUnit::TT_TEXTURE);
 
         for (int i = 0; i < mMipChain.size(); ++i)
         {
@@ -564,12 +562,9 @@ void LLReflectionMapManager::updateProbeFace(LLReflectionMap* probe, U32 face)
                 gGL.getTexUnit(diffuseChannel)->bind(&(mMipChain[i - 1]));
             }
 
-            gGL.getTexUnit(depthChannel)->bind(depth_rt, true);
-
+            
             gReflectionMipProgram.uniform1f(resScale, 1.f/(mProbeResolution*2));
-            gReflectionMipProgram.uniform1f(znear, probe->getNearClip());
-            gReflectionMipProgram.uniform1f(zfar, MAX_FAR_CLIP);
-
+            
             gPipeline.mScreenTriangleVB->setBuffer();
             gPipeline.mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
             
@@ -597,7 +592,6 @@ void LLReflectionMapManager::updateProbeFace(LLReflectionMap* probe, U32 face)
         gGL.popMatrix();
 
         gGL.getTexUnit(diffuseChannel)->unbind(LLTexUnit::TT_TEXTURE);
-        gGL.getTexUnit(depthChannel)->unbind(LLTexUnit::TT_TEXTURE);
         gReflectionMipProgram.unbind();
     }
 
@@ -1021,7 +1015,7 @@ void LLReflectionMapManager::initReflectionMaps()
         mTexture = new LLCubeMapArray();
 
         // store mReflectionProbeCount+2 cube maps, final two cube maps are used for render target and radiance map generation source)
-        mTexture->allocate(mProbeResolution, 4, mReflectionProbeCount + 2);
+        mTexture->allocate(mProbeResolution, 3, mReflectionProbeCount + 2);
 
         mIrradianceMaps = new LLCubeMapArray();
         mIrradianceMaps->allocate(LL_IRRADIANCE_MAP_RESOLUTION, 4, mReflectionProbeCount, FALSE);
-- 
GitLab