rice: avoid redundant gl calls 1/head
authornotaz <notasas@gmail.com>
Tue, 24 Jun 2014 21:14:07 +0000 (00:14 +0300)
committernotaz <notasas@gmail.com>
Tue, 24 Jun 2014 21:14:07 +0000 (00:14 +0300)
source/gles2rice/src/OGLES2FragmentShaders.cpp
source/gles2rice/src/OGLES2FragmentShaders.h
source/gles2rice/src/OGLExtRender.cpp
source/gles2rice/src/OGLRender.cpp

index 1378720..0fa4150 100755 (executable)
@@ -147,7 +147,7 @@ bool COGLFragmentShaderCombiner::Initialize(void)
     if( !COGLColorCombiner::Initialize() )
         return false;
 
-    COGLGraphicsContext *pcontext = (COGLGraphicsContext *)(CGraphicsContext::g_pGraphicsContext);
+//    COGLGraphicsContext *pcontext = (COGLGraphicsContext *)(CGraphicsContext::g_pGraphicsContext);
 //    if( pcontext->IsExtensionSupported("GL_fragment_shader") )
 //    {
         m_bShaderIsSupported = true;
@@ -295,7 +295,7 @@ bool COGL_FragmentProgramCombiner::Initialize(void)
     if( !COGLColorCombiner4::Initialize() )
         return false;
 
-    COGLGraphicsContext *pcontext = (COGLGraphicsContext *)(CGraphicsContext::g_pGraphicsContext);
+//    COGLGraphicsContext *pcontext = (COGLGraphicsContext *)(CGraphicsContext::g_pGraphicsContext);
 //    if( pcontext->IsExtensionSupported("GL_fragment_program") )
 //    {
         m_bFragmentProgramIsSupported = true;
@@ -306,6 +306,14 @@ bool COGL_FragmentProgramCombiner::Initialize(void)
 
 
 
+void COGL_FragmentProgramCombiner::UseProgram(GLuint program)
+{
+    if (program != currentProgram) {
+        glUseProgram(program);
+        currentProgram = program;
+    }
+}
+
 void COGL_FragmentProgramCombiner::DisableCombiner(void)
 {
     //glDisable(GL_FRAGMENT_PROGRAM);
@@ -317,7 +325,7 @@ void COGL_FragmentProgramCombiner::InitCombinerCycleCopy(void)
 {
     m_pOGLRender->DisableMultiTexture();
     m_pOGLRender->EnableTexUnit(0,TRUE);
-    glUseProgram(copyProgram);
+    UseProgram(copyProgram);
     glUniform1f(copyAlphaLocation,m_AlphaRef);
     OPENGL_CHECK_ERRORS;
     glEnableVertexAttribArray(VS_POSITION);
@@ -340,7 +348,7 @@ void COGL_FragmentProgramCombiner::InitCombinerCycleCopy(void)
 
 void COGL_FragmentProgramCombiner::InitCombinerCycleFill(void)
 {
-    glUseProgram(fillProgram);
+    UseProgram(fillProgram);
     glUniform4f(fillColorLocation,((gRDP.fillColor>>16)&0xFF)/255.0f,((gRDP.fillColor>>8)&0xFF)/255.0f,((gRDP.fillColor)&0xFF)/255.0f,((gRDP.fillColor>>24)&0xFF)/255.0f);
     OPENGL_CHECK_ERRORS;
 }
@@ -591,7 +599,7 @@ int COGL_FragmentProgramCombiner::ParseDecodedMux()
                                printf("%s\n",Log);
                        }
 
-                       glUseProgram(res.programID);
+                       UseProgram(res.programID);
                        OPENGL_CHECK_ERRORS;
 
                        //Bind texture samplers
@@ -634,7 +642,7 @@ void COGL_FragmentProgramCombiner::GenerateCombinerSetting(int index)
 {
     GLuint ID = m_vCompiledShaders[index].programID;
 
-    glUseProgram(ID);
+    UseProgram(ID);
     glEnableVertexAttribArray(VS_POSITION);
     OPENGL_CHECK_ERRORS;
     glVertexAttribPointer(VS_POSITION,4,GL_FLOAT,GL_FALSE,sizeof(float)*5,&(g_vtxProjected5[0][0]));
@@ -663,57 +671,81 @@ void COGL_FragmentProgramCombiner::GenerateCombinerSetting(int index)
 
 void COGL_FragmentProgramCombiner::GenerateCombinerSettingConstants(int index)
 {
-    OGLShaderCombinerSaveType prog = m_vCompiledShaders[index];
+    OGLShaderCombinerSaveType &prog = m_vCompiledShaders[index];
 
-    glUseProgram(prog.programID);
+    UseProgram(prog.programID);
     float *pf;
     if(prog.EnvColorLocation != -1)
     {
         pf = GetEnvColorfv();
-        glUniform4fv(prog.EnvColorLocation,1, pf);
-        OPENGL_CHECK_ERRORS;
+        if (memcmp(pf, prog.EnvColors, sizeof(prog.EnvColors))) {
+            memcpy(prog.EnvColors, pf, sizeof(prog.EnvColors));
+            glUniform4fv(prog.EnvColorLocation, 1, pf);
+            OPENGL_CHECK_ERRORS;
+        }
     }
 
     if(prog.PrimColorLocation != -1)
     {
         pf = GetPrimitiveColorfv();
-        glUniform4fv(prog.PrimColorLocation,1, pf);
-        OPENGL_CHECK_ERRORS;
+        if (memcmp(pf, prog.PrimColors, sizeof(prog.PrimColors))) {
+            memcpy(prog.PrimColors, pf, sizeof(prog.PrimColors));
+            glUniform4fv(prog.PrimColorLocation, 1, pf);
+            OPENGL_CHECK_ERRORS;
+        }
     }
 
     if(prog.EnvFracLocation != -1)
     {
-        float frac = gRDP.LODFrac / 255.0f;
-        float tempf[4] = {frac,frac,frac,frac};
-        glUniform4fv(prog.EnvFracLocation,1, tempf);
-        OPENGL_CHECK_ERRORS;
+        // avoid slow float compare..
+        if( *(int *)&gRDP.LODFrac != *(int *)&prog.EnvLODFrac ) {
+            prog.EnvLODFrac = gRDP.LODFrac;
+            float frac = gRDP.LODFrac / 255.0f;
+            float tempf[4] = {frac,frac,frac,frac};
+            glUniform4fv(prog.EnvFracLocation, 1, tempf);
+            OPENGL_CHECK_ERRORS;
+        }
     }
 
     if(prog.PrimFracLocation != -1)
     {
-        float frac2 = gRDP.primLODFrac / 255.0f;
-        float tempf2[4] = {frac2,frac2,frac2,frac2};
-        glUniform4fv(prog.PrimFracLocation,1, tempf2);
-        OPENGL_CHECK_ERRORS;
+        if( *(int *)&gRDP.primLODFrac != *(int *)&prog.PrimLODFrac ) {
+            prog.PrimLODFrac = gRDP.primLODFrac;
+            float frac2 = gRDP.primLODFrac / 255.0f;
+            float tempf2[4] = {frac2,frac2,frac2,frac2};
+            glUniform4fv(prog.PrimFracLocation, 1, tempf2);
+            OPENGL_CHECK_ERRORS;
+        }
     }
 
-       if(prog.FogColorLocation != -1)
+    if(prog.FogColorLocation != -1)
     {
-        glUniform4f(prog.FogColorLocation, gRDP.fvFogColor[0],gRDP.fvFogColor[1],gRDP.fvFogColor[2], gRDP.fvFogColor[3]);
-        OPENGL_CHECK_ERRORS;
+        pf = &gRDP.fvFogColor[0];
+        if (memcmp(pf, prog.FogColors, sizeof(prog.FogColors))) {
+            memcpy(prog.FogColors, pf, sizeof(prog.FogColors));
+            glUniform4fv(prog.FogColorLocation, 1, pf);
+            OPENGL_CHECK_ERRORS;
+        }
     }
        
-       if(prog.FogMinMaxLocation != -1)        
-       {
-        glUniform2f(prog.FogMinMaxLocation,gRSPfFogMin,gRSPfFogMax);
-        OPENGL_CHECK_ERRORS;
+    if(prog.FogMinMaxLocation != -1)   
+    {
+        if( gRSPfFogMin != prog.FogMin || gRSPfFogMax != prog.FogMax ) {
+            prog.FogMin = gRSPfFogMin;
+            prog.FogMax = gRSPfFogMax;
+            glUniform2f(prog.FogMinMaxLocation,gRSPfFogMin,gRSPfFogMax);
+            OPENGL_CHECK_ERRORS;
+        }
     }
        
     if(prog.AlphaRefLocation != -1)
-       {
-        glUniform1f(prog.AlphaRefLocation,m_AlphaRef);
-               OPENGL_CHECK_ERRORS;
-       }
+    {
+        if( m_AlphaRef != prog.AlphaRef ) {
+            prog.AlphaRef = m_AlphaRef;
+            glUniform1f(prog.AlphaRefLocation, m_AlphaRef);
+            OPENGL_CHECK_ERRORS;
+        }
+    }
 }
 
 int COGL_FragmentProgramCombiner::FindCompiledMux()
index 70b97cf..2245cb0 100755 (executable)
@@ -45,6 +45,14 @@ typedef struct {
     GLint FogColorLocation;
     GLint FogMinMaxLocation;
 
+    float PrimColors[4];
+    float EnvColors[4];
+    float PrimLODFrac;
+    float EnvLODFrac;
+    float AlphaRef;
+    float FogColors[4];
+    float FogMin;
+    float FogMax;
 } OGLShaderCombinerSaveType;
 
 
@@ -87,6 +95,9 @@ private:
     bool bFogState;
     bool bFogPreviousState;
 
+    void UseProgram(GLuint program);
+    GLuint currentProgram;
+
 #ifdef DEBUGGER
     void DisplaySimpleMuxString(void);
 #endif
index b87c80c..6a4f338 100644 (file)
@@ -122,6 +122,8 @@ void COGLExtRender::SetTexWrapS(int unitno,GLuint flag)
     static GLuint mtex[8];
     if( m_curBoundTex[unitno] != mtex[unitno] || mflag[unitno] != flag )
     {
+        pglActiveTexture(GL_TEXTURE0_ARB+unitno);
+        OPENGL_CHECK_ERRORS;
         mtex[unitno] = m_curBoundTex[0];
         mflag[unitno] = flag;
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, flag);
@@ -173,8 +175,6 @@ void COGLExtRender::SetTextureUFlag(TextureUVFlag dwFlag, uint32 dwTile)
     {
         if( m_textureUnitMap[textureNo] == tex )
         {
-            pglActiveTexture(GL_TEXTURE0_ARB+textureNo);
-            OPENGL_CHECK_ERRORS;
             COGLTexture* pTexture = g_textures[(gRSP.curTile+tex)&7].m_pCOGLTexture;
             if( pTexture ) 
             {
index f11798e..c63a86e 100755 (executable)
@@ -350,9 +350,14 @@ void OGLRender::SetZUpdate(BOOL bZUpdate)
 
 void OGLRender::ApplyZBias(int bias)
 {
+    static int old_bias;
     float f1 = bias > 0 ? -3.0f : 0.0f;  // z offset = -3.0 * max(abs(dz/dx),abs(dz/dy)) per pixel delta z slope
     float f2 = bias > 0 ? -3.0f : 0.0f;  // z offset += -3.0 * 1 bit
 
+    if (bias == old_bias)
+        return;
+    old_bias = bias;
+
 #ifdef PAULSCODE
 //    Android_JNI_GetPolygonOffset(hardwareType, bias, &f1, &f2);
 //     glPolygonOffset(0.2f, 0.2f);
@@ -986,6 +991,7 @@ void OGLRender::DrawSimpleRect(int nX0, int nY0, int nX1, int nY1, uint32 dwColo
     OPENGL_CHECK_ERRORS;
 }
 
+#if 0
 void OGLRender::InitCombinerBlenderForSimpleRectDraw(uint32 tile)
 {
     //glEnable(GL_CULL_FACE);
@@ -997,6 +1003,7 @@ void OGLRender::InitCombinerBlenderForSimpleRectDraw(uint32 tile)
     OPENGL_CHECK_ERRORS;
     //glEnable(GL_ALPHA_TEST);
 }
+#endif
 
 COLOR OGLRender::PostProcessDiffuseColor(COLOR curDiffuseColor)
 {