Save on gl[Enable/Disable]VertexAttribArray calls.

This commit is contained in:
Henrik Rydgard 2013-01-06 23:50:05 +01:00
parent 9363a867e2
commit a1bf5a2a10
3 changed files with 24 additions and 25 deletions

View file

@ -124,6 +124,7 @@ LinkedShader::LinkedShader(Shader *vs, Shader *fs)
a_weight4567 = glGetAttribLocation(program, "a_weight4567");
glUseProgram(program);
// Default uniform values
glUniform1i(u_tex, 0);
// The rest, use the "dirty" mechanism.
@ -177,6 +178,23 @@ static void SetMatrix4x3(int uniform, const float *m4x3) {
void LinkedShader::use() {
glUseProgram(program);
updateUniforms();
glEnableVertexAttribArray(a_position);
if (a_texcoord != -1) glEnableVertexAttribArray(a_texcoord);
if (a_color0 != -1) glEnableVertexAttribArray(a_color0);
if (a_color1 != -1) glEnableVertexAttribArray(a_color1);
if (a_normal != -1) glEnableVertexAttribArray(a_normal);
if (a_weight0123 != -1) glEnableVertexAttribArray(a_weight0123);
if (a_weight4567 != -1) glEnableVertexAttribArray(a_weight4567);
}
void LinkedShader::stop() {
glDisableVertexAttribArray(a_position);
if (a_texcoord != -1) glDisableVertexAttribArray(a_texcoord);
if (a_color0 != -1) glDisableVertexAttribArray(a_color0);
if (a_color1 != -1) glDisableVertexAttribArray(a_color1);
if (a_normal != -1) glDisableVertexAttribArray(a_normal);
if (a_weight0123 != -1) glDisableVertexAttribArray(a_weight0123);
if (a_weight4567 != -1) glDisableVertexAttribArray(a_weight4567);
}
void LinkedShader::updateUniforms() {
@ -327,6 +345,11 @@ LinkedShader *ShaderManager::ApplyShader(int prim)
return lastShader; // Already all set.
}
if (lastShader != 0) {
// There was a previous shader and we're switching.
lastShader->stop();
}
lastVSID = VSID;
lastFSID = FSID;

View file

@ -32,6 +32,7 @@ public:
~LinkedShader();
void use();
void stop();
void updateUniforms();
uint32_t program;

View file

@ -271,15 +271,9 @@ const GlTypeInfo GLComp[8] = {
static inline void VertexAttribSetup(int attrib, int fmt, int stride, u8 *ptr) {
if (attrib != -1 && fmt) {
const GlTypeInfo &type = GLComp[fmt];
glEnableVertexAttribArray(attrib);
glVertexAttribPointer(attrib, type.count, type.type, type.normalized, stride, ptr);
}
}
static inline void VertexAttribDisable(int attrib, int fmt) {
if (attrib != -1 && fmt) {
glDisableVertexAttribArray(attrib);
}
}
// TODO: Use VBO and get rid of the vertexData pointers - with that, we will supply only offsets
static void SetupDecFmtForDraw(LinkedShader *program, const DecVtxFormat &decFmt, u8 *vertexData) {
@ -292,16 +286,6 @@ static void SetupDecFmtForDraw(LinkedShader *program, const DecVtxFormat &decFmt
VertexAttribSetup(program->a_position, decFmt.posfmt, decFmt.stride, vertexData + decFmt.posoff);
}
static void DesetupDecFmtForDraw(LinkedShader *program, const DecVtxFormat &decFmt) {
VertexAttribDisable(program->a_weight0123, decFmt.w0fmt);
VertexAttribDisable(program->a_weight4567, decFmt.w1fmt);
VertexAttribDisable(program->a_texcoord, decFmt.uvfmt);
VertexAttribDisable(program->a_color0, decFmt.c0fmt);
VertexAttribDisable(program->a_color1, decFmt.c1fmt);
VertexAttribDisable(program->a_normal, decFmt.nrmfmt);
VertexAttribDisable(program->a_position, decFmt.posfmt);
}
// The verts are in the order: BR BL TL TR
static void SwapUVs(TransformedVertex &a, TransformedVertex &b) {
float tempu = a.u;
@ -599,10 +583,6 @@ void TransformDrawEngine::SoftwareTransformAndDraw(
// TODO: Make a cache for glEnableVertexAttribArray and glVertexAttribPtr states,
// these spam the gDebugger log.
glEnableVertexAttribArray(program->a_position);
if (program->a_texcoord != -1) glEnableVertexAttribArray(program->a_texcoord);
if (program->a_color0 != -1) glEnableVertexAttribArray(program->a_color0);
if (program->a_color1 != -1) glEnableVertexAttribArray(program->a_color1);
const int vertexSize = sizeof(transformed[0]);
glVertexAttribPointer(program->a_position, 3, GL_FLOAT, GL_FALSE, vertexSize, drawBuffer);
if (program->a_texcoord != -1) glVertexAttribPointer(program->a_texcoord, 2, GL_FLOAT, GL_FALSE, vertexSize, ((uint8_t*)drawBuffer) + 3 * 4);
@ -613,10 +593,6 @@ void TransformDrawEngine::SoftwareTransformAndDraw(
} else {
glDrawArrays(glprim[prim], 0, numTrans);
}
glDisableVertexAttribArray(program->a_position);
if (program->a_texcoord != -1) glDisableVertexAttribArray(program->a_texcoord);
if (program->a_color0 != -1) glDisableVertexAttribArray(program->a_color0);
if (program->a_color1 != -1) glDisableVertexAttribArray(program->a_color1);
}
void TransformDrawEngine::SubmitPrim(void *verts, void *inds, int prim, int vertexCount, u32 vertType, int forceIndexType, int *bytesRead) {
@ -728,7 +704,6 @@ void TransformDrawEngine::Flush() {
} else {
glDrawElements(glprim[prim], indexGen.VertexCount(), GL_UNSIGNED_SHORT, (GLvoid *)decIndex);
}
DesetupDecFmtForDraw(program, dec.GetDecVtxFmt());
} else {
SoftwareTransformAndDraw(prim, decoded, program, indexGen.VertexCount(), dec.VertexType(), (void *)decIndex, GE_VTYPE_IDX_16BIT, dec.GetDecVtxFmt(),
indexGen.MaxIndex());