From 17f46868d28876b65adcf24a45e6c19a20f2834c Mon Sep 17 00:00:00 2001
From: Dave Parks <davep@lindenlab.com>
Date: Wed, 10 Aug 2011 14:40:39 -0500
Subject: [PATCH] SH-2265 Fix for impostor alpha masking being broken.

---
 .../shaders/class1/deferred/impostorF.glsl    |  8 ++++++
 .../shaders/class1/objects/impostorF.glsl     | 26 +++++++++++++++++++
 .../shaders/class1/objects/impostorV.glsl     | 16 ++++++++++++
 indra/newview/lldrawpoolavatar.cpp            | 11 ++++++++
 indra/newview/llviewershadermgr.cpp           | 15 +++++++++++
 indra/newview/llviewershadermgr.h             |  1 +
 6 files changed, 77 insertions(+)
 create mode 100644 indra/newview/app_settings/shaders/class1/objects/impostorF.glsl
 create mode 100644 indra/newview/app_settings/shaders/class1/objects/impostorV.glsl

diff --git a/indra/newview/app_settings/shaders/class1/deferred/impostorF.glsl b/indra/newview/app_settings/shaders/class1/deferred/impostorF.glsl
index fa811f0d559..75f594ed8f9 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/impostorF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/impostorF.glsl
@@ -5,6 +5,8 @@
  * $/LicenseInfo$
  */
  
+uniform float minimum_alpha;
+uniform float maximum_alpha;
 
 
 uniform sampler2D diffuseMap;
@@ -14,6 +16,12 @@ uniform sampler2D specularMap;
 void main() 
 {
 	vec4 col = texture2D(diffuseMap, gl_TexCoord[0].xy);
+
+	if (col.a < minimum_alpha || col.a > maximum_alpha)
+	{
+		discard;
+	}
+
 	gl_FragData[0] = vec4(col.rgb, col.a * 0.005);
 	gl_FragData[1] = texture2D(specularMap, gl_TexCoord[0].xy);
 	gl_FragData[2] = vec4(texture2D(normalMap, gl_TexCoord[0].xy).xyz, 0.0);
diff --git a/indra/newview/app_settings/shaders/class1/objects/impostorF.glsl b/indra/newview/app_settings/shaders/class1/objects/impostorF.glsl
new file mode 100644
index 00000000000..7257132f066
--- /dev/null
+++ b/indra/newview/app_settings/shaders/class1/objects/impostorF.glsl
@@ -0,0 +1,26 @@
+/** 
+ * @file impostorF.glsl
+ *
+ * $LicenseInfo:firstyear=2007&license=viewerlgpl$
+ * $/LicenseInfo$
+ */
+ 
+uniform float minimum_alpha;
+uniform float maximum_alpha;
+
+vec3 fullbrightAtmosTransport(vec3 light);
+vec3 fullbrightScaleSoftClip(vec3 light);
+
+uniform sampler2D diffuseMap;
+
+void main()
+{
+	vec4 color = texture2D(diffuseMap,gl_TexCoord[0].xy) * gl_Color;
+	
+	if (color.a < minimum_alpha || color.a > maximum_alpha)
+	{
+		discard;
+	}
+
+	gl_FragColor = color;
+}
diff --git a/indra/newview/app_settings/shaders/class1/objects/impostorV.glsl b/indra/newview/app_settings/shaders/class1/objects/impostorV.glsl
new file mode 100644
index 00000000000..724b86a1b72
--- /dev/null
+++ b/indra/newview/app_settings/shaders/class1/objects/impostorV.glsl
@@ -0,0 +1,16 @@
+/**
+ * @file impostorV.glsl
+ *
+ * $LicenseInfo:firstyear=2007&license=viewerlgpl$
+ * $/LicenseInfo$
+ */
+ 
+ 
+void main()
+{
+	//transform vertex
+	gl_Position = ftransform();
+	gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
+	
+	gl_FrontColor = gl_Color;
+}
diff --git a/indra/newview/lldrawpoolavatar.cpp b/indra/newview/lldrawpoolavatar.cpp
index 694b7dcedd1..28e464b60df 100644
--- a/indra/newview/lldrawpoolavatar.cpp
+++ b/indra/newview/lldrawpoolavatar.cpp
@@ -589,12 +589,22 @@ void LLDrawPoolAvatar::beginImpostor()
 		LLVOAvatar::sNumVisibleAvatars = 0;
 	}
 
+	if (LLGLSLShader::sNoFixedFunction)
+	{
+		gImpostorProgram.bind();
+		gImpostorProgram.setAlphaRange(0.01f, 1.f);
+	}
+
 	gPipeline.enableLightsFullbright(LLColor4(1,1,1,1));
 	sDiffuseChannel = 0;
 }
 
 void LLDrawPoolAvatar::endImpostor()
 {
+	if (LLGLSLShader::sNoFixedFunction)
+	{
+		gImpostorProgram.unbind();
+	}
 	gPipeline.enableLightsDynamic();
 }
 
@@ -647,6 +657,7 @@ void LLDrawPoolAvatar::beginDeferredImpostor()
 	sDiffuseChannel = sVertexProgram->enableTexture(LLViewerShaderMgr::DIFFUSE_MAP);
 
 	sVertexProgram->bind();
+	sVertexProgram->setAlphaRange(0.01f, 1.f);
 }
 
 void LLDrawPoolAvatar::endDeferredImpostor()
diff --git a/indra/newview/llviewershadermgr.cpp b/indra/newview/llviewershadermgr.cpp
index fa8d43e0b23..36106752a26 100644
--- a/indra/newview/llviewershadermgr.cpp
+++ b/indra/newview/llviewershadermgr.cpp
@@ -118,6 +118,7 @@ LLGLSLShader		gAvatarProgram;
 LLGLSLShader		gAvatarWaterProgram;
 LLGLSLShader		gAvatarEyeballProgram;
 LLGLSLShader		gAvatarPickProgram;
+LLGLSLShader		gImpostorProgram;
 
 // WindLight shader handles
 LLGLSLShader			gWLSkyProgram;
@@ -186,6 +187,7 @@ LLViewerShaderMgr::LLViewerShaderMgr() :
 	mShaderList.push_back(&gWaterProgram);
 	mShaderList.push_back(&gAvatarEyeballProgram); 
 	mShaderList.push_back(&gObjectSimpleProgram);
+	mShaderList.push_back(&gImpostorProgram);
 	mShaderList.push_back(&gObjectSimpleAlphaMaskProgram);
 	mShaderList.push_back(&gObjectBumpProgram);
 	mShaderList.push_back(&gUIProgram);
@@ -638,6 +640,7 @@ void LLViewerShaderMgr::unloadShaders()
 	gSolidColorProgram.unload();
 
 	gObjectSimpleProgram.unload();
+	gImpostorProgram.unload();
 	gObjectSimpleAlphaMaskProgram.unload();
 	gObjectBumpProgram.unload();
 	gObjectSimpleWaterProgram.unload();
@@ -1678,6 +1681,7 @@ BOOL LLViewerShaderMgr::loadShadersObject()
 		gObjectFullbrightShinyWaterProgram.unload();
 		gObjectShinyWaterProgram.unload();
 		gObjectSimpleProgram.unload();
+		gImpostorProgram.unload();
 		gObjectSimpleAlphaMaskProgram.unload();
 		gObjectBumpProgram.unload();
 		gObjectSimpleWaterProgram.unload();
@@ -1874,6 +1878,17 @@ BOOL LLViewerShaderMgr::loadShadersObject()
 		success = gObjectFullbrightShinyNonIndexedWaterProgram.createShader(NULL, &mShinyUniforms);
 	}
 
+	if (success)
+	{
+		gImpostorProgram.mName = "Impostor Shader";
+		gImpostorProgram.mFeatures.disableTextureIndex = true;
+		gImpostorProgram.mShaderFiles.clear();
+		gImpostorProgram.mShaderFiles.push_back(make_pair("objects/impostorV.glsl", GL_VERTEX_SHADER_ARB));
+		gImpostorProgram.mShaderFiles.push_back(make_pair("objects/impostorF.glsl", GL_FRAGMENT_SHADER_ARB));
+		gImpostorProgram.mShaderLevel = mVertexShaderLevel[SHADER_OBJECT];
+		success = gImpostorProgram.createShader(NULL, NULL);
+	}
+
 	if (success)
 	{
 		gObjectSimpleProgram.mName = "Simple Shader";
diff --git a/indra/newview/llviewershadermgr.h b/indra/newview/llviewershadermgr.h
index 629ef32adb4..d4040f11e1f 100644
--- a/indra/newview/llviewershadermgr.h
+++ b/indra/newview/llviewershadermgr.h
@@ -351,6 +351,7 @@ extern LLGLSLShader			gAvatarProgram;
 extern LLGLSLShader			gAvatarWaterProgram;
 extern LLGLSLShader			gAvatarEyeballProgram;
 extern LLGLSLShader			gAvatarPickProgram;
+extern LLGLSLShader			gImpostorProgram;
 
 // WindLight shader handles
 extern LLGLSLShader			gWLSkyProgram;
-- 
GitLab