From 882118612d4315245813bb5e49c0a3ec0ebf36ea Mon Sep 17 00:00:00 2001 From: dfighter1985 Date: Mon, 9 Sep 2013 05:00:53 +0200 Subject: [PATCH] Moved some constants to uniforms. --HG-- branch : gsoc2013-dfighter --- .../OpenGL3/driver_glsl_shader_generator.cpp | 62 +++++---------- .../nel/src/3d/driver/OpenGL3/driver_opengl.h | 1 + .../driver/OpenGL3/driver_opengl_material.cpp | 42 ++++++---- .../driver/OpenGL3/driver_opengl_program.cpp | 77 ++++++++++++++++++- 4 files changed, 120 insertions(+), 62 deletions(-) diff --git a/code/nel/src/3d/driver/OpenGL3/driver_glsl_shader_generator.cpp b/code/nel/src/3d/driver/OpenGL3/driver_glsl_shader_generator.cpp index e2815ece6..4ee0bcc8a 100644 --- a/code/nel/src/3d/driver/OpenGL3/driver_glsl_shader_generator.cpp +++ b/code/nel/src/3d/driver/OpenGL3/driver_glsl_shader_generator.cpp @@ -258,41 +258,20 @@ namespace NL3D void CGLSLShaderGenerator::addDiffuse() { - ss << "vec4 diffuse = vec4( "; - ss << float( material->getDiffuse().R / 255.0f ) << ", "; - ss << float( material->getDiffuse().G / 255.0f ) << ", "; - ss << float( material->getDiffuse().B / 255.0f ) << ", "; - ss << float( material->getDiffuse().A / 255.0f ) << " );"; - ss << std::endl; + ss << "uniform vec4 diffuse;" << std::endl; } void CGLSLShaderGenerator::addColor() { - CRGBA color = material->getColor(); - ss << "vec4 mcolor = vec4( "; - ss << color.R / 255.0f << ", "; - ss << color.G / 255.0f << ", "; - ss << color.B / 255.0f << ", "; - ss << color.A / 255.0f << " );"; - ss << std::endl; + ss << "uniform vec4 mcolor;" << std::endl; } void CGLSLShaderGenerator::addConstants() { - int j = 0; - for( int i = TexCoord0; i < TexCoord4; i++ ) - { - if( hasFlag( vbFormat, vertexFlags[ i ] ) ) - { - ss << "vec4 " << constantNames[ j ] << " = vec4( "; - ss << float( material->_TexEnvs[ j ].ConstantColor.R / 255.0f ) << ", "; - ss << float( material->_TexEnvs[ j ].ConstantColor.G / 255.0f ) << ", "; - ss << float( material->_TexEnvs[ j ].ConstantColor.B / 255.0f ) << ", "; - ss << float( material->_TexEnvs[ j ].ConstantColor.A / 255.0f ) << " );"; - ss << std::endl; - } - j++; - } + ss << "uniform vec4 contant0;" << std::endl; + ss << "uniform vec4 contant1;" << std::endl; + ss << "uniform vec4 contant2;" << std::endl; + ss << "uniform vec4 contant3;" << std::endl; } void CGLSLShaderGenerator::generateNormalVS() @@ -483,13 +462,14 @@ namespace NL3D } sampler++; } + addColor(); + addConstants(); + ss << std::endl; ss << "void main( void )" << std::endl; ss << "{" << std::endl; - addColor(); - bool textures = false; sampler = 0; for( int i = TexCoord0; i < NumOffsets; i++ ) @@ -533,8 +513,6 @@ namespace NL3D void CGLSLShaderGenerator::generateTexEnv() { - addConstants(); - uint32 stage = 0; for( int i = TexCoord0; i < TexCoord4; i++ ) { @@ -890,13 +868,14 @@ namespace NL3D ss << "uniform vec4 constant1;" << std::endl; ss << "uniform vec4 constant2;" << std::endl; ss << "uniform vec4 constant3;" << std::endl; + + addDiffuse(); + ss << std::endl; ss << "void main( void )" << std::endl; ss << "{" << std::endl; - addDiffuse(); - // Lightmap UV coords are at position 1 for( int i = 0; i < ntextures - 1; i++ ) { @@ -939,11 +918,10 @@ namespace NL3D ss << "smooth in vec3 cubeTexCoords;" << std::endl; ss << "uniform sampler2D sampler0;" << std::endl; ss << "uniform samplerCube cubeSampler;" << std::endl; - ss << "void main( void )" << std::endl; - ss << "{" << 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 texel;" << std::endl; @@ -968,15 +946,15 @@ namespace NL3D if( material->getShader() == CMaterial::PerPixelLighting ) ss << "uniform samplerCube cubeSampler2;" << std::endl; + addDiffuse(); + + addConstants(); + ss << std::endl; ss << "void main( void )" << std::endl; ss << "{" << std::endl; - addConstants(); - - addDiffuse(); - ss << "vec4 texel0 = textureCube( cubeSampler0, cubeTexCoords0 );" << std::endl; ss << "vec4 texel1 = texture2D( sampler1, texCoord1 );" << std::endl; @@ -1067,13 +1045,11 @@ namespace NL3D { ss << "uniform sampler2D sampler0;" << std::endl; ss << "uniform sampler2D sampler1;" << std::endl; + addDiffuse(); ss << std::endl; ss << "void main( void )" << std::endl; ss << "{" << std::endl; - - addDiffuse(); - ss << "vec4 tex0 = texture2D( sampler0, texCoord0 );" << std::endl; ss << "vec4 tex1 = texture2D( sampler1, texCoord1 );" << std::endl; ss << "vec4 tex = mix( tex0, tex1, diffuse.a );" << std::endl; diff --git a/code/nel/src/3d/driver/OpenGL3/driver_opengl.h b/code/nel/src/3d/driver/OpenGL3/driver_opengl.h index 1363b96cf..a12d81a68 100644 --- a/code/nel/src/3d/driver/OpenGL3/driver_opengl.h +++ b/code/nel/src/3d/driver/OpenGL3/driver_opengl.h @@ -380,6 +380,7 @@ public: virtual bool setupMaterial(CMaterial& mat); bool setupProgram(CMaterial& mat); + void setupUniforms(CMaterial& mat); void releaseProgram(); virtual void startSpecularBatch(); diff --git a/code/nel/src/3d/driver/OpenGL3/driver_opengl_material.cpp b/code/nel/src/3d/driver/OpenGL3/driver_opengl_material.cpp index 25174adf8..1276e9dd3 100644 --- a/code/nel/src/3d/driver/OpenGL3/driver_opengl_material.cpp +++ b/code/nel/src/3d/driver/OpenGL3/driver_opengl_material.cpp @@ -587,20 +587,38 @@ void CDriverGL3::endMultiPass() } } - const char *samplers[ 4 ] = - { - "sampler0", - "sampler1", - "sampler2", - "sampler3" - }; +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; - for( int i = 0; i < 4; i++ ) + for( int i = 0; i < IDRV_MAT_MAXTEXTURES; i++ ) { + // Set constant + int cl = getUniformLocation( constNames[ i ] ); + if( cl != -1 ) + { + GLfloat glCol[ 4 ]; + convColor( mat._TexEnvs[ i ].ConstantColor, glCol ); + setUniform4f( cl, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ] ); + } + + // Set texture ITexture *t = mat.getTexture( i ); if( t == NULL ) continue; @@ -676,14 +694,6 @@ sint CDriverGL3::beginLightMapMultiPass () return std::max (_NLightMapPass, (uint)1); } -const char *constNames[ 4 ] = -{ - "constant0", - "constant1", - "constant2", - "constant3" -}; - // *************************************************************************** void CDriverGL3::setupLightMapPass(uint pass) { diff --git a/code/nel/src/3d/driver/OpenGL3/driver_opengl_program.cpp b/code/nel/src/3d/driver/OpenGL3/driver_opengl_program.cpp index b8bc77534..d0e3cb488 100644 --- a/code/nel/src/3d/driver/OpenGL3/driver_opengl_program.cpp +++ b/code/nel/src/3d/driver/OpenGL3/driver_opengl_program.cpp @@ -23,6 +23,18 @@ #include "driver_opengl_vertex_buffer_hard.h" #include "nel/3d/i_program_object.h" +namespace +{ + const char *constNames[ NL3D::IDRV_MAT_MAXTEXTURES ] = + { + "constant0", + "constant1", + "constant2", + "constant3" + }; + +} + namespace NL3D { bool CDriverGL3::activeProgramObject( IProgramObject *program ) @@ -215,7 +227,19 @@ namespace NL3D desc.setProgram( p ); shaderCache.cacheShader( desc ); } - + + setupUniforms( mat ); + +#endif + + return true; + } + + void CDriverGL3::setupUniforms( CMaterial& mat ) + { + +#ifdef GLSL + int mvpIndex = getUniformLocation( "mvpMatrix" ); if( mvpIndex != -1 ) { @@ -223,9 +247,56 @@ namespace NL3D setUniformMatrix4fv( mvpIndex, 1, false, mat.get() ); } -#endif + int colorIndex = getUniformLocation( "mcolor" ); + if( colorIndex != -1 ) + { + GLfloat glCol[ 4 ]; + CRGBA col = mat.getColor(); + glCol[ 0 ] = col.R / 255.0f; + glCol[ 1 ] = col.G / 255.0f; + glCol[ 2 ] = col.B / 255.0f; + glCol[ 3 ] = col.A / 255.0f; + + setUniform4f( colorIndex, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ] ); + } - return true; + int diffuseIndex = getUniformLocation( "diffuse" ); + if( diffuseIndex != -1 ) + { + GLfloat glCol[ 4 ]; + CRGBA col = mat.getDiffuse(); + glCol[ 0 ] = col.R / 255.0f; + glCol[ 1 ] = col.G / 255.0f; + glCol[ 2 ] = col.B / 255.0f; + glCol[ 3 ] = col.A / 255.0f; + + setUniform4f( diffuseIndex, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ] ); + } + + + // Lightmaps have special constants + if( mat.getShader() != CMaterial::LightMap ) + { + + for( int i = 0; i < IDRV_MAT_MAXTEXTURES; i++ ) + { + int cl = getUniformLocation( constNames[ i ] ); + if( cl != -1 ) + { + CRGBA col = mat._TexEnvs[ i ].ConstantColor; + GLfloat glCol[ 4 ]; + glCol[ 0 ] = col.R / 255.0f; + glCol[ 1 ] = col.G / 255.0f; + glCol[ 2 ] = col.B / 255.0f; + glCol[ 3 ] = col.A / 255.0f; + + setUniform4f( cl, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ] ); + } + } + + } + +#endif } void CDriverGL3::releaseProgram()