Minimizes render target changes
TRAC #12154
This patch reduces calls to SetRenderTarget.
Signed-off-by: Nicolas Capens
Signed-off-by: Daniel Koch

Author:    Shannon Woods

git-svn-id: https://angleproject.googlecode.com/svn/trunk@259 736b8ea6-26fd-11df-bfd4-992fa37f6226
diff --git a/src/libGLESv2/Context.cpp b/src/libGLESv2/Context.cpp
index bece470..98abe79 100644
--- a/src/libGLESv2/Context.cpp
+++ b/src/libGLESv2/Context.cpp
@@ -36,7 +36,10 @@
 Context::Context(const egl::Config *config)
     : mConfig(config)
 {
+    mAppliedRenderTargetSerial = 0;
+
     setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+
     mState.depthClearValue = 1.0f;
     mState.stencilClearValue = 0;
 
@@ -276,10 +279,14 @@
         mPsProfile = "ps_2_0";
         mVsProfile = "vs_2_0";
     }
+
+    markAllStateDirty();
 }
 
+// This function will set all of the state-related dirty flags, so that all state is set during next pre-draw.
 void Context::markAllStateDirty()
 {
+    mAppliedRenderTargetSerial = 0;
     mAppliedProgram = 0;
 }
 
@@ -1482,6 +1489,7 @@
 bool Context::applyRenderTarget(bool ignoreViewport)
 {
     IDirect3DDevice9 *device = getDevice();
+
     Framebuffer *framebufferObject = getFramebuffer();
 
     if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
@@ -1494,7 +1502,13 @@
     IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
     IDirect3DSurface9 *depthStencil = framebufferObject->getDepthStencil();
 
-    device->SetRenderTarget(0, renderTarget);
+    unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
+    if (renderTargetSerial != mAppliedRenderTargetSerial)
+    {
+        device->SetRenderTarget(0, renderTarget);
+        mAppliedRenderTargetSerial = renderTargetSerial;
+    }
+
     device->SetDepthStencilSurface(depthStencil);
 
     D3DVIEWPORT9 viewport;
diff --git a/src/libGLESv2/Context.h b/src/libGLESv2/Context.h
index 5e87422..5f58d44 100644
--- a/src/libGLESv2/Context.h
+++ b/src/libGLESv2/Context.h
@@ -445,6 +445,7 @@
     bool mHasBeenCurrent;
 
     unsigned int mAppliedProgram;
+    unsigned int mAppliedRenderTargetSerial;
 
     const char *mPsProfile;
     const char *mVsProfile;
diff --git a/src/libGLESv2/Framebuffer.cpp b/src/libGLESv2/Framebuffer.cpp
index 937de42..9d44c6f 100644
--- a/src/libGLESv2/Framebuffer.cpp
+++ b/src/libGLESv2/Framebuffer.cpp
@@ -92,6 +92,18 @@
     }
 }
 
+unsigned int Framebuffer::getRenderTargetSerial()
+{
+    Renderbuffer *colorbuffer = getColorbuffer();
+
+    if (colorbuffer)
+    {
+        return colorbuffer->getSerial();
+    }
+
+    return 0;
+}
+
 IDirect3DSurface9 *Framebuffer::getRenderTarget()
 {
     Renderbuffer *colorbuffer = getColorbuffer();
diff --git a/src/libGLESv2/Framebuffer.h b/src/libGLESv2/Framebuffer.h
index 80062b4..72054e0 100644
--- a/src/libGLESv2/Framebuffer.h
+++ b/src/libGLESv2/Framebuffer.h
@@ -39,6 +39,8 @@
     IDirect3DSurface9 *getRenderTarget();
     IDirect3DSurface9 *getDepthStencil();
 
+    unsigned int getRenderTargetSerial();
+
     Colorbuffer *getColorbuffer();
     Depthbuffer *getDepthbuffer();
     Stencilbuffer *getStencilbuffer();
diff --git a/src/libGLESv2/Renderbuffer.cpp b/src/libGLESv2/Renderbuffer.cpp
index 3d267ef..32eabcf 100644
--- a/src/libGLESv2/Renderbuffer.cpp
+++ b/src/libGLESv2/Renderbuffer.cpp
@@ -15,11 +15,14 @@
 
 namespace gl
 {
+unsigned int Renderbuffer::mCurrentSerial = 1;
+
 Renderbuffer::Renderbuffer()
 {
     mWidth = 0;
     mHeight = 0;
     mFormat = GL_RGBA4; // default format, needs to be one of the expected renderbuffer formats
+    mSerial = issueSerial();
 }
 
 Renderbuffer::~Renderbuffer()
@@ -66,6 +69,16 @@
     return mFormat;
 }
 
+unsigned int Renderbuffer::getSerial() const
+{
+    return mSerial;
+}
+
+unsigned int Renderbuffer::issueSerial()
+{
+    return mCurrentSerial++;
+}
+
 Colorbuffer::Colorbuffer(IDirect3DSurface9 *renderTarget) : mRenderTarget(renderTarget)
 {
     if (renderTarget)
@@ -78,6 +91,7 @@
         mWidth = description.Width;
         mHeight = description.Height;
     }
+
 }
 
 Colorbuffer::Colorbuffer(int width, int height, GLenum format)
diff --git a/src/libGLESv2/Renderbuffer.h b/src/libGLESv2/Renderbuffer.h
index 632361e..ee9133c 100644
--- a/src/libGLESv2/Renderbuffer.h
+++ b/src/libGLESv2/Renderbuffer.h
@@ -36,14 +36,20 @@
     int getWidth();
     int getHeight();
     GLenum getFormat();
+    unsigned int getSerial() const;
+
+    static unsigned int issueSerial();
 
   protected:
     int mWidth;
     int mHeight;
     GLenum mFormat;
+    unsigned int mSerial;
 
   private:
     DISALLOW_COPY_AND_ASSIGN(Renderbuffer);
+
+    static unsigned int mCurrentSerial;
 };
 
 class Colorbuffer : public Renderbuffer
diff --git a/src/libGLESv2/Texture.h b/src/libGLESv2/Texture.h
index e7c0db3..809510d 100644
--- a/src/libGLESv2/Texture.h
+++ b/src/libGLESv2/Texture.h
@@ -69,7 +69,8 @@
     class TextureColorbufferProxy : public Colorbuffer
     {
       public:
-        TextureColorbufferProxy(Texture *texture, GLenum target); // target is a 2D-like texture target (GL_TEXTURE_2D or one of the cube face targets)
+        TextureColorbufferProxy(Texture *texture, GLenum target);
+            // target is a 2D-like texture target (GL_TEXTURE_2D or one of the cube face targets)
 
         virtual IDirect3DSurface9 *getRenderTarget();