Blame SOURCES/0005-cogl-Allow-glBlitFramebuffer-between-onscreen-offscr.patch

1a3082
From e4b2234d9918e9d3357ac3c7ca3898599725d3da Mon Sep 17 00:00:00 2001
1a3082
From: Pekka Paalanen <pekka.paalanen@collabora.com>
1a3082
Date: Mon, 6 May 2019 15:08:29 +0300
1a3082
Subject: [PATCH 05/12] cogl: Allow glBlitFramebuffer between
1a3082
 onscreen/offscreen
1a3082
1a3082
Depends on "cogl: Replace ANGLE with GLES3 and NV framebuffer_blit"
1a3082
1a3082
Allow blitting between onscreen and offscreen framebuffers by doing the y-flip
1a3082
as necessary. This was not possible with ANGLE, but now with ANGLE gone,
1a3082
glBlitFramebuffer supports flipping the copied image.
1a3082
1a3082
This will be useful in follow-up work to copy from onscreen primary GPU
1a3082
framebuffer to an offscreen secondary GPU framebuffer.
1a3082
1a3082
https://gitlab.gnome.org/GNOME/mutter/merge_requests/615
1a3082
1a3082
(cherry picked from commit 45289b3d65e308117f1bc8fe6a4c88c1baaacca7)
1a3082
---
1a3082
 cogl/cogl/cogl-framebuffer-private.h      | 14 +++----
1a3082
 cogl/cogl/cogl-framebuffer.c              | 46 ++++++++++++++++++-----
1a3082
 cogl/cogl/driver/gl/cogl-framebuffer-gl.c |  5 +--
1a3082
 3 files changed, 43 insertions(+), 22 deletions(-)
1a3082
1a3082
diff --git a/cogl/cogl/cogl-framebuffer-private.h b/cogl/cogl/cogl-framebuffer-private.h
1a3082
index b06fbaee1..f68153d8b 100644
1a3082
--- a/cogl/cogl/cogl-framebuffer-private.h
1a3082
+++ b/cogl/cogl/cogl-framebuffer-private.h
1a3082
@@ -381,7 +381,11 @@ _cogl_push_framebuffers (CoglFramebuffer *draw_buffer,
1a3082
  * This blits a region of the color buffer of the source buffer
1a3082
  * to the destination buffer. This function should only be
1a3082
  * called if the COGL_PRIVATE_FEATURE_OFFSCREEN_BLIT feature is
1a3082
- * advertised. The two buffers must both be offscreen.
1a3082
+ * advertised.
1a3082
+ *
1a3082
+ * The source and destination rectangles are defined in offscreen
1a3082
+ * framebuffer orientation. When copying between an offscreen and
1a3082
+ * onscreen framebuffers, the image is y-flipped accordingly.
1a3082
  *
1a3082
  * The two buffers must have the same value types (e.g. floating-point,
1a3082
  * unsigned int, signed int, or fixed-point), but color formats do not
1a3082
@@ -396,14 +400,6 @@ _cogl_push_framebuffers (CoglFramebuffer *draw_buffer,
1a3082
  * scale the results it may make more sense to draw a primitive
1a3082
  * instead.
1a3082
  *
1a3082
- * We can only really support blitting between two offscreen buffers
1a3082
- * for this function on GLES2.0. This is because we effectively render
1a3082
- * upside down to offscreen buffers to maintain Cogl's representation
1a3082
- * of the texture coordinate system where 0,0 is the top left of the
1a3082
- * texture. If we were to blit from an offscreen to an onscreen buffer
1a3082
- * then we would need to mirror the blit along the x-axis but the GLES
1a3082
- * extension does not support this.
1a3082
- *
1a3082
  * The GL function is documented to be affected by the scissor. This
1a3082
  * function therefore ensure that an empty clip stack is flushed
1a3082
  * before performing the blit which means the scissor is effectively
1a3082
diff --git a/cogl/cogl/cogl-framebuffer.c b/cogl/cogl/cogl-framebuffer.c
1a3082
index 0bc225945..90976a611 100644
1a3082
--- a/cogl/cogl/cogl-framebuffer.c
1a3082
+++ b/cogl/cogl/cogl-framebuffer.c
1a3082
@@ -1460,15 +1460,12 @@ _cogl_blit_framebuffer (CoglFramebuffer *src,
1a3082
                         int height)
1a3082
 {
1a3082
   CoglContext *ctx = src->context;
1a3082
+  int src_x1, src_y1, src_x2, src_y2;
1a3082
+  int dst_x1, dst_y1, dst_x2, dst_y2;
1a3082
 
1a3082
   _COGL_RETURN_IF_FAIL (_cogl_has_private_feature
1a3082
                         (ctx, COGL_PRIVATE_FEATURE_OFFSCREEN_BLIT));
1a3082
 
1a3082
-  /* We can only support blitting between offscreen buffers because
1a3082
-     otherwise we would need to mirror the image and GLES2.0 doesn't
1a3082
-     support this */
1a3082
-  _COGL_RETURN_IF_FAIL (cogl_is_offscreen (src));
1a3082
-  _COGL_RETURN_IF_FAIL (cogl_is_offscreen (dest));
1a3082
   /* The buffers must use the same premult convention */
1a3082
   _COGL_RETURN_IF_FAIL ((src->internal_format & COGL_PREMULT_BIT) ==
1a3082
                         (dest->internal_format & COGL_PREMULT_BIT));
1a3082
@@ -1492,10 +1489,41 @@ _cogl_blit_framebuffer (CoglFramebuffer *src,
1a3082
    * as changed */
1a3082
   ctx->current_draw_buffer_changes |= COGL_FRAMEBUFFER_STATE_CLIP;
1a3082
 
1a3082
-  ctx->glBlitFramebuffer (src_x, src_y,
1a3082
-                          src_x + width, src_y + height,
1a3082
-                          dst_x, dst_y,
1a3082
-                          dst_x + width, dst_y + height,
1a3082
+  /* Offscreens we do the normal way, onscreens need an y-flip. Even if
1a3082
+   * we consider offscreens to be rendered upside-down, the offscreen
1a3082
+   * orientation is in this function's API. */
1a3082
+  if (cogl_is_offscreen (src))
1a3082
+    {
1a3082
+      src_x1 = src_x;
1a3082
+      src_y1 = src_y;
1a3082
+      src_x2 = src_x + width;
1a3082
+      src_y2 = src_y + height;
1a3082
+    }
1a3082
+  else
1a3082
+    {
1a3082
+      src_x1 = src_x;
1a3082
+      src_y1 = cogl_framebuffer_get_height (src) - src_y;
1a3082
+      src_x2 = src_x + width;
1a3082
+      src_y2 = src_y1 - height;
1a3082
+    }
1a3082
+
1a3082
+  if (cogl_is_offscreen (dest))
1a3082
+    {
1a3082
+      dst_x1 = dst_x;
1a3082
+      dst_y1 = dst_y;
1a3082
+      dst_x2 = dst_x + width;
1a3082
+      dst_y2 = dst_y + height;
1a3082
+    }
1a3082
+  else
1a3082
+    {
1a3082
+      dst_x1 = dst_x;
1a3082
+      dst_y1 = cogl_framebuffer_get_height (dest) - dst_y;
1a3082
+      dst_x2 = dst_x + width;
1a3082
+      dst_y2 = dst_y1 - height;
1a3082
+    }
1a3082
+
1a3082
+  ctx->glBlitFramebuffer (src_x1, src_y1, src_x2, src_y2,
1a3082
+                          dst_x1, dst_y1, dst_x2, dst_y2,
1a3082
                           GL_COLOR_BUFFER_BIT,
1a3082
                           GL_NEAREST);
1a3082
 }
1a3082
diff --git a/cogl/cogl/driver/gl/cogl-framebuffer-gl.c b/cogl/cogl/driver/gl/cogl-framebuffer-gl.c
1a3082
index 5402a7075..83e1d263a 100644
1a3082
--- a/cogl/cogl/driver/gl/cogl-framebuffer-gl.c
1a3082
+++ b/cogl/cogl/driver/gl/cogl-framebuffer-gl.c
1a3082
@@ -400,12 +400,9 @@ _cogl_framebuffer_gl_flush_state (CoglFramebuffer *draw_buffer,
1a3082
       else
1a3082
         {
1a3082
           /* NB: Currently we only take advantage of binding separate
1a3082
-           * read/write buffers for offscreen framebuffer blit
1a3082
-           * purposes.  */
1a3082
+           * read/write buffers for framebuffer blit purposes. */
1a3082
           _COGL_RETURN_IF_FAIL (_cogl_has_private_feature
1a3082
                                 (ctx, COGL_PRIVATE_FEATURE_OFFSCREEN_BLIT));
1a3082
-          _COGL_RETURN_IF_FAIL (draw_buffer->type == COGL_FRAMEBUFFER_TYPE_OFFSCREEN);
1a3082
-          _COGL_RETURN_IF_FAIL (read_buffer->type == COGL_FRAMEBUFFER_TYPE_OFFSCREEN);
1a3082
 
1a3082
           _cogl_framebuffer_gl_bind (draw_buffer, GL_DRAW_FRAMEBUFFER);
1a3082
           _cogl_framebuffer_gl_bind (read_buffer, GL_READ_FRAMEBUFFER);
1a3082
-- 
1a3082
2.21.0
1a3082