intel: Refactor selection of miptree tiling
[mesa.git] / src / mesa / drivers / dri / intel / intel_mipmap_tree.c
index 1640590b962394196c7fcac307b3f9b17cc49cca..3798c152ac2290e5e9686d8a933612ce61490e86 100644 (file)
@@ -247,7 +247,7 @@ intel_miptree_create_layout(struct intel_context *intel,
    mt->physical_depth0 = depth0;
 
    if (!for_region &&
-       _mesa_is_depthstencil_format(_mesa_get_format_base_format(format)) &&
+       _mesa_get_format_base_format(format) == GL_DEPTH_STENCIL &&
        (intel->must_use_separate_stencil ||
        (intel->has_separate_stencil &&
         intel->vtbl.is_hiz_depth_format(intel, format)))) {
@@ -297,6 +297,63 @@ intel_miptree_create_layout(struct intel_context *intel,
    return mt;
 }
 
+/**
+ * \brief Helper function for intel_miptree_create().
+ */
+static uint32_t
+intel_miptree_choose_tiling(struct intel_context *intel,
+                            gl_format format,
+                            uint32_t width0,
+                            uint32_t num_samples,
+                            bool force_y_tiling,
+                            struct intel_mipmap_tree *mt)
+{
+
+   if (format == MESA_FORMAT_S8) {
+      /* The stencil buffer is W tiled. However, we request from the kernel a
+       * non-tiled buffer because the GTT is incapable of W fencing.
+       */
+      return I915_TILING_NONE;
+   }
+
+   if (!intel->use_texture_tiling || _mesa_is_format_compressed(format))
+      return I915_TILING_NONE;
+
+   if (force_y_tiling)
+      return I915_TILING_Y;
+
+   if (num_samples > 1) {
+      /* From p82 of the Sandy Bridge PRM, dw3[1] of SURFACE_STATE ("Tiled
+       * Surface"):
+       *
+       *   [DevSNB+]: For multi-sample render targets, this field must be
+       *   1. MSRTs can only be tiled.
+       *
+       * Our usual reason for preferring X tiling (fast blits using the
+       * blitting engine) doesn't apply to MSAA, since we'll generally be
+       * downsampling or upsampling when blitting between the MSAA buffer
+       * and another buffer, and the blitting engine doesn't support that.
+       * So use Y tiling, since it makes better use of the cache.
+       */
+      return I915_TILING_Y;
+   }
+
+   GLenum base_format = _mesa_get_format_base_format(format);
+   if (intel->gen >= 4 &&
+       (base_format == GL_DEPTH_COMPONENT ||
+        base_format == GL_DEPTH_STENCIL_EXT))
+      return I915_TILING_Y;
+
+   if (width0 >= 64) {
+      if (ALIGN(mt->total_width * mt->cpp, 512) < 32768)
+         return I915_TILING_X;
+
+      perf_debug("%dx%d miptree too large to blit, falling back to untiled",
+                 mt->total_width, mt->total_height);
+   }
+
+   return I915_TILING_NONE;
+}
 
 struct intel_mipmap_tree *
 intel_miptree_create(struct intel_context *intel,
@@ -312,8 +369,6 @@ intel_miptree_create(struct intel_context *intel,
                      bool force_y_tiling)
 {
    struct intel_mipmap_tree *mt;
-   uint32_t tiling = I915_TILING_NONE;
-   GLenum base_format;
    gl_format tex_format = format;
    gl_format etc_format = MESA_FORMAT_NONE;
    GLuint total_width, total_height;
@@ -352,34 +407,6 @@ intel_miptree_create(struct intel_context *intel,
    }
 
    etc_format = (format != tex_format) ? tex_format : MESA_FORMAT_NONE;
-   base_format = _mesa_get_format_base_format(format);
-
-   if (num_samples > 1) {
-      /* From p82 of the Sandy Bridge PRM, dw3[1] of SURFACE_STATE ("Tiled
-       * Surface"):
-       *
-       *   [DevSNB+]: For multi-sample render targets, this field must be
-       *   1. MSRTs can only be tiled.
-       *
-       * Our usual reason for preferring X tiling (fast blits using the
-       * blitting engine) doesn't apply to MSAA, since we'll generally be
-       * downsampling or upsampling when blitting between the MSAA buffer
-       * and another buffer, and the blitting engine doesn't support that.
-       * So use Y tiling, since it makes better use of the cache.
-       */
-      force_y_tiling = true;
-   }
-
-   if (intel->use_texture_tiling && !_mesa_is_format_compressed(format)) {
-      if (intel->gen >= 4 &&
-         (base_format == GL_DEPTH_COMPONENT ||
-          base_format == GL_DEPTH_STENCIL_EXT))
-        tiling = I915_TILING_Y;
-      else if (force_y_tiling) {
-         tiling = I915_TILING_Y;
-      } else if (width0 >= 64)
-        tiling = I915_TILING_X;
-   }
 
    mt = intel_miptree_create_layout(intel, target, format,
                                      first_level, last_level, width0,
@@ -397,15 +424,14 @@ intel_miptree_create(struct intel_context *intel,
    total_height = mt->total_height;
 
    if (format == MESA_FORMAT_S8) {
-      /* The stencil buffer is W tiled. However, we request from the kernel a
-       * non-tiled buffer because the GTT is incapable of W fencing.  So round
-       * up the width and height to match the size of W tiles (64x64).
-       */
-      tiling = I915_TILING_NONE;
+      /* Align to size of W tile, 64x64. */
       total_width = ALIGN(total_width, 64);
       total_height = ALIGN(total_height, 64);
    }
 
+   uint32_t tiling = intel_miptree_choose_tiling(intel, format, width0,
+                                                 num_samples, force_y_tiling,
+                                                 mt);
    mt->etc_format = etc_format;
    mt->region = intel_region_alloc(intel->intelScreen,
                                   tiling,
@@ -869,11 +895,16 @@ intel_miptree_copy_slice(struct intel_context *intel,
 /**
  * Copies the image's current data to the given miptree, and associates that
  * miptree with the image.
+ *
+ * If \c invalidate is true, then the actual image data does not need to be
+ * copied, but the image still needs to be associated to the new miptree (this
+ * is set to true if we're about to clear the image).
  */
 void
 intel_miptree_copy_teximage(struct intel_context *intel,
                            struct intel_texture_image *intelImage,
-                           struct intel_mipmap_tree *dst_mt)
+                           struct intel_mipmap_tree *dst_mt,
+                            bool invalidate)
 {
    struct intel_mipmap_tree *src_mt = intelImage->mt;
    struct intel_texture_object *intel_obj =
@@ -882,8 +913,10 @@ intel_miptree_copy_teximage(struct intel_context *intel,
    int face = intelImage->base.Base.Face;
    GLuint depth = intelImage->base.Base.Depth;
 
-   for (int slice = 0; slice < depth; slice++) {
-      intel_miptree_copy_slice(intel, dst_mt, src_mt, level, face, slice);
+   if (!invalidate) {
+      for (int slice = 0; slice < depth; slice++) {
+         intel_miptree_copy_slice(intel, dst_mt, src_mt, level, face, slice);
+      }
    }
 
    intel_miptree_reference(&intelImage->mt, dst_mt);
@@ -1340,9 +1373,30 @@ intel_miptree_unmap_blit(struct intel_context *intel,
                         unsigned int level,
                         unsigned int slice)
 {
-   assert(!(map->mode & GL_MAP_WRITE_BIT));
-
+   struct gl_context *ctx = &intel->ctx;
    drm_intel_bo_unmap(map->bo);
+
+   if (map->mode & GL_MAP_WRITE_BIT) {
+      unsigned int image_x, image_y;
+      int x = map->x;
+      int y = map->y;
+      intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
+      x += image_x;
+      y += image_y;
+
+      bool ok = intelEmitCopyBlit(intel,
+                                  mt->region->cpp,
+                                  map->stride, map->bo,
+                                  0, I915_TILING_NONE,
+                                  mt->region->pitch, mt->region->bo,
+                                  mt->offset, mt->region->tiling,
+                                  0, 0,
+                                  x, y,
+                                  map->w, map->h,
+                                  GL_COPY);
+      WARN_ONCE(!ok, "Failed to blit from linear temporary mapping");
+   }
+
    drm_intel_bo_unreference(map->bo);
 }
 
@@ -1675,6 +1729,23 @@ intel_miptree_map_singlesample(struct intel_context *intel,
 {
    struct intel_miptree_map *map;
 
+   /* Estimate the size of the mappable aperture into the GTT.  There's an
+    * ioctl to get the whole GTT size, but not one to get the mappable subset.
+    * It turns out it's basically always 256MB, though some ancient hardware
+    * was smaller.
+    */
+   uint32_t gtt_size = 256 * 1024 * 1024;
+   if (intel->gen == 2)
+      gtt_size = 128 * 1024 * 1024;
+
+   /* We don't want to map two objects such that a memcpy between them would
+    * just fault one mapping in and then the other over and over forever.  So
+    * we would need to divide the GTT size by 2.  Additionally, some GTT is
+    * taken up by things like the framebuffer and the ringbuffer and such, so
+    * be more conservative.
+    */
+   uint32_t max_gtt_map_object_size = gtt_size / 4;
+
    assert(mt->num_samples <= 1);
 
    map = intel_miptree_attach_map(mt, level, slice, x, y, w, h, mode);
@@ -1721,6 +1792,10 @@ intel_miptree_map_singlesample(struct intel_context *intel,
             mt->region->tiling == I915_TILING_X &&
             mt->region->pitch < 32768) {
       intel_miptree_map_blit(intel, mt, map, level, slice);
+   } else if (mt->region->tiling != I915_TILING_NONE &&
+              mt->region->bo->size >= max_gtt_map_object_size) {
+      assert(mt->region->pitch < 32768);
+      intel_miptree_map_blit(intel, mt, map, level, slice);
    } else {
       intel_miptree_map_gtt(intel, mt, map, level, slice);
    }