From 0fed3884c21453eac23a4de714bc87cac9e5377e Mon Sep 17 00:00:00 2001
From: Dave Parks <davep@lindenlab.com>
Date: Mon, 10 Oct 2022 10:19:36 -0500
Subject: [PATCH] SL-18190 Use funky distance based error correction only on
 water surface (revisit for general use later).

---
 .../class1/deferred/reflectionProbeF.glsl     |  9 +++-
 .../class2/deferred/reflectionProbeF.glsl     |  9 +++-
 .../class3/deferred/reflectionProbeF.glsl     | 46 ++++++++++++-------
 .../shaders/class3/environment/waterF.glsl    |  4 +-
 4 files changed, 47 insertions(+), 21 deletions(-)

diff --git a/indra/newview/app_settings/shaders/class1/deferred/reflectionProbeF.glsl b/indra/newview/app_settings/shaders/class1/deferred/reflectionProbeF.glsl
index 4b79297aefb..dd850ff97cb 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/reflectionProbeF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/reflectionProbeF.glsl
@@ -25,12 +25,19 @@
 
 // fallback stub -- will be used if actual reflection probe shader failed to load (output pink so it's obvious)
 void sampleReflectionProbes(inout vec3 ambenv, inout vec3 glossenv,
-        vec3 pos, vec3 norm, float glossiness)
+        vec3 pos, vec3 norm, float glossiness, bool errorCorrect)
 {
     ambenv = vec3(1,0,1);
     glossenv = vec3(1,0,1);
 }
 
+void sampleReflectionProbes(inout vec3 ambenv, inout vec3 glossenv,
+    vec3 pos, vec3 norm, float glossiness)
+{
+    sampleReflectionProbes(ambenv, glossenv,
+        pos, norm, glossiness, false);
+}
+
 void sampleReflectionProbesLegacy(inout vec3 ambenv, inout vec3 glossenv, inout vec3 legacyenv, 
         vec3 pos, vec3 norm, float glossiness, float envIntensity)
 {
diff --git a/indra/newview/app_settings/shaders/class2/deferred/reflectionProbeF.glsl b/indra/newview/app_settings/shaders/class2/deferred/reflectionProbeF.glsl
index 0236680ac0e..eb261434381 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,
-        vec3 pos, vec3 norm, float glossiness)
+        vec3 pos, vec3 norm, float glossiness, bool errorCorrect)
 {
     ambenv = vec3(reflection_probe_ambiance * 0.25);
     
@@ -43,6 +43,13 @@ 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,
+    vec3 pos, vec3 norm, float glossiness)
+{
+    sampleReflectionProbes(ambenv, glossenv,
+        pos, norm, glossiness, false);
+}
+
 void sampleReflectionProbesLegacy(inout vec3 ambenv, inout vec3 glossenv, inout vec3 legacyenv, 
         vec3 pos, vec3 norm, float glossiness, float envIntensity)
 {
diff --git a/indra/newview/app_settings/shaders/class3/deferred/reflectionProbeF.glsl b/indra/newview/app_settings/shaders/class3/deferred/reflectionProbeF.glsl
index ec9218f00ec..925398671c2 100644
--- a/indra/newview/app_settings/shaders/class3/deferred/reflectionProbeF.glsl
+++ b/indra/newview/app_settings/shaders/class3/deferred/reflectionProbeF.glsl
@@ -374,7 +374,7 @@ vec3 tapIrradianceMap(vec3 pos, vec3 dir, vec3 c, float r2, int i)
     }
 }
 
-vec3 sampleProbes(vec3 pos, vec3 dir, float lod, float minweight)
+vec3 sampleProbes(vec3 pos, vec3 dir, float lod, bool errorCorrect)
 {
     float wsum = 0.0;
     vec3 col = vec3(0,0,0);
@@ -399,12 +399,15 @@ vec3 sampleProbes(vec3 pos, vec3 dir, float lod, float minweight)
         {
             vec3 vi, wi;
 
-            vec3 refcol = tapRefMap(pos, dir, vi, wi, lod, refSphere[i].xyz, rr, i);
+            float atten = 1.0 - max(d2 - r2, 0.0) / (rr - r2);
+            float w;
+            vec3 refcol;
 
-            float atten = 1.0-max(d2-r2, 0.0)/(rr-r2);
+            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, vi, wi, abs(lod + 2), refSphere[i].xyz, rr, i);
 
-            if (refIndex[i].w >= 0)
-            {
                 //adjust lookup by distance result
                 float d = length(vi - wi);
                 vi += dir * d;
@@ -414,16 +417,20 @@ vec3 sampleProbes(vec3 pos, vec3 dir, float lod, float minweight)
                 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);
+                w = pow(w, 32.0);
+            }
+            else
+            {
+                w = 1.0 / d2;
+                refcol = tapRefMap(pos, dir, vi, wi, lod, refSphere[i].xyz, rr, i);
             }
 
-            //float w = 1.0 / max(d3, 1.0);
-            vec3 pi = normalize(wi - pos);
-            float w = max(dot(pi, dir), 0.1);
-            w = pow(w, 32.0);
-            
             w *= atten;
 
-
             //w *= p; // boost weight based on priority
             col += refcol.rgb*w;
 
@@ -431,7 +438,6 @@ vec3 sampleProbes(vec3 pos, vec3 dir, float lod, float minweight)
         }
     }
 
-#if 1
     if (probeInfluences < 1)
     { //edge-of-scene probe or no probe influence, mix in with embiggened version of probes closest to camera 
         for (int idx = 0; idx < 8; ++idx)
@@ -455,7 +461,6 @@ vec3 sampleProbes(vec3 pos, vec3 dir, float lod, float minweight)
             }
         }
     }
-#endif
 
     if (wsum > 0.0)
     {
@@ -537,7 +542,7 @@ vec3 sampleProbeAmbient(vec3 pos, vec3 dir)
 }
 
 void sampleReflectionProbes(inout vec3 ambenv, inout vec3 glossenv,
-        vec3 pos, vec3 norm, float glossiness)
+        vec3 pos, vec3 norm, float glossiness, bool errorCorrect)
 {
     // TODO - don't hard code lods
     float reflection_lods = 7;
@@ -548,7 +553,14 @@ 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, 1.f);
+    glossenv = sampleProbes(pos, normalize(refnormpersp), lod, errorCorrect);
+}
+
+void sampleReflectionProbes(inout vec3 ambenv, inout vec3 glossenv,
+    vec3 pos, vec3 norm, float glossiness)
+{
+    sampleReflectionProbes(ambenv, glossenv,
+        pos, norm, glossiness, false);
 }
 
 void sampleReflectionProbesLegacy(inout vec3 ambenv, inout vec3 glossenv, inout vec3 legacyenv,
@@ -565,12 +577,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, 1.f);
+        glossenv = sampleProbes(pos, normalize(refnormpersp), lod, false);
     }
 
     if (envIntensity > 0.0)
     {
-        legacyenv = sampleProbes(pos, normalize(refnormpersp), 0.0, 1.f);
+        legacyenv = sampleProbes(pos, normalize(refnormpersp), 0.0, false);
     }
 }
 
diff --git a/indra/newview/app_settings/shaders/class3/environment/waterF.glsl b/indra/newview/app_settings/shaders/class3/environment/waterF.glsl
index 4c361884a5e..9793a0e7862 100644
--- a/indra/newview/app_settings/shaders/class3/environment/waterF.glsl
+++ b/indra/newview/app_settings/shaders/class3/environment/waterF.glsl
@@ -103,7 +103,7 @@ vec3 transform_normal(vec3 vNt)
 }
 
 void sampleReflectionProbes(inout vec3 ambenv, inout vec3 glossenv,
-    vec3 pos, vec3 norm, float glossiness);
+    vec3 pos, vec3 norm, float glossiness, bool errorCorrect);
 
 vec3 getPositionWithNDC(vec3 ndc);
 
@@ -215,7 +215,7 @@ void main()
     
     vec3 irradiance = vec3(0);
     vec3 radiance = vec3(0);
-    sampleReflectionProbes(irradiance, radiance, pos, refnorm, gloss);
+    sampleReflectionProbes(irradiance, radiance, pos, refnorm, gloss, true);
     radiance *= 0.5;
     irradiance = fb.rgb;
 
-- 
GitLab