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_inlines.h"
37 #include "pipe/p_util.h"
38 #include "pipe/p_winsys.h"
40 #include "i915_context.h"
41 #include "i915_texture.h"
42 #include "i915_debug.h"
43 #include "i915_screen.h"
46 * Helper function and arrays
50 * Initial offset for Cube map.
52 static const int initial_offsets
[6][2] = {
62 * Step offsets for Cube map.
64 static const int step_offsets
[6][2] = {
73 static unsigned minify( unsigned d
)
79 power_of_two(unsigned x
)
88 round_up(unsigned n
, unsigned multiple
)
90 return (n
+ multiple
- 1) & ~(multiple
- 1);
95 * More advanced helper funcs
100 i915_miptree_set_level_info(struct i915_texture
*tex
,
103 unsigned w
, unsigned h
, unsigned d
)
105 struct pipe_texture
*pt
= &tex
->base
;
107 assert(level
< PIPE_MAX_TEXTURE_LEVELS
);
109 pt
->width
[level
] = w
;
110 pt
->height
[level
] = h
;
111 pt
->depth
[level
] = d
;
113 tex
->nr_images
[level
] = nr_images
;
116 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
117 level, w, h, d, x, y, tex->level_offset[level]);
120 /* Not sure when this would happen, but anyway:
122 if (tex
->image_offset
[level
]) {
123 FREE(tex
->image_offset
[level
]);
124 tex
->image_offset
[level
] = NULL
;
128 assert(!tex
->image_offset
[level
]);
130 tex
->image_offset
[level
] = (unsigned *) MALLOC(nr_images
* sizeof(unsigned));
131 tex
->image_offset
[level
][0] = 0;
135 i915_miptree_set_image_offset(struct i915_texture
*tex
,
136 unsigned level
, unsigned img
, unsigned x
, unsigned y
)
138 if (img
== 0 && level
== 0)
139 assert(x
== 0 && y
== 0);
141 assert(img
< tex
->nr_images
[level
]);
143 tex
->image_offset
[level
][img
] = (x
+ y
* tex
->pitch
);
146 printf("%s level %d img %d pos %d,%d image_offset %x\n",
147 __FUNCTION__, level, img, x, y, tex->image_offset[level][img]);
158 * Special case to deal with display targets.
161 i915_displaytarget_layout(struct i915_texture
*tex
)
163 struct pipe_texture
*pt
= &tex
->base
;
165 if (pt
->last_level
> 0 || pt
->cpp
!= 4)
168 i915_miptree_set_level_info( tex
, 0, 1,
172 i915_miptree_set_image_offset( tex
, 0, 0, 0, 0 );
174 if (tex
->base
.width
[0] >= 128) {
175 tex
->pitch
= power_of_two(tex
->base
.width
[0] * pt
->cpp
) / pt
->cpp
;
176 tex
->total_height
= round_up(tex
->base
.height
[0], 8);
179 tex
->pitch
= round_up(tex
->base
.width
[0], 64 / pt
->cpp
);
180 tex
->total_height
= tex
->base
.height
[0];
184 printf("%s size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
185 tex->base.width[0], tex->base.height[0], pt->cpp,
186 tex->pitch, tex->total_height, tex->pitch * tex->total_height * 4);
193 i945_miptree_layout_2d( struct i915_texture
*tex
)
195 struct pipe_texture
*pt
= &tex
->base
;
196 int align_h
= 2, align_w
= 4;
200 unsigned width
= pt
->width
[0];
201 unsigned height
= pt
->height
[0];
203 #if 0 /* used for tiled display targets */
204 if (pt
->last_level
== 0 && pt
->cpp
== 4)
205 if (i915_displaytarget_layout(tex
))
209 tex
->pitch
= pt
->width
[0];
211 /* May need to adjust pitch to accomodate the placement of
212 * the 2nd mipmap level. This occurs when the alignment
213 * constraints of mipmap placement push the right edge of the
214 * 2nd mipmap level out past the width of its parent.
216 if (pt
->last_level
> 0) {
217 unsigned mip1_width
= align_int(minify(pt
->width
[0]), align_w
)
218 + minify(minify(pt
->width
[0]));
220 if (mip1_width
> pt
->width
[0])
221 tex
->pitch
= mip1_width
;
224 /* Pitch must be a whole number of dwords, even though we
225 * express it in texels.
227 tex
->pitch
= align_int(tex
->pitch
* pt
->cpp
, 64) / pt
->cpp
;
228 tex
->total_height
= 0;
230 for (level
= 0; level
<= pt
->last_level
; level
++) {
233 i915_miptree_set_level_info(tex
, level
, 1, width
, height
, 1);
234 i915_miptree_set_image_offset(tex
, level
, 0, x
, y
);
237 img_height
= MAX2(1, height
/4);
239 img_height
= align_int(height
, align_h
);
242 /* Because the images are packed better, the final offset
243 * might not be the maximal one:
245 tex
->total_height
= MAX2(tex
->total_height
, y
+ img_height
);
247 /* Layout_below: step right after second mipmap level.
250 x
+= align_int(width
, align_w
);
256 width
= minify(width
);
257 height
= minify(height
);
262 i945_miptree_layout_cube(struct i915_texture
*tex
)
264 struct pipe_texture
*pt
= &tex
->base
;
267 const unsigned dim
= pt
->width
[0];
269 unsigned lvlWidth
= pt
->width
[0], lvlHeight
= pt
->height
[0];
272 printf("%s %i, %i\n", __FUNCTION__, pt->width[0], pt->height[0]);
275 assert(lvlWidth
== lvlHeight
); /* cubemap images are square */
278 * XXX Should only be used for compressed formats. But lets
279 * keep this code active just in case.
281 * Depending on the size of the largest images, pitch can be
282 * determined either by the old-style packing of cubemap faces,
283 * or the final row of 4x4, 2x2 and 1x1 faces below this.
286 tex
->pitch
= ((dim
* pt
->cpp
* 2 + 3) & ~3) / pt
->cpp
;
291 * XXX The 4 is only needed for compressed formats. See above.
293 tex
->total_height
= dim
* 4 + 4;
295 /* Set all the levels to effectively occupy the whole rectangular region.
297 for (level
= 0; level
<= pt
->last_level
; level
++) {
298 i915_miptree_set_level_info(tex
, level
, 6, lvlWidth
, lvlHeight
, 1);
303 for (face
= 0; face
< 6; face
++) {
304 unsigned x
= initial_offsets
[face
][0] * dim
;
305 unsigned y
= initial_offsets
[face
][1] * dim
;
308 #if 0 /* Fix and enable this code for compressed formats */
309 if (dim
== 4 && face
>= 4) {
310 y
= tex
->total_height
- 4;
313 else if (dim
< 4 && (face
> 0)) {
314 y
= tex
->total_height
- 4;
319 for (level
= 0; level
<= pt
->last_level
; level
++) {
320 i915_miptree_set_image_offset(tex
, level
, face
, x
, y
);
324 #if 0 /* Fix and enable this code for compressed formats */
328 case PIPE_TEX_FACE_POS_X
:
329 case PIPE_TEX_FACE_NEG_X
:
330 x
+= step_offsets
[face
][0] * d
;
331 y
+= step_offsets
[face
][1] * d
;
333 case PIPE_TEX_FACE_POS_Y
:
334 case PIPE_TEX_FACE_NEG_Y
:
338 case PIPE_TEX_FACE_POS_Z
:
339 case PIPE_TEX_FACE_NEG_Z
:
340 y
= tex
->total_height
- 4;
345 y
= tex
->total_height
- 4;
354 x
+= step_offsets
[face
][0] * d
;
355 y
+= step_offsets
[face
][1] * d
;
365 i915_miptree_layout(struct i915_texture
* tex
)
367 struct pipe_texture
*pt
= &tex
->base
;
370 switch (pt
->target
) {
371 case PIPE_TEXTURE_CUBE
: {
372 const unsigned dim
= pt
->width
[0];
374 unsigned lvlWidth
= pt
->width
[0], lvlHeight
= pt
->height
[0];
376 assert(lvlWidth
== lvlHeight
); /* cubemap images are square */
378 /* double pitch for cube layouts */
379 tex
->pitch
= ((dim
* pt
->cpp
* 2 + 3) & ~3) / pt
->cpp
;
380 tex
->total_height
= dim
* 4;
382 for (level
= 0; level
<= pt
->last_level
; level
++) {
383 i915_miptree_set_level_info(tex
, level
, 6,
390 for (face
= 0; face
< 6; face
++) {
391 unsigned x
= initial_offsets
[face
][0] * dim
;
392 unsigned y
= initial_offsets
[face
][1] * dim
;
395 for (level
= 0; level
<= pt
->last_level
; level
++) {
396 i915_miptree_set_image_offset(tex
, level
, face
, x
, y
);
398 x
+= step_offsets
[face
][0] * d
;
399 y
+= step_offsets
[face
][1] * d
;
404 case PIPE_TEXTURE_3D
:{
405 unsigned width
= pt
->width
[0];
406 unsigned height
= pt
->height
[0];
407 unsigned depth
= pt
->depth
[0];
408 unsigned stack_height
= 0;
410 /* Calculate the size of a single slice.
412 tex
->pitch
= ((pt
->width
[0] * pt
->cpp
+ 3) & ~3) / pt
->cpp
;
414 /* XXX: hardware expects/requires 9 levels at minimum.
416 for (level
= 0; level
<= MAX2(8, pt
->last_level
);
418 i915_miptree_set_level_info(tex
, level
, depth
,
419 width
, height
, depth
);
422 stack_height
+= MAX2(2, height
);
424 width
= minify(width
);
425 height
= minify(height
);
426 depth
= minify(depth
);
429 /* Fixup depth image_offsets:
431 depth
= pt
->depth
[0];
432 for (level
= 0; level
<= pt
->last_level
; level
++) {
434 for (i
= 0; i
< depth
; i
++)
435 i915_miptree_set_image_offset(tex
, level
, i
,
436 0, i
* stack_height
);
438 depth
= minify(depth
);
442 /* Multiply slice size by texture depth for total size. It's
443 * remarkable how wasteful of memory the i915 texture layouts
444 * are. They are largely fixed in the i945.
446 tex
->total_height
= stack_height
* pt
->depth
[0];
451 unsigned width
= pt
->width
[0];
452 unsigned height
= pt
->height
[0];
455 tex
->pitch
= ((pt
->width
[0] * pt
->cpp
+ 3) & ~3) / pt
->cpp
;
456 tex
->total_height
= 0;
458 for (level
= 0; level
<= pt
->last_level
; level
++) {
459 i915_miptree_set_level_info(tex
, level
, 1,
461 i915_miptree_set_image_offset(tex
, level
, 0,
462 0, tex
->total_height
);
465 img_height
= MAX2(1, height
/ 4);
467 img_height
= (MAX2(2, height
) + 1) & ~1;
469 tex
->total_height
+= img_height
;
471 width
= minify(width
);
472 height
= minify(height
);
478 DBG("%s: %dx%dx%d - sz 0x%x\n", __FUNCTION__,
480 tex->total_height, pt->cpp, tex->pitch * tex->total_height * pt->cpp);
488 i945_miptree_layout(struct i915_texture
* tex
)
490 struct pipe_texture
*pt
= &tex
->base
;
493 switch (pt
->target
) {
494 case PIPE_TEXTURE_CUBE
:
495 i945_miptree_layout_cube(tex
);
497 case PIPE_TEXTURE_3D
:{
498 unsigned width
= pt
->width
[0];
499 unsigned height
= pt
->height
[0];
500 unsigned depth
= pt
->depth
[0];
501 unsigned pack_x_pitch
, pack_x_nr
;
502 unsigned pack_y_pitch
;
504 tex
->pitch
= ((pt
->width
[0] * pt
->cpp
+ 3) & ~3) / pt
->cpp
;
505 tex
->total_height
= 0;
507 pack_y_pitch
= MAX2(pt
->height
[0], 2);
508 pack_x_pitch
= tex
->pitch
;
511 for (level
= 0; level
<= pt
->last_level
; level
++) {
512 unsigned nr_images
= pt
->target
== PIPE_TEXTURE_3D
? depth
: 6;
517 i915_miptree_set_level_info(tex
, level
, nr_images
,
518 width
, height
, depth
);
520 for (q
= 0; q
< nr_images
;) {
521 for (j
= 0; j
< pack_x_nr
&& q
< nr_images
; j
++, q
++) {
522 i915_miptree_set_image_offset(tex
, level
, q
, x
, y
+ tex
->total_height
);
531 tex
->total_height
+= y
;
533 if (pack_x_pitch
> 4) {
536 assert(pack_x_pitch
* pack_x_nr
<= tex
->pitch
);
539 if (pack_y_pitch
> 2) {
543 width
= minify(width
);
544 height
= minify(height
);
545 depth
= minify(depth
);
550 case PIPE_TEXTURE_1D
:
551 case PIPE_TEXTURE_2D
:
552 // case PIPE_TEXTURE_RECTANGLE:
553 i945_miptree_layout_2d(tex
);
561 DBG("%s: %dx%dx%d - sz 0x%x\n", __FUNCTION__,
563 tex->total_height, pt->cpp, tex->pitch * tex->total_height * pt->cpp);
570 static struct pipe_texture
*
571 i915_texture_create(struct pipe_screen
*screen
,
572 const struct pipe_texture
*templat
)
574 struct i915_screen
*i915screen
= i915_screen(screen
);
575 struct pipe_winsys
*ws
= screen
->winsys
;
576 struct i915_texture
*tex
= CALLOC_STRUCT(i915_texture
);
581 tex
->base
= *templat
;
582 tex
->base
.refcount
= 1;
583 tex
->base
.screen
= screen
;
585 if (i915screen
->is_i945
) {
586 if (!i945_miptree_layout(tex
))
589 if (!i915_miptree_layout(tex
))
593 tex
->buffer
= ws
->buffer_create(ws
, 64,
594 PIPE_BUFFER_USAGE_PIXEL
,
595 tex
->pitch
* tex
->base
.cpp
*
610 i915_texture_release(struct pipe_screen
*screen
,
611 struct pipe_texture
**pt
)
617 DBG("%s %p refcount will be %d\n",
618 __FUNCTION__, (void *) *pt, (*pt)->refcount - 1);
620 if (--(*pt
)->refcount
<= 0) {
621 struct i915_texture
*tex
= (struct i915_texture
*)*pt
;
625 DBG("%s deleting %p\n", __FUNCTION__, (void *) tex);
628 pipe_buffer_reference(screen
->winsys
, &tex
->buffer
, NULL
);
630 for (i
= 0; i
< PIPE_MAX_TEXTURE_LEVELS
; i
++)
631 if (tex
->image_offset
[i
])
632 FREE(tex
->image_offset
[i
]);
639 static struct pipe_surface
*
640 i915_get_tex_surface(struct pipe_screen
*screen
,
641 struct pipe_texture
*pt
,
642 unsigned face
, unsigned level
, unsigned zslice
,
645 struct i915_texture
*tex
= (struct i915_texture
*)pt
;
646 struct pipe_winsys
*ws
= screen
->winsys
;
647 struct pipe_surface
*ps
;
648 unsigned offset
; /* in bytes */
650 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
651 offset
= tex
->image_offset
[level
][face
] * pt
->cpp
;
653 else if (pt
->target
== PIPE_TEXTURE_3D
) {
654 offset
= tex
->image_offset
[level
][zslice
] * pt
->cpp
;
657 offset
= tex
->image_offset
[level
][0] * pt
->cpp
;
662 ps
= CALLOC_STRUCT(pipe_surface
);
666 pipe_texture_reference(&ps
->texture
, pt
);
667 pipe_buffer_reference(ws
, &ps
->buffer
, tex
->buffer
);
668 ps
->format
= pt
->format
;
670 ps
->width
= pt
->width
[level
];
671 ps
->height
= pt
->height
[level
];
672 ps
->pitch
= tex
->pitch
;
675 ps
->status
= PIPE_SURFACE_STATUS_DEFINED
;
680 static struct pipe_texture
*
681 i915_texture_blanket(struct pipe_screen
* screen
,
682 const struct pipe_texture
*base
,
683 const unsigned *pitch
,
684 struct pipe_buffer
*buffer
)
686 struct i915_texture
*tex
;
689 /* Only supports one type */
690 if (base
->target
!= PIPE_TEXTURE_2D
||
691 base
->last_level
!= 0 ||
692 base
->depth
[0] != 1) {
696 tex
= CALLOC_STRUCT(i915_texture
);
702 tex
->pitch
= pitch
[0];
704 i915_miptree_set_level_info(tex
, 0, 1, base
->width
[0], base
->height
[0], 1);
705 i915_miptree_set_image_offset(tex
, 0, 0, 0, 0);
707 pipe_buffer_reference(screen
->winsys
, &tex
->buffer
, buffer
);
713 i915_init_texture_functions(struct i915_context
*i915
)
715 // i915->pipe.texture_update = i915_texture_update;
719 i915_tex_surface_release(struct pipe_screen
*screen
,
720 struct pipe_surface
**surface
)
722 struct pipe_surface
*surf
= *surface
;
724 if (--surf
->refcount
== 0) {
726 /* This really should not be possible, but it's actually
727 * happening quite a bit... Will fix.
729 if (surf
->status
== PIPE_SURFACE_STATUS_CLEAR
) {
730 debug_printf("XXX destroying a surface with pending clears...\n");
734 pipe_texture_reference(&surf
->texture
, NULL
);
735 pipe_buffer_reference(screen
->winsys
, &surf
->buffer
, NULL
);
743 i915_init_screen_texture_functions(struct pipe_screen
*screen
)
745 screen
->texture_create
= i915_texture_create
;
746 screen
->texture_release
= i915_texture_release
;
747 screen
->get_tex_surface
= i915_get_tex_surface
;
748 screen
->texture_blanket
= i915_texture_blanket
;
749 screen
->tex_surface_release
= i915_tex_surface_release
;