From d6d2f74fbbf22ba70166a97e15b6c3b39e42ac4d Mon Sep 17 00:00:00 2001
From: Dave Parks <davep@lindenlab.com>
Date: Mon, 1 Jul 2013 16:27:20 -0500
Subject: [PATCH] NORSPEC-290 Shader optimization WIP -- compatibility pass
 with OpenGL 3.3, slight cleanup.

---
 indra/llrender/llglslshader.cpp                | 18 +-----------------
 indra/llrender/llrender.cpp                    |  4 ++--
 .../shaders/class1/deferred/alphaF.glsl        |  3 +--
 .../class1/deferred/multiPointLightF.glsl      | 14 ++------------
 .../shaders/class1/deferred/pointLightF.glsl   | 14 +++++---------
 indra/newview/llviewerdisplay.cpp              |  1 +
 indra/newview/pipeline.cpp                     |  9 ++-------
 7 files changed, 14 insertions(+), 49 deletions(-)

diff --git a/indra/llrender/llglslshader.cpp b/indra/llrender/llglslshader.cpp
index 52b00f7c155..dfb73de6f31 100755
--- a/indra/llrender/llglslshader.cpp
+++ b/indra/llrender/llglslshader.cpp
@@ -1203,23 +1203,7 @@ void LLGLSLShader::uniform1i(const LLStaticHashedString& uniform, GLint v)
 	}
 }
 
-void LLGLSLShader::uniform2i(const string& uniform, GLint i, GLint j)
-{
-	GLint location = getUniformLocation(uniform);
-				
-	if (location >= 0)
-	{
-		std::map<GLint, LLVector4>::iterator iter = mValue.find(location);
-		LLVector4 vec(i,j,0.f,0.f);
-		if (iter == mValue.end() || shouldChange(iter->second,vec))
-		{
-			glUniform2iARB(location, i, j);
-			mValue[location] = vec;
-		}
-	}
-}
-
-void LLGLSLShader::uniform2i(const string& uniform, GLint i, GLint j)
+void LLGLSLShader::uniform2i(const LLStaticHashedString& uniform, GLint i, GLint j)
 {
 	GLint location = getUniformLocation(uniform);
 				
diff --git a/indra/llrender/llrender.cpp b/indra/llrender/llrender.cpp
index c4585936ee6..3393d352de0 100755
--- a/indra/llrender/llrender.cpp
+++ b/indra/llrender/llrender.cpp
@@ -832,8 +832,8 @@ LLLightState::LLLightState(S32 index)
 : mIndex(index),
   mEnabled(false),
   mConstantAtten(1.f),
-  mLinearAtten(0.f),
-  mQuadraticAtten(0.f),
+  mLinearAtten(1.f),
+  mQuadraticAtten(1.f),
   mSpotExponent(0.f),
   mSpotCutoff(180.f)
 {
diff --git a/indra/newview/app_settings/shaders/class1/deferred/alphaF.glsl b/indra/newview/app_settings/shaders/class1/deferred/alphaF.glsl
index b666b7b0d9e..4b428cb904b 100755
--- a/indra/newview/app_settings/shaders/class1/deferred/alphaF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/alphaF.glsl
@@ -93,7 +93,7 @@ float calcPointLightOrSpotLight(vec3 v, vec3 n, vec4 lp, vec3 ln, float la, floa
 	
 	float da = 0.0;
 
-//	if (d > 0.0 && la > 0.0 && fa > 0.0)
+	//if (d > 0.0 && la > 0.0 && fa > 0.0)
 	{
 		//normalize light vector
 		lv /= d;
@@ -230,7 +230,6 @@ void main()
 	}
 #endif
 
-	vec4 gamma_diff = diff;
 	vec3 normal = vary_norm; 
 	
 	vec3 l = light_position[0].xyz;
diff --git a/indra/newview/app_settings/shaders/class1/deferred/multiPointLightF.glsl b/indra/newview/app_settings/shaders/class1/deferred/multiPointLightF.glsl
index ed51e01a530..5a08980fec0 100755
--- a/indra/newview/app_settings/shaders/class1/deferred/multiPointLightF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/multiPointLightF.glsl
@@ -36,7 +36,6 @@ uniform sampler2DRect diffuseRect;
 uniform sampler2DRect specularRect;
 uniform sampler2DRect normalMap;
 uniform samplerCube environmentMap;
-uniform sampler2D noiseMap;
 uniform sampler2D lightFunc;
 
 
@@ -99,7 +98,6 @@ void main()
 	norm = normalize(norm);
 	vec4 spec = texture2DRect(specularRect, frag.xy);
 	vec3 diff = texture2DRect(diffuseRect, frag.xy).rgb;
-	float noise = texture2D(noiseMap, frag.xy/128.0).b;
 	vec3 out_col = vec3(0,0,0);
 	vec3 npos = normalize(-pos);
 
@@ -122,14 +120,10 @@ void main()
 				dist_atten *= dist_atten;
 				dist_atten *= 2.0;
 			
-				dist_atten *= noise;
-
 				float lit = da * dist_atten;
 						
 				vec3 col = light_col[i].rgb*lit*diff;
 			
-				//vec3 col = vec3(dist2, light_col[i].a, lit);
-			
 				if (spec.a > 0.0)
 				{
 					lit = min(da*6.0, 1.0) * dist_atten;
@@ -144,12 +138,8 @@ void main()
 					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);
-						col += lit*scol*light_col[i].rgb*spec.rgb;
-						//col += spec.rgb;
-					}
+					float scol = fres*texture2D(lightFunc, vec2(nh, spec.a)).r*gt/(nh*da);
+					col += max(lit*scol*light_col[i].rgb*spec.rgb, vec3(0.0));
 				}
 			
 				out_col += col;
diff --git a/indra/newview/app_settings/shaders/class1/deferred/pointLightF.glsl b/indra/newview/app_settings/shaders/class1/deferred/pointLightF.glsl
index f162f705928..b331258952c 100755
--- a/indra/newview/app_settings/shaders/class1/deferred/pointLightF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/pointLightF.glsl
@@ -102,11 +102,10 @@ void main()
 	{
 		vec3 norm = texture2DRect(normalMap, frag.xy).xyz;
 		norm = decode_normal(norm.xy); // unpack norm
-		float da = dot(norm, lv);
-	
+			
 		norm = normalize(norm);
 		lv = normalize(lv);
-		da = max(dot(norm, lv), 0.0);
+		float da = max(dot(norm, lv), 0.0);
 	
 		//float noise = texture2D(noiseMap, frag.xy/128.0).b;
 	
@@ -116,7 +115,7 @@ void main()
 		dist_atten *= dist_atten;
 		dist_atten *= 2.0;
 	
-		float lit = da * dist_atten; // * noise;
+		float lit = da * dist_atten;
 
 		col = color.rgb*lit*col;
 
@@ -135,11 +134,8 @@ void main()
 			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);
-				col += lit*scol*color.rgb*spec.rgb;
-			}
+			float scol = fres*texture2D(lightFunc, vec2(nh, spec.a)).r*gt/(nh*da);
+			col += max(lit*scol*color.rgb*spec.rgb, vec3(0.0));
 		}
 	}
 	
diff --git a/indra/newview/llviewerdisplay.cpp b/indra/newview/llviewerdisplay.cpp
index 422f18566f2..aae84709d84 100755
--- a/indra/newview/llviewerdisplay.cpp
+++ b/indra/newview/llviewerdisplay.cpp
@@ -675,6 +675,7 @@ void display(BOOL rebuild, F32 zoom_factor, int subfield, BOOL for_snapshot)
 		LLViewerCamera::sCurCameraID = LLViewerCamera::CAMERA_WORLD;
 		LLPipeline::sUnderWaterRender = LLViewerCamera::getInstance()->cameraUnderWater() ? TRUE : FALSE;
 		gPipeline.updateCull(*LLViewerCamera::getInstance(), result, water_clip);
+		LLPipeline::sUnderWaterRender = FALSE;
 		stop_glerror();
 
 		LLGLState::checkStates();
diff --git a/indra/newview/pipeline.cpp b/indra/newview/pipeline.cpp
index 7fa0a972ade..14529099b54 100755
--- a/indra/newview/pipeline.cpp
+++ b/indra/newview/pipeline.cpp
@@ -6316,13 +6316,6 @@ void LLPipeline::setupHWLights(LLDrawPool* pool)
 			F32 x = (3.f * (1.f + light->getLightFalloff())); // why this magic?  probably trying to match a historic behavior.
 			float linatten = x / (light_radius); // % of brightness at radius
 
-			if (LLPipeline::sRenderDeferred)
-			{
-				/*light_color.mV[0] = powf(light_color.mV[0], 2.2f);
-				light_color.mV[1] = powf(light_color.mV[1], 2.2f);
-				light_color.mV[2] = powf(light_color.mV[2], 2.2f);*/
-			}
-
 			mHWLightColors[cur_light] = light_color;
 			LLLightState* light_state = gGL.getLight(cur_light);
 			
@@ -6381,6 +6374,8 @@ void LLPipeline::setupHWLights(LLDrawPool* pool)
 		light->setDiffuse(LLColor4::black);
 		light->setAmbient(LLColor4::black);
 		light->setSpecular(LLColor4::black);
+		light->setQuadraticAttenuation(1.f);
+		light->setLinearAttenuation(1.f);
 	}
 	if (gAgentAvatarp &&
 		gAgentAvatarp->mSpecialRenderMode == 3)
-- 
GitLab