From 99371f892e7b35739be01e63036b4355e0e4dfe7 Mon Sep 17 00:00:00 2001
From: Jonathan Goodman <geenz@geenzo.com>
Date: Tue, 28 Aug 2012 14:44:28 -0400
Subject: [PATCH] Added new bindTexture and unbindTexture API to LLGLSLShader. 
 Reviewed by davep.

---
 indra/llrender/llglslshader.cpp | 52 +++++++++++++++++++++++++++++++++
 indra/llrender/llglslshader.h   |  9 +++++-
 2 files changed, 60 insertions(+), 1 deletion(-)

diff --git a/indra/llrender/llglslshader.cpp b/indra/llrender/llglslshader.cpp
index d345b6593ee..04878cabe2e 100644
--- a/indra/llrender/llglslshader.cpp
+++ b/indra/llrender/llglslshader.cpp
@@ -476,6 +476,58 @@ void LLGLSLShader::bindNoShader(void)
 	}
 }
 
+S32 LLGLSLShader::bindTexture(const std::string &uniform, LLTexture *texture, LLTexUnit::eTextureType mode)
+{
+	S32 channel = 0;
+	channel = getUniformLocation(uniform);
+	
+	return bindTexture(channel, texture, mode);
+}
+
+S32 LLGLSLShader::bindTexture(S32 uniform, LLTexture *texture, LLTexUnit::eTextureType mode)
+{
+	if (uniform < 0 || uniform >= (S32)mTexture.size())
+	{
+		UNIFORM_ERRS << "Uniform out of range: " << uniform << LL_ENDL;
+		return -1;
+	}
+	
+	uniform = mTexture[uniform];
+	
+	if (uniform > -1)
+	{
+		gGL.getTexUnit(uniform)->bind(texture, mode);
+	}
+	
+	return uniform;
+}
+
+S32 LLGLSLShader::unbindTexture(const std::string &uniform, LLTexUnit::eTextureType mode)
+{
+	S32 channel = 0;
+	channel = getUniformLocation(uniform);
+	
+	return unbindTexture(channel);
+}
+
+S32 LLGLSLShader::unbindTexture(S32 uniform, LLTexUnit::eTextureType mode)
+{
+	if (uniform < 0 || uniform >= (S32)mTexture.size())
+	{
+		UNIFORM_ERRS << "Uniform out of range: " << uniform << LL_ENDL;
+		return -1;
+	}
+	
+	uniform = mTexture[uniform];
+	
+	if (uniform > -1)
+	{
+		gGL.getTexUnit(uniform)->unbind(mode);
+	}
+	
+	return uniform;
+}
+
 S32 LLGLSLShader::enableTexture(S32 uniform, LLTexUnit::eTextureType mode)
 {
 	if (uniform < 0 || uniform >= (S32)mTexture.size())
diff --git a/indra/llrender/llglslshader.h b/indra/llrender/llglslshader.h
index bee5d9adda8..185e6e7aeb8 100644
--- a/indra/llrender/llglslshader.h
+++ b/indra/llrender/llglslshader.h
@@ -130,7 +130,14 @@ class LLGLSLShader
 	//the corresponding channel will be active upon return
 	//returns channel texture is enabled in from [0-MAX)
 	S32 enableTexture(S32 uniform, LLTexUnit::eTextureType mode = LLTexUnit::TT_TEXTURE);
-	S32 disableTexture(S32 uniform, LLTexUnit::eTextureType mode = LLTexUnit::TT_TEXTURE); 
+	S32 disableTexture(S32 uniform, LLTexUnit::eTextureType mode = LLTexUnit::TT_TEXTURE);
+	
+	// bindTexture returns the texture unit we've bound the texture to.
+	// You can reuse the return value to unbind a texture when required.
+	S32 bindTexture(const std::string& uniform, LLTexture *texture, LLTexUnit::eTextureType mode = LLTexUnit::TT_TEXTURE);
+	S32 bindTexture(S32 uniform, LLTexture *texture, LLTexUnit::eTextureType mode = LLTexUnit::TT_TEXTURE);
+	S32 unbindTexture(const std::string& uniform, LLTexUnit::eTextureType mode = LLTexUnit::TT_TEXTURE);
+	S32 unbindTexture(S32 uniform, LLTexUnit::eTextureType mode = LLTexUnit::TT_TEXTURE);
 	
     BOOL link(BOOL suppress_errors = FALSE);
 	void bind();
-- 
GitLab