Uniform index caching.

--HG--
branch : gsoc2013-dfighter
hg/feature/gsoc2013-dfighter
dfighter1985 11 years ago
parent 16290f7ff9
commit 1acccdb5ef

@ -26,7 +26,30 @@ namespace NL3D
class IProgramObject
{
public:
enum EUniform
{
MVPMatrix,
MVMatrix,
TexMatrix0,
TexMatrix1,
TexMatrix2,
TexMatrix3,
Constant0,
Constant1,
Constant2,
Constant3,
Diffuse,
Color,
Sampler0,
Sampler1,
Sampler2,
Sampler3,
NUM_UNIFORMS
};
IProgramObject(){}
virtual ~IProgramObject(){}
virtual bool attachVertexProgram( IProgram *shader ) = 0;
@ -43,6 +66,10 @@ namespace NL3D
virtual bool validate( std::string &log ) = 0;
virtual void cacheUniformIndices() = 0;
virtual int getUniformIndex( EUniform uniform ) = 0;
bool isLinked() const{ return linked; }
protected:

@ -31,7 +31,7 @@ namespace NL3D
programId = nglCreateProgram();
nlassert( programId != 0 );
linked = false;
std::fill( uniformIndices, uniformIndices + NUM_UNIFORMS, -1 );
}
CGLSLProgram::~CGLSLProgram()
@ -173,6 +173,43 @@ namespace NL3D
return true;
}
const char *uniformNames[ CGLSLProgram::NUM_UNIFORMS ] =
{
"mvpMatrix",
"mvMatrix",
"texMatrix0",
"texMatrix1",
"texMatrix2",
"texMatrix3",
"constant0",
"constant1",
"constant2",
"constant3",
"diffuse",
"mcolor",
"sampler0",
"sampler1",
"sampler2",
"sampler3"
};
void CGLSLProgram::cacheUniformIndices()
{
nlassert( programId != 0 );
for( int i = MVPMatrix; i < NUM_UNIFORMS; i++ )
{
uniformIndices[ i ] = nglGetUniformLocation( programId, uniformNames[ i ] );
}
}
int CGLSLProgram::getUniformIndex( EUniform uniform )
{
nlassert( uniform < NUM_UNIFORMS );
return uniformIndices[ uniform ];
}
void CGLSLProgram::deleteShaders()
{
std::vector< IProgram* >::iterator itr;

@ -41,9 +41,15 @@ namespace NL3D
bool validate( std::string &log );
void cacheUniformIndices();
int getUniformIndex( EUniform uniform );
private:
void deleteShaders();
int uniformIndices[ NUM_UNIFORMS ];
std::vector< IProgram* > vertexPrograms;
std::vector< IProgram* > pixelPrograms;
};

@ -268,10 +268,10 @@ namespace NL3D
void CGLSLShaderGenerator::addConstants()
{
ss << "uniform vec4 contant0;" << std::endl;
ss << "uniform vec4 contant1;" << std::endl;
ss << "uniform vec4 contant2;" << std::endl;
ss << "uniform vec4 contant3;" << std::endl;
ss << "uniform vec4 constant0;" << std::endl;
ss << "uniform vec4 constant1;" << std::endl;
ss << "uniform vec4 constant2;" << std::endl;
ss << "uniform vec4 constant3;" << std::endl;
}
void CGLSLShaderGenerator::generateNormalVS()
@ -297,7 +297,7 @@ namespace NL3D
{
ss << "uniform mat4 mvMatrix;" << std::endl;
ss << "uniform mat4 texMatrix;" << std::endl;
ss << "uniform mat4 texMatrix0;" << std::endl;
ss << "smooth out vec3 cubeTexCoords;" << std::endl;
ss << std::endl;
@ -315,7 +315,7 @@ namespace NL3D
ss << "vec3 n = vnormal.xyz;" << std::endl;
ss << "cubeTexCoords = ReflectionMap( ep, n );" << std::endl;
ss << "vec4 t = vec4( cubeTexCoords, 1.0 );" << std::endl;
ss << "t = t * texMatrix;" << std::endl;
ss << "t = t * texMatrix0;" << std::endl;
ss << "cubeTexCoords = t.xyz;" << std::endl;
ss << "gl_Position = mvpMatrix * v" << attribNames[ 0 ] << ";" << std::endl;
@ -863,11 +863,7 @@ namespace NL3D
for( int i = 0; i < ntextures; i++ )
ss << "uniform sampler2D sampler" << i << ";" << std::endl;
ss << "uniform vec4 constant0;" << std::endl;
ss << "uniform vec4 constant1;" << std::endl;
ss << "uniform vec4 constant2;" << std::endl;
ss << "uniform vec4 constant3;" << std::endl;
addConstants();
addDiffuse();
@ -917,13 +913,13 @@ namespace NL3D
{
ss << "smooth in vec3 cubeTexCoords;" << std::endl;
ss << "uniform sampler2D sampler0;" << std::endl;
ss << "uniform samplerCube cubeSampler;" << std::endl;
ss << "uniform samplerCube sampler1;" << std::endl;
addDiffuse();
ss << "void main( void )" << std::endl;
ss << "{" << std::endl;
ss << "vec4 texel0 = texture2D( sampler0, texCoord0 );" << std::endl;
ss << "vec4 texel1 = textureCube( cubeSampler, cubeTexCoords );" << std::endl;
ss << "vec4 texel1 = textureCube( sampler1, cubeTexCoords );" << std::endl;
ss << "vec4 texel;" << std::endl;
ss << "texel.rgb = texel0.rgb * diffuse;" << std::endl;
ss << "texel.a = texel0.a;" << std::endl;

@ -20,6 +20,7 @@
#include "nel/3d/texture_mem.h"
#include "nel/3d/texture_bump.h"
#include "nel/3d/material.h"
#include "nel/3d/i_program_object.h"
namespace NL3D {
@ -587,22 +588,6 @@ void CDriverGL3::endMultiPass()
}
}
const char *samplers[ IDRV_MAT_MAXTEXTURES ] =
{
"sampler0",
"sampler1",
"sampler2",
"sampler3"
};
const char *constNames[ IDRV_MAT_MAXTEXTURES ] =
{
"constant0",
"constant1",
"constant2",
"constant3"
};
void CDriverGL3::setupNormalPass()
{
const CMaterial &mat = *_CurrentMaterial;
@ -610,7 +595,7 @@ void CDriverGL3::setupNormalPass()
for( int i = 0; i < IDRV_MAT_MAXTEXTURES; i++ )
{
// Set constant
int cl = getUniformLocation( constNames[ i ] );
int cl = currentProgram->getUniformIndex( IProgramObject::EUniform( IProgramObject::Constant0 + i ) );
if( cl != -1 )
{
GLfloat glCol[ 4 ];
@ -623,7 +608,7 @@ void CDriverGL3::setupNormalPass()
if( t == NULL )
continue;
int index = getUniformLocation( samplers[ i ] );
int index = currentProgram->getUniformIndex( IProgramObject::EUniform( IProgramObject::Sampler0 + i ) );
if( index == -1 )
continue;
@ -808,7 +793,7 @@ void CDriverGL3::setupLightMapPass(uint pass)
// setup constant color with Lightmap factor.
stdEnv.ConstantColor=lmapFactor;
#ifdef GLSL
int cl = getUniformLocation( constNames[ stage ] );
int cl = currentProgram->getUniformIndex( IProgramObject::EUniform( IProgramObject::Constant0 + stage ) );
if( cl != -1 )
{
GLfloat glCol[ 4 ];
@ -816,7 +801,7 @@ void CDriverGL3::setupLightMapPass(uint pass)
setUniform4f( cl, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ] );
}
int tl = getUniformLocation( samplers[ stage ] );
int tl = currentProgram->getUniformIndex( IProgramObject::EUniform( IProgramObject::Sampler0 + stage ) );
if( tl != -1 )
{
setUniform1i( tl, stage );
@ -885,7 +870,7 @@ void CDriverGL3::setupLightMapPass(uint pass)
_DriverGLStates.setTexGenMode(stage, 0);
#ifdef GLSL
int tl = getUniformLocation( samplers[ stage ] );
int tl = currentProgram->getUniformIndex( IProgramObject::EUniform( IProgramObject::Sampler0 + stage ) );
if( tl != -1 )
{
setUniform1i( tl, stage );
@ -1164,25 +1149,25 @@ void CDriverGL3::setupSpecularPass(uint pass)
#ifdef GLSL
int sl0 = getUniformLocation( "sampler0" );
int sl0 = currentProgram->getUniformIndex( IProgramObject::Sampler0 );
if( sl0 != -1 )
{
setUniform1i( sl0, 0 );
}
int sl1 = getUniformLocation( "cubeSampler" );
int sl1 = currentProgram->getUniformIndex( IProgramObject::Sampler1 );
if( sl1 != -1 )
{
setUniform1i( sl1, 1 );
}
int mvl = getUniformLocation( "mvMatrix" );
int mvl = currentProgram->getUniformIndex( IProgramObject::MVMatrix );
if( mvl != -1 )
{
setUniformMatrix4fv( mvl, 1, false, _ModelViewMatrix.get() );
}
int tml = getUniformLocation( "texMatrix" );
int tml = currentProgram->getUniformIndex( IProgramObject::TexMatrix0 );
if( tml != -1 )
{
setUniformMatrix4fv( mvl, 1, false, _UserTexMat[ 1 ].get() );

@ -224,6 +224,7 @@ namespace NL3D
if( !activeProgramObject( p ) )
return false;
p->cacheUniformIndices();
desc.setProgram( p );
shaderCache.cacheShader( desc );
}
@ -240,14 +241,14 @@ namespace NL3D
#ifdef GLSL
int mvpIndex = getUniformLocation( "mvpMatrix" );
int mvpIndex = currentProgram->getUniformIndex( IProgramObject::MVPMatrix );
if( mvpIndex != -1 )
{
CMatrix mat = _GLProjMat * _ModelViewMatrix;
setUniformMatrix4fv( mvpIndex, 1, false, mat.get() );
}
int colorIndex = getUniformLocation( "mcolor" );
int colorIndex = currentProgram->getUniformIndex( IProgramObject::Color );
if( colorIndex != -1 )
{
GLfloat glCol[ 4 ];
@ -260,7 +261,7 @@ namespace NL3D
setUniform4f( colorIndex, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ] );
}
int diffuseIndex = getUniformLocation( "diffuse" );
int diffuseIndex = currentProgram->getUniformIndex( IProgramObject::Diffuse );
if( diffuseIndex != -1 )
{
GLfloat glCol[ 4 ];
@ -280,7 +281,7 @@ namespace NL3D
for( int i = 0; i < IDRV_MAT_MAXTEXTURES; i++ )
{
int cl = getUniformLocation( constNames[ i ] );
int cl = currentProgram->getUniformIndex( IProgramObject::EUniform( IProgramObject::Constant0 + i ) );
if( cl != -1 )
{
CRGBA col = mat._TexEnvs[ i ].ConstantColor;

Loading…
Cancel
Save