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_winsys.h"
38 #include "util/u_math.h"
39 #include "util/u_memory.h"
41 #include "i915_context.h"
42 #include "i915_texture.h"
43 #include "i915_debug.h"
44 #include "i915_screen.h"
47 * Helper function and arrays
51 * Initial offset for Cube map.
53 static const int initial_offsets
[6][2] = {
63 * Step offsets for Cube map.
65 static const int step_offsets
[6][2] = {
74 static unsigned minify( unsigned d
)
80 power_of_two(unsigned x
)
89 round_up(unsigned n
, unsigned multiple
)
91 return (n
+ multiple
- 1) & ~(multiple
- 1);
96 * More advanced helper funcs
101 i915_miptree_set_level_info(struct i915_texture
*tex
,
104 unsigned w
, unsigned h
, unsigned d
)
106 struct pipe_texture
*pt
= &tex
->base
;
108 assert(level
< PIPE_MAX_TEXTURE_LEVELS
);
110 pt
->width
[level
] = w
;
111 pt
->height
[level
] = h
;
112 pt
->depth
[level
] = d
;
114 pt
->nblocksx
[level
] = pf_get_nblocksx(&pt
->block
, w
);
115 pt
->nblocksy
[level
] = pf_get_nblocksy(&pt
->block
, h
);
117 tex
->nr_images
[level
] = nr_images
;
120 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
121 level, w, h, d, x, y, tex->level_offset[level]);
124 /* Not sure when this would happen, but anyway:
126 if (tex
->image_offset
[level
]) {
127 FREE(tex
->image_offset
[level
]);
128 tex
->image_offset
[level
] = NULL
;
132 assert(!tex
->image_offset
[level
]);
134 tex
->image_offset
[level
] = (unsigned *) MALLOC(nr_images
* sizeof(unsigned));
135 tex
->image_offset
[level
][0] = 0;
139 i915_miptree_set_image_offset(struct i915_texture
*tex
,
140 unsigned level
, unsigned img
, unsigned x
, unsigned y
)
142 if (img
== 0 && level
== 0)
143 assert(x
== 0 && y
== 0);
145 assert(img
< tex
->nr_images
[level
]);
147 tex
->image_offset
[level
][img
] = y
* tex
->stride
+ x
* tex
->base
.block
.size
;
150 printf("%s level %d img %d pos %d,%d image_offset %x\n",
151 __FUNCTION__, level, img, x, y, tex->image_offset[level][img]);
162 * Special case to deal with display targets.
165 i915_displaytarget_layout(struct i915_texture
*tex
)
167 struct pipe_texture
*pt
= &tex
->base
;
169 if (pt
->last_level
> 0 || pt
->block
.size
!= 4)
172 i915_miptree_set_level_info( tex
, 0, 1,
176 i915_miptree_set_image_offset( tex
, 0, 0, 0, 0 );
178 if (tex
->base
.width
[0] >= 128) {
179 tex
->stride
= power_of_two(tex
->base
.nblocksx
[0] * pt
->block
.size
);
180 tex
->total_nblocksy
= round_up(tex
->base
.nblocksy
[0], 8);
183 tex
->stride
= round_up(tex
->base
.nblocksx
[0] * pt
->block
.size
, 64);
184 tex
->total_nblocksy
= tex
->base
.nblocksy
[0];
188 printf("%s size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
189 tex->base.width[0], tex->base.height[0], pt->block.size,
190 tex->stride, tex->total_nblocksy, tex->stride * tex->total_nblocksy);
197 i945_miptree_layout_2d( struct i915_texture
*tex
)
199 struct pipe_texture
*pt
= &tex
->base
;
200 const int align_x
= 2, align_y
= 4;
204 unsigned width
= pt
->width
[0];
205 unsigned height
= pt
->height
[0];
206 unsigned nblocksx
= pt
->nblocksx
[0];
207 unsigned nblocksy
= pt
->nblocksy
[0];
209 /* used for tiled display targets */
211 if (i915_displaytarget_layout(tex
))
214 tex
->stride
= round_up(pt
->nblocksx
[0] * pt
->block
.size
, 4);
216 /* May need to adjust pitch to accomodate the placement of
217 * the 2nd mipmap level. This occurs when the alignment
218 * constraints of mipmap placement push the right edge of the
219 * 2nd mipmap level out past the width of its parent.
221 if (pt
->last_level
> 0) {
222 unsigned mip1_nblocksx
223 = align(pf_get_nblocksx(&pt
->block
, minify(width
)), align_x
)
224 + pf_get_nblocksx(&pt
->block
, minify(minify(width
)));
226 if (mip1_nblocksx
> nblocksx
)
227 tex
->stride
= mip1_nblocksx
* pt
->block
.size
;
230 /* Pitch must be a whole number of dwords
232 tex
->stride
= align(tex
->stride
, 64);
233 tex
->total_nblocksy
= 0;
235 for (level
= 0; level
<= pt
->last_level
; level
++) {
236 i915_miptree_set_level_info(tex
, level
, 1, width
, height
, 1);
237 i915_miptree_set_image_offset(tex
, level
, 0, x
, y
);
239 nblocksy
= align(nblocksy
, align_y
);
241 /* Because the images are packed better, the final offset
242 * might not be the maximal one:
244 tex
->total_nblocksy
= MAX2(tex
->total_nblocksy
, y
+ nblocksy
);
246 /* Layout_below: step right after second mipmap level.
249 x
+= align(nblocksx
, align_x
);
255 width
= minify(width
);
256 height
= minify(height
);
257 nblocksx
= pf_get_nblocksx(&pt
->block
, width
);
258 nblocksy
= pf_get_nblocksy(&pt
->block
, height
);
263 i945_miptree_layout_cube(struct i915_texture
*tex
)
265 struct pipe_texture
*pt
= &tex
->base
;
268 const unsigned nblocks
= pt
->nblocksx
[0];
270 unsigned width
= pt
->width
[0];
271 unsigned height
= pt
->height
[0];
274 printf("%s %i, %i\n", __FUNCTION__, pt->width[0], pt->height[0]);
277 assert(width
== height
); /* cubemap images are square */
280 * XXX Should only be used for compressed formats. But lets
281 * keep this code active just in case.
283 * Depending on the size of the largest images, pitch can be
284 * determined either by the old-style packing of cubemap faces,
285 * or the final row of 4x4, 2x2 and 1x1 faces below this.
288 tex
->stride
= round_up(nblocks
* pt
->block
.size
* 2, 4);
290 tex
->stride
= 14 * 8 * pt
->block
.size
;
292 tex
->total_nblocksy
= nblocks
* 4;
294 /* Set all the levels to effectively occupy the whole rectangular region.
296 for (level
= 0; level
<= pt
->last_level
; level
++) {
297 i915_miptree_set_level_info(tex
, level
, 6, width
, height
, 1);
302 for (face
= 0; face
< 6; face
++) {
303 unsigned x
= initial_offsets
[face
][0] * nblocks
;
304 unsigned y
= initial_offsets
[face
][1] * nblocks
;
305 unsigned d
= nblocks
;
307 #if 0 /* Fix and enable this code for compressed formats */
308 if (nblocks
== 4 && face
>= 4) {
309 y
= tex
->total_height
- 4;
312 else if (nblocks
< 4 && (face
> 0)) {
313 y
= tex
->total_height
- 4;
318 for (level
= 0; level
<= pt
->last_level
; level
++) {
319 i915_miptree_set_image_offset(tex
, level
, face
, x
, y
);
323 #if 0 /* Fix and enable this code for compressed formats */
327 case PIPE_TEX_FACE_POS_X
:
328 case PIPE_TEX_FACE_NEG_X
:
329 x
+= step_offsets
[face
][0] * d
;
330 y
+= step_offsets
[face
][1] * d
;
332 case PIPE_TEX_FACE_POS_Y
:
333 case PIPE_TEX_FACE_NEG_Y
:
337 case PIPE_TEX_FACE_POS_Z
:
338 case PIPE_TEX_FACE_NEG_Z
:
339 y
= tex
->total_height
- 4;
344 y
= tex
->total_height
- 4;
353 x
+= step_offsets
[face
][0] * d
;
354 y
+= step_offsets
[face
][1] * d
;
364 i915_miptree_layout(struct i915_texture
* tex
)
366 struct pipe_texture
*pt
= &tex
->base
;
369 switch (pt
->target
) {
370 case PIPE_TEXTURE_CUBE
: {
371 const unsigned nblocks
= pt
->nblocksx
[0];
373 unsigned width
= pt
->width
[0], height
= pt
->height
[0];
375 assert(width
== height
); /* cubemap images are square */
377 /* double pitch for cube layouts */
378 tex
->stride
= round_up(nblocks
* pt
->block
.size
* 2, 4);
379 tex
->total_nblocksy
= nblocks
* 4;
381 for (level
= 0; level
<= pt
->last_level
; level
++) {
382 i915_miptree_set_level_info(tex
, level
, 6,
389 for (face
= 0; face
< 6; face
++) {
390 unsigned x
= initial_offsets
[face
][0] * nblocks
;
391 unsigned y
= initial_offsets
[face
][1] * nblocks
;
392 unsigned d
= nblocks
;
394 for (level
= 0; level
<= pt
->last_level
; level
++) {
395 i915_miptree_set_image_offset(tex
, level
, face
, x
, y
);
397 x
+= step_offsets
[face
][0] * d
;
398 y
+= step_offsets
[face
][1] * d
;
403 case PIPE_TEXTURE_3D
:{
404 unsigned width
= pt
->width
[0];
405 unsigned height
= pt
->height
[0];
406 unsigned depth
= pt
->depth
[0];
407 unsigned nblocksx
= pt
->nblocksx
[0];
408 unsigned nblocksy
= pt
->nblocksy
[0];
409 unsigned stack_nblocksy
= 0;
411 /* Calculate the size of a single slice.
413 tex
->stride
= round_up(pt
->nblocksx
[0] * pt
->block
.size
, 4);
415 /* XXX: hardware expects/requires 9 levels at minimum.
417 for (level
= 0; level
<= MAX2(8, pt
->last_level
);
419 i915_miptree_set_level_info(tex
, level
, depth
,
420 width
, height
, depth
);
423 stack_nblocksy
+= MAX2(2, nblocksy
);
425 width
= minify(width
);
426 height
= minify(height
);
427 depth
= minify(depth
);
428 nblocksx
= pf_get_nblocksx(&pt
->block
, width
);
429 nblocksy
= pf_get_nblocksy(&pt
->block
, height
);
432 /* Fixup depth image_offsets:
434 depth
= pt
->depth
[0];
435 for (level
= 0; level
<= pt
->last_level
; level
++) {
437 for (i
= 0; i
< depth
; i
++)
438 i915_miptree_set_image_offset(tex
, level
, i
,
439 0, i
* stack_nblocksy
);
441 depth
= minify(depth
);
445 /* Multiply slice size by texture depth for total size. It's
446 * remarkable how wasteful of memory the i915 texture layouts
447 * are. They are largely fixed in the i945.
449 tex
->total_nblocksy
= stack_nblocksy
* pt
->depth
[0];
454 unsigned width
= pt
->width
[0];
455 unsigned height
= pt
->height
[0];
456 unsigned nblocksx
= pt
->nblocksx
[0];
457 unsigned nblocksy
= pt
->nblocksy
[0];
459 tex
->stride
= round_up(pt
->nblocksx
[0] * pt
->block
.size
, 4);
460 tex
->total_nblocksy
= 0;
462 for (level
= 0; level
<= pt
->last_level
; level
++) {
463 i915_miptree_set_level_info(tex
, level
, 1,
465 i915_miptree_set_image_offset(tex
, level
, 0,
466 0, tex
->total_nblocksy
);
468 nblocksy
= round_up(MAX2(2, nblocksy
), 2);
470 tex
->total_nblocksy
+= nblocksy
;
472 width
= minify(width
);
473 height
= minify(height
);
474 nblocksx
= pf_get_nblocksx(&pt
->block
, width
);
475 nblocksy
= pf_get_nblocksy(&pt
->block
, height
);
481 DBG("%s: %dx%dx%d - sz 0x%x\n", __FUNCTION__,
483 tex->total_nblocksy, pt->block.size, tex->stride * tex->total_nblocksy);
491 i945_miptree_layout(struct i915_texture
* tex
)
493 struct pipe_texture
*pt
= &tex
->base
;
496 switch (pt
->target
) {
497 case PIPE_TEXTURE_CUBE
:
498 i945_miptree_layout_cube(tex
);
500 case PIPE_TEXTURE_3D
:{
501 unsigned width
= pt
->width
[0];
502 unsigned height
= pt
->height
[0];
503 unsigned depth
= pt
->depth
[0];
504 unsigned nblocksx
= pt
->nblocksx
[0];
505 unsigned nblocksy
= pt
->nblocksy
[0];
506 unsigned pack_x_pitch
, pack_x_nr
;
507 unsigned pack_y_pitch
;
509 tex
->stride
= round_up(pt
->nblocksx
[0] * pt
->block
.size
, 4);
510 tex
->total_nblocksy
= 0;
512 pack_y_pitch
= MAX2(pt
->nblocksy
[0], 2);
513 pack_x_pitch
= tex
->stride
/ pt
->block
.size
;
516 for (level
= 0; level
<= pt
->last_level
; level
++) {
517 unsigned nr_images
= pt
->target
== PIPE_TEXTURE_3D
? depth
: 6;
522 i915_miptree_set_level_info(tex
, level
, nr_images
,
523 width
, height
, depth
);
525 for (q
= 0; q
< nr_images
;) {
526 for (j
= 0; j
< pack_x_nr
&& q
< nr_images
; j
++, q
++) {
527 i915_miptree_set_image_offset(tex
, level
, q
, x
, y
+ tex
->total_nblocksy
);
536 tex
->total_nblocksy
+= y
;
538 if (pack_x_pitch
> 4) {
541 assert(pack_x_pitch
* pack_x_nr
* pt
->block
.size
<= tex
->stride
);
544 if (pack_y_pitch
> 2) {
548 width
= minify(width
);
549 height
= minify(height
);
550 depth
= minify(depth
);
551 nblocksx
= pf_get_nblocksx(&pt
->block
, width
);
552 nblocksy
= pf_get_nblocksy(&pt
->block
, height
);
557 case PIPE_TEXTURE_1D
:
558 case PIPE_TEXTURE_2D
:
559 // case PIPE_TEXTURE_RECTANGLE:
560 i945_miptree_layout_2d(tex
);
568 DBG("%s: %dx%dx%d - sz 0x%x\n", __FUNCTION__,
570 tex->total_nblocksy, pt->block.size, tex->stride * tex->total_nblocksy);
577 static struct pipe_texture
*
578 i915_texture_create(struct pipe_screen
*screen
,
579 const struct pipe_texture
*templat
)
581 struct i915_screen
*i915screen
= i915_screen(screen
);
582 struct pipe_winsys
*ws
= screen
->winsys
;
583 struct i915_texture
*tex
= CALLOC_STRUCT(i915_texture
);
589 tex
->base
= *templat
;
590 tex
->base
.refcount
= 1;
591 tex
->base
.screen
= screen
;
593 tex
->base
.nblocksx
[0] = pf_get_nblocksx(&tex
->base
.block
, tex
->base
.width
[0]);
594 tex
->base
.nblocksy
[0] = pf_get_nblocksy(&tex
->base
.block
, tex
->base
.height
[0]);
596 if (i915screen
->is_i945
) {
597 if (!i945_miptree_layout(tex
))
600 if (!i915_miptree_layout(tex
))
604 tex_size
= tex
->stride
* tex
->total_nblocksy
;
606 tex
->buffer
= ws
->buffer_create(ws
, 64,
607 PIPE_BUFFER_USAGE_PIXEL
,
614 void *ptr
= ws
->buffer_map(ws
, tex
->buffer
,
615 PIPE_BUFFER_USAGE_CPU_WRITE
);
616 memset(ptr
, 0x80, tex_size
);
617 ws
->buffer_unmap(ws
, tex
->buffer
);
629 i915_texture_release(struct pipe_screen
*screen
,
630 struct pipe_texture
**pt
)
636 DBG("%s %p refcount will be %d\n",
637 __FUNCTION__, (void *) *pt, (*pt)->refcount - 1);
639 if (--(*pt
)->refcount
<= 0) {
640 struct i915_texture
*tex
= (struct i915_texture
*)*pt
;
644 DBG("%s deleting %p\n", __FUNCTION__, (void *) tex);
647 pipe_buffer_reference(screen
, &tex
->buffer
, NULL
);
649 for (i
= 0; i
< PIPE_MAX_TEXTURE_LEVELS
; i
++)
650 if (tex
->image_offset
[i
])
651 FREE(tex
->image_offset
[i
]);
658 static struct pipe_surface
*
659 i915_get_tex_surface(struct pipe_screen
*screen
,
660 struct pipe_texture
*pt
,
661 unsigned face
, unsigned level
, unsigned zslice
,
664 struct i915_texture
*tex
= (struct i915_texture
*)pt
;
665 struct pipe_winsys
*ws
= screen
->winsys
;
666 struct pipe_surface
*ps
;
667 unsigned offset
; /* in bytes */
669 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
670 offset
= tex
->image_offset
[level
][face
];
672 else if (pt
->target
== PIPE_TEXTURE_3D
) {
673 offset
= tex
->image_offset
[level
][zslice
];
676 offset
= tex
->image_offset
[level
][0];
681 ps
= CALLOC_STRUCT(pipe_surface
);
685 pipe_texture_reference(&ps
->texture
, pt
);
686 pipe_buffer_reference(screen
, &ps
->buffer
, tex
->buffer
);
687 ps
->format
= pt
->format
;
688 ps
->width
= pt
->width
[level
];
689 ps
->height
= pt
->height
[level
];
690 ps
->block
= pt
->block
;
691 ps
->nblocksx
= pt
->nblocksx
[level
];
692 ps
->nblocksy
= pt
->nblocksy
[level
];
693 ps
->stride
= tex
->stride
;
696 ps
->status
= PIPE_SURFACE_STATUS_DEFINED
;
701 static struct pipe_texture
*
702 i915_texture_blanket(struct pipe_screen
* screen
,
703 const struct pipe_texture
*base
,
704 const unsigned *stride
,
705 struct pipe_buffer
*buffer
)
707 struct i915_texture
*tex
;
710 /* Only supports one type */
711 if (base
->target
!= PIPE_TEXTURE_2D
||
712 base
->last_level
!= 0 ||
713 base
->depth
[0] != 1) {
717 tex
= CALLOC_STRUCT(i915_texture
);
722 tex
->base
.refcount
= 1;
723 tex
->base
.screen
= screen
;
725 tex
->stride
= stride
[0];
727 i915_miptree_set_level_info(tex
, 0, 1, base
->width
[0], base
->height
[0], 1);
728 i915_miptree_set_image_offset(tex
, 0, 0, 0, 0);
730 pipe_buffer_reference(screen
, &tex
->buffer
, buffer
);
736 i915_init_texture_functions(struct i915_context
*i915
)
738 // i915->pipe.texture_update = i915_texture_update;
742 i915_tex_surface_release(struct pipe_screen
*screen
,
743 struct pipe_surface
**surface
)
745 struct pipe_surface
*surf
= *surface
;
747 if (--surf
->refcount
== 0) {
749 /* This really should not be possible, but it's actually
750 * happening quite a bit... Will fix.
752 if (surf
->status
== PIPE_SURFACE_STATUS_CLEAR
) {
753 debug_printf("XXX destroying a surface with pending clears...\n");
757 pipe_texture_reference(&surf
->texture
, NULL
);
758 pipe_buffer_reference(screen
, &surf
->buffer
, NULL
);
766 i915_init_screen_texture_functions(struct pipe_screen
*screen
)
768 screen
->texture_create
= i915_texture_create
;
769 screen
->texture_release
= i915_texture_release
;
770 screen
->get_tex_surface
= i915_get_tex_surface
;
771 screen
->texture_blanket
= i915_texture_blanket
;
772 screen
->tex_surface_release
= i915_tex_surface_release
;