Cache shader uniform locations instead of looking them up every frame (1-2 fps boost yay?)

This commit is contained in:
parax0
2016-04-29 15:59:26 -06:00
parent e781908205
commit 420f483b56
4 changed files with 46 additions and 28 deletions

View File

@@ -159,6 +159,7 @@ bool CShader::LinkShaders()
mLightBlockIndex = GetUniformBlockIndex("LightBlock");
mBoneTransformBlockIndex = GetUniformBlockIndex("BoneTransformBlock");
CacheCommonUniforms();
mProgramExists = true;
return true;
}
@@ -183,6 +184,17 @@ GLuint CShader::GetUniformBlockIndex(const char* pkUniformBlock)
return glGetUniformBlockIndex(mProgram, pkUniformBlock);
}
void CShader::SetTextureUniforms(u32 NumTextures)
{
for (u32 iTex = 0; iTex < NumTextures; iTex++)
glUniform1i(mTextureUniforms[iTex], iTex);
}
void CShader::SetNumLights(u32 NumLights)
{
glUniform1i(mNumLightsUniform, NumLights);
}
void CShader::SetCurrent()
{
if (spCurrentShader != this)
@@ -238,6 +250,17 @@ void CShader::KillCachedShader()
}
// ************ PRIVATE ************
void CShader::CacheCommonUniforms()
{
for (u32 iTex = 0; iTex < 8; iTex++)
{
TString TexUniform = "Texture" + TString::FromInt32(iTex);
mTextureUniforms[iTex] = glGetUniformLocation(mProgram, *TexUniform);
}
mNumLightsUniform = glGetUniformLocation(mProgram, "NumLights");
}
void CShader::DumpShaderSource(GLuint Shader, const TString& rkOut)
{
GLint SourceLen;

View File

@@ -19,6 +19,10 @@ class CShader
GLuint mLightBlockIndex;
GLuint mBoneTransformBlockIndex;
// Cached uniform locations
GLint mTextureUniforms[8];
GLint mNumLightsUniform;
static CShader* spCurrentShader;
public:
@@ -32,6 +36,8 @@ public:
GLuint GetProgramID();
GLuint GetUniformLocation(const char* pkUniform);
GLuint GetUniformBlockIndex(const char* pkUniformBlock);
void SetTextureUniforms(u32 NumTextures);
void SetNumLights(u32 NumLights);
void SetCurrent();
// Static
@@ -40,6 +46,7 @@ public:
static void KillCachedShader();
private:
void CacheCommonUniforms();
void DumpShaderSource(GLuint Shader, const TString& rkOut);
};