i965: Port gen7+ 3DSTATE_SOL to genxml.
[mesa.git] / src / mesa / drivers / dri / i965 / intel_tex_subimage.c
index 5604a7d8e79a59fdf69f0d6b069359abb35e50b2..912622253a4ccae70a0236e373e2e6f1d29a428e 100644 (file)
@@ -1,32 +1,30 @@
-
-/**************************************************************************
- * 
- * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
+/*
+ * Copyright 2003 VMware, Inc.
  * All Rights Reserved.
- * 
+ *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the
  * "Software"), to deal in the Software without restriction, including
  * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sub license, and/or sell copies of the Software, and to
+ * distribute, sublicense, and/or sell copies of the Software, and to
  * permit persons to whom the Software is furnished to do so, subject to
  * the following conditions:
- * 
+ *
  * The above copyright notice and this permission notice (including the
  * next paragraph) shall be included in all copies or substantial portions
  * of the Software.
- * 
+ *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
- * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- * 
- **************************************************************************/
+ */
 
 #include "main/bufferobj.h"
+#include "main/image.h"
 #include "main/macros.h"
 #include "main/mtypes.h"
 #include "main/pbo.h"
 #include "main/texstore.h"
 #include "main/texcompress.h"
 #include "main/enums.h"
+#include "drivers/common/meta.h"
 
 #include "brw_context.h"
 #include "intel_batchbuffer.h"
 #include "intel_tex.h"
 #include "intel_mipmap_tree.h"
 #include "intel_blit.h"
+#include "intel_tiled_memcpy.h"
 
 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
 
-static bool
-intel_blit_texsubimage(struct gl_context * ctx,
-                      struct gl_texture_image *texImage,
-                      GLint xoffset, GLint yoffset,
-                      GLint width, GLint height,
-                      GLenum format, GLenum type, const void *pixels,
-                      const struct gl_pixelstore_attrib *packing)
-{
-   struct brw_context *brw = brw_context(ctx);
-   struct intel_context *intel = intel_context(ctx);
-   struct intel_texture_image *intelImage = intel_texture_image(texImage);
-
-   /* Try to do a blit upload of the subimage if the texture is
-    * currently busy.
-    */
-   if (!intelImage->mt)
-      return false;
-
-   /* The blitter can't handle Y tiling */
-   if (intelImage->mt->region->tiling == I915_TILING_Y)
-      return false;
-
-   if (texImage->TexObject->Target != GL_TEXTURE_2D)
-      return false;
-
-   /* On gen6, it's probably not worth swapping to the blit ring to do
-    * this because of all the overhead involved.
-    */
-   if (intel->gen >= 6)
-      return false;
-
-   if (!drm_intel_bo_busy(intelImage->mt->region->bo))
-      return false;
-
-   DBG("BLT subimage %s target %s level %d offset %d,%d %dx%d\n",
-       __FUNCTION__,
-       _mesa_lookup_enum_by_nr(texImage->TexObject->Target),
-       texImage->Level, xoffset, yoffset, width, height);
-
-   pixels = _mesa_validate_pbo_teximage(ctx, 2, width, height, 1,
-                                       format, type, pixels, packing,
-                                       "glTexSubImage");
-   if (!pixels)
-      return false;
-
-   struct intel_mipmap_tree *temp_mt =
-      intel_miptree_create(brw, GL_TEXTURE_2D, texImage->TexFormat,
-                           0, 0,
-                           width, height, 1,
-                           false, 0, INTEL_MIPTREE_TILING_NONE);
-   if (!temp_mt)
-      goto err;
-
-   GLubyte *dst = intel_miptree_map_raw(brw, temp_mt);
-   if (!dst)
-      goto err;
-
-   if (!_mesa_texstore(ctx, 2, texImage->_BaseFormat,
-                      texImage->TexFormat,
-                      temp_mt->region->pitch,
-                      &dst,
-                      width, height, 1,
-                      format, type, pixels, packing)) {
-      _mesa_error(ctx, GL_OUT_OF_MEMORY, "intelTexSubImage");
-   }
-
-   intel_miptree_unmap_raw(brw, temp_mt);
-
-   bool ret;
-
-   ret = intel_miptree_blit(brw,
-                            temp_mt, 0, 0,
-                            0, 0, false,
-                            intelImage->mt, texImage->Level, texImage->Face,
-                            xoffset, yoffset, false,
-                            width, height, GL_COPY);
-   assert(ret);
-
-   intel_miptree_release(&temp_mt);
-   _mesa_unmap_teximage_pbo(ctx, packing);
-
-   return ret;
-
-err:
-   _mesa_error(ctx, GL_OUT_OF_MEMORY, "intelTexSubImage");
-   intel_miptree_release(&temp_mt);
-   _mesa_unmap_teximage_pbo(ctx, packing);
-   return false;
-}
-
 /**
  * \brief A fast path for glTexImage and glTexSubImage.
  *
  * \param for_glTexImage Was this called from glTexImage or glTexSubImage?
  *
- * This fast path is taken when the hardware natively supports the texture
- * format (such as GL_BGRA) and when the texture memory is X-tiled. It uploads
+ * This fast path is taken when the texture format is BGRA, RGBA,
+ * A or L and when the texture memory is X- or Y-tiled.  It uploads
  * the texture data by mapping the texture memory without a GTT fence, thus
- * acquiring a tiled view of the memory, and then memcpy'ing sucessive
- * subspans within each tile.
+ * acquiring a tiled view of the memory, and then copying sucessive
+ * spans within each tile.
  *
  * This is a performance win over the conventional texture upload path because
  * it avoids the performance penalty of writing through the write-combine
  * buffer. In the conventional texture upload path,
  * texstore.c:store_texsubimage(), the texture memory is mapped through a GTT
  * fence, thus acquiring a linear view of the memory, then each row in the
- * image is memcpy'd. In this fast path, we replace each row's memcpy with
- * a sequence of memcpy's over each bit6 swizzle span in the row.
+ * image is memcpy'd. In this fast path, we replace each row's copy with
+ * a sequence of copies over each linear span in tile.
  *
- * This fast path's use case is Google Chrome's paint rectangles.  Chrome (as
+ * One use case is Google Chrome's paint rectangles.  Chrome (as
  * of version 21) renders each page as a tiling of 256x256 GL_BGRA textures.
  * Each page's content is initially uploaded with glTexImage2D and damaged
  * regions are updated with glTexSubImage2D. On some workloads, the
@@ -171,22 +81,29 @@ intel_texsubimage_tiled_memcpy(struct gl_context * ctx,
 {
    struct brw_context *brw = brw_context(ctx);
    struct intel_texture_image *image = intel_texture_image(texImage);
+   int src_pitch;
 
    /* The miptree's buffer. */
-   drm_intel_bo *bo;
+   struct brw_bo *bo;
 
    int error = 0;
 
-   /* This fastpath is restricted to a specific texture type: level 0 of
-    * a 2D BGRA texture. It could be generalized to support more types by
-    * varying the arithmetic loop below.
+   uint32_t cpp;
+   mem_copy_fn mem_copy = NULL;
+
+   /* 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 source stride
+    * with _mesa_image_row_stride. However, before removing the restrictions
+    * we need tests.
     */
    if (!brw->has_llc ||
-       format != GL_BGRA ||
-       type != GL_UNSIGNED_BYTE ||
-       texImage->TexFormat != MESA_FORMAT_ARGB8888 ||
-       texImage->TexObject->Target != GL_TEXTURE_2D ||
-       texImage->Level != 0 ||
+       !(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 ||
@@ -198,102 +115,77 @@ intel_texsubimage_tiled_memcpy(struct gl_context * ctx,
        packing->Invert)
       return false;
 
+   /* Only a simple blit, no scale, bias or other mapping. */
+   if (ctx->_ImageTransferState)
+      return false;
+
+   if (!intel_get_memcpy(texImage->TexFormat, format, type, &mem_copy, &cpp))
+      return false;
+
+   /* If this is a nontrivial texture view, let another path handle it instead. */
+   if (texImage->TexObject->MinLayer)
+      return false;
+
    if (for_glTexImage)
       ctx->Driver.AllocTextureImageBuffer(ctx, texImage);
 
    if (!image->mt ||
-       image->mt->region->tiling != I915_TILING_X) {
-      /* The algorithm below is written only for X-tiled memory. */
+       (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;
    }
 
    /* Since we are going to write raw data to the miptree, we need to resolve
     * any pending fast color clears before we start.
     */
-   intel_miptree_resolve_color(brw, image->mt);
+   intel_miptree_all_slices_resolve_color(brw, image->mt, 0);
 
-   bo = image->mt->region->bo;
+   bo = image->mt->bo;
 
-   if (drm_intel_bo_references(brw->batch.bo, bo)) {
+   if (brw_batch_references(&brw->batch, bo)) {
       perf_debug("Flushing before mapping a referenced bo.\n");
       intel_batchbuffer_flush(brw);
    }
 
-   if (unlikely(brw->perf_debug)) {
-      if (drm_intel_bo_busy(bo)) {
-         perf_debug("Mapping a busy BO, causing a stall on the GPU.\n");
-      }
-   }
-
-   error = drm_intel_bo_map(bo, true /*write_enable*/);
+   error = brw_bo_map(brw, bo, true /* write enable */);
    if (error || bo->virtual == NULL) {
-      DBG("%s: failed to map bo\n", __FUNCTION__);
+      DBG("%s: failed to map bo\n", __func__);
       return false;
    }
 
+   src_pitch = _mesa_image_row_stride(packing, width, format, type);
+
    /* We postponed printing this message until having committed to executing
     * the function.
     */
-   DBG("%s: level=%d offset=(%d,%d) (w,h)=(%d,%d)\n",
-       __FUNCTION__, texImage->Level, xoffset, yoffset, width, height);
-
-   /* In the tiling algorithm below, some variables are in units of pixels,
-    * others are in units of bytes, and others (such as height) are unitless.
-    * Each variable name is suffixed with its units.
-    */
-
-   const uint32_t x_max_pixels = xoffset + width;
-   const uint32_t y_max_pixels = yoffset + height;
-
-   const uint32_t tile_size_bytes = 4096;
-
-   const uint32_t tile_width_bytes = 512;
-   const uint32_t tile_width_pixels = 128;
-
-   const uint32_t tile_height = 8;
-
-   const uint32_t cpp = 4; /* chars per pixel of GL_BGRA */
-   const uint32_t swizzle_width_pixels = 16;
-
-   const uint32_t stride_bytes = image->mt->region->pitch;
-   const uint32_t width_tiles = stride_bytes / tile_width_bytes;
-
-   for (uint32_t y_pixels = yoffset; y_pixels < y_max_pixels; ++y_pixels) {
-      const uint32_t y_offset_bytes = (y_pixels / tile_height) * width_tiles * tile_size_bytes
-                                    + (y_pixels % tile_height) * tile_width_bytes;
-
-      for (uint32_t x_pixels = xoffset; x_pixels < x_max_pixels; x_pixels += swizzle_width_pixels) {
-         const uint32_t x_offset_bytes = (x_pixels / tile_width_pixels) * tile_size_bytes
-                                       + (x_pixels % tile_width_pixels) * cpp;
-
-         intptr_t offset_bytes = y_offset_bytes + x_offset_bytes;
-         if (brw->has_swizzling) {
-#if 0
-            /* Clear, unoptimized version. */
-            bool bit6 = (offset_bytes >> 6) & 1;
-            bool bit9 = (offset_bytes >> 9) & 1;
-            bool bit10 = (offset_bytes >> 10) & 1;
-
-            if (bit9 ^ bit10)
-               offset_bytes ^= (1 << 6);
-#else
-            /* Optimized, obfuscated version. */
-            offset_bytes ^= ((offset_bytes >> 3) ^ (offset_bytes >> 4))
-                          & (1 << 6);
-#endif
-         }
-
-         const uint32_t swizzle_bound_pixels = ALIGN(x_pixels + 1, swizzle_width_pixels);
-         const uint32_t memcpy_bound_pixels = MIN2(x_max_pixels, swizzle_bound_pixels);
-         const uint32_t copy_size = cpp * (memcpy_bound_pixels - x_pixels);
-
-         memcpy(bo->virtual + offset_bytes, pixels, copy_size);
-         pixels += copy_size;
-         x_pixels -= (x_pixels % swizzle_width_pixels);
-      }
-   }
-
-   drm_intel_bo_unmap(bo);
+   DBG("%s: level=%d offset=(%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) "
+       "for_glTexImage=%d\n",
+       __func__, texImage->Level, xoffset, yoffset, width, height,
+       format, type, texImage->TexFormat, image->mt->tiling,
+       packing->Alignment, packing->RowLength, packing->SkipPixels,
+       packing->SkipRows, for_glTexImage);
+
+   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;
+
+   linear_to_tiled(
+      xoffset * cpp, (xoffset + width) * cpp,
+      yoffset, yoffset + height,
+      bo->virtual,
+      pixels - (ptrdiff_t) yoffset * src_pitch - (ptrdiff_t) xoffset * cpp,
+      image->mt->pitch, src_pitch,
+      brw->has_swizzling,
+      image->mt->tiling,
+      mem_copy
+   );
+
+   brw_bo_unmap(bo);
    return true;
 }
 
@@ -307,8 +199,27 @@ intelTexSubImage(struct gl_context * ctx,
                  const GLvoid * pixels,
                  const struct gl_pixelstore_attrib *packing)
 {
+   struct intel_mipmap_tree *mt = intel_texture_image(texImage)->mt;
    bool ok;
 
+   bool tex_busy = mt && brw_bo_busy(mt->bo);
+
+   if (mt && mt->format == MESA_FORMAT_S_UINT8)
+      mt->r8stencil_needs_update = true;
+
+   DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
+       __func__, _mesa_get_format_name(texImage->TexFormat),
+       _mesa_enum_to_string(texImage->TexObject->Target),
+       _mesa_enum_to_string(format), _mesa_enum_to_string(type),
+       texImage->Level, texImage->Width, texImage->Height, texImage->Depth);
+
+   ok = _mesa_meta_pbo_TexSubImage(ctx, dims, texImage,
+                                   xoffset, yoffset, zoffset,
+                                   width, height, depth, format, type,
+                                   pixels, tex_busy, packing);
+   if (ok)
+      return;
+
    ok = intel_texsubimage_tiled_memcpy(ctx, dims, texImage,
                                        xoffset, yoffset, zoffset,
                                        width, height, depth,
@@ -317,16 +228,10 @@ intelTexSubImage(struct gl_context * ctx,
    if (ok)
      return;
 
-   /* The intel_blit_texsubimage() function only handles 2D images */
-   if (dims != 2 || !intel_blit_texsubimage(ctx, texImage,
-                              xoffset, yoffset,
-                              width, height,
-                              format, type, pixels, packing)) {
-      _mesa_store_texsubimage(ctx, dims, texImage,
-                              xoffset, yoffset, zoffset,
-                              width, height, depth,
-                              format, type, pixels, packing);
-   }
+   _mesa_store_texsubimage(ctx, dims, texImage,
+                           xoffset, yoffset, zoffset,
+                           width, height, depth,
+                           format, type, pixels, packing);
 }
 
 void