commit 6ebc6d2238fcb972eebc0920a493e18daee0800e
Author: Hans Petter Jansson <hpj@cl.no>
Date:   Thu Oct 10 18:10:52 2013 +0200

    Add API to control per-framebuffer depth writing
    
    Add framebuffer methods cogl_framebuffer_[gs]et_depth_write_enabled()
    and backend bits to pass the state on to glDepthMask().
    
    This allows us to enable or disable depth writing per-framebuffer, which
    if disabled saves us some work in glClear(). When rendering, the flag
    is combined with the pipeline's depth writing flag using a logical AND.
    
    Depth writing is enabled by default.

diff --git a/cogl/cogl-framebuffer-private.h b/cogl/cogl-framebuffer-private.h
index 7513412..94bc6d2 100644
--- a/cogl/cogl-framebuffer-private.h
+++ b/cogl/cogl-framebuffer-private.h
@@ -78,7 +78,8 @@ typedef enum _CoglFramebufferStateIndex
   COGL_FRAMEBUFFER_STATE_INDEX_PROJECTION         = 5,
   COGL_FRAMEBUFFER_STATE_INDEX_COLOR_MASK         = 6,
   COGL_FRAMEBUFFER_STATE_INDEX_FRONT_FACE_WINDING = 7,
-  COGL_FRAMEBUFFER_STATE_INDEX_MAX                = 8
+  COGL_FRAMEBUFFER_STATE_INDEX_DEPTH_WRITE        = 8,
+  COGL_FRAMEBUFFER_STATE_INDEX_MAX                = 9
 } CoglFramebufferStateIndex;
 
 typedef enum _CoglFramebufferState
@@ -90,7 +91,8 @@ typedef enum _CoglFramebufferState
   COGL_FRAMEBUFFER_STATE_MODELVIEW          = 1<<4,
   COGL_FRAMEBUFFER_STATE_PROJECTION         = 1<<5,
   COGL_FRAMEBUFFER_STATE_COLOR_MASK         = 1<<6,
-  COGL_FRAMEBUFFER_STATE_FRONT_FACE_WINDING = 1<<7
+  COGL_FRAMEBUFFER_STATE_FRONT_FACE_WINDING = 1<<7,
+  COGL_FRAMEBUFFER_STATE_DEPTH_WRITE        = 1<<8
 } CoglFramebufferState;
 
 #define COGL_FRAMEBUFFER_STATE_ALL ((1<<COGL_FRAMEBUFFER_STATE_INDEX_MAX) - 1)
@@ -143,6 +145,7 @@ struct _CoglFramebuffer
   CoglClipState       clip_state;
 
   CoglBool            dither_enabled;
+  CoglBool            depth_writing_enabled;
   CoglColorMask       color_mask;
 
   /* We journal the textured rectangles we want to submit to OpenGL so
diff --git a/cogl/cogl-framebuffer.c b/cogl/cogl-framebuffer.c
index b98719c..1f79b2f 100644
--- a/cogl/cogl-framebuffer.c
+++ b/cogl/cogl-framebuffer.c
@@ -106,6 +106,7 @@ _cogl_framebuffer_init (CoglFramebuffer *framebuffer,
   framebuffer->viewport_age = 0;
   framebuffer->viewport_age_for_scissor_workaround = -1;
   framebuffer->dither_enabled = TRUE;
+  framebuffer->depth_writing_enabled = TRUE;
 
   framebuffer->modelview_stack = cogl_matrix_stack_new (ctx);
   framebuffer->projection_stack = cogl_matrix_stack_new (ctx);
@@ -801,6 +802,14 @@ _cogl_framebuffer_compare_front_face_winding_state (CoglFramebuffer *a,
     return 0;
 }
 
+static unsigned long
+_cogl_framebuffer_compare_depth_write_state (CoglFramebuffer *a,
+                                             CoglFramebuffer *b)
+{
+  return a->depth_writing_enabled != b->depth_writing_enabled ?
+    COGL_FRAMEBUFFER_STATE_DEPTH_WRITE : 0;
+}
+
 unsigned long
 _cogl_framebuffer_compare (CoglFramebuffer *a,
                            CoglFramebuffer *b,
@@ -849,6 +858,10 @@ _cogl_framebuffer_compare (CoglFramebuffer *a,
           differences |=
             _cogl_framebuffer_compare_front_face_winding_state (a, b);
           break;
+        case COGL_FRAMEBUFFER_STATE_INDEX_DEPTH_WRITE:
+          differences |=
+            _cogl_framebuffer_compare_depth_write_state (a, b);
+          break;
         default:
           g_warn_if_reached ();
         }
@@ -957,6 +970,26 @@ cogl_framebuffer_set_color_mask (CoglFramebuffer *framebuffer,
 }
 
 CoglBool
+cogl_framebuffer_get_depth_write_enabled (CoglFramebuffer *framebuffer)
+{
+  return framebuffer->depth_writing_enabled;
+}
+
+void
+cogl_framebuffer_set_depth_write_enabled (CoglFramebuffer *framebuffer,
+                                          CoglBool depth_write_enabled)
+{
+  /* XXX: Currently depth write changes don't go through the journal */
+  _cogl_framebuffer_flush_journal (framebuffer);
+
+  framebuffer->depth_writing_enabled = depth_write_enabled;
+
+  if (framebuffer->context->current_draw_buffer == framebuffer)
+    framebuffer->context->current_draw_buffer_changes |=
+      COGL_FRAMEBUFFER_STATE_DEPTH_WRITE;
+}
+
+CoglBool
 cogl_framebuffer_get_dither_enabled (CoglFramebuffer *framebuffer)
 {
   return framebuffer->dither_enabled;
diff --git a/cogl/cogl-framebuffer.h b/cogl/cogl-framebuffer.h
index 2ad41f8..6a112e7 100644
--- a/cogl/cogl-framebuffer.h
+++ b/cogl/cogl-framebuffer.h
@@ -750,6 +750,39 @@ cogl_framebuffer_set_dither_enabled (CoglFramebuffer *framebuffer,
                                      CoglBool dither_enabled);
 
 /**
+ * cogl_framebuffer_get_depth_write_enabled:
+ * @framebuffer: a pointer to a #CoglFramebuffer
+ *
+ * Queries whether depth buffer writing is enabled for @framebuffer. This
+ * can be controlled via cogl_framebuffer_set_depth_write_enabled().
+ *
+ * Return value: %TRUE if depth writing is enabled or %FALSE if not.
+ * Since: 2.0
+ * Stability: unstable
+ */
+CoglBool
+cogl_framebuffer_get_depth_write_enabled (CoglFramebuffer *framebuffer);
+
+/**
+ * cogl_framebuffer_set_depth_write_enabled:
+ * @framebuffer: a pointer to a #CoglFramebuffer
+ * @depth_write_enabled: %TRUE to enable depth writing or %FALSE to disable
+ *
+ * Enables or disables depth buffer writing when rendering to @framebuffer.
+ * If depth writing is enabled for both the framebuffer and the rendering
+ * pipeline, and the framebuffer has an associated depth buffer, depth
+ * information will be written to this buffer during rendering.
+ *
+ * Depth buffer writing is enabled by default.
+ *
+ * Since: 2.0
+ * Stability: unstable
+ */
+void
+cogl_framebuffer_set_depth_write_enabled (CoglFramebuffer *framebuffer,
+                                          CoglBool depth_write_enabled);
+
+/**
  * cogl_framebuffer_get_color_mask:
  * @framebuffer: a pointer to a #CoglFramebuffer
  *
diff --git a/cogl/driver/gl/cogl-framebuffer-gl.c b/cogl/driver/gl/cogl-framebuffer-gl.c
index 12c6bbd..a40f531 100644
--- a/cogl/driver/gl/cogl-framebuffer-gl.c
+++ b/cogl/driver/gl/cogl-framebuffer-gl.c
@@ -916,11 +916,11 @@ _cogl_framebuffer_gl_clear (CoglFramebuffer *framebuffer,
     {
       gl_buffers |= GL_DEPTH_BUFFER_BIT;
 
-      if (ctx->depth_writing_enabled_cache != TRUE)
+      if (ctx->depth_writing_enabled_cache != framebuffer->depth_writing_enabled)
         {
-          GE( ctx, glDepthMask (TRUE));
+          GE( ctx, glDepthMask (framebuffer->depth_writing_enabled));
 
-          ctx->depth_writing_enabled_cache = TRUE;
+          ctx->depth_writing_enabled_cache = framebuffer->depth_writing_enabled;
 
           /* Make sure the DepthMask is updated when the next primitive is drawn */
           ctx->current_pipeline_changes_since_flush |=
diff --git a/cogl/driver/gl/cogl-pipeline-opengl.c b/cogl/driver/gl/cogl-pipeline-opengl.c
index 4e3aedc..51db17a 100644
--- a/cogl/driver/gl/cogl-pipeline-opengl.c
+++ b/cogl/driver/gl/cogl-pipeline-opengl.c
@@ -407,6 +407,11 @@ static void
 flush_depth_state (CoglContext *ctx,
                    CoglDepthState *depth_state)
 {
+  CoglBool depth_writing_enabled = depth_state->write_enabled;
+
+  if (ctx->current_draw_buffer)
+    depth_writing_enabled &= ctx->current_draw_buffer->depth_writing_enabled;
+
   if (ctx->depth_test_enabled_cache != depth_state->test_enabled)
     {
       if (depth_state->test_enabled == TRUE)
@@ -423,11 +428,11 @@ flush_depth_state (CoglContext *ctx,
       ctx->depth_test_function_cache = depth_state->test_function;
     }
 
-  if (ctx->depth_writing_enabled_cache != depth_state->write_enabled)
+  if (ctx->depth_writing_enabled_cache != depth_writing_enabled)
     {
-      GE (ctx, glDepthMask (depth_state->write_enabled ?
+      GE (ctx, glDepthMask (depth_writing_enabled ?
                             GL_TRUE : GL_FALSE));
-      ctx->depth_writing_enabled_cache = depth_state->write_enabled;
+      ctx->depth_writing_enabled_cache = depth_writing_enabled;
     }
 
   if (ctx->driver != COGL_DRIVER_GLES1 &&
