From aa5f80b975b4bdef2a056c376d8be3c88d16f5a6 Mon Sep 17 00:00:00 2001 From: SmileTheory Date: Tue, 14 Oct 2014 06:15:06 -0700 Subject: OpenGL2: Reduce redundant GL calls. --- src/renderergl2/tr_backend.c | 103 +++++++++++++++++++++++++------------------ 1 file changed, 60 insertions(+), 43 deletions(-) (limited to 'src/renderergl2/tr_backend.c') diff --git a/src/renderergl2/tr_backend.c b/src/renderergl2/tr_backend.c index 2ba52f69..0e7bbd77 100644 --- a/src/renderergl2/tr_backend.c +++ b/src/renderergl2/tr_backend.c @@ -122,8 +122,6 @@ void GL_Cull( int cullType ) { return; } - glState.faceCulling = cullType; - if ( cullType == CT_TWO_SIDED ) { qglDisable( GL_CULL_FACE ); @@ -131,7 +129,11 @@ void GL_Cull( int cullType ) { else { qboolean cullFront; - qglEnable( GL_CULL_FACE ); + + if ( glState.faceCulling == CT_TWO_SIDED ) + { + qglEnable( GL_CULL_FACE ); + } cullFront = (cullType == CT_FRONT_SIDED); if ( backEnd.viewParms.isMirror ) @@ -144,8 +146,13 @@ void GL_Cull( int cullType ) { cullFront = !cullFront; } - qglCullFace( cullFront ? GL_FRONT : GL_BACK ); + if (glState.faceCullFront != cullFront) + qglCullFace( cullFront ? GL_FRONT : GL_BACK ); + + glState.faceCullFront = cullFront; } + + glState.faceCulling = cullType; } /* @@ -220,10 +227,26 @@ void GL_State( unsigned long stateBits ) // 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 ) { case GLS_SRCBLEND_ZERO: @@ -289,13 +312,8 @@ void GL_State( unsigned long stateBits ) break; } - qglEnable( GL_BLEND ); qglBlendFunc( srcFactor, dstFactor ); } - else - { - qglDisable( GL_BLEND ); - } } // @@ -348,26 +366,36 @@ void GL_State( unsigned long stateBits ) // 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: - qglDisable( GL_ALPHA_TEST ); - break; - case GLS_ATEST_GT_0: - qglEnable( GL_ALPHA_TEST ); - qglAlphaFunc( GL_GREATER, 0.0f ); - break; - case GLS_ATEST_LT_80: - qglEnable( GL_ALPHA_TEST ); - qglAlphaFunc( GL_LESS, 0.5f ); - break; - case GLS_ATEST_GE_80: - qglEnable( GL_ALPHA_TEST ); - qglAlphaFunc( GL_GEQUAL, 0.5f ); - break; - default: - assert( 0 ); - break; + qglEnable(GL_ALPHA_TEST); + } + else if (newState == 0) + { + qglDisable(GL_ALPHA_TEST); + } + + if (newState != 0 && storedState != newState) + { + glState.storedGlState &= ~GLS_ATEST_BITS; + glState.storedGlState |= newState; + + switch ( newState ) + { + case GLS_ATEST_GT_0: + qglAlphaFunc( GL_GREATER, 0.0f ); + break; + case GLS_ATEST_LT_80: + qglAlphaFunc( GL_LESS, 0.5f ); + break; + case GLS_ATEST_GE_80: + qglAlphaFunc( GL_GEQUAL, 0.5f ); + break; + } } } @@ -406,6 +434,7 @@ static void RB_Hyperspace( void ) { c = ( backEnd.refdef.time & 255 ) / 255.0f; qglClearColor( c, c, c, 1 ); qglClear( GL_COLOR_BUFFER_BIT ); + qglClearColor(0.0f, 0.0f, 0.0f, 1.0f); backEnd.isHyperspace = qtrue; } @@ -490,25 +519,12 @@ void RB_BeginDrawingView (void) { if ( r_fastsky->integer && !( backEnd.refdef.rdflags & RDF_NOWORLDMODEL ) ) { 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 if (tr.renderCubeFbo && backEnd.viewParms.targetFbo == tr.renderCubeFbo) { clearBits |= GL_COLOR_BUFFER_BIT; - qglClearColor( 0.0f, 0.0f, 0.0f, 1.0f ); } qglClear( clearBits ); @@ -524,6 +540,7 @@ void RB_BeginDrawingView (void) { } 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 backEnd.skyRenderedThisView = qfalse; -- cgit