intel_mipmap_tree.c revision d913a15898fc8edc68ee673e2ae038cf2d7e8af9
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
2877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt#include "intel_context.h"
2977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt#include "intel_mipmap_tree.h"
3077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt#include "intel_regions.h"
31d913a15898fc8edc68ee673e2ae038cf2d7e8af9Eric Anholt#include "intel_chipset.h"
3277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt#include "enums.h"
3377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
3477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt#define FILE_DEBUG_FLAG DEBUG_MIPTREE
3577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
3677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtstatic GLenum
3777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholttarget_to_target(GLenum target)
3877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
3977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   switch (target) {
4077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
4177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
4277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
4377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
4477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
4577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
4677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      return GL_TEXTURE_CUBE_MAP_ARB;
4777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   default:
4877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      return target;
4977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   }
5077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
5177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
5277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtstruct intel_mipmap_tree *
5377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtintel_miptree_create(struct intel_context *intel,
54659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt		     GLenum target,
55659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt		     GLenum internal_format,
56659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt		     GLuint first_level,
57659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt		     GLuint last_level,
58659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt		     GLuint width0,
59659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt		     GLuint height0,
60659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt		     GLuint depth0,
61659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt		     GLuint cpp,
62659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt		     GLuint compress_byte)
6377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
6477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   GLboolean ok;
6577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
6677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
6777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   DBG("%s target %s format %s level %d..%d\n", __FUNCTION__,
6877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt       _mesa_lookup_enum_by_nr(target),
6977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt       _mesa_lookup_enum_by_nr(internal_format), first_level, last_level);
7077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
7177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->target = target_to_target(target);
7277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->internal_format = internal_format;
7377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->first_level = first_level;
7477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->last_level = last_level;
7577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->width0 = width0;
7677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->height0 = height0;
7777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->depth0 = depth0;
7877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->cpp = compress_byte ? compress_byte : cpp;
7977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->compressed = compress_byte ? 1 : 0;
8077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->refcount = 1;
8177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
82c0b4257aa9ba783674ccf7162799385734dff211Eric Anholt#ifdef I915
83c0b4257aa9ba783674ccf7162799385734dff211Eric Anholt   if (IS_945(intel->intelScreen->deviceID))
8477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      ok = i945_miptree_layout(mt);
85c0b4257aa9ba783674ccf7162799385734dff211Eric Anholt   else
8677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      ok = i915_miptree_layout(mt);
87c0b4257aa9ba783674ccf7162799385734dff211Eric Anholt#else
88c0b4257aa9ba783674ccf7162799385734dff211Eric Anholt   ok = brw_miptree_layout(mt);
89c0b4257aa9ba783674ccf7162799385734dff211Eric Anholt#endif
9077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
9177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   if (ok) {
92c0b4257aa9ba783674ccf7162799385734dff211Eric Anholt#ifdef I915
9377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      if (!mt->compressed) {
9477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt	 int align;
9577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
967c71ef3a3d0cf2620525f468960cdc76a0fb0d33Eric Anholt	 if (intel->ttm) {
9777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt	    /* XXX: Align pitch to multiple of 64 bytes for now to allow
9877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt	     * render-to-texture to work in all cases. This should probably be
9977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt	     * replaced at some point by some scheme to only do this when really
10077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt	     * necessary.
10177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt	     */
10277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt	    align = 63;
10377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt	 } else {
10477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt	    align = 3;
10577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt	 }
10677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
10777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt	 mt->pitch = (mt->pitch * cpp + align) & ~align;
10877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
10977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt	 /* XXX: At least the i915 seems very upset when the pitch is a multiple
11077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt	  * of 1024 and sometimes 512 bytes - performance can drop by several
11177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt	  * times. Go to the next multiple of the required alignment for now.
11277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt	  */
11377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt	 if (!(mt->pitch & 511))
11477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt	    mt->pitch += align + 1;
11577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
11677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt	 mt->pitch /= cpp;
11777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      }
118c0b4257aa9ba783674ccf7162799385734dff211Eric Anholt#endif /* I915 */
11977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
1207c71ef3a3d0cf2620525f468960cdc76a0fb0d33Eric Anholt      mt->region = intel_region_alloc(intel,
12177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt                                      mt->cpp, mt->pitch, mt->total_height);
12277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   }
12377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
12477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   if (!mt->region) {
12577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      free(mt);
12677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      return NULL;
12777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   }
12877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
12977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   return mt;
13077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
13177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
13277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
13377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtvoid
13477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtintel_miptree_reference(struct intel_mipmap_tree **dst,
13577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt                        struct intel_mipmap_tree *src)
13677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
13777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   src->refcount++;
13877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   *dst = src;
13977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   DBG("%s %p refcount now %d\n", __FUNCTION__, src, src->refcount);
14077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
14177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
14277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtvoid
14377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtintel_miptree_release(struct intel_context *intel,
14477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt                      struct intel_mipmap_tree **mt)
14577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
14677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   if (!*mt)
14777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      return;
14877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
14977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   DBG("%s %p refcount will be %d\n", __FUNCTION__, *mt, (*mt)->refcount - 1);
15077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   if (--(*mt)->refcount <= 0) {
15177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      GLuint i;
15277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
15377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      DBG("%s deleting %p\n", __FUNCTION__, *mt);
15477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
15577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      intel_region_release(&((*mt)->region));
15677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
15777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      for (i = 0; i < MAX_TEXTURE_LEVELS; i++)
15877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt         if ((*mt)->level[i].image_offset)
15977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt            free((*mt)->level[i].image_offset);
16077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
16177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      free(*mt);
16277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   }
16377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   *mt = NULL;
16477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
16577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
16677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
16777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
16877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
16977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt/* Can the image be pulled into a unified mipmap tree.  This mirrors
17077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * the completeness test in a lot of ways.
17177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt *
17277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * Not sure whether I want to pass gl_texture_image here.
17377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt */
17477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric AnholtGLboolean
17577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtintel_miptree_match_image(struct intel_mipmap_tree *mt,
17677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt                          struct gl_texture_image *image,
17777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt                          GLuint face, GLuint level)
17877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
17977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   /* Images with borders are never pulled into mipmap trees.
18077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt    */
18177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   if (image->Border)
18277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      return GL_FALSE;
18377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
18477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   if (image->InternalFormat != mt->internal_format ||
18577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt       image->IsCompressed != mt->compressed)
18677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      return GL_FALSE;
18777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
18877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   /* Test image dimensions against the base level image adjusted for
18977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt    * minification.  This will also catch images not present in the
19077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt    * tree, changed targets, etc.
19177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt    */
19277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   if (image->Width != mt->level[level].width ||
19377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt       image->Height != mt->level[level].height ||
19477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt       image->Depth != mt->level[level].depth)
19577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      return GL_FALSE;
19677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
19777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   return GL_TRUE;
19877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
19977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
20077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
20177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtvoid
20277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtintel_miptree_set_level_info(struct intel_mipmap_tree *mt,
203659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			     GLuint level,
204659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			     GLuint nr_images,
205659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			     GLuint x, GLuint y,
206659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			     GLuint w, GLuint h, GLuint d)
20777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
20877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->level[level].width = w;
20977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->level[level].height = h;
21077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->level[level].depth = d;
21177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->level[level].level_offset = (x + y * mt->pitch) * mt->cpp;
21277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->level[level].nr_images = nr_images;
21377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
21477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
21577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt       level, w, h, d, x, y, mt->level[level].level_offset);
21677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
21777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   /* Not sure when this would happen, but anyway:
21877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt    */
21977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   if (mt->level[level].image_offset) {
22077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      free(mt->level[level].image_offset);
22177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      mt->level[level].image_offset = NULL;
22277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   }
22377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
22477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   assert(nr_images);
22577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
22677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->level[level].image_offset = malloc(nr_images * sizeof(GLuint));
22777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->level[level].image_offset[0] = 0;
22877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
22977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
23077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
23177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
23277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtvoid
23377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtintel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
234659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			       GLuint level, GLuint img,
235659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			       GLuint x, GLuint y)
23677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
23777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   if (img == 0 && level == 0)
23877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      assert(x == 0 && y == 0);
23977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
24077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   assert(img < mt->level[level].nr_images);
24177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
24277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   mt->level[level].image_offset[img] = (x + y * mt->pitch);
24377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
24477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   DBG("%s level %d img %d pos %d,%d image_offset %x\n",
24577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt       __FUNCTION__, level, img, x, y, mt->level[level].image_offset[img]);
24677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
24777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
24877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
24977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt/* Although we use the image_offset[] array to store relative offsets
25077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * to cube faces, Mesa doesn't know anything about this and expects
25177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * each cube face to be treated as a separate image.
25277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt *
25377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * These functions present that view to mesa:
25477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt */
25577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtconst GLuint *
25677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtintel_miptree_depth_offsets(struct intel_mipmap_tree *mt, GLuint level)
25777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
25877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   static const GLuint zero = 0;
25977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
26077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   if (mt->target != GL_TEXTURE_3D || mt->level[level].nr_images == 1)
26177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      return &zero;
26277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   else
26377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      return mt->level[level].image_offset;
26477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
26577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
26677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
26777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric AnholtGLuint
268659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholtintel_miptree_image_offset(struct intel_mipmap_tree *mt,
269659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			   GLuint face, GLuint level)
27077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
27177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   if (mt->target == GL_TEXTURE_CUBE_MAP_ARB)
27277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      return (mt->level[level].level_offset +
273659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt	      mt->level[level].image_offset[face] * mt->cpp);
27477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   else
27577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      return mt->level[level].level_offset;
27677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
27777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
27877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
27977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
28077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt/**
28177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * Map a teximage in a mipmap tree.
28277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * \param row_stride  returns row stride in bytes
28377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * \param image_stride  returns image stride in bytes (for 3D textures).
28477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt * \return address of mapping
28577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt */
28677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric AnholtGLubyte *
28777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtintel_miptree_image_map(struct intel_context * intel,
28877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt                        struct intel_mipmap_tree * mt,
28977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt                        GLuint face,
29077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt                        GLuint level,
29177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt                        GLuint * row_stride, GLuint * image_offsets)
29277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
29377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   DBG("%s \n", __FUNCTION__);
29477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
29577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   if (row_stride)
29677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      *row_stride = mt->pitch * mt->cpp;
29777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
29877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   if (image_offsets)
29977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      memcpy(image_offsets, mt->level[level].image_offset,
30077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt             mt->level[level].depth * sizeof(GLuint));
30177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
3027c71ef3a3d0cf2620525f468960cdc76a0fb0d33Eric Anholt   return (intel_region_map(intel, mt->region) +
30377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt           intel_miptree_image_offset(mt, face, level));
30477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
30577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
30677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtvoid
30777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtintel_miptree_image_unmap(struct intel_context *intel,
30877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt                          struct intel_mipmap_tree *mt)
30977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
31077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   DBG("%s\n", __FUNCTION__);
3117c71ef3a3d0cf2620525f468960cdc76a0fb0d33Eric Anholt   intel_region_unmap(intel, mt->region);
31277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
31377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
31477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
31577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
31677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt/* Upload data for a particular image.
31777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt */
31877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtvoid
31977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtintel_miptree_image_data(struct intel_context *intel,
320659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			 struct intel_mipmap_tree *dst,
321659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			 GLuint face,
322659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			 GLuint level,
323659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			 void *src,
324659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			 GLuint src_row_pitch,
325659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			 GLuint src_image_pitch)
32677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
32777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   GLuint depth = dst->level[level].depth;
32877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   GLuint dst_offset = intel_miptree_image_offset(dst, face, level);
32977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   const GLuint *dst_depth_offset = intel_miptree_depth_offsets(dst, level);
33077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   GLuint i;
33177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   GLuint height = 0;
33277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
33377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   DBG("%s\n", __FUNCTION__);
33477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   for (i = 0; i < depth; i++) {
33577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      height = dst->level[level].height;
33677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      if(dst->compressed)
33777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt	 height /= 4;
338659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt      intel_region_data(intel,
339659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			dst->region,
340659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			dst_offset + dst_depth_offset[i], /* dst_offset */
341659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			0, 0,                             /* dstx, dsty */
342659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			src,
343659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			src_row_pitch,
344659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			0, 0,                             /* source x, y */
345659baa3f25275b622dad626992af60f3c9ea6d66Eric Anholt			dst->level[level].width, height); /* width, height */
34677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
34777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt      src += src_image_pitch * dst->cpp;
34877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   }
34977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
35077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
35177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtextern GLuint intel_compressed_alignment(GLenum);
35277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt/* Copy mipmap image between trees
35377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt */
35477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtvoid
35577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtintel_miptree_image_copy(struct intel_context *intel,
35677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt                         struct intel_mipmap_tree *dst,
35777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt                         GLuint face, GLuint level,
35877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt                         struct intel_mipmap_tree *src)
35977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt{
36077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   GLuint width = src->level[level].width;
36177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   GLuint height = src->level[level].height;
36277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   GLuint depth = src->level[level].depth;
36377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   GLuint dst_offset = intel_miptree_image_offset(dst, face, level);
36477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   GLuint src_offset = intel_miptree_image_offset(src, face, level);
36577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   const GLuint *dst_depth_offset = intel_miptree_depth_offsets(dst, level);
36677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   const GLuint *src_depth_offset = intel_miptree_depth_offsets(src, level);
36777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   GLuint i;
36877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
36977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   if (dst->compressed) {
37077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt       GLuint alignment = intel_compressed_alignment(dst->internal_format);
37177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt       height = (height + 3) / 4;
37277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt       width = ((width + alignment - 1) & ~(alignment - 1));
37377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   }
37477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
37577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   for (i = 0; i < depth; i++) {
3767c71ef3a3d0cf2620525f468960cdc76a0fb0d33Eric Anholt      intel_region_copy(intel,
37777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt                        dst->region, dst_offset + dst_depth_offset[i],
37877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt                        0,
37977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt                        0,
38077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt                        src->region, src_offset + src_depth_offset[i],
38177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt                        0, 0, width, height);
38277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt   }
38377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
38477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt}
385