19f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt/*
29f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt Copyright (C) Intel Corp.  2006.  All Rights Reserved.
39f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
49f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt develop this 3D driver.
59f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
69f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt Permission is hereby granted, free of charge, to any person obtaining
79f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt a copy of this software and associated documentation files (the
89f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt "Software"), to deal in the Software without restriction, including
99f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt without limitation the rights to use, copy, modify, merge, publish,
109f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt distribute, sublicense, and/or sell copies of the Software, and to
119f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt permit persons to whom the Software is furnished to do so, subject to
129f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt the following conditions:
139f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
149f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt The above copyright notice and this permission notice (including the
159f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt next paragraph) shall be included in all copies or substantial
169f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt portions of the Software.
179f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
189f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
199f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
209f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
219f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
229f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
239f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
249f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
259f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
269f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt **********************************************************************/
279f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt /*
289f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt  * Authors:
299f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt  *   Keith Whitwell <keith@tungstengraphics.com>
309f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt  */
319f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
329f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt/* Code to layout images in a mipmap tree for i965.
339f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt */
349f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
359f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt#include "intel_mipmap_tree.h"
363416ef303af633668cece0b199b4a8b2388c1e2fMichel Dänzer#include "intel_tex_layout.h"
37e3a6e60040b7f6ea7965e52f8f9881ed31e0347cEric Anholt#include "intel_context.h"
38ecadb51bbcb972a79f3ed79e65a7986b9396e757Brian Paul#include "main/macros.h"
399f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
4027674c41352dc78ad79f67cebca16d7896cd2093Eric Anholt#define FILE_DEBUG_FLAG DEBUG_MIPTREE
4127674c41352dc78ad79f67cebca16d7896cd2093Eric Anholt
422e0aefc1b9023a3d4003c1974489252d29b1d65bEric Anholtstatic void
432e0aefc1b9023a3d4003c1974489252d29b1d65bEric Anholtbrw_miptree_layout_texture_array(struct intel_context *intel,
44d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace				 struct intel_mipmap_tree *mt)
452e0aefc1b9023a3d4003c1974489252d29b1d65bEric Anholt{
462e0aefc1b9023a3d4003c1974489252d29b1d65bEric Anholt   GLuint level;
472e0aefc1b9023a3d4003c1974489252d29b1d65bEric Anholt   GLuint qpitch = 0;
482e0aefc1b9023a3d4003c1974489252d29b1d65bEric Anholt   int h0, h1, q;
492e0aefc1b9023a3d4003c1974489252d29b1d65bEric Anholt
50017c13d55b5b086774d6afea2ca754482c624c6aChad Versace   h0 = ALIGN(mt->height0, mt->align_h);
51017c13d55b5b086774d6afea2ca754482c624c6aChad Versace   h1 = ALIGN(minify(mt->height0), mt->align_h);
52455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry   if (mt->array_spacing_lod0)
53455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry      qpitch = h0;
54455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry   else
55455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry      qpitch = (h0 + h1 + (intel->gen >= 7 ? 12 : 11) * mt->align_h);
562e0aefc1b9023a3d4003c1974489252d29b1d65bEric Anholt   if (mt->compressed)
572e0aefc1b9023a3d4003c1974489252d29b1d65bEric Anholt      qpitch /= 4;
582e0aefc1b9023a3d4003c1974489252d29b1d65bEric Anholt
59d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   i945_miptree_layout_2d(mt);
602e0aefc1b9023a3d4003c1974489252d29b1d65bEric Anholt
612e0aefc1b9023a3d4003c1974489252d29b1d65bEric Anholt   for (level = mt->first_level; level <= mt->last_level; level++) {
62d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      for (q = 0; q < mt->depth0; q++) {
632e0aefc1b9023a3d4003c1974489252d29b1d65bEric Anholt	 intel_miptree_set_image_offset(mt, level, q, 0, q * qpitch);
642e0aefc1b9023a3d4003c1974489252d29b1d65bEric Anholt      }
652e0aefc1b9023a3d4003c1974489252d29b1d65bEric Anholt   }
66d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace   mt->total_height = qpitch * mt->depth0;
672e0aefc1b9023a3d4003c1974489252d29b1d65bEric Anholt}
682e0aefc1b9023a3d4003c1974489252d29b1d65bEric Anholt
69d7cdbc3c5415d2dd5eee58834b13714a89eacf2bKenneth Graunkevoid
70d7cdbc3c5415d2dd5eee58834b13714a89eacf2bKenneth Graunkebrw_miptree_layout(struct intel_context *intel, struct intel_mipmap_tree *mt)
719f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
729f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   switch (mt->target) {
73bd0861e2742c22e4bce83bce40dfdbfd713835dfEric Anholt   case GL_TEXTURE_CUBE_MAP:
74956f866030f7bea5fc4a2de28c72e60bdc3a5b3dZhenyu Wang      if (intel->gen >= 5) {
752e0aefc1b9023a3d4003c1974489252d29b1d65bEric Anholt	 /* On Ironlake, cube maps are finally represented as just a series of
762e0aefc1b9023a3d4003c1974489252d29b1d65bEric Anholt	  * MIPLAYOUT_BELOW 2D textures (like 2D texture arrays), separated by a
772e0aefc1b9023a3d4003c1974489252d29b1d65bEric Anholt	  * pitch of qpitch rows, where qpitch is defined by the equation given
782e0aefc1b9023a3d4003c1974489252d29b1d65bEric Anholt	  * in Volume 1 of the BSpec.
792e0aefc1b9023a3d4003c1974489252d29b1d65bEric Anholt	  */
80d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace	 brw_miptree_layout_texture_array(intel, mt);
812e0aefc1b9023a3d4003c1974489252d29b1d65bEric Anholt	 break;
822995bf0d68f1b28ba68b81e9dc79e3ab52bc2795Xiang, Haihao      }
838a472427559ea33186c71dfbab8254651fec3077Ian Romanick      assert(mt->depth0 == 6);
842e0aefc1b9023a3d4003c1974489252d29b1d65bEric Anholt      /* FALLTHROUGH */
852995bf0d68f1b28ba68b81e9dc79e3ab52bc2795Xiang, Haihao
869f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_TEXTURE_3D: {
879f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      GLuint width  = mt->width0;
889f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      GLuint height = mt->height0;
899f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      GLuint depth = mt->depth0;
909f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      GLuint pack_x_pitch, pack_x_nr;
919f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      GLuint pack_y_pitch;
929f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      GLuint level;
939f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
949f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      mt->total_height = 0;
95bd0861e2742c22e4bce83bce40dfdbfd713835dfEric Anholt
9600b86ecf6f2f936bad6d628622ea5546c780ab8dXiang, Haihao      if (mt->compressed) {
97017c13d55b5b086774d6afea2ca754482c624c6aChad Versace          mt->total_width = ALIGN(width, mt->align_w);
9800b86ecf6f2f936bad6d628622ea5546c780ab8dXiang, Haihao          pack_y_pitch = (height + 3) / 4;
9900b86ecf6f2f936bad6d628622ea5546c780ab8dXiang, Haihao      } else {
100362c1bf75eb74de5b4655c481b74f79718ed4a34Eric Anholt	 mt->total_width = mt->width0;
101017c13d55b5b086774d6afea2ca754482c624c6aChad Versace	 pack_y_pitch = ALIGN(mt->height0, mt->align_h);
10200b86ecf6f2f936bad6d628622ea5546c780ab8dXiang, Haihao      }
1039f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
1046c3f6968911e0bb3256e91b070166d3da78a4775Eric Anholt      pack_x_pitch = width;
1059f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      pack_x_nr = 1;
1069f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
1075b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt      for (level = mt->first_level ; level <= mt->last_level ; level++) {
1089f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 GLint x = 0;
1099f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 GLint y = 0;
1109f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 GLint q, j;
111bd0861e2742c22e4bce83bce40dfdbfd713835dfEric Anholt
112d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace	 intel_miptree_set_level_info(mt, level,
1139f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt				      0, mt->total_height,
1149f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt				      width, height, depth);
1159f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
1168a472427559ea33186c71dfbab8254651fec3077Ian Romanick	 for (q = 0; q < depth; /* empty */) {
1178a472427559ea33186c71dfbab8254651fec3077Ian Romanick	    for (j = 0; j < pack_x_nr && q < depth; j++, q++) {
1189f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	       intel_miptree_set_image_offset(mt, level, q, x, y);
1199f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	       x += pack_x_pitch;
1209f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	    }
121f939776cb2372a3427784f88d34bf14c18a5a212Yuanhan Liu            if (x > mt->total_width)
122f939776cb2372a3427784f88d34bf14c18a5a212Yuanhan Liu               mt->total_width = x;
1239f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
1249f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	    x = 0;
125bd0861e2742c22e4bce83bce40dfdbfd713835dfEric Anholt	    y += pack_y_pitch;
1269f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 }
1279f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
1289f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
1299f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 mt->total_height += y;
1309f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 width  = minify(width);
1319f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 height = minify(height);
13279d981cd03f2e2e8f67ab21d52711de989bb9a44Kenneth Graunke	 if (mt->target == GL_TEXTURE_3D)
13379d981cd03f2e2e8f67ab21d52711de989bb9a44Kenneth Graunke	    depth = minify(depth);
13400b86ecf6f2f936bad6d628622ea5546c780ab8dXiang, Haihao
135bd0861e2742c22e4bce83bce40dfdbfd713835dfEric Anholt	 if (mt->compressed) {
136bd0861e2742c22e4bce83bce40dfdbfd713835dfEric Anholt	    pack_y_pitch = (height + 3) / 4;
137bd0861e2742c22e4bce83bce40dfdbfd713835dfEric Anholt
138017c13d55b5b086774d6afea2ca754482c624c6aChad Versace	    if (pack_x_pitch > ALIGN(width, mt->align_w)) {
139017c13d55b5b086774d6afea2ca754482c624c6aChad Versace	       pack_x_pitch = ALIGN(width, mt->align_w);
140bd0861e2742c22e4bce83bce40dfdbfd713835dfEric Anholt	       pack_x_nr <<= 1;
141bd0861e2742c22e4bce83bce40dfdbfd713835dfEric Anholt	    }
142bd0861e2742c22e4bce83bce40dfdbfd713835dfEric Anholt	 } else {
143f939776cb2372a3427784f88d34bf14c18a5a212Yuanhan Liu            pack_x_nr <<= 1;
144bd0861e2742c22e4bce83bce40dfdbfd713835dfEric Anholt	    if (pack_x_pitch > 4) {
145bd0861e2742c22e4bce83bce40dfdbfd713835dfEric Anholt	       pack_x_pitch >>= 1;
146bd0861e2742c22e4bce83bce40dfdbfd713835dfEric Anholt	    }
147bd0861e2742c22e4bce83bce40dfdbfd713835dfEric Anholt
148bd0861e2742c22e4bce83bce40dfdbfd713835dfEric Anholt	    if (pack_y_pitch > 2) {
149bd0861e2742c22e4bce83bce40dfdbfd713835dfEric Anholt	       pack_y_pitch >>= 1;
150017c13d55b5b086774d6afea2ca754482c624c6aChad Versace	       pack_y_pitch = ALIGN(pack_y_pitch, mt->align_h);
151bd0861e2742c22e4bce83bce40dfdbfd713835dfEric Anholt	    }
152bd0861e2742c22e4bce83bce40dfdbfd713835dfEric Anholt	 }
15300b86ecf6f2f936bad6d628622ea5546c780ab8dXiang, Haihao
1549f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      }
155a70e1315846cd5e8d6f2b622821ff8262fe7179dEric Anholt      /* The 965's sampler lays cachelines out according to how accesses
156a70e1315846cd5e8d6f2b622821ff8262fe7179dEric Anholt       * in the texture surfaces run, so they may be "vertical" through
157a70e1315846cd5e8d6f2b622821ff8262fe7179dEric Anholt       * memory.  As a result, the docs say in Surface Padding Requirements:
158a70e1315846cd5e8d6f2b622821ff8262fe7179dEric Anholt       * Sampling Engine Surfaces that two extra rows of padding are required.
159a70e1315846cd5e8d6f2b622821ff8262fe7179dEric Anholt       */
160a70e1315846cd5e8d6f2b622821ff8262fe7179dEric Anholt      if (mt->target == GL_TEXTURE_CUBE_MAP)
16129e51c3872531366570d032147abad50f8a3c1afEric Anholt	 mt->total_height += 2;
1629f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1639f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
1649f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
165669f1822d2a60865514faf37f9fde21e4567b3d2Eric Anholt   case GL_TEXTURE_2D_ARRAY:
166669f1822d2a60865514faf37f9fde21e4567b3d2Eric Anholt   case GL_TEXTURE_1D_ARRAY:
167d7b33309fe160212f2eb73f471f3aedcb5d0b5c1Chad Versace      brw_miptree_layout_texture_array(intel, mt);
168669f1822d2a60865514faf37f9fde21e4567b3d2Eric Anholt      break;
169669f1822d2a60865514faf37f9fde21e4567b3d2Eric Anholt
1703416ef303af633668cece0b199b4a8b2388c1e2fMichel Dänzer   default:
1711bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      switch (mt->msaa_layout) {
1721bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      case INTEL_MSAA_LAYOUT_UMS:
1731bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      case INTEL_MSAA_LAYOUT_CMS:
174455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry         brw_miptree_layout_texture_array(intel, mt);
1751bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry         break;
1761bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      case INTEL_MSAA_LAYOUT_NONE:
1771bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      case INTEL_MSAA_LAYOUT_IMS:
178455ac562722f60ac9fb0c3d3c697fa339fa011adPaul Berry         i945_miptree_layout_2d(mt);
1791bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry         break;
1801bd4d456cdecf7bea55f4e3dac574af54efad994Paul Berry      }
1819f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1829f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
183362c1bf75eb74de5b4655c481b74f79718ed4a34Eric Anholt   DBG("%s: %dx%dx%d\n", __FUNCTION__,
184362c1bf75eb74de5b4655c481b74f79718ed4a34Eric Anholt       mt->total_width, mt->total_height, mt->cpp);
1859f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
1869f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
187