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>
33 #include "pipe/p_state.h"
34 #include "pipe/p_context.h"
35 #include "pipe/p_defines.h"
36 #include "pipe/p_util.h"
37 #include "pipe/p_winsys.h"
39 #include "i915_context.h"
40 #include "i915_texture.h"
41 #include "i915_debug.h"
44 static unsigned minify( unsigned d
)
49 static int align(int value
, int alignment
)
51 return (value
+ alignment
- 1) & ~(alignment
- 1);
56 i915_miptree_set_level_info(struct i915_texture
*tex
,
59 unsigned x
, unsigned y
, unsigned w
, unsigned h
, unsigned d
)
61 struct pipe_texture
*pt
= &tex
->base
;
63 assert(level
< PIPE_MAX_TEXTURE_LEVELS
);
66 pt
->height
[level
] = h
;
69 tex
->level_offset
[level
] = (x
+ y
* tex
->pitch
) * pt
->cpp
;
70 tex
->nr_images
[level
] = nr_images
;
73 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
74 level, w, h, d, x, y, tex->level_offset[level]);
77 /* Not sure when this would happen, but anyway:
79 if (tex
->image_offset
[level
]) {
80 FREE(tex
->image_offset
[level
]);
81 tex
->image_offset
[level
] = NULL
;
85 assert(!tex
->image_offset
[level
]);
87 tex
->image_offset
[level
] = (unsigned *) MALLOC(nr_images
* sizeof(unsigned));
88 tex
->image_offset
[level
][0] = 0;
93 i915_miptree_set_image_offset(struct i915_texture
*tex
,
94 unsigned level
, unsigned img
, unsigned x
, unsigned y
)
96 if (img
== 0 && level
== 0)
97 assert(x
== 0 && y
== 0);
99 assert(img
< tex
->nr_images
[level
]);
101 tex
->image_offset
[level
][img
] = (x
+ y
* tex
->pitch
);
104 DBG("%s level %d img %d pos %d,%d image_offset %x\n",
105 __FUNCTION__, level, img, x, y, tex->image_offset[level][img]);
111 i945_miptree_layout_2d( struct i915_texture
*tex
)
113 struct pipe_texture
*pt
= &tex
->base
;
114 int align_h
= 2, align_w
= 4;
118 unsigned width
= pt
->width
[0];
119 unsigned height
= pt
->height
[0];
121 tex
->pitch
= pt
->width
[0];
123 /* May need to adjust pitch to accomodate the placement of
124 * the 2nd mipmap. This occurs when the alignment
125 * constraints of mipmap placement push the right edge of the
126 * 2nd mipmap out past the width of its parent.
128 if (pt
->first_level
!= pt
->last_level
) {
129 unsigned mip1_width
= align(minify(pt
->width
[0]), align_w
)
130 + minify(minify(pt
->width
[0]));
132 if (mip1_width
> pt
->width
[0])
133 tex
->pitch
= mip1_width
;
136 /* Pitch must be a whole number of dwords, even though we
137 * express it in texels.
139 tex
->pitch
= align(tex
->pitch
* pt
->cpp
, 4) / pt
->cpp
;
140 tex
->total_height
= 0;
142 for ( level
= pt
->first_level
; level
<= pt
->last_level
; level
++ ) {
145 i915_miptree_set_level_info(tex
, level
, 1, x
, y
, width
, height
, 1);
148 img_height
= MAX2(1, height
/4);
150 img_height
= align(height
, align_h
);
153 /* Because the images are packed better, the final offset
154 * might not be the maximal one:
156 tex
->total_height
= MAX2(tex
->total_height
, y
+ img_height
);
158 /* Layout_below: step right after second mipmap.
160 if (level
== pt
->first_level
+ 1) {
161 x
+= align(width
, align_w
);
167 width
= minify(width
);
168 height
= minify(height
);
173 static const int initial_offsets
[6][2] = {
182 static const int step_offsets
[6][2] = {
193 i915_miptree_layout(struct pipe_context
*pipe
, struct i915_texture
* tex
)
195 struct pipe_texture
*pt
= &tex
->base
;
198 switch (pt
->target
) {
199 case PIPE_TEXTURE_CUBE
: {
200 const unsigned dim
= pt
->width
[0];
202 unsigned lvlWidth
= pt
->width
[0], lvlHeight
= pt
->height
[0];
204 assert(lvlWidth
== lvlHeight
); /* cubemap images are square */
206 /* double pitch for cube layouts */
207 tex
->pitch
= ((dim
* pt
->cpp
* 2 + 3) & ~3) / pt
->cpp
;
208 tex
->total_height
= dim
* 4;
210 for (level
= pt
->first_level
; level
<= pt
->last_level
; level
++) {
211 i915_miptree_set_level_info(tex
, level
, 6,
213 /*OLD: tex->pitch, tex->total_height,*/
220 for (face
= 0; face
< 6; face
++) {
221 unsigned x
= initial_offsets
[face
][0] * dim
;
222 unsigned y
= initial_offsets
[face
][1] * dim
;
225 for (level
= pt
->first_level
; level
<= pt
->last_level
; level
++) {
226 i915_miptree_set_image_offset(tex
, level
, face
, x
, y
);
228 x
+= step_offsets
[face
][0] * d
;
229 y
+= step_offsets
[face
][1] * d
;
234 case PIPE_TEXTURE_3D
:{
235 unsigned width
= pt
->width
[0];
236 unsigned height
= pt
->height
[0];
237 unsigned depth
= pt
->depth
[0];
238 unsigned stack_height
= 0;
240 /* Calculate the size of a single slice.
242 tex
->pitch
= ((pt
->width
[0] * pt
->cpp
+ 3) & ~3) / pt
->cpp
;
244 /* XXX: hardware expects/requires 9 levels at minimum.
246 for (level
= pt
->first_level
; level
<= MAX2(8, pt
->last_level
);
248 i915_miptree_set_level_info(tex
, level
, depth
, 0, tex
->total_height
,
249 width
, height
, depth
);
252 stack_height
+= MAX2(2, height
);
254 width
= minify(width
);
255 height
= minify(height
);
256 depth
= minify(depth
);
259 /* Fixup depth image_offsets:
261 depth
= pt
->depth
[0];
262 for (level
= pt
->first_level
; level
<= pt
->last_level
; level
++) {
264 for (i
= 0; i
< depth
; i
++)
265 i915_miptree_set_image_offset(tex
, level
, i
,
266 0, i
* stack_height
);
268 depth
= minify(depth
);
272 /* Multiply slice size by texture depth for total size. It's
273 * remarkable how wasteful of memory the i915 texture layouts
274 * are. They are largely fixed in the i945.
276 tex
->total_height
= stack_height
* pt
->depth
[0];
281 unsigned width
= pt
->width
[0];
282 unsigned height
= pt
->height
[0];
285 tex
->pitch
= ((pt
->width
[0] * pt
->cpp
+ 3) & ~3) / pt
->cpp
;
286 tex
->total_height
= 0;
288 for (level
= pt
->first_level
; level
<= pt
->last_level
; level
++) {
289 i915_miptree_set_level_info(tex
, level
, 1,
290 0, tex
->total_height
,
294 img_height
= MAX2(1, height
/ 4);
296 img_height
= (MAX2(2, height
) + 1) & ~1;
298 tex
->total_height
+= img_height
;
300 width
= minify(width
);
301 height
= minify(height
);
307 DBG("%s: %dx%dx%d - sz 0x%x\n", __FUNCTION__,
309 tex->total_height, pt->cpp, tex->pitch * tex->total_height * pt->cpp);
317 i945_miptree_layout(struct pipe_context
*pipe
, struct i915_texture
* tex
)
319 struct pipe_texture
*pt
= &tex
->base
;
322 switch (pt
->target
) {
323 case PIPE_TEXTURE_CUBE
:{
324 const unsigned dim
= pt
->width
[0];
326 unsigned lvlWidth
= pt
->width
[0], lvlHeight
= pt
->height
[0];
328 assert(lvlWidth
== lvlHeight
); /* cubemap images are square */
330 /* Depending on the size of the largest images, pitch can be
331 * determined either by the old-style packing of cubemap faces,
332 * or the final row of 4x4, 2x2 and 1x1 faces below this.
335 tex
->pitch
= ((dim
* pt
->cpp
* 2 + 3) & ~3) / pt
->cpp
;
339 tex
->total_height
= dim
* 4 + 4;
341 /* Set all the levels to effectively occupy the whole rectangular region.
343 for (level
= pt
->first_level
; level
<= pt
->last_level
; level
++) {
344 i915_miptree_set_level_info(tex
, level
, 6,
346 lvlWidth
, lvlHeight
, 1);
352 for (face
= 0; face
< 6; face
++) {
353 unsigned x
= initial_offsets
[face
][0] * dim
;
354 unsigned y
= initial_offsets
[face
][1] * dim
;
357 if (dim
== 4 && face
>= 4) {
358 y
= tex
->total_height
- 4;
361 else if (dim
< 4 && (face
> 0 || pt
->first_level
> 0)) {
362 y
= tex
->total_height
- 4;
366 for (level
= pt
->first_level
; level
<= pt
->last_level
; level
++) {
367 i915_miptree_set_image_offset(tex
, level
, face
, x
, y
);
374 case PIPE_TEX_FACE_POS_X
:
375 case PIPE_TEX_FACE_NEG_X
:
376 x
+= step_offsets
[face
][0] * d
;
377 y
+= step_offsets
[face
][1] * d
;
379 case PIPE_TEX_FACE_POS_Y
:
380 case PIPE_TEX_FACE_NEG_Y
:
384 case PIPE_TEX_FACE_POS_Z
:
385 case PIPE_TEX_FACE_NEG_Z
:
386 y
= tex
->total_height
- 4;
392 y
= tex
->total_height
- 4;
401 x
+= step_offsets
[face
][0] * d
;
402 y
+= step_offsets
[face
][1] * d
;
409 case PIPE_TEXTURE_3D
:{
410 unsigned width
= pt
->width
[0];
411 unsigned height
= pt
->height
[0];
412 unsigned depth
= pt
->depth
[0];
413 unsigned pack_x_pitch
, pack_x_nr
;
414 unsigned pack_y_pitch
;
417 tex
->pitch
= ((pt
->width
[0] * pt
->cpp
+ 3) & ~3) / pt
->cpp
;
418 tex
->total_height
= 0;
420 pack_y_pitch
= MAX2(pt
->height
[0], 2);
421 pack_x_pitch
= tex
->pitch
;
424 for (level
= pt
->first_level
; level
<= pt
->last_level
; level
++) {
425 unsigned nr_images
= pt
->target
== PIPE_TEXTURE_3D
? depth
: 6;
430 i915_miptree_set_level_info(tex
, level
, nr_images
,
431 0, tex
->total_height
,
432 width
, height
, depth
);
434 for (q
= 0; q
< nr_images
;) {
435 for (j
= 0; j
< pack_x_nr
&& q
< nr_images
; j
++, q
++) {
436 i915_miptree_set_image_offset(tex
, level
, q
, x
, y
);
445 tex
->total_height
+= y
;
447 if (pack_x_pitch
> 4) {
450 assert(pack_x_pitch
* pack_x_nr
<= tex
->pitch
);
453 if (pack_y_pitch
> 2) {
457 width
= minify(width
);
458 height
= minify(height
);
459 depth
= minify(depth
);
464 case PIPE_TEXTURE_1D
:
465 case PIPE_TEXTURE_2D
:
466 // case PIPE_TEXTURE_RECTANGLE:
467 i945_miptree_layout_2d(tex
);
475 DBG("%s: %dx%dx%d - sz 0x%x\n", __FUNCTION__,
477 tex->total_height, pt->cpp, tex->pitch * tex->total_height * pt->cpp);
484 i915_texture_create(struct pipe_context
*pipe
, struct pipe_texture
**pt
)
486 struct i915_texture
*tex
= REALLOC(*pt
, sizeof(struct pipe_texture
),
487 sizeof(struct i915_texture
));
490 struct i915_context
*i915
= i915_context(pipe
);
492 memset(&tex
->base
+ 1, 0,
493 sizeof(struct i915_texture
) - sizeof(struct pipe_texture
));
495 if (i915
->flags
.is_i945
? i945_miptree_layout(pipe
, tex
) :
496 i915_miptree_layout(pipe
, tex
)) {
497 tex
->region
= pipe
->winsys
->region_alloc(pipe
->winsys
,
498 tex
->pitch
* tex
->base
.cpp
*
500 PIPE_SURFACE_FLAG_TEXTURE
);
513 i915_texture_release(struct pipe_context
*pipe
, struct pipe_texture
**pt
)
519 DBG("%s %p refcount will be %d\n",
520 __FUNCTION__, (void *) *pt, (*pt)->refcount - 1);
522 if (--(*pt
)->refcount
<= 0) {
523 struct i915_texture
*tex
= (struct i915_texture
*)*pt
;
527 DBG("%s deleting %p\n", __FUNCTION__, (void *) tex);
530 pipe
->winsys
->region_release(pipe
->winsys
, &tex
->region
);
532 for (i
= 0; i
< PIPE_MAX_TEXTURE_LEVELS
; i
++)
533 if (tex
->image_offset
[i
])
534 free(tex
->image_offset
[i
]);