Blame SOURCES/Add-support-for-setting-up-stereo-CoglOnscreens.patch

bae666
From d16df5a5aab126d8114b932ba8abab05ff242026 Mon Sep 17 00:00:00 2001
07f135
From: "Owen W. Taylor" <otaylor@fishsoup.net>
07f135
Date: Mon, 28 Apr 2014 12:37:32 -0400
bae666
Subject: [PATCH] Add support for setting up stereo CoglOnscreens
07f135
07f135
If we want to show quad-buffer stereo with Cogl, we need to pick an
07f135
appropriate fbconfig for creating the CoglOnscreen objects. Add
07f135
cogl_onscreen_template_set_stereo_enabled() to indicate whether
07f135
stereo support is needed.
07f135
07f135
Add cogl_framebuffer_get_stereo_mode() to see if a framebuffer was
07f135
created with stereo support.
07f135
07f135
Add cogl_framebuffer_get_stereo_mode() to pick whether to draw to
07f135
the left, right, or both buffers.
bae666
bae666
Reviewed-by: Robert Bragg <robert.bragg@intel.com>
07f135
---
07f135
 cogl/cogl-context-private.h                     |  1 +
07f135
 cogl/cogl-framebuffer-private.h                 |  8 +++-
07f135
 cogl/cogl-framebuffer.c                         | 41 +++++++++++++++++++
07f135
 cogl/cogl-framebuffer.h                         | 52 +++++++++++++++++++++++++
07f135
 cogl/cogl-onscreen-template.c                   |  8 ++++
07f135
 cogl/cogl-onscreen-template.h                   | 18 +++++++++
07f135
 cogl/cogl-types.h                               | 15 +++++++
bae666
 cogl/driver/gl/cogl-framebuffer-gl.c            | 36 +++++++++++++++++
07f135
 cogl/winsys/cogl-winsys-glx-feature-functions.h |  1 +
07f135
 cogl/winsys/cogl-winsys-glx.c                   |  8 +++-
bae666
 10 files changed, 185 insertions(+), 3 deletions(-)
07f135
07f135
diff --git a/cogl/cogl-context-private.h b/cogl/cogl-context-private.h
bae666
index 1000ee5..9e66207 100644
07f135
--- a/cogl/cogl-context-private.h
07f135
+++ b/cogl/cogl-context-private.h
bae666
@@ -270,6 +270,7 @@ struct _CoglContext
07f135
 
07f135
   CoglBool current_gl_dither_enabled;
07f135
   CoglColorMask current_gl_color_mask;
07f135
+  GLenum current_gl_draw_buffer;
07f135
 
07f135
   /* Clipping */
07f135
   /* TRUE if we have a valid clipping stack flushed. In that case
07f135
diff --git a/cogl/cogl-framebuffer-private.h b/cogl/cogl-framebuffer-private.h
bae666
index 4891d53..99ac2fb 100644
07f135
--- a/cogl/cogl-framebuffer-private.h
07f135
+++ b/cogl/cogl-framebuffer-private.h
bae666
@@ -61,6 +61,7 @@ typedef struct
07f135
   int samples_per_pixel;
07f135
   CoglBool swap_throttled;
07f135
   CoglBool depth_texture_enabled;
07f135
+  CoglBool stereo_enabled;
07f135
 } CoglFramebufferConfig;
07f135
 
bae666
 /* Flags to pass to _cogl_offscreen_new_with_texture_full */
bae666
@@ -86,7 +87,8 @@ typedef enum _CoglFramebufferStateIndex
07f135
   COGL_FRAMEBUFFER_STATE_INDEX_COLOR_MASK         = 6,
07f135
   COGL_FRAMEBUFFER_STATE_INDEX_FRONT_FACE_WINDING = 7,
bae666
   COGL_FRAMEBUFFER_STATE_INDEX_DEPTH_WRITE        = 8,
bae666
-  COGL_FRAMEBUFFER_STATE_INDEX_MAX                = 9
bae666
+  COGL_FRAMEBUFFER_STATE_INDEX_STEREO_MODE        = 9,
bae666
+  COGL_FRAMEBUFFER_STATE_INDEX_MAX                = 10
07f135
 } CoglFramebufferStateIndex;
07f135
 
07f135
 typedef enum _CoglFramebufferState
bae666
@@ -99,7 +101,8 @@ typedef enum _CoglFramebufferState
07f135
   COGL_FRAMEBUFFER_STATE_PROJECTION         = 1<<5,
07f135
   COGL_FRAMEBUFFER_STATE_COLOR_MASK         = 1<<6,
bae666
   COGL_FRAMEBUFFER_STATE_FRONT_FACE_WINDING = 1<<7,
bae666
-  COGL_FRAMEBUFFER_STATE_DEPTH_WRITE        = 1<<8
bae666
+  COGL_FRAMEBUFFER_STATE_DEPTH_WRITE        = 1<<8,
bae666
+  COGL_FRAMEBUFFER_STATE_STEREO_MODE        = 1<<9
07f135
 } CoglFramebufferState;
07f135
 
07f135
 #define COGL_FRAMEBUFFER_STATE_ALL ((1<
bae666
@@ -154,6 +157,7 @@ struct _CoglFramebuffer
07f135
   CoglBool            dither_enabled;
bae666
   CoglBool            depth_writing_enabled;
07f135
   CoglColorMask       color_mask;
07f135
+  CoglStereoMode      stereo_mode;
07f135
 
07f135
   /* We journal the textured rectangles we want to submit to OpenGL so
07f135
    * we have an oppertunity to batch them together into less draw
07f135
diff --git a/cogl/cogl-framebuffer.c b/cogl/cogl-framebuffer.c
bae666
index 135b873..1f70d3d 100644
07f135
--- a/cogl/cogl-framebuffer.c
07f135
+++ b/cogl/cogl-framebuffer.c
bae666
@@ -907,6 +907,14 @@ _cogl_framebuffer_compare_depth_write_state (CoglFramebuffer *a,
bae666
     COGL_FRAMEBUFFER_STATE_DEPTH_WRITE : 0;
07f135
 }
07f135
 
07f135
+static unsigned long
07f135
+_cogl_framebuffer_compare_stereo_mode (CoglFramebuffer *a,
07f135
+				       CoglFramebuffer *b)
07f135
+{
07f135
+  return a->stereo_mode != b->stereo_mode ?
07f135
+    COGL_FRAMEBUFFER_STATE_STEREO_MODE : 0;
07f135
+}
07f135
+
07f135
 unsigned long
07f135
 _cogl_framebuffer_compare (CoglFramebuffer *a,
07f135
                            CoglFramebuffer *b,
bae666
@@ -959,6 +967,10 @@ _cogl_framebuffer_compare (CoglFramebuffer *a,
07f135
           differences |=
bae666
             _cogl_framebuffer_compare_depth_write_state (a, b);
07f135
           break;
07f135
+        case COGL_FRAMEBUFFER_STATE_INDEX_STEREO_MODE:
07f135
+          differences |=
07f135
+            _cogl_framebuffer_compare_stereo_mode (a, b);
07f135
+          break;
07f135
         default:
07f135
           g_warn_if_reached ();
07f135
         }
bae666
@@ -1046,6 +1058,12 @@ _cogl_framebuffer_get_stencil_bits (CoglFramebuffer *framebuffer)
07f135
   return bits.stencil;
07f135
 }
07f135
 
07f135
+gboolean
07f135
+cogl_framebuffer_get_is_stereo (CoglFramebuffer *framebuffer)
07f135
+{
07f135
+  return framebuffer->config.stereo_enabled;
07f135
+}
07f135
+
07f135
 CoglColorMask
07f135
 cogl_framebuffer_get_color_mask (CoglFramebuffer *framebuffer)
07f135
 {
bae666
@@ -1069,6 +1087,29 @@ cogl_framebuffer_set_color_mask (CoglFramebuffer *framebuffer,
07f135
       COGL_FRAMEBUFFER_STATE_COLOR_MASK;
07f135
 }
07f135
 
07f135
+CoglStereoMode
07f135
+cogl_framebuffer_get_stereo_mode (CoglFramebuffer *framebuffer)
07f135
+{
07f135
+  return framebuffer->stereo_mode;
07f135
+}
07f135
+
07f135
+void
07f135
+cogl_framebuffer_set_stereo_mode (CoglFramebuffer *framebuffer,
07f135
+				  CoglStereoMode   stereo_mode)
07f135
+{
07f135
+  if (framebuffer->stereo_mode == stereo_mode)
07f135
+    return;
07f135
+
07f135
+  /* Stereo mode changes don't go through the journal */
07f135
+  _cogl_framebuffer_flush_journal (framebuffer);
07f135
+
07f135
+  framebuffer->stereo_mode = stereo_mode;
07f135
+
07f135
+  if (framebuffer->context->current_draw_buffer == framebuffer)
07f135
+    framebuffer->context->current_draw_buffer_changes |=
07f135
+      COGL_FRAMEBUFFER_STATE_STEREO_MODE;
07f135
+}
07f135
+
07f135
 CoglBool
bae666
 cogl_framebuffer_get_depth_write_enabled (CoglFramebuffer *framebuffer)
07f135
 {
07f135
diff --git a/cogl/cogl-framebuffer.h b/cogl/cogl-framebuffer.h
bae666
index 347284f..58d65a8 100644
07f135
--- a/cogl/cogl-framebuffer.h
07f135
+++ b/cogl/cogl-framebuffer.h
bae666
@@ -737,6 +737,23 @@ cogl_framebuffer_get_alpha_bits (CoglFramebuffer *framebuffer);
07f135
 int
07f135
 cogl_framebuffer_get_depth_bits (CoglFramebuffer *framebuffer);
07f135
 
07f135
+/*
07f135
+ * cogl_framebuffer_get_is_stereo:
07f135
+ * @framebuffer: a pointer to a #CoglFramebuffer
07f135
+ *
07f135
+ * Retrieves whether @framebuffer has separate left and right
07f135
+ * buffers for use with stereo drawing. See
07f135
+ * cogl_framebuffer_set_stereo_mode().
07f135
+ *
07f135
+ * Return value: %TRUE if @framebuffer has separate left and
07f135
+ * right buffers.
07f135
+ *
07f135
+ * Since: 1.20
07f135
+ * Stability: unstable
07f135
+ */
07f135
+CoglBool
07f135
+cogl_framebuffer_get_is_stereo (CoglFramebuffer *framebuffer);
07f135
+
07f135
 /**
07f135
  * cogl_framebuffer_get_dither_enabled:
07f135
  * @framebuffer: a pointer to a #CoglFramebuffer
bae666
@@ -847,6 +864,41 @@ cogl_framebuffer_set_color_mask (CoglFramebuffer *framebuffer,
bae666
                                  CoglColorMask color_mask);
07f135
 
07f135
 /**
07f135
+ * cogl_framebuffer_get_stereo_mode:
07f135
+ * @framebuffer: a pointer to a #CoglFramebuffer
07f135
+ *
07f135
+ * Gets the current #CoglStereoMode, which defines which stereo buffers
07f135
+ * should be drawn to. See cogl_framebuffer_set_stereo_mode().
07f135
+ *
07f135
+ * Returns: A #CoglStereoMode
07f135
+ * Since: 1.20
07f135
+ * Stability: unstable
07f135
+ */
07f135
+CoglStereoMode
07f135
+cogl_framebuffer_get_stereo_mode (CoglFramebuffer *framebuffer);
07f135
+
07f135
+/**
07f135
+ * cogl_framebuffer_set_stereo_mode:
07f135
+ * @framebuffer: a pointer to a #CoglFramebuffer
07f135
+ * @stereo_mode: A #CoglStereoMode specifying which stereo buffers
07f135
+ *               should be drawn tow.
07f135
+ *
07f135
+ * Sets which stereo buffers should be drawn to. The default
07f135
+ * is %COGL_STEREO_BOTH, which means that both the left and
07f135
+ * right buffers will be affected by drawing. For this to have
07f135
+ * an effect, the display system must support stereo drawables,
07f135
+ * and the framebuffer must have been created with stereo
07f135
+ * enabled. (See cogl_onscreen_template_set_stereo_enabled(),
07f135
+ * cogl_framebuffer_get_is_stereo().)
07f135
+ *
07f135
+ * Since: 1.20
07f135
+ * Stability: unstable
07f135
+ */
07f135
+void
07f135
+cogl_framebuffer_set_stereo_mode (CoglFramebuffer *framebuffer,
07f135
+				  CoglStereoMode stereo_mode);
07f135
+
07f135
+/**
07f135
  * cogl_framebuffer_set_depth_texture_enabled:
07f135
  * @framebuffer: A #CoglFramebuffer
07f135
  * @enabled: TRUE or FALSE
07f135
diff --git a/cogl/cogl-onscreen-template.c b/cogl/cogl-onscreen-template.c
bae666
index 5a5e54c..3940627 100644
07f135
--- a/cogl/cogl-onscreen-template.c
07f135
+++ b/cogl/cogl-onscreen-template.c
bae666
@@ -95,3 +95,11 @@ cogl_onscreen_template_set_swap_throttled (
07f135
 {
07f135
   onscreen_template->config.swap_throttled = throttled;
07f135
 }
07f135
+
07f135
+void
07f135
+cogl_onscreen_template_set_stereo_enabled (
07f135
+					   CoglOnscreenTemplate *onscreen_template,
07f135
+					   CoglBool enabled)
07f135
+{
07f135
+  onscreen_template->config.stereo_enabled = enabled;
07f135
+}
07f135
diff --git a/cogl/cogl-onscreen-template.h b/cogl/cogl-onscreen-template.h
bae666
index cd1d853..d8714fa 100644
07f135
--- a/cogl/cogl-onscreen-template.h
07f135
+++ b/cogl/cogl-onscreen-template.h
bae666
@@ -107,6 +107,24 @@ cogl_onscreen_template_set_swap_throttled (
07f135
                                           CoglBool throttled);
07f135
 
07f135
 /**
07f135
+ * cogl_onscreen_template_set_stereo_enabled:
07f135
+ * @onscreen_template: A #CoglOnscreenTemplate template framebuffer
07f135
+ * @enabled: Whether framebuffers are created with stereo buffers
07f135
+ *
07f135
+ * Sets whether future #CoglOnscreen framebuffers derived from this
07f135
+ * template are attempted to be created with both left and right
07f135
+ * buffers, for use with stereo display. If the display system
07f135
+ * does not support stereo, then creation of the framebuffer will
07f135
+ * fail.
07f135
+ *
07f135
+ * Since: 1.20
07f135
+ * Stability: unstable
07f135
+ */
07f135
+void
07f135
+cogl_onscreen_template_set_stereo_enabled (
07f135
+					   CoglOnscreenTemplate *onscreen_template,
07f135
+					   CoglBool enabled);
07f135
+/**
07f135
  * cogl_is_onscreen_template:
07f135
  * @object: A #CoglObject pointer
07f135
  *
07f135
diff --git a/cogl/cogl-types.h b/cogl/cogl-types.h
bae666
index b4d79c7..6accf8d 100644
07f135
--- a/cogl/cogl-types.h
07f135
+++ b/cogl/cogl-types.h
bae666
@@ -920,6 +920,21 @@ typedef enum { /*< prefix=COGL_READ_PIXELS >*/
07f135
   COGL_READ_PIXELS_COLOR_BUFFER = 1L << 0
07f135
 } CoglReadPixelsFlags;
07f135
 
07f135
+/**
07f135
+ * CoglStereoMode:
07f135
+ * @COGL_STEREO_BOTH: draw to both stereo buffers
07f135
+ * @COGL_STEREO_LEFT: draw only to the left stereo buffer
07f135
+ * @COGL_STEREO_RIGHT: draw only to the left stereo buffer
07f135
+ *
07f135
+ * Represents how draw should affect the two buffers
07f135
+ * of a stereo framebuffer. See cogl_framebuffer_set_stereo_mode().
07f135
+ */
07f135
+typedef enum {
07f135
+  COGL_STEREO_BOTH,
07f135
+  COGL_STEREO_LEFT,
07f135
+  COGL_STEREO_RIGHT
07f135
+} CoglStereoMode;
07f135
+
07f135
 COGL_END_DECLS
07f135
 
07f135
 #endif /* __COGL_TYPES_H__ */
07f135
diff --git a/cogl/driver/gl/cogl-framebuffer-gl.c b/cogl/driver/gl/cogl-framebuffer-gl.c
bae666
index c0f094d..793b10b 100644
07f135
--- a/cogl/driver/gl/cogl-framebuffer-gl.c
07f135
+++ b/cogl/driver/gl/cogl-framebuffer-gl.c
bae666
@@ -236,6 +236,39 @@ _cogl_framebuffer_gl_flush_front_face_winding_state (CoglFramebuffer *framebuffe
07f135
   context->current_pipeline_age--;
07f135
 }
07f135
 
07f135
+static void
07f135
+_cogl_framebuffer_gl_flush_stereo_mode_state (CoglFramebuffer *framebuffer)
07f135
+{
07f135
+  CoglContext *ctx = framebuffer->context;
07f135
+  GLenum draw_buffer = GL_BACK;
07f135
+
07f135
+  if (framebuffer->type == COGL_FRAMEBUFFER_TYPE_OFFSCREEN)
07f135
+    return;
07f135
+
bae666
+  /* The one-shot default draw buffer setting in _cogl_framebuffer_gl_bind
bae666
+   * must have already happened. If not it would override what we set here. */
bae666
+  g_assert (ctx->was_bound_to_onscreen);
bae666
+
07f135
+  switch (framebuffer->stereo_mode)
07f135
+    {
07f135
+    case COGL_STEREO_BOTH:
07f135
+      draw_buffer = GL_BACK;
07f135
+      break;
07f135
+    case COGL_STEREO_LEFT:
07f135
+      draw_buffer = GL_BACK_LEFT;
07f135
+      break;
07f135
+    case COGL_STEREO_RIGHT:
07f135
+      draw_buffer = GL_BACK_RIGHT;
07f135
+      break;
07f135
+    }
07f135
+
07f135
+  if (ctx->current_gl_draw_buffer != draw_buffer)
07f135
+    {
07f135
+      GE (ctx, glDrawBuffer (draw_buffer));
07f135
+      ctx->current_gl_draw_buffer = draw_buffer;
07f135
+    }
07f135
+}
07f135
+
07f135
 void
07f135
 _cogl_framebuffer_gl_bind (CoglFramebuffer *framebuffer, GLenum target)
07f135
 {
bae666
@@ -406,6 +439,9 @@ _cogl_framebuffer_gl_flush_state (CoglFramebuffer *draw_buffer,
bae666
           /* Nothing to do for depth write state change; the state will always
bae666
            * be taken into account when flushing the pipeline's depth state. */
07f135
           break;
07f135
+        case COGL_FRAMEBUFFER_STATE_INDEX_STEREO_MODE:
07f135
+          _cogl_framebuffer_gl_flush_stereo_mode_state (draw_buffer);
07f135
+          break;
07f135
         default:
07f135
           g_warn_if_reached ();
07f135
         }
07f135
diff --git a/cogl/winsys/cogl-winsys-glx-feature-functions.h b/cogl/winsys/cogl-winsys-glx-feature-functions.h
bae666
index 1e2cec1..ed9df70 100644
07f135
--- a/cogl/winsys/cogl-winsys-glx-feature-functions.h
07f135
+++ b/cogl/winsys/cogl-winsys-glx-feature-functions.h
bae666
@@ -184,6 +184,7 @@ COGL_WINSYS_FEATURE_BEGIN (255, 255,
bae666
                            "swap_event\0",
07f135
                            0,
07f135
                            COGL_WINSYS_FEATURE_SYNC_AND_COMPLETE_EVENT)
07f135
+
07f135
 COGL_WINSYS_FEATURE_END ()
07f135
 
07f135
 COGL_WINSYS_FEATURE_BEGIN (255, 255,
07f135
diff --git a/cogl/winsys/cogl-winsys-glx.c b/cogl/winsys/cogl-winsys-glx.c
bae666
index 3095acf..bf2b5be 100644
07f135
--- a/cogl/winsys/cogl-winsys-glx.c
07f135
+++ b/cogl/winsys/cogl-winsys-glx.c
bae666
@@ -909,6 +909,11 @@ glx_attributes_from_framebuffer_config (CoglDisplay *display,
07f135
   attributes[i++] = 1;
07f135
   attributes[i++] = GLX_STENCIL_SIZE;
07f135
   attributes[i++] = config->need_stencil ? 1: GLX_DONT_CARE;
07f135
+  if (config->stereo_enabled)
07f135
+    {
07f135
+      attributes[i++] = GLX_STEREO;
07f135
+      attributes[i++] = TRUE;
07f135
+    }
07f135
 
07f135
   if (glx_renderer->glx_major == 1 && glx_renderer->glx_minor >= 4 &&
07f135
       config->samples_per_pixel)
bae666
@@ -948,6 +953,7 @@ find_fbconfig (CoglDisplay *display,
07f135
                                              xscreen_num,
07f135
                                              attributes,
07f135
                                              &n_configs);
07f135
+
07f135
   if (!configs || n_configs == 0)
07f135
     {
07f135
       _cogl_set_error (error, COGL_WINSYS_ERROR,
bae666
@@ -1856,7 +1862,7 @@ _cogl_winsys_onscreen_swap_region (CoglOnscreen *onscreen,
07f135
                                       rect[0], rect[1], x2, y2,
07f135
                                       GL_COLOR_BUFFER_BIT, GL_NEAREST);
07f135
         }
07f135
-      context->glDrawBuffer (GL_BACK);
07f135
+      context->glDrawBuffer (context->current_gl_draw_buffer);
07f135
     }
07f135
 
07f135
   /* NB: unlike glXSwapBuffers, glXCopySubBuffer and
07f135
-- 
bae666
2.1.0
07f135