intel_mipmap_tree.c revision 8b762ebd72c1bb1a9827a667ad55ba9516505adf
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"
408b762ebd72c1bb1a9827a667ad55ba9516505adfBrian Paul#include "main/glformats.h"
41a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace#include "main/texcompress_etc.h"
42b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt#include "main/teximage.h"
4377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
4477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt#define FILE_DEBUG_FLAG DEBUG_MIPTREE
4577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
4677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtstatic GLenum
4777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholttarget_to_target(GLenum target)
4877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
4977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   switch (target) {
5077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
5177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
5277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
5377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
5477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
5577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
5677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      return GL_TEXTURE_CUBE_MAP_ARB;
5777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   default:
5877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      return target;
5977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   }
6077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
6177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
627e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace/**
637e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace * @param for_region Indicates that the caller is
647e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace *        intel_miptree_create_for_region(). If true, then do not create
657e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace *        \c stencil_mt.
667e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace */
676d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsbergstatic struct intel_mipmap_tree *
686d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsbergintel_miptree_create_internal(struct intel_context *intel,
696d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg			      GLenum target,
70d5809115b568d8b74f47316607dce0730964517aEric Anholt			      gl_format format,
715b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt			      GLuint first_level,
725b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt			      GLuint last_level,
736d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg			      GLuint width0,
746d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg			      GLuint height0,
757e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace			      GLuint depth0,
7619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry			      bool for_region,
77455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry                              GLuint num_samples,
781bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry                              enum intel_msaa_layout msaa_layout)
7977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
8077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
816dcc398ac0837025cf60b4d6a056fa3b0a16466fEric Anholt   int compress_byte = 0;
8277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
835b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt   DBG("%s target %s format %s level %d..%d <-- %p\n", __FUNCTION__,
8477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt       _mesa_lookup_enum_by_nr(target),
859a523a48af05118424714f0a34ca3dda6861186aEric Anholt       _mesa_get_format_name(format),
865b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt       first_level, last_level, mt);
8777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
886dcc398ac0837025cf60b4d6a056fa3b0a16466fEric Anholt   if (_mesa_is_format_compressed(format))
896dcc398ac0837025cf60b4d6a056fa3b0a16466fEric Anholt      compress_byte = intel_compressed_num_bytes(format);
906dcc398ac0837025cf60b4d6a056fa3b0a16466fEric Anholt
9177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->target = target_to_target(target);
92d5809115b568d8b74f47316607dce0730964517aEric Anholt   mt->format = format;
935b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt   mt->first_level = first_level;
945b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt   mt->last_level = last_level;
9577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->width0 = width0;
9677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->height0 = height0;
97f94fef83db10f0c9327bd3dd43510ad31c94d82aEric Anholt   mt->cpp = compress_byte ? compress_byte : _mesa_get_format_bytes(mt->format);
9819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   mt->num_samples = num_samples;
9977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->compressed = compress_byte ? 1 : 0;
1001bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   mt->msaa_layout = msaa_layout;
10177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->refcount = 1;
10277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
1031bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   /* array_spacing_lod0 is only used for non-IMS MSAA surfaces.  TODO: can we
1041bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry    * use it elsewhere?
105455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry    */
1061bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   switch (msaa_layout) {
1071bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_NONE:
1081bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_IMS:
1091bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      mt->array_spacing_lod0 = false;
1101bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
1111bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_UMS:
1121bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_CMS:
1131bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      mt->array_spacing_lod0 = true;
1141bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
1151bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   }
116455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry
117d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   if (target == GL_TEXTURE_CUBE_MAP) {
118d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      assert(depth0 == 1);
119d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      mt->depth0 = 6;
120d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   } else {
121d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      mt->depth0 = depth0;
122d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   }
123d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace
1247e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace   if (!for_region &&
1257e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace       _mesa_is_depthstencil_format(_mesa_get_format_base_format(format)) &&
1265c9a55665d78b96bfb8ce8eab43b5558dd656a6dEric Anholt       (intel->must_use_separate_stencil ||
1275c9a55665d78b96bfb8ce8eab43b5558dd656a6dEric Anholt	(intel->has_separate_stencil &&
1285c9a55665d78b96bfb8ce8eab43b5558dd656a6dEric Anholt	 intel->vtbl.is_hiz_depth_format(intel, format)))) {
1291bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      /* MSAA stencil surfaces always use IMS layout. */
1301bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      enum intel_msaa_layout msaa_layout =
1311bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry         num_samples > 0 ? INTEL_MSAA_LAYOUT_IMS : INTEL_MSAA_LAYOUT_NONE;
1323eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace      mt->stencil_mt = intel_miptree_create(intel,
1333eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace                                            mt->target,
1343eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace                                            MESA_FORMAT_S8,
1353eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace                                            mt->first_level,
1363eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace                                            mt->last_level,
1373eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace                                            mt->width0,
1383eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace                                            mt->height0,
1393eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace                                            mt->depth0,
14019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry                                            true,
141455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry                                            num_samples,
1421bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry                                            msaa_layout);
1433eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace      if (!mt->stencil_mt) {
1443eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace	 intel_miptree_release(&mt);
1453eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace	 return NULL;
1463eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace      }
147fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt
148fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt      /* Fix up the Z miptree format for how we're splitting out separate
149fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt       * stencil.  Gen7 expects there to be no stencil bits in its depth buffer.
150fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt       */
151fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt      if (mt->format == MESA_FORMAT_S8_Z24) {
152fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt	 mt->format = MESA_FORMAT_X8_Z24;
1534790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt      } else if (mt->format == MESA_FORMAT_Z32_FLOAT_X24S8) {
1544790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt	 mt->format = MESA_FORMAT_Z32_FLOAT;
1554790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt	 mt->cpp = 4;
156fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt      } else {
1574790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt	 _mesa_problem(NULL, "Unknown format %s in separate stencil mt\n",
158fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt		       _mesa_get_format_name(mt->format));
159fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt      }
1603eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace   }
1613eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace
1624790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt   intel_get_texture_alignment_unit(intel, mt->format,
1634790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt				    &mt->align_w, &mt->align_h);
1644790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt
1654790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt#ifdef I915
1664790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt   (void) intel;
1674790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt   if (intel->is_945)
1684790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt      i945_miptree_layout(mt);
1694790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt   else
1704790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt      i915_miptree_layout(mt);
1714790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt#else
1724790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt   brw_miptree_layout(intel, mt);
1734790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt#endif
1744790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt
1756d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg   return mt;
1766d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg}
1776d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg
17847a7535f413d6467082de224f64eecc046227406Brian Paul
1796d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsbergstruct intel_mipmap_tree *
1806d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsbergintel_miptree_create(struct intel_context *intel,
1816d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg		     GLenum target,
182d5809115b568d8b74f47316607dce0730964517aEric Anholt		     gl_format format,
1835b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt		     GLuint first_level,
1845b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt		     GLuint last_level,
1856d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg		     GLuint width0,
1866d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg		     GLuint height0,
187f94fef83db10f0c9327bd3dd43510ad31c94d82aEric Anholt		     GLuint depth0,
18819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry		     bool expect_accelerated_upload,
189455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry                     GLuint num_samples,
1901bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry                     enum intel_msaa_layout msaa_layout)
1916d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg{
1926d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg   struct intel_mipmap_tree *mt;
193453f0b1f243b26e02b371293e9c8bbf09362c14aEric Anholt   uint32_t tiling = I915_TILING_NONE;
194a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   GLenum base_format;
195a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   bool wraps_etc1 = false;
196a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace
197a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   if (format == MESA_FORMAT_ETC1_RGB8) {
198a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace      format = MESA_FORMAT_RGBX8888_REV;
199a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace      wraps_etc1 = true;
200a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   }
201a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace
202a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   base_format = _mesa_get_format_base_format(format);
2031ba96651e12b3c74fb9c8f5a61b183ef36a27b1eEric Anholt
2046dcc398ac0837025cf60b4d6a056fa3b0a16466fEric Anholt   if (intel->use_texture_tiling && !_mesa_is_format_compressed(format)) {
205caf3038123d6d29afd7d1f0cd6db98a2282c3ca1Eric Anholt      if (intel->gen >= 4 &&
2069c0ba017c8ff7caafc3ff94da3c035e687231596Eric Anholt	  (base_format == GL_DEPTH_COMPONENT ||
2079c0ba017c8ff7caafc3ff94da3c035e687231596Eric Anholt	   base_format == GL_DEPTH_STENCIL_EXT))
208bd10f0e84f1491363d76d92dcbd410ab5cc43dbeEric Anholt	 tiling = I915_TILING_Y;
2091bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      else if (msaa_layout != INTEL_MSAA_LAYOUT_NONE) {
21019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry         /* From p82 of the Sandy Bridge PRM, dw3[1] of SURFACE_STATE ("Tiled
21119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          * Surface"):
21219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          *
21319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          *   [DevSNB+]: For multi-sample render targets, this field must be
21419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          *   1. MSRTs can only be tiled.
21519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          *
21619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          * Our usual reason for preferring X tiling (fast blits using the
21719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          * blitting engine) doesn't apply to MSAA, since we'll generally be
21819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          * downsampling or upsampling when blitting between the MSAA buffer
21919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          * and another buffer, and the blitting engine doesn't support that.
22019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          * So use Y tiling, since it makes better use of the cache.
22119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          */
22219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry         tiling = I915_TILING_Y;
22319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      } else if (width0 >= 64)
224bd10f0e84f1491363d76d92dcbd410ab5cc43dbeEric Anholt	 tiling = I915_TILING_X;
225453f0b1f243b26e02b371293e9c8bbf09362c14aEric Anholt   }
2266d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg
227f22068d5be7c829d3768154845ef3c5a2986fed4Eric Anholt   if (format == MESA_FORMAT_S8) {
228f22068d5be7c829d3768154845ef3c5a2986fed4Eric Anholt      /* The stencil buffer is W tiled. However, we request from the kernel a
229a27c7d80afc3160a0face4b8781bf921229bc3ccPaul Berry       * non-tiled buffer because the GTT is incapable of W fencing.  So round
230a27c7d80afc3160a0face4b8781bf921229bc3ccPaul Berry       * up the width and height to match the size of W tiles (64x64).
231f22068d5be7c829d3768154845ef3c5a2986fed4Eric Anholt       */
232f22068d5be7c829d3768154845ef3c5a2986fed4Eric Anholt      tiling = I915_TILING_NONE;
233f22068d5be7c829d3768154845ef3c5a2986fed4Eric Anholt      width0 = ALIGN(width0, 64);
234a27c7d80afc3160a0face4b8781bf921229bc3ccPaul Berry      height0 = ALIGN(height0, 64);
235f22068d5be7c829d3768154845ef3c5a2986fed4Eric Anholt   }
236f22068d5be7c829d3768154845ef3c5a2986fed4Eric Anholt
2379a523a48af05118424714f0a34ca3dda6861186aEric Anholt   mt = intel_miptree_create_internal(intel, target, format,
2385b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt				      first_level, last_level, width0,
2397e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace				      height0, depth0,
2401bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry				      false, num_samples, msaa_layout);
2417ed1fd5d8438e55fe24091844cdfccb0881306bcXiang, Haihao   /*
2428b69c42b356d51c3a37bc0af41738b016c2adc5bXiang, Haihao    * pitch == 0 || height == 0  indicates the null texture
2437ed1fd5d8438e55fe24091844cdfccb0881306bcXiang, Haihao    */
2441a662e7c18cab98f1b122f6766faf338725de673Yuanhan Liu   if (!mt || !mt->total_width || !mt->total_height) {
245747f0307626ef5bcf2f889ab66bcc95ab8eda2c8Chad Versace      intel_miptree_release(&mt);
2466d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg      return NULL;
247a7d0665c00053839065592c339bfb298187a82beVinson Lee   }
2486d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg
249a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   mt->wraps_etc1 = wraps_etc1;
2509087ba128089ed0dc00e6eb38f37126fb7557d3bKristian Høgsberg   mt->region = intel_region_alloc(intel->intelScreen,
2511ba96651e12b3c74fb9c8f5a61b183ef36a27b1eEric Anholt				   tiling,
2528db761409dadc2e899d4e7107eff3aa07b07aa11Eric Anholt				   mt->cpp,
253362c1bf75eb74de5b4655c481b74f79718ed4a34Eric Anholt				   mt->total_width,
2548db761409dadc2e899d4e7107eff3aa07b07aa11Eric Anholt				   mt->total_height,
25540dd024be618d805b3744e15d25e115018641324Eric Anholt				   expect_accelerated_upload);
25602ebad900db4ef1ac42cbfb41b433919a4c857a2Kristian Høgsberg   mt->offset = 0;
2576d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg
25877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   if (!mt->region) {
259747f0307626ef5bcf2f889ab66bcc95ab8eda2c8Chad Versace       intel_miptree_release(&mt);
2606d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg       return NULL;
2616d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg   }
2626d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg
2636d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg   return mt;
2646d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg}
2656d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg
26647a7535f413d6467082de224f64eecc046227406Brian Paul
2676d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsbergstruct intel_mipmap_tree *
2686d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsbergintel_miptree_create_for_region(struct intel_context *intel,
2696d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg				GLenum target,
270d5809115b568d8b74f47316607dce0730964517aEric Anholt				gl_format format,
2716f23d9b637602d9997896c6ca5cba0dfe36a092eIan Romanick				struct intel_region *region)
2726d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg{
2736d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg   struct intel_mipmap_tree *mt;
2746d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg
2759a523a48af05118424714f0a34ca3dda6861186aEric Anholt   mt = intel_miptree_create_internal(intel, target, format,
2765b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt				      0, 0,
2777e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace				      region->width, region->height, 1,
278455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry				      true, 0 /* num_samples */,
2791bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry                                      INTEL_MSAA_LAYOUT_NONE);
2806d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg   if (!mt)
2816d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg      return mt;
282da011faf48155a5c02ebc1fe1fa20a4f54b8c657Eric Anholt
283e72a44215312ae1f3c812ba28e47b4aec3589de9Pierre Willenbrock   intel_region_reference(&mt->region, region);
28477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
28577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   return mt;
28647a7535f413d6467082de224f64eecc046227406Brian Paul}
28747a7535f413d6467082de224f64eecc046227406Brian Paul
288455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry/**
2891bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * Determine which MSAA layout should be used by the MSAA surface being
2901bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * created, based on the chip generation and the surface type.
291455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry */
2921bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berrystatic enum intel_msaa_layout
2931bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berrycompute_msaa_layout(struct intel_context *intel, gl_format format)
294455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry{
2951bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   /* Prior to Gen7, all MSAA surfaces used IMS layout. */
296455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry   if (intel->gen < 7)
2971bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      return INTEL_MSAA_LAYOUT_IMS;
298455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry
2991bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   /* In Gen7, IMS layout is only used for depth and stencil buffers. */
300455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry   switch (_mesa_get_format_base_format(format)) {
301455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry   case GL_DEPTH_COMPONENT:
302455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry   case GL_STENCIL_INDEX:
303455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry   case GL_DEPTH_STENCIL:
3041bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      return INTEL_MSAA_LAYOUT_IMS;
305455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry   default:
30633202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry      /* From the Ivy Bridge PRM, Vol4 Part1 p77 ("MCS Enable"):
30733202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry       *
30833202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry       *   This field must be set to 0 for all SINT MSRTs when all RT channels
30933202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry       *   are not written
31033202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry       *
31133202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry       * In practice this means that we have to disable MCS for all signed
31233202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry       * integer MSAA buffers.  The alternative, to disable MCS only when one
31333202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry       * of the render target channels is disabled, is impractical because it
31433202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry       * would require converting between CMS and UMS MSAA layouts on the fly,
31533202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry       * which is expensive.
31633202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry       */
31733202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry      if (_mesa_get_format_datatype(format) == GL_INT) {
31833202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry         /* TODO: is this workaround needed for future chipsets? */
31933202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry         assert(intel->gen == 7);
32033202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry         return INTEL_MSAA_LAYOUT_UMS;
32133202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry      } else {
32233202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry         return INTEL_MSAA_LAYOUT_CMS;
32333202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry      }
324455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry   }
325455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry}
326455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry
327005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versacestruct intel_mipmap_tree*
328005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versaceintel_miptree_create_for_renderbuffer(struct intel_context *intel,
329005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versace                                      gl_format format,
330005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versace                                      uint32_t width,
33119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry                                      uint32_t height,
33219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry                                      uint32_t num_samples)
333005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versace{
334005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versace   struct intel_mipmap_tree *mt;
335455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry   uint32_t depth = 1;
3361bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   enum intel_msaa_layout msaa_layout = INTEL_MSAA_LAYOUT_NONE;
337455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry
338455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry   if (num_samples > 0) {
339455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry      /* Adjust width/height/depth for MSAA */
3401bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      msaa_layout = compute_msaa_layout(intel, format);
3411bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      if (msaa_layout == INTEL_MSAA_LAYOUT_IMS) {
342455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry         /* In the Sandy Bridge PRM, volume 4, part 1, page 31, it says:
343455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *
344455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *     "Any of the other messages (sample*, LOD, load4) used with a
345455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *      (4x) multisampled surface will in-effect sample a surface with
346455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *      double the height and width as that indicated in the surface
347455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *      state. Each pixel position on the original-sized surface is
348455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *      replaced with a 2x2 of samples with the following arrangement:
349455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *
350455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *         sample 0 sample 2
351455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *         sample 1 sample 3"
352455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *
353455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          * Thus, when sampling from a multisampled texture, it behaves as
354455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          * though the layout in memory for (x,y,sample) is:
355455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *
356455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *      (0,0,0) (0,0,2)   (1,0,0) (1,0,2)
357455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *      (0,0,1) (0,0,3)   (1,0,1) (1,0,3)
358455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *
359455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *      (0,1,0) (0,1,2)   (1,1,0) (1,1,2)
360455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *      (0,1,1) (0,1,3)   (1,1,1) (1,1,3)
361455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *
362455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          * However, the actual layout of multisampled data in memory is:
363455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *
364455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *      (0,0,0) (1,0,0)   (0,0,1) (1,0,1)
365455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *      (0,1,0) (1,1,0)   (0,1,1) (1,1,1)
366455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *
367455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *      (0,0,2) (1,0,2)   (0,0,3) (1,0,3)
368455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *      (0,1,2) (1,1,2)   (0,1,3) (1,1,3)
369455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *
370455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          * This pattern repeats for each 2x2 pixel block.
371455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *
372455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          * As a result, when calculating the size of our 4-sample buffer for
373455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          * an odd width or height, we have to align before scaling up because
374455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          * sample 3 is in that bottom right 2x2 block.
375455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          */
376455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry         switch (num_samples) {
377455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry         case 4:
378455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry            width = ALIGN(width, 2) * 2;
379455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry            height = ALIGN(height, 2) * 2;
380455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry            break;
381455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry         case 8:
382455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry            width = ALIGN(width, 2) * 4;
383455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry            height = ALIGN(height, 2) * 2;
384455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry            break;
385455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry         default:
386455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry            /* num_samples should already have been quantized to 0, 4, or
387455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry             * 8.
388455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry             */
389455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry            assert(false);
390455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry         }
391455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry      } else {
392455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry         /* Non-interleaved */
393455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry         depth = num_samples;
394455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry      }
39519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
39619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
397f22068d5be7c829d3768154845ef3c5a2986fed4Eric Anholt   mt = intel_miptree_create(intel, GL_TEXTURE_2D, format, 0, 0,
398455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry			     width, height, depth, true, num_samples,
3991bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry                             msaa_layout);
400005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versace
401005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versace   return mt;
402005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versace}
403005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versace
40477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtvoid
40577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtintel_miptree_reference(struct intel_mipmap_tree **dst,
40677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt                        struct intel_mipmap_tree *src)
40777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
40832fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt   if (*dst == src)
40932fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt      return;
41032fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt
41132fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt   intel_miptree_release(dst);
41232fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt
41332fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt   if (src) {
41432fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt      src->refcount++;
41532fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt      DBG("%s %p refcount now %d\n", __FUNCTION__, src, src->refcount);
41632fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt   }
41732fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt
41877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   *dst = src;
41977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
42077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
42147a7535f413d6467082de224f64eecc046227406Brian Paul
42277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtvoid
423db3ada6055814a4bd5aa95fc9505fc101864391dEric Anholtintel_miptree_release(struct intel_mipmap_tree **mt)
42477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
42577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   if (!*mt)
42677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      return;
42777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
42877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   DBG("%s %p refcount will be %d\n", __FUNCTION__, *mt, (*mt)->refcount - 1);
42977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   if (--(*mt)->refcount <= 0) {
43077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      GLuint i;
43177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
43277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      DBG("%s deleting %p\n", __FUNCTION__, *mt);
43377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
43477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      intel_region_release(&((*mt)->region));
4353eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace      intel_miptree_release(&(*mt)->stencil_mt);
436a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace      intel_miptree_release(&(*mt)->hiz_mt);
437ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry      intel_miptree_release(&(*mt)->mcs_mt);
4382945abea338031cbe90665df60152982bfca6177Chad Versace      intel_resolve_map_clear(&(*mt)->hiz_map);
43977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
4402d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt      for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
441c2ddde70daece24d1eb7185946032158993a9e4dChad Versace	 free((*mt)->level[i].slice);
4422d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt      }
44377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
44477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      free(*mt);
44577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   }
44677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   *mt = NULL;
44777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
44877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
449fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholtvoid
450fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholtintel_miptree_get_dimensions_for_image(struct gl_texture_image *image,
451fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt                                       int *width, int *height, int *depth)
452fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt{
453fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt   switch (image->TexObject->Target) {
454fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt   case GL_TEXTURE_1D_ARRAY:
455fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt      *width = image->Width;
456fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt      *height = 1;
457fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt      *depth = image->Height;
458fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt      break;
459fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt   default:
460fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt      *width = image->Width;
461fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt      *height = image->Height;
462fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt      *depth = image->Depth;
463fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt      break;
464fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt   }
465fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt}
46677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
46747a7535f413d6467082de224f64eecc046227406Brian Paul/**
46847a7535f413d6467082de224f64eecc046227406Brian Paul * Can the image be pulled into a unified mipmap tree?  This mirrors
46977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * the completeness test in a lot of ways.
47077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt *
47177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * Not sure whether I want to pass gl_texture_image here.
47277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt */
4732e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunkebool
47477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtintel_miptree_match_image(struct intel_mipmap_tree *mt,
4758f30ceaaefc33401b08739a16ce1c5638d6432faEric Anholt                          struct gl_texture_image *image)
47677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
4778f30ceaaefc33401b08739a16ce1c5638d6432faEric Anholt   struct intel_texture_image *intelImage = intel_texture_image(image);
478fa2c886863492cc3eeee6d2059ae24edc1cb2bffBrian Paul   GLuint level = intelImage->base.Base.Level;
479fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt   int width, height, depth;
480b6bdafdf2cf1110b4a5ca7cf9e1c3dcb124b800fBrian Paul
481ab7794cada02f3b3b5e3a642c20eeedeb17b65a6Eric Anholt   if (target_to_target(image->TexObject->Target) != mt->target)
482ab7794cada02f3b3b5e3a642c20eeedeb17b65a6Eric Anholt      return false;
483ab7794cada02f3b3b5e3a642c20eeedeb17b65a6Eric Anholt
484fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt   if (image->TexFormat != mt->format &&
485fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt       !(image->TexFormat == MESA_FORMAT_S8_Z24 &&
486fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt	 mt->format == MESA_FORMAT_X8_Z24 &&
487fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt	 mt->stencil_mt)) {
4882e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunke      return false;
489fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt   }
490d12fa3511da23d8285f3ea1a51a1f328cdbb1462Xiang, Haihao
491fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt   intel_miptree_get_dimensions_for_image(image, &width, &height, &depth);
492fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt
4937f854a5028ef00a0e041e78a770456204dc58eefEric Anholt   if (mt->target == GL_TEXTURE_CUBE_MAP)
4947f854a5028ef00a0e041e78a770456204dc58eefEric Anholt      depth = 6;
4957f854a5028ef00a0e041e78a770456204dc58eefEric Anholt
49677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   /* Test image dimensions against the base level image adjusted for
49777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt    * minification.  This will also catch images not present in the
49877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt    * tree, changed targets, etc.
49977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt    */
500fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt   if (width != mt->level[level].width ||
501fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt       height != mt->level[level].height ||
502fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt       depth != mt->level[level].depth)
5032e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunke      return false;
50477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
5052e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunke   return true;
50677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
50777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
50877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
50977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtvoid
51077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtintel_miptree_set_level_info(struct intel_mipmap_tree *mt,
511659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			     GLuint level,
512659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			     GLuint x, GLuint y,
513659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			     GLuint w, GLuint h, GLuint d)
51477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
51577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->level[level].width = w;
51677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->level[level].height = h;
51777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->level[level].depth = d;
5182d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt   mt->level[level].level_x = x;
5192d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt   mt->level[level].level_y = y;
52077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
5210c51390e4b5e04b992e50fcbed751024e6c329deEric Anholt   DBG("%s level %d size: %d,%d,%d offset %d,%d\n", __FUNCTION__,
5220c51390e4b5e04b992e50fcbed751024e6c329deEric Anholt       level, w, h, d, x, y);
52377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
524c2ddde70daece24d1eb7185946032158993a9e4dChad Versace   assert(mt->level[level].slice == NULL);
52577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
526221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt   mt->level[level].slice = calloc(d, sizeof(*mt->level[0].slice));
527c2ddde70daece24d1eb7185946032158993a9e4dChad Versace   mt->level[level].slice[0].x_offset = mt->level[level].level_x;
528c2ddde70daece24d1eb7185946032158993a9e4dChad Versace   mt->level[level].slice[0].y_offset = mt->level[level].level_y;
52977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
53077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
53177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
53277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtvoid
5332d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholtintel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
5342d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt			       GLuint level, GLuint img,
5352d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt			       GLuint x, GLuint y)
53677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
53777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   if (img == 0 && level == 0)
53877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      assert(x == 0 && y == 0);
53977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
540d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   assert(img < mt->level[level].depth);
54177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
542c2ddde70daece24d1eb7185946032158993a9e4dChad Versace   mt->level[level].slice[img].x_offset = mt->level[level].level_x + x;
543c2ddde70daece24d1eb7185946032158993a9e4dChad Versace   mt->level[level].slice[img].y_offset = mt->level[level].level_y + y;
54477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
5452d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt   DBG("%s level %d img %d pos %d,%d\n",
5462d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt       __FUNCTION__, level, img,
547c2ddde70daece24d1eb7185946032158993a9e4dChad Versace       mt->level[level].slice[img].x_offset,
548c2ddde70daece24d1eb7185946032158993a9e4dChad Versace       mt->level[level].slice[img].y_offset);
54977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
55077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
55147a7535f413d6467082de224f64eecc046227406Brian Paul
552d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace/**
553d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace * For cube map textures, either the \c face parameter can be used, of course,
554d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace * or the cube face can be interpreted as a depth layer and the \c layer
555d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace * parameter used.
556d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace */
5572995bf0d68f1b28ba68b81e9dc79e3ab52bc2795Xiang, Haihaovoid
5582d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholtintel_miptree_get_image_offset(struct intel_mipmap_tree *mt,
559d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace			       GLuint level, GLuint face, GLuint layer,
5602d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt			       GLuint *x, GLuint *y)
56177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
562d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   int slice;
563d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace
564d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   if (face > 0) {
565d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      assert(mt->target == GL_TEXTURE_CUBE_MAP);
566d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      assert(face < 6);
567d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      assert(layer == 0);
568d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      slice = face;
569d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   } else {
570d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      /* This branch may be taken even if the texture target is a cube map. In
571d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace       * that case, the caller chose to interpret each cube face as a layer.
572d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace       */
573d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      assert(face == 0);
574d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      slice = layer;
5752d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt   }
576d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace
577d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   *x = mt->level[level].slice[slice].x_offset;
578d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   *y = mt->level[level].slice[slice].y_offset;
57977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
58077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
581278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versacestatic void
582278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versaceintel_miptree_copy_slice(struct intel_context *intel,
583278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			 struct intel_mipmap_tree *dst_mt,
584278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			 struct intel_mipmap_tree *src_mt,
585278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			 int level,
586278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			 int face,
587278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			 int depth)
588278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
589278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace{
590278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   gl_format format = src_mt->format;
591278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   uint32_t width = src_mt->level[level].width;
592278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   uint32_t height = src_mt->level[level].height;
593278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
594278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   assert(depth < src_mt->level[level].depth);
595278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
596278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   if (dst_mt->compressed) {
597017c13d55b5b086774d6afea2ca754482c624c6aChad Versace      height = ALIGN(height, dst_mt->align_h) / dst_mt->align_h;
598017c13d55b5b086774d6afea2ca754482c624c6aChad Versace      width = ALIGN(width, dst_mt->align_w);
599278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   }
600278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
601278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   uint32_t dst_x, dst_y, src_x, src_y;
602278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   intel_miptree_get_image_offset(dst_mt, level, face, depth,
603278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace				  &dst_x, &dst_y);
604278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   intel_miptree_get_image_offset(src_mt, level, face, depth,
605278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace				  &src_x, &src_y);
606278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
607278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   DBG("validate blit mt %p %d,%d/%d -> mt %p %d,%d/%d (%dx%d)\n",
608278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace       src_mt, src_x, src_y, src_mt->region->pitch * src_mt->region->cpp,
609278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace       dst_mt, dst_x, dst_y, dst_mt->region->pitch * dst_mt->region->cpp,
610278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace       width, height);
611278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
612278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   if (!intelEmitCopyBlit(intel,
613278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			  dst_mt->region->cpp,
614278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			  src_mt->region->pitch, src_mt->region->bo,
615278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			  0, src_mt->region->tiling,
616278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			  dst_mt->region->pitch, dst_mt->region->bo,
617278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			  0, dst_mt->region->tiling,
618278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			  src_x, src_y,
619278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			  dst_x, dst_y,
620278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			  width, height,
621278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			  GL_COPY)) {
622278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
623278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace      fallback_debug("miptree validate blit for %s failed\n",
624278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace		     _mesa_get_format_name(format));
625278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace      void *dst = intel_region_map(intel, dst_mt->region, GL_MAP_WRITE_BIT);
626278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace      void *src = intel_region_map(intel, src_mt->region, GL_MAP_READ_BIT);
627278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
628278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace      _mesa_copy_rect(dst,
629278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace		      dst_mt->cpp,
630278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace		      dst_mt->region->pitch,
631278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace		      dst_x, dst_y,
632278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace		      width, height,
633278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace		      src, src_mt->region->pitch,
634278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace		      src_x, src_y);
635278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
636278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace      intel_region_unmap(intel, dst_mt->region);
637278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace      intel_region_unmap(intel, src_mt->region);
638278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   }
6393eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace
6403eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace   if (src_mt->stencil_mt) {
6413eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace      intel_miptree_copy_slice(intel,
6423eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace                               dst_mt->stencil_mt, src_mt->stencil_mt,
6433eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace                               level, face, depth);
6443eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace   }
645278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace}
646278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
64777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt/**
648b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt * Copies the image's current data to the given miptree, and associates that
649b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt * miptree with the image.
65077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt */
65177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtvoid
652b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholtintel_miptree_copy_teximage(struct intel_context *intel,
653b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt			    struct intel_texture_image *intelImage,
654b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt			    struct intel_mipmap_tree *dst_mt)
65577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
656b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt   struct intel_mipmap_tree *src_mt = intelImage->mt;
657b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt   int level = intelImage->base.Base.Level;
658b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt   int face = intelImage->base.Base.Face;
659d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   GLuint depth = intelImage->base.Base.Depth;
660b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt
661278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   for (int slice = 0; slice < depth; slice++) {
662278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace      intel_miptree_copy_slice(intel, dst_mt, src_mt, level, face, slice);
66377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   }
66477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
665b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt   intel_miptree_reference(&intelImage->mt, dst_mt);
66677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
667278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
668a2e44b0813e956440c451c107cf5564b56cbe98eChad Versacebool
669ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berryintel_miptree_alloc_mcs(struct intel_context *intel,
670ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry                        struct intel_mipmap_tree *mt,
671ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry                        GLuint num_samples)
672ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry{
673ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry   assert(mt->mcs_mt == NULL);
674ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry   assert(intel->gen >= 7); /* MCS only used on Gen7+ */
675ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry   assert(num_samples == 4); /* TODO: support 8x MSAA */
676ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry
677ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry   /* From the Ivy Bridge PRM, Vol4 Part1 p76, "MCS Base Address":
678ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    *
679ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    *     "The MCS surface must be stored as Tile Y."
680ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    *
681ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    * We set msaa_format to INTEL_MSAA_LAYOUT_CMS to force
682ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    * intel_miptree_create() to use Y tiling.  msaa_format is otherwise
683ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    * ignored for the MCS miptree.
684ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    */
685ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry   mt->mcs_mt = intel_miptree_create(intel,
686ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry                                     mt->target,
687ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry                                     MESA_FORMAT_A8,
688ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry                                     mt->first_level,
689ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry                                     mt->last_level,
690ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry                                     mt->width0,
691ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry                                     mt->height0,
692ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry                                     mt->depth0,
693ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry                                     true,
694ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry                                     0 /* num_samples */,
695ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry                                     INTEL_MSAA_LAYOUT_CMS);
696ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry
697ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry   /* From the Ivy Bridge PRM, Vol 2 Part 1 p326:
698ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    *
699ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    *     When MCS buffer is enabled and bound to MSRT, it is required that it
700ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    *     is cleared prior to any rendering.
701ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    *
702ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    * Since we don't use the MCS buffer for any purpose other than rendering,
703ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    * it makes sense to just clear it immediately upon allocation.
704ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    *
705ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    * Note: the clear value for MCS buffers is all 1's, so we memset to 0xff.
706ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    */
707ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry   void *data = intel_region_map(intel, mt->mcs_mt->region, 0);
708ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry   memset(data, 0xff, mt->mcs_mt->region->bo->size);
709ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry   intel_region_unmap(intel, mt->mcs_mt->region);
710ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry
711ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry   return mt->mcs_mt;
712ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry}
713ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry
714ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berrybool
715a2e44b0813e956440c451c107cf5564b56cbe98eChad Versaceintel_miptree_alloc_hiz(struct intel_context *intel,
71619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry			struct intel_mipmap_tree *mt,
71719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry                        GLuint num_samples)
718a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace{
719a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace   assert(mt->hiz_mt == NULL);
7201bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   /* MSAA HiZ surfaces always use IMS layout. */
721a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace   mt->hiz_mt = intel_miptree_create(intel,
722a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace                                     mt->target,
723a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace                                     MESA_FORMAT_X8_Z24,
724a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace                                     mt->first_level,
725a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace                                     mt->last_level,
726a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace                                     mt->width0,
727a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace                                     mt->height0,
728a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace                                     mt->depth0,
72919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry                                     true,
730455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry                                     num_samples,
7311bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry                                     INTEL_MSAA_LAYOUT_IMS);
7323d760664e6349c72624aa6d54d40df0233995c8eChad Versace
7333d760664e6349c72624aa6d54d40df0233995c8eChad Versace   if (!mt->hiz_mt)
7343d760664e6349c72624aa6d54d40df0233995c8eChad Versace      return false;
7353d760664e6349c72624aa6d54d40df0233995c8eChad Versace
7363d760664e6349c72624aa6d54d40df0233995c8eChad Versace   /* Mark that all slices need a HiZ resolve. */
7373d760664e6349c72624aa6d54d40df0233995c8eChad Versace   struct intel_resolve_map *head = &mt->hiz_map;
7383d760664e6349c72624aa6d54d40df0233995c8eChad Versace   for (int level = mt->first_level; level <= mt->last_level; ++level) {
7393d760664e6349c72624aa6d54d40df0233995c8eChad Versace      for (int layer = 0; layer < mt->level[level].depth; ++layer) {
7403d760664e6349c72624aa6d54d40df0233995c8eChad Versace	 head->next = malloc(sizeof(*head->next));
7413d760664e6349c72624aa6d54d40df0233995c8eChad Versace	 head->next->prev = head;
7423d760664e6349c72624aa6d54d40df0233995c8eChad Versace	 head->next->next = NULL;
7433d760664e6349c72624aa6d54d40df0233995c8eChad Versace	 head = head->next;
7443d760664e6349c72624aa6d54d40df0233995c8eChad Versace
7453d760664e6349c72624aa6d54d40df0233995c8eChad Versace	 head->level = level;
7463d760664e6349c72624aa6d54d40df0233995c8eChad Versace	 head->layer = layer;
747072634da4a6ee5eafb5f5df26ca5f988209e6d40Eric Anholt	 head->need = GEN6_HIZ_OP_HIZ_RESOLVE;
7483d760664e6349c72624aa6d54d40df0233995c8eChad Versace      }
7493d760664e6349c72624aa6d54d40df0233995c8eChad Versace   }
7503d760664e6349c72624aa6d54d40df0233995c8eChad Versace
7513d760664e6349c72624aa6d54d40df0233995c8eChad Versace   return true;
752a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace}
7532945abea338031cbe90665df60152982bfca6177Chad Versace
7542945abea338031cbe90665df60152982bfca6177Chad Versacevoid
7552945abea338031cbe90665df60152982bfca6177Chad Versaceintel_miptree_slice_set_needs_hiz_resolve(struct intel_mipmap_tree *mt,
7562945abea338031cbe90665df60152982bfca6177Chad Versace					  uint32_t level,
7572945abea338031cbe90665df60152982bfca6177Chad Versace					  uint32_t layer)
7582945abea338031cbe90665df60152982bfca6177Chad Versace{
7592945abea338031cbe90665df60152982bfca6177Chad Versace   intel_miptree_check_level_layer(mt, level, layer);
7602945abea338031cbe90665df60152982bfca6177Chad Versace
7612945abea338031cbe90665df60152982bfca6177Chad Versace   if (!mt->hiz_mt)
7622945abea338031cbe90665df60152982bfca6177Chad Versace      return;
7632945abea338031cbe90665df60152982bfca6177Chad Versace
7642945abea338031cbe90665df60152982bfca6177Chad Versace   intel_resolve_map_set(&mt->hiz_map,
765072634da4a6ee5eafb5f5df26ca5f988209e6d40Eric Anholt			 level, layer, GEN6_HIZ_OP_HIZ_RESOLVE);
7662945abea338031cbe90665df60152982bfca6177Chad Versace}
7672945abea338031cbe90665df60152982bfca6177Chad Versace
7682945abea338031cbe90665df60152982bfca6177Chad Versace
7692945abea338031cbe90665df60152982bfca6177Chad Versacevoid
7702945abea338031cbe90665df60152982bfca6177Chad Versaceintel_miptree_slice_set_needs_depth_resolve(struct intel_mipmap_tree *mt,
7712945abea338031cbe90665df60152982bfca6177Chad Versace                                            uint32_t level,
7722945abea338031cbe90665df60152982bfca6177Chad Versace                                            uint32_t layer)
7732945abea338031cbe90665df60152982bfca6177Chad Versace{
7742945abea338031cbe90665df60152982bfca6177Chad Versace   intel_miptree_check_level_layer(mt, level, layer);
7752945abea338031cbe90665df60152982bfca6177Chad Versace
7762945abea338031cbe90665df60152982bfca6177Chad Versace   if (!mt->hiz_mt)
7772945abea338031cbe90665df60152982bfca6177Chad Versace      return;
7782945abea338031cbe90665df60152982bfca6177Chad Versace
7792945abea338031cbe90665df60152982bfca6177Chad Versace   intel_resolve_map_set(&mt->hiz_map,
780072634da4a6ee5eafb5f5df26ca5f988209e6d40Eric Anholt			 level, layer, GEN6_HIZ_OP_DEPTH_RESOLVE);
7812945abea338031cbe90665df60152982bfca6177Chad Versace}
7822945abea338031cbe90665df60152982bfca6177Chad Versace
7832945abea338031cbe90665df60152982bfca6177Chad Versacestatic bool
7842945abea338031cbe90665df60152982bfca6177Chad Versaceintel_miptree_slice_resolve(struct intel_context *intel,
7852945abea338031cbe90665df60152982bfca6177Chad Versace			    struct intel_mipmap_tree *mt,
7862945abea338031cbe90665df60152982bfca6177Chad Versace			    uint32_t level,
7872945abea338031cbe90665df60152982bfca6177Chad Versace			    uint32_t layer,
78854308f78a2f8675bfd854761f9cd8a6b71e119d0Eric Anholt			    enum gen6_hiz_op need)
7892945abea338031cbe90665df60152982bfca6177Chad Versace{
7902945abea338031cbe90665df60152982bfca6177Chad Versace   intel_miptree_check_level_layer(mt, level, layer);
7912945abea338031cbe90665df60152982bfca6177Chad Versace
7922945abea338031cbe90665df60152982bfca6177Chad Versace   struct intel_resolve_map *item =
7932945abea338031cbe90665df60152982bfca6177Chad Versace	 intel_resolve_map_get(&mt->hiz_map, level, layer);
7942945abea338031cbe90665df60152982bfca6177Chad Versace
7952945abea338031cbe90665df60152982bfca6177Chad Versace   if (!item || item->need != need)
7962945abea338031cbe90665df60152982bfca6177Chad Versace      return false;
7972945abea338031cbe90665df60152982bfca6177Chad Versace
79854308f78a2f8675bfd854761f9cd8a6b71e119d0Eric Anholt   intel_hiz_exec(intel, mt, level, layer, need);
7992945abea338031cbe90665df60152982bfca6177Chad Versace   intel_resolve_map_remove(item);
8002945abea338031cbe90665df60152982bfca6177Chad Versace   return true;
8012945abea338031cbe90665df60152982bfca6177Chad Versace}
8022945abea338031cbe90665df60152982bfca6177Chad Versace
8032945abea338031cbe90665df60152982bfca6177Chad Versacebool
8042945abea338031cbe90665df60152982bfca6177Chad Versaceintel_miptree_slice_resolve_hiz(struct intel_context *intel,
8052945abea338031cbe90665df60152982bfca6177Chad Versace				struct intel_mipmap_tree *mt,
8062945abea338031cbe90665df60152982bfca6177Chad Versace				uint32_t level,
8072945abea338031cbe90665df60152982bfca6177Chad Versace				uint32_t layer)
8082945abea338031cbe90665df60152982bfca6177Chad Versace{
8092945abea338031cbe90665df60152982bfca6177Chad Versace   return intel_miptree_slice_resolve(intel, mt, level, layer,
81054308f78a2f8675bfd854761f9cd8a6b71e119d0Eric Anholt				      GEN6_HIZ_OP_HIZ_RESOLVE);
8112945abea338031cbe90665df60152982bfca6177Chad Versace}
8122945abea338031cbe90665df60152982bfca6177Chad Versace
8132945abea338031cbe90665df60152982bfca6177Chad Versacebool
8142945abea338031cbe90665df60152982bfca6177Chad Versaceintel_miptree_slice_resolve_depth(struct intel_context *intel,
8152945abea338031cbe90665df60152982bfca6177Chad Versace				  struct intel_mipmap_tree *mt,
8162945abea338031cbe90665df60152982bfca6177Chad Versace				  uint32_t level,
8172945abea338031cbe90665df60152982bfca6177Chad Versace				  uint32_t layer)
8182945abea338031cbe90665df60152982bfca6177Chad Versace{
8192945abea338031cbe90665df60152982bfca6177Chad Versace   return intel_miptree_slice_resolve(intel, mt, level, layer,
82054308f78a2f8675bfd854761f9cd8a6b71e119d0Eric Anholt				      GEN6_HIZ_OP_DEPTH_RESOLVE);
8212945abea338031cbe90665df60152982bfca6177Chad Versace}
8222945abea338031cbe90665df60152982bfca6177Chad Versace
8232945abea338031cbe90665df60152982bfca6177Chad Versacestatic bool
8242945abea338031cbe90665df60152982bfca6177Chad Versaceintel_miptree_all_slices_resolve(struct intel_context *intel,
8252945abea338031cbe90665df60152982bfca6177Chad Versace				 struct intel_mipmap_tree *mt,
82654308f78a2f8675bfd854761f9cd8a6b71e119d0Eric Anholt				 enum gen6_hiz_op need)
8272945abea338031cbe90665df60152982bfca6177Chad Versace{
8282945abea338031cbe90665df60152982bfca6177Chad Versace   bool did_resolve = false;
8290ed11e333147e280208d9d0b3ff3f39970547643Anuj Phogat   struct intel_resolve_map *i, *next;
8302945abea338031cbe90665df60152982bfca6177Chad Versace
8310ed11e333147e280208d9d0b3ff3f39970547643Anuj Phogat   for (i = mt->hiz_map.next; i; i = next) {
832a0a0a909f21acfcddaab603079ba98266e8daf0eKenneth Graunke      next = i->next;
8332945abea338031cbe90665df60152982bfca6177Chad Versace      if (i->need != need)
8342945abea338031cbe90665df60152982bfca6177Chad Versace	 continue;
83554308f78a2f8675bfd854761f9cd8a6b71e119d0Eric Anholt
83654308f78a2f8675bfd854761f9cd8a6b71e119d0Eric Anholt      intel_hiz_exec(intel, mt, i->level, i->layer, need);
8372945abea338031cbe90665df60152982bfca6177Chad Versace      intel_resolve_map_remove(i);
8382945abea338031cbe90665df60152982bfca6177Chad Versace      did_resolve = true;
8392945abea338031cbe90665df60152982bfca6177Chad Versace   }
8402945abea338031cbe90665df60152982bfca6177Chad Versace
8412945abea338031cbe90665df60152982bfca6177Chad Versace   return did_resolve;
8422945abea338031cbe90665df60152982bfca6177Chad Versace}
8432945abea338031cbe90665df60152982bfca6177Chad Versace
8442945abea338031cbe90665df60152982bfca6177Chad Versacebool
8452945abea338031cbe90665df60152982bfca6177Chad Versaceintel_miptree_all_slices_resolve_hiz(struct intel_context *intel,
8462945abea338031cbe90665df60152982bfca6177Chad Versace				     struct intel_mipmap_tree *mt)
8472945abea338031cbe90665df60152982bfca6177Chad Versace{
8482945abea338031cbe90665df60152982bfca6177Chad Versace   return intel_miptree_all_slices_resolve(intel, mt,
84954308f78a2f8675bfd854761f9cd8a6b71e119d0Eric Anholt					   GEN6_HIZ_OP_HIZ_RESOLVE);
8502945abea338031cbe90665df60152982bfca6177Chad Versace}
8512945abea338031cbe90665df60152982bfca6177Chad Versace
8522945abea338031cbe90665df60152982bfca6177Chad Versacebool
8532945abea338031cbe90665df60152982bfca6177Chad Versaceintel_miptree_all_slices_resolve_depth(struct intel_context *intel,
8542945abea338031cbe90665df60152982bfca6177Chad Versace				       struct intel_mipmap_tree *mt)
8552945abea338031cbe90665df60152982bfca6177Chad Versace{
8562945abea338031cbe90665df60152982bfca6177Chad Versace   return intel_miptree_all_slices_resolve(intel, mt,
85754308f78a2f8675bfd854761f9cd8a6b71e119d0Eric Anholt					   GEN6_HIZ_OP_DEPTH_RESOLVE);
8582945abea338031cbe90665df60152982bfca6177Chad Versace}
859b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt
860baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholtstatic void
861baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholtintel_miptree_map_gtt(struct intel_context *intel,
862baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		      struct intel_mipmap_tree *mt,
863baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		      struct intel_miptree_map *map,
864baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		      unsigned int level, unsigned int slice)
865b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt{
866b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt   unsigned int bw, bh;
867b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt   void *base;
868b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt   unsigned int image_x, image_y;
869baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   int x = map->x;
870baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   int y = map->y;
871221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt
872b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt   /* For compressed formats, the stride is the number of bytes per
873b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt    * row of blocks.  intel_miptree_get_image_offset() already does
874b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt    * the divide.
875b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt    */
876b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt   _mesa_get_format_block_size(mt->format, &bw, &bh);
877b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt   assert(y % bh == 0);
878b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt   y /= bh;
879b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt
880baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   base = intel_region_map(intel, mt->region, map->mode);
881b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt
882cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat   if (base == NULL)
883cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      map->ptr = NULL;
884cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat   else {
885cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      /* Note that in the case of cube maps, the caller must have passed the
886cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat       * slice number referencing the face.
887cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      */
888cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      intel_miptree_get_image_offset(mt, level, 0, slice, &image_x, &image_y);
889cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      x += image_x;
890cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      y += image_y;
891cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat
892cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      map->stride = mt->region->pitch * mt->cpp;
893cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      map->ptr = base + y * map->stride + x * mt->cpp;
894cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat   }
895221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt
896b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt   DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __FUNCTION__,
897221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt       map->x, map->y, map->w, map->h,
898b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt       mt, _mesa_get_format_name(mt->format),
899221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt       x, y, map->ptr, map->stride);
900b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt}
901b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt
902baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholtstatic void
903baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholtintel_miptree_unmap_gtt(struct intel_context *intel,
904baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt			struct intel_mipmap_tree *mt,
905baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt			struct intel_miptree_map *map,
906baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt			unsigned int level,
907baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt			unsigned int slice)
908baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt{
909baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   intel_region_unmap(intel, mt->region);
910baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt}
911baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt
912b48c3bca87b30003f9e117d299011380e743aec9Eric Anholtstatic void
9135655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholtintel_miptree_map_blit(struct intel_context *intel,
9145655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt		       struct intel_mipmap_tree *mt,
9155655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt		       struct intel_miptree_map *map,
9165655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt		       unsigned int level, unsigned int slice)
9175655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt{
9185655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   unsigned int image_x, image_y;
9195655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   int x = map->x;
9205655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   int y = map->y;
9215655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   int ret;
9225655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
9235655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   /* The blitter requires the pitch to be aligned to 4. */
9245655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   map->stride = ALIGN(map->w * mt->region->cpp, 4);
9255655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
9265655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   map->bo = drm_intel_bo_alloc(intel->bufmgr, "intel_miptree_map_blit() temp",
9275655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt				map->stride * map->h, 4096);
9285655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   if (!map->bo) {
9295655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt      fprintf(stderr, "Failed to allocate blit temporary\n");
9305655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt      goto fail;
9315655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   }
9325655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
9335655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   intel_miptree_get_image_offset(mt, level, 0, slice, &image_x, &image_y);
9345655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   x += image_x;
9355655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   y += image_y;
9365655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
9375655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   if (!intelEmitCopyBlit(intel,
9385655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			  mt->region->cpp,
9395655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			  mt->region->pitch, mt->region->bo,
9405655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			  0, mt->region->tiling,
9415655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			  map->stride / mt->region->cpp, map->bo,
9425655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			  0, I915_TILING_NONE,
9435655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			  x, y,
9445655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			  0, 0,
9455655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			  map->w, map->h,
9465655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			  GL_COPY)) {
9475655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt      fprintf(stderr, "Failed to blit\n");
9485655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt      goto fail;
9495655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   }
9505655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
9515655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   intel_batchbuffer_flush(intel);
9525655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   ret = drm_intel_bo_map(map->bo, (map->mode & GL_MAP_WRITE_BIT) != 0);
9535655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   if (ret) {
9545655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt      fprintf(stderr, "Failed to map blit temporary\n");
9555655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt      goto fail;
9565655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   }
9575655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
9585655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   map->ptr = map->bo->virtual;
9595655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
9605655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __FUNCTION__,
9615655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt       map->x, map->y, map->w, map->h,
9625655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt       mt, _mesa_get_format_name(mt->format),
9635655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt       x, y, map->ptr, map->stride);
9645655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
9655655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   return;
9665655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
9675655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholtfail:
9685655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   drm_intel_bo_unreference(map->bo);
9695655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   map->ptr = NULL;
9705655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   map->stride = 0;
9715655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt}
9725655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
9735655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholtstatic void
9745655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholtintel_miptree_unmap_blit(struct intel_context *intel,
9755655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			 struct intel_mipmap_tree *mt,
9765655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			 struct intel_miptree_map *map,
9775655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			 unsigned int level,
9785655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			 unsigned int slice)
9795655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt{
9805655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   assert(!(map->mode & GL_MAP_WRITE_BIT));
9815655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
9825655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   drm_intel_bo_unmap(map->bo);
9835655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   drm_intel_bo_unreference(map->bo);
9845655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt}
9855655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
9865655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholtstatic void
987b48c3bca87b30003f9e117d299011380e743aec9Eric Anholtintel_miptree_map_s8(struct intel_context *intel,
988b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt		     struct intel_mipmap_tree *mt,
989b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt		     struct intel_miptree_map *map,
990b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt		     unsigned int level, unsigned int slice)
991b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt{
992b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   map->stride = map->w;
993b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   map->buffer = map->ptr = malloc(map->stride * map->h);
994b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   if (!map->buffer)
995b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      return;
996b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
997b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   /* One of either READ_BIT or WRITE_BIT or both is set.  READ_BIT implies no
998b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt    * INVALIDATE_RANGE_BIT.  WRITE_BIT needs the original values read in unless
999b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt    * invalidate is set, since we'll be writing the whole rectangle from our
1000b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt    * temporary buffer back out.
1001b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt    */
1002b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
1003b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      uint8_t *untiled_s8_map = map->ptr;
1004b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      uint8_t *tiled_s8_map = intel_region_map(intel, mt->region,
1005b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt					       GL_MAP_READ_BIT);
1006b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      unsigned int image_x, image_y;
1007b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
1008b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      intel_miptree_get_image_offset(mt, level, 0, slice, &image_x, &image_y);
1009b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
1010b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      for (uint32_t y = 0; y < map->h; y++) {
1011b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	 for (uint32_t x = 0; x < map->w; x++) {
1012b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	    ptrdiff_t offset = intel_offset_S8(mt->region->pitch,
1013b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	                                       x + image_x + map->x,
1014f172eae8b23d0612865895c52af745021ae20a4cDaniel Vetter	                                       y + image_y + map->y,
1015f172eae8b23d0612865895c52af745021ae20a4cDaniel Vetter					       intel->has_swizzling);
1016b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	    untiled_s8_map[y * map->w + x] = tiled_s8_map[offset];
1017b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	 }
1018b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      }
1019b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
1020b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      intel_region_unmap(intel, mt->region);
1021b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
1022b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      DBG("%s: %d,%d %dx%d from mt %p %d,%d = %p/%d\n", __FUNCTION__,
1023b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	  map->x, map->y, map->w, map->h,
1024b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	  mt, map->x + image_x, map->y + image_y, map->ptr, map->stride);
1025b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   } else {
1026b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __FUNCTION__,
1027b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	  map->x, map->y, map->w, map->h,
1028b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	  mt, map->ptr, map->stride);
1029b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   }
1030b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt}
1031b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
1032b48c3bca87b30003f9e117d299011380e743aec9Eric Anholtstatic void
1033b48c3bca87b30003f9e117d299011380e743aec9Eric Anholtintel_miptree_unmap_s8(struct intel_context *intel,
1034b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt		       struct intel_mipmap_tree *mt,
1035b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt		       struct intel_miptree_map *map,
1036b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt		       unsigned int level,
1037b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt		       unsigned int slice)
1038b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt{
1039b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   if (map->mode & GL_MAP_WRITE_BIT) {
1040b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      unsigned int image_x, image_y;
1041b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      uint8_t *untiled_s8_map = map->ptr;
1042b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      uint8_t *tiled_s8_map = intel_region_map(intel, mt->region, map->mode);
1043b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
1044b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      intel_miptree_get_image_offset(mt, level, 0, slice, &image_x, &image_y);
1045b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
1046b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      for (uint32_t y = 0; y < map->h; y++) {
1047b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	 for (uint32_t x = 0; x < map->w; x++) {
1048b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	    ptrdiff_t offset = intel_offset_S8(mt->region->pitch,
1049b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	                                       x + map->x,
1050f172eae8b23d0612865895c52af745021ae20a4cDaniel Vetter	                                       y + map->y,
1051f172eae8b23d0612865895c52af745021ae20a4cDaniel Vetter					       intel->has_swizzling);
1052b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	    tiled_s8_map[offset] = untiled_s8_map[y * map->w + x];
1053b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	 }
1054b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      }
1055b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
1056b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      intel_region_unmap(intel, mt->region);
1057b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   }
1058b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
1059b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   free(map->buffer);
1060b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt}
1061b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
1062a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versacestatic void
1063a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versaceintel_miptree_map_etc1(struct intel_context *intel,
1064a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace                       struct intel_mipmap_tree *mt,
1065a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace                       struct intel_miptree_map *map,
1066a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace                       unsigned int level,
1067a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace                       unsigned int slice)
1068a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace{
1069a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   /* For justification of these invariants,
1070a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace    * see intel_mipmap_tree:wraps_etc1.
1071a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace    */
1072a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   assert(mt->wraps_etc1);
1073a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   assert(mt->format == MESA_FORMAT_RGBX8888_REV);
1074a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace
1075a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   /* From the GL_OES_compressed_ETC1_RGB8_texture spec:
1076a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace    *   INVALID_OPERATION is generated by CompressedTexSubImage2D,
1077a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace    *   TexSubImage2D, or CopyTexSubImage2D if the texture image <level>
1078a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace    *   bound to <target> has internal format ETC1_RGB8_OES.
1079a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace    *
1080a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace    * This implies that intel_miptree_map_etc1() can only be called from
1081a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace    * glCompressedTexImage2D, and hence the assertions below hold.
1082a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace    */
1083a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   assert(map->mode & GL_MAP_WRITE_BIT);
1084a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   assert(map->mode & GL_MAP_INVALIDATE_RANGE_BIT);
1085a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   assert(map->x == 0);
1086a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   assert(map->y == 0);
1087a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace
1088a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   /* Each ETC1 block contains 4x4 pixels in 8 bytes. */
1089a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   map->stride = 2 * map->w;
1090a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   map->buffer = map->ptr = malloc(map->stride * map->h);
1091a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace}
1092a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace
1093a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versacestatic void
1094a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versaceintel_miptree_unmap_etc1(struct intel_context *intel,
1095a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace                         struct intel_mipmap_tree *mt,
1096a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace                         struct intel_miptree_map *map,
1097a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace                         unsigned int level,
1098a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace                         unsigned int slice)
1099a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace{
1100a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   uint32_t image_x;
1101a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   uint32_t image_y;
1102a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   intel_miptree_get_image_offset(mt, level, 0, slice, &image_x, &image_y);
1103a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace
1104a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   uint8_t *xbgr = intel_region_map(intel, mt->region, map->mode)
1105a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace                 + image_y * mt->region->pitch * mt->region->cpp
1106a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace                 + image_x * mt->region->cpp;
1107a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace
1108a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   _mesa_etc1_unpack_rgba8888(xbgr, mt->region->pitch * mt->region->cpp,
1109a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace                              map->ptr, map->stride,
1110a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace                              map->w, map->h);
1111a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace
1112a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   intel_region_unmap(intel, mt->region);
1113a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   free(map->buffer);
1114a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace}
1115a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace
1116ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt/**
1117ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt * Mapping function for packed depth/stencil miptrees backed by real separate
1118ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt * miptrees for depth and stencil.
1119ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt *
1120ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt * On gen7, and to support HiZ pre-gen7, we have to have the stencil buffer
1121ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt * separate from the depth buffer.  Yet at the GL API level, we have to expose
1122ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt * packed depth/stencil textures and FBO attachments, and Mesa core expects to
1123ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt * be able to map that memory for texture storage and glReadPixels-type
1124ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt * operations.  We give Mesa core that access by mallocing a temporary and
1125ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt * copying the data between the actual backing store and the temporary.
1126ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt */
1127ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholtstatic void
1128ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholtintel_miptree_map_depthstencil(struct intel_context *intel,
1129ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt			       struct intel_mipmap_tree *mt,
1130ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt			       struct intel_miptree_map *map,
1131ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt			       unsigned int level, unsigned int slice)
1132ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt{
1133ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   struct intel_mipmap_tree *z_mt = mt;
1134ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   struct intel_mipmap_tree *s_mt = mt->stencil_mt;
1135353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt   bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z32_FLOAT;
1136353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt   int packed_bpp = map_z32f_x24s8 ? 8 : 4;
1137ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1138ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   map->stride = map->w * packed_bpp;
1139ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   map->buffer = map->ptr = malloc(map->stride * map->h);
1140ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   if (!map->buffer)
1141ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      return;
1142ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1143ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   /* One of either READ_BIT or WRITE_BIT or both is set.  READ_BIT implies no
1144ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt    * INVALIDATE_RANGE_BIT.  WRITE_BIT needs the original values read in unless
1145ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt    * invalidate is set, since we'll be writing the whole rectangle from our
1146ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt    * temporary buffer back out.
1147ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt    */
1148ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
1149ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      uint32_t *packed_map = map->ptr;
1150ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      uint8_t *s_map = intel_region_map(intel, s_mt->region, GL_MAP_READ_BIT);
1151ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      uint32_t *z_map = intel_region_map(intel, z_mt->region, GL_MAP_READ_BIT);
1152ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      unsigned int s_image_x, s_image_y;
1153ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      unsigned int z_image_x, z_image_y;
1154ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1155ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_miptree_get_image_offset(s_mt, level, 0, slice,
1156ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				     &s_image_x, &s_image_y);
1157ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_miptree_get_image_offset(z_mt, level, 0, slice,
1158ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				     &z_image_x, &z_image_y);
1159ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1160ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      for (uint32_t y = 0; y < map->h; y++) {
1161ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	 for (uint32_t x = 0; x < map->w; x++) {
1162ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	    int map_x = map->x + x, map_y = map->y + y;
1163ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	    ptrdiff_t s_offset = intel_offset_S8(s_mt->region->pitch,
1164ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt						 map_x + s_image_x,
1165f172eae8b23d0612865895c52af745021ae20a4cDaniel Vetter						 map_y + s_image_y,
1166f172eae8b23d0612865895c52af745021ae20a4cDaniel Vetter						 intel->has_swizzling);
1167ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	    ptrdiff_t z_offset = ((map_y + z_image_y) * z_mt->region->pitch +
1168ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				  (map_x + z_image_x));
1169ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	    uint8_t s = s_map[s_offset];
1170ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	    uint32_t z = z_map[z_offset];
1171ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1172353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	    if (map_z32f_x24s8) {
1173353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	       packed_map[(y * map->w + x) * 2 + 0] = z;
1174353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	       packed_map[(y * map->w + x) * 2 + 1] = s;
1175353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	    } else {
1176353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	       packed_map[y * map->w + x] = (s << 24) | (z & 0x00ffffff);
1177353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	    }
1178ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	 }
1179ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      }
1180ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1181ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_region_unmap(intel, s_mt->region);
1182ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_region_unmap(intel, z_mt->region);
1183ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1184ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      DBG("%s: %d,%d %dx%d from z mt %p %d,%d, s mt %p %d,%d = %p/%d\n",
1185ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  __FUNCTION__,
1186ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  map->x, map->y, map->w, map->h,
1187ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  z_mt, map->x + z_image_x, map->y + z_image_y,
1188ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  s_mt, map->x + s_image_x, map->y + s_image_y,
1189ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  map->ptr, map->stride);
1190ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   } else {
1191ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __FUNCTION__,
1192ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  map->x, map->y, map->w, map->h,
1193ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  mt, map->ptr, map->stride);
1194ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   }
1195ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt}
1196ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1197ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholtstatic void
1198ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholtintel_miptree_unmap_depthstencil(struct intel_context *intel,
1199ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				 struct intel_mipmap_tree *mt,
1200ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				 struct intel_miptree_map *map,
1201ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				 unsigned int level,
1202ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				 unsigned int slice)
1203ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt{
1204ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   struct intel_mipmap_tree *z_mt = mt;
1205ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   struct intel_mipmap_tree *s_mt = mt->stencil_mt;
1206353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt   bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z32_FLOAT;
1207ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1208ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   if (map->mode & GL_MAP_WRITE_BIT) {
1209ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      uint32_t *packed_map = map->ptr;
1210ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      uint8_t *s_map = intel_region_map(intel, s_mt->region, map->mode);
1211ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      uint32_t *z_map = intel_region_map(intel, z_mt->region, map->mode);
1212ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      unsigned int s_image_x, s_image_y;
1213ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      unsigned int z_image_x, z_image_y;
1214ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1215ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_miptree_get_image_offset(s_mt, level, 0, slice,
1216ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				     &s_image_x, &s_image_y);
1217ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_miptree_get_image_offset(z_mt, level, 0, slice,
1218ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				     &z_image_x, &z_image_y);
1219ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1220ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      for (uint32_t y = 0; y < map->h; y++) {
1221ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	 for (uint32_t x = 0; x < map->w; x++) {
1222ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	    ptrdiff_t s_offset = intel_offset_S8(s_mt->region->pitch,
1223ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt						 x + s_image_x + map->x,
1224f172eae8b23d0612865895c52af745021ae20a4cDaniel Vetter						 y + s_image_y + map->y,
1225f172eae8b23d0612865895c52af745021ae20a4cDaniel Vetter						 intel->has_swizzling);
1226ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	    ptrdiff_t z_offset = ((y + z_image_y) * z_mt->region->pitch +
1227ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				  (x + z_image_x));
1228ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1229353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	    if (map_z32f_x24s8) {
1230353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	       z_map[z_offset] = packed_map[(y * map->w + x) * 2 + 0];
1231353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	       s_map[s_offset] = packed_map[(y * map->w + x) * 2 + 1];
1232353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	    } else {
1233353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	       uint32_t packed = packed_map[y * map->w + x];
1234353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	       s_map[s_offset] = packed >> 24;
1235353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	       z_map[z_offset] = packed;
1236353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	    }
1237ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	 }
1238ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      }
1239ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1240ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_region_unmap(intel, s_mt->region);
1241ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_region_unmap(intel, z_mt->region);
1242ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1243ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      DBG("%s: %d,%d %dx%d from z mt %p (%s) %d,%d, s mt %p %d,%d = %p/%d\n",
1244ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  __FUNCTION__,
1245ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  map->x, map->y, map->w, map->h,
1246ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  z_mt, _mesa_get_format_name(z_mt->format),
1247ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  map->x + z_image_x, map->y + z_image_y,
1248ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  s_mt, map->x + s_image_x, map->y + s_image_y,
1249ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  map->ptr, map->stride);
1250ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   }
1251ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1252ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   free(map->buffer);
1253ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt}
1254ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1255baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholtvoid
1256baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholtintel_miptree_map(struct intel_context *intel,
1257baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  struct intel_mipmap_tree *mt,
1258baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  unsigned int level,
1259baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  unsigned int slice,
1260baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  unsigned int x,
1261baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  unsigned int y,
1262baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  unsigned int w,
1263baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  unsigned int h,
1264baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  GLbitfield mode,
1265baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  void **out_ptr,
1266baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  int *out_stride)
1267baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt{
1268baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   struct intel_miptree_map *map;
1269baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt
1270baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   map = calloc(1, sizeof(struct intel_miptree_map));
1271baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   if (!map){
1272baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt      *out_ptr = NULL;
1273baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt      *out_stride = 0;
1274baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt      return;
1275baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   }
1276baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt
1277baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   assert(!mt->level[level].slice[slice].map);
1278baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   mt->level[level].slice[slice].map = map;
1279baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   map->mode = mode;
1280baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   map->x = x;
1281baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   map->y = y;
1282baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   map->w = w;
1283baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   map->h = h;
1284baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt
1285baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   intel_miptree_slice_resolve_depth(intel, mt, level, slice);
1286baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   if (map->mode & GL_MAP_WRITE_BIT) {
1287baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt      intel_miptree_slice_set_needs_hiz_resolve(mt, level, slice);
1288baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   }
1289baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt
1290b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   if (mt->format == MESA_FORMAT_S8) {
1291b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      intel_miptree_map_s8(intel, mt, map, level, slice);
1292a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   } else if (mt->wraps_etc1) {
1293a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace      intel_miptree_map_etc1(intel, mt, map, level, slice);
1294ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   } else if (mt->stencil_mt) {
1295ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_miptree_map_depthstencil(intel, mt, map, level, slice);
129684e5f1c635899c657da58ca51d5e841354e9de9cEugeni Dodonov   } else if (intel->has_llc &&
12975655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt	      !(mode & GL_MAP_WRITE_BIT) &&
12985655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt	      !mt->compressed &&
12995655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt	      mt->region->tiling == I915_TILING_X) {
13005655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt      intel_miptree_map_blit(intel, mt, map, level, slice);
1301b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   } else {
1302b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      intel_miptree_map_gtt(intel, mt, map, level, slice);
1303b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   }
1304baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt
1305baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   *out_ptr = map->ptr;
1306baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   *out_stride = map->stride;
1307cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat
1308cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat   if (map->ptr == NULL) {
1309cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      mt->level[level].slice[slice].map = NULL;
1310cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      free(map);
1311cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat   }
1312baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt}
1313baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt
1314b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholtvoid
1315b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholtintel_miptree_unmap(struct intel_context *intel,
1316b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt		    struct intel_mipmap_tree *mt,
1317b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt		    unsigned int level,
1318b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt		    unsigned int slice)
1319b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt{
1320221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt   struct intel_miptree_map *map = mt->level[level].slice[slice].map;
1321221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt
1322221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt   if (!map)
1323221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt      return;
1324221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt
1325b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt   DBG("%s: mt %p (%s) level %d slice %d\n", __FUNCTION__,
1326b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt       mt, _mesa_get_format_name(mt->format), level, slice);
1327b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt
1328b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   if (mt->format == MESA_FORMAT_S8) {
1329b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      intel_miptree_unmap_s8(intel, mt, map, level, slice);
1330a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   } else if (mt->wraps_etc1) {
1331a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace      intel_miptree_unmap_etc1(intel, mt, map, level, slice);
1332ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   } else if (mt->stencil_mt) {
1333ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_miptree_unmap_depthstencil(intel, mt, map, level, slice);
13345655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   } else if (map->bo) {
13355655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt      intel_miptree_unmap_blit(intel, mt, map, level, slice);
1336b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   } else {
1337b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      intel_miptree_unmap_gtt(intel, mt, map, level, slice);
1338b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   }
1339221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt
1340221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt   mt->level[level].slice[slice].map = NULL;
1341221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt   free(map);
1342b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt}
1343