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 * Cube texture map layout for i830M-GM915.
61 * Hardware layout looks like:
103 i915_miptree_layout_cube(struct intel_context
*intel
,
104 struct intel_mipmap_tree
* mt
)
106 const GLuint dim
= mt
->width0
;
108 GLuint lvlWidth
= mt
->width0
, lvlHeight
= mt
->height0
;
111 assert(lvlWidth
== lvlHeight
); /* cubemap images are square */
113 /* double pitch for cube layouts */
114 mt
->pitch
= intel_miptree_pitch_align (intel
, mt
, dim
* 2);
115 mt
->total_height
= dim
* 4;
117 for (level
= mt
->first_level
; level
<= mt
->last_level
; level
++) {
118 intel_miptree_set_level_info(mt
, level
, 6,
120 /*OLD: mt->pitch, mt->total_height,*/
127 for (face
= 0; face
< 6; face
++) {
128 GLuint x
= initial_offsets
[face
][0] * dim
;
129 GLuint y
= initial_offsets
[face
][1] * dim
;
132 for (level
= mt
->first_level
; level
<= mt
->last_level
; level
++) {
133 intel_miptree_set_image_offset(mt
, level
, face
, x
, y
);
136 _mesa_printf("cube mipmap %d/%d (%d..%d) is 0x0\n",
137 face
, level
, mt
->first_level
, mt
->last_level
);
140 x
+= step_offsets
[face
][0] * d
;
141 y
+= step_offsets
[face
][1] * d
;
147 i915_miptree_layout_3d(struct intel_context
*intel
,
148 struct intel_mipmap_tree
* mt
)
150 GLuint width
= mt
->width0
;
151 GLuint height
= mt
->height0
;
152 GLuint depth
= mt
->depth0
;
153 GLuint stack_height
= 0;
156 /* Calculate the size of a single slice. */
157 mt
->pitch
= intel_miptree_pitch_align (intel
, mt
, mt
->width0
);
159 /* XXX: hardware expects/requires 9 levels at minimum. */
160 for (level
= mt
->first_level
; level
<= MAX2(8, mt
->last_level
); level
++) {
161 intel_miptree_set_level_info(mt
, level
, depth
, 0, mt
->total_height
,
162 width
, height
, depth
);
164 stack_height
+= MAX2(2, height
);
166 width
= minify(width
);
167 height
= minify(height
);
168 depth
= minify(depth
);
171 /* Fixup depth image_offsets: */
173 for (level
= mt
->first_level
; level
<= mt
->last_level
; level
++) {
175 for (i
= 0; i
< depth
; i
++) {
176 intel_miptree_set_image_offset(mt
, level
, i
,
177 0, i
* stack_height
);
180 depth
= minify(depth
);
183 /* Multiply slice size by texture depth for total size. It's
184 * remarkable how wasteful of memory the i915 texture layouts
185 * are. They are largely fixed in the i945.
187 mt
->total_height
= stack_height
* mt
->depth0
;
191 i915_miptree_layout_2d(struct intel_context
*intel
,
192 struct intel_mipmap_tree
* mt
)
194 GLuint width
= mt
->width0
;
195 GLuint height
= mt
->height0
;
199 mt
->pitch
= intel_miptree_pitch_align (intel
, mt
, mt
->width0
);
200 mt
->total_height
= 0;
202 for (level
= mt
->first_level
; level
<= mt
->last_level
; level
++) {
203 intel_miptree_set_level_info(mt
, level
, 1,
208 img_height
= MAX2(1, height
/ 4);
210 img_height
= (MAX2(2, height
) + 1) & ~1;
212 mt
->total_height
+= img_height
;
214 width
= minify(width
);
215 height
= minify(height
);
220 i915_miptree_layout(struct intel_context
*intel
, struct intel_mipmap_tree
* mt
)
222 switch (mt
->target
) {
223 case GL_TEXTURE_CUBE_MAP
:
224 i915_miptree_layout_cube(intel
, mt
);
227 i915_miptree_layout_3d(intel
, mt
);
231 case GL_TEXTURE_RECTANGLE_ARB
:
232 i915_miptree_layout_2d(intel
, mt
);
235 _mesa_problem(NULL
, "Unexpected tex target in i915_miptree_layout()");
239 DBG("%s: %dx%dx%d - sz 0x%x\n", __FUNCTION__
,
241 mt
->total_height
, mt
->cpp
, mt
->pitch
* mt
->total_height
* mt
->cpp
);
248 * Cube texture map layout for GM945 and later.
250 * The hardware layout looks like the 830-915 layout, except for the small
251 * sizes. A zoomed in view of the layout for 945 is:
293 * +---+ +---+---+---+---+---+---+---+---+---+
294 * |4x4| |4x4| |2x2| |2x2| |2x2| |2x2|
295 * | +z| | -z| | +x| | +y| | +z| | -x| ...
296 * | | | | | | | | | | | |
297 * +---+ +---+ +---+ +---+ +---+ +---+
299 * The bottom row continues with the remaining 2x2 then the 1x1 mip contents
300 * in order, with each of them aligned to a 4x4 block boundary. Thus, for
301 * 32x32 cube maps and smaller, the bottom row layout is going to dictate the
302 * pitch of the tree. For a tree with 4x4 images, the pitch is at least
303 * 14 * 8 = 112 texels, for 2x2 it is at least 12 * 8 texels, and for 1x1
304 * it is 6 * 8 texels.
308 i945_miptree_layout_cube(struct intel_context
*intel
,
309 struct intel_mipmap_tree
* mt
)
311 const GLuint dim
= mt
->width0
;
313 GLuint lvlWidth
= mt
->width0
, lvlHeight
= mt
->height0
;
316 assert(lvlWidth
== lvlHeight
); /* cubemap images are square */
318 /* Depending on the size of the largest images, pitch can be
319 * determined either by the old-style packing of cubemap faces,
320 * or the final row of 4x4, 2x2 and 1x1 faces below this.
323 mt
->pitch
= intel_miptree_pitch_align (intel
, mt
, dim
* 2);
325 mt
->pitch
= intel_miptree_pitch_align (intel
, mt
, 14 * 8);
328 mt
->total_height
= dim
* 4 + 4;
330 mt
->total_height
= 4;
332 /* Set all the levels to effectively occupy the whole rectangular region. */
333 for (level
= mt
->first_level
; level
<= mt
->last_level
; level
++) {
334 intel_miptree_set_level_info(mt
, level
, 6,
336 lvlWidth
, lvlHeight
, 1);
341 for (face
= 0; face
< 6; face
++) {
342 GLuint x
= initial_offsets
[face
][0] * dim
;
343 GLuint y
= initial_offsets
[face
][1] * dim
;
346 if (dim
== 4 && face
>= 4) {
347 y
= mt
->total_height
- 4;
349 } else if (dim
< 4 && (face
> 0 || mt
->first_level
> 0)) {
350 y
= mt
->total_height
- 4;
354 for (level
= mt
->first_level
; level
<= mt
->last_level
; level
++) {
355 intel_miptree_set_image_offset(mt
, level
, face
, x
, y
);
364 x
+= step_offsets
[face
][0] * d
;
365 y
+= step_offsets
[face
][1] * d
;
374 y
= mt
->total_height
- 4;
380 y
= mt
->total_height
- 4;
389 x
+= step_offsets
[face
][0] * d
;
390 y
+= step_offsets
[face
][1] * d
;
398 i945_miptree_layout_3d(struct intel_context
*intel
,
399 struct intel_mipmap_tree
* mt
)
401 GLuint width
= mt
->width0
;
402 GLuint height
= mt
->height0
;
403 GLuint depth
= mt
->depth0
;
404 GLuint pack_x_pitch
, pack_x_nr
;
408 mt
->pitch
= intel_miptree_pitch_align (intel
, mt
, mt
->width0
);
409 mt
->total_height
= 0;
411 pack_y_pitch
= MAX2(mt
->height0
, 2);
412 pack_x_pitch
= mt
->pitch
;
415 for (level
= mt
->first_level
; level
<= mt
->last_level
; level
++) {
420 intel_miptree_set_level_info(mt
, level
, depth
,
422 width
, height
, depth
);
424 for (q
= 0; q
< depth
;) {
425 for (j
= 0; j
< pack_x_nr
&& q
< depth
; j
++, q
++) {
426 intel_miptree_set_image_offset(mt
, level
, q
, x
, y
);
434 mt
->total_height
+= y
;
436 if (pack_x_pitch
> 4) {
439 assert(pack_x_pitch
* pack_x_nr
<= mt
->pitch
);
442 if (pack_y_pitch
> 2) {
446 width
= minify(width
);
447 height
= minify(height
);
448 depth
= minify(depth
);
453 i945_miptree_layout(struct intel_context
*intel
, struct intel_mipmap_tree
* mt
)
455 switch (mt
->target
) {
456 case GL_TEXTURE_CUBE_MAP
:
458 i945_miptree_layout_cube(intel
, mt
);
460 i915_miptree_layout_cube(intel
, mt
);
463 i945_miptree_layout_3d(intel
, mt
);
467 case GL_TEXTURE_RECTANGLE_ARB
:
468 i945_miptree_layout_2d(intel
, mt
);
471 _mesa_problem(NULL
, "Unexpected tex target in i945_miptree_layout()");
475 DBG("%s: %dx%dx%d - sz 0x%x\n", __FUNCTION__
,
477 mt
->total_height
, mt
->cpp
, mt
->pitch
* mt
->total_height
* mt
->cpp
);