From cdfbb7a7535ea68cd96d8eb9193d18034886a23e Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 25 Nov 2010 12:30:38 +0000 Subject: [PATCH] From Michael Platings, To address performance bottleneck that occurs when using large number of uniforms introduced a name to uniqued ID scheme for Uniforms so comparisons can be done on a uint rather than a string. --- include/osg/Program | 11 ++++++----- include/osg/State | 2 +- include/osg/Uniform | 7 +++++++ src/osg/Program.cpp | 12 ++++++------ src/osg/Uniform.cpp | 30 ++++++++++++++++++++++++++++-- 5 files changed, 48 insertions(+), 14 deletions(-) diff --git a/include/osg/Program b/include/osg/Program index a9c87e07b..f57d0130c 100644 --- a/include/osg/Program +++ b/include/osg/Program @@ -147,8 +147,9 @@ class OSG_EXPORT Program : public osg::StateAttribute GLenum _type; GLint _size; }; + typedef std::map< unsigned int, ActiveVarInfo > ActiveUniformMap; typedef std::map< std::string, ActiveVarInfo > ActiveVarInfoMap; - const ActiveVarInfoMap& getActiveUniforms(unsigned int contextID) const; + const ActiveUniformMap& getActiveUniforms(unsigned int contextID) const; const ActiveVarInfoMap& getActiveAttribs(unsigned int contextID) const; public: @@ -189,7 +190,7 @@ class OSG_EXPORT Program : public osg::StateAttribute inline void apply(const Uniform& uniform) const { - GLint location = getUniformLocation(uniform.getName()); + GLint location = getUniformLocation(uniform.getNameID()); if (location>=0) { if ((unsigned int)location>=_lastAppliedUniformList.size()) _lastAppliedUniformList.resize(location+1); @@ -211,10 +212,10 @@ class OSG_EXPORT Program : public osg::StateAttribute } } - const ActiveVarInfoMap& getActiveUniforms() const {return _uniformInfoMap;} + const ActiveUniformMap& getActiveUniforms() const {return _uniformInfoMap;} const ActiveVarInfoMap& getActiveAttribs() const {return _attribInfoMap;} - inline GLint getUniformLocation( const std::string& name ) const { ActiveVarInfoMap::const_iterator itr = _uniformInfoMap.find(name); return (itr!=_uniformInfoMap.end()) ? itr->second._location : -1; } + inline GLint getUniformLocation( unsigned int uniformNameID ) const { ActiveUniformMap::const_iterator itr = _uniformInfoMap.find(uniformNameID); return (itr!=_uniformInfoMap.end()) ? itr->second._location : -1; } inline GLint getAttribLocation( const std::string& name ) const { ActiveVarInfoMap::const_iterator itr = _attribInfoMap.find(name); return (itr!=_attribInfoMap.end()) ? itr->second._location : -1; } inline void addShaderToAttach(Shader *shader) @@ -243,7 +244,7 @@ class OSG_EXPORT Program : public osg::StateAttribute bool _isLinked; const unsigned int _contextID; - ActiveVarInfoMap _uniformInfoMap; + ActiveUniformMap _uniformInfoMap; ActiveVarInfoMap _attribInfoMap; typedef std::pair, unsigned int> UniformModifiedCountPair; diff --git a/include/osg/State b/include/osg/State index 18d6c0650..c8a1c11cf 100644 --- a/include/osg/State +++ b/include/osg/State @@ -1253,7 +1253,7 @@ class OSG_EXPORT State : public Referenced, public Observer } inline const Program::PerContextProgram* getLastAppliedProgramObject() const { return _lastAppliedProgramObject; } - inline GLint getUniformLocation( const std::string& name ) const { return _lastAppliedProgramObject ? _lastAppliedProgramObject->getUniformLocation(name) : -1; } + inline GLint getUniformLocation( unsigned int uniformNameID ) const { return _lastAppliedProgramObject ? _lastAppliedProgramObject->getUniformLocation(uniformNameID) : -1; } inline GLint getAttribLocation( const std::string& name ) const { return _lastAppliedProgramObject ? _lastAppliedProgramObject->getAttribLocation(name) : -1; } typedef std::pair AttributePair; diff --git a/include/osg/Uniform b/include/osg/Uniform index 48d4e1eb9..0207f7141 100644 --- a/include/osg/Uniform +++ b/include/osg/Uniform @@ -268,6 +268,9 @@ class OSG_EXPORT Uniform : public Object /** Return the internal data array type corresponding to a GLSL type */ static GLenum getInternalArrayType( Type t ); + /** Return the number that the name maps to uniquely */ + static unsigned int getNameID(const std::string& name); + /** convenient scalar (non-array) constructors w/ assignment */ explicit Uniform( const char* name, float f ); explicit Uniform( const char* name, int i ); @@ -472,6 +475,8 @@ class OSG_EXPORT Uniform : public Object inline void setModifiedCount(unsigned int mc) { _modifiedCount = mc; } inline unsigned int getModifiedCount() const { return _modifiedCount; } + /** Get the number that the Uniform's name maps to uniquely */ + unsigned int getNameID() const; void apply(const GL2Extensions* ext, GLint location) const; @@ -493,6 +498,8 @@ class OSG_EXPORT Uniform : public Object Type _type; unsigned int _numElements; + unsigned int _nameID; + // The internal data for osg::Uniforms are stored as an array of // getInternalArrayType() of length getInternalArrayNumElements(). diff --git a/src/osg/Program.cpp b/src/osg/Program.cpp index 38768caf1..ac2945f5e 100644 --- a/src/osg/Program.cpp +++ b/src/osg/Program.cpp @@ -452,7 +452,7 @@ bool Program::getGlProgramInfoLog(unsigned int contextID, std::string& log) cons return getPCP( contextID )->getInfoLog( log ); } -const Program::ActiveVarInfoMap& Program::getActiveUniforms(unsigned int contextID) const +const Program::ActiveUniformMap& Program::getActiveUniforms(unsigned int contextID) const { return getPCP( contextID )->getActiveUniforms(); } @@ -609,13 +609,13 @@ void Program::PerContextProgram::linkProgram(osg::State& state) if( loc != -1 ) { - _uniformInfoMap[reinterpret_cast(name)] = ActiveVarInfo(loc,type,size); + _uniformInfoMap[Uniform::getNameID(reinterpret_cast(name))] = ActiveVarInfo(loc,type,size); OSG_INFO << "\tUniform \"" << name << "\"" - << " loc="<< loc - << " size="<< size - << " type=" << Uniform::getTypename((Uniform::Type)type) - << std::endl; + << " loc="<< loc + << " size="<< size + << " type=" << Uniform::getTypename((Uniform::Type)type) + << std::endl; } } delete [] name; diff --git a/src/osg/Uniform.cpp b/src/osg/Uniform.cpp index 2bbf4dce2..02e9ef1c5 100644 --- a/src/osg/Uniform.cpp +++ b/src/osg/Uniform.cpp @@ -21,6 +21,7 @@ #include #include +#include #include using namespace osg; @@ -30,13 +31,13 @@ using namespace osg; /////////////////////////////////////////////////////////////////////////// Uniform::Uniform() : - _type(UNDEFINED), _numElements(0), _modifiedCount(0) + _type(UNDEFINED), _numElements(0), _nameID(UINT_MAX), _modifiedCount(0) { } Uniform::Uniform( Type type, const std::string& name, int numElements ) : - _type(type), _numElements(0), _modifiedCount(0) + _type(type), _numElements(0), _nameID(UINT_MAX), _modifiedCount(0) { setName(name); setNumElements(numElements); @@ -88,6 +89,7 @@ void Uniform::setName( const std::string& name ) return; } _name = name; + _nameID = getNameID(name); } void Uniform::setNumElements( unsigned int numElements ) @@ -255,6 +257,7 @@ void Uniform::copyData(const Uniform& rhs) { // caller must ensure that _type==rhs._type _numElements = rhs._numElements; + _nameID = rhs._nameID; if (rhs._floatArray.valid() || rhs._intArray.valid() || rhs._uintArray.valid()) allocateDataArray(); if( _floatArray.valid() && rhs._floatArray.valid() ) *_floatArray = *rhs._floatArray; if( _intArray.valid() && rhs._intArray.valid() ) *_intArray = *rhs._intArray; @@ -601,6 +604,24 @@ GLenum Uniform::getInternalArrayType( Type t ) } +unsigned int Uniform::getNameID(const std::string& name) +{ + typedef std::map UniformNameIDMap; + static OpenThreads::Mutex s_mutex_uniformNameIDMap; + static UniformNameIDMap s_uniformNameIDMap; + + OpenThreads::ScopedLock lock(s_mutex_uniformNameIDMap); + UniformNameIDMap::iterator it = s_uniformNameIDMap.find(name); + if (it != s_uniformNameIDMap.end()) + { + return it->second; + } + unsigned int id = s_uniformNameIDMap.size(); + s_uniformNameIDMap.insert(UniformNameIDMap::value_type(name, id)); + return id; +} + + /////////////////////////////////////////////////////////////////////////// // value constructors for single-element (ie: non-array) uniforms @@ -1388,6 +1409,11 @@ bool Uniform::getElement( unsigned int index, bool& b0, bool& b1, bool& b2, bool return true; } +unsigned int Uniform::getNameID() const +{ + return _nameID; +} + /////////////////////////////////////////////////////////////////////////// void Uniform::apply(const GL2Extensions* ext, GLint location) const