|
From: <axl...@us...> - 2009-06-16 02:50:28
|
Revision: 341
http://hgengine.svn.sourceforge.net/hgengine/?rev=341&view=rev
Author: axlecrusher
Date: 2009-06-16 00:27:10 +0000 (Tue, 16 Jun 2009)
Log Message:
-----------
use a global uniform register
Modified Paths:
--------------
Mercury2/src/RenderGraph.cpp
Mercury2/src/Shader.cpp
Mercury2/src/Shader.h
Mercury2/src/Texture.cpp
Modified: Mercury2/src/RenderGraph.cpp
===================================================================
--- Mercury2/src/RenderGraph.cpp 2009-06-15 00:08:01 UTC (rev 340)
+++ Mercury2/src/RenderGraph.cpp 2009-06-16 00:27:10 UTC (rev 341)
@@ -8,6 +8,7 @@
void RenderGraphEntry::Render()
{
MercuryMatrix modelView;
+ ShaderAttribute sa;
if (m_node)
{
@@ -17,18 +18,11 @@
modelView.Transpose();
glLoadMatrixf( modelView.Ptr() );
-
- Shader* currentShader = Shader::GetCurrentShader();
- if ( currentShader )
- {
- int location = currentShader->GetUniformLocation("HG_ModelMatrix");
- if ( location != -1 )
- {
- glUniformMatrix4fv(location, 1, 1,m_matrix->Ptr());
- GLERRORCHECK;
- }
- }
+ sa.type = ShaderAttribute::TYPE_MATRIX;
+ sa.value.matrix = m_matrix->Ptr();
+ Shader::SetAttribute("HG_ModelMatrix", sa);
+
m_node->Render( modelView ); //calls on children assets
}
@@ -40,6 +34,7 @@
if (m_node)
{
glLoadMatrixf( modelView.Ptr() );
+ Shader::SetAttribute("HG_ModelMatrix", sa);
m_node->PostRender( modelView ); //calls on children assets
}
}
Modified: Mercury2/src/Shader.cpp
===================================================================
--- Mercury2/src/Shader.cpp 2009-06-15 00:08:01 UTC (rev 340)
+++ Mercury2/src/Shader.cpp 2009-06-16 00:27:10 UTC (rev 341)
@@ -173,6 +173,7 @@
}
free( Buffer );
}
+
return LinkShaders();
}
@@ -327,6 +328,13 @@
glGetActiveUniformARB( iProgramID, i, 1024, &bufflen, &size, &type, buffer );
buffer[bufflen] = 0;
m_uniforms[buffer] = glGetUniformLocationARB( iProgramID, buffer );
+
+ //load in global data if it exists
+ std::map< MString, ShaderAttribute >::iterator sai = m_globalAttributes.find( buffer );
+ if (sai != m_globalAttributes.end())
+ {
+ SetAttributeInternal(sai->first, sai->second);
+ }
}
return true;
}
@@ -408,29 +416,8 @@
glUseProgramObjectARB( iProgramID );
GLERRORCHECK;
-/*
- for( unsigned i = 0; i < m_vShaderTabs.size(); ++i )
- {
- int location = glGetUniformLocationARB( iProgramID, m_vShaderTabs[i]->name.c_str() );
-
- ShaderAttribute * sa = m_vShaderTabs[i];
- if ( sa->ShaderControlled )
- {
- switch( sa->type )
- {
- case ShaderAttribute::TYPE_INT:
- case ShaderAttribute::TYPE_SAMPLER:
- glUniform1iARB( location, sa->value.iInt );
- GLERRORCHECK;
- break;
- case ShaderAttribute::TYPE_FLOAT:
- case ShaderAttribute::TYPE_FLOATV4:
- glUniform4fvARB( location, 4, &sa->value.fFloatV4[0] );
- GLERRORCHECK;
- break;
- };
- }
- }*/
+
+ //set global attributes here
}
void Shader::DeactivateShader()
@@ -447,6 +434,46 @@
return i->second;
}
+void Shader::SetAttribute(const MString& name, const ShaderAttribute& x)
+{
+ m_globalAttributes[name] = x;
+
+ Shader *current = GetCurrentShader();
+ if (current) current->SetAttributeInternal( name, x );
+}
+
+void Shader::RemoveAttribute(const MString& name)
+{
+ std::map< MString, ShaderAttribute>::iterator i = m_globalAttributes.find( name );
+ if ( i != m_globalAttributes.end() ) m_globalAttributes.erase( i );
+ //no sense in unsetting it in the current shader, what would we set it to?
+}
+
+void Shader::SetAttributeInternal(const MString& name, const ShaderAttribute& x)
+{
+ int location = GetUniformLocation( name );
+
+ if ( location != -1 )
+ {
+ switch( x.type )
+ {
+ case ShaderAttribute::TYPE_INT:
+ case ShaderAttribute::TYPE_SAMPLER:
+ glUniform1iARB( location, x.value.iInt );
+ break;
+ case ShaderAttribute::TYPE_FLOAT:
+ case ShaderAttribute::TYPE_FLOATV4:
+ glUniform4fvARB( location, 4, &x.value.fFloatV4[0] );
+ break;
+ case ShaderAttribute::TYPE_MATRIX:
+ glUniformMatrix4fv(location, 1, 1, x.value.matrix); //transpase too
+ };
+ GLERRORCHECK;
+ }
+}
+
+std::map< MString, ShaderAttribute> Shader::m_globalAttributes;
+
/*
* Copyright (c) 2009 Charles Lohr
* All rights reserved.
Modified: Mercury2/src/Shader.h
===================================================================
--- Mercury2/src/Shader.h 2009-06-15 00:08:01 UTC (rev 340)
+++ Mercury2/src/Shader.h 2009-06-16 00:27:10 UTC (rev 341)
@@ -5,11 +5,13 @@
#include <map>
#include <vector>
+#include <MercuryMatrix.h>
+
///Basic Attribute for all shaders
class ShaderAttribute
{
public:
- ShaderAttribute() : type( TYPE_INT ) { value.iInt = 0; ShaderControlled=0;}
+ ShaderAttribute() : type( TYPE_INT ) { value.iInt = 0; }
///Type of ShaderAttribute for shader
enum ShaderAttributeTyp
@@ -17,7 +19,8 @@
TYPE_INT, ///Synonomous to 'int' when passing into a shader
TYPE_SAMPLER, ///Synonomous to 'sampler2D' when passing into a shader
TYPE_FLOAT, ///Synonomous to 'float' when passing into a shader
- TYPE_FLOATV4 ///Synonomous to 'vec4' when passing into a shader
+ TYPE_FLOATV4, ///Synonomous to 'vec4' when passing into a shader
+ TYPE_MATRIX ///Synonomous to 'mat4' when passing into a shader
} type;
///Actual data for value.
@@ -27,10 +30,8 @@
unsigned int iSampler; ///Synonomous to 'sampler2D'
float fFloat; ///Synonomous to 'float'
float fFloatV4[4]; ///Synonomous to 'vec4'
+ const float* matrix; ///Synonomous to 'mat4'
} value;
-
- MString name;
- bool ShaderControlled;
};
///Shader Attribute Retainer
@@ -71,7 +72,9 @@
virtual void PostRender(const MercuryNode* node);
static Shader* Generate() { return new Shader; }
virtual void LoadFromXML(const XMLNode& node);
- int32_t GetUniformLocation(const MString& n);
+
+ static void SetAttribute(const MString& name, const ShaderAttribute& x);
+ static void RemoveAttribute(const MString& name);
///Explicitly get the OpenGL ProgramID in the event you need it for advanced techniques
unsigned int GetProgramID() { return iProgramID; }
@@ -79,6 +82,10 @@
private:
void LoadShader( const MString& path, float priority );
+ int32_t GetUniformLocation(const MString& n);
+
+ void SetAttributeInternal(const MString& name, const ShaderAttribute& x);
+
///Suggested function for loading shaders.
/** This function looks for {sShaderName}.vert and {sShaderName}.frag. It will
attempt to load, compile and link the files. If any errors are found, they will
@@ -142,11 +149,7 @@
unsigned int fragmentShader;
///Shader attributes
- /** This is the system that helps make it possible to blast
- through all attributes currently set up by dereferencing
- the pointers in the attributes repository.
- */
-// std::vector< ShaderAttribute * > m_vShaderTabs;
+ /** These are the attributes linked into the shader */
std::map< MString, int > m_uniforms;
///Name of the shader
@@ -160,6 +163,9 @@
///Original Shader (to re-enable when leaving)
Shader * OriginalShader;
+
+ //global uniform that should be applied to all shaders
+ static std::map< MString, ShaderAttribute > m_globalAttributes;
};
#endif
Modified: Mercury2/src/Texture.cpp
===================================================================
--- Mercury2/src/Texture.cpp 2009-06-15 00:08:01 UTC (rev 340)
+++ Mercury2/src/Texture.cpp 2009-06-16 00:27:10 UTC (rev 341)
@@ -132,17 +132,10 @@
GLERRORCHECK;
- Shader* currentShader = Shader::GetCurrentShader();
- if ( currentShader )
- {
- MString uname = ssprintf("HG_Texture%d", m_activeTextures);
- int location = currentShader->GetUniformLocation( uname );
- if ( location != -1 )
- {
- glUniform1i(location,m_activeTextures);
- GLERRORCHECK;
- }
- }
+ ShaderAttribute sa;
+ sa.type = ShaderAttribute::TYPE_SAMPLER;
+ sa.value.iSampler = m_textureResource;
+ Shader::SetAttribute( ssprintf("HG_Texture%d", m_activeTextures), sa);
++m_activeTextures;
++m_textureBinds;
@@ -155,6 +148,9 @@
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable( GL_TEXTURE_2D );
GLERRORCHECK;
+
+ Shader::RemoveAttribute( ssprintf("HG_Texture%d", m_activeTextures) );
+
--m_activeTextures;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|