From fb7c887a5e09024731038eef0a57e5f1e8e08b2e Mon Sep 17 00:00:00 2001
From: Graham Linden <graham@lindenlab.com>
Date: Tue, 26 Mar 2019 10:28:25 -0700
Subject: [PATCH] More consistent lighting across ALM/non-ALM/deferred/forward
 rendering.

---
 .../shaders/class1/deferred/alphaF.glsl       | 11 +++++++++--
 .../shaders/class1/deferred/materialF.glsl    | 19 ++++++++++++-------
 .../shaders/class1/lighting/lightFuncV.glsl   |  2 +-
 .../class2/deferred/multiSpotLightF.glsl      | 13 +++++--------
 .../shaders/class2/deferred/spotLightF.glsl   | 12 +++++-------
 indra/newview/llvovolume.cpp                  |  9 ++++++---
 indra/newview/pipeline.cpp                    |  6 +++---
 7 files changed, 41 insertions(+), 31 deletions(-)

diff --git a/indra/newview/app_settings/shaders/class1/deferred/alphaF.glsl b/indra/newview/app_settings/shaders/class1/deferred/alphaF.glsl
index fdbf5ec840b..dac93cee3a8 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/alphaF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/alphaF.glsl
@@ -100,12 +100,14 @@ vec3 calcPointLightOrSpotLight(vec3 light_col, vec3 diffuse, vec3 v, vec3 n, vec
         vec3 norm = normalize(n);
 
         da = max(0.0, dot(norm, lv));
+        //da = min(da, shadow);
         da = clamp(da, 0.0, 1.0);
  
         //distance attenuation
         float dist = d/la;
         float dist_atten = clamp(1.0-(dist-1.0*(1.0-fa))/fa, 0.0, 1.0);
         dist_atten *= dist_atten;
+        dist_atten *= 2.0f;
 
         // spotlight coefficient.
         float spot = max(dot(-ln, lv), is_pointlight);
@@ -113,12 +115,16 @@ vec3 calcPointLightOrSpotLight(vec3 light_col, vec3 diffuse, vec3 v, vec3 n, vec
 
         // to match spotLight (but not multiSpotLight) *sigh*
         float lit = max(da * dist_atten,0.0);
+
+        // the shadowmap is wrong for alpha objects
+        // since we only have 2 maps but N spots
+        //col = lit * light_col * diffuse * shadow;
         col = lit * light_col * diffuse;
 
         float amb_da = ambiance;
+        amb_da += (da*0.5) * (1.0 - shadow) * ambiance;
+        amb_da += (da*da*0.5 + 0.5) * (1.0 - shadow) * ambiance;
         amb_da *= dist_atten;
-        amb_da += (da*0.5) * ambiance;
-        amb_da += (da*da*0.5 + 0.25) * ambiance;
         amb_da = min(amb_da, 1.0f - lit);
 
         col.rgb += amb_da * light_col * diffuse;
@@ -189,6 +195,7 @@ void main()
     vec3 light_dir = (sun_up_factor == 1) ? sun_dir: moon_dir;
     float da = dot(norm.xyz, light_dir.xyz);
           da = clamp(da, 0.0, 1.0);
+          da = pow(da, 1.0 / 1.3);
 
     vec4 color = vec4(0,0,0,0);
 
diff --git a/indra/newview/app_settings/shaders/class1/deferred/materialF.glsl b/indra/newview/app_settings/shaders/class1/deferred/materialF.glsl
index 9112b6afd37..c43b5b22bf0 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/materialF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/materialF.glsl
@@ -104,23 +104,28 @@ vec3 calcPointLightOrSpotLight(vec3 light_col, vec3 npos, vec3 diffuse, vec4 spe
         float dist = d/la;
         float dist_atten = clamp(1.0-(dist-1.0*(1.0-fa))/fa, 0.0, 1.0);
         dist_atten *= dist_atten;
-        
+        dist_atten *= 2.0f;
+
         // spotlight coefficient.
         float spot = max(dot(-ln, lv), is_pointlight);
         da *= spot*spot; // GL_SPOT_EXPONENT=2
 
         //angular attenuation
         da = dot(n, lv);
+        //da = min(da, shadow);
         da *= clamp(da, 0.0, 1.0);
-        
+
         float lit = max(da * dist_atten, 0.0);
 
+        // shadowmap is wrong for alpha-blended objs
+        // since we created shadowmaps for 2 but render N
+        //col = light_col*lit*diffuse*shadow;
         col = light_col*lit*diffuse;
-
+        
         float amb_da = ambiance;
+        amb_da += (da*0.5) * (1.0 - shadow) * ambiance;
+        amb_da += (da*da*0.5 + 0.5) * (1.0 - shadow) * ambiance;
         amb_da *= dist_atten;
-        amb_da += (da*0.5) * ambiance;
-        amb_da += (da*da*0.5 + 0.25) * ambiance;
         amb_da = min(amb_da, 1.0f - lit);
 
         col.rgb += amb_da * light_col * diffuse;
@@ -135,10 +140,10 @@ vec3 calcPointLightOrSpotLight(vec3 light_col, vec3 npos, vec3 diffuse, vec4 spe
             float sa = nh;
             float fres = pow(1 - dot(h, npos), 5)*0.4+0.5;
 
-            float gtdenom = abs(2 * nh);
+            float gtdenom = 2 * nh;
             float gt = max(0, min(gtdenom * nv / vh, gtdenom * da / vh));
                                 
-            if (gtdenom > 0.0)
+            if (nh > 0.0)
             {
                 float scol = fres*texture2D(lightFunc, vec2(nh, spec.a)).r*gt/(nh*da);
                 vec3 speccol = lit*scol*light_col.rgb*spec.rgb;
diff --git a/indra/newview/app_settings/shaders/class1/lighting/lightFuncV.glsl b/indra/newview/app_settings/shaders/class1/lighting/lightFuncV.glsl
index a9288b3df68..8defcf9f522 100644
--- a/indra/newview/app_settings/shaders/class1/lighting/lightFuncV.glsl
+++ b/indra/newview/app_settings/shaders/class1/lighting/lightFuncV.glsl
@@ -28,7 +28,7 @@
 
 float calcDirectionalLight(vec3 n, vec3 l)
 {
-	float a = max(dot(n,l),0.0);
+	float a = max(dot(n,normalize(l)),0.0);
 	return a;
 }
 
diff --git a/indra/newview/app_settings/shaders/class2/deferred/multiSpotLightF.glsl b/indra/newview/app_settings/shaders/class2/deferred/multiSpotLightF.glsl
index c4f406aa76c..05192e1737d 100644
--- a/indra/newview/app_settings/shaders/class2/deferred/multiSpotLightF.glsl
+++ b/indra/newview/app_settings/shaders/class2/deferred/multiSpotLightF.glsl
@@ -143,11 +143,11 @@ void main()
 	vec3 pos = getPosition(frag.xy).xyz;
 	vec3 lv = center.xyz-pos.xyz;
 	float dist = length(lv);
-	dist /= size;
-	if (dist > 1.0)
-	{
-		discard;
-	}
+
+    if ((size > 0) && ((dist / size) > 1.0))
+    {
+        discard;
+    }
 	
 	float shadow = 1.0;
 	
@@ -225,11 +225,8 @@ void main()
 		vec4 amb_plcol = texture2DLodAmbient(projectionMap, proj_tc.xy, proj_lod);
 							
 		amb_da += (da*da*0.5+0.5)*(1.0-shadow)*proj_ambiance;
-				
 		amb_da *= dist_atten * noise;
-			
 		amb_da = min(amb_da, 1.0-lit);
-			
 		col += amb_da*color.rgb*diff_tex.rgb*amb_plcol.rgb*amb_plcol.a;
 	}
 	
diff --git a/indra/newview/app_settings/shaders/class2/deferred/spotLightF.glsl b/indra/newview/app_settings/shaders/class2/deferred/spotLightF.glsl
index 67eb503e728..4a5f5e642bb 100644
--- a/indra/newview/app_settings/shaders/class2/deferred/spotLightF.glsl
+++ b/indra/newview/app_settings/shaders/class2/deferred/spotLightF.glsl
@@ -143,11 +143,11 @@ void main()
 	vec3 pos = getPosition(frag.xy).xyz;
 	vec3 lv = trans_center.xyz-pos.xyz;
 	float dist = length(lv);
-	dist /= size;
-	if (dist > 1.0)
-	{
-		discard;
-	}
+
+    if ((size > 0) && ((dist / size) > 1.0))
+    {
+        discard;
+    }
 	
 	float shadow = 1.0;
 	
@@ -224,9 +224,7 @@ void main()
 		vec4 amb_plcol = texture2DLodAmbient(projectionMap, proj_tc.xy, proj_lod);
 							
 		amb_da += (da*da*0.5+0.5)*(1.0-shadow)*proj_ambiance;
-				
 		amb_da *= dist_atten * noise;
-			
 		amb_da = min(amb_da, 1.0-lit);
 			
 		col += amb_da*color.rgb*diff_tex.rgb*amb_plcol.rgb*amb_plcol.a;
diff --git a/indra/newview/llvovolume.cpp b/indra/newview/llvovolume.cpp
index 671f4d49a77..f97e0ff2e72 100644
--- a/indra/newview/llvovolume.cpp
+++ b/indra/newview/llvovolume.cpp
@@ -3275,12 +3275,13 @@ F32 LLVOVolume::getSpotLightPriority() const
 
 void LLVOVolume::updateSpotLightPriority()
 {
+    F32 r = getLightRadius();
 	LLVector3 pos = mDrawable->getPositionAgent();
-	LLVector3 at(0,0,-1);
-	at *= getRenderRotation();
+    LLVector3 agent_pos = gAgent.getPositionAgent();
 
-	F32 r = getLightRadius()*0.5f;
+	LLVector3 at(0,0,-1);
 
+	at *= getRenderRotation();
 	pos += at * r;
 
 	at = LLViewerCamera::getInstance()->getAtAxis();
@@ -3289,6 +3290,8 @@ void LLVOVolume::updateSpotLightPriority()
 	
 	mSpotLightPriority = gPipeline.calcPixelArea(pos, LLVector3(r,r,r), *LLViewerCamera::getInstance());
 
+    //mSpotLightPriority = (agent_pos - pos).length() + r;
+
 	if (mLightTexture.notNull())
 	{
 		mLightTexture->addTextureStats(mSpotLightPriority);
diff --git a/indra/newview/pipeline.cpp b/indra/newview/pipeline.cpp
index 4c93d16fb1e..8bc68ac9eaf 100644
--- a/indra/newview/pipeline.cpp
+++ b/indra/newview/pipeline.cpp
@@ -8750,7 +8750,7 @@ void LLPipeline::renderDeferredLighting(LLRenderTarget* screen_target)
                             gDeferredLightProgram.uniform3fv(LLShaderMgr::LIGHT_CENTER, 1, c);
                             gDeferredLightProgram.uniform1f(LLShaderMgr::LIGHT_SIZE, s);
                             gDeferredLightProgram.uniform3fv(LLShaderMgr::DIFFUSE_COLOR, 1, col.mV);
-                            gDeferredLightProgram.uniform1f(LLShaderMgr::LIGHT_FALLOFF, volume->getLightFalloff()*0.5f);
+                            gDeferredLightProgram.uniform1f(LLShaderMgr::LIGHT_FALLOFF, volume->getLightFalloff());
                             gGL.syncMatrices();
                             
                             mCubeVB->drawRange(LLRender::TRIANGLE_FAN, 0, 7, 8, get_box_fan_indices(camera, center));
@@ -8809,7 +8809,7 @@ void LLPipeline::renderDeferredLighting(LLRenderTarget* screen_target)
                     gDeferredSpotLightProgram.uniform3fv(LLShaderMgr::LIGHT_CENTER, 1, c);
                     gDeferredSpotLightProgram.uniform1f(LLShaderMgr::LIGHT_SIZE, s);
                     gDeferredSpotLightProgram.uniform3fv(LLShaderMgr::DIFFUSE_COLOR, 1, col.mV);
-                    gDeferredSpotLightProgram.uniform1f(LLShaderMgr::LIGHT_FALLOFF, volume->getLightFalloff()*0.5f);
+                    gDeferredSpotLightProgram.uniform1f(LLShaderMgr::LIGHT_FALLOFF, volume->getLightFalloff());
                     gGL.syncMatrices();
                                         
                     mCubeVB->drawRange(LLRender::TRIANGLE_FAN, 0, 7, 8, get_box_fan_indices(camera, center));
@@ -8906,7 +8906,7 @@ void LLPipeline::renderDeferredLighting(LLRenderTarget* screen_target)
                     gDeferredMultiSpotLightProgram.uniform3fv(LLShaderMgr::LIGHT_CENTER, 1, tc.v);
                     gDeferredMultiSpotLightProgram.uniform1f(LLShaderMgr::LIGHT_SIZE, s);
                     gDeferredMultiSpotLightProgram.uniform3fv(LLShaderMgr::DIFFUSE_COLOR, 1, col.mV);
-                    gDeferredMultiSpotLightProgram.uniform1f(LLShaderMgr::LIGHT_FALLOFF, volume->getLightFalloff()*0.5f);
+                    gDeferredMultiSpotLightProgram.uniform1f(LLShaderMgr::LIGHT_FALLOFF, volume->getLightFalloff());
                     mDeferredVB->drawArrays(LLRender::TRIANGLES, 0, 3);
                 }
 
-- 
GitLab