From a10ec81e82d79bd79d5b058fda1b370073bfb480 Mon Sep 17 00:00:00 2001
From: Graham Linden <graham@lindenlab.com>
Date: Fri, 15 Mar 2019 08:13:04 -0700
Subject: [PATCH] SL-10743, SL-10744

Don't step on SUNLIGHT_COLOR uniform w/ syncLightState competing set.

Put drawpool alpha render loop lighting setup changes as they were
(this will give back some performance and possibly require reopening 10566).
---
 indra/llinventory/llsettingssky.cpp           |  7 +--
 indra/llrender/llrender.cpp                   |  2 -
 .../shaders/class1/deferred/alphaF.glsl       |  3 +-
 .../shaders/class1/deferred/materialF.glsl    |  1 -
 .../class2/windlight/atmosphericsF.glsl       |  6 +-
 .../class2/windlight/atmosphericsV.glsl       |  6 +-
 indra/newview/lldrawpoolalpha.cpp             | 59 +++++++++++++++++--
 7 files changed, 69 insertions(+), 15 deletions(-)

diff --git a/indra/llinventory/llsettingssky.cpp b/indra/llinventory/llsettingssky.cpp
index 744c5853e47..f1bea326c11 100644
--- a/indra/llinventory/llsettingssky.cpp
+++ b/indra/llinventory/llsettingssky.cpp
@@ -1281,19 +1281,18 @@ void LLSettingsSky::calculateLightSettings() const
 
     // and vary_sunlight will work properly with moon light
     F32 lighty = lightnorm[1];
-
-    lighty = llmax(0.f, lighty);
-    if(lighty > 0.f)
+    if(fabs(lighty) > 0.001f)
     {
         lighty = 1.f / lighty;
     }
+    lighty = llmax(0.001f, lighty);
     componentMultBy(sunlight, componentExp((light_atten * -1.f) * lighty));
 
     //increase ambient when there are more clouds
     LLColor3 tmpAmbient = ambient + (smear(1.f) - ambient) * cloud_shadow * 0.5f;
 
     //brightness of surface both sunlight and ambient
-    mSunDiffuse = gammaCorrect(componentMult(sunlight, light_transmittance));       
+    mSunDiffuse = gammaCorrect(componentMult(sunlight, light_transmittance));
     mSunAmbient = gammaCorrect(componentMult(tmpAmbient, light_transmittance) * 0.5);
 
     mMoonDiffuse  = gammaCorrect(componentMult(LLColor3::white, light_transmittance) * 0.5f);
diff --git a/indra/llrender/llrender.cpp b/indra/llrender/llrender.cpp
index e3bb9298cb2..dc662bf8982 100644
--- a/indra/llrender/llrender.cpp
+++ b/indra/llrender/llrender.cpp
@@ -1180,8 +1180,6 @@ void LLRender::syncLightState()
 		shader->uniform4fv(LLShaderMgr::LIGHT_ATTENUATION, 8, attenuation[0].mV);
 		shader->uniform3fv(LLShaderMgr::LIGHT_DIFFUSE, 8, diffuse[0].mV);
 		shader->uniform4fv(LLShaderMgr::LIGHT_AMBIENT, 1, mAmbientLightColor.mV);
-		shader->uniform4fv(LLShaderMgr::SUNLIGHT_COLOR, 1, diffuse[0].mV);
-        shader->uniform4fv(LLShaderMgr::MOONLIGHT_COLOR, 1, diffuseB[0].mV);
 	}
 }
 
diff --git a/indra/newview/app_settings/shaders/class1/deferred/alphaF.glsl b/indra/newview/app_settings/shaders/class1/deferred/alphaF.glsl
index 251ec7d7856..fdbf5ec840b 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/alphaF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/alphaF.glsl
@@ -118,7 +118,7 @@ vec3 calcPointLightOrSpotLight(vec3 light_col, vec3 diffuse, vec3 v, vec3 n, vec
         float amb_da = ambiance;
         amb_da *= dist_atten;
         amb_da += (da*0.5) * ambiance;
-        amb_da += (da*da*0.5 + 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;
@@ -228,7 +228,6 @@ void main()
     color.rgb += light.rgb;
 
     color.rgb = linear_to_srgb(color.rgb);
-
 #endif
 
 #ifdef WATER_FOG
diff --git a/indra/newview/app_settings/shaders/class1/deferred/materialF.glsl b/indra/newview/app_settings/shaders/class1/deferred/materialF.glsl
index 76c1fc7cf43..ae2dd24b191 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/materialF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/materialF.glsl
@@ -112,7 +112,6 @@ vec3 calcPointLightOrSpotLight(vec3 light_col, vec3 npos, vec3 diffuse, vec4 spe
         //angular attenuation
         da = dot(n, lv);
         da *= clamp(da, 0.0, 1.0);
-        da *= pow(da, 1.0 / 1.3);
         
         float lit = max(da * dist_atten, 0.0);
 
diff --git a/indra/newview/app_settings/shaders/class2/windlight/atmosphericsF.glsl b/indra/newview/app_settings/shaders/class2/windlight/atmosphericsF.glsl
index 565c00ba798..14af657e67e 100644
--- a/indra/newview/app_settings/shaders/class2/windlight/atmosphericsF.glsl
+++ b/indra/newview/app_settings/shaders/class2/windlight/atmosphericsF.glsl
@@ -96,7 +96,11 @@ void calcFragAtmospherics(vec3 inPositionEye, float ambFactor, out vec3 sunlit,
 
     //(TERRAIN) compute sunlight from lightnorm only (for short rays like terrain)
     temp2.y = max(0.0, tmpLightnorm.y);
-    temp2.y = 1. / temp2.y;
+    if (temp2.y > 0.001f)
+    {
+        temp2.y = 1. / temp2.y;
+    }
+    temp2.y = max(0.001f, temp2.y);
     sunlight *= exp(-light_atten * temp2.y);
 
     // main atmospheric scattering line integral
diff --git a/indra/newview/app_settings/shaders/class2/windlight/atmosphericsV.glsl b/indra/newview/app_settings/shaders/class2/windlight/atmosphericsV.glsl
index bef7a8827a2..6c4098b9fb0 100644
--- a/indra/newview/app_settings/shaders/class2/windlight/atmosphericsV.glsl
+++ b/indra/newview/app_settings/shaders/class2/windlight/atmosphericsV.glsl
@@ -91,7 +91,11 @@ void calcAtmospherics(vec3 inPositionEye) {
 
     //(TERRAIN) compute sunlight from lightnorm only (for short rays like terrain)
     temp2.y = max(0.0, tmpLightnorm.y);
-    temp2.y = 1. / temp2.y;
+    if (temp2.y > 0.001f)
+    {
+        temp2.y = 1. / temp2.y;
+    }
+    temp2.y = max(0.001f, temp2.y);
     sunlight *= exp( - light_atten * temp2.y);
 
     // main atmospheric scattering line integral
diff --git a/indra/newview/lldrawpoolalpha.cpp b/indra/newview/lldrawpoolalpha.cpp
index 7aaab2e3af8..4c625eb6849 100644
--- a/indra/newview/lldrawpoolalpha.cpp
+++ b/indra/newview/lldrawpoolalpha.cpp
@@ -67,6 +67,7 @@ static LLTrace::BlockTimerStatHandle FTM_RENDER_ALPHA_DEFERRED_SHADER_BINDS("Alp
 static LLTrace::BlockTimerStatHandle FTM_RENDER_ALPHA_DEFERRED_TEX_BINDS("Alpha Def Tex Binds");
 static LLTrace::BlockTimerStatHandle FTM_RENDER_ALPHA_MESH_REBUILD("Alpha Mesh Rebuild");
 static LLTrace::BlockTimerStatHandle FTM_RENDER_ALPHA_EMISSIVE("Alpha Emissive");
+static LLTrace::BlockTimerStatHandle FTM_RENDER_ALPHA_LIGHT_SETUP("Alpha Light Setup");
 
 LLDrawPoolAlpha::LLDrawPoolAlpha(U32 type) :
 		LLRenderPass(type), current_shader(NULL), target_shader(NULL),
@@ -640,11 +641,13 @@ void LLDrawPoolAlpha::renderAlpha(U32 mask, S32 pass)
 					}
 				}
 
+#if BATCH_FULLBRIGHTS
                 if (params.mFullbright)
 				{
                     fullbrights.push_back(&params);
                     continue;
 				}
+#endif
 
 				LLRenderPass::applyModelMatrix(params);
 
@@ -654,10 +657,31 @@ void LLDrawPoolAlpha::renderAlpha(U32 mask, S32 pass)
 				{
 					mat = params.mMaterial;
 				}
-								
+				
+				if (params.mFullbright)
+				{
+					// Turn off lighting if it hasn't already been so.
+					if (light_enabled || !initialized_lighting)
+					{
+                        LL_RECORD_BLOCK_TIME(FTM_RENDER_ALPHA_LIGHT_SETUP);
+
+						initialized_lighting = TRUE;
+						if (use_shaders) 
+						{
+							target_shader = fullbright_shader;
+						}
+						else
+						{
+							gPipeline.enableLightsFullbright(LLColor4(1,1,1,1));
+						}
+						light_enabled = FALSE;
+					}
+				}
 				// Turn on lighting if it isn't already.
-				if (!light_enabled || !initialized_lighting)
+				else if (!light_enabled || !initialized_lighting)
 				{
+                    LL_RECORD_BLOCK_TIME(FTM_RENDER_ALPHA_LIGHT_SETUP);
+
 					initialized_lighting = TRUE;
 					if (use_shaders) 
 					{
@@ -689,10 +713,14 @@ void LLDrawPoolAlpha::renderAlpha(U32 mask, S32 pass)
                         current_shader = target_shader;
 					}
 				}
-				else
+				else if (!params.mFullbright)
 				{
 					target_shader = simple_shader;
 				}
+				else
+				{
+					target_shader = fullbright_shader;
+				}
 				
 				if(use_shaders && (current_shader != target_shader))
 				{// If we need shaders, and we're not ALREADY using the proper shader, then bind it
@@ -754,7 +782,26 @@ void LLDrawPoolAlpha::renderAlpha(U32 mask, S32 pass)
 					params.mVertexBuffer->hasDataType(LLVertexBuffer::TYPE_EMISSIVE))
 				{
                     LL_RECORD_BLOCK_TIME(FTM_RENDER_ALPHA_EMISSIVE);
+#if BATCH_EMISSIVES
                     emissives.push_back(&params);
+#else
+// install glow-accumulating blend mode
+					gGL.blendFunc(LLRender::BF_ZERO, LLRender::BF_ONE, // don't touch color
+					LLRender::BF_ONE, LLRender::BF_ONE); // add to alpha (glow)
+
+					emissive_shader->bind();
+					
+					params.mVertexBuffer->setBuffer((mask & ~LLVertexBuffer::MAP_COLOR) | LLVertexBuffer::MAP_EMISSIVE);
+					
+					// do the actual drawing, again
+					params.mVertexBuffer->drawRange(params.mDrawMode, params.mStart, params.mEnd, params.mCount, params.mOffset);
+					gPipeline.addTrianglesDrawn(params.mCount, params.mDrawMode);
+
+					// restore our alpha blend mode
+					gGL.blendFunc(mColorSFactor, mColorDFactor, mAlphaSFactor, mAlphaDFactor);
+
+					current_shader->bind();
+#endif
 				}
 			
 				if (tex_setup)
@@ -766,9 +813,13 @@ void LLDrawPoolAlpha::renderAlpha(U32 mask, S32 pass)
 				}
 			}
 
+#if BATCH_FULLBRIGHTS
             renderFullbrights(mask, fullbrights);
-            renderEmissives(mask, emissives);
+#endif
 
+#if BATCH_EMISSIVES
+            renderEmissives(mask, emissives);
+#endif
             if (current_shader)
             {
                 current_shader->bind();
-- 
GitLab