meta: use _mesa_prepare_mipmap_levels()
[mesa.git] / src / mesa / drivers / common / meta_tex_subimage.c
index ffbbacd8fd892ffcf99f1991838cd58373bdb4c8..62c3fce4249c259ef37baae98abedb4069b3681a 100644 (file)
  *    Jason Ekstrand <jason.ekstrand@intel.com>
  */
 
+#include "blend.h"
 #include "bufferobj.h"
 #include "buffers.h"
+#include "clear.h"
 #include "fbobject.h"
+#include "framebuffer.h"
 #include "glformats.h"
 #include "glheader.h"
 #include "image.h"
 #include "macros.h"
 #include "meta.h"
 #include "pbo.h"
+#include "readpix.h"
 #include "shaderapi.h"
 #include "state.h"
 #include "teximage.h"
 #include "uniforms.h"
 #include "varray.h"
 
+static bool
+need_signed_unsigned_int_conversion(mesa_format mesaFormat,
+                                    GLenum format, GLenum type)
+{
+   const GLenum mesaFormatType = _mesa_get_format_datatype(mesaFormat);
+   const bool is_format_integer = _mesa_is_enum_format_integer(format);
+   return (mesaFormatType == GL_INT &&
+           is_format_integer &&
+           (type == GL_UNSIGNED_INT ||
+            type == GL_UNSIGNED_SHORT ||
+            type == GL_UNSIGNED_BYTE)) ||
+          (mesaFormatType == GL_UNSIGNED_INT &&
+           is_format_integer &&
+           (type == GL_INT ||
+            type == GL_SHORT ||
+            type == GL_BYTE));
+}
+
 static struct gl_texture_image *
-create_texture_for_pbo(struct gl_context *ctx, bool create_pbo,
-                       GLenum pbo_target, int width, int height,
+create_texture_for_pbo(struct gl_context *ctx,
+                       bool create_pbo, GLenum pbo_target,
+                       int dims, int width, int height, int depth,
                        GLenum format, GLenum type, const void *pixels,
                        const struct gl_pixelstore_attrib *packing,
-                       GLuint *tmp_pbo, GLuint *tmp_tex)
+                       struct gl_buffer_object **tmp_pbo, GLuint *tmp_tex)
 {
    uint32_t pbo_format;
    GLenum internal_format;
@@ -70,30 +93,45 @@ create_texture_for_pbo(struct gl_context *ctx, bool create_pbo,
       return NULL;
 
    /* Account for SKIP_PIXELS, SKIP_ROWS, ALIGNMENT, and SKIP_IMAGES */
-   pixels = _mesa_image_address3d(packing, pixels,
-                                  width, height, format, type, 0, 0, 0);
+   uint32_t first_pixel = _mesa_image_offset(dims, packing, width, height,
+                                             format, type,
+                                             0, 0, 0);
+   uint32_t last_pixel =  _mesa_image_offset(dims, packing, width, height,
+                                             format, type,
+                                             depth-1, height-1, width);
    row_stride = _mesa_image_row_stride(packing, width, format, type);
 
    if (_mesa_is_bufferobj(packing->BufferObj)) {
-      *tmp_pbo = 0;
+      *tmp_pbo = NULL;
       buffer_obj = packing->BufferObj;
+      first_pixel += (intptr_t)pixels;
    } else {
+      bool is_pixel_pack = pbo_target == GL_PIXEL_PACK_BUFFER;
+
       assert(create_pbo);
 
-      _mesa_GenBuffers(1, tmp_pbo);
+      *tmp_pbo = ctx->Driver.NewBufferObject(ctx, 0xDEADBEEF);
+      if (*tmp_pbo == NULL)
+         return NULL;
 
-      /* We are not doing this inside meta_begin/end.  However, we know the
-       * client doesn't have the given target bound, so we can go ahead and
-       * squash it.  We'll set it back when we're done.
+      /* In case of GL_PIXEL_PACK_BUFFER, pass null pointer for the pixel
+       * data to avoid unnecessary data copying in _mesa_buffer_data.
        */
-      _mesa_BindBuffer(pbo_target, *tmp_pbo);
-
-      _mesa_BufferData(pbo_target, row_stride * height, pixels, GL_STREAM_DRAW);
-
-      buffer_obj = ctx->Unpack.BufferObj;
-      pixels = NULL;
-
-      _mesa_BindBuffer(pbo_target, 0);
+      if (is_pixel_pack)
+         _mesa_buffer_data(ctx, *tmp_pbo, GL_NONE,
+                           last_pixel - first_pixel,
+                           NULL,
+                           GL_STREAM_READ,
+                           __func__);
+      else
+         _mesa_buffer_data(ctx, *tmp_pbo, GL_NONE,
+                           last_pixel - first_pixel,
+                           (char *)pixels + first_pixel,
+                           GL_STREAM_DRAW,
+                           __func__);
+
+      buffer_obj = *tmp_pbo;
+      first_pixel = 0;
    }
 
    _mesa_GenTextures(1, tmp_tex);
@@ -106,18 +144,25 @@ create_texture_for_pbo(struct gl_context *ctx, bool create_pbo,
 
    internal_format = _mesa_get_format_base_format(pbo_format);
 
+   /* The texture is addressed as a single very-tall image, so we
+    * need to pack the multiple image depths together taking the
+    * inter-image padding into account.
+    */
+   int image_height = packing->ImageHeight == 0 ? height : packing->ImageHeight;
+   int full_height = image_height * (depth - 1) + height;
+
    tex_image = _mesa_get_tex_image(ctx, tex_obj, tex_obj->Target, 0);
-   _mesa_init_teximage_fields(ctx, tex_image, width, height, 1,
+   _mesa_init_teximage_fields(ctx, tex_image, width, full_height, 1,
                               0, internal_format, pbo_format);
 
    read_only = pbo_target == GL_PIXEL_UNPACK_BUFFER;
    if (!ctx->Driver.SetTextureStorageForBufferObject(ctx, tex_obj,
                                                      buffer_obj,
-                                                     (intptr_t)pixels,
+                                                     first_pixel,
                                                      row_stride,
                                                      read_only)) {
       _mesa_DeleteTextures(1, tmp_tex);
-      _mesa_DeleteBuffers(1, tmp_pbo);
+      _mesa_reference_buffer_object(ctx, tmp_pbo, NULL);
       return NULL;
    }
 
@@ -130,19 +175,21 @@ _mesa_meta_pbo_TexSubImage(struct gl_context *ctx, GLuint dims,
                            int xoffset, int yoffset, int zoffset,
                            int width, int height, int depth,
                            GLenum format, GLenum type, const void *pixels,
-                           bool allocate_storage, bool create_pbo,
+                           bool create_pbo,
                            const struct gl_pixelstore_attrib *packing)
 {
-   GLuint pbo = 0, pbo_tex = 0, fbos[2] = { 0, 0 };
+   struct gl_buffer_object *pbo = NULL;
+   GLuint pbo_tex = 0;
+   struct gl_framebuffer *readFb = NULL;
+   struct gl_framebuffer *drawFb = NULL;
+   int image_height;
    struct gl_texture_image *pbo_tex_image;
    GLenum status;
    bool success = false;
-   int z, iters;
+   int z;
 
-   /* XXX: This should probably be passed in from somewhere */
-   const char *where = "_mesa_meta_pbo_TexSubImage";
-
-   if (!_mesa_is_bufferobj(packing->BufferObj) && !create_pbo)
+   if (!_mesa_is_bufferobj(packing->BufferObj) &&
+       (!create_pbo || pixels == NULL))
       return false;
 
    if (format == GL_DEPTH_COMPONENT ||
@@ -154,56 +201,71 @@ _mesa_meta_pbo_TexSubImage(struct gl_context *ctx, GLuint dims,
    if (ctx->_ImageTransferState)
       return false;
 
-   if (!_mesa_validate_pbo_access(dims, packing, width, height, depth,
-                                  format, type, INT_MAX, pixels)) {
-      _mesa_error(ctx, GL_INVALID_OPERATION,
-                  "%s(out of bounds PBO access)", where);
-      return true;
-   }
+   /* This function rely on BlitFramebuffer to fill in the pixel data for
+    * glTex[Sub]Image*D. But, BlitFrameBuffer doesn't support signed to
+    * unsigned or unsigned to signed integer conversions.
+    */
+   if (need_signed_unsigned_int_conversion(tex_image->TexFormat, format, type))
+      return false;
 
-   if (_mesa_check_disallowed_mapping(packing->BufferObj)) {
-      /* buffer is mapped - that's an error */
-      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(PBO is mapped)", where);
-      return true;
-   }
+   /* For arrays, use a tall (height * depth) 2D texture but taking into
+    * account the inter-image padding specified with the image height packing
+    * property.
+    */
+   image_height = packing->ImageHeight == 0 ? height : packing->ImageHeight;
 
-   /* Only accept tightly packed pixels from the user. */
-   if (packing->ImageHeight != 0 && packing->ImageHeight != height)
-      return false;
+   _mesa_meta_begin(ctx, ~(MESA_META_PIXEL_TRANSFER |
+                           MESA_META_PIXEL_STORE));
 
-   /* For arrays, use a tall (height * depth) 2D texture. */
    pbo_tex_image = create_texture_for_pbo(ctx, create_pbo,
                                           GL_PIXEL_UNPACK_BUFFER,
-                                          width, height * depth,
+                                          dims, width, height, depth,
                                           format, type, pixels, packing,
                                           &pbo, &pbo_tex);
-   if (!pbo_tex_image)
+   if (!pbo_tex_image) {
+      _mesa_meta_end(ctx);
       return false;
+   }
 
-   if (allocate_storage)
-      ctx->Driver.AllocTextureImageBuffer(ctx, tex_image);
+   readFb = ctx->Driver.NewFramebuffer(ctx, 0xDEADBEEF);
+   if (readFb == NULL)
+      goto fail;
 
-   /* Only stash the current FBO */
-   _mesa_meta_begin(ctx, 0);
+   drawFb = ctx->Driver.NewFramebuffer(ctx, 0xDEADBEEF);
+   if (drawFb == NULL)
+      goto fail;
 
-   _mesa_GenFramebuffers(2, fbos);
-   _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER, fbos[0]);
-   _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, fbos[1]);
+   _mesa_bind_framebuffers(ctx, drawFb, tex_image ? readFb : ctx->ReadBuffer);
 
-   _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
-                             pbo_tex_image, 0);
+   if (tex_image->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
+      assert(depth == 1);
+      assert(zoffset == 0);
+      depth = height;
+      height = 1;
+      image_height = 1;
+      zoffset = yoffset;
+      yoffset = 0;
+   }
+
+   _mesa_meta_framebuffer_texture_image(ctx, ctx->ReadBuffer,
+                                        GL_COLOR_ATTACHMENT0,
+                                        pbo_tex_image, 0);
    /* If this passes on the first layer it should pass on the others */
-   status = _mesa_CheckFramebufferStatus(GL_READ_FRAMEBUFFER);
+   status = _mesa_check_framebuffer_status(ctx, ctx->ReadBuffer);
    if (status != GL_FRAMEBUFFER_COMPLETE)
       goto fail;
 
-   _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
-                             tex_image, zoffset);
+   _mesa_meta_framebuffer_texture_image(ctx, ctx->DrawBuffer,
+                                        GL_COLOR_ATTACHMENT0,
+                                        tex_image, zoffset);
    /* If this passes on the first layer it should pass on the others */
-   status = _mesa_CheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);
+   status = _mesa_check_framebuffer_status(ctx, ctx->DrawBuffer);
    if (status != GL_FRAMEBUFFER_COMPLETE)
       goto fail;
 
+   /* Explicitly disable sRGB encoding */
+   ctx->DrawBuffer->Visual.sRGBCapable = false;
+
    _mesa_update_state(ctx);
 
    if (_mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
@@ -213,37 +275,28 @@ _mesa_meta_pbo_TexSubImage(struct gl_context *ctx, GLuint dims,
                                   GL_COLOR_BUFFER_BIT, GL_NEAREST))
       goto fail;
 
-   iters = tex_image->TexObject->Target == GL_TEXTURE_1D_ARRAY ?
-           height : depth;
-
-   for (z = 1; z < iters; z++) {
-      _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
-                                pbo_tex_image, z);
-      _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
-                                tex_image, zoffset + z);
+   for (z = 1; z < depth; z++) {
+      _mesa_meta_framebuffer_texture_image(ctx, ctx->DrawBuffer,
+                                           GL_COLOR_ATTACHMENT0,
+                                           tex_image, zoffset + z);
 
       _mesa_update_state(ctx);
 
-      if (tex_image->TexObject->Target == GL_TEXTURE_1D_ARRAY)
-         _mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
-                                    0, z, width, z + 1,
-                                    xoffset, yoffset,
-                                    xoffset + width, yoffset + 1,
-                                    GL_COLOR_BUFFER_BIT, GL_NEAREST);
-      else
-         _mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
-                                    0, z * height, width, (z + 1) * height,
-                                    xoffset, yoffset,
-                                    xoffset + width, yoffset + height,
-                                    GL_COLOR_BUFFER_BIT, GL_NEAREST);
+      _mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
+                                 0, z * image_height,
+                                 width, z * image_height + height,
+                                 xoffset, yoffset,
+                                 xoffset + width, yoffset + height,
+                                 GL_COLOR_BUFFER_BIT, GL_NEAREST);
    }
 
    success = true;
 
 fail:
-   _mesa_DeleteFramebuffers(2, fbos);
+   _mesa_reference_framebuffer(&readFb, NULL);
+   _mesa_reference_framebuffer(&drawFb, NULL);
    _mesa_DeleteTextures(1, &pbo_tex);
-   _mesa_DeleteBuffers(1, &pbo);
+   _mesa_reference_buffer_object(ctx, &pbo, NULL);
 
    _mesa_meta_end(ctx);
 
@@ -258,14 +311,18 @@ _mesa_meta_pbo_GetTexSubImage(struct gl_context *ctx, GLuint dims,
                               GLenum format, GLenum type, const void *pixels,
                               const struct gl_pixelstore_attrib *packing)
 {
-   GLuint pbo = 0, pbo_tex = 0, fbos[2] = { 0, 0 };
+   struct gl_buffer_object *pbo = NULL;
+   GLuint pbo_tex = 0;
+   struct gl_framebuffer *readFb;
+   struct gl_framebuffer *drawFb;
+   int image_height;
    struct gl_texture_image *pbo_tex_image;
-   GLenum status;
-   bool success = false;
-   int z, iters;
-
-   /* XXX: This should probably be passed in from somewhere */
-   const char *where = "_mesa_meta_pbo_GetTexSubImage";
+   struct gl_renderbuffer *rb = NULL;
+   GLenum dstBaseFormat = _mesa_unpack_format_to_base_format(format);
+   GLenum status, src_base_format;
+   bool success = false, clear_channels_to_zero = false;
+   float save_clear_color[4];
+   int z;
 
    if (!_mesa_is_bufferobj(packing->BufferObj))
       return false;
@@ -276,63 +333,99 @@ _mesa_meta_pbo_GetTexSubImage(struct gl_context *ctx, GLuint dims,
        format == GL_COLOR_INDEX)
       return false;
 
-   if (ctx->_ImageTransferState)
-      return false;
+   /* Don't use meta path for readpixels in below conditions. */
+   if (!tex_image) {
+      rb = ctx->ReadBuffer->_ColorReadBuffer;
 
-   if (!_mesa_validate_pbo_access(dims, packing, width, height, depth,
-                                  format, type, INT_MAX, pixels)) {
-      _mesa_error(ctx, GL_INVALID_OPERATION,
-                  "%s(out of bounds PBO access)", where);
-      return true;
+      /* _mesa_get_readpixels_transfer_ops() includes the cases of read
+       * color clamping along with the ctx->_ImageTransferState.
+       */
+      if (_mesa_get_readpixels_transfer_ops(ctx, rb->Format, format,
+                                            type, GL_FALSE))
+         return false;
+
+      if (_mesa_need_rgb_to_luminance_conversion(rb->_BaseFormat,
+                                                 dstBaseFormat))
+         return false;
+
+      /* This function rely on BlitFramebuffer to fill in the pixel data for
+       * ReadPixels. But, BlitFrameBuffer doesn't support signed to unsigned
+       * or unsigned to signed integer conversions. OpenGL spec expects an
+       * invalid operation in that case.
+       */
+      if (need_signed_unsigned_int_conversion(rb->Format, format, type))
+         return false;
    }
 
-   if (_mesa_check_disallowed_mapping(packing->BufferObj)) {
-      /* buffer is mapped - that's an error */
-      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(PBO is mapped)", where);
-      return true;
-   }
+   /* For arrays, use a tall (height * depth) 2D texture but taking into
+    * account the inter-image padding specified with the image height packing
+    * property.
+    */
+   image_height = packing->ImageHeight == 0 ? height : packing->ImageHeight;
 
-   /* Only accept tightly packed pixels from the user. */
-   if (packing->ImageHeight != 0 && packing->ImageHeight != height)
-      return false;
+   _mesa_meta_begin(ctx, ~(MESA_META_PIXEL_TRANSFER |
+                           MESA_META_PIXEL_STORE));
 
-   /* For arrays, use a tall (height * depth) 2D texture. */
    pbo_tex_image = create_texture_for_pbo(ctx, false, GL_PIXEL_PACK_BUFFER,
-                                          width, height * depth,
+                                          dims, width, height, depth,
                                           format, type, pixels, packing,
                                           &pbo, &pbo_tex);
-   if (!pbo_tex_image)
+
+   if (!pbo_tex_image) {
+      _mesa_meta_end(ctx);
       return false;
+   }
 
-   /* Only stash the current FBO */
-   _mesa_meta_begin(ctx, 0);
+   /* GL_CLAMP_FRAGMENT_COLOR doesn't affect ReadPixels and GettexImage */
+   if (ctx->Extensions.ARB_color_buffer_float)
+      _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);
 
-   _mesa_GenFramebuffers(2, fbos);
+   readFb = ctx->Driver.NewFramebuffer(ctx, 0xDEADBEEF);
+   if (readFb == NULL)
+      goto fail;
+
+   drawFb = ctx->Driver.NewFramebuffer(ctx, 0xDEADBEEF);
+   if (drawFb == NULL)
+      goto fail;
+
+   if (tex_image && tex_image->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
+      assert(depth == 1);
+      assert(zoffset == 0);
+      depth = height;
+      height = 1;
+      image_height = 1;
+      zoffset = yoffset;
+      yoffset = 0;
+   }
 
    /* If we were given a texture, bind it to the read framebuffer.  If not,
     * we're doing a ReadPixels and we should just use whatever framebuffer
     * the client has bound.
     */
+   _mesa_bind_framebuffers(ctx, drawFb, tex_image ? readFb : ctx->ReadBuffer);
    if (tex_image) {
-      _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER, fbos[0]);
-      _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
-                                tex_image, zoffset);
+      _mesa_meta_framebuffer_texture_image(ctx, ctx->ReadBuffer,
+                                           GL_COLOR_ATTACHMENT0,
+                                           tex_image, zoffset);
       /* If this passes on the first layer it should pass on the others */
-      status = _mesa_CheckFramebufferStatus(GL_READ_FRAMEBUFFER);
+      status = _mesa_check_framebuffer_status(ctx, ctx->ReadBuffer);
       if (status != GL_FRAMEBUFFER_COMPLETE)
          goto fail;
    } else {
       assert(depth == 1);
    }
 
-   _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, fbos[1]);
-   _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
-                             pbo_tex_image, 0);
+   _mesa_meta_framebuffer_texture_image(ctx, ctx->DrawBuffer,
+                                        GL_COLOR_ATTACHMENT0,
+                                        pbo_tex_image, 0);
    /* If this passes on the first layer it should pass on the others */
-   status = _mesa_CheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);
+   status = _mesa_check_framebuffer_status(ctx, ctx->DrawBuffer);
    if (status != GL_FRAMEBUFFER_COMPLETE)
       goto fail;
 
+   /* Explicitly disable sRGB encoding */
+   ctx->DrawBuffer->Visual.sRGBCapable = false;
+
    _mesa_update_state(ctx);
 
    if (_mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
@@ -342,39 +435,58 @@ _mesa_meta_pbo_GetTexSubImage(struct gl_context *ctx, GLuint dims,
                                   GL_COLOR_BUFFER_BIT, GL_NEAREST))
       goto fail;
 
-   if (tex_image && tex_image->TexObject->Target == GL_TEXTURE_1D_ARRAY)
-      iters = height;
-   else
-      iters = depth;
+   src_base_format = tex_image ?
+                     tex_image->_BaseFormat :
+                     ctx->ReadBuffer->_ColorReadBuffer->_BaseFormat;
 
-   for (z = 1; z < iters; z++) {
-      _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
-                                tex_image, zoffset + z);
-      _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
-                                pbo_tex_image, z);
+   /* Depending on the base formats involved we might need to rebase some
+    * values. For example if we download from a Luminance format to RGBA
+    * format, we want G=0 and B=0.
+    */
+   clear_channels_to_zero =
+      _mesa_need_luminance_to_rgb_conversion(src_base_format,
+                                             pbo_tex_image->_BaseFormat);
+
+   if (clear_channels_to_zero) {
+      memcpy(save_clear_color, ctx->Color.ClearColor.f, 4 * sizeof(float));
+      /* Clear the Green, Blue channels. */
+      _mesa_ColorMask(GL_FALSE, GL_TRUE, GL_TRUE,
+                      src_base_format != GL_LUMINANCE_ALPHA);
+      _mesa_ClearColor(0.0, 0.0, 0.0, 1.0);
+      _mesa_Clear(GL_COLOR_BUFFER_BIT);
+   }
+
+   for (z = 1; z < depth; z++) {
+      _mesa_meta_framebuffer_texture_image(ctx, ctx->ReadBuffer,
+                                           GL_COLOR_ATTACHMENT0,
+                                           tex_image, zoffset + z);
 
       _mesa_update_state(ctx);
 
-      if (tex_image->TexObject->Target == GL_TEXTURE_1D_ARRAY)
-         _mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
-                                    xoffset, yoffset,
-                                    xoffset + width, yoffset + 1,
-                                    0, z, width, z + 1,
-                                    GL_COLOR_BUFFER_BIT, GL_NEAREST);
-      else
-         _mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
-                                    xoffset, yoffset,
-                                    xoffset + width, yoffset + height,
-                                    0, z * height, width, (z + 1) * height,
-                                    GL_COLOR_BUFFER_BIT, GL_NEAREST);
+      _mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
+                                 xoffset, yoffset,
+                                 xoffset + width, yoffset + height,
+                                 0, z * image_height,
+                                 width, z * image_height + height,
+                                 GL_COLOR_BUFFER_BIT, GL_NEAREST);
+      if (clear_channels_to_zero)
+         _mesa_Clear(GL_COLOR_BUFFER_BIT);
+   }
+
+   /* Unmask the color channels and restore the saved clear color values. */
+   if (clear_channels_to_zero) {
+      _mesa_ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+      _mesa_ClearColor(save_clear_color[0], save_clear_color[1],
+                       save_clear_color[2], save_clear_color[3]);
    }
 
    success = true;
 
 fail:
-   _mesa_DeleteFramebuffers(2, fbos);
+   _mesa_reference_framebuffer(&drawFb, NULL);
+   _mesa_reference_framebuffer(&readFb, NULL);
    _mesa_DeleteTextures(1, &pbo_tex);
-   _mesa_DeleteBuffers(1, &pbo);
+   _mesa_reference_buffer_object(ctx, &pbo, NULL);
 
    _mesa_meta_end(ctx);