i965: Disable aux buffers for EGLImage-backed miptrees
[mesa.git] / src / mesa / drivers / dri / i965 / intel_tex_image.c
index 331777919c6e8369dd79b304b63afe6b891c133f..290d313465d147b2e7376d90cf3538ec0ff3704e 100644 (file)
@@ -24,7 +24,7 @@
 #include "intel_blit.h"
 #include "intel_fbo.h"
 #include "intel_image.h"
-
+#include "intel_tiled_memcpy.h"
 #include "brw_context.h"
 
 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
@@ -85,86 +85,6 @@ intel_miptree_create_for_teximage(struct brw_context *brw,
                                false);
 }
 
-/* XXX: Do this for TexSubImage also:
- */
-static bool
-try_pbo_upload(struct gl_context *ctx,
-               struct gl_texture_image *image,
-               const struct gl_pixelstore_attrib *unpack,
-              GLenum format, GLenum type, const void *pixels)
-{
-   struct intel_texture_image *intelImage = intel_texture_image(image);
-   struct brw_context *brw = brw_context(ctx);
-   struct intel_buffer_object *pbo = intel_buffer_object(unpack->BufferObj);
-   GLuint src_offset;
-   drm_intel_bo *src_buffer;
-
-   if (!_mesa_is_bufferobj(unpack->BufferObj))
-      return false;
-
-   DBG("trying pbo upload\n");
-
-   if (ctx->_ImageTransferState || unpack->SkipPixels || unpack->SkipRows) {
-      DBG("%s: image transfer\n", __FUNCTION__);
-      return false;
-   }
-
-   ctx->Driver.AllocTextureImageBuffer(ctx, image);
-
-   if (!intelImage->mt) {
-      DBG("%s: no miptree\n", __FUNCTION__);
-      return false;
-   }
-
-   if (!_mesa_format_matches_format_and_type(intelImage->mt->format,
-                                             format, type, false)) {
-      DBG("%s: format mismatch (upload to %s with format 0x%x, type 0x%x)\n",
-         __FUNCTION__, _mesa_get_format_name(intelImage->mt->format),
-         format, type);
-      return false;
-   }
-
-   if (image->TexObject->Target == GL_TEXTURE_1D_ARRAY ||
-       image->TexObject->Target == GL_TEXTURE_2D_ARRAY) {
-      DBG("%s: no support for array textures\n", __FUNCTION__);
-      return false;
-   }
-
-   int src_stride =
-      _mesa_image_row_stride(unpack, image->Width, format, type);
-
-   /* note: potential 64-bit ptr to 32-bit int cast */
-   src_offset = (GLuint) (unsigned long) pixels;
-   src_buffer = intel_bufferobj_buffer(brw, pbo,
-                                       src_offset, src_stride * image->Height);
-
-   struct intel_mipmap_tree *pbo_mt =
-      intel_miptree_create_for_bo(brw,
-                                  src_buffer,
-                                  intelImage->mt->format,
-                                  src_offset,
-                                  image->Width, image->Height,
-                                  src_stride);
-   if (!pbo_mt)
-      return false;
-
-   if (!intel_miptree_blit(brw,
-                           pbo_mt, 0, 0,
-                           0, 0, false,
-                           intelImage->mt, image->Level, image->Face,
-                           0, 0, false,
-                           image->Width, image->Height, GL_COPY)) {
-      DBG("%s: blit failed\n", __FUNCTION__);
-      intel_miptree_release(&pbo_mt);
-      return false;
-   }
-
-   intel_miptree_release(&pbo_mt);
-
-   DBG("%s: success\n", __FUNCTION__);
-   return true;
-}
-
 static void
 intelTexImage(struct gl_context * ctx,
               GLuint dims,
@@ -172,31 +92,44 @@ intelTexImage(struct gl_context * ctx,
               GLenum format, GLenum type, const void *pixels,
               const struct gl_pixelstore_attrib *unpack)
 {
+   struct intel_texture_image *intelImage = intel_texture_image(texImage);
    bool ok;
 
+   bool tex_busy = intelImage->mt && drm_intel_bo_busy(intelImage->mt->bo);
+
    DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
        __FUNCTION__, _mesa_get_format_name(texImage->TexFormat),
        _mesa_lookup_enum_by_nr(texImage->TexObject->Target),
        _mesa_lookup_enum_by_nr(format), _mesa_lookup_enum_by_nr(type),
        texImage->Level, texImage->Width, texImage->Height, texImage->Depth);
 
+   /* Allocate storage for texture data. */
+   if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage)) {
+      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
+      return;
+   }
+
+   assert(intelImage->mt);
+
+   ok = _mesa_meta_pbo_TexSubImage(ctx, dims, texImage, 0, 0, 0,
+                                   texImage->Width, texImage->Height,
+                                   texImage->Depth,
+                                   format, type, pixels,
+                                   false /*allocate_storage*/,
+                                   tex_busy, unpack);
+   if (ok)
+      return;
+
    ok = intel_texsubimage_tiled_memcpy(ctx, dims, texImage,
                                        0, 0, 0, /*x,y,z offsets*/
                                        texImage->Width,
                                        texImage->Height,
                                        texImage->Depth,
                                        format, type, pixels, unpack,
-                                       true /*for_glTexImage*/);
+                                       false /*allocate_storage*/);
    if (ok)
       return;
 
-   /* Attempt to use the blitter for PBO image uploads.
-    */
-   if (dims <= 2 &&
-       try_pbo_upload(ctx, texImage, unpack, format, type, pixels)) {
-      return;
-   }
-
    DBG("%s: upload image %dx%dx%d pixels %p\n",
        __FUNCTION__, texImage->Width, texImage->Height, texImage->Depth,
        pixels);
@@ -221,7 +154,8 @@ intel_set_texture_image_bo(struct gl_context *ctx,
                            uint32_t offset,
                            GLuint width, GLuint height,
                            GLuint pitch,
-                           GLuint tile_x, GLuint tile_y)
+                           GLuint tile_x, GLuint tile_y,
+                           bool disable_aux_buffers)
 {
    struct brw_context *brw = brw_context(ctx);
    struct intel_texture_image *intel_image = intel_texture_image(image);
@@ -236,7 +170,8 @@ intel_set_texture_image_bo(struct gl_context *ctx,
    ctx->Driver.FreeTextureImageBuffer(ctx, image);
 
    intel_image->mt = intel_miptree_create_for_bo(brw, bo, image->TexFormat,
-                                                 0, width, height, pitch);
+                                                 0, width, height, 1, pitch,
+                                                 disable_aux_buffers);
    if (intel_image->mt == NULL)
        return;
    intel_image->mt->target = target;
@@ -320,7 +255,8 @@ intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
                               rb->Base.Base.Width,
                               rb->Base.Base.Height,
                               rb->mt->pitch,
-                              0, 0);
+                              0, 0,
+                              false /*disable_aux_buffers*/);
    _mesa_unlock_texture(&brw->ctx, texObj);
 }
 
@@ -410,100 +346,138 @@ intel_image_target_texture_2d(struct gl_context *ctx, GLenum target,
       return;
    }
 
+   /* Disable creation of the texture's aux buffers because the driver exposes
+    * no EGL API to manage them. That is, there is no API for resolving the aux
+    * buffer's content to the main buffer nor for invalidating the aux buffer's
+    * content.
+    */
    intel_set_texture_image_bo(ctx, texImage, image->bo,
                               target, image->internal_format,
                               image->format, image->offset,
                               image->width,  image->height,
                               image->pitch,
-                              image->tile_x, image->tile_y);
+                              image->tile_x, image->tile_y,
+                              true /*disable_aux_buffers*/);
 }
 
-static bool
-blit_texture_to_pbo(struct gl_context *ctx,
-                    GLenum format, GLenum type,
-                    GLvoid * pixels, struct gl_texture_image *texImage)
+/**
+ * \brief A fast path for glGetTexImage.
+ *
+ * \see intel_readpixels_tiled_memcpy()
+ */
+bool
+intel_gettexsubimage_tiled_memcpy(struct gl_context *ctx,
+                                  struct gl_texture_image *texImage,
+                                  GLint xoffset, GLint yoffset,
+                                  GLsizei width, GLsizei height,
+                                  GLenum format, GLenum type,
+                                  GLvoid *pixels,
+                                  const struct gl_pixelstore_attrib *packing)
 {
-   struct intel_texture_image *intelImage = intel_texture_image(texImage);
    struct brw_context *brw = brw_context(ctx);
-   const struct gl_pixelstore_attrib *pack = &ctx->Pack;
-   struct intel_buffer_object *dst = intel_buffer_object(pack->BufferObj);
-   GLuint dst_offset;
-   drm_intel_bo *dst_buffer;
-   GLenum target = texImage->TexObject->Target;
-
-   /* Check if we can use GPU blit to copy from the hardware texture
-    * format to the user's format/type.
-    * Note that GL's pixel transfer ops don't apply to glGetTexImage()
-    */
+   struct intel_texture_image *image = intel_texture_image(texImage);
+   int dst_pitch;
+
+   /* The miptree's buffer. */
+   drm_intel_bo *bo;
+
+   int error = 0;
+
+   uint32_t cpp;
+   mem_copy_fn mem_copy = NULL;
 
-   if (!_mesa_format_matches_format_and_type(intelImage->mt->format, format,
-                                             type, false))
-   {
-      perf_debug("%s: unsupported format, fallback to CPU mapping for PBO\n",
-                 __FUNCTION__);
+   /* This fastpath is restricted to specific texture types:
+    * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
+    * more types.
+    *
+    * FINISHME: The restrictions below on packing alignment and packing row
+    * length are likely unneeded now because we calculate the destination stride
+    * with _mesa_image_row_stride. However, before removing the restrictions
+    * we need tests.
+    */
+   if (!brw->has_llc ||
+       !(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT_8_8_8_8_REV) ||
+       !(texImage->TexObject->Target == GL_TEXTURE_2D ||
+         texImage->TexObject->Target == GL_TEXTURE_RECTANGLE) ||
+       pixels == NULL ||
+       _mesa_is_bufferobj(packing->BufferObj) ||
+       packing->Alignment > 4 ||
+       packing->SkipPixels > 0 ||
+       packing->SkipRows > 0 ||
+       (packing->RowLength != 0 && packing->RowLength != width) ||
+       packing->SwapBytes ||
+       packing->LsbFirst ||
+       packing->Invert)
+      return false;
 
+   /* We can't handle copying from RGBX or BGRX because the tiled_memcpy
+    * function doesn't set the last channel to 1.
+    */
+   if (texImage->TexFormat == MESA_FORMAT_B8G8R8X8_UNORM ||
+       texImage->TexFormat == MESA_FORMAT_R8G8B8X8_UNORM)
       return false;
-   }
 
-   if (ctx->_ImageTransferState) {
-      perf_debug("%s: bad transfer state, fallback to CPU mapping for PBO\n",
-                 __FUNCTION__);
+   if (!intel_get_memcpy(texImage->TexFormat, format, type, &mem_copy, &cpp,
+                         INTEL_DOWNLOAD))
       return false;
-   }
 
-   if (pack->SwapBytes || pack->LsbFirst) {
-      perf_debug("%s: unsupported pack swap params\n",
-                 __FUNCTION__);
+   /* If this is a nontrivial texture view, let another path handle it instead. */
+   if (texImage->TexObject->MinLayer)
       return false;
-   }
 
-   if (target == GL_TEXTURE_1D_ARRAY ||
-       target == GL_TEXTURE_2D_ARRAY ||
-       target == GL_TEXTURE_CUBE_MAP_ARRAY ||
-       target == GL_TEXTURE_3D) {
-      perf_debug("%s: no support for multiple slices, fallback to CPU mapping "
-                 "for PBO\n", __FUNCTION__);
+   if (!image->mt ||
+       (image->mt->tiling != I915_TILING_X &&
+       image->mt->tiling != I915_TILING_Y)) {
+      /* The algorithm is written only for X- or Y-tiled memory. */
       return false;
    }
 
-   int dst_stride = _mesa_image_row_stride(pack, texImage->Width, format, type);
-   bool dst_flip = false;
-   /* Mesa flips the dst_stride for ctx->Pack.Invert, our mt must have a
-    * normal dst_stride.
+   /* Since we are going to write raw data to the miptree, we need to resolve
+    * any pending fast color clears before we start.
     */
-   struct gl_pixelstore_attrib uninverted_pack = *pack;
-   if (ctx->Pack.Invert) {
-      dst_stride = -dst_stride;
-      dst_flip = true;
-      uninverted_pack.Invert = false;
+   intel_miptree_resolve_color(brw, image->mt);
+
+   bo = image->mt->bo;
+
+   if (drm_intel_bo_references(brw->batch.bo, bo)) {
+      perf_debug("Flushing before mapping a referenced bo.\n");
+      intel_batchbuffer_flush(brw);
    }
-   dst_offset = (GLintptr) pixels;
-   dst_offset += _mesa_image_offset(2, &uninverted_pack, texImage->Width,
-                                    texImage->Height, format, type, 0, 0, 0);
-   dst_buffer = intel_bufferobj_buffer(brw, dst, dst_offset,
-                                       texImage->Height * dst_stride);
-
-   struct intel_mipmap_tree *pbo_mt =
-      intel_miptree_create_for_bo(brw,
-                                  dst_buffer,
-                                  intelImage->mt->format,
-                                  dst_offset,
-                                  texImage->Width, texImage->Height,
-                                  dst_stride);
-
-   if (!pbo_mt)
-      return false;
 
-   if (!intel_miptree_blit(brw,
-                           intelImage->mt, texImage->Level, texImage->Face,
-                           0, 0, false,
-                           pbo_mt, 0, 0,
-                           0, 0, dst_flip,
-                           texImage->Width, texImage->Height, GL_COPY))
+   error = brw_bo_map(brw, bo, false /* write enable */, "miptree");
+   if (error) {
+      DBG("%s: failed to map bo\n", __FUNCTION__);
       return false;
+   }
 
-   intel_miptree_release(&pbo_mt);
-
+   dst_pitch = _mesa_image_row_stride(packing, width, format, type);
+
+   DBG("%s: level=%d x,y=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
+       "mesa_format=0x%x tiling=%d "
+       "packing=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d)\n",
+       __FUNCTION__, texImage->Level, xoffset, yoffset, width, height,
+       format, type, texImage->TexFormat, image->mt->tiling,
+       packing->Alignment, packing->RowLength, packing->SkipPixels,
+       packing->SkipRows);
+
+   int level = texImage->Level + texImage->TexObject->MinLevel;
+
+   /* Adjust x and y offset based on miplevel */
+   xoffset += image->mt->level[level].level_x;
+   yoffset += image->mt->level[level].level_y;
+
+   tiled_to_linear(
+      xoffset * cpp, (xoffset + width) * cpp,
+      yoffset, yoffset + height,
+      pixels - (ptrdiff_t) yoffset * dst_pitch - (ptrdiff_t) xoffset * cpp,
+      bo->virtual,
+      dst_pitch, image->mt->pitch,
+      brw->has_swizzling,
+      image->mt->tiling,
+      mem_copy
+   );
+
+   drm_intel_bo_unmap(bo);
    return true;
 }
 
@@ -511,15 +485,28 @@ static void
 intel_get_tex_image(struct gl_context *ctx,
                     GLenum format, GLenum type, GLvoid *pixels,
                     struct gl_texture_image *texImage) {
+   struct brw_context *brw = brw_context(ctx);
+   bool ok;
+
    DBG("%s\n", __FUNCTION__);
 
    if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
-      /* Using PBOs, so try the BLT based path. */
-      if (blit_texture_to_pbo(ctx, format, type, pixels, texImage))
+      if (_mesa_meta_pbo_GetTexSubImage(ctx, 3, texImage, 0, 0, 0,
+                                        texImage->Width, texImage->Height,
+                                        texImage->Depth, format, type,
+                                        pixels, &ctx->Pack))
          return;
 
+      perf_debug("%s: fallback to CPU mapping in PBO case\n", __FUNCTION__);
    }
 
+   ok = intel_gettexsubimage_tiled_memcpy(ctx, texImage, 0, 0,
+                                          texImage->Width, texImage->Height,
+                                          format, type, pixels, &ctx->Pack);
+
+   if(ok)
+      return;
+
    _mesa_meta_GetTexImage(ctx, format, type, pixels, texImage);
 
    DBG("%s - DONE\n", __FUNCTION__);