From 29bdf1a234a70b02050f7774af873a40a1cfd21d Mon Sep 17 00:00:00 2001
From: ruslantproductengine <ruslantproductengine@lindenlab.com>
Date: Sun, 1 Mar 2015 14:10:54 +0200
Subject: [PATCH] MAINT-4839 FIXED Animation of Mesh Dress doesn't work.
 MAINT-4165 FIXED Crash on skinned, texture animated geometry with deferred
 rendering enabled.

---
 indra/llrender/llglslshader.cpp    | 67 ++++++++++++++++++++++++++++++
 indra/llrender/llrender.cpp        | 53 +++++++++++------------
 indra/llrender/llrender.h          |  2 +-
 indra/newview/lldrawpoolavatar.cpp |  7 +++-
 4 files changed, 100 insertions(+), 29 deletions(-)

diff --git a/indra/llrender/llglslshader.cpp b/indra/llrender/llglslshader.cpp
index 3ceed95248d..f9f7d07c893 100755
--- a/indra/llrender/llglslshader.cpp
+++ b/indra/llrender/llglslshader.cpp
@@ -722,10 +722,77 @@ BOOL LLGLSLShader::mapUniforms(const vector<LLStaticHashedString> * uniforms)
     GLint activeCount;
     glGetObjectParameterivARB(mProgramObject, GL_OBJECT_ACTIVE_UNIFORMS_ARB, &activeCount);
 
+	//........................................................................................................................................
+	//........................................................................................
+
+	/*
+	EXPLANATION:
+	This is part of code is temporary because as the final result the mapUniform() should be rewrited. 
+	But it's a huge a volume of work which is need to be a more carefully performed for avoid possible 
+	regression's (i.e. it should be formalized a separate ticket in JIRA).
+
+	RESON:
+	The reason of this code is that SL engine is very sensitive to fact that "diffuseMap" should be appear 
+	first as uniform parameter which is should get 0-"texture channel" index (see mapUniformTextureChannel() and mActiveTextureChannels) 
+	it influence to which is texture matrix will be updated during rendering.
+
+	But, order of indexe's of uniform variables is not defined and GLSL compiler can change it as want
+	, even if the "diffuseMap" will be appear and use first in shader code.
+
+	As example where this situation appear see: "Deferred Material Shader 28/29/30/31"
+	And tickets: MAINT-4165, MAINT-4839
+	*/
+	
+
+	S32 diffuseMap = glGetUniformLocationARB(mProgramObject, "diffuseMap");
+	S32 bumpMap = glGetUniformLocationARB(mProgramObject, "bumpMap");
+
+	std::set<S32> skip_index;
+
+	if(diffuseMap != -1 && bumpMap != -1)
+	{
+		GLenum type;
+		GLsizei length;
+		GLint size = -1;
+		char name[1024];        
+
+		//diffuse map
+		for (S32 i = 0; i < activeCount; i++)
+		{
+			name[0] = 0;
+			
+			glGetActiveUniformARB(mProgramObject, i, 1024, &length, &size, &type, (GLcharARB *)name);
+
+			if(std::string(name) == "diffuseMap") {
+				diffuseMap = i;
+			}
+
+			if(std::string(name) == "bumpMap") {
+				bumpMap = i;
+			}
+		}
+		
+		if(bumpMap < diffuseMap)
+		{
+			mapUniform(diffuseMap, uniforms);
+			mapUniform(bumpMap, uniforms);
+
+			skip_index.insert(diffuseMap);
+			skip_index.insert(bumpMap);
+		}
+	}
+
+	//........................................................................................
+	
     for (S32 i = 0; i < activeCount; i++)
     {
+		//........................................................................................
+		if(skip_index.end() != skip_index.find(i)) continue;
+		//........................................................................................
+		
         mapUniform(i, uniforms);
     }
+	//........................................................................................................................................
 
     unbind();
 
diff --git a/indra/llrender/llrender.cpp b/indra/llrender/llrender.cpp
index 1ca6e99ecf9..69420dd0bb3 100755
--- a/indra/llrender/llrender.cpp
+++ b/indra/llrender/llrender.cpp
@@ -104,10 +104,10 @@ LLTexUnit::LLTexUnit(S32 index)
 	mCurrColorSrc1(TBS_TEX_COLOR), mCurrColorSrc2(TBS_PREV_COLOR),
 	mCurrAlphaSrc1(TBS_TEX_ALPHA), mCurrAlphaSrc2(TBS_PREV_ALPHA),
 	mCurrColorScale(1), mCurrAlphaScale(1), mCurrTexture(0),
-	mHasMipMaps(false)
+	mHasMipMaps(false),
+	mIndex(index)
 {
 	llassert_always(index < (S32)LL_NUM_TEXTURE_LAYERS);
-	mIndex = index;
 }
 
 //static
@@ -227,33 +227,34 @@ bool LLTexUnit::bind(LLTexture* texture, bool for_rendering, bool forceBind)
 	stop_glerror();
 	if (mIndex >= 0)
 	{
-	gGL.flush();
+		gGL.flush();
+
+		LLImageGL* gl_tex = NULL ;
 
-	LLImageGL* gl_tex = NULL ;
 		if (texture != NULL && (gl_tex = texture->getGLTexture()))
-	{
-			if (gl_tex->getTexName()) //if texture exists
-	{
-	//in audit, replace the selected texture by the default one.
-	if ((mCurrTexture != gl_tex->getTexName()) || forceBind)
-	{
-		activate();
-		enable(gl_tex->getTarget());
-		mCurrTexture = gl_tex->getTexName();
-		glBindTexture(sGLTextureType[gl_tex->getTarget()], mCurrTexture);
-		if(gl_tex->updateBindStats(gl_tex->mTextureMemory))
-		{
-			texture->setActive() ;
-			texture->updateBindStatsForTester() ;
-		}
-		mHasMipMaps = gl_tex->mHasMipMaps;
-		if (gl_tex->mTexOptionsDirty)
 		{
-			gl_tex->mTexOptionsDirty = false;
-			setTextureAddressMode(gl_tex->mAddressMode);
-			setTextureFilteringOption(gl_tex->mFilterOption);
-		}
-	}
+			if (gl_tex->getTexName()) //if texture exists
+			{
+				//in audit, replace the selected texture by the default one.
+				if ((mCurrTexture != gl_tex->getTexName()) || forceBind)
+				{
+					activate();
+					enable(gl_tex->getTarget());
+					mCurrTexture = gl_tex->getTexName();
+					glBindTexture(sGLTextureType[gl_tex->getTarget()], mCurrTexture);
+					if(gl_tex->updateBindStats(gl_tex->mTextureMemory))
+					{
+						texture->setActive() ;
+						texture->updateBindStatsForTester() ;
+					}
+					mHasMipMaps = gl_tex->mHasMipMaps;
+					if (gl_tex->mTexOptionsDirty)
+					{
+						gl_tex->mTexOptionsDirty = false;
+						setTextureAddressMode(gl_tex->mAddressMode);
+						setTextureFilteringOption(gl_tex->mFilterOption);
+					}
+				}
 			}
 			else
 			{
diff --git a/indra/llrender/llrender.h b/indra/llrender/llrender.h
index 42b02a81598..a67fb8da522 100755
--- a/indra/llrender/llrender.h
+++ b/indra/llrender/llrender.h
@@ -198,7 +198,7 @@ class LLTexUnit
 	void setHasMipMaps(bool hasMips) { mHasMipMaps = hasMips; }
 
 protected:
-	S32					mIndex;
+	const S32			mIndex;
 	U32					mCurrTexture;
 	eTextureType		mCurrTexType;
 	eTextureBlendType	mCurrBlendType;
diff --git a/indra/newview/lldrawpoolavatar.cpp b/indra/newview/lldrawpoolavatar.cpp
index ab96201a635..c92c7355847 100755
--- a/indra/newview/lldrawpoolavatar.cpp
+++ b/indra/newview/lldrawpoolavatar.cpp
@@ -1799,9 +1799,12 @@ void LLDrawPoolAvatar::renderRigged(LLVOAvatar* avatar, U32 type, bool glow)
 
 			if (mat)
 			{
-				gGL.getTexUnit(sDiffuseChannel)->bind(face->getTexture(LLRender::DIFFUSE_MAP));
-				gGL.getTexUnit(normal_channel)->bind(face->getTexture(LLRender::NORMAL_MAP));
+				//order is important here LLRender::DIFFUSE_MAP should be last, becouse it change 
+				//(gGL).mCurrTextureUnitIndex
 				gGL.getTexUnit(specular_channel)->bind(face->getTexture(LLRender::SPECULAR_MAP));
+				gGL.getTexUnit(normal_channel)->bind(face->getTexture(LLRender::NORMAL_MAP));
+				gGL.getTexUnit(sDiffuseChannel)->bind(face->getTexture(LLRender::DIFFUSE_MAP), false, true);
+
 
 				LLColor4 col = mat->getSpecularLightColor();
 				F32 spec = mat->getSpecularLightExponent()/255.f;
-- 
GitLab