From 0e9193d84c0f96b890123f092f22f81ae84fe6dc Mon Sep 17 00:00:00 2001
From: Geenz <geenz@geenzo.com>
Date: Wed, 3 Apr 2019 15:51:20 -0700
Subject: [PATCH] Make sure that the SL BRDF is being applied to objects lit by
 the sun and moon. Fixes BUG-226620

--HG--
branch : BUG-226620
---
 .../shaders/class1/deferred/softenLightF.glsl | 28 +++++++++++++------
 .../shaders/class2/deferred/softenLightF.glsl | 28 +++++++++++++------
 2 files changed, 40 insertions(+), 16 deletions(-)

diff --git a/indra/newview/app_settings/shaders/class1/deferred/softenLightF.glsl b/indra/newview/app_settings/shaders/class1/deferred/softenLightF.glsl
index e22240727cd..97b5e56529d 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/softenLightF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/softenLightF.glsl
@@ -123,14 +123,26 @@ void main()
 
         if (spec.a > 0.0) // specular reflection
         {
-            // the old infinite-sky shiny reflection
-            float sa = dot(refnormpersp, light_dir.xyz);
-            vec3 dumbshiny = sunlit*(texture2D(lightFunc, vec2(sa, spec.a)).r);
-            
-            // add the two types of shiny together
-            vec3 spec_contrib = dumbshiny * spec.rgb;
-            bloom = dot(spec_contrib, spec_contrib) / 6;
-            col += spec_contrib;
+            vec3 npos = -normalize(pos.xyz);
+
+            //vec3 ref = dot(pos+lv, norm);
+            vec3 h = normalize(light_dir.xyz+npos);
+            float nh = dot(norm.xyz, h);
+            float nv = dot(norm.xyz, npos);
+            float vh = dot(npos, h);
+            float sa = nh;
+            float fres = pow(1 - dot(h, npos), 5)*0.4+0.5;
+
+            float gtdenom = 2 * nh;
+            float gt = max(0, min(gtdenom * nv / vh, gtdenom * da / vh));
+                                    
+            if (nh > 0.0)
+            {
+                float scol = fres*texture2D(lightFunc, vec2(nh, spec.a)).r*gt/(nh*da);
+                vec3 speccol = sun_contrib*scol*spec.rgb;
+                speccol = max(speccol, vec3(0));
+                col += speccol;
+            }
         }
         
         col.rgb += diffuse.a * diffuse.rgb;
diff --git a/indra/newview/app_settings/shaders/class2/deferred/softenLightF.glsl b/indra/newview/app_settings/shaders/class2/deferred/softenLightF.glsl
index 28248aaf6d9..42258cc729a 100644
--- a/indra/newview/app_settings/shaders/class2/deferred/softenLightF.glsl
+++ b/indra/newview/app_settings/shaders/class2/deferred/softenLightF.glsl
@@ -136,14 +136,26 @@ void main()
 
         if (spec.a > 0.0) // specular reflection
         {
-            // the old infinite-sky shiny reflection
-            float sa = dot(refnormpersp, light_dir.xyz);
-            vec3 dumbshiny = sunlit*scol*(texture2D(lightFunc, vec2(sa, spec.a)).r);
-            
-            // add the two types of shiny together
-            vec3 spec_contrib = dumbshiny * spec.rgb;
-            bloom = dot(spec_contrib, spec_contrib) / 6;
-            col += spec_contrib;
+            vec3 npos = -normalize(pos.xyz);
+
+            //vec3 ref = dot(pos+lv, norm);
+            vec3 h = normalize(light_dir.xyz+npos);
+            float nh = dot(norm.xyz, h);
+            float nv = dot(norm.xyz, npos);
+            float vh = dot(npos, h);
+            float sa = nh;
+            float fres = pow(1 - dot(h, npos), 5)*0.4+0.5;
+
+            float gtdenom = 2 * nh;
+            float gt = max(0, min(gtdenom * nv / vh, gtdenom * da / vh));
+                                    
+            if (nh > 0.0)
+            {
+                float scontrib = fres*texture2D(lightFunc, vec2(nh, spec.a)).r*gt/(nh*da);
+                vec3 speccol = sun_contrib*scontrib*spec.rgb;
+                speccol = max(speccol, vec3(0));
+                col += speccol;
+            }
         }
         
         col.rgb += diffuse.a * diffuse.rgb;
-- 
GitLab