1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 /** @file i915_tex_layout.c
29 * Code to layout images in a mipmap tree for i830M-GM915 and G945 and beyond.
32 #include "intel_mipmap_tree.h"
33 #include "intel_tex_layout.h"
34 #include "main/macros.h"
35 #include "intel_context.h"
37 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
39 static GLint initial_offsets
[6][2] = {
40 [FACE_POS_X
] = {0, 0},
41 [FACE_POS_Y
] = {1, 0},
42 [FACE_POS_Z
] = {1, 1},
43 [FACE_NEG_X
] = {0, 2},
44 [FACE_NEG_Y
] = {1, 2},
45 [FACE_NEG_Z
] = {1, 3},
49 static GLint step_offsets
[6][2] = {
50 [FACE_POS_X
] = {0, 2},
51 [FACE_POS_Y
] = {-1, 2},
52 [FACE_POS_Z
] = {-1, 1},
53 [FACE_NEG_X
] = {0, 2},
54 [FACE_NEG_Y
] = {-1, 2},
55 [FACE_NEG_Z
] = {-1, 1},
59 static GLint bottom_offsets
[6] = {
60 [FACE_POS_X
] = 16 + 0 * 8,
61 [FACE_POS_Y
] = 16 + 1 * 8,
62 [FACE_POS_Z
] = 16 + 2 * 8,
63 [FACE_NEG_X
] = 16 + 3 * 8,
64 [FACE_NEG_Y
] = 16 + 4 * 8,
65 [FACE_NEG_Z
] = 16 + 5 * 8,
70 * Cube texture map layout for i830M-GM915.
72 * Hardware layout looks like:
114 i915_miptree_layout_cube(struct intel_context
*intel
,
115 struct intel_mipmap_tree
* mt
,
118 const GLuint dim
= mt
->width0
;
120 GLuint lvlWidth
= mt
->width0
, lvlHeight
= mt
->height0
;
123 assert(lvlWidth
== lvlHeight
); /* cubemap images are square */
125 /* double pitch for cube layouts */
126 mt
->total_width
= dim
* 2;
127 mt
->total_height
= dim
* 4;
129 for (level
= mt
->first_level
; level
<= mt
->last_level
; level
++) {
130 intel_miptree_set_level_info(mt
, level
, 6,
138 for (face
= 0; face
< 6; face
++) {
139 GLuint x
= initial_offsets
[face
][0] * dim
;
140 GLuint y
= initial_offsets
[face
][1] * dim
;
143 for (level
= mt
->first_level
; level
<= mt
->last_level
; level
++) {
144 intel_miptree_set_image_offset(mt
, level
, face
, x
, y
);
147 printf("cube mipmap %d/%d (%d..%d) is 0x0\n",
148 face
, level
, mt
->first_level
, mt
->last_level
);
151 x
+= step_offsets
[face
][0] * d
;
152 y
+= step_offsets
[face
][1] * d
;
158 i915_miptree_layout_3d(struct intel_context
*intel
,
159 struct intel_mipmap_tree
* mt
,
162 GLuint width
= mt
->width0
;
163 GLuint height
= mt
->height0
;
164 GLuint depth
= mt
->depth0
;
165 GLuint stack_height
= 0;
168 /* Calculate the size of a single slice. */
169 mt
->total_width
= mt
->width0
;
171 /* XXX: hardware expects/requires 9 levels at minimum. */
172 for (level
= mt
->first_level
; level
<= MAX2(8, mt
->last_level
); level
++) {
173 intel_miptree_set_level_info(mt
, level
, depth
, 0, mt
->total_height
,
174 width
, height
, depth
);
176 stack_height
+= MAX2(2, height
);
178 width
= minify(width
);
179 height
= minify(height
);
180 depth
= minify(depth
);
183 /* Fixup depth image_offsets: */
185 for (level
= mt
->first_level
; level
<= mt
->last_level
; level
++) {
187 for (i
= 0; i
< depth
; i
++) {
188 intel_miptree_set_image_offset(mt
, level
, i
,
189 0, i
* stack_height
);
192 depth
= minify(depth
);
195 /* Multiply slice size by texture depth for total size. It's
196 * remarkable how wasteful of memory the i915 texture layouts
197 * are. They are largely fixed in the i945.
199 mt
->total_height
= stack_height
* mt
->depth0
;
203 i915_miptree_layout_2d(struct intel_context
*intel
,
204 struct intel_mipmap_tree
* mt
,
207 GLuint width
= mt
->width0
;
208 GLuint height
= mt
->height0
;
212 mt
->total_width
= mt
->width0
;
213 mt
->total_height
= 0;
215 for (level
= mt
->first_level
; level
<= mt
->last_level
; level
++) {
216 intel_miptree_set_level_info(mt
, level
, 1,
221 img_height
= MAX2(1, height
/ 4);
223 img_height
= (MAX2(2, height
) + 1) & ~1;
225 mt
->total_height
+= img_height
;
227 width
= minify(width
);
228 height
= minify(height
);
233 i915_miptree_layout(struct intel_context
*intel
, struct intel_mipmap_tree
* mt
,
236 switch (mt
->target
) {
237 case GL_TEXTURE_CUBE_MAP
:
238 i915_miptree_layout_cube(intel
, mt
, tiling
);
241 i915_miptree_layout_3d(intel
, mt
, tiling
);
245 case GL_TEXTURE_RECTANGLE_ARB
:
246 i915_miptree_layout_2d(intel
, mt
, tiling
);
249 _mesa_problem(NULL
, "Unexpected tex target in i915_miptree_layout()");
253 DBG("%s: %dx%dx%d\n", __FUNCTION__
,
254 mt
->total_width
, mt
->total_height
, mt
->cpp
);
261 * Cube texture map layout for GM945 and later.
263 * The hardware layout looks like the 830-915 layout, except for the small
264 * sizes. A zoomed in view of the layout for 945 is:
306 * +---+ +---+---+---+---+---+---+---+---+---+
307 * |4x4| |4x4| |2x2| |2x2| |2x2| |2x2|
308 * | +z| | -z| | +x| | +y| | +z| | -x| ...
309 * | | | | | | | | | | | |
310 * +---+ +---+ +---+ +---+ +---+ +---+
312 * The bottom row continues with the remaining 2x2 then the 1x1 mip contents
313 * in order, with each of them aligned to a 8x8 block boundary. Thus, for
314 * 32x32 cube maps and smaller, the bottom row layout is going to dictate the
315 * pitch of the tree. For a tree with 4x4 images, the pitch is at least
316 * 14 * 8 = 112 texels, for 2x2 it is at least 12 * 8 texels, and for 1x1
317 * it is 6 * 8 texels.
321 i945_miptree_layout_cube(struct intel_context
*intel
,
322 struct intel_mipmap_tree
* mt
,
325 const GLuint dim
= mt
->width0
;
327 GLuint lvlWidth
= mt
->width0
, lvlHeight
= mt
->height0
;
330 assert(lvlWidth
== lvlHeight
); /* cubemap images are square */
332 /* Depending on the size of the largest images, pitch can be
333 * determined either by the old-style packing of cubemap faces,
334 * or the final row of 4x4, 2x2 and 1x1 faces below this.
337 mt
->total_width
= dim
* 2;
339 mt
->total_width
= 14 * 8;
342 mt
->total_height
= dim
* 4 + 4;
344 mt
->total_height
= 4;
346 /* Set all the levels to effectively occupy the whole rectangular region. */
347 for (level
= mt
->first_level
; level
<= mt
->last_level
; level
++) {
348 intel_miptree_set_level_info(mt
, level
, 6,
350 lvlWidth
, lvlHeight
, 1);
355 for (face
= 0; face
< 6; face
++) {
356 GLuint x
= initial_offsets
[face
][0] * dim
;
357 GLuint y
= initial_offsets
[face
][1] * dim
;
360 if (dim
== 4 && face
>= 4) {
361 y
= mt
->total_height
- 4;
363 } else if (dim
< 4 && (face
> 0 || mt
->first_level
> 0)) {
364 y
= mt
->total_height
- 4;
368 for (level
= mt
->first_level
; level
<= mt
->last_level
; level
++) {
369 intel_miptree_set_image_offset(mt
, level
, face
, x
, y
);
378 x
+= step_offsets
[face
][0] * d
;
379 y
+= step_offsets
[face
][1] * d
;
388 y
= mt
->total_height
- 4;
395 y
= mt
->total_height
- 4;
396 x
= bottom_offsets
[face
];
404 x
+= step_offsets
[face
][0] * d
;
405 y
+= step_offsets
[face
][1] * d
;
413 i945_miptree_layout_3d(struct intel_context
*intel
,
414 struct intel_mipmap_tree
* mt
,
417 GLuint width
= mt
->width0
;
418 GLuint height
= mt
->height0
;
419 GLuint depth
= mt
->depth0
;
420 GLuint pack_x_pitch
, pack_x_nr
;
424 mt
->total_width
= mt
->width0
;
425 mt
->total_height
= 0;
427 pack_y_pitch
= MAX2(mt
->height0
, 2);
428 pack_x_pitch
= mt
->total_width
;
431 for (level
= mt
->first_level
; level
<= mt
->last_level
; level
++) {
436 intel_miptree_set_level_info(mt
, level
, depth
,
438 width
, height
, depth
);
440 for (q
= 0; q
< depth
;) {
441 for (j
= 0; j
< pack_x_nr
&& q
< depth
; j
++, q
++) {
442 intel_miptree_set_image_offset(mt
, level
, q
, x
, y
);
450 mt
->total_height
+= y
;
452 if (pack_x_pitch
> 4) {
455 assert(pack_x_pitch
* pack_x_nr
<= mt
->total_width
);
458 if (pack_y_pitch
> 2) {
462 width
= minify(width
);
463 height
= minify(height
);
464 depth
= minify(depth
);
469 i945_miptree_layout(struct intel_context
*intel
, struct intel_mipmap_tree
* mt
,
472 switch (mt
->target
) {
473 case GL_TEXTURE_CUBE_MAP
:
475 i945_miptree_layout_cube(intel
, mt
, tiling
);
477 i915_miptree_layout_cube(intel
, mt
, tiling
);
480 i945_miptree_layout_3d(intel
, mt
, tiling
);
484 case GL_TEXTURE_RECTANGLE_ARB
:
485 i945_miptree_layout_2d(intel
, mt
, tiling
);
488 _mesa_problem(NULL
, "Unexpected tex target in i945_miptree_layout()");
492 DBG("%s: %dx%dx%d\n", __FUNCTION__
,
493 mt
->total_width
, mt
->total_height
, mt
->cpp
);