intel_mipmap_tree.c revision f4873babdc27f697df9f4642209eca21a02ac476
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
28f4873babdc27f697df9f4642209eca21a02ac476Chad Versace#include <GL/gl.h>
29f4873babdc27f697df9f4642209eca21a02ac476Chad Versace#include <GL/internal/dri_interface.h>
30f4873babdc27f697df9f4642209eca21a02ac476Chad Versace
313eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace#include "intel_batchbuffer.h"
3277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt#include "intel_context.h"
3377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt#include "intel_mipmap_tree.h"
3477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt#include "intel_regions.h"
352945abea338031cbe90665df60152982bfca6177Chad Versace#include "intel_resolve_map.h"
363eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace#include "intel_span.h"
373732d0a77d2cbae50874f5a4ebdb3d8f06021a57Brian Paul#include "intel_tex_layout.h"
386dcc398ac0837025cf60b4d6a056fa3b0a16466fEric Anholt#include "intel_tex.h"
39b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt#include "intel_blit.h"
403eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace
41ecadb51bbcb972a79f3ed79e65a7986b9396e757Brian Paul#include "main/enums.h"
42c374487a54aca2dd1053645092367c1cf0414ef7Eric Anholt#include "main/formats.h"
438b762ebd72c1bb1a9827a667ad55ba9516505adfBrian Paul#include "main/glformats.h"
44a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace#include "main/texcompress_etc.h"
45b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt#include "main/teximage.h"
4677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
4777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt#define FILE_DEBUG_FLAG DEBUG_MIPTREE
4877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
4977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtstatic GLenum
5077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholttarget_to_target(GLenum target)
5177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
5277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   switch (target) {
5377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
5477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
5577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
5677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
5777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
5877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
5977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      return GL_TEXTURE_CUBE_MAP_ARB;
6077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   default:
6177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      return target;
6277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   }
6377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
6477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
657e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace/**
667e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace * @param for_region Indicates that the caller is
677e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace *        intel_miptree_create_for_region(). If true, then do not create
687e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace *        \c stencil_mt.
697e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace */
706d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsbergstatic struct intel_mipmap_tree *
716d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsbergintel_miptree_create_internal(struct intel_context *intel,
726d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg			      GLenum target,
73d5809115b568d8b74f47316607dce0730964517aEric Anholt			      gl_format format,
745b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt			      GLuint first_level,
755b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt			      GLuint last_level,
766d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg			      GLuint width0,
776d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg			      GLuint height0,
787e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace			      GLuint depth0,
7919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry			      bool for_region,
80455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry                              GLuint num_samples,
811bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry                              enum intel_msaa_layout msaa_layout)
8277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
8377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
846dcc398ac0837025cf60b4d6a056fa3b0a16466fEric Anholt   int compress_byte = 0;
8577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
865b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt   DBG("%s target %s format %s level %d..%d <-- %p\n", __FUNCTION__,
8777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt       _mesa_lookup_enum_by_nr(target),
889a523a48af05118424714f0a34ca3dda6861186aEric Anholt       _mesa_get_format_name(format),
895b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt       first_level, last_level, mt);
9077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
916dcc398ac0837025cf60b4d6a056fa3b0a16466fEric Anholt   if (_mesa_is_format_compressed(format))
926dcc398ac0837025cf60b4d6a056fa3b0a16466fEric Anholt      compress_byte = intel_compressed_num_bytes(format);
936dcc398ac0837025cf60b4d6a056fa3b0a16466fEric Anholt
9477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->target = target_to_target(target);
95d5809115b568d8b74f47316607dce0730964517aEric Anholt   mt->format = format;
965b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt   mt->first_level = first_level;
975b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt   mt->last_level = last_level;
9877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->width0 = width0;
9977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->height0 = height0;
100f94fef83db10f0c9327bd3dd43510ad31c94d82aEric Anholt   mt->cpp = compress_byte ? compress_byte : _mesa_get_format_bytes(mt->format);
10119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   mt->num_samples = num_samples;
10277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->compressed = compress_byte ? 1 : 0;
1031bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   mt->msaa_layout = msaa_layout;
10477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->refcount = 1;
10577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
1061bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   /* array_spacing_lod0 is only used for non-IMS MSAA surfaces.  TODO: can we
1071bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry    * use it elsewhere?
108455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry    */
1091bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   switch (msaa_layout) {
1101bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_NONE:
1111bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_IMS:
1121bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      mt->array_spacing_lod0 = false;
1131bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
1141bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_UMS:
1151bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   case INTEL_MSAA_LAYOUT_CMS:
1161bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      mt->array_spacing_lod0 = true;
1171bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      break;
1181bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   }
119455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry
120d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   if (target == GL_TEXTURE_CUBE_MAP) {
121d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      assert(depth0 == 1);
122d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      mt->depth0 = 6;
123d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   } else {
124d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      mt->depth0 = depth0;
125d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   }
126d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace
1277e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace   if (!for_region &&
1287e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace       _mesa_is_depthstencil_format(_mesa_get_format_base_format(format)) &&
1295c9a55665d78b96bfb8ce8eab43b5558dd656a6dEric Anholt       (intel->must_use_separate_stencil ||
1305c9a55665d78b96bfb8ce8eab43b5558dd656a6dEric Anholt	(intel->has_separate_stencil &&
1315c9a55665d78b96bfb8ce8eab43b5558dd656a6dEric Anholt	 intel->vtbl.is_hiz_depth_format(intel, format)))) {
1321bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      /* MSAA stencil surfaces always use IMS layout. */
1331bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      enum intel_msaa_layout msaa_layout =
13497fc89c6cbaa3b5ef7f678d2dc2c7d5bbba05315Paul Berry         num_samples > 1 ? INTEL_MSAA_LAYOUT_IMS : INTEL_MSAA_LAYOUT_NONE;
1353eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace      mt->stencil_mt = intel_miptree_create(intel,
1363eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace                                            mt->target,
1373eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace                                            MESA_FORMAT_S8,
1383eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace                                            mt->first_level,
1393eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace                                            mt->last_level,
1403eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace                                            mt->width0,
1413eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace                                            mt->height0,
1423eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace                                            mt->depth0,
14319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry                                            true,
144455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry                                            num_samples,
1451bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry                                            msaa_layout);
1463eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace      if (!mt->stencil_mt) {
1473eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace	 intel_miptree_release(&mt);
1483eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace	 return NULL;
1493eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace      }
150fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt
151fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt      /* Fix up the Z miptree format for how we're splitting out separate
152fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt       * stencil.  Gen7 expects there to be no stencil bits in its depth buffer.
153fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt       */
154fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt      if (mt->format == MESA_FORMAT_S8_Z24) {
155fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt	 mt->format = MESA_FORMAT_X8_Z24;
1564790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt      } else if (mt->format == MESA_FORMAT_Z32_FLOAT_X24S8) {
1574790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt	 mt->format = MESA_FORMAT_Z32_FLOAT;
1584790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt	 mt->cpp = 4;
159fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt      } else {
1604790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt	 _mesa_problem(NULL, "Unknown format %s in separate stencil mt\n",
161fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt		       _mesa_get_format_name(mt->format));
162fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt      }
1633eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace   }
1643eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace
1654790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt   intel_get_texture_alignment_unit(intel, mt->format,
1664790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt				    &mt->align_w, &mt->align_h);
1674790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt
1684790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt#ifdef I915
1694790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt   (void) intel;
1704790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt   if (intel->is_945)
1714790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt      i945_miptree_layout(mt);
1724790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt   else
1734790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt      i915_miptree_layout(mt);
1744790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt#else
1754790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt   brw_miptree_layout(intel, mt);
1764790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt#endif
1774790c4ae2489b8c72e6d98f3f80b5a9e7f799203Eric Anholt
1786d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg   return mt;
1796d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg}
1806d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg
18147a7535f413d6467082de224f64eecc046227406Brian Paul
1826d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsbergstruct intel_mipmap_tree *
1836d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsbergintel_miptree_create(struct intel_context *intel,
1846d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg		     GLenum target,
185d5809115b568d8b74f47316607dce0730964517aEric Anholt		     gl_format format,
1865b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt		     GLuint first_level,
1875b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt		     GLuint last_level,
1886d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg		     GLuint width0,
1896d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg		     GLuint height0,
190f94fef83db10f0c9327bd3dd43510ad31c94d82aEric Anholt		     GLuint depth0,
19119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry		     bool expect_accelerated_upload,
192455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry                     GLuint num_samples,
1931bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry                     enum intel_msaa_layout msaa_layout)
1946d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg{
1956d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg   struct intel_mipmap_tree *mt;
196453f0b1f243b26e02b371293e9c8bbf09362c14aEric Anholt   uint32_t tiling = I915_TILING_NONE;
197a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   GLenum base_format;
198a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   bool wraps_etc1 = false;
199a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace
200a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   if (format == MESA_FORMAT_ETC1_RGB8) {
201a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace      format = MESA_FORMAT_RGBX8888_REV;
202a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace      wraps_etc1 = true;
203a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   }
204a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace
205a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   base_format = _mesa_get_format_base_format(format);
2061ba96651e12b3c74fb9c8f5a61b183ef36a27b1eEric Anholt
2076dcc398ac0837025cf60b4d6a056fa3b0a16466fEric Anholt   if (intel->use_texture_tiling && !_mesa_is_format_compressed(format)) {
208caf3038123d6d29afd7d1f0cd6db98a2282c3ca1Eric Anholt      if (intel->gen >= 4 &&
2099c0ba017c8ff7caafc3ff94da3c035e687231596Eric Anholt	  (base_format == GL_DEPTH_COMPONENT ||
2109c0ba017c8ff7caafc3ff94da3c035e687231596Eric Anholt	   base_format == GL_DEPTH_STENCIL_EXT))
211bd10f0e84f1491363d76d92dcbd410ab5cc43dbeEric Anholt	 tiling = I915_TILING_Y;
2121bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      else if (msaa_layout != INTEL_MSAA_LAYOUT_NONE) {
21319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry         /* From p82 of the Sandy Bridge PRM, dw3[1] of SURFACE_STATE ("Tiled
21419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          * Surface"):
21519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          *
21619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          *   [DevSNB+]: For multi-sample render targets, this field must be
21719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          *   1. MSRTs can only be tiled.
21819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          *
21919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          * Our usual reason for preferring X tiling (fast blits using the
22019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          * blitting engine) doesn't apply to MSAA, since we'll generally be
22119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          * downsampling or upsampling when blitting between the MSAA buffer
22219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          * and another buffer, and the blitting engine doesn't support that.
22319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          * So use Y tiling, since it makes better use of the cache.
22419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry          */
22519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry         tiling = I915_TILING_Y;
22619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      } else if (width0 >= 64)
227bd10f0e84f1491363d76d92dcbd410ab5cc43dbeEric Anholt	 tiling = I915_TILING_X;
228453f0b1f243b26e02b371293e9c8bbf09362c14aEric Anholt   }
2296d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg
230f22068d5be7c829d3768154845ef3c5a2986fed4Eric Anholt   if (format == MESA_FORMAT_S8) {
231f22068d5be7c829d3768154845ef3c5a2986fed4Eric Anholt      /* The stencil buffer is W tiled. However, we request from the kernel a
232a27c7d80afc3160a0face4b8781bf921229bc3ccPaul Berry       * non-tiled buffer because the GTT is incapable of W fencing.  So round
233a27c7d80afc3160a0face4b8781bf921229bc3ccPaul Berry       * up the width and height to match the size of W tiles (64x64).
234f22068d5be7c829d3768154845ef3c5a2986fed4Eric Anholt       */
235f22068d5be7c829d3768154845ef3c5a2986fed4Eric Anholt      tiling = I915_TILING_NONE;
236f22068d5be7c829d3768154845ef3c5a2986fed4Eric Anholt      width0 = ALIGN(width0, 64);
237a27c7d80afc3160a0face4b8781bf921229bc3ccPaul Berry      height0 = ALIGN(height0, 64);
238f22068d5be7c829d3768154845ef3c5a2986fed4Eric Anholt   }
239f22068d5be7c829d3768154845ef3c5a2986fed4Eric Anholt
2409a523a48af05118424714f0a34ca3dda6861186aEric Anholt   mt = intel_miptree_create_internal(intel, target, format,
2415b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt				      first_level, last_level, width0,
2427e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace				      height0, depth0,
2431bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry				      false, num_samples, msaa_layout);
2447ed1fd5d8438e55fe24091844cdfccb0881306bcXiang, Haihao   /*
2458b69c42b356d51c3a37bc0af41738b016c2adc5bXiang, Haihao    * pitch == 0 || height == 0  indicates the null texture
2467ed1fd5d8438e55fe24091844cdfccb0881306bcXiang, Haihao    */
2471a662e7c18cab98f1b122f6766faf338725de673Yuanhan Liu   if (!mt || !mt->total_width || !mt->total_height) {
248747f0307626ef5bcf2f889ab66bcc95ab8eda2c8Chad Versace      intel_miptree_release(&mt);
2496d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg      return NULL;
250a7d0665c00053839065592c339bfb298187a82beVinson Lee   }
2516d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg
252a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   mt->wraps_etc1 = wraps_etc1;
2539087ba128089ed0dc00e6eb38f37126fb7557d3bKristian Høgsberg   mt->region = intel_region_alloc(intel->intelScreen,
2541ba96651e12b3c74fb9c8f5a61b183ef36a27b1eEric Anholt				   tiling,
2558db761409dadc2e899d4e7107eff3aa07b07aa11Eric Anholt				   mt->cpp,
256362c1bf75eb74de5b4655c481b74f79718ed4a34Eric Anholt				   mt->total_width,
2578db761409dadc2e899d4e7107eff3aa07b07aa11Eric Anholt				   mt->total_height,
25840dd024be618d805b3744e15d25e115018641324Eric Anholt				   expect_accelerated_upload);
25902ebad900db4ef1ac42cbfb41b433919a4c857a2Kristian Høgsberg   mt->offset = 0;
2606d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg
26177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   if (!mt->region) {
262747f0307626ef5bcf2f889ab66bcc95ab8eda2c8Chad Versace       intel_miptree_release(&mt);
2636d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg       return NULL;
2646d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg   }
2656d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg
2666d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg   return mt;
2676d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg}
2686d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg
26947a7535f413d6467082de224f64eecc046227406Brian Paul
2706d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsbergstruct intel_mipmap_tree *
2716d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsbergintel_miptree_create_for_region(struct intel_context *intel,
2726d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg				GLenum target,
273d5809115b568d8b74f47316607dce0730964517aEric Anholt				gl_format format,
2746f23d9b637602d9997896c6ca5cba0dfe36a092eIan Romanick				struct intel_region *region)
2756d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg{
2766d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg   struct intel_mipmap_tree *mt;
2776d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg
2789a523a48af05118424714f0a34ca3dda6861186aEric Anholt   mt = intel_miptree_create_internal(intel, target, format,
2795b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt				      0, 0,
2807e08bf08d13228001f6306800b5bd69b89b1bb6fChad Versace				      region->width, region->height, 1,
281455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry				      true, 0 /* num_samples */,
2821bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry                                      INTEL_MSAA_LAYOUT_NONE);
2836d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg   if (!mt)
2846d48779c7e5c9002d1bec4b1266ca05a474218efKristian Høgsberg      return mt;
285da011faf48155a5c02ebc1fe1fa20a4f54b8c657Eric Anholt
286e72a44215312ae1f3c812ba28e47b4aec3589de9Pierre Willenbrock   intel_region_reference(&mt->region, region);
28777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
28877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   return mt;
28947a7535f413d6467082de224f64eecc046227406Brian Paul}
29047a7535f413d6467082de224f64eecc046227406Brian Paul
291455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry/**
2921bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * Determine which MSAA layout should be used by the MSAA surface being
2931bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry * created, based on the chip generation and the surface type.
294455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry */
2951bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berrystatic enum intel_msaa_layout
2961bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berrycompute_msaa_layout(struct intel_context *intel, gl_format format)
297455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry{
2981bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   /* Prior to Gen7, all MSAA surfaces used IMS layout. */
299455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry   if (intel->gen < 7)
3001bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      return INTEL_MSAA_LAYOUT_IMS;
301455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry
3021bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   /* In Gen7, IMS layout is only used for depth and stencil buffers. */
303455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry   switch (_mesa_get_format_base_format(format)) {
304455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry   case GL_DEPTH_COMPONENT:
305455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry   case GL_STENCIL_INDEX:
306455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry   case GL_DEPTH_STENCIL:
3071bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      return INTEL_MSAA_LAYOUT_IMS;
308455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry   default:
30933202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry      /* From the Ivy Bridge PRM, Vol4 Part1 p77 ("MCS Enable"):
31033202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry       *
31133202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry       *   This field must be set to 0 for all SINT MSRTs when all RT channels
31233202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry       *   are not written
31333202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry       *
31433202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry       * In practice this means that we have to disable MCS for all signed
31533202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry       * integer MSAA buffers.  The alternative, to disable MCS only when one
31633202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry       * of the render target channels is disabled, is impractical because it
31733202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry       * would require converting between CMS and UMS MSAA layouts on the fly,
31833202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry       * which is expensive.
31933202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry       */
32033202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry      if (_mesa_get_format_datatype(format) == GL_INT) {
32133202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry         /* TODO: is this workaround needed for future chipsets? */
32233202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry         assert(intel->gen == 7);
32333202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry         return INTEL_MSAA_LAYOUT_UMS;
32433202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry      } else {
32533202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry         return INTEL_MSAA_LAYOUT_CMS;
32633202b4876a88b6f54ca7022eadd2875a2d3508aPaul Berry      }
327455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry   }
328455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry}
329455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry
330f4873babdc27f697df9f4642209eca21a02ac476Chad Versace/**
331f4873babdc27f697df9f4642209eca21a02ac476Chad Versace * For a singlesample DRI2 buffer, this simply wraps the given region with a miptree.
332f4873babdc27f697df9f4642209eca21a02ac476Chad Versace *
333f4873babdc27f697df9f4642209eca21a02ac476Chad Versace * For a multisample DRI2 buffer, this wraps the given region with
334f4873babdc27f697df9f4642209eca21a02ac476Chad Versace * a singlesample miptree, then creates a multisample miptree into which the
335f4873babdc27f697df9f4642209eca21a02ac476Chad Versace * singlesample miptree is embedded as a child.
336f4873babdc27f697df9f4642209eca21a02ac476Chad Versace */
337f4873babdc27f697df9f4642209eca21a02ac476Chad Versacestruct intel_mipmap_tree*
338f4873babdc27f697df9f4642209eca21a02ac476Chad Versaceintel_miptree_create_for_dri2_buffer(struct intel_context *intel,
339f4873babdc27f697df9f4642209eca21a02ac476Chad Versace                                     unsigned dri_attachment,
340f4873babdc27f697df9f4642209eca21a02ac476Chad Versace                                     gl_format format,
341f4873babdc27f697df9f4642209eca21a02ac476Chad Versace                                     uint32_t num_samples,
342f4873babdc27f697df9f4642209eca21a02ac476Chad Versace                                     struct intel_region *region)
343f4873babdc27f697df9f4642209eca21a02ac476Chad Versace{
344f4873babdc27f697df9f4642209eca21a02ac476Chad Versace   struct intel_mipmap_tree *singlesample_mt = NULL;
345f4873babdc27f697df9f4642209eca21a02ac476Chad Versace   struct intel_mipmap_tree *multisample_mt = NULL;
346f4873babdc27f697df9f4642209eca21a02ac476Chad Versace   GLenum base_format = _mesa_get_format_base_format(format);
347f4873babdc27f697df9f4642209eca21a02ac476Chad Versace
348f4873babdc27f697df9f4642209eca21a02ac476Chad Versace   /* Only the front and back buffers, which are color buffers, are shared
349f4873babdc27f697df9f4642209eca21a02ac476Chad Versace    * through DRI2.
350f4873babdc27f697df9f4642209eca21a02ac476Chad Versace    */
351f4873babdc27f697df9f4642209eca21a02ac476Chad Versace   assert(dri_attachment == __DRI_BUFFER_BACK_LEFT ||
352f4873babdc27f697df9f4642209eca21a02ac476Chad Versace          dri_attachment == __DRI_BUFFER_FRONT_LEFT ||
353f4873babdc27f697df9f4642209eca21a02ac476Chad Versace          dri_attachment == __DRI_BUFFER_FAKE_FRONT_LEFT);
354f4873babdc27f697df9f4642209eca21a02ac476Chad Versace   assert(base_format == GL_RGB || base_format == GL_RGBA);
355f4873babdc27f697df9f4642209eca21a02ac476Chad Versace
356f4873babdc27f697df9f4642209eca21a02ac476Chad Versace   singlesample_mt = intel_miptree_create_for_region(intel, GL_TEXTURE_2D,
357f4873babdc27f697df9f4642209eca21a02ac476Chad Versace                                                     format, region);
358f4873babdc27f697df9f4642209eca21a02ac476Chad Versace   if (!singlesample_mt)
359f4873babdc27f697df9f4642209eca21a02ac476Chad Versace      return NULL;
360f4873babdc27f697df9f4642209eca21a02ac476Chad Versace
361f4873babdc27f697df9f4642209eca21a02ac476Chad Versace   if (num_samples == 0)
362f4873babdc27f697df9f4642209eca21a02ac476Chad Versace      return singlesample_mt;
363f4873babdc27f697df9f4642209eca21a02ac476Chad Versace
364f4873babdc27f697df9f4642209eca21a02ac476Chad Versace   multisample_mt = intel_miptree_create_for_renderbuffer(intel,
365f4873babdc27f697df9f4642209eca21a02ac476Chad Versace                                                          format,
366f4873babdc27f697df9f4642209eca21a02ac476Chad Versace                                                          region->width,
367f4873babdc27f697df9f4642209eca21a02ac476Chad Versace                                                          region->height,
368f4873babdc27f697df9f4642209eca21a02ac476Chad Versace                                                          num_samples);
369f4873babdc27f697df9f4642209eca21a02ac476Chad Versace   if (!multisample_mt) {
370f4873babdc27f697df9f4642209eca21a02ac476Chad Versace      intel_miptree_release(&singlesample_mt);
371f4873babdc27f697df9f4642209eca21a02ac476Chad Versace      return NULL;
372f4873babdc27f697df9f4642209eca21a02ac476Chad Versace   }
373f4873babdc27f697df9f4642209eca21a02ac476Chad Versace
374f4873babdc27f697df9f4642209eca21a02ac476Chad Versace   multisample_mt->singlesample_mt = singlesample_mt;
375f4873babdc27f697df9f4642209eca21a02ac476Chad Versace   multisample_mt->need_downsample = false;
376f4873babdc27f697df9f4642209eca21a02ac476Chad Versace
377f4873babdc27f697df9f4642209eca21a02ac476Chad Versace   if (intel->is_front_buffer_rendering &&
378f4873babdc27f697df9f4642209eca21a02ac476Chad Versace       (dri_attachment == __DRI_BUFFER_FRONT_LEFT ||
379f4873babdc27f697df9f4642209eca21a02ac476Chad Versace        dri_attachment == __DRI_BUFFER_FAKE_FRONT_LEFT)) {
380f4873babdc27f697df9f4642209eca21a02ac476Chad Versace      intel_miptree_upsample(intel, multisample_mt);
381f4873babdc27f697df9f4642209eca21a02ac476Chad Versace   }
382f4873babdc27f697df9f4642209eca21a02ac476Chad Versace
383f4873babdc27f697df9f4642209eca21a02ac476Chad Versace   return multisample_mt;
384f4873babdc27f697df9f4642209eca21a02ac476Chad Versace}
385f4873babdc27f697df9f4642209eca21a02ac476Chad Versace
386005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versacestruct intel_mipmap_tree*
387005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versaceintel_miptree_create_for_renderbuffer(struct intel_context *intel,
388005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versace                                      gl_format format,
389005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versace                                      uint32_t width,
39019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry                                      uint32_t height,
39119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry                                      uint32_t num_samples)
392005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versace{
393005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versace   struct intel_mipmap_tree *mt;
394455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry   uint32_t depth = 1;
3951bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   enum intel_msaa_layout msaa_layout = INTEL_MSAA_LAYOUT_NONE;
3964eba67285fb6b5d2dd4927e8dc4b3e2945435309Chad Versace   bool ok;
397455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry
39897fc89c6cbaa3b5ef7f678d2dc2c7d5bbba05315Paul Berry   if (num_samples > 1) {
399455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry      /* Adjust width/height/depth for MSAA */
4001bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      msaa_layout = compute_msaa_layout(intel, format);
4011bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      if (msaa_layout == INTEL_MSAA_LAYOUT_IMS) {
402455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry         /* In the Sandy Bridge PRM, volume 4, part 1, page 31, it says:
403455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *
404455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *     "Any of the other messages (sample*, LOD, load4) used with a
405455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *      (4x) multisampled surface will in-effect sample a surface with
406455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *      double the height and width as that indicated in the surface
407455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *      state. Each pixel position on the original-sized surface is
408455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *      replaced with a 2x2 of samples with the following arrangement:
409455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *
410455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *         sample 0 sample 2
411455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *         sample 1 sample 3"
412455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *
413455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          * Thus, when sampling from a multisampled texture, it behaves as
414455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          * though the layout in memory for (x,y,sample) is:
415455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *
416455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *      (0,0,0) (0,0,2)   (1,0,0) (1,0,2)
417455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *      (0,0,1) (0,0,3)   (1,0,1) (1,0,3)
418455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *
419455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *      (0,1,0) (0,1,2)   (1,1,0) (1,1,2)
420455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *      (0,1,1) (0,1,3)   (1,1,1) (1,1,3)
421455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *
422455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          * However, the actual layout of multisampled data in memory is:
423455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *
424455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *      (0,0,0) (1,0,0)   (0,0,1) (1,0,1)
425455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *      (0,1,0) (1,1,0)   (0,1,1) (1,1,1)
426455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *
427455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *      (0,0,2) (1,0,2)   (0,0,3) (1,0,3)
428455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *      (0,1,2) (1,1,2)   (0,1,3) (1,1,3)
429455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *
430455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          * This pattern repeats for each 2x2 pixel block.
431455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          *
432455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          * As a result, when calculating the size of our 4-sample buffer for
433455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          * an odd width or height, we have to align before scaling up because
434455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          * sample 3 is in that bottom right 2x2 block.
435455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry          */
436455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry         switch (num_samples) {
437455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry         case 4:
438455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry            width = ALIGN(width, 2) * 2;
439455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry            height = ALIGN(height, 2) * 2;
440455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry            break;
441455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry         case 8:
442455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry            width = ALIGN(width, 2) * 4;
443455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry            height = ALIGN(height, 2) * 2;
444455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry            break;
445455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry         default:
44697fc89c6cbaa3b5ef7f678d2dc2c7d5bbba05315Paul Berry            /* num_samples should already have been quantized to 0, 1, 4, or
447455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry             * 8.
448455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry             */
449455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry            assert(false);
450455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry         }
451455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry      } else {
452455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry         /* Non-interleaved */
453455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry         depth = num_samples;
454455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry      }
45519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   }
45619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
457f22068d5be7c829d3768154845ef3c5a2986fed4Eric Anholt   mt = intel_miptree_create(intel, GL_TEXTURE_2D, format, 0, 0,
458455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry			     width, height, depth, true, num_samples,
4591bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry                             msaa_layout);
4604eba67285fb6b5d2dd4927e8dc4b3e2945435309Chad Versace   if (!mt)
4614eba67285fb6b5d2dd4927e8dc4b3e2945435309Chad Versace      goto fail;
4624eba67285fb6b5d2dd4927e8dc4b3e2945435309Chad Versace
4634eba67285fb6b5d2dd4927e8dc4b3e2945435309Chad Versace   if (intel->vtbl.is_hiz_depth_format(intel, format)) {
4644eba67285fb6b5d2dd4927e8dc4b3e2945435309Chad Versace      ok = intel_miptree_alloc_hiz(intel, mt, num_samples);
4654eba67285fb6b5d2dd4927e8dc4b3e2945435309Chad Versace      if (!ok)
4664eba67285fb6b5d2dd4927e8dc4b3e2945435309Chad Versace         goto fail;
4674eba67285fb6b5d2dd4927e8dc4b3e2945435309Chad Versace   }
4684eba67285fb6b5d2dd4927e8dc4b3e2945435309Chad Versace
4694eba67285fb6b5d2dd4927e8dc4b3e2945435309Chad Versace   if (mt->msaa_layout == INTEL_MSAA_LAYOUT_CMS) {
4704eba67285fb6b5d2dd4927e8dc4b3e2945435309Chad Versace      ok = intel_miptree_alloc_mcs(intel, mt, num_samples);
4714eba67285fb6b5d2dd4927e8dc4b3e2945435309Chad Versace      if (!ok)
4724eba67285fb6b5d2dd4927e8dc4b3e2945435309Chad Versace         goto fail;
4734eba67285fb6b5d2dd4927e8dc4b3e2945435309Chad Versace   }
474005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versace
475005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versace   return mt;
4764eba67285fb6b5d2dd4927e8dc4b3e2945435309Chad Versace
4774eba67285fb6b5d2dd4927e8dc4b3e2945435309Chad Versacefail:
4784eba67285fb6b5d2dd4927e8dc4b3e2945435309Chad Versace   intel_miptree_release(&mt);
4794eba67285fb6b5d2dd4927e8dc4b3e2945435309Chad Versace   return NULL;
480005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versace}
481005149d5860ad55c5e58e2de8a138e3a763f2036Chad Versace
48277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtvoid
48377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtintel_miptree_reference(struct intel_mipmap_tree **dst,
48477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt                        struct intel_mipmap_tree *src)
48577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
48632fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt   if (*dst == src)
48732fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt      return;
48832fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt
48932fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt   intel_miptree_release(dst);
49032fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt
49132fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt   if (src) {
49232fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt      src->refcount++;
49332fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt      DBG("%s %p refcount now %d\n", __FUNCTION__, src, src->refcount);
49432fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt   }
49532fe506ae14efa055f4773f422e2edd9fd1cffeeEric Anholt
49677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   *dst = src;
49777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
49877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
49947a7535f413d6467082de224f64eecc046227406Brian Paul
50077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtvoid
501db3ada6055814a4bd5aa95fc9505fc101864391dEric Anholtintel_miptree_release(struct intel_mipmap_tree **mt)
50277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
50377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   if (!*mt)
50477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      return;
50577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
50677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   DBG("%s %p refcount will be %d\n", __FUNCTION__, *mt, (*mt)->refcount - 1);
50777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   if (--(*mt)->refcount <= 0) {
50877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      GLuint i;
50977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
51077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      DBG("%s deleting %p\n", __FUNCTION__, *mt);
51177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
51277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      intel_region_release(&((*mt)->region));
5133eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace      intel_miptree_release(&(*mt)->stencil_mt);
514a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace      intel_miptree_release(&(*mt)->hiz_mt);
515ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry      intel_miptree_release(&(*mt)->mcs_mt);
516f4873babdc27f697df9f4642209eca21a02ac476Chad Versace      intel_miptree_release(&(*mt)->singlesample_mt);
5172945abea338031cbe90665df60152982bfca6177Chad Versace      intel_resolve_map_clear(&(*mt)->hiz_map);
51877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
5192d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt      for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
520c2ddde70daece24d1eb7185946032158993a9e4dChad Versace	 free((*mt)->level[i].slice);
5212d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt      }
52277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
52377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      free(*mt);
52477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   }
52577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   *mt = NULL;
52677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
52777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
528fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholtvoid
529fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholtintel_miptree_get_dimensions_for_image(struct gl_texture_image *image,
530fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt                                       int *width, int *height, int *depth)
531fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt{
532fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt   switch (image->TexObject->Target) {
533fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt   case GL_TEXTURE_1D_ARRAY:
534fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt      *width = image->Width;
535fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt      *height = 1;
536fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt      *depth = image->Height;
537fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt      break;
538fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt   default:
539fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt      *width = image->Width;
540fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt      *height = image->Height;
541fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt      *depth = image->Depth;
542fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt      break;
543fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt   }
544fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt}
54577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
54647a7535f413d6467082de224f64eecc046227406Brian Paul/**
54747a7535f413d6467082de224f64eecc046227406Brian Paul * Can the image be pulled into a unified mipmap tree?  This mirrors
54877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * the completeness test in a lot of ways.
54977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt *
55077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * Not sure whether I want to pass gl_texture_image here.
55177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt */
5522e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunkebool
55377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtintel_miptree_match_image(struct intel_mipmap_tree *mt,
5548f30ceaaefc33401b08739a16ce1c5638d6432faEric Anholt                          struct gl_texture_image *image)
55577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
5568f30ceaaefc33401b08739a16ce1c5638d6432faEric Anholt   struct intel_texture_image *intelImage = intel_texture_image(image);
557fa2c886863492cc3eeee6d2059ae24edc1cb2bffBrian Paul   GLuint level = intelImage->base.Base.Level;
558fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt   int width, height, depth;
559b6bdafdf2cf1110b4a5ca7cf9e1c3dcb124b800fBrian Paul
560ab7794cada02f3b3b5e3a642c20eeedeb17b65a6Eric Anholt   if (target_to_target(image->TexObject->Target) != mt->target)
561ab7794cada02f3b3b5e3a642c20eeedeb17b65a6Eric Anholt      return false;
562ab7794cada02f3b3b5e3a642c20eeedeb17b65a6Eric Anholt
563fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt   if (image->TexFormat != mt->format &&
564fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt       !(image->TexFormat == MESA_FORMAT_S8_Z24 &&
565fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt	 mt->format == MESA_FORMAT_X8_Z24 &&
566fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt	 mt->stencil_mt)) {
5672e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunke      return false;
568fdf18b323156098ba5fb2881aa1a7888d2e0667fEric Anholt   }
569d12fa3511da23d8285f3ea1a51a1f328cdbb1462Xiang, Haihao
570fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt   intel_miptree_get_dimensions_for_image(image, &width, &height, &depth);
571fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt
5727f854a5028ef00a0e041e78a770456204dc58eefEric Anholt   if (mt->target == GL_TEXTURE_CUBE_MAP)
5737f854a5028ef00a0e041e78a770456204dc58eefEric Anholt      depth = 6;
5747f854a5028ef00a0e041e78a770456204dc58eefEric Anholt
57577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   /* Test image dimensions against the base level image adjusted for
57677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt    * minification.  This will also catch images not present in the
57777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt    * tree, changed targets, etc.
57877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt    */
579fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt   if (width != mt->level[level].width ||
580fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt       height != mt->level[level].height ||
581fd99cd0e10849205749aad580fea8c970fb46a31Eric Anholt       depth != mt->level[level].depth)
5822e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunke      return false;
58377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
5842e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunke   return true;
58577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
58677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
58777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
58877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtvoid
58977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtintel_miptree_set_level_info(struct intel_mipmap_tree *mt,
590659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			     GLuint level,
591659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			     GLuint x, GLuint y,
592659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			     GLuint w, GLuint h, GLuint d)
59377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
59477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->level[level].width = w;
59577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->level[level].height = h;
59677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->level[level].depth = d;
5972d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt   mt->level[level].level_x = x;
5982d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt   mt->level[level].level_y = y;
59977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
6000c51390e4b5e04b992e50fcbed751024e6c329deEric Anholt   DBG("%s level %d size: %d,%d,%d offset %d,%d\n", __FUNCTION__,
6010c51390e4b5e04b992e50fcbed751024e6c329deEric Anholt       level, w, h, d, x, y);
60277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
603c2ddde70daece24d1eb7185946032158993a9e4dChad Versace   assert(mt->level[level].slice == NULL);
60477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
605221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt   mt->level[level].slice = calloc(d, sizeof(*mt->level[0].slice));
606c2ddde70daece24d1eb7185946032158993a9e4dChad Versace   mt->level[level].slice[0].x_offset = mt->level[level].level_x;
607c2ddde70daece24d1eb7185946032158993a9e4dChad Versace   mt->level[level].slice[0].y_offset = mt->level[level].level_y;
60877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
60977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
61077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
61177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtvoid
6122d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholtintel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
6132d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt			       GLuint level, GLuint img,
6142d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt			       GLuint x, GLuint y)
61577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
61677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   if (img == 0 && level == 0)
61777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      assert(x == 0 && y == 0);
61877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
619d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   assert(img < mt->level[level].depth);
62077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
621c2ddde70daece24d1eb7185946032158993a9e4dChad Versace   mt->level[level].slice[img].x_offset = mt->level[level].level_x + x;
622c2ddde70daece24d1eb7185946032158993a9e4dChad Versace   mt->level[level].slice[img].y_offset = mt->level[level].level_y + y;
62377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
6242d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt   DBG("%s level %d img %d pos %d,%d\n",
6252d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt       __FUNCTION__, level, img,
626c2ddde70daece24d1eb7185946032158993a9e4dChad Versace       mt->level[level].slice[img].x_offset,
627c2ddde70daece24d1eb7185946032158993a9e4dChad Versace       mt->level[level].slice[img].y_offset);
62877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
62977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
63047a7535f413d6467082de224f64eecc046227406Brian Paul
631d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace/**
632d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace * For cube map textures, either the \c face parameter can be used, of course,
633d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace * or the cube face can be interpreted as a depth layer and the \c layer
634d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace * parameter used.
635d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace */
6362995bf0d68f1b28ba68b81e9dc79e3ab52bc2795Xiang, Haihaovoid
6372d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholtintel_miptree_get_image_offset(struct intel_mipmap_tree *mt,
638d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace			       GLuint level, GLuint face, GLuint layer,
6392d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt			       GLuint *x, GLuint *y)
64077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
641d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   int slice;
642d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace
643d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   if (face > 0) {
644d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      assert(mt->target == GL_TEXTURE_CUBE_MAP);
645d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      assert(face < 6);
646d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      assert(layer == 0);
647d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      slice = face;
648d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   } else {
649d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      /* This branch may be taken even if the texture target is a cube map. In
650d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace       * that case, the caller chose to interpret each cube face as a layer.
651d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace       */
652d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      assert(face == 0);
653d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      slice = layer;
6542d17dbfb5346b6d75e87c839148cbe125bf5cd6dEric Anholt   }
655d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace
656d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   *x = mt->level[level].slice[slice].x_offset;
657d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   *y = mt->level[level].slice[slice].y_offset;
65877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
65977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
660278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versacestatic void
661278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versaceintel_miptree_copy_slice(struct intel_context *intel,
662278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			 struct intel_mipmap_tree *dst_mt,
663278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			 struct intel_mipmap_tree *src_mt,
664278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			 int level,
665278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			 int face,
666278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			 int depth)
667278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
668278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace{
669278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   gl_format format = src_mt->format;
670278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   uint32_t width = src_mt->level[level].width;
671278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   uint32_t height = src_mt->level[level].height;
672278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
673278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   assert(depth < src_mt->level[level].depth);
674278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
675278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   if (dst_mt->compressed) {
676017c13d55b5b086774d6afea2ca754482c624c6aChad Versace      height = ALIGN(height, dst_mt->align_h) / dst_mt->align_h;
677017c13d55b5b086774d6afea2ca754482c624c6aChad Versace      width = ALIGN(width, dst_mt->align_w);
678278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   }
679278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
680278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   uint32_t dst_x, dst_y, src_x, src_y;
681278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   intel_miptree_get_image_offset(dst_mt, level, face, depth,
682278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace				  &dst_x, &dst_y);
683278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   intel_miptree_get_image_offset(src_mt, level, face, depth,
684278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace				  &src_x, &src_y);
685278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
686278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   DBG("validate blit mt %p %d,%d/%d -> mt %p %d,%d/%d (%dx%d)\n",
687278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace       src_mt, src_x, src_y, src_mt->region->pitch * src_mt->region->cpp,
688278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace       dst_mt, dst_x, dst_y, dst_mt->region->pitch * dst_mt->region->cpp,
689278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace       width, height);
690278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
691278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   if (!intelEmitCopyBlit(intel,
692278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			  dst_mt->region->cpp,
693278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			  src_mt->region->pitch, src_mt->region->bo,
694278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			  0, src_mt->region->tiling,
695278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			  dst_mt->region->pitch, dst_mt->region->bo,
696278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			  0, dst_mt->region->tiling,
697278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			  src_x, src_y,
698278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			  dst_x, dst_y,
699278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			  width, height,
700278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace			  GL_COPY)) {
701278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
702278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace      fallback_debug("miptree validate blit for %s failed\n",
703278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace		     _mesa_get_format_name(format));
704278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace      void *dst = intel_region_map(intel, dst_mt->region, GL_MAP_WRITE_BIT);
705278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace      void *src = intel_region_map(intel, src_mt->region, GL_MAP_READ_BIT);
706278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
707278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace      _mesa_copy_rect(dst,
708278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace		      dst_mt->cpp,
709278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace		      dst_mt->region->pitch,
710278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace		      dst_x, dst_y,
711278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace		      width, height,
712278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace		      src, src_mt->region->pitch,
713278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace		      src_x, src_y);
714278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
715278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace      intel_region_unmap(intel, dst_mt->region);
716278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace      intel_region_unmap(intel, src_mt->region);
717278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   }
7183eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace
7193eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace   if (src_mt->stencil_mt) {
7203eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace      intel_miptree_copy_slice(intel,
7213eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace                               dst_mt->stencil_mt, src_mt->stencil_mt,
7223eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace                               level, face, depth);
7233eb12dfaeed03f77e31943eea164acb03e86bbc9Chad Versace   }
724278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace}
725278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
72677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt/**
727b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt * Copies the image's current data to the given miptree, and associates that
728b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt * miptree with the image.
72977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt */
73077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtvoid
731b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholtintel_miptree_copy_teximage(struct intel_context *intel,
732b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt			    struct intel_texture_image *intelImage,
733b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt			    struct intel_mipmap_tree *dst_mt)
73477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
735b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt   struct intel_mipmap_tree *src_mt = intelImage->mt;
736b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt   int level = intelImage->base.Base.Level;
737b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt   int face = intelImage->base.Base.Face;
738d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   GLuint depth = intelImage->base.Base.Depth;
739b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt
740278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace   for (int slice = 0; slice < depth; slice++) {
741278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace      intel_miptree_copy_slice(intel, dst_mt, src_mt, level, face, slice);
74277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   }
74377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
744b07c78bfe94c17e6fccba70923b03a29c751fde1Eric Anholt   intel_miptree_reference(&intelImage->mt, dst_mt);
74577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
746278e77a1192d5251c5b70a555e676f72b446e8e1Chad Versace
747a2e44b0813e956440c451c107cf5564b56cbe98eChad Versacebool
748ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berryintel_miptree_alloc_mcs(struct intel_context *intel,
749ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry                        struct intel_mipmap_tree *mt,
750ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry                        GLuint num_samples)
751ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry{
752ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry   assert(mt->mcs_mt == NULL);
753ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry   assert(intel->gen >= 7); /* MCS only used on Gen7+ */
754728561271303baa6aaeb5bdd953e62b117feba4dPaul Berry
755728561271303baa6aaeb5bdd953e62b117feba4dPaul Berry   /* Choose the correct format for the MCS buffer.  All that really matters
756728561271303baa6aaeb5bdd953e62b117feba4dPaul Berry    * is that we allocate the right buffer size, since we'll always be
757728561271303baa6aaeb5bdd953e62b117feba4dPaul Berry    * accessing this miptree using MCS-specific hardware mechanisms, which
758728561271303baa6aaeb5bdd953e62b117feba4dPaul Berry    * infer the correct format based on num_samples.
759728561271303baa6aaeb5bdd953e62b117feba4dPaul Berry    */
760728561271303baa6aaeb5bdd953e62b117feba4dPaul Berry   gl_format format;
761728561271303baa6aaeb5bdd953e62b117feba4dPaul Berry   switch (num_samples) {
762728561271303baa6aaeb5bdd953e62b117feba4dPaul Berry   case 4:
763728561271303baa6aaeb5bdd953e62b117feba4dPaul Berry      /* 8 bits/pixel are required for MCS data when using 4x MSAA (2 bits for
764728561271303baa6aaeb5bdd953e62b117feba4dPaul Berry       * each sample).
765728561271303baa6aaeb5bdd953e62b117feba4dPaul Berry       */
766ee9f6a34ccf7f51111b21bba8d8f9d03cc0a2c74Paul Berry      format = MESA_FORMAT_R8;
767728561271303baa6aaeb5bdd953e62b117feba4dPaul Berry      break;
768728561271303baa6aaeb5bdd953e62b117feba4dPaul Berry   case 8:
769728561271303baa6aaeb5bdd953e62b117feba4dPaul Berry      /* 32 bits/pixel are required for MCS data when using 8x MSAA (3 bits
770728561271303baa6aaeb5bdd953e62b117feba4dPaul Berry       * for each sample, plus 8 padding bits).
771728561271303baa6aaeb5bdd953e62b117feba4dPaul Berry       */
772728561271303baa6aaeb5bdd953e62b117feba4dPaul Berry      format = MESA_FORMAT_R_UINT32;
773728561271303baa6aaeb5bdd953e62b117feba4dPaul Berry      break;
774728561271303baa6aaeb5bdd953e62b117feba4dPaul Berry   default:
775728561271303baa6aaeb5bdd953e62b117feba4dPaul Berry      assert(!"Unrecognized sample count in intel_miptree_alloc_mcs");
776728561271303baa6aaeb5bdd953e62b117feba4dPaul Berry      break;
777728561271303baa6aaeb5bdd953e62b117feba4dPaul Berry   };
778ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry
779ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry   /* From the Ivy Bridge PRM, Vol4 Part1 p76, "MCS Base Address":
780ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    *
781ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    *     "The MCS surface must be stored as Tile Y."
782ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    *
783ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    * We set msaa_format to INTEL_MSAA_LAYOUT_CMS to force
784ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    * intel_miptree_create() to use Y tiling.  msaa_format is otherwise
785ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    * ignored for the MCS miptree.
786ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    */
787ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry   mt->mcs_mt = intel_miptree_create(intel,
788ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry                                     mt->target,
789728561271303baa6aaeb5bdd953e62b117feba4dPaul Berry                                     format,
790ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry                                     mt->first_level,
791ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry                                     mt->last_level,
792ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry                                     mt->width0,
793ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry                                     mt->height0,
794ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry                                     mt->depth0,
795ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry                                     true,
796ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry                                     0 /* num_samples */,
797ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry                                     INTEL_MSAA_LAYOUT_CMS);
798ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry
799ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry   /* From the Ivy Bridge PRM, Vol 2 Part 1 p326:
800ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    *
801ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    *     When MCS buffer is enabled and bound to MSRT, it is required that it
802ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    *     is cleared prior to any rendering.
803ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    *
804ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    * Since we don't use the MCS buffer for any purpose other than rendering,
805ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    * it makes sense to just clear it immediately upon allocation.
806ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    *
807ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    * Note: the clear value for MCS buffers is all 1's, so we memset to 0xff.
808ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry    */
809ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry   void *data = intel_region_map(intel, mt->mcs_mt->region, 0);
810ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry   memset(data, 0xff, mt->mcs_mt->region->bo->size);
811ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry   intel_region_unmap(intel, mt->mcs_mt->region);
812ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry
813ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry   return mt->mcs_mt;
814ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry}
815ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berry
816ccae1b1cd7b89102a9d9bfc29eb1e7e48aad8969Paul Berrybool
817a2e44b0813e956440c451c107cf5564b56cbe98eChad Versaceintel_miptree_alloc_hiz(struct intel_context *intel,
81819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry			struct intel_mipmap_tree *mt,
81919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry                        GLuint num_samples)
820a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace{
821a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace   assert(mt->hiz_mt == NULL);
8221bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry   /* MSAA HiZ surfaces always use IMS layout. */
823a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace   mt->hiz_mt = intel_miptree_create(intel,
824a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace                                     mt->target,
825a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace                                     MESA_FORMAT_X8_Z24,
826a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace                                     mt->first_level,
827a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace                                     mt->last_level,
828a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace                                     mt->width0,
829a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace                                     mt->height0,
830a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace                                     mt->depth0,
83119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry                                     true,
832455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry                                     num_samples,
8331bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry                                     INTEL_MSAA_LAYOUT_IMS);
8343d760664e6349c72624aa6d54d40df0233995c8eChad Versace
8353d760664e6349c72624aa6d54d40df0233995c8eChad Versace   if (!mt->hiz_mt)
8363d760664e6349c72624aa6d54d40df0233995c8eChad Versace      return false;
8373d760664e6349c72624aa6d54d40df0233995c8eChad Versace
8383d760664e6349c72624aa6d54d40df0233995c8eChad Versace   /* Mark that all slices need a HiZ resolve. */
8393d760664e6349c72624aa6d54d40df0233995c8eChad Versace   struct intel_resolve_map *head = &mt->hiz_map;
8403d760664e6349c72624aa6d54d40df0233995c8eChad Versace   for (int level = mt->first_level; level <= mt->last_level; ++level) {
8413d760664e6349c72624aa6d54d40df0233995c8eChad Versace      for (int layer = 0; layer < mt->level[level].depth; ++layer) {
8423d760664e6349c72624aa6d54d40df0233995c8eChad Versace	 head->next = malloc(sizeof(*head->next));
8433d760664e6349c72624aa6d54d40df0233995c8eChad Versace	 head->next->prev = head;
8443d760664e6349c72624aa6d54d40df0233995c8eChad Versace	 head->next->next = NULL;
8453d760664e6349c72624aa6d54d40df0233995c8eChad Versace	 head = head->next;
8463d760664e6349c72624aa6d54d40df0233995c8eChad Versace
8473d760664e6349c72624aa6d54d40df0233995c8eChad Versace	 head->level = level;
8483d760664e6349c72624aa6d54d40df0233995c8eChad Versace	 head->layer = layer;
849072634da4a6ee5eafb5f5df26ca5f988209e6d40Eric Anholt	 head->need = GEN6_HIZ_OP_HIZ_RESOLVE;
8503d760664e6349c72624aa6d54d40df0233995c8eChad Versace      }
8513d760664e6349c72624aa6d54d40df0233995c8eChad Versace   }
8523d760664e6349c72624aa6d54d40df0233995c8eChad Versace
8533d760664e6349c72624aa6d54d40df0233995c8eChad Versace   return true;
854a2e44b0813e956440c451c107cf5564b56cbe98eChad Versace}
8552945abea338031cbe90665df60152982bfca6177Chad Versace
8562945abea338031cbe90665df60152982bfca6177Chad Versacevoid
8572945abea338031cbe90665df60152982bfca6177Chad Versaceintel_miptree_slice_set_needs_hiz_resolve(struct intel_mipmap_tree *mt,
8582945abea338031cbe90665df60152982bfca6177Chad Versace					  uint32_t level,
8592945abea338031cbe90665df60152982bfca6177Chad Versace					  uint32_t layer)
8602945abea338031cbe90665df60152982bfca6177Chad Versace{
8612945abea338031cbe90665df60152982bfca6177Chad Versace   intel_miptree_check_level_layer(mt, level, layer);
8622945abea338031cbe90665df60152982bfca6177Chad Versace
8632945abea338031cbe90665df60152982bfca6177Chad Versace   if (!mt->hiz_mt)
8642945abea338031cbe90665df60152982bfca6177Chad Versace      return;
8652945abea338031cbe90665df60152982bfca6177Chad Versace
8662945abea338031cbe90665df60152982bfca6177Chad Versace   intel_resolve_map_set(&mt->hiz_map,
867072634da4a6ee5eafb5f5df26ca5f988209e6d40Eric Anholt			 level, layer, GEN6_HIZ_OP_HIZ_RESOLVE);
8682945abea338031cbe90665df60152982bfca6177Chad Versace}
8692945abea338031cbe90665df60152982bfca6177Chad Versace
8702945abea338031cbe90665df60152982bfca6177Chad Versace
8712945abea338031cbe90665df60152982bfca6177Chad Versacevoid
8722945abea338031cbe90665df60152982bfca6177Chad Versaceintel_miptree_slice_set_needs_depth_resolve(struct intel_mipmap_tree *mt,
8732945abea338031cbe90665df60152982bfca6177Chad Versace                                            uint32_t level,
8742945abea338031cbe90665df60152982bfca6177Chad Versace                                            uint32_t layer)
8752945abea338031cbe90665df60152982bfca6177Chad Versace{
8762945abea338031cbe90665df60152982bfca6177Chad Versace   intel_miptree_check_level_layer(mt, level, layer);
8772945abea338031cbe90665df60152982bfca6177Chad Versace
8782945abea338031cbe90665df60152982bfca6177Chad Versace   if (!mt->hiz_mt)
8792945abea338031cbe90665df60152982bfca6177Chad Versace      return;
8802945abea338031cbe90665df60152982bfca6177Chad Versace
8812945abea338031cbe90665df60152982bfca6177Chad Versace   intel_resolve_map_set(&mt->hiz_map,
882072634da4a6ee5eafb5f5df26ca5f988209e6d40Eric Anholt			 level, layer, GEN6_HIZ_OP_DEPTH_RESOLVE);
8832945abea338031cbe90665df60152982bfca6177Chad Versace}
8842945abea338031cbe90665df60152982bfca6177Chad Versace
8852945abea338031cbe90665df60152982bfca6177Chad Versacestatic bool
8862945abea338031cbe90665df60152982bfca6177Chad Versaceintel_miptree_slice_resolve(struct intel_context *intel,
8872945abea338031cbe90665df60152982bfca6177Chad Versace			    struct intel_mipmap_tree *mt,
8882945abea338031cbe90665df60152982bfca6177Chad Versace			    uint32_t level,
8892945abea338031cbe90665df60152982bfca6177Chad Versace			    uint32_t layer,
89054308f78a2f8675bfd854761f9cd8a6b71e119d0Eric Anholt			    enum gen6_hiz_op need)
8912945abea338031cbe90665df60152982bfca6177Chad Versace{
8922945abea338031cbe90665df60152982bfca6177Chad Versace   intel_miptree_check_level_layer(mt, level, layer);
8932945abea338031cbe90665df60152982bfca6177Chad Versace
8942945abea338031cbe90665df60152982bfca6177Chad Versace   struct intel_resolve_map *item =
8952945abea338031cbe90665df60152982bfca6177Chad Versace	 intel_resolve_map_get(&mt->hiz_map, level, layer);
8962945abea338031cbe90665df60152982bfca6177Chad Versace
8972945abea338031cbe90665df60152982bfca6177Chad Versace   if (!item || item->need != need)
8982945abea338031cbe90665df60152982bfca6177Chad Versace      return false;
8992945abea338031cbe90665df60152982bfca6177Chad Versace
90054308f78a2f8675bfd854761f9cd8a6b71e119d0Eric Anholt   intel_hiz_exec(intel, mt, level, layer, need);
9012945abea338031cbe90665df60152982bfca6177Chad Versace   intel_resolve_map_remove(item);
9022945abea338031cbe90665df60152982bfca6177Chad Versace   return true;
9032945abea338031cbe90665df60152982bfca6177Chad Versace}
9042945abea338031cbe90665df60152982bfca6177Chad Versace
9052945abea338031cbe90665df60152982bfca6177Chad Versacebool
9062945abea338031cbe90665df60152982bfca6177Chad Versaceintel_miptree_slice_resolve_hiz(struct intel_context *intel,
9072945abea338031cbe90665df60152982bfca6177Chad Versace				struct intel_mipmap_tree *mt,
9082945abea338031cbe90665df60152982bfca6177Chad Versace				uint32_t level,
9092945abea338031cbe90665df60152982bfca6177Chad Versace				uint32_t layer)
9102945abea338031cbe90665df60152982bfca6177Chad Versace{
9112945abea338031cbe90665df60152982bfca6177Chad Versace   return intel_miptree_slice_resolve(intel, mt, level, layer,
91254308f78a2f8675bfd854761f9cd8a6b71e119d0Eric Anholt				      GEN6_HIZ_OP_HIZ_RESOLVE);
9132945abea338031cbe90665df60152982bfca6177Chad Versace}
9142945abea338031cbe90665df60152982bfca6177Chad Versace
9152945abea338031cbe90665df60152982bfca6177Chad Versacebool
9162945abea338031cbe90665df60152982bfca6177Chad Versaceintel_miptree_slice_resolve_depth(struct intel_context *intel,
9172945abea338031cbe90665df60152982bfca6177Chad Versace				  struct intel_mipmap_tree *mt,
9182945abea338031cbe90665df60152982bfca6177Chad Versace				  uint32_t level,
9192945abea338031cbe90665df60152982bfca6177Chad Versace				  uint32_t layer)
9202945abea338031cbe90665df60152982bfca6177Chad Versace{
9212945abea338031cbe90665df60152982bfca6177Chad Versace   return intel_miptree_slice_resolve(intel, mt, level, layer,
92254308f78a2f8675bfd854761f9cd8a6b71e119d0Eric Anholt				      GEN6_HIZ_OP_DEPTH_RESOLVE);
9232945abea338031cbe90665df60152982bfca6177Chad Versace}
9242945abea338031cbe90665df60152982bfca6177Chad Versace
9252945abea338031cbe90665df60152982bfca6177Chad Versacestatic bool
9262945abea338031cbe90665df60152982bfca6177Chad Versaceintel_miptree_all_slices_resolve(struct intel_context *intel,
9272945abea338031cbe90665df60152982bfca6177Chad Versace				 struct intel_mipmap_tree *mt,
92854308f78a2f8675bfd854761f9cd8a6b71e119d0Eric Anholt				 enum gen6_hiz_op need)
9292945abea338031cbe90665df60152982bfca6177Chad Versace{
9302945abea338031cbe90665df60152982bfca6177Chad Versace   bool did_resolve = false;
9310ed11e333147e280208d9d0b3ff3f39970547643Anuj Phogat   struct intel_resolve_map *i, *next;
9322945abea338031cbe90665df60152982bfca6177Chad Versace
9330ed11e333147e280208d9d0b3ff3f39970547643Anuj Phogat   for (i = mt->hiz_map.next; i; i = next) {
934a0a0a909f21acfcddaab603079ba98266e8daf0eKenneth Graunke      next = i->next;
9352945abea338031cbe90665df60152982bfca6177Chad Versace      if (i->need != need)
9362945abea338031cbe90665df60152982bfca6177Chad Versace	 continue;
93754308f78a2f8675bfd854761f9cd8a6b71e119d0Eric Anholt
93854308f78a2f8675bfd854761f9cd8a6b71e119d0Eric Anholt      intel_hiz_exec(intel, mt, i->level, i->layer, need);
9392945abea338031cbe90665df60152982bfca6177Chad Versace      intel_resolve_map_remove(i);
9402945abea338031cbe90665df60152982bfca6177Chad Versace      did_resolve = true;
9412945abea338031cbe90665df60152982bfca6177Chad Versace   }
9422945abea338031cbe90665df60152982bfca6177Chad Versace
9432945abea338031cbe90665df60152982bfca6177Chad Versace   return did_resolve;
9442945abea338031cbe90665df60152982bfca6177Chad Versace}
9452945abea338031cbe90665df60152982bfca6177Chad Versace
9462945abea338031cbe90665df60152982bfca6177Chad Versacebool
9472945abea338031cbe90665df60152982bfca6177Chad Versaceintel_miptree_all_slices_resolve_hiz(struct intel_context *intel,
9482945abea338031cbe90665df60152982bfca6177Chad Versace				     struct intel_mipmap_tree *mt)
9492945abea338031cbe90665df60152982bfca6177Chad Versace{
9502945abea338031cbe90665df60152982bfca6177Chad Versace   return intel_miptree_all_slices_resolve(intel, mt,
95154308f78a2f8675bfd854761f9cd8a6b71e119d0Eric Anholt					   GEN6_HIZ_OP_HIZ_RESOLVE);
9522945abea338031cbe90665df60152982bfca6177Chad Versace}
9532945abea338031cbe90665df60152982bfca6177Chad Versace
9542945abea338031cbe90665df60152982bfca6177Chad Versacebool
9552945abea338031cbe90665df60152982bfca6177Chad Versaceintel_miptree_all_slices_resolve_depth(struct intel_context *intel,
9562945abea338031cbe90665df60152982bfca6177Chad Versace				       struct intel_mipmap_tree *mt)
9572945abea338031cbe90665df60152982bfca6177Chad Versace{
9582945abea338031cbe90665df60152982bfca6177Chad Versace   return intel_miptree_all_slices_resolve(intel, mt,
95954308f78a2f8675bfd854761f9cd8a6b71e119d0Eric Anholt					   GEN6_HIZ_OP_DEPTH_RESOLVE);
9602945abea338031cbe90665df60152982bfca6177Chad Versace}
961b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt
962f4873babdc27f697df9f4642209eca21a02ac476Chad Versace/**
963f4873babdc27f697df9f4642209eca21a02ac476Chad Versace * \brief Downsample from mt to mt->singlesample_mt.
964f4873babdc27f697df9f4642209eca21a02ac476Chad Versace *
965f4873babdc27f697df9f4642209eca21a02ac476Chad Versace * If the miptree needs no downsample, then skip.
966f4873babdc27f697df9f4642209eca21a02ac476Chad Versace */
967f4873babdc27f697df9f4642209eca21a02ac476Chad Versacevoid
968f4873babdc27f697df9f4642209eca21a02ac476Chad Versaceintel_miptree_downsample(struct intel_context *intel,
969f4873babdc27f697df9f4642209eca21a02ac476Chad Versace                         struct intel_mipmap_tree *mt)
970f4873babdc27f697df9f4642209eca21a02ac476Chad Versace{
971f4873babdc27f697df9f4642209eca21a02ac476Chad Versace   /* TODO: stub */
972f4873babdc27f697df9f4642209eca21a02ac476Chad Versace}
973f4873babdc27f697df9f4642209eca21a02ac476Chad Versace
974f4873babdc27f697df9f4642209eca21a02ac476Chad Versace/**
975f4873babdc27f697df9f4642209eca21a02ac476Chad Versace * \brief Upsample from mt->singlesample_mt to mt.
976f4873babdc27f697df9f4642209eca21a02ac476Chad Versace *
977f4873babdc27f697df9f4642209eca21a02ac476Chad Versace * The upsample is done unconditionally.
978f4873babdc27f697df9f4642209eca21a02ac476Chad Versace */
979f4873babdc27f697df9f4642209eca21a02ac476Chad Versacevoid
980f4873babdc27f697df9f4642209eca21a02ac476Chad Versaceintel_miptree_upsample(struct intel_context *intel,
981f4873babdc27f697df9f4642209eca21a02ac476Chad Versace                       struct intel_mipmap_tree *mt)
982f4873babdc27f697df9f4642209eca21a02ac476Chad Versace{
983f4873babdc27f697df9f4642209eca21a02ac476Chad Versace   /* TODO: stub */
984f4873babdc27f697df9f4642209eca21a02ac476Chad Versace}
985f4873babdc27f697df9f4642209eca21a02ac476Chad Versace
986baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholtstatic void
987baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholtintel_miptree_map_gtt(struct intel_context *intel,
988baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		      struct intel_mipmap_tree *mt,
989baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		      struct intel_miptree_map *map,
990baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		      unsigned int level, unsigned int slice)
991b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt{
992b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt   unsigned int bw, bh;
993b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt   void *base;
994b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt   unsigned int image_x, image_y;
995baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   int x = map->x;
996baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   int y = map->y;
997221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt
998b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt   /* For compressed formats, the stride is the number of bytes per
999b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt    * row of blocks.  intel_miptree_get_image_offset() already does
1000b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt    * the divide.
1001b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt    */
1002b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt   _mesa_get_format_block_size(mt->format, &bw, &bh);
1003b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt   assert(y % bh == 0);
1004b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt   y /= bh;
1005b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt
1006baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   base = intel_region_map(intel, mt->region, map->mode);
1007b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt
1008cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat   if (base == NULL)
1009cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      map->ptr = NULL;
1010cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat   else {
1011cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      /* Note that in the case of cube maps, the caller must have passed the
1012cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat       * slice number referencing the face.
1013cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      */
1014cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      intel_miptree_get_image_offset(mt, level, 0, slice, &image_x, &image_y);
1015cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      x += image_x;
1016cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      y += image_y;
1017cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat
1018cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      map->stride = mt->region->pitch * mt->cpp;
1019cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      map->ptr = base + y * map->stride + x * mt->cpp;
1020cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat   }
1021221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt
1022b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt   DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __FUNCTION__,
1023221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt       map->x, map->y, map->w, map->h,
1024b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt       mt, _mesa_get_format_name(mt->format),
1025221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt       x, y, map->ptr, map->stride);
1026b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt}
1027b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt
1028baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholtstatic void
1029baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholtintel_miptree_unmap_gtt(struct intel_context *intel,
1030baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt			struct intel_mipmap_tree *mt,
1031baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt			struct intel_miptree_map *map,
1032baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt			unsigned int level,
1033baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt			unsigned int slice)
1034baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt{
1035baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   intel_region_unmap(intel, mt->region);
1036baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt}
1037baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt
1038b48c3bca87b30003f9e117d299011380e743aec9Eric Anholtstatic void
10395655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholtintel_miptree_map_blit(struct intel_context *intel,
10405655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt		       struct intel_mipmap_tree *mt,
10415655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt		       struct intel_miptree_map *map,
10425655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt		       unsigned int level, unsigned int slice)
10435655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt{
10445655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   unsigned int image_x, image_y;
10455655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   int x = map->x;
10465655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   int y = map->y;
10475655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   int ret;
10485655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
10495655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   /* The blitter requires the pitch to be aligned to 4. */
10505655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   map->stride = ALIGN(map->w * mt->region->cpp, 4);
10515655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
10525655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   map->bo = drm_intel_bo_alloc(intel->bufmgr, "intel_miptree_map_blit() temp",
10535655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt				map->stride * map->h, 4096);
10545655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   if (!map->bo) {
10555655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt      fprintf(stderr, "Failed to allocate blit temporary\n");
10565655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt      goto fail;
10575655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   }
10585655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
10595655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   intel_miptree_get_image_offset(mt, level, 0, slice, &image_x, &image_y);
10605655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   x += image_x;
10615655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   y += image_y;
10625655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
10635655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   if (!intelEmitCopyBlit(intel,
10645655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			  mt->region->cpp,
10655655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			  mt->region->pitch, mt->region->bo,
10665655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			  0, mt->region->tiling,
10675655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			  map->stride / mt->region->cpp, map->bo,
10685655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			  0, I915_TILING_NONE,
10695655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			  x, y,
10705655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			  0, 0,
10715655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			  map->w, map->h,
10725655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			  GL_COPY)) {
10735655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt      fprintf(stderr, "Failed to blit\n");
10745655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt      goto fail;
10755655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   }
10765655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
10775655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   intel_batchbuffer_flush(intel);
10785655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   ret = drm_intel_bo_map(map->bo, (map->mode & GL_MAP_WRITE_BIT) != 0);
10795655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   if (ret) {
10805655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt      fprintf(stderr, "Failed to map blit temporary\n");
10815655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt      goto fail;
10825655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   }
10835655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
10845655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   map->ptr = map->bo->virtual;
10855655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
10865655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __FUNCTION__,
10875655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt       map->x, map->y, map->w, map->h,
10885655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt       mt, _mesa_get_format_name(mt->format),
10895655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt       x, y, map->ptr, map->stride);
10905655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
10915655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   return;
10925655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
10935655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholtfail:
10945655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   drm_intel_bo_unreference(map->bo);
10955655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   map->ptr = NULL;
10965655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   map->stride = 0;
10975655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt}
10985655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
10995655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholtstatic void
11005655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholtintel_miptree_unmap_blit(struct intel_context *intel,
11015655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			 struct intel_mipmap_tree *mt,
11025655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			 struct intel_miptree_map *map,
11035655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			 unsigned int level,
11045655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt			 unsigned int slice)
11055655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt{
11065655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   assert(!(map->mode & GL_MAP_WRITE_BIT));
11075655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
11085655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   drm_intel_bo_unmap(map->bo);
11095655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   drm_intel_bo_unreference(map->bo);
11105655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt}
11115655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt
11125655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholtstatic void
1113b48c3bca87b30003f9e117d299011380e743aec9Eric Anholtintel_miptree_map_s8(struct intel_context *intel,
1114b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt		     struct intel_mipmap_tree *mt,
1115b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt		     struct intel_miptree_map *map,
1116b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt		     unsigned int level, unsigned int slice)
1117b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt{
1118b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   map->stride = map->w;
1119b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   map->buffer = map->ptr = malloc(map->stride * map->h);
1120b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   if (!map->buffer)
1121b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      return;
1122b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
1123b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   /* One of either READ_BIT or WRITE_BIT or both is set.  READ_BIT implies no
1124b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt    * INVALIDATE_RANGE_BIT.  WRITE_BIT needs the original values read in unless
1125b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt    * invalidate is set, since we'll be writing the whole rectangle from our
1126b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt    * temporary buffer back out.
1127b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt    */
1128b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
1129b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      uint8_t *untiled_s8_map = map->ptr;
1130b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      uint8_t *tiled_s8_map = intel_region_map(intel, mt->region,
1131b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt					       GL_MAP_READ_BIT);
1132b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      unsigned int image_x, image_y;
1133b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
1134b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      intel_miptree_get_image_offset(mt, level, 0, slice, &image_x, &image_y);
1135b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
1136b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      for (uint32_t y = 0; y < map->h; y++) {
1137b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	 for (uint32_t x = 0; x < map->w; x++) {
1138b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	    ptrdiff_t offset = intel_offset_S8(mt->region->pitch,
1139b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	                                       x + image_x + map->x,
1140f172eae8b23d0612865895c52af745021ae20a4cDaniel Vetter	                                       y + image_y + map->y,
1141f172eae8b23d0612865895c52af745021ae20a4cDaniel Vetter					       intel->has_swizzling);
1142b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	    untiled_s8_map[y * map->w + x] = tiled_s8_map[offset];
1143b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	 }
1144b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      }
1145b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
1146b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      intel_region_unmap(intel, mt->region);
1147b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
1148b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      DBG("%s: %d,%d %dx%d from mt %p %d,%d = %p/%d\n", __FUNCTION__,
1149b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	  map->x, map->y, map->w, map->h,
1150b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	  mt, map->x + image_x, map->y + image_y, map->ptr, map->stride);
1151b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   } else {
1152b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __FUNCTION__,
1153b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	  map->x, map->y, map->w, map->h,
1154b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	  mt, map->ptr, map->stride);
1155b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   }
1156b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt}
1157b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
1158b48c3bca87b30003f9e117d299011380e743aec9Eric Anholtstatic void
1159b48c3bca87b30003f9e117d299011380e743aec9Eric Anholtintel_miptree_unmap_s8(struct intel_context *intel,
1160b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt		       struct intel_mipmap_tree *mt,
1161b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt		       struct intel_miptree_map *map,
1162b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt		       unsigned int level,
1163b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt		       unsigned int slice)
1164b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt{
1165b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   if (map->mode & GL_MAP_WRITE_BIT) {
1166b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      unsigned int image_x, image_y;
1167b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      uint8_t *untiled_s8_map = map->ptr;
1168b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      uint8_t *tiled_s8_map = intel_region_map(intel, mt->region, map->mode);
1169b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
1170b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      intel_miptree_get_image_offset(mt, level, 0, slice, &image_x, &image_y);
1171b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
1172b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      for (uint32_t y = 0; y < map->h; y++) {
1173b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	 for (uint32_t x = 0; x < map->w; x++) {
1174b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	    ptrdiff_t offset = intel_offset_S8(mt->region->pitch,
1175b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	                                       x + map->x,
1176f172eae8b23d0612865895c52af745021ae20a4cDaniel Vetter	                                       y + map->y,
1177f172eae8b23d0612865895c52af745021ae20a4cDaniel Vetter					       intel->has_swizzling);
1178b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	    tiled_s8_map[offset] = untiled_s8_map[y * map->w + x];
1179b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt	 }
1180b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      }
1181b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
1182b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      intel_region_unmap(intel, mt->region);
1183b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   }
1184b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
1185b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   free(map->buffer);
1186b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt}
1187b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt
1188a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versacestatic void
1189a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versaceintel_miptree_map_etc1(struct intel_context *intel,
1190a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace                       struct intel_mipmap_tree *mt,
1191a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace                       struct intel_miptree_map *map,
1192a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace                       unsigned int level,
1193a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace                       unsigned int slice)
1194a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace{
1195a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   /* For justification of these invariants,
1196a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace    * see intel_mipmap_tree:wraps_etc1.
1197a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace    */
1198a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   assert(mt->wraps_etc1);
1199a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   assert(mt->format == MESA_FORMAT_RGBX8888_REV);
1200a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace
1201a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   /* From the GL_OES_compressed_ETC1_RGB8_texture spec:
1202a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace    *   INVALID_OPERATION is generated by CompressedTexSubImage2D,
1203a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace    *   TexSubImage2D, or CopyTexSubImage2D if the texture image <level>
1204a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace    *   bound to <target> has internal format ETC1_RGB8_OES.
1205a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace    *
1206a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace    * This implies that intel_miptree_map_etc1() can only be called from
1207a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace    * glCompressedTexImage2D, and hence the assertions below hold.
1208a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace    */
1209a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   assert(map->mode & GL_MAP_WRITE_BIT);
1210a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   assert(map->mode & GL_MAP_INVALIDATE_RANGE_BIT);
1211a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   assert(map->x == 0);
1212a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   assert(map->y == 0);
1213a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace
1214a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   /* Each ETC1 block contains 4x4 pixels in 8 bytes. */
1215a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   map->stride = 2 * map->w;
1216a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   map->buffer = map->ptr = malloc(map->stride * map->h);
1217a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace}
1218a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace
1219a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versacestatic void
1220a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versaceintel_miptree_unmap_etc1(struct intel_context *intel,
1221a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace                         struct intel_mipmap_tree *mt,
1222a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace                         struct intel_miptree_map *map,
1223a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace                         unsigned int level,
1224a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace                         unsigned int slice)
1225a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace{
1226a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   uint32_t image_x;
1227a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   uint32_t image_y;
1228a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   intel_miptree_get_image_offset(mt, level, 0, slice, &image_x, &image_y);
1229a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace
1230a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   uint8_t *xbgr = intel_region_map(intel, mt->region, map->mode)
1231a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace                 + image_y * mt->region->pitch * mt->region->cpp
1232a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace                 + image_x * mt->region->cpp;
1233a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace
1234a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   _mesa_etc1_unpack_rgba8888(xbgr, mt->region->pitch * mt->region->cpp,
1235a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace                              map->ptr, map->stride,
1236a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace                              map->w, map->h);
1237a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace
1238a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   intel_region_unmap(intel, mt->region);
1239a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   free(map->buffer);
1240a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace}
1241a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace
1242ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt/**
1243ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt * Mapping function for packed depth/stencil miptrees backed by real separate
1244ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt * miptrees for depth and stencil.
1245ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt *
1246ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt * On gen7, and to support HiZ pre-gen7, we have to have the stencil buffer
1247ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt * separate from the depth buffer.  Yet at the GL API level, we have to expose
1248ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt * packed depth/stencil textures and FBO attachments, and Mesa core expects to
1249ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt * be able to map that memory for texture storage and glReadPixels-type
1250ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt * operations.  We give Mesa core that access by mallocing a temporary and
1251ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt * copying the data between the actual backing store and the temporary.
1252ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt */
1253ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholtstatic void
1254ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholtintel_miptree_map_depthstencil(struct intel_context *intel,
1255ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt			       struct intel_mipmap_tree *mt,
1256ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt			       struct intel_miptree_map *map,
1257ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt			       unsigned int level, unsigned int slice)
1258ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt{
1259ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   struct intel_mipmap_tree *z_mt = mt;
1260ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   struct intel_mipmap_tree *s_mt = mt->stencil_mt;
1261353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt   bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z32_FLOAT;
1262353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt   int packed_bpp = map_z32f_x24s8 ? 8 : 4;
1263ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1264ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   map->stride = map->w * packed_bpp;
1265ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   map->buffer = map->ptr = malloc(map->stride * map->h);
1266ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   if (!map->buffer)
1267ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      return;
1268ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1269ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   /* One of either READ_BIT or WRITE_BIT or both is set.  READ_BIT implies no
1270ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt    * INVALIDATE_RANGE_BIT.  WRITE_BIT needs the original values read in unless
1271ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt    * invalidate is set, since we'll be writing the whole rectangle from our
1272ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt    * temporary buffer back out.
1273ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt    */
1274ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
1275ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      uint32_t *packed_map = map->ptr;
1276ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      uint8_t *s_map = intel_region_map(intel, s_mt->region, GL_MAP_READ_BIT);
1277ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      uint32_t *z_map = intel_region_map(intel, z_mt->region, GL_MAP_READ_BIT);
1278ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      unsigned int s_image_x, s_image_y;
1279ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      unsigned int z_image_x, z_image_y;
1280ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1281ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_miptree_get_image_offset(s_mt, level, 0, slice,
1282ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				     &s_image_x, &s_image_y);
1283ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_miptree_get_image_offset(z_mt, level, 0, slice,
1284ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				     &z_image_x, &z_image_y);
1285ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1286ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      for (uint32_t y = 0; y < map->h; y++) {
1287ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	 for (uint32_t x = 0; x < map->w; x++) {
1288ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	    int map_x = map->x + x, map_y = map->y + y;
1289ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	    ptrdiff_t s_offset = intel_offset_S8(s_mt->region->pitch,
1290ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt						 map_x + s_image_x,
1291f172eae8b23d0612865895c52af745021ae20a4cDaniel Vetter						 map_y + s_image_y,
1292f172eae8b23d0612865895c52af745021ae20a4cDaniel Vetter						 intel->has_swizzling);
1293ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	    ptrdiff_t z_offset = ((map_y + z_image_y) * z_mt->region->pitch +
1294ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				  (map_x + z_image_x));
1295ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	    uint8_t s = s_map[s_offset];
1296ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	    uint32_t z = z_map[z_offset];
1297ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1298353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	    if (map_z32f_x24s8) {
1299353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	       packed_map[(y * map->w + x) * 2 + 0] = z;
1300353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	       packed_map[(y * map->w + x) * 2 + 1] = s;
1301353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	    } else {
1302353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	       packed_map[y * map->w + x] = (s << 24) | (z & 0x00ffffff);
1303353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	    }
1304ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	 }
1305ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      }
1306ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1307ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_region_unmap(intel, s_mt->region);
1308ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_region_unmap(intel, z_mt->region);
1309ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1310ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      DBG("%s: %d,%d %dx%d from z mt %p %d,%d, s mt %p %d,%d = %p/%d\n",
1311ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  __FUNCTION__,
1312ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  map->x, map->y, map->w, map->h,
1313ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  z_mt, map->x + z_image_x, map->y + z_image_y,
1314ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  s_mt, map->x + s_image_x, map->y + s_image_y,
1315ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  map->ptr, map->stride);
1316ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   } else {
1317ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __FUNCTION__,
1318ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  map->x, map->y, map->w, map->h,
1319ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  mt, map->ptr, map->stride);
1320ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   }
1321ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt}
1322ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1323ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholtstatic void
1324ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholtintel_miptree_unmap_depthstencil(struct intel_context *intel,
1325ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				 struct intel_mipmap_tree *mt,
1326ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				 struct intel_miptree_map *map,
1327ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				 unsigned int level,
1328ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				 unsigned int slice)
1329ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt{
1330ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   struct intel_mipmap_tree *z_mt = mt;
1331ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   struct intel_mipmap_tree *s_mt = mt->stencil_mt;
1332353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt   bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z32_FLOAT;
1333ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1334ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   if (map->mode & GL_MAP_WRITE_BIT) {
1335ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      uint32_t *packed_map = map->ptr;
1336ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      uint8_t *s_map = intel_region_map(intel, s_mt->region, map->mode);
1337ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      uint32_t *z_map = intel_region_map(intel, z_mt->region, map->mode);
1338ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      unsigned int s_image_x, s_image_y;
1339ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      unsigned int z_image_x, z_image_y;
1340ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1341ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_miptree_get_image_offset(s_mt, level, 0, slice,
1342ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				     &s_image_x, &s_image_y);
1343ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_miptree_get_image_offset(z_mt, level, 0, slice,
1344ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				     &z_image_x, &z_image_y);
1345ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1346ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      for (uint32_t y = 0; y < map->h; y++) {
1347ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	 for (uint32_t x = 0; x < map->w; x++) {
1348ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	    ptrdiff_t s_offset = intel_offset_S8(s_mt->region->pitch,
1349ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt						 x + s_image_x + map->x,
1350f172eae8b23d0612865895c52af745021ae20a4cDaniel Vetter						 y + s_image_y + map->y,
1351f172eae8b23d0612865895c52af745021ae20a4cDaniel Vetter						 intel->has_swizzling);
1352ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	    ptrdiff_t z_offset = ((y + z_image_y) * z_mt->region->pitch +
1353ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt				  (x + z_image_x));
1354ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1355353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	    if (map_z32f_x24s8) {
1356353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	       z_map[z_offset] = packed_map[(y * map->w + x) * 2 + 0];
1357353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	       s_map[s_offset] = packed_map[(y * map->w + x) * 2 + 1];
1358353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	    } else {
1359353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	       uint32_t packed = packed_map[y * map->w + x];
1360353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	       s_map[s_offset] = packed >> 24;
1361353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	       z_map[z_offset] = packed;
1362353f7ba4abbfd15d1e4e4b7a38f88e64ce85f6a2Eric Anholt	    }
1363ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	 }
1364ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      }
1365ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1366ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_region_unmap(intel, s_mt->region);
1367ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_region_unmap(intel, z_mt->region);
1368ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1369ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      DBG("%s: %d,%d %dx%d from z mt %p (%s) %d,%d, s mt %p %d,%d = %p/%d\n",
1370ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  __FUNCTION__,
1371ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  map->x, map->y, map->w, map->h,
1372ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  z_mt, _mesa_get_format_name(z_mt->format),
1373ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  map->x + z_image_x, map->y + z_image_y,
1374ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  s_mt, map->x + s_image_x, map->y + s_image_y,
1375ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt	  map->ptr, map->stride);
1376ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   }
1377ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1378ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   free(map->buffer);
1379ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt}
1380ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt
1381baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholtvoid
1382baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholtintel_miptree_map(struct intel_context *intel,
1383baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  struct intel_mipmap_tree *mt,
1384baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  unsigned int level,
1385baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  unsigned int slice,
1386baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  unsigned int x,
1387baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  unsigned int y,
1388baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  unsigned int w,
1389baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  unsigned int h,
1390baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  GLbitfield mode,
1391baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  void **out_ptr,
1392baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt		  int *out_stride)
1393baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt{
1394baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   struct intel_miptree_map *map;
1395baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt
1396baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   map = calloc(1, sizeof(struct intel_miptree_map));
1397baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   if (!map){
1398baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt      *out_ptr = NULL;
1399baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt      *out_stride = 0;
1400baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt      return;
1401baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   }
1402baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt
1403baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   assert(!mt->level[level].slice[slice].map);
1404baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   mt->level[level].slice[slice].map = map;
1405baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   map->mode = mode;
1406baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   map->x = x;
1407baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   map->y = y;
1408baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   map->w = w;
1409baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   map->h = h;
1410baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt
1411baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   intel_miptree_slice_resolve_depth(intel, mt, level, slice);
1412baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   if (map->mode & GL_MAP_WRITE_BIT) {
1413baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt      intel_miptree_slice_set_needs_hiz_resolve(mt, level, slice);
1414baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   }
1415baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt
1416b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   if (mt->format == MESA_FORMAT_S8) {
1417b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      intel_miptree_map_s8(intel, mt, map, level, slice);
1418a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   } else if (mt->wraps_etc1) {
1419a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace      intel_miptree_map_etc1(intel, mt, map, level, slice);
1420ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   } else if (mt->stencil_mt) {
1421ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_miptree_map_depthstencil(intel, mt, map, level, slice);
142284e5f1c635899c657da58ca51d5e841354e9de9cEugeni Dodonov   } else if (intel->has_llc &&
14235655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt	      !(mode & GL_MAP_WRITE_BIT) &&
14245655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt	      !mt->compressed &&
14255655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt	      mt->region->tiling == I915_TILING_X) {
14265655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt      intel_miptree_map_blit(intel, mt, map, level, slice);
1427b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   } else {
1428b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      intel_miptree_map_gtt(intel, mt, map, level, slice);
1429b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   }
1430baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt
1431baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   *out_ptr = map->ptr;
1432baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt   *out_stride = map->stride;
1433cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat
1434cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat   if (map->ptr == NULL) {
1435cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      mt->level[level].slice[slice].map = NULL;
1436cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat      free(map);
1437cdcfd5d1d60179e60e3a0a47dda71bfe91083105Anuj Phogat   }
1438baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt}
1439baeaa062e92afbec47fad73fd3d464a1e7d1fe08Eric Anholt
1440b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholtvoid
1441b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholtintel_miptree_unmap(struct intel_context *intel,
1442b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt		    struct intel_mipmap_tree *mt,
1443b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt		    unsigned int level,
1444b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt		    unsigned int slice)
1445b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt{
1446221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt   struct intel_miptree_map *map = mt->level[level].slice[slice].map;
1447221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt
1448221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt   if (!map)
1449221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt      return;
1450221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt
1451b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt   DBG("%s: mt %p (%s) level %d slice %d\n", __FUNCTION__,
1452b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt       mt, _mesa_get_format_name(mt->format), level, slice);
1453b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt
1454b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   if (mt->format == MESA_FORMAT_S8) {
1455b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      intel_miptree_unmap_s8(intel, mt, map, level, slice);
1456a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace   } else if (mt->wraps_etc1) {
1457a5a34b153d494ad4374e7b2c8ea13b1073a887e2Chad Versace      intel_miptree_unmap_etc1(intel, mt, map, level, slice);
1458ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt   } else if (mt->stencil_mt) {
1459ed3aeb56ec3fde24c2fc69515c0b5d348b41caf3Eric Anholt      intel_miptree_unmap_depthstencil(intel, mt, map, level, slice);
14605655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt   } else if (map->bo) {
14615655ebf4664b2e9a50b976612f8a1aeb26275311Eric Anholt      intel_miptree_unmap_blit(intel, mt, map, level, slice);
1462b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   } else {
1463b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt      intel_miptree_unmap_gtt(intel, mt, map, level, slice);
1464b48c3bca87b30003f9e117d299011380e743aec9Eric Anholt   }
1465221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt
1466221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt   mt->level[level].slice[slice].map = NULL;
1467221a36514b4ecffdaa3be5c43e67c75cc8c30ab8Eric Anholt   free(map);
1468b75291c61c40a3a690b08f8aa013ad2d3d2deda8Eric Anholt}
1469