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 **************************************************************************/
29 * Keith Whitwell <keith@tungstengraphics.com>
30 * Michel Dänzer <michel@tungstengraphics.com>
34 #include "pipe/p_state.h"
35 #include "intel_mipmap_tree.h"
38 static GLuint
minify( GLuint d
)
43 static int align(int value
, int alignment
)
45 return (value
+ alignment
- 1) & ~(alignment
- 1);
50 mipmaptree_set_level_info(struct pipe_mipmap_tree
*mt
,
53 GLuint x
, GLuint y
, GLuint w
, GLuint h
, GLuint d
)
55 assert(level
< MAX_TEXTURE_LEVELS
);
57 mt
->level
[level
].width
= w
;
58 mt
->level
[level
].height
= h
;
59 mt
->level
[level
].depth
= d
;
60 mt
->level
[level
].level_offset
= (x
+ y
* mt
->pitch
) * mt
->cpp
;
61 mt
->level
[level
].nr_images
= nr_images
;
64 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
65 level, w, h, d, x, y, mt->level[level].level_offset);
68 /* Not sure when this would happen, but anyway:
70 if (mt
->level
[level
].image_offset
) {
71 free(mt
->level
[level
].image_offset
);
72 mt
->level
[level
].image_offset
= NULL
;
77 mt
->level
[level
].image_offset
= malloc(nr_images
* sizeof(GLuint
));
78 mt
->level
[level
].image_offset
[0] = 0;
83 i945_miptree_layout_2d( struct pipe_mipmap_tree
*mt
)
85 GLint align_h
= 2, align_w
= 4;
89 GLuint width
= mt
->width0
;
90 GLuint height
= mt
->height0
;
92 mt
->pitch
= mt
->width0
;
94 /* May need to adjust pitch to accomodate the placement of
95 * the 2nd mipmap. This occurs when the alignment
96 * constraints of mipmap placement push the right edge of the
97 * 2nd mipmap out past the width of its parent.
99 if (mt
->first_level
!= mt
->last_level
) {
100 GLuint mip1_width
= align(minify(mt
->width0
), align_w
)
101 + minify(minify(mt
->width0
));
103 if (mip1_width
> mt
->width0
)
104 mt
->pitch
= mip1_width
;
107 /* Pitch must be a whole number of dwords, even though we
108 * express it in texels.
110 mt
->pitch
= align(mt
->pitch
* mt
->cpp
, 4) / mt
->cpp
;
111 mt
->total_height
= 0;
113 for ( level
= mt
->first_level
; level
<= mt
->last_level
; level
++ ) {
116 mipmaptree_set_level_info(mt
, level
, 1, x
, y
, width
, height
, 1);
119 img_height
= MAX2(1, height
/4);
121 img_height
= align(height
, align_h
);
124 /* Because the images are packed better, the final offset
125 * might not be the maximal one:
127 mt
->total_height
= MAX2(mt
->total_height
, y
+ img_height
);
129 /* Layout_below: step right after second mipmap.
131 if (level
== mt
->first_level
+ 1) {
132 x
+= align(width
, align_w
);
138 width
= minify(width
);
139 height
= minify(height
);
144 static const GLint initial_offsets
[6][2] = {
153 static const GLint step_offsets
[6][2] = {
164 i915_miptree_layout(struct pipe_context
*pipe
, struct pipe_mipmap_tree
* mt
)
168 switch (mt
->target
) {
169 case GL_TEXTURE_CUBE_MAP
:{
170 const GLuint dim
= mt
->width0
;
172 GLuint lvlWidth
= mt
->width0
, lvlHeight
= mt
->height0
;
174 assert(lvlWidth
== lvlHeight
); /* cubemap images are square */
176 /* double pitch for cube layouts */
177 mt
->pitch
= ((dim
* mt
->cpp
* 2 + 3) & ~3) / mt
->cpp
;
178 mt
->total_height
= dim
* 4;
180 for (level
= mt
->first_level
; level
<= mt
->last_level
; level
++) {
181 intel_miptree_set_level_info(mt
, level
, 6,
183 /*OLD: mt->pitch, mt->total_height,*/
190 for (face
= 0; face
< 6; face
++) {
191 GLuint x
= initial_offsets
[face
][0] * dim
;
192 GLuint y
= initial_offsets
[face
][1] * dim
;
195 for (level
= mt
->first_level
; level
<= mt
->last_level
; level
++) {
196 intel_miptree_set_image_offset(mt
, level
, face
, x
, y
);
199 _mesa_printf("cube mipmap %d/%d (%d..%d) is 0x0\n",
200 face
, level
, mt
->first_level
, mt
->last_level
);
203 x
+= step_offsets
[face
][0] * d
;
204 y
+= step_offsets
[face
][1] * d
;
210 GLuint width
= mt
->width0
;
211 GLuint height
= mt
->height0
;
212 GLuint depth
= mt
->depth0
;
213 GLuint stack_height
= 0;
215 /* Calculate the size of a single slice.
217 mt
->pitch
= ((mt
->width0
* mt
->cpp
+ 3) & ~3) / mt
->cpp
;
219 /* XXX: hardware expects/requires 9 levels at minimum.
221 for (level
= mt
->first_level
; level
<= MAX2(8, mt
->last_level
);
223 intel_miptree_set_level_info(mt
, level
, depth
, 0, mt
->total_height
,
224 width
, height
, depth
);
227 stack_height
+= MAX2(2, height
);
229 width
= minify(width
);
230 height
= minify(height
);
231 depth
= minify(depth
);
234 /* Fixup depth image_offsets:
237 for (level
= mt
->first_level
; level
<= mt
->last_level
; level
++) {
239 for (i
= 0; i
< depth
; i
++)
240 intel_miptree_set_image_offset(mt
, level
, i
,
241 0, i
* stack_height
);
243 depth
= minify(depth
);
247 /* Multiply slice size by texture depth for total size. It's
248 * remarkable how wasteful of memory the i915 texture layouts
249 * are. They are largely fixed in the i945.
251 mt
->total_height
= stack_height
* mt
->depth0
;
256 GLuint width
= mt
->width0
;
257 GLuint height
= mt
->height0
;
260 mt
->pitch
= ((mt
->width0
* mt
->cpp
+ 3) & ~3) / mt
->cpp
;
261 mt
->total_height
= 0;
263 for (level
= mt
->first_level
; level
<= mt
->last_level
; level
++) {
264 intel_miptree_set_level_info(mt
, level
, 1,
269 img_height
= MAX2(1, height
/ 4);
271 img_height
= (MAX2(2, height
) + 1) & ~1;
273 mt
->total_height
+= img_height
;
275 width
= minify(width
);
276 height
= minify(height
);
282 DBG("%s: %dx%dx%d - sz 0x%x\n", __FUNCTION__,
284 mt->total_height, mt->cpp, mt->pitch * mt->total_height * mt->cpp);
292 i945_miptree_layout(struct pipe_context
*pipe
, struct pipe_mipmap_tree
* mt
)
296 switch (mt
->target
) {
297 case GL_TEXTURE_CUBE_MAP
:{
298 const GLuint dim
= mt
->width0
;
300 GLuint lvlWidth
= mt
->width0
, lvlHeight
= mt
->height0
;
302 assert(lvlWidth
== lvlHeight
); /* cubemap images are square */
304 /* Depending on the size of the largest images, pitch can be
305 * determined either by the old-style packing of cubemap faces,
306 * or the final row of 4x4, 2x2 and 1x1 faces below this.
309 mt
->pitch
= ((dim
* mt
->cpp
* 2 + 3) & ~3) / mt
->cpp
;
313 mt
->total_height
= dim
* 4 + 4;
315 /* Set all the levels to effectively occupy the whole rectangular region.
317 for (level
= mt
->first_level
; level
<= mt
->last_level
; level
++) {
318 intel_miptree_set_level_info(mt
, level
, 6,
320 lvlWidth
, lvlHeight
, 1);
326 for (face
= 0; face
< 6; face
++) {
327 GLuint x
= initial_offsets
[face
][0] * dim
;
328 GLuint y
= initial_offsets
[face
][1] * dim
;
331 if (dim
== 4 && face
>= 4) {
332 y
= mt
->total_height
- 4;
335 else if (dim
< 4 && (face
> 0 || mt
->first_level
> 0)) {
336 y
= mt
->total_height
- 4;
340 for (level
= mt
->first_level
; level
<= mt
->last_level
; level
++) {
341 intel_miptree_set_image_offset(mt
, level
, face
, x
, y
);
350 x
+= step_offsets
[face
][0] * d
;
351 y
+= step_offsets
[face
][1] * d
;
360 y
= mt
->total_height
- 4;
366 y
= mt
->total_height
- 4;
375 x
+= step_offsets
[face
][0] * d
;
376 y
+= step_offsets
[face
][1] * d
;
384 GLuint width
= mt
->width0
;
385 GLuint height
= mt
->height0
;
386 GLuint depth
= mt
->depth0
;
387 GLuint pack_x_pitch
, pack_x_nr
;
391 mt
->pitch
= ((mt
->width0
* mt
->cpp
+ 3) & ~3) / mt
->cpp
;
392 mt
->total_height
= 0;
394 pack_y_pitch
= MAX2(mt
->height0
, 2);
395 pack_x_pitch
= mt
->pitch
;
398 for (level
= mt
->first_level
; level
<= mt
->last_level
; level
++) {
399 GLuint nr_images
= mt
->target
== GL_TEXTURE_3D
? depth
: 6;
404 intel_miptree_set_level_info(mt
, level
, nr_images
,
406 width
, height
, depth
);
408 for (q
= 0; q
< nr_images
;) {
409 for (j
= 0; j
< pack_x_nr
&& q
< nr_images
; j
++, q
++) {
410 intel_miptree_set_image_offset(mt
, level
, q
, x
, y
);
419 mt
->total_height
+= y
;
421 if (pack_x_pitch
> 4) {
424 assert(pack_x_pitch
* pack_x_nr
<= mt
->pitch
);
427 if (pack_y_pitch
> 2) {
431 width
= minify(width
);
432 height
= minify(height
);
433 depth
= minify(depth
);
440 case GL_TEXTURE_RECTANGLE_ARB
:
441 i945_miptree_layout_2d(mt
);
444 _mesa_problem(NULL
, "Unexpected tex target in i945_miptree_layout()");
448 DBG("%s: %dx%dx%d - sz 0x%x\n", __FUNCTION__,
450 mt->total_height, mt->cpp, mt->pitch * mt->total_height * mt->cpp);