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 "util/u_inlines.h"
37 #include "util/u_format.h"
38 #include "util/u_math.h"
39 #include "util/u_memory.h"
41 #include "i915_context.h"
42 #include "i915_resource.h"
43 #include "i915_screen.h"
44 #include "i915_winsys.h"
48 * Helper function and arrays
53 * Initial offset for Cube map.
55 static const int initial_offsets
[6][2] = {
65 * Step offsets for Cube map.
67 static const int step_offsets
[6][2] = {
76 /* XXX really need twice the size if x is already pot?
77 Otherwise just use util_next_power_of_two?
80 power_of_two(unsigned x
)
90 * More advanced helper funcs
95 i915_texture_set_level_info(struct i915_texture
*tex
,
96 unsigned level
, unsigned nr_images
)
98 assert(level
< Elements(tex
->nr_images
));
100 assert(!tex
->image_offset
[level
]);
102 tex
->nr_images
[level
] = nr_images
;
103 tex
->image_offset
[level
] = (unsigned *) MALLOC(nr_images
* sizeof(unsigned));
104 tex
->image_offset
[level
][0] = 0;
108 i915_texture_set_image_offset(struct i915_texture
*tex
,
109 unsigned level
, unsigned img
,
110 unsigned x
, unsigned y
)
112 /* for the first image and level make sure offset is zero */
113 assert(!(img
== 0 && level
== 0) || (x
== 0 && y
== 0));
114 assert(img
< tex
->nr_images
[level
]);
116 tex
->image_offset
[level
][img
] = y
* tex
->stride
+ x
* util_format_get_blocksize(tex
->b
.b
.format
);
119 printf("%s level %d img %d pos %d,%d image_offset %x\n",
120 __FUNCTION__, level, img, x, y, tex->image_offset[level][img]);
126 * Shared layout functions
131 * Special case to deal with scanout textures.
134 i9x5_scanout_layout(struct i915_texture
*tex
)
136 struct pipe_resource
*pt
= &tex
->b
.b
;
138 if (pt
->last_level
> 0 || util_format_get_blocksize(pt
->format
) != 4)
141 i915_texture_set_level_info(tex
, 0, 1);
142 i915_texture_set_image_offset(tex
, 0, 0, 0, 0);
144 if (pt
->width0
>= 240) {
145 tex
->stride
= power_of_two(util_format_get_stride(pt
->format
, pt
->width0
));
146 tex
->total_nblocksy
= align(util_format_get_nblocksy(pt
->format
, pt
->height0
), 8);
147 tex
->hw_tiled
= I915_TILE_X
;
148 } else if (pt
->width0
== 64 && pt
->height0
== 64) {
149 tex
->stride
= power_of_two(util_format_get_stride(pt
->format
, pt
->width0
));
150 tex
->total_nblocksy
= align(util_format_get_nblocksy(pt
->format
, pt
->height0
), 8);
155 debug_printf("%s size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__
,
156 pt
->width0
, pt
->height0
, util_format_get_blocksize(pt
->format
),
157 tex
->stride
, tex
->total_nblocksy
, tex
->stride
* tex
->total_nblocksy
);
163 * Special case to deal with shared textures.
166 i9x5_display_target_layout(struct i915_texture
*tex
)
168 struct pipe_resource
*pt
= &tex
->b
.b
;
170 if (pt
->last_level
> 0 || util_format_get_blocksize(pt
->format
) != 4)
173 /* fallback to normal textures for small textures */
174 if (pt
->width0
< 240)
177 i915_texture_set_level_info(tex
, 0, 1);
178 i915_texture_set_image_offset(tex
, 0, 0, 0, 0);
180 tex
->stride
= power_of_two(util_format_get_stride(pt
->format
, pt
->width0
));
181 tex
->total_nblocksy
= align(util_format_get_nblocksy(pt
->format
, pt
->height0
), 8);
182 tex
->hw_tiled
= I915_TILE_X
;
184 debug_printf("%s size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__
,
185 pt
->width0
, pt
->height0
, util_format_get_blocksize(pt
->format
),
186 tex
->stride
, tex
->total_nblocksy
, tex
->stride
* tex
->total_nblocksy
);
192 * Helper function for special layouts
195 i9x5_special_layout(struct i915_texture
*tex
)
197 struct pipe_resource
*pt
= &tex
->b
.b
;
199 /* Scanouts needs special care */
200 if (pt
->bind
& PIPE_BIND_SCANOUT
)
201 if (i9x5_scanout_layout(tex
))
204 /* Shared buffers needs to be compatible with X servers
206 * XXX: need a better name than shared for this if it is to be part
207 * of core gallium, and probably move the flag to resource.flags,
208 * rather than bindings.
210 if (pt
->bind
& (PIPE_BIND_SHARED
| PIPE_BIND_DISPLAY_TARGET
))
211 if (i9x5_display_target_layout(tex
))
219 * i915 layout functions
224 i915_texture_layout_2d(struct i915_texture
*tex
)
226 struct pipe_resource
*pt
= &tex
->b
.b
;
228 unsigned width
= pt
->width0
;
229 unsigned height
= pt
->height0
;
230 unsigned nblocksy
= util_format_get_nblocksy(pt
->format
, pt
->width0
);
231 unsigned align_y
= 2;
233 if (util_format_is_s3tc(pt
->format
))
236 tex
->stride
= align(util_format_get_stride(pt
->format
, pt
->width0
), 4);
237 tex
->total_nblocksy
= 0;
239 for (level
= 0; level
<= pt
->last_level
; level
++) {
240 i915_texture_set_level_info(tex
, level
, 1);
241 i915_texture_set_image_offset(tex
, level
, 0, 0, tex
->total_nblocksy
);
243 tex
->total_nblocksy
+= nblocksy
;
245 width
= u_minify(width
, 1);
246 height
= u_minify(height
, 1);
247 nblocksy
= align(util_format_get_nblocksy(pt
->format
, height
), align_y
);
252 i915_texture_layout_3d(struct i915_texture
*tex
)
254 struct pipe_resource
*pt
= &tex
->b
.b
;
257 unsigned width
= pt
->width0
;
258 unsigned height
= pt
->height0
;
259 unsigned depth
= pt
->depth0
;
260 unsigned nblocksy
= util_format_get_nblocksy(pt
->format
, pt
->height0
);
261 unsigned stack_nblocksy
= 0;
263 /* Calculate the size of a single slice.
265 tex
->stride
= align(util_format_get_stride(pt
->format
, pt
->width0
), 4);
267 /* XXX: hardware expects/requires 9 levels at minimum.
269 for (level
= 0; level
<= MAX2(8, pt
->last_level
); level
++) {
270 i915_texture_set_level_info(tex
, level
, depth
);
272 stack_nblocksy
+= MAX2(2, nblocksy
);
274 width
= u_minify(width
, 1);
275 height
= u_minify(height
, 1);
276 nblocksy
= util_format_get_nblocksy(pt
->format
, height
);
279 /* Fixup depth image_offsets:
281 for (level
= 0; level
<= pt
->last_level
; level
++) {
283 for (i
= 0; i
< depth
; i
++)
284 i915_texture_set_image_offset(tex
, level
, i
, 0, i
* stack_nblocksy
);
286 depth
= u_minify(depth
, 1);
289 /* Multiply slice size by texture depth for total size. It's
290 * remarkable how wasteful of memory the i915 texture layouts
291 * are. They are largely fixed in the i945.
293 tex
->total_nblocksy
= stack_nblocksy
* pt
->depth0
;
297 i915_texture_layout_cube(struct i915_texture
*tex
)
299 struct pipe_resource
*pt
= &tex
->b
.b
;
300 const unsigned nblocks
= util_format_get_nblocksx(pt
->format
, pt
->width0
);
304 assert(pt
->width0
== pt
->height0
); /* cubemap images are square */
306 /* double pitch for cube layouts */
307 tex
->stride
= align(nblocks
* util_format_get_blocksize(pt
->format
) * 2, 4);
308 tex
->total_nblocksy
= nblocks
* 4;
310 for (level
= 0; level
<= pt
->last_level
; level
++)
311 i915_texture_set_level_info(tex
, level
, 6);
313 for (face
= 0; face
< 6; face
++) {
314 unsigned x
= initial_offsets
[face
][0] * nblocks
;
315 unsigned y
= initial_offsets
[face
][1] * nblocks
;
316 unsigned d
= nblocks
;
318 for (level
= 0; level
<= pt
->last_level
; level
++) {
319 i915_texture_set_image_offset(tex
, level
, face
, x
, y
);
321 x
+= step_offsets
[face
][0] * d
;
322 y
+= step_offsets
[face
][1] * d
;
328 i915_texture_layout(struct i915_texture
* tex
)
330 struct pipe_resource
*pt
= &tex
->b
.b
;
332 switch (pt
->target
) {
333 case PIPE_TEXTURE_1D
:
334 case PIPE_TEXTURE_2D
:
335 if (!i9x5_special_layout(tex
))
336 i915_texture_layout_2d(tex
);
338 case PIPE_TEXTURE_3D
:
339 i915_texture_layout_3d(tex
);
341 case PIPE_TEXTURE_CUBE
:
342 i915_texture_layout_cube(tex
);
354 * i945 layout functions
359 i945_texture_layout_2d(struct i915_texture
*tex
)
361 struct pipe_resource
*pt
= &tex
->b
.b
;
362 int align_x
= 4, align_y
= 2;
366 unsigned width
= pt
->width0
;
367 unsigned height
= pt
->height0
;
368 unsigned nblocksx
= util_format_get_nblocksx(pt
->format
, pt
->width0
);
369 unsigned nblocksy
= util_format_get_nblocksy(pt
->format
, pt
->height0
);
371 if (util_format_is_s3tc(pt
->format
)) {
376 tex
->stride
= align(util_format_get_stride(pt
->format
, pt
->width0
), 4);
378 /* May need to adjust pitch to accomodate the placement of
379 * the 2nd mipmap level. This occurs when the alignment
380 * constraints of mipmap placement push the right edge of the
381 * 2nd mipmap level out past the width of its parent.
383 if (pt
->last_level
> 0) {
384 unsigned mip1_nblocksx
=
385 align(util_format_get_nblocksx(pt
->format
, u_minify(pt
->width0
, 1)), align_x
) +
386 util_format_get_nblocksx(pt
->format
, u_minify(pt
->width0
, 2));
388 if (mip1_nblocksx
> nblocksx
)
389 tex
->stride
= mip1_nblocksx
* util_format_get_blocksize(pt
->format
);
392 /* Pitch must be a whole number of dwords
394 tex
->stride
= align(tex
->stride
, 64);
395 tex
->total_nblocksy
= 0;
397 for (level
= 0; level
<= pt
->last_level
; level
++) {
398 i915_texture_set_level_info(tex
, level
, 1);
399 i915_texture_set_image_offset(tex
, level
, 0, x
, y
);
401 /* Because the images are packed better, the final offset
402 * might not be the maximal one:
404 tex
->total_nblocksy
= MAX2(tex
->total_nblocksy
, y
+ nblocksy
);
406 /* Layout_below: step right after second mipmap level.
414 width
= u_minify(width
, 1);
415 height
= u_minify(height
, 1);
416 nblocksx
= align(util_format_get_nblocksx(pt
->format
, width
), align_x
);
417 nblocksy
= align(util_format_get_nblocksy(pt
->format
, height
), align_y
);
422 i945_texture_layout_3d(struct i915_texture
*tex
)
424 struct pipe_resource
*pt
= &tex
->b
.b
;
425 unsigned width
= pt
->width0
;
426 unsigned height
= pt
->height0
;
427 unsigned depth
= pt
->depth0
;
428 unsigned nblocksy
= util_format_get_nblocksy(pt
->format
, pt
->width0
);
429 unsigned pack_x_pitch
, pack_x_nr
;
430 unsigned pack_y_pitch
;
433 tex
->stride
= align(util_format_get_stride(pt
->format
, pt
->width0
), 4);
434 tex
->total_nblocksy
= 0;
436 pack_y_pitch
= MAX2(nblocksy
, 2);
437 pack_x_pitch
= tex
->stride
/ util_format_get_blocksize(pt
->format
);
440 for (level
= 0; level
<= pt
->last_level
; level
++) {
445 i915_texture_set_level_info(tex
, level
, depth
);
447 for (q
= 0; q
< depth
;) {
448 for (j
= 0; j
< pack_x_nr
&& q
< depth
; j
++, q
++) {
449 i915_texture_set_image_offset(tex
, level
, q
, x
, y
+ tex
->total_nblocksy
);
457 tex
->total_nblocksy
+= y
;
459 if (pack_x_pitch
> 4) {
462 assert(pack_x_pitch
* pack_x_nr
* util_format_get_blocksize(pt
->format
) <= tex
->stride
);
465 if (pack_y_pitch
> 2) {
469 width
= u_minify(width
, 1);
470 height
= u_minify(height
, 1);
471 depth
= u_minify(depth
, 1);
472 nblocksy
= util_format_get_nblocksy(pt
->format
, height
);
477 i945_texture_layout_cube(struct i915_texture
*tex
)
479 struct pipe_resource
*pt
= &tex
->b
.b
;
481 const unsigned nblocks
= util_format_get_nblocksx(pt
->format
, pt
->width0
);
485 printf("%s %i, %i\n", __FUNCTION__, pt->width0, pt->height0);
488 assert(pt
->width0
== pt
->height0
); /* cubemap images are square */
491 * XXX Should only be used for compressed formats. But lets
492 * keep this code active just in case.
494 * Depending on the size of the largest images, pitch can be
495 * determined either by the old-style packing of cubemap faces,
496 * or the final row of 4x4, 2x2 and 1x1 faces below this.
499 tex
->stride
= align(nblocks
* util_format_get_blocksize(pt
->format
) * 2, 4);
501 tex
->stride
= 14 * 8 * util_format_get_blocksize(pt
->format
);
503 tex
->total_nblocksy
= nblocks
* 4;
505 /* Set all the levels to effectively occupy the whole rectangular region.
507 for (level
= 0; level
<= pt
->last_level
; level
++)
508 i915_texture_set_level_info(tex
, level
, 6);
510 for (face
= 0; face
< 6; face
++) {
511 unsigned x
= initial_offsets
[face
][0] * nblocks
;
512 unsigned y
= initial_offsets
[face
][1] * nblocks
;
513 unsigned d
= nblocks
;
515 #if 0 /* Fix and enable this code for compressed formats */
516 if (nblocks
== 4 && face
>= 4) {
517 y
= tex
->total_height
- 4;
520 else if (nblocks
< 4 && (face
> 0)) {
521 y
= tex
->total_height
- 4;
526 for (level
= 0; level
<= pt
->last_level
; level
++) {
527 i915_texture_set_image_offset(tex
, level
, face
, x
, y
);
531 #if 0 /* Fix and enable this code for compressed formats */
535 case PIPE_TEX_FACE_POS_X
:
536 case PIPE_TEX_FACE_NEG_X
:
537 x
+= step_offsets
[face
][0] * d
;
538 y
+= step_offsets
[face
][1] * d
;
540 case PIPE_TEX_FACE_POS_Y
:
541 case PIPE_TEX_FACE_NEG_Y
:
545 case PIPE_TEX_FACE_POS_Z
:
546 case PIPE_TEX_FACE_NEG_Z
:
547 y
= tex
->total_height
- 4;
552 y
= tex
->total_height
- 4;
561 x
+= step_offsets
[face
][0] * d
;
562 y
+= step_offsets
[face
][1] * d
;
572 i945_texture_layout(struct i915_texture
* tex
)
574 struct pipe_resource
*pt
= &tex
->b
.b
;
576 switch (pt
->target
) {
577 case PIPE_TEXTURE_1D
:
578 case PIPE_TEXTURE_2D
:
579 if (!i9x5_special_layout(tex
))
580 i945_texture_layout_2d(tex
);
582 case PIPE_TEXTURE_3D
:
583 i945_texture_layout_3d(tex
);
585 case PIPE_TEXTURE_CUBE
:
586 i945_texture_layout_cube(tex
);
599 * Screen texture functions
605 i915_texture_get_handle(struct pipe_screen
* screen
,
606 struct pipe_resource
*texture
,
607 struct winsys_handle
*whandle
)
609 struct i915_screen
*is
= i915_screen(screen
);
610 struct i915_texture
*tex
= i915_texture(texture
);
611 struct i915_winsys
*iws
= is
->iws
;
613 return iws
->buffer_get_handle(iws
, tex
->buffer
, whandle
, tex
->stride
);
618 i915_texture_destroy(struct pipe_screen
*screen
,
619 struct pipe_resource
*pt
)
621 struct i915_texture
*tex
= i915_texture(pt
);
622 struct i915_winsys
*iws
= i915_screen(screen
)->iws
;
626 DBG("%s deleting %p\n", __FUNCTION__, (void *) tex);
629 iws
->buffer_destroy(iws
, tex
->buffer
);
631 for (i
= 0; i
< Elements(tex
->image_offset
); i
++)
632 if (tex
->image_offset
[i
])
633 FREE(tex
->image_offset
[i
]);
638 static struct pipe_transfer
*
639 i915_texture_get_transfer(struct pipe_context
*context
,
640 struct pipe_resource
*resource
,
641 struct pipe_subresource sr
,
643 const struct pipe_box
*box
)
645 struct i915_texture
*tex
= i915_texture(resource
);
646 struct pipe_transfer
*transfer
= CALLOC_STRUCT(pipe_transfer
);
647 if (transfer
== NULL
)
650 transfer
->resource
= resource
;
652 transfer
->usage
= usage
;
653 transfer
->box
= *box
;
654 transfer
->stride
= tex
->stride
;
661 i915_texture_transfer_map(struct pipe_context
*pipe
,
662 struct pipe_transfer
*transfer
)
664 struct pipe_resource
*resource
= transfer
->resource
;
665 struct i915_texture
*tex
= i915_texture(resource
);
666 struct i915_winsys
*iws
= i915_screen(pipe
->screen
)->iws
;
667 struct pipe_subresource sr
= transfer
->sr
;
668 struct pipe_box
*box
= &transfer
->box
;
669 enum pipe_format format
= resource
->format
;
673 if (resource
->target
== PIPE_TEXTURE_CUBE
) {
674 offset
= tex
->image_offset
[sr
.level
][sr
.face
];
675 } else if (resource
->target
== PIPE_TEXTURE_3D
) {
676 offset
= tex
->image_offset
[sr
.level
][box
->z
];
678 offset
= tex
->image_offset
[sr
.level
][0];
679 assert(sr
.face
== 0);
683 map
= iws
->buffer_map(iws
, tex
->buffer
,
684 (transfer
->usage
& PIPE_TRANSFER_WRITE
) ? TRUE
: FALSE
);
688 return map
+ offset
+
689 box
->y
/ util_format_get_blockheight(format
) * transfer
->stride
+
690 box
->x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
694 i915_texture_transfer_unmap(struct pipe_context
*pipe
,
695 struct pipe_transfer
*transfer
)
697 struct i915_texture
*tex
= i915_texture(transfer
->resource
);
698 struct i915_winsys
*iws
= i915_screen(tex
->b
.b
.screen
)->iws
;
699 iws
->buffer_unmap(iws
, tex
->buffer
);
704 struct u_resource_vtbl i915_texture_vtbl
=
706 i915_texture_get_handle
, /* get_handle */
707 i915_texture_destroy
, /* resource_destroy */
708 NULL
, /* is_resource_referenced */
709 i915_texture_get_transfer
, /* get_transfer */
710 u_default_transfer_destroy
, /* transfer_destroy */
711 i915_texture_transfer_map
, /* transfer_map */
712 u_default_transfer_flush_region
, /* transfer_flush_region */
713 i915_texture_transfer_unmap
, /* transfer_unmap */
714 u_default_transfer_inline_write
/* transfer_inline_write */
720 struct pipe_resource
*
721 i915_texture_create(struct pipe_screen
*screen
,
722 const struct pipe_resource
*template)
724 struct i915_screen
*is
= i915_screen(screen
);
725 struct i915_winsys
*iws
= is
->iws
;
726 struct i915_texture
*tex
= CALLOC_STRUCT(i915_texture
);
728 unsigned buf_usage
= 0;
733 tex
->b
.b
= *template;
734 tex
->b
.vtbl
= &i915_texture_vtbl
;
735 pipe_reference_init(&tex
->b
.b
.reference
, 1);
736 tex
->b
.b
.screen
= screen
;
739 if (!i945_texture_layout(tex
))
742 if (!i915_texture_layout(tex
))
746 tex_size
= tex
->stride
* tex
->total_nblocksy
;
748 /* for scanouts and cursors, cursors arn't scanouts */
750 /* XXX: use a custom flag for cursors, don't rely on magically
751 * guessing that this is Xorg asking for a cursor
753 if ((template->bind
& PIPE_BIND_SCANOUT
) && template->width0
!= 64)
754 buf_usage
= I915_NEW_SCANOUT
;
756 buf_usage
= I915_NEW_TEXTURE
;
758 tex
->buffer
= iws
->buffer_create(iws
, tex_size
, 64, buf_usage
);
762 /* setup any hw fences */
764 assert(tex
->sw_tiled
== I915_TILE_NONE
);
765 iws
->buffer_set_fence_reg(iws
, tex
->buffer
, tex
->stride
, tex
->hw_tiled
);
770 void *ptr
= ws
->buffer_map(ws
, tex
->buffer
,
771 PIPE_BUFFER_USAGE_CPU_WRITE
);
772 memset(ptr
, 0x80, tex_size
);
773 ws
->buffer_unmap(ws
, tex
->buffer
);
783 struct pipe_resource
*
784 i915_texture_from_handle(struct pipe_screen
* screen
,
785 const struct pipe_resource
*template,
786 struct winsys_handle
*whandle
)
788 struct i915_screen
*is
= i915_screen(screen
);
789 struct i915_texture
*tex
;
790 struct i915_winsys
*iws
= is
->iws
;
791 struct i915_winsys_buffer
*buffer
;
796 buffer
= iws
->buffer_from_handle(iws
, whandle
, &stride
);
798 /* Only supports one type */
799 if (template->target
!= PIPE_TEXTURE_2D
||
800 template->last_level
!= 0 ||
801 template->depth0
!= 1) {
805 tex
= CALLOC_STRUCT(i915_texture
);
809 tex
->b
.b
= *template;
810 tex
->b
.vtbl
= &i915_texture_vtbl
;
811 pipe_reference_init(&tex
->b
.b
.reference
, 1);
812 tex
->b
.b
.screen
= screen
;
814 tex
->stride
= stride
;
816 i915_texture_set_level_info(tex
, 0, 1);
817 i915_texture_set_image_offset(tex
, 0, 0, 0, 0);
819 tex
->buffer
= buffer
;