mirror of
https://github.com/AxioDL/PrimeWorldEditor.git
synced 2025-12-21 10:49:23 +00:00
Cache shader uniform locations instead of looking them up every frame (1-2 fps boost yay?)
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user