intel_mipmap_tree.c revision 072634da4a6ee5eafb5f5df26ca5f988209e6d40
177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt/**************************************************************************
277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt *
377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * All Rights Reserved.
577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt *
677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * Permission is hereby granted, free of charge, to any person obtaining a
777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * copy of this software and associated documentation files (the
877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * "Software"), to deal in the Software without restriction, including
977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * without limitation the rights to use, copy, modify, merge, publish,
1077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * distribute, sub license, and/or sell copies of the Software, and to
1177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * permit persons to whom the Software is furnished to do so, subject to
1277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * the following conditions:
1377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt *
1477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * The above copyright notice and this permission notice (including the
1577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * next paragraph) shall be included in all copies or substantial portions
1677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * of the Software.
1777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt *
1877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
2177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
2277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
2377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
2477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt *
2677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt **************************************************************************/
2777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
283eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace#include "intel_batchbuffer.h"
2977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt#include "intel_context.h"
3077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt#include "intel_mipmap_tree.h"
3177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt#include "intel_regions.h"
322945abea338031cbe90665df60152982bfca6177Chad Versace#include "intel_resolve_map.h"
333eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace#include "intel_span.h"
343732d0a77d2cbae50874f5a4ebdb3d8f06021a57Brian Paul#include "intel_tex_layout.h"
356dcc398ac0837025cf60b4d6a056fa3b0a16466fEric Anholt#include "intel_tex.h"
36b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt#include "intel_blit.h"
373eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace
38ecadb51bbcb972a79f3ed79e65a7986b9396e757Brian Paul#include "main/enums.h"
39c374487a54aca2dd1053645092367c1cf0414ef7Eric Anholt#include "main/formats.h"
403eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace#include "main/image.h"
41b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt#include "main/teximage.h"
4277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
4377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt#define FILE_DEBUG_FLAG DEBUG_MIPTREE
4477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
4577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtstatic GLenum
4677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholttarget_to_target(GLenum target)
4777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
4877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   switch (target) {
4977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
5077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
5177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
5277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
5377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
5477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
5577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      return GL_TEXTURE_CUBE_MAP_ARB;
5677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   default:
5777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      return target;
5877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   }
5977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
6077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
617e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace/**
627e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace * @param for_region Indicates that the caller is
637e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace *        intel_miptree_create_for_region(). If true, then do not create
647e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace *        \c stencil_mt.
657e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace */
666d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsbergstatic struct intel_mipmap_tree *
676d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsbergintel_miptree_create_internal(struct intel_context *intel,
686d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg			      GLenum target,
69d5809115b568d8b74f47316607dce0730964517aEric Anholt			      gl_format format,
705b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt			      GLuint first_level,
715b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt			      GLuint last_level,
726d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg			      GLuint width0,
736d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg			      GLuint height0,
747e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace			      GLuint depth0,
7519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry			      bool for_region,
7619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry                              GLuint num_samples)
7777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
7877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
796dcc398ac0837025cf60b4d6a056fa3b0a16466fEric Anholt   int compress_byte = 0;
8077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
815b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt   DBG("%s target %s format %s level %d..%d <-- %p\n", __FUNCTION__,
8277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt       _mesa_lookup_enum_by_nr(target),
839a523a48af05118424714f0a34ca3dda6861186aEric Anholt       _mesa_get_format_name(format),
845b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt       first_level, last_level, mt);
8577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
866dcc398ac0837025cf60b4d6a056fa3b0a16466fEric Anholt   if (_mesa_is_format_compressed(format))
876dcc398ac0837025cf60b4d6a056fa3b0a16466fEric Anholt      compress_byte = intel_compressed_num_bytes(format);
886dcc398ac0837025cf60b4d6a056fa3b0a16466fEric Anholt
8977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->target = target_to_target(target);
90d5809115b568d8b74f47316607dce0730964517aEric Anholt   mt->format = format;
915b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt   mt->first_level = first_level;
925b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt   mt->last_level = last_level;
9377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->width0 = width0;
9477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->height0 = height0;
95f94fef83db10f0c9327bd3dd43510ad31c94d82aEric Anholt   mt->cpp = compress_byte ? compress_byte : _mesa_get_format_bytes(mt->format);
9619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   mt->num_samples = num_samples;
9777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->compressed = compress_byte ? 1 : 0;
9877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->refcount = 1;
9977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
100d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   if (target == GL_TEXTURE_CUBE_MAP) {
101d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      assert(depth0 == 1);
102d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      mt->depth0 = 6;
103d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   } else {
104d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      mt->depth0 = depth0;
105d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   }
106d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace
1077e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace   if (!for_region &&
1087e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace       _mesa_is_depthstencil_format(_mesa_get_format_base_format(format)) &&
1095c9a55665d78b96bfb8ce8eab43b5558dd656a6dEric Anholt       (intel->must_use_separate_stencil ||
1105c9a55665d78b96bfb8ce8eab43b5558dd656a6dEric Anholt	(intel->has_separate_stencil &&
1115c9a55665d78b96bfb8ce8eab43b5558dd656a6dEric Anholt	 intel->vtbl.is_hiz_depth_format(intel, format)))) {
1123eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace      mt->stencil_mt = intel_miptree_create(intel,
1133eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace                                            mt->target,
1143eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace                                            MESA_FORMAT_S8,
1153eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace                                            mt->first_level,
1163eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace                                            mt->last_level,
1173eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace                                            mt->width0,
1183eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace                                            mt->height0,
1193eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace                                            mt->depth0,
12019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry                                            true,
12119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry                                            num_samples);
1223eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace      if (!mt->stencil_mt) {
1233eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace	 intel_miptree_release(&mt);
1243eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace	 return NULL;
1253eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace      }
126fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt
127fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt      /* Fix up the Z miptree format for how we're splitting out separate
128fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt       * stencil.  Gen7 expects there to be no stencil bits in its depth buffer.
129fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt       */
130fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt      if (mt->format == MESA_FORMAT_S8_Z24) {
131fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt	 mt->format = MESA_FORMAT_X8_Z24;
1324790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt      } else if (mt->format == MESA_FORMAT_Z32_FLOAT_X24S8) {
1334790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt	 mt->format = MESA_FORMAT_Z32_FLOAT;
1344790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt	 mt->cpp = 4;
135fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt      } else {
1364790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt	 _mesa_problem(NULL, "Unknown format %s in separate stencil mt\n",
137fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt		       _mesa_get_format_name(mt->format));
138fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt      }
1393eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace   }
1403eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace
1414790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt   intel_get_texture_alignment_unit(intel, mt->format,
1424790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt				    &mt->align_w, &mt->align_h);
1434790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt
1444790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt#ifdef I915
1454790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt   (void) intel;
1464790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt   if (intel->is_945)
1474790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt      i945_miptree_layout(mt);
1484790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt   else
1494790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt      i915_miptree_layout(mt);
1504790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt#else
1514790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt   brw_miptree_layout(intel, mt);
1524790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt#endif
1534790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt
1546d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg   return mt;
1556d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg}
1566d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg
15747a7535f413d6467082de224f64eecc046227406Brian Paul
1586d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsbergstruct intel_mipmap_tree *
1596d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsbergintel_miptree_create(struct intel_context *intel,
1606d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg		     GLenum target,
161d5809115b568d8b74f47316607dce0730964517aEric Anholt		     gl_format format,
1625b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt		     GLuint first_level,
1635b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt		     GLuint last_level,
1646d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg		     GLuint width0,
1656d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg		     GLuint height0,
166f94fef83db10f0c9327bd3dd43510ad31c94d82aEric Anholt		     GLuint depth0,
16719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry		     bool expect_accelerated_upload,
16819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry                     GLuint num_samples)
1696d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg{
1706d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg   struct intel_mipmap_tree *mt;
171453f0b1f243b26e02b371293e9c8bbf09362c14aEric Anholt   uint32_t tiling = I915_TILING_NONE;
1729c5fdbb721147f7304faaa8960f5b64e25a8f673Eric Anholt   GLenum base_format = _mesa_get_format_base_format(format);
1731ba96651e12b3c74fb9c8f5a61b183ef36a27b1eEric Anholt
1746dcc398ac0837025cf60b4d6a056fa3b0a16466fEric Anholt   if (intel->use_texture_tiling && !_mesa_is_format_compressed(format)) {
175caf3038123d6d29afd7d1f0cd6db98a2282c3ca1Eric Anholt      if (intel->gen >= 4 &&
1769c0ba017c8ff7caafc3ff94da3c035e687231596Eric Anholt	  (base_format == GL_DEPTH_COMPONENT ||
1779c0ba017c8ff7caafc3ff94da3c035e687231596Eric Anholt	   base_format == GL_DEPTH_STENCIL_EXT))
178bd10f0e84f1491363d76d92dcbd410ab5cc43dbeEric Anholt	 tiling = I915_TILING_Y;
17919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      else if (num_samples > 0) {
18019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry         /* From p82 of the Sandy Bridge PRM, dw3[1] of SURFACE_STATE ("Tiled
18119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          * Surface"):
18219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          *
18319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          *   [DevSNB+]: For multi-sample render targets, this field must be
18419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          *   1. MSRTs can only be tiled.
18519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          *
18619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          * Our usual reason for preferring X tiling (fast blits using the
18719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          * blitting engine) doesn't apply to MSAA, since we'll generally be
18819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          * downsampling or upsampling when blitting between the MSAA buffer
18919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          * and another buffer, and the blitting engine doesn't support that.
19019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          * So use Y tiling, since it makes better use of the cache.
19119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          */
19219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry         tiling = I915_TILING_Y;
19319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      } else if (width0 >= 64)
194bd10f0e84f1491363d76d92dcbd410ab5cc43dbeEric Anholt	 tiling = I915_TILING_X;
195453f0b1f243b26e02b371293e9c8bbf09362c14aEric Anholt   }
1966d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg
197f22068d5be7c829d3768154845ef3c5a2986fed4Eric Anholt   if (format == MESA_FORMAT_S8) {
198f22068d5be7c829d3768154845ef3c5a2986fed4Eric Anholt      /* The stencil buffer is W tiled. However, we request from the kernel a
199a27c7d80afc3160a0face4b8781bf921229bc3ccPaul Berry       * non-tiled buffer because the GTT is incapable of W fencing.  So round
200a27c7d80afc3160a0face4b8781bf921229bc3ccPaul Berry       * up the width and height to match the size of W tiles (64x64).
201f22068d5be7c829d3768154845ef3c5a2986fed4Eric Anholt       */
202f22068d5be7c829d3768154845ef3c5a2986fed4Eric Anholt      tiling = I915_TILING_NONE;
203f22068d5be7c829d3768154845ef3c5a2986fed4Eric Anholt      width0 = ALIGN(width0, 64);
204a27c7d80afc3160a0face4b8781bf921229bc3ccPaul Berry      height0 = ALIGN(height0, 64);
205f22068d5be7c829d3768154845ef3c5a2986fed4Eric Anholt   }
206f22068d5be7c829d3768154845ef3c5a2986fed4Eric Anholt
2079a523a48af05118424714f0a34ca3dda6861186aEric Anholt   mt = intel_miptree_create_internal(intel, target, format,
2085b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt				      first_level, last_level, width0,
2097e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace				      height0, depth0,
21019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry				      false, num_samples);
2117ed1fd5d8438e55fe24091844cdfccb0881306bcXiang, Haihao   /*
2128b69c42b356d51c3a37bc0af41738b016c2adc5bXiang, Haihao    * pitch == 0 || height == 0  indicates the null texture
2137ed1fd5d8438e55fe24091844cdfccb0881306bcXiang, Haihao    */
2141a662e7c18cab98f1b122f6766faf338725de673Yuanhan Liu   if (!mt || !mt->total_width || !mt->total_height) {
215747f0307626ef5bcf2f889ab66bcc95ab8eda2c8Chad Versace      intel_miptree_release(&mt);
2166d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg      return NULL;
217a7d0665c00053839065592c339bfb298187a82beVinson Lee   }
2186d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg
2199087ba128089ed0dc00e6eb38f37126fb7557d3bKristian Høgsberg   mt->region = intel_region_alloc(intel->intelScreen,
2201ba96651e12b3c74fb9c8f5a61b183ef36a27b1eEric Anholt				   tiling,
2218db761409dadc2e899d4e7107eff3aa07b07aa11Eric Anholt				   mt->cpp,
222362c1bf75eb74de5b4655c481b74f79718ed4a34Eric Anholt				   mt->total_width,
2238db761409dadc2e899d4e7107eff3aa07b07aa11Eric Anholt				   mt->total_height,
22440dd024be618d805b3744e15d25e115018641324Eric Anholt				   expect_accelerated_upload);
2256d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg
22677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   if (!mt->region) {
227747f0307626ef5bcf2f889ab66bcc95ab8eda2c8Chad Versace       intel_miptree_release(&mt);
2286d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg       return NULL;
2296d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg   }
2306d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg
2316d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg   return mt;
2326d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg}
2336d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg
23447a7535f413d6467082de224f64eecc046227406Brian Paul
2356d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsbergstruct intel_mipmap_tree *
2366d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsbergintel_miptree_create_for_region(struct intel_context *intel,
2376d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg				GLenum target,
238d5809115b568d8b74f47316607dce0730964517aEric Anholt				gl_format format,
2396f23d9b637602d9997896c6ca5cba0dfe36a092eIan Romanick				struct intel_region *region)
2406d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg{
2416d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg   struct intel_mipmap_tree *mt;
2426d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg
2439a523a48af05118424714f0a34ca3dda6861186aEric Anholt   mt = intel_miptree_create_internal(intel, target, format,
2445b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt				      0, 0,
2457e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace				      region->width, region->height, 1,
24619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry				      true, 0 /* num_samples */);
2476d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg   if (!mt)
2486d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg      return mt;
249da011faf48155a5c02ebc1fe1fa20a4f54b8c657Eric Anholt
250e72a44215312ae1f3c812ba28e47b4aec3589de9Pierre Willenbrock   intel_region_reference(&mt->region, region);
25177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
25277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   return mt;
25347a7535f413d6467082de224f64eecc046227406Brian Paul}
25447a7535f413d6467082de224f64eecc046227406Brian Paul
255005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versacestruct intel_mipmap_tree*
256005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versaceintel_miptree_create_for_renderbuffer(struct intel_context *intel,
257005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versace                                      gl_format format,
258005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versace                                      uint32_t width,
25919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry                                      uint32_t height,
26019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry                                      uint32_t num_samples)
261005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versace{
262005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versace   struct intel_mipmap_tree *mt;
263005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versace
26419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* Adjust width/height for MSAA */
26519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   if (num_samples > 4) {
26619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      num_samples = 8;
26719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      width *= 4;
26819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      height *= 2;
26919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   } else if (num_samples > 0) {
27019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      num_samples = 4;
27119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      width *= 2;
27219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      height *= 2;
27319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
27419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
275f22068d5be7c829d3768154845ef3c5a2986fed4Eric Anholt   mt = intel_miptree_create(intel, GL_TEXTURE_2D, format, 0, 0,
27619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry			     width, height, 1, true, num_samples);
277005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versace
278005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versace   return mt;
279005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versace}
280005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versace
28177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtvoid
28277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtintel_miptree_reference(struct intel_mipmap_tree **dst,
28377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt                        struct intel_mipmap_tree *src)
28477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
28532fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt   if (*dst == src)
28632fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt      return;
28732fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt
28832fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt   intel_miptree_release(dst);
28932fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt
29032fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt   if (src) {
29132fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt      src->refcount++;
29232fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt      DBG("%s %p refcount now %d\n", __FUNCTION__, src, src->refcount);
29332fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt   }
29432fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt
29577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   *dst = src;
29677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
29777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
29847a7535f413d6467082de224f64eecc046227406Brian Paul
29977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtvoid
300db3ada6055814a4bd5aa95fc9505fc101864391dEric Anholtintel_miptree_release(struct intel_mipmap_tree **mt)
30177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
30277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   if (!*mt)
30377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      return;
30477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
30577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   DBG("%s %p refcount will be %d\n", __FUNCTION__, *mt, (*mt)->refcount - 1);
30677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   if (--(*mt)->refcount <= 0) {
30777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      GLuint i;
30877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
30977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      DBG("%s deleting %p\n", __FUNCTION__, *mt);
31077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
31177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      intel_region_release(&((*mt)->region));
3123eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace      intel_miptree_release(&(*mt)->stencil_mt);
313a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace      intel_miptree_release(&(*mt)->hiz_mt);
3142945abea338031cbe90665df60152982bfca6177Chad Versace      intel_resolve_map_clear(&(*mt)->hiz_map);
31577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
3162d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt      for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
317c2ddde70daece24d1eb7185946032158993a9e4dChad Versace	 free((*mt)->level[i].slice);
3182d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt      }
31977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
32077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      free(*mt);
32177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   }
32277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   *mt = NULL;
32377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
32477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
325fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholtvoid
326fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholtintel_miptree_get_dimensions_for_image(struct gl_texture_image *image,
327fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt                                       int *width, int *height, int *depth)
328fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt{
329fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt   switch (image->TexObject->Target) {
330fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt   case GL_TEXTURE_1D_ARRAY:
331fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt      *width = image->Width;
332fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt      *height = 1;
333fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt      *depth = image->Height;
334fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt      break;
335fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt   default:
336fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt      *width = image->Width;
337fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt      *height = image->Height;
338fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt      *depth = image->Depth;
339fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt      break;
340fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt   }
341fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt}
34277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
34347a7535f413d6467082de224f64eecc046227406Brian Paul/**
34447a7535f413d6467082de224f64eecc046227406Brian Paul * Can the image be pulled into a unified mipmap tree?  This mirrors
34577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * the completeness test in a lot of ways.
34677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt *
34777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * Not sure whether I want to pass gl_texture_image here.
34877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt */
3492e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunkebool
35077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtintel_miptree_match_image(struct intel_mipmap_tree *mt,
3518f30ceaaefc33401b08739a16ce1c5638d6432faEric Anholt                          struct gl_texture_image *image)
35277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
3538f30ceaaefc33401b08739a16ce1c5638d6432faEric Anholt   struct intel_texture_image *intelImage = intel_texture_image(image);
354fa2c886863492cc3eeee6d2059ae24edc1cb2bffBrian Paul   GLuint level = intelImage->base.Base.Level;
355fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt   int width, height, depth;
356b6bdafdf2cf1110b4a5ca7cf9e1c3dcb124b800fBrian Paul
357ab7794cada02f3b3b5e3a642c20eeedeb17b65a6Eric Anholt   if (target_to_target(image->TexObject->Target) != mt->target)
358ab7794cada02f3b3b5e3a642c20eeedeb17b65a6Eric Anholt      return false;
359ab7794cada02f3b3b5e3a642c20eeedeb17b65a6Eric Anholt
360fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt   if (image->TexFormat != mt->format &&
361fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt       !(image->TexFormat == MESA_FORMAT_S8_Z24 &&
362fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt	 mt->format == MESA_FORMAT_X8_Z24 &&
363fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt	 mt->stencil_mt)) {
3642e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunke      return false;
365fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt   }
366d12fa3511da23d8285f3ea1a51a1f328cdbb1462Xiang, Haihao
367fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt   intel_miptree_get_dimensions_for_image(image, &width, &height, &depth);
368fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt
3697f854a5028ef00a0e041e78a770456204dc58eefEric Anholt   if (mt->target == GL_TEXTURE_CUBE_MAP)
3707f854a5028ef00a0e041e78a770456204dc58eefEric Anholt      depth = 6;
3717f854a5028ef00a0e041e78a770456204dc58eefEric Anholt
37277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   /* Test image dimensions against the base level image adjusted for
37377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt    * minification.  This will also catch images not present in the
37477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt    * tree, changed targets, etc.
37577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt    */
376fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt   if (width != mt->level[level].width ||
377fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt       height != mt->level[level].height ||
378fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt       depth != mt->level[level].depth)
3792e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunke      return false;
38077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
3812e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunke   return true;
38277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
38377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
38477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
38577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtvoid
38677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtintel_miptree_set_level_info(struct intel_mipmap_tree *mt,
387659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			     GLuint level,
388659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			     GLuint x, GLuint y,
389659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			     GLuint w, GLuint h, GLuint d)
39077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
39177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->level[level].width = w;
39277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->level[level].height = h;
39377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->level[level].depth = d;
3942d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt   mt->level[level].level_x = x;
3952d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt   mt->level[level].level_y = y;
39677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
3970c51390e4b5e04b992e50fcbed751024e6c329deEric Anholt   DBG("%s level %d size: %d,%d,%d offset %d,%d\n", __FUNCTION__,
3980c51390e4b5e04b992e50fcbed751024e6c329deEric Anholt       level, w, h, d, x, y);
39977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
400c2ddde70daece24d1eb7185946032158993a9e4dChad Versace   assert(mt->level[level].slice == NULL);
40177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
402221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt   mt->level[level].slice = calloc(d, sizeof(*mt->level[0].slice));
403c2ddde70daece24d1eb7185946032158993a9e4dChad Versace   mt->level[level].slice[0].x_offset = mt->level[level].level_x;
404c2ddde70daece24d1eb7185946032158993a9e4dChad Versace   mt->level[level].slice[0].y_offset = mt->level[level].level_y;
40577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
40677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
40777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
40877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtvoid
4092d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholtintel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
4102d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt			       GLuint level, GLuint img,
4112d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt			       GLuint x, GLuint y)
41277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
41377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   if (img == 0 && level == 0)
41477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      assert(x == 0 && y == 0);
41577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
416d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   assert(img < mt->level[level].depth);
41777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
418c2ddde70daece24d1eb7185946032158993a9e4dChad Versace   mt->level[level].slice[img].x_offset = mt->level[level].level_x + x;
419c2ddde70daece24d1eb7185946032158993a9e4dChad Versace   mt->level[level].slice[img].y_offset = mt->level[level].level_y + y;
42077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
4212d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt   DBG("%s level %d img %d pos %d,%d\n",
4222d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt       __FUNCTION__, level, img,
423c2ddde70daece24d1eb7185946032158993a9e4dChad Versace       mt->level[level].slice[img].x_offset,
424c2ddde70daece24d1eb7185946032158993a9e4dChad Versace       mt->level[level].slice[img].y_offset);
42577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
42677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
42747a7535f413d6467082de224f64eecc046227406Brian Paul
428d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace/**
429d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace * For cube map textures, either the \c face parameter can be used, of course,
430d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace * or the cube face can be interpreted as a depth layer and the \c layer
431d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace * parameter used.
432d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace */
4332995bf0d68f1b28ba68b81e9dc79e3ab52bc2795Xiang, Haihaovoid
4342d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholtintel_miptree_get_image_offset(struct intel_mipmap_tree *mt,
435d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace			       GLuint level, GLuint face, GLuint layer,
4362d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt			       GLuint *x, GLuint *y)
43777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
438d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   int slice;
439d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace
440d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   if (face > 0) {
441d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      assert(mt->target == GL_TEXTURE_CUBE_MAP);
442d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      assert(face < 6);
443d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      assert(layer == 0);
444d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      slice = face;
445d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   } else {
446d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      /* This branch may be taken even if the texture target is a cube map. In
447d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace       * that case, the caller chose to interpret each cube face as a layer.
448d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace       */
449d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      assert(face == 0);
450d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      slice = layer;
4512d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt   }
452d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace
453d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   *x = mt->level[level].slice[slice].x_offset;
454d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   *y = mt->level[level].slice[slice].y_offset;
45577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
45677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
457278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versacestatic void
458278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versaceintel_miptree_copy_slice(struct intel_context *intel,
459278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			 struct intel_mipmap_tree *dst_mt,
460278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			 struct intel_mipmap_tree *src_mt,
461278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			 int level,
462278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			 int face,
463278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			 int depth)
464278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
465278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace{
466278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   gl_format format = src_mt->format;
467278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   uint32_t width = src_mt->level[level].width;
468278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   uint32_t height = src_mt->level[level].height;
469278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
470278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   assert(depth < src_mt->level[level].depth);
471278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
472278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   if (dst_mt->compressed) {
473017c13d55b5b086774d6afea2ca754482c624c6aChad Versace      height = ALIGN(height, dst_mt->align_h) / dst_mt->align_h;
474017c13d55b5b086774d6afea2ca754482c624c6aChad Versace      width = ALIGN(width, dst_mt->align_w);
475278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   }
476278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
477278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   uint32_t dst_x, dst_y, src_x, src_y;
478278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   intel_miptree_get_image_offset(dst_mt, level, face, depth,
479278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace				  &dst_x, &dst_y);
480278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   intel_miptree_get_image_offset(src_mt, level, face, depth,
481278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace				  &src_x, &src_y);
482278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
483278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   DBG("validate blit mt %p %d,%d/%d -> mt %p %d,%d/%d (%dx%d)\n",
484278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace       src_mt, src_x, src_y, src_mt->region->pitch * src_mt->region->cpp,
485278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace       dst_mt, dst_x, dst_y, dst_mt->region->pitch * dst_mt->region->cpp,
486278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace       width, height);
487278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
488278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   if (!intelEmitCopyBlit(intel,
489278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			  dst_mt->region->cpp,
490278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			  src_mt->region->pitch, src_mt->region->bo,
491278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			  0, src_mt->region->tiling,
492278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			  dst_mt->region->pitch, dst_mt->region->bo,
493278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			  0, dst_mt->region->tiling,
494278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			  src_x, src_y,
495278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			  dst_x, dst_y,
496278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			  width, height,
497278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			  GL_COPY)) {
498278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
499278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace      fallback_debug("miptree validate blit for %s failed\n",
500278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace		     _mesa_get_format_name(format));
501278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace      void *dst = intel_region_map(intel, dst_mt->region, GL_MAP_WRITE_BIT);
502278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace      void *src = intel_region_map(intel, src_mt->region, GL_MAP_READ_BIT);
503278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
504278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace      _mesa_copy_rect(dst,
505278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace		      dst_mt->cpp,
506278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace		      dst_mt->region->pitch,
507278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace		      dst_x, dst_y,
508278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace		      width, height,
509278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace		      src, src_mt->region->pitch,
510278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace		      src_x, src_y);
511278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
512278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace      intel_region_unmap(intel, dst_mt->region);
513278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace      intel_region_unmap(intel, src_mt->region);
514278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   }
5153eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace
5163eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace   if (src_mt->stencil_mt) {
5173eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace      intel_miptree_copy_slice(intel,
5183eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace                               dst_mt->stencil_mt, src_mt->stencil_mt,
5193eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace                               level, face, depth);
5203eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace   }
521278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace}
522278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
52377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt/**
524b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt * Copies the image's current data to the given miptree, and associates that
525b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt * miptree with the image.
52677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt */
52777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtvoid
528b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholtintel_miptree_copy_teximage(struct intel_context *intel,
529b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt			    struct intel_texture_image *intelImage,
530b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt			    struct intel_mipmap_tree *dst_mt)
53177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
532b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt   struct intel_mipmap_tree *src_mt = intelImage->mt;
533b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt   int level = intelImage->base.Base.Level;
534b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt   int face = intelImage->base.Base.Face;
535d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   GLuint depth = intelImage->base.Base.Depth;
536b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt
537278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   for (int slice = 0; slice < depth; slice++) {
538278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace      intel_miptree_copy_slice(intel, dst_mt, src_mt, level, face, slice);
53977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   }
54077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
541b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt   intel_miptree_reference(&intelImage->mt, dst_mt);
54277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
543278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
544a2e44b0813e956440c451c107cf5564b56cbe98eChad Versacebool
545a2e44b0813e956440c451c107cf5564b56cbe98eChad Versaceintel_miptree_alloc_hiz(struct intel_context *intel,
54619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry			struct intel_mipmap_tree *mt,
54719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry                        GLuint num_samples)
548a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace{
549a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace   assert(mt->hiz_mt == NULL);
550a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace   mt->hiz_mt = intel_miptree_create(intel,
551a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace                                     mt->target,
552a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace                                     MESA_FORMAT_X8_Z24,
553a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace                                     mt->first_level,
554a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace                                     mt->last_level,
555a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace                                     mt->width0,
556a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace                                     mt->height0,
557a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace                                     mt->depth0,
55819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry                                     true,
55919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry                                     num_samples);
5603d760664e6349c72624aa6d54d40df0233995c8eChad Versace
5613d760664e6349c72624aa6d54d40df0233995c8eChad Versace   if (!mt->hiz_mt)
5623d760664e6349c72624aa6d54d40df0233995c8eChad Versace      return false;
5633d760664e6349c72624aa6d54d40df0233995c8eChad Versace
5643d760664e6349c72624aa6d54d40df0233995c8eChad Versace   /* Mark that all slices need a HiZ resolve. */
5653d760664e6349c72624aa6d54d40df0233995c8eChad Versace   struct intel_resolve_map *head = &mt->hiz_map;
5663d760664e6349c72624aa6d54d40df0233995c8eChad Versace   for (int level = mt->first_level; level <= mt->last_level; ++level) {
5673d760664e6349c72624aa6d54d40df0233995c8eChad Versace      for (int layer = 0; layer < mt->level[level].depth; ++layer) {
5683d760664e6349c72624aa6d54d40df0233995c8eChad Versace	 head->next = malloc(sizeof(*head->next));
5693d760664e6349c72624aa6d54d40df0233995c8eChad Versace	 head->next->prev = head;
5703d760664e6349c72624aa6d54d40df0233995c8eChad Versace	 head->next->next = NULL;
5713d760664e6349c72624aa6d54d40df0233995c8eChad Versace	 head = head->next;
5723d760664e6349c72624aa6d54d40df0233995c8eChad Versace
5733d760664e6349c72624aa6d54d40df0233995c8eChad Versace	 head->level = level;
5743d760664e6349c72624aa6d54d40df0233995c8eChad Versace	 head->layer = layer;
575072634da4a6ee5eafb5f5df26ca5f988209e6d40Eric Anholt	 head->need = GEN6_HIZ_OP_HIZ_RESOLVE;
5763d760664e6349c72624aa6d54d40df0233995c8eChad Versace      }
5773d760664e6349c72624aa6d54d40df0233995c8eChad Versace   }
5783d760664e6349c72624aa6d54d40df0233995c8eChad Versace
5793d760664e6349c72624aa6d54d40df0233995c8eChad Versace   return true;
580a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace}
5812945abea338031cbe90665df60152982bfca6177Chad Versace
5822945abea338031cbe90665df60152982bfca6177Chad Versacevoid
5832945abea338031cbe90665df60152982bfca6177Chad Versaceintel_miptree_slice_set_needs_hiz_resolve(struct intel_mipmap_tree *mt,
5842945abea338031cbe90665df60152982bfca6177Chad Versace					  uint32_t level,
5852945abea338031cbe90665df60152982bfca6177Chad Versace					  uint32_t layer)
5862945abea338031cbe90665df60152982bfca6177Chad Versace{
5872945abea338031cbe90665df60152982bfca6177Chad Versace   intel_miptree_check_level_layer(mt, level, layer);
5882945abea338031cbe90665df60152982bfca6177Chad Versace
5892945abea338031cbe90665df60152982bfca6177Chad Versace   if (!mt->hiz_mt)
5902945abea338031cbe90665df60152982bfca6177Chad Versace      return;
5912945abea338031cbe90665df60152982bfca6177Chad Versace
5922945abea338031cbe90665df60152982bfca6177Chad Versace   intel_resolve_map_set(&mt->hiz_map,
593072634da4a6ee5eafb5f5df26ca5f988209e6d40Eric Anholt			 level, layer, GEN6_HIZ_OP_HIZ_RESOLVE);
5942945abea338031cbe90665df60152982bfca6177Chad Versace}
5952945abea338031cbe90665df60152982bfca6177Chad Versace
5962945abea338031cbe90665df60152982bfca6177Chad Versace
5972945abea338031cbe90665df60152982bfca6177Chad Versacevoid
5982945abea338031cbe90665df60152982bfca6177Chad Versaceintel_miptree_slice_set_needs_depth_resolve(struct intel_mipmap_tree *mt,
5992945abea338031cbe90665df60152982bfca6177Chad Versace                                            uint32_t level,
6002945abea338031cbe90665df60152982bfca6177Chad Versace                                            uint32_t layer)
6012945abea338031cbe90665df60152982bfca6177Chad Versace{
6022945abea338031cbe90665df60152982bfca6177Chad Versace   intel_miptree_check_level_layer(mt, level, layer);
6032945abea338031cbe90665df60152982bfca6177Chad Versace
6042945abea338031cbe90665df60152982bfca6177Chad Versace   if (!mt->hiz_mt)
6052945abea338031cbe90665df60152982bfca6177Chad Versace      return;
6062945abea338031cbe90665df60152982bfca6177Chad Versace
6072945abea338031cbe90665df60152982bfca6177Chad Versace   intel_resolve_map_set(&mt->hiz_map,
608072634da4a6ee5eafb5f5df26ca5f988209e6d40Eric Anholt			 level, layer, GEN6_HIZ_OP_DEPTH_RESOLVE);
6092945abea338031cbe90665df60152982bfca6177Chad Versace}
6102945abea338031cbe90665df60152982bfca6177Chad Versace
6112945abea338031cbe90665df60152982bfca6177Chad Versacetypedef void (*resolve_func_t)(struct intel_context *intel,
6122945abea338031cbe90665df60152982bfca6177Chad Versace			       struct intel_mipmap_tree *mt,
6132945abea338031cbe90665df60152982bfca6177Chad Versace			       uint32_t level,
6142945abea338031cbe90665df60152982bfca6177Chad Versace			       uint32_t layer);
6152945abea338031cbe90665df60152982bfca6177Chad Versace
6162945abea338031cbe90665df60152982bfca6177Chad Versacestatic bool
6172945abea338031cbe90665df60152982bfca6177Chad Versaceintel_miptree_slice_resolve(struct intel_context *intel,
6182945abea338031cbe90665df60152982bfca6177Chad Versace			    struct intel_mipmap_tree *mt,
6192945abea338031cbe90665df60152982bfca6177Chad Versace			    uint32_t level,
6202945abea338031cbe90665df60152982bfca6177Chad Versace			    uint32_t layer,
621072634da4a6ee5eafb5f5df26ca5f988209e6d40Eric Anholt			    enum gen6_hiz_op need,
6222945abea338031cbe90665df60152982bfca6177Chad Versace			    resolve_func_t func)
6232945abea338031cbe90665df60152982bfca6177Chad Versace{
6242945abea338031cbe90665df60152982bfca6177Chad Versace   intel_miptree_check_level_layer(mt, level, layer);
6252945abea338031cbe90665df60152982bfca6177Chad Versace
6262945abea338031cbe90665df60152982bfca6177Chad Versace   struct intel_resolve_map *item =
6272945abea338031cbe90665df60152982bfca6177Chad Versace	 intel_resolve_map_get(&mt->hiz_map, level, layer);
6282945abea338031cbe90665df60152982bfca6177Chad Versace
6292945abea338031cbe90665df60152982bfca6177Chad Versace   if (!item || item->need != need)
6302945abea338031cbe90665df60152982bfca6177Chad Versace      return false;
6312945abea338031cbe90665df60152982bfca6177Chad Versace
6322945abea338031cbe90665df60152982bfca6177Chad Versace   func(intel, mt, level, layer);
6332945abea338031cbe90665df60152982bfca6177Chad Versace   intel_resolve_map_remove(item);
6342945abea338031cbe90665df60152982bfca6177Chad Versace   return true;
6352945abea338031cbe90665df60152982bfca6177Chad Versace}
6362945abea338031cbe90665df60152982bfca6177Chad Versace
6372945abea338031cbe90665df60152982bfca6177Chad Versacebool
6382945abea338031cbe90665df60152982bfca6177Chad Versaceintel_miptree_slice_resolve_hiz(struct intel_context *intel,
6392945abea338031cbe90665df60152982bfca6177Chad Versace				struct intel_mipmap_tree *mt,
6402945abea338031cbe90665df60152982bfca6177Chad Versace				uint32_t level,
6412945abea338031cbe90665df60152982bfca6177Chad Versace				uint32_t layer)
6422945abea338031cbe90665df60152982bfca6177Chad Versace{
6432945abea338031cbe90665df60152982bfca6177Chad Versace   return intel_miptree_slice_resolve(intel, mt, level, layer,
644072634da4a6ee5eafb5f5df26ca5f988209e6d40Eric Anholt				      GEN6_HIZ_OP_HIZ_RESOLVE,
6452945abea338031cbe90665df60152982bfca6177Chad Versace				      intel->vtbl.resolve_hiz_slice);
6462945abea338031cbe90665df60152982bfca6177Chad Versace}
6472945abea338031cbe90665df60152982bfca6177Chad Versace
6482945abea338031cbe90665df60152982bfca6177Chad Versacebool
6492945abea338031cbe90665df60152982bfca6177Chad Versaceintel_miptree_slice_resolve_depth(struct intel_context *intel,
6502945abea338031cbe90665df60152982bfca6177Chad Versace				  struct intel_mipmap_tree *mt,
6512945abea338031cbe90665df60152982bfca6177Chad Versace				  uint32_t level,
6522945abea338031cbe90665df60152982bfca6177Chad Versace				  uint32_t layer)
6532945abea338031cbe90665df60152982bfca6177Chad Versace{
6542945abea338031cbe90665df60152982bfca6177Chad Versace   return intel_miptree_slice_resolve(intel, mt, level, layer,
655072634da4a6ee5eafb5f5df26ca5f988209e6d40Eric Anholt				      GEN6_HIZ_OP_DEPTH_RESOLVE,
6562945abea338031cbe90665df60152982bfca6177Chad Versace				      intel->vtbl.resolve_depth_slice);
6572945abea338031cbe90665df60152982bfca6177Chad Versace}
6582945abea338031cbe90665df60152982bfca6177Chad Versace
6592945abea338031cbe90665df60152982bfca6177Chad Versacestatic bool
6602945abea338031cbe90665df60152982bfca6177Chad Versaceintel_miptree_all_slices_resolve(struct intel_context *intel,
6612945abea338031cbe90665df60152982bfca6177Chad Versace				 struct intel_mipmap_tree *mt,
662072634da4a6ee5eafb5f5df26ca5f988209e6d40Eric Anholt				 enum gen6_hiz_op need,
6632945abea338031cbe90665df60152982bfca6177Chad Versace				 resolve_func_t func)
6642945abea338031cbe90665df60152982bfca6177Chad Versace{
6652945abea338031cbe90665df60152982bfca6177Chad Versace   bool did_resolve = false;
6660ed11e333147e280208d9d0b3ff3f39970547643Anuj Phogat   struct intel_resolve_map *i, *next;
6672945abea338031cbe90665df60152982bfca6177Chad Versace
6680ed11e333147e280208d9d0b3ff3f39970547643Anuj Phogat   for (i = mt->hiz_map.next; i; i = next) {
669a0a0a909f21acfcddaab603079ba98266e8daf0eKenneth Graunke      next = i->next;
6702945abea338031cbe90665df60152982bfca6177Chad Versace      if (i->need != need)
6712945abea338031cbe90665df60152982bfca6177Chad Versace	 continue;
6722945abea338031cbe90665df60152982bfca6177Chad Versace      func(intel, mt, i->level, i->layer);
6732945abea338031cbe90665df60152982bfca6177Chad Versace      intel_resolve_map_remove(i);
6742945abea338031cbe90665df60152982bfca6177Chad Versace      did_resolve = true;
6752945abea338031cbe90665df60152982bfca6177Chad Versace   }
6762945abea338031cbe90665df60152982bfca6177Chad Versace
6772945abea338031cbe90665df60152982bfca6177Chad Versace   return did_resolve;
6782945abea338031cbe90665df60152982bfca6177Chad Versace}
6792945abea338031cbe90665df60152982bfca6177Chad Versace
6802945abea338031cbe90665df60152982bfca6177Chad Versacebool
6812945abea338031cbe90665df60152982bfca6177Chad Versaceintel_miptree_all_slices_resolve_hiz(struct intel_context *intel,
6822945abea338031cbe90665df60152982bfca6177Chad Versace				     struct intel_mipmap_tree *mt)
6832945abea338031cbe90665df60152982bfca6177Chad Versace{
6842945abea338031cbe90665df60152982bfca6177Chad Versace   return intel_miptree_all_slices_resolve(intel, mt,
685072634da4a6ee5eafb5f5df26ca5f988209e6d40Eric Anholt					   GEN6_HIZ_OP_HIZ_RESOLVE,
6862945abea338031cbe90665df60152982bfca6177Chad Versace					   intel->vtbl.resolve_hiz_slice);
6872945abea338031cbe90665df60152982bfca6177Chad Versace}
6882945abea338031cbe90665df60152982bfca6177Chad Versace
6892945abea338031cbe90665df60152982bfca6177Chad Versacebool
6902945abea338031cbe90665df60152982bfca6177Chad Versaceintel_miptree_all_slices_resolve_depth(struct intel_context *intel,
6912945abea338031cbe90665df60152982bfca6177Chad Versace				       struct intel_mipmap_tree *mt)
6922945abea338031cbe90665df60152982bfca6177Chad Versace{
6932945abea338031cbe90665df60152982bfca6177Chad Versace   return intel_miptree_all_slices_resolve(intel, mt,
694072634da4a6ee5eafb5f5df26ca5f988209e6d40Eric Anholt					   GEN6_HIZ_OP_DEPTH_RESOLVE,
6952945abea338031cbe90665df60152982bfca6177Chad Versace					   intel->vtbl.resolve_depth_slice);
6962945abea338031cbe90665df60152982bfca6177Chad Versace}
697b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt
698baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholtstatic void
699baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholtintel_miptree_map_gtt(struct intel_context *intel,
700baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		      struct intel_mipmap_tree *mt,
701baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		      struct intel_miptree_map *map,
702baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		      unsigned int level, unsigned int slice)
703b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt{
704b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt   unsigned int bw, bh;
705b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt   void *base;
706b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt   unsigned int image_x, image_y;
707baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   int x = map->x;
708baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   int y = map->y;
709221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt
710b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt   /* For compressed formats, the stride is the number of bytes per
711b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt    * row of blocks.  intel_miptree_get_image_offset() already does
712b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt    * the divide.
713b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt    */
714b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt   _mesa_get_format_block_size(mt->format, &bw, &bh);
715b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt   assert(y % bh == 0);
716b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt   y /= bh;
717b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt
718baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   base = intel_region_map(intel, mt->region, map->mode);
719b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt
720cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat   if (base == NULL)
721cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      map->ptr = NULL;
722cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat   else {
723cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      /* Note that in the case of cube maps, the caller must have passed the
724cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat       * slice number referencing the face.
725cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      */
726cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      intel_miptree_get_image_offset(mt, level, 0, slice, &image_x, &image_y);
727cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      x += image_x;
728cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      y += image_y;
729cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat
730cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      map->stride = mt->region->pitch * mt->cpp;
731cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      map->ptr = base + y * map->stride + x * mt->cpp;
732cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat   }
733221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt
734b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt   DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __FUNCTION__,
735221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt       map->x, map->y, map->w, map->h,
736b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt       mt, _mesa_get_format_name(mt->format),
737221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt       x, y, map->ptr, map->stride);
738b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt}
739b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt
740baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholtstatic void
741baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholtintel_miptree_unmap_gtt(struct intel_context *intel,
742baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt			struct intel_mipmap_tree *mt,
743baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt			struct intel_miptree_map *map,
744baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt			unsigned int level,
745baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt			unsigned int slice)
746baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt{
747baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   intel_region_unmap(intel, mt->region);
748baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt}
749baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt
750b48c3bca87b30003f9e117d299011380e743aec9Eric Anholtstatic void
7515655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholtintel_miptree_map_blit(struct intel_context *intel,
7525655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt		       struct intel_mipmap_tree *mt,
7535655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt		       struct intel_miptree_map *map,
7545655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt		       unsigned int level, unsigned int slice)
7555655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt{
7565655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   unsigned int image_x, image_y;
7575655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   int x = map->x;
7585655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   int y = map->y;
7595655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   int ret;
7605655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
7615655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   /* The blitter requires the pitch to be aligned to 4. */
7625655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   map->stride = ALIGN(map->w * mt->region->cpp, 4);
7635655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
7645655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   map->bo = drm_intel_bo_alloc(intel->bufmgr, "intel_miptree_map_blit() temp",
7655655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt				map->stride * map->h, 4096);
7665655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   if (!map->bo) {
7675655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt      fprintf(stderr, "Failed to allocate blit temporary\n");
7685655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt      goto fail;
7695655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   }
7705655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
7715655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   intel_miptree_get_image_offset(mt, level, 0, slice, &image_x, &image_y);
7725655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   x += image_x;
7735655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   y += image_y;
7745655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
7755655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   if (!intelEmitCopyBlit(intel,
7765655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			  mt->region->cpp,
7775655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			  mt->region->pitch, mt->region->bo,
7785655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			  0, mt->region->tiling,
7795655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			  map->stride / mt->region->cpp, map->bo,
7805655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			  0, I915_TILING_NONE,
7815655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			  x, y,
7825655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			  0, 0,
7835655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			  map->w, map->h,
7845655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			  GL_COPY)) {
7855655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt      fprintf(stderr, "Failed to blit\n");
7865655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt      goto fail;
7875655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   }
7885655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
7895655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   intel_batchbuffer_flush(intel);
7905655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   ret = drm_intel_bo_map(map->bo, (map->mode & GL_MAP_WRITE_BIT) != 0);
7915655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   if (ret) {
7925655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt      fprintf(stderr, "Failed to map blit temporary\n");
7935655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt      goto fail;
7945655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   }
7955655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
7965655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   map->ptr = map->bo->virtual;
7975655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
7985655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __FUNCTION__,
7995655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt       map->x, map->y, map->w, map->h,
8005655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt       mt, _mesa_get_format_name(mt->format),
8015655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt       x, y, map->ptr, map->stride);
8025655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
8035655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   return;
8045655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
8055655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholtfail:
8065655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   drm_intel_bo_unreference(map->bo);
8075655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   map->ptr = NULL;
8085655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   map->stride = 0;
8095655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt}
8105655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
8115655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholtstatic void
8125655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholtintel_miptree_unmap_blit(struct intel_context *intel,
8135655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			 struct intel_mipmap_tree *mt,
8145655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			 struct intel_miptree_map *map,
8155655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			 unsigned int level,
8165655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			 unsigned int slice)
8175655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt{
8185655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   assert(!(map->mode & GL_MAP_WRITE_BIT));
8195655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
8205655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   drm_intel_bo_unmap(map->bo);
8215655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   drm_intel_bo_unreference(map->bo);
8225655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt}
8235655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
8245655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholtstatic void
825b48c3bca87b30003f9e117d299011380e743aec9Eric Anholtintel_miptree_map_s8(struct intel_context *intel,
826b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt		     struct intel_mipmap_tree *mt,
827b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt		     struct intel_miptree_map *map,
828b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt		     unsigned int level, unsigned int slice)
829b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt{
830b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   map->stride = map->w;
831b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   map->buffer = map->ptr = malloc(map->stride * map->h);
832b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   if (!map->buffer)
833b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      return;
834b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
835b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   /* One of either READ_BIT or WRITE_BIT or both is set.  READ_BIT implies no
836b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt    * INVALIDATE_RANGE_BIT.  WRITE_BIT needs the original values read in unless
837b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt    * invalidate is set, since we'll be writing the whole rectangle from our
838b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt    * temporary buffer back out.
839b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt    */
840b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
841b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      uint8_t *untiled_s8_map = map->ptr;
842b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      uint8_t *tiled_s8_map = intel_region_map(intel, mt->region,
843b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt					       GL_MAP_READ_BIT);
844b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      unsigned int image_x, image_y;
845b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
846b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      intel_miptree_get_image_offset(mt, level, 0, slice, &image_x, &image_y);
847b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
848b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      for (uint32_t y = 0; y < map->h; y++) {
849b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	 for (uint32_t x = 0; x < map->w; x++) {
850b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	    ptrdiff_t offset = intel_offset_S8(mt->region->pitch,
851b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	                                       x + image_x + map->x,
852f172eae8b23d0612865895c52af745021ae20a4cDaniel Vetter	                                       y + image_y + map->y,
853f172eae8b23d0612865895c52af745021ae20a4cDaniel Vetter					       intel->has_swizzling);
854b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	    untiled_s8_map[y * map->w + x] = tiled_s8_map[offset];
855b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	 }
856b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      }
857b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
858b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      intel_region_unmap(intel, mt->region);
859b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
860b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      DBG("%s: %d,%d %dx%d from mt %p %d,%d = %p/%d\n", __FUNCTION__,
861b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	  map->x, map->y, map->w, map->h,
862b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	  mt, map->x + image_x, map->y + image_y, map->ptr, map->stride);
863b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   } else {
864b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __FUNCTION__,
865b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	  map->x, map->y, map->w, map->h,
866b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	  mt, map->ptr, map->stride);
867b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   }
868b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt}
869b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
870b48c3bca87b30003f9e117d299011380e743aec9Eric Anholtstatic void
871b48c3bca87b30003f9e117d299011380e743aec9Eric Anholtintel_miptree_unmap_s8(struct intel_context *intel,
872b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt		       struct intel_mipmap_tree *mt,
873b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt		       struct intel_miptree_map *map,
874b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt		       unsigned int level,
875b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt		       unsigned int slice)
876b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt{
877b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   if (map->mode & GL_MAP_WRITE_BIT) {
878b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      unsigned int image_x, image_y;
879b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      uint8_t *untiled_s8_map = map->ptr;
880b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      uint8_t *tiled_s8_map = intel_region_map(intel, mt->region, map->mode);
881b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
882b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      intel_miptree_get_image_offset(mt, level, 0, slice, &image_x, &image_y);
883b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
884b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      for (uint32_t y = 0; y < map->h; y++) {
885b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	 for (uint32_t x = 0; x < map->w; x++) {
886b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	    ptrdiff_t offset = intel_offset_S8(mt->region->pitch,
887b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	                                       x + map->x,
888f172eae8b23d0612865895c52af745021ae20a4cDaniel Vetter	                                       y + map->y,
889f172eae8b23d0612865895c52af745021ae20a4cDaniel Vetter					       intel->has_swizzling);
890b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	    tiled_s8_map[offset] = untiled_s8_map[y * map->w + x];
891b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	 }
892b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      }
893b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
894b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      intel_region_unmap(intel, mt->region);
895b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   }
896b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
897b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   free(map->buffer);
898b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt}
899b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
900ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt/**
901ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt * Mapping function for packed depth/stencil miptrees backed by real separate
902ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt * miptrees for depth and stencil.
903ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt *
904ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt * On gen7, and to support HiZ pre-gen7, we have to have the stencil buffer
905ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt * separate from the depth buffer.  Yet at the GL API level, we have to expose
906ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt * packed depth/stencil textures and FBO attachments, and Mesa core expects to
907ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt * be able to map that memory for texture storage and glReadPixels-type
908ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt * operations.  We give Mesa core that access by mallocing a temporary and
909ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt * copying the data between the actual backing store and the temporary.
910ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt */
911ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholtstatic void
912ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholtintel_miptree_map_depthstencil(struct intel_context *intel,
913ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt			       struct intel_mipmap_tree *mt,
914ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt			       struct intel_miptree_map *map,
915ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt			       unsigned int level, unsigned int slice)
916ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt{
917ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   struct intel_mipmap_tree *z_mt = mt;
918ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   struct intel_mipmap_tree *s_mt = mt->stencil_mt;
919353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt   bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z32_FLOAT;
920353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt   int packed_bpp = map_z32f_x24s8 ? 8 : 4;
921ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
922ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   map->stride = map->w * packed_bpp;
923ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   map->buffer = map->ptr = malloc(map->stride * map->h);
924ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   if (!map->buffer)
925ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      return;
926ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
927ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   /* One of either READ_BIT or WRITE_BIT or both is set.  READ_BIT implies no
928ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt    * INVALIDATE_RANGE_BIT.  WRITE_BIT needs the original values read in unless
929ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt    * invalidate is set, since we'll be writing the whole rectangle from our
930ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt    * temporary buffer back out.
931ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt    */
932ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
933ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      uint32_t *packed_map = map->ptr;
934ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      uint8_t *s_map = intel_region_map(intel, s_mt->region, GL_MAP_READ_BIT);
935ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      uint32_t *z_map = intel_region_map(intel, z_mt->region, GL_MAP_READ_BIT);
936ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      unsigned int s_image_x, s_image_y;
937ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      unsigned int z_image_x, z_image_y;
938ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
939ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_miptree_get_image_offset(s_mt, level, 0, slice,
940ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				     &s_image_x, &s_image_y);
941ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_miptree_get_image_offset(z_mt, level, 0, slice,
942ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				     &z_image_x, &z_image_y);
943ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
944ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      for (uint32_t y = 0; y < map->h; y++) {
945ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	 for (uint32_t x = 0; x < map->w; x++) {
946ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	    int map_x = map->x + x, map_y = map->y + y;
947ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	    ptrdiff_t s_offset = intel_offset_S8(s_mt->region->pitch,
948ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt						 map_x + s_image_x,
949f172eae8b23d0612865895c52af745021ae20a4cDaniel Vetter						 map_y + s_image_y,
950f172eae8b23d0612865895c52af745021ae20a4cDaniel Vetter						 intel->has_swizzling);
951ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	    ptrdiff_t z_offset = ((map_y + z_image_y) * z_mt->region->pitch +
952ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				  (map_x + z_image_x));
953ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	    uint8_t s = s_map[s_offset];
954ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	    uint32_t z = z_map[z_offset];
955ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
956353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	    if (map_z32f_x24s8) {
957353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	       packed_map[(y * map->w + x) * 2 + 0] = z;
958353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	       packed_map[(y * map->w + x) * 2 + 1] = s;
959353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	    } else {
960353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	       packed_map[y * map->w + x] = (s << 24) | (z & 0x00ffffff);
961353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	    }
962ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	 }
963ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      }
964ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
965ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_region_unmap(intel, s_mt->region);
966ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_region_unmap(intel, z_mt->region);
967ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
968ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      DBG("%s: %d,%d %dx%d from z mt %p %d,%d, s mt %p %d,%d = %p/%d\n",
969ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  __FUNCTION__,
970ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  map->x, map->y, map->w, map->h,
971ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  z_mt, map->x + z_image_x, map->y + z_image_y,
972ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  s_mt, map->x + s_image_x, map->y + s_image_y,
973ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  map->ptr, map->stride);
974ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   } else {
975ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __FUNCTION__,
976ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  map->x, map->y, map->w, map->h,
977ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  mt, map->ptr, map->stride);
978ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   }
979ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt}
980ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
981ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholtstatic void
982ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholtintel_miptree_unmap_depthstencil(struct intel_context *intel,
983ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				 struct intel_mipmap_tree *mt,
984ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				 struct intel_miptree_map *map,
985ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				 unsigned int level,
986ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				 unsigned int slice)
987ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt{
988ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   struct intel_mipmap_tree *z_mt = mt;
989ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   struct intel_mipmap_tree *s_mt = mt->stencil_mt;
990353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt   bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z32_FLOAT;
991ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
992ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   if (map->mode & GL_MAP_WRITE_BIT) {
993ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      uint32_t *packed_map = map->ptr;
994ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      uint8_t *s_map = intel_region_map(intel, s_mt->region, map->mode);
995ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      uint32_t *z_map = intel_region_map(intel, z_mt->region, map->mode);
996ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      unsigned int s_image_x, s_image_y;
997ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      unsigned int z_image_x, z_image_y;
998ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
999ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_miptree_get_image_offset(s_mt, level, 0, slice,
1000ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				     &s_image_x, &s_image_y);
1001ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_miptree_get_image_offset(z_mt, level, 0, slice,
1002ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				     &z_image_x, &z_image_y);
1003ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1004ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      for (uint32_t y = 0; y < map->h; y++) {
1005ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	 for (uint32_t x = 0; x < map->w; x++) {
1006ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	    ptrdiff_t s_offset = intel_offset_S8(s_mt->region->pitch,
1007ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt						 x + s_image_x + map->x,
1008f172eae8b23d0612865895c52af745021ae20a4cDaniel Vetter						 y + s_image_y + map->y,
1009f172eae8b23d0612865895c52af745021ae20a4cDaniel Vetter						 intel->has_swizzling);
1010ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	    ptrdiff_t z_offset = ((y + z_image_y) * z_mt->region->pitch +
1011ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				  (x + z_image_x));
1012ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1013353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	    if (map_z32f_x24s8) {
1014353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	       z_map[z_offset] = packed_map[(y * map->w + x) * 2 + 0];
1015353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	       s_map[s_offset] = packed_map[(y * map->w + x) * 2 + 1];
1016353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	    } else {
1017353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	       uint32_t packed = packed_map[y * map->w + x];
1018353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	       s_map[s_offset] = packed >> 24;
1019353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	       z_map[z_offset] = packed;
1020353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	    }
1021ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	 }
1022ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      }
1023ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1024ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_region_unmap(intel, s_mt->region);
1025ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_region_unmap(intel, z_mt->region);
1026ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1027ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      DBG("%s: %d,%d %dx%d from z mt %p (%s) %d,%d, s mt %p %d,%d = %p/%d\n",
1028ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  __FUNCTION__,
1029ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  map->x, map->y, map->w, map->h,
1030ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  z_mt, _mesa_get_format_name(z_mt->format),
1031ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  map->x + z_image_x, map->y + z_image_y,
1032ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  s_mt, map->x + s_image_x, map->y + s_image_y,
1033ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  map->ptr, map->stride);
1034ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   }
1035ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1036ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   free(map->buffer);
1037ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt}
1038ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1039baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholtvoid
1040baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholtintel_miptree_map(struct intel_context *intel,
1041baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  struct intel_mipmap_tree *mt,
1042baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  unsigned int level,
1043baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  unsigned int slice,
1044baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  unsigned int x,
1045baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  unsigned int y,
1046baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  unsigned int w,
1047baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  unsigned int h,
1048baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  GLbitfield mode,
1049baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  void **out_ptr,
1050baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  int *out_stride)
1051baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt{
1052baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   struct intel_miptree_map *map;
1053baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt
1054baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   map = calloc(1, sizeof(struct intel_miptree_map));
1055baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   if (!map){
1056baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt      *out_ptr = NULL;
1057baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt      *out_stride = 0;
1058baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt      return;
1059baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   }
1060baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt
1061baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   assert(!mt->level[level].slice[slice].map);
1062baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   mt->level[level].slice[slice].map = map;
1063baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   map->mode = mode;
1064baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   map->x = x;
1065baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   map->y = y;
1066baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   map->w = w;
1067baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   map->h = h;
1068baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt
1069baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   intel_miptree_slice_resolve_depth(intel, mt, level, slice);
1070baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   if (map->mode & GL_MAP_WRITE_BIT) {
1071baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt      intel_miptree_slice_set_needs_hiz_resolve(mt, level, slice);
1072baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   }
1073baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt
1074b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   if (mt->format == MESA_FORMAT_S8) {
1075b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      intel_miptree_map_s8(intel, mt, map, level, slice);
1076ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   } else if (mt->stencil_mt) {
1077ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_miptree_map_depthstencil(intel, mt, map, level, slice);
107884e5f1c635899c657da58ca51d5e841354e9de9cEugeni Dodonov   } else if (intel->has_llc &&
10795655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt	      !(mode & GL_MAP_WRITE_BIT) &&
10805655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt	      !mt->compressed &&
10815655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt	      mt->region->tiling == I915_TILING_X) {
10825655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt      intel_miptree_map_blit(intel, mt, map, level, slice);
1083b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   } else {
1084b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      intel_miptree_map_gtt(intel, mt, map, level, slice);
1085b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   }
1086baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt
1087baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   *out_ptr = map->ptr;
1088baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   *out_stride = map->stride;
1089cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat
1090cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat   if (map->ptr == NULL) {
1091cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      mt->level[level].slice[slice].map = NULL;
1092cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      free(map);
1093cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat   }
1094baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt}
1095baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt
1096b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholtvoid
1097b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholtintel_miptree_unmap(struct intel_context *intel,
1098b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt		    struct intel_mipmap_tree *mt,
1099b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt		    unsigned int level,
1100b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt		    unsigned int slice)
1101b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt{
1102221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt   struct intel_miptree_map *map = mt->level[level].slice[slice].map;
1103221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt
1104221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt   if (!map)
1105221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt      return;
1106221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt
1107b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt   DBG("%s: mt %p (%s) level %d slice %d\n", __FUNCTION__,
1108b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt       mt, _mesa_get_format_name(mt->format), level, slice);
1109b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt
1110b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   if (mt->format == MESA_FORMAT_S8) {
1111b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      intel_miptree_unmap_s8(intel, mt, map, level, slice);
1112ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   } else if (mt->stencil_mt) {
1113ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_miptree_unmap_depthstencil(intel, mt, map, level, slice);
11145655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   } else if (map->bo) {
11155655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt      intel_miptree_unmap_blit(intel, mt, map, level, slice);
1116b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   } else {
1117b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      intel_miptree_unmap_gtt(intel, mt, map, level, slice);
1118b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   }
1119221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt
1120221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt   mt->level[level].slice[slice].map = NULL;
1121221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt   free(map);
1122b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt}
1123