OpenGL2: Reduce redundant GL calls.

This commit is contained in:
SmileTheory 2014-10-14 06:15:06 -07:00
parent 84206c8598
commit 0578b70f38
10 changed files with 73 additions and 78 deletions

View file

@ -121,8 +121,6 @@ void GL_Cull( int cullType ) {
return; return;
} }
glState.faceCulling = cullType;
if ( cullType == CT_TWO_SIDED ) if ( cullType == CT_TWO_SIDED )
{ {
qglDisable( GL_CULL_FACE ); qglDisable( GL_CULL_FACE );
@ -130,7 +128,11 @@ void GL_Cull( int cullType ) {
else else
{ {
qboolean cullFront; qboolean cullFront;
qglEnable( GL_CULL_FACE );
if ( glState.faceCulling == CT_TWO_SIDED )
{
qglEnable( GL_CULL_FACE );
}
cullFront = (cullType == CT_FRONT_SIDED); cullFront = (cullType == CT_FRONT_SIDED);
if ( backEnd.viewParms.isMirror ) if ( backEnd.viewParms.isMirror )
@ -143,8 +145,13 @@ void GL_Cull( int cullType ) {
cullFront = !cullFront; cullFront = !cullFront;
} }
qglCullFace( cullFront ? GL_FRONT : GL_BACK ); if (glState.faceCullFront != cullFront)
qglCullFace( cullFront ? GL_FRONT : GL_BACK );
glState.faceCullFront = cullFront;
} }
glState.faceCulling = cullType;
} }
/* /*
@ -219,10 +226,26 @@ void GL_State( unsigned long stateBits )
// //
if ( diff & ( GLS_SRCBLEND_BITS | GLS_DSTBLEND_BITS ) ) if ( diff & ( GLS_SRCBLEND_BITS | GLS_DSTBLEND_BITS ) )
{ {
GLenum srcFactor = GL_ONE, dstFactor = GL_ONE; uint32_t oldState = glState.glStateBits & ( GLS_SRCBLEND_BITS | GLS_DSTBLEND_BITS );
uint32_t newState = stateBits & ( GLS_SRCBLEND_BITS | GLS_DSTBLEND_BITS );
uint32_t storedState = glState.storedGlState & ( GLS_SRCBLEND_BITS | GLS_DSTBLEND_BITS );
if ( stateBits & ( GLS_SRCBLEND_BITS | GLS_DSTBLEND_BITS ) ) if (oldState == 0)
{ {
qglEnable( GL_BLEND );
}
else if (newState == 0)
{
qglDisable( GL_BLEND );
}
if (newState != 0 && storedState != newState)
{
GLenum srcFactor = GL_ONE, dstFactor = GL_ONE;
glState.storedGlState &= ~( GLS_SRCBLEND_BITS | GLS_DSTBLEND_BITS );
glState.storedGlState |= newState;
switch ( stateBits & GLS_SRCBLEND_BITS ) switch ( stateBits & GLS_SRCBLEND_BITS )
{ {
case GLS_SRCBLEND_ZERO: case GLS_SRCBLEND_ZERO:
@ -288,13 +311,8 @@ void GL_State( unsigned long stateBits )
break; break;
} }
qglEnable( GL_BLEND );
qglBlendFunc( srcFactor, dstFactor ); qglBlendFunc( srcFactor, dstFactor );
} }
else
{
qglDisable( GL_BLEND );
}
} }
// //
@ -347,26 +365,36 @@ void GL_State( unsigned long stateBits )
// //
if ( diff & GLS_ATEST_BITS ) if ( diff & GLS_ATEST_BITS )
{ {
switch ( stateBits & GLS_ATEST_BITS ) uint32_t oldState = glState.glStateBits & GLS_ATEST_BITS;
uint32_t newState = stateBits & GLS_ATEST_BITS;
uint32_t storedState = glState.storedGlState & GLS_ATEST_BITS;
if (oldState == 0)
{ {
case 0: qglEnable(GL_ALPHA_TEST);
qglDisable( GL_ALPHA_TEST ); }
break; else if (newState == 0)
case GLS_ATEST_GT_0: {
qglEnable( GL_ALPHA_TEST ); qglDisable(GL_ALPHA_TEST);
qglAlphaFunc( GL_GREATER, 0.0f ); }
break;
case GLS_ATEST_LT_80: if (newState != 0 && storedState != newState)
qglEnable( GL_ALPHA_TEST ); {
qglAlphaFunc( GL_LESS, 0.5f ); glState.storedGlState &= ~GLS_ATEST_BITS;
break; glState.storedGlState |= newState;
case GLS_ATEST_GE_80:
qglEnable( GL_ALPHA_TEST ); switch ( newState )
qglAlphaFunc( GL_GEQUAL, 0.5f ); {
break; case GLS_ATEST_GT_0:
default: qglAlphaFunc( GL_GREATER, 0.0f );
assert( 0 ); break;
break; case GLS_ATEST_LT_80:
qglAlphaFunc( GL_LESS, 0.5f );
break;
case GLS_ATEST_GE_80:
qglAlphaFunc( GL_GEQUAL, 0.5f );
break;
}
} }
} }
@ -405,6 +433,7 @@ static void RB_Hyperspace( void ) {
c = ( backEnd.refdef.time & 255 ) / 255.0f; c = ( backEnd.refdef.time & 255 ) / 255.0f;
qglClearColor( c, c, c, 1 ); qglClearColor( c, c, c, 1 );
qglClear( GL_COLOR_BUFFER_BIT ); qglClear( GL_COLOR_BUFFER_BIT );
qglClearColor(0.0f, 0.0f, 0.0f, 1.0f);
backEnd.isHyperspace = qtrue; backEnd.isHyperspace = qtrue;
} }
@ -489,25 +518,12 @@ void RB_BeginDrawingView (void) {
if ( r_fastsky->integer && !( backEnd.refdef.rdflags & RDF_NOWORLDMODEL ) ) if ( r_fastsky->integer && !( backEnd.refdef.rdflags & RDF_NOWORLDMODEL ) )
{ {
clearBits |= GL_COLOR_BUFFER_BIT; // FIXME: only if sky shaders have been used clearBits |= GL_COLOR_BUFFER_BIT; // FIXME: only if sky shaders have been used
#ifdef _DEBUG
qglClearColor( 0.8f, 0.7f, 0.4f, 1.0f ); // FIXME: get color of sky
#else
qglClearColor( 0.0f, 0.0f, 0.0f, 1.0f ); // FIXME: get color of sky
#endif
}
// clear to white for shadow maps
if (backEnd.viewParms.flags & VPF_SHADOWMAP)
{
clearBits |= GL_COLOR_BUFFER_BIT;
qglClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
} }
// clear to black for cube maps // clear to black for cube maps
if (tr.renderCubeFbo && backEnd.viewParms.targetFbo == tr.renderCubeFbo) if (tr.renderCubeFbo && backEnd.viewParms.targetFbo == tr.renderCubeFbo)
{ {
clearBits |= GL_COLOR_BUFFER_BIT; clearBits |= GL_COLOR_BUFFER_BIT;
qglClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
} }
qglClear( clearBits ); qglClear( clearBits );
@ -523,6 +539,7 @@ void RB_BeginDrawingView (void) {
} }
glState.faceCulling = -1; // force face culling to set next time glState.faceCulling = -1; // force face culling to set next time
glState.faceCullFront = -1; // same as above
// we will only draw a sun if there was sky rendered in this view // we will only draw a sun if there was sky rendered in this view
backEnd.skyRenderedThisView = qfalse; backEnd.skyRenderedThisView = qfalse;

View file

@ -425,7 +425,6 @@ void RE_BeginFrame( stereoFrame_t stereoFrame ) {
backEnd.colorMask[1] = GL_FALSE; backEnd.colorMask[1] = GL_FALSE;
backEnd.colorMask[2] = GL_FALSE; backEnd.colorMask[2] = GL_FALSE;
backEnd.colorMask[3] = GL_FALSE; backEnd.colorMask[3] = GL_FALSE;
qglClearColor(0.0f, 0.0f, 0.0f, 1.0f);
if (glRefConfig.framebufferObject) if (glRefConfig.framebufferObject)
{ {

View file

@ -447,7 +447,6 @@ void FBO_Init(void)
if (tr.renderFbo) if (tr.renderFbo)
{ {
FBO_Bind(tr.renderFbo); FBO_Bind(tr.renderFbo);
qglClearColor( 1, 0, 0.5, 1 );
qglClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); qglClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
FBO_Bind(NULL); FBO_Bind(NULL);
} }

View file

@ -1485,25 +1485,6 @@ void GLSL_BindNullProgram(void)
} }
void GLSL_VertexAttribsState(uint32_t stateBits)
{
int attribIndex;
for (attribIndex = 0; attribIndex < ATTR_INDEX_COUNT; attribIndex++)
{
uint32_t attribBit = 1 << attribIndex;
if (stateBits & attribBit)
{
qglEnableVertexAttribArrayARB(attribIndex);
}
else
{
qglDisableVertexAttribArrayARB(attribIndex);
}
}
GLSL_VertexAttribPointers(stateBits);
}
void GLSL_VertexAttribPointers(uint32_t attribBits) void GLSL_VertexAttribPointers(uint32_t attribBits)
{ {
int newFrame, oldFrame; int newFrame, oldFrame;

View file

@ -944,6 +944,7 @@ void GL_SetDefaultState( void )
// make sure our GL state vector is set correctly // make sure our GL state vector is set correctly
// //
glState.glStateBits = GLS_DEPTHTEST_DISABLE | GLS_DEPTHMASK_TRUE; glState.glStateBits = GLS_DEPTHTEST_DISABLE | GLS_DEPTHMASK_TRUE;
glState.storedGlState = 0;
glState.currentProgram = 0; glState.currentProgram = 0;
qglUseProgramObjectARB(0); qglUseProgramObjectARB(0);
@ -961,6 +962,11 @@ void GL_SetDefaultState( void )
if (glRefConfig.seamlessCubeMap) if (glRefConfig.seamlessCubeMap)
qglEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS); qglEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
// GL_POLYGON_OFFSET_FILL will be glEnable()d when this is used
qglPolygonOffset( r_offsetFactor->value, r_offsetUnits->value );
qglClearColor( 0.0f, 0.0f, 0.0f, 1.0f ); // FIXME: get color of sky
} }
/* /*

View file

@ -1362,7 +1362,9 @@ typedef struct {
qboolean finishCalled; qboolean finishCalled;
int texEnv[2]; int texEnv[2];
int faceCulling; int faceCulling;
unsigned long glStateBits; int faceCullFront;
uint32_t glStateBits;
uint32_t storedGlState;
uint32_t vertexAttribsNewFrame; uint32_t vertexAttribsNewFrame;
uint32_t vertexAttribsOldFrame; uint32_t vertexAttribsOldFrame;
float vertexAttribsInterpolation; float vertexAttribsInterpolation;
@ -2211,7 +2213,6 @@ GLSL
void GLSL_InitGPUShaders(void); void GLSL_InitGPUShaders(void);
void GLSL_ShutdownGPUShaders(void); void GLSL_ShutdownGPUShaders(void);
void GLSL_VertexAttribsState(uint32_t stateBits);
void GLSL_VertexAttribPointers(uint32_t attribBits); void GLSL_VertexAttribPointers(uint32_t attribBits);
void GLSL_BindProgram(shaderProgram_t * program); void GLSL_BindProgram(shaderProgram_t * program);
void GLSL_BindNullProgram(void); void GLSL_BindNullProgram(void);

View file

@ -145,7 +145,6 @@ static void DrawTris (shaderCommands_t *input) {
shaderProgram_t *sp = &tr.textureColorShader; shaderProgram_t *sp = &tr.textureColorShader;
vec4_t color; vec4_t color;
GLSL_VertexAttribsState(ATTR_POSITION);
GLSL_BindProgram(sp); GLSL_BindProgram(sp);
GLSL_SetUniformMat4(sp, UNIFORM_MODELVIEWPROJECTIONMATRIX, glState.modelviewProjection); GLSL_SetUniformMat4(sp, UNIFORM_MODELVIEWPROJECTIONMATRIX, glState.modelviewProjection);
@ -1537,15 +1536,12 @@ void RB_StageIteratorGeneric( void )
if ( input->shader->polygonOffset ) if ( input->shader->polygonOffset )
{ {
qglEnable( GL_POLYGON_OFFSET_FILL ); qglEnable( GL_POLYGON_OFFSET_FILL );
qglPolygonOffset( r_offsetFactor->value, r_offsetUnits->value );
} }
// //
// Set vertex attribs and pointers // Set vertex attribs and pointers
// //
if (tess.useInternalVao) if (glState.vertexAnimation)
GLSL_VertexAttribsState(vertexAttribs);
else if (glState.vertexAnimation)
GLSL_VertexAttribPointers(vertexAttribs & (ATTR_POSITION | ATTR_POSITION2 | ATTR_NORMAL | ATTR_NORMAL2 | ATTR_TANGENT | ATTR_TANGENT2)); GLSL_VertexAttribPointers(vertexAttribs & (ATTR_POSITION | ATTR_POSITION2 | ATTR_NORMAL | ATTR_NORMAL2 | ATTR_TANGENT | ATTR_TANGENT2));
// //

View file

@ -442,7 +442,6 @@ static void DrawSkySide( struct image_s *image, const int mins[2], const int max
shaderProgram_t *sp = &tr.lightallShader[0]; shaderProgram_t *sp = &tr.lightallShader[0];
vec4_t vector; vec4_t vector;
GLSL_VertexAttribsState(ATTR_POSITION | ATTR_TEXCOORD);
GLSL_BindProgram(sp); GLSL_BindProgram(sp);
GLSL_SetUniformMat4(sp, UNIFORM_MODELVIEWPROJECTIONMATRIX, glState.modelviewProjection); GLSL_SetUniformMat4(sp, UNIFORM_MODELVIEWPROJECTIONMATRIX, glState.modelviewProjection);

View file

@ -204,8 +204,6 @@ void RB_InstantQuad2(vec4_t quadVerts[4], vec2_t texCoords[4])
RB_UpdateTessVao(ATTR_POSITION | ATTR_TEXCOORD); RB_UpdateTessVao(ATTR_POSITION | ATTR_TEXCOORD);
GLSL_VertexAttribsState(ATTR_POSITION | ATTR_TEXCOORD);
R_DrawElementsVao(tess.numIndexes, tess.firstIndex, tess.minIndex, tess.maxIndex); R_DrawElementsVao(tess.numIndexes, tess.firstIndex, tess.minIndex, tess.maxIndex);
tess.numIndexes = 0; tess.numIndexes = 0;
@ -602,7 +600,6 @@ static void RB_SurfaceBeam( void )
// FIXME: A lot of this can probably be removed for speed, and refactored into a more convenient function // FIXME: A lot of this can probably be removed for speed, and refactored into a more convenient function
RB_UpdateTessVao(ATTR_POSITION); RB_UpdateTessVao(ATTR_POSITION);
GLSL_VertexAttribsState(ATTR_POSITION);
GLSL_BindProgram(sp); GLSL_BindProgram(sp);
GLSL_SetUniformMat4(sp, UNIFORM_MODELVIEWPROJECTIONMATRIX, glState.modelviewProjection); GLSL_SetUniformMat4(sp, UNIFORM_MODELVIEWPROJECTIONMATRIX, glState.modelviewProjection);
@ -1612,7 +1609,8 @@ void RB_SurfaceVaoMdvMesh(srfVaoMdvMesh_t * surface)
glState.vertexAttribsOldFrame = refEnt->oldframe; glState.vertexAttribsOldFrame = refEnt->oldframe;
glState.vertexAttribsNewFrame = refEnt->frame; glState.vertexAttribsNewFrame = refEnt->frame;
glState.vertexAnimation = qtrue; if (surface->mdvModel->numFrames > 1)
glState.vertexAnimation = qtrue;
RB_EndSurface(); RB_EndSurface();

View file

@ -404,8 +404,7 @@ void R_BindVao(vao_t * vao)
qglBindBufferARB(GL_ARRAY_BUFFER_ARB, vao->vertexesVBO); qglBindBufferARB(GL_ARRAY_BUFFER_ARB, vao->vertexesVBO);
qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vao->indexesIBO); qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vao->indexesIBO);
if (vao != tess.vao) Vao_SetVertexPointers(vao);
Vao_SetVertexPointers(vao);
} }
} }
} }