diff --git a/source/lib/ffglex/FFGLShader.cpp b/source/lib/ffglex/FFGLShader.cpp index 9cf8fba..4a6d4bb 100644 --- a/source/lib/ffglex/FFGLShader.cpp +++ b/source/lib/ffglex/FFGLShader.cpp @@ -179,6 +179,8 @@ void FFGLShader::FreeGLResources() glDeleteProgram( programID ); programID = 0; } + + uniformLocations.clear(); } void FFGLShader::Set( const char* name, float value ) @@ -229,7 +231,13 @@ GLuint FFGLShader::GetGLID() const */ GLint FFGLShader::FindUniform( const char* name ) const { - return glGetUniformLocation( programID, name ); + auto it = uniformLocations.find( name ); + if( it != uniformLocations.end() ) + return it->second; + + GLint location = glGetUniformLocation( programID, name ); + uniformLocations[ name ] = location; + return location; } bool FFGLShader::CompileVertexShader( const char* vertexShader ) @@ -318,6 +326,9 @@ bool FFGLShader::CompileFragmentShader( const char* fragmentShader ) } bool FFGLShader::LinkProgram() { + //Any cached uniform locations belong to the program we're about to replace, so they're no longer valid. + uniformLocations.clear(); + programID = glCreateProgram(); glAttachShader( programID, vertexShaderID ); diff --git a/source/lib/ffglex/FFGLShader.h b/source/lib/ffglex/FFGLShader.h index a23af93..a82425a 100644 --- a/source/lib/ffglex/FFGLShader.h +++ b/source/lib/ffglex/FFGLShader.h @@ -3,6 +3,7 @@ #include #include #include +#include namespace ffglex { @@ -47,6 +48,7 @@ class FFGLShader final GLuint fragmentShaderID; //!< The ID OpenGL gave our fragment shader. 0 for invalid. GLuint programID; //!< The ID OpenGL gave our shader program. Bind this to use this shader. 0 for invalid. std::vector< std::string > transformFeedbackVaryings;//!< The varyings that will be captured using a transform feedback. Ordered in the order of capturing. + mutable std::unordered_map< std::string, GLint > uniformLocations;//!< Cache of uniform name -> location for the currently linked programID, populated lazily by FindUniform. }; }//End namespace ffglex \ No newline at end of file