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"
40 #include "util/u_rect.h"
42 #include "i915_context.h"
43 #include "i915_resource.h"
44 #include "i915_screen.h"
45 #include "i915_winsys.h"
46 #include "i915_debug.h"
49 #define DEBUG_TEXTURES 0
52 * Helper function and arrays
57 * Initial offset for Cube map.
59 static const int initial_offsets
[6][2] = {
60 [PIPE_TEX_FACE_POS_X
] = {0, 0},
61 [PIPE_TEX_FACE_POS_Y
] = {1, 0},
62 [PIPE_TEX_FACE_POS_Z
] = {1, 1},
63 [PIPE_TEX_FACE_NEG_X
] = {0, 2},
64 [PIPE_TEX_FACE_NEG_Y
] = {1, 2},
65 [PIPE_TEX_FACE_NEG_Z
] = {1, 3},
69 * Step offsets for Cube map.
71 static const int step_offsets
[6][2] = {
72 [PIPE_TEX_FACE_POS_X
] = { 0, 2},
73 [PIPE_TEX_FACE_POS_Y
] = {-1, 2},
74 [PIPE_TEX_FACE_POS_Z
] = {-1, 1},
75 [PIPE_TEX_FACE_NEG_X
] = { 0, 2},
76 [PIPE_TEX_FACE_NEG_Y
] = {-1, 2},
77 [PIPE_TEX_FACE_NEG_Z
] = {-1, 1},
81 * For compressed level 2
83 static const int bottom_offsets
[6] = {
84 [PIPE_TEX_FACE_POS_X
] = 16 + 0 * 8,
85 [PIPE_TEX_FACE_POS_Y
] = 16 + 1 * 8,
86 [PIPE_TEX_FACE_POS_Z
] = 16 + 2 * 8,
87 [PIPE_TEX_FACE_NEG_X
] = 16 + 3 * 8,
88 [PIPE_TEX_FACE_NEG_Y
] = 16 + 4 * 8,
89 [PIPE_TEX_FACE_NEG_Z
] = 16 + 5 * 8,
92 static INLINE
unsigned
93 align_nblocksx(enum pipe_format format
, unsigned width
, unsigned align_to
)
95 return align(util_format_get_nblocksx(format
, width
), align_to
);
98 static INLINE
unsigned
99 align_nblocksy(enum pipe_format format
, unsigned width
, unsigned align_to
)
101 return align(util_format_get_nblocksy(format
, width
), align_to
);
104 static INLINE
unsigned
105 get_pot_stride(enum pipe_format format
, unsigned width
)
107 return util_next_power_of_two(util_format_get_stride(format
, width
));
110 static INLINE
const char*
111 get_tiling_string(enum i915_winsys_buffer_tile tile
)
128 * More advanced helper funcs
133 i915_texture_set_level_info(struct i915_texture
*tex
,
134 unsigned level
, unsigned nr_images
)
136 assert(level
< Elements(tex
->nr_images
));
138 assert(!tex
->image_offset
[level
]);
140 tex
->nr_images
[level
] = nr_images
;
141 tex
->image_offset
[level
] = MALLOC(nr_images
* sizeof(struct offset_pair
));
142 tex
->image_offset
[level
][0].nblocksx
= 0;
143 tex
->image_offset
[level
][0].nblocksy
= 0;
146 INLINE
unsigned i915_texture_offset(struct i915_texture
*tex
,
147 unsigned level
, unsigned layer
)
150 x
= tex
->image_offset
[level
][layer
].nblocksx
151 * util_format_get_blocksize(tex
->b
.b
.format
);
152 y
= tex
->image_offset
[level
][layer
].nblocksy
;
154 return y
* tex
->stride
+ x
;
158 i915_texture_set_image_offset(struct i915_texture
*tex
,
159 unsigned level
, unsigned img
,
160 unsigned nblocksx
, unsigned nblocksy
)
162 /* for the first image and level make sure offset is zero */
163 assert(!(img
== 0 && level
== 0) || (nblocksx
== 0 && nblocksy
== 0));
164 assert(img
< tex
->nr_images
[level
]);
166 tex
->image_offset
[level
][img
].nblocksx
= nblocksx
;
167 tex
->image_offset
[level
][img
].nblocksy
= nblocksy
;
170 debug_printf("%s: %p level %u, img %u (%u, %u)\n", __FUNCTION__
,
171 tex
, level
, img
, x
, y
);
175 static enum i915_winsys_buffer_tile
176 i915_texture_tiling(struct i915_screen
*is
, struct i915_texture
*tex
)
178 if (!is
->debug
.tiling
)
179 return I915_TILE_NONE
;
181 if (tex
->b
.b
.target
== PIPE_TEXTURE_1D
)
182 return I915_TILE_NONE
;
184 /* Use X tiling for 2D, 3D and compressed textures */
190 * Shared layout functions
195 * Special case to deal with scanout textures.
198 i9x5_scanout_layout(struct i915_texture
*tex
)
200 struct pipe_resource
*pt
= &tex
->b
.b
;
202 if (pt
->last_level
> 0 || util_format_get_blocksize(pt
->format
) != 4)
205 i915_texture_set_level_info(tex
, 0, 1);
206 i915_texture_set_image_offset(tex
, 0, 0, 0, 0);
208 if (pt
->width0
>= 240) {
209 tex
->stride
= align(util_format_get_stride(pt
->format
, pt
->width0
), 64);
210 tex
->total_nblocksy
= align_nblocksy(pt
->format
, pt
->height0
, 8);
211 tex
->tiling
= I915_TILE_X
;
212 /* special case for cursors */
213 } else if (pt
->width0
== 64 && pt
->height0
== 64) {
214 tex
->stride
= get_pot_stride(pt
->format
, pt
->width0
);
215 tex
->total_nblocksy
= align_nblocksy(pt
->format
, pt
->height0
, 8);
221 debug_printf("%s size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__
,
222 pt
->width0
, pt
->height0
, util_format_get_blocksize(pt
->format
),
223 tex
->stride
, tex
->total_nblocksy
, tex
->stride
* tex
->total_nblocksy
);
230 * Special case to deal with shared textures.
233 i9x5_display_target_layout(struct i915_texture
*tex
)
235 struct pipe_resource
*pt
= &tex
->b
.b
;
237 if (pt
->last_level
> 0 || util_format_get_blocksize(pt
->format
) != 4)
240 /* fallback to normal textures for small textures */
241 if (pt
->width0
< 240)
244 i915_texture_set_level_info(tex
, 0, 1);
245 i915_texture_set_image_offset(tex
, 0, 0, 0, 0);
247 tex
->stride
= align(util_format_get_stride(pt
->format
, pt
->width0
), 64);
248 tex
->total_nblocksy
= align_nblocksy(pt
->format
, pt
->height0
, 8);
249 tex
->tiling
= I915_TILE_X
;
252 debug_printf("%s size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__
,
253 pt
->width0
, pt
->height0
, util_format_get_blocksize(pt
->format
),
254 tex
->stride
, tex
->total_nblocksy
, tex
->stride
* tex
->total_nblocksy
);
261 * Helper function for special layouts
264 i9x5_special_layout(struct i915_texture
*tex
)
266 struct pipe_resource
*pt
= &tex
->b
.b
;
268 /* Scanouts needs special care */
269 if (pt
->bind
& PIPE_BIND_SCANOUT
)
270 if (i9x5_scanout_layout(tex
))
273 /* Shared buffers needs to be compatible with X servers
275 * XXX: need a better name than shared for this if it is to be part
276 * of core gallium, and probably move the flag to resource.flags,
277 * rather than bindings.
279 if (pt
->bind
& (PIPE_BIND_SHARED
| PIPE_BIND_DISPLAY_TARGET
))
280 if (i9x5_display_target_layout(tex
))
287 * Cube layout used on i915 and for non-compressed textures on i945.
290 i9x5_texture_layout_cube(struct i915_texture
*tex
)
292 struct pipe_resource
*pt
= &tex
->b
.b
;
293 const unsigned nblocks
= util_format_get_nblocksx(pt
->format
, pt
->width0
);
297 assert(pt
->width0
== pt
->height0
); /* cubemap images are square */
299 /* double pitch for cube layouts */
300 tex
->stride
= align(nblocks
* util_format_get_blocksize(pt
->format
) * 2, 4);
301 tex
->total_nblocksy
= nblocks
* 4;
303 for (level
= 0; level
<= pt
->last_level
; level
++)
304 i915_texture_set_level_info(tex
, level
, 6);
306 for (face
= 0; face
< 6; face
++) {
307 unsigned x
= initial_offsets
[face
][0] * nblocks
;
308 unsigned y
= initial_offsets
[face
][1] * nblocks
;
309 unsigned d
= nblocks
;
311 for (level
= 0; level
<= pt
->last_level
; level
++) {
312 i915_texture_set_image_offset(tex
, level
, face
, x
, y
);
314 x
+= step_offsets
[face
][0] * d
;
315 y
+= step_offsets
[face
][1] * d
;
322 * i915 layout functions
327 i915_texture_layout_2d(struct i915_texture
*tex
)
329 struct pipe_resource
*pt
= &tex
->b
.b
;
331 unsigned width
= pt
->width0
;
332 unsigned height
= pt
->height0
;
333 unsigned nblocksy
= util_format_get_nblocksy(pt
->format
, pt
->width0
);
334 unsigned align_y
= 2;
336 if (util_format_is_s3tc(pt
->format
))
339 tex
->stride
= align(util_format_get_stride(pt
->format
, pt
->width0
), 4);
340 tex
->total_nblocksy
= 0;
342 for (level
= 0; level
<= pt
->last_level
; level
++) {
343 i915_texture_set_level_info(tex
, level
, 1);
344 i915_texture_set_image_offset(tex
, level
, 0, 0, tex
->total_nblocksy
);
346 tex
->total_nblocksy
+= nblocksy
;
348 width
= u_minify(width
, 1);
349 height
= u_minify(height
, 1);
350 nblocksy
= align_nblocksy(pt
->format
, height
, align_y
);
355 i915_texture_layout_3d(struct i915_texture
*tex
)
357 struct pipe_resource
*pt
= &tex
->b
.b
;
360 unsigned width
= pt
->width0
;
361 unsigned height
= pt
->height0
;
362 unsigned depth
= pt
->depth0
;
363 unsigned nblocksy
= util_format_get_nblocksy(pt
->format
, pt
->height0
);
364 unsigned stack_nblocksy
= 0;
366 /* Calculate the size of a single slice.
368 tex
->stride
= align(util_format_get_stride(pt
->format
, pt
->width0
), 4);
370 /* XXX: hardware expects/requires 9 levels at minimum.
372 for (level
= 0; level
<= MAX2(8, pt
->last_level
); level
++) {
373 i915_texture_set_level_info(tex
, level
, depth
);
375 stack_nblocksy
+= MAX2(2, nblocksy
);
377 width
= u_minify(width
, 1);
378 height
= u_minify(height
, 1);
379 nblocksy
= util_format_get_nblocksy(pt
->format
, height
);
382 /* Fixup depth image_offsets:
384 for (level
= 0; level
<= pt
->last_level
; level
++) {
386 for (i
= 0; i
< depth
; i
++)
387 i915_texture_set_image_offset(tex
, level
, i
, 0, i
* stack_nblocksy
);
389 depth
= u_minify(depth
, 1);
392 /* Multiply slice size by texture depth for total size. It's
393 * remarkable how wasteful of memory the i915 texture layouts
394 * are. They are largely fixed in the i945.
396 tex
->total_nblocksy
= stack_nblocksy
* pt
->depth0
;
400 i915_texture_layout(struct i915_texture
* tex
)
402 switch (tex
->b
.b
.target
) {
403 case PIPE_TEXTURE_1D
:
404 case PIPE_TEXTURE_2D
:
405 case PIPE_TEXTURE_RECT
:
406 if (!i9x5_special_layout(tex
))
407 i915_texture_layout_2d(tex
);
409 case PIPE_TEXTURE_3D
:
410 i915_texture_layout_3d(tex
);
412 case PIPE_TEXTURE_CUBE
:
413 i9x5_texture_layout_cube(tex
);
425 * i945 layout functions
430 i945_texture_layout_2d(struct i915_texture
*tex
)
432 struct pipe_resource
*pt
= &tex
->b
.b
;
433 int align_x
= 4, align_y
= 2;
437 unsigned width
= pt
->width0
;
438 unsigned height
= pt
->height0
;
439 unsigned nblocksx
= util_format_get_nblocksx(pt
->format
, pt
->width0
);
440 unsigned nblocksy
= util_format_get_nblocksy(pt
->format
, pt
->height0
);
442 if (util_format_is_s3tc(pt
->format
)) {
447 tex
->stride
= align(util_format_get_stride(pt
->format
, pt
->width0
), 4);
449 /* May need to adjust pitch to accomodate the placement of
450 * the 2nd mipmap level. This occurs when the alignment
451 * constraints of mipmap placement push the right edge of the
452 * 2nd mipmap level out past the width of its parent.
454 if (pt
->last_level
> 0) {
455 unsigned mip1_nblocksx
=
456 align_nblocksx(pt
->format
, u_minify(pt
->width0
, 1), align_x
) +
457 util_format_get_nblocksx(pt
->format
, u_minify(pt
->width0
, 2));
459 if (mip1_nblocksx
> nblocksx
)
460 tex
->stride
= mip1_nblocksx
* util_format_get_blocksize(pt
->format
);
463 /* Pitch must be a whole number of dwords
465 tex
->stride
= align(tex
->stride
, 64);
466 tex
->total_nblocksy
= 0;
468 for (level
= 0; level
<= pt
->last_level
; level
++) {
469 i915_texture_set_level_info(tex
, level
, 1);
470 i915_texture_set_image_offset(tex
, level
, 0, x
, y
);
472 /* Because the images are packed better, the final offset
473 * might not be the maximal one:
475 tex
->total_nblocksy
= MAX2(tex
->total_nblocksy
, y
+ nblocksy
);
477 /* Layout_below: step right after second mipmap level.
485 width
= u_minify(width
, 1);
486 height
= u_minify(height
, 1);
487 nblocksx
= align_nblocksx(pt
->format
, width
, align_x
);
488 nblocksy
= align_nblocksy(pt
->format
, height
, align_y
);
493 i945_texture_layout_3d(struct i915_texture
*tex
)
495 struct pipe_resource
*pt
= &tex
->b
.b
;
496 unsigned width
= pt
->width0
;
497 unsigned height
= pt
->height0
;
498 unsigned depth
= pt
->depth0
;
499 unsigned nblocksy
= util_format_get_nblocksy(pt
->format
, pt
->width0
);
500 unsigned pack_x_pitch
, pack_x_nr
;
501 unsigned pack_y_pitch
;
504 tex
->stride
= align(util_format_get_stride(pt
->format
, pt
->width0
), 4);
505 tex
->total_nblocksy
= 0;
507 pack_y_pitch
= MAX2(nblocksy
, 2);
508 pack_x_pitch
= tex
->stride
/ util_format_get_blocksize(pt
->format
);
511 for (level
= 0; level
<= pt
->last_level
; level
++) {
516 i915_texture_set_level_info(tex
, level
, depth
);
518 for (q
= 0; q
< depth
;) {
519 for (j
= 0; j
< pack_x_nr
&& q
< depth
; j
++, q
++) {
520 i915_texture_set_image_offset(tex
, level
, q
, x
, y
+ tex
->total_nblocksy
);
528 tex
->total_nblocksy
+= y
;
530 if (pack_x_pitch
> 4) {
533 assert(pack_x_pitch
* pack_x_nr
* util_format_get_blocksize(pt
->format
) <= tex
->stride
);
536 if (pack_y_pitch
> 2) {
540 width
= u_minify(width
, 1);
541 height
= u_minify(height
, 1);
542 depth
= u_minify(depth
, 1);
543 nblocksy
= util_format_get_nblocksy(pt
->format
, height
);
548 i945_texture_layout_cube(struct i915_texture
*tex
)
550 struct pipe_resource
*pt
= &tex
->b
.b
;
551 const unsigned nblocks
= util_format_get_nblocksx(pt
->format
, pt
->width0
);
552 const unsigned dim
= pt
->width0
;
556 assert(pt
->width0
== pt
->height0
); /* cubemap images are square */
557 assert(util_next_power_of_two(pt
->width0
) == pt
->width0
); /* npot only */
558 assert(util_format_is_s3tc(pt
->format
)); /* compressed only */
561 * Depending on the size of the largest images, pitch can be
562 * determined either by the old-style packing of cubemap faces,
563 * or the final row of 4x4, 2x2 and 1x1 faces below this.
568 if (pt
->width0
>= 64)
569 tex
->stride
= nblocks
* 2 * util_format_get_blocksize(pt
->format
);
571 tex
->stride
= 14 * 2 * util_format_get_blocksize(pt
->format
);
574 * Something similary apply for height as well.
577 tex
->total_nblocksy
= nblocks
* 4 + 1;
579 tex
->total_nblocksy
= 1;
581 /* Set all the levels to effectively occupy the whole rectangular region */
582 for (level
= 0; level
<= pt
->last_level
; level
++)
583 i915_texture_set_level_info(tex
, level
, 6);
585 for (face
= 0; face
< 6; face
++) {
586 /* all calculations in pixels */
587 unsigned total_height
= tex
->total_nblocksy
* 4;
588 unsigned x
= initial_offsets
[face
][0] * dim
;
589 unsigned y
= initial_offsets
[face
][1] * dim
;
592 if (dim
== 4 && face
>= 4) {
594 y
= tex
->total_nblocksy
* 4 - 4; /* 4 = 1 block */
595 } else if (dim
< 4 && (face
> 0)) {
597 y
= total_height
- 4;
600 for (level
= 0; level
<= pt
->last_level
; level
++) {
601 i915_texture_set_image_offset(tex
, level
, face
,
602 util_format_get_nblocksx(pt
->format
, x
),
603 util_format_get_nblocksy(pt
->format
, y
));
610 case PIPE_TEX_FACE_POS_X
:
611 case PIPE_TEX_FACE_NEG_X
:
612 x
+= step_offsets
[face
][0] * d
;
613 y
+= step_offsets
[face
][1] * d
;
615 case PIPE_TEX_FACE_POS_Y
:
616 case PIPE_TEX_FACE_NEG_Y
:
620 case PIPE_TEX_FACE_POS_Z
:
621 case PIPE_TEX_FACE_NEG_Z
:
622 y
= total_height
- 4;
628 y
= total_height
- 4;
629 x
= bottom_offsets
[face
];
635 x
+= step_offsets
[face
][0] * d
;
636 y
+= step_offsets
[face
][1] * d
;
644 i945_texture_layout(struct i915_texture
* tex
)
646 switch (tex
->b
.b
.target
) {
647 case PIPE_TEXTURE_1D
:
648 case PIPE_TEXTURE_2D
:
649 case PIPE_TEXTURE_RECT
:
650 if (!i9x5_special_layout(tex
))
651 i945_texture_layout_2d(tex
);
653 case PIPE_TEXTURE_3D
:
654 i945_texture_layout_3d(tex
);
656 case PIPE_TEXTURE_CUBE
:
657 if (!util_format_is_s3tc(tex
->b
.b
.format
))
658 i9x5_texture_layout_cube(tex
);
660 i945_texture_layout_cube(tex
);
673 * Screen texture functions
679 i915_texture_get_handle(struct pipe_screen
* screen
,
680 struct pipe_resource
*texture
,
681 struct winsys_handle
*whandle
)
683 struct i915_screen
*is
= i915_screen(screen
);
684 struct i915_texture
*tex
= i915_texture(texture
);
685 struct i915_winsys
*iws
= is
->iws
;
687 return iws
->buffer_get_handle(iws
, tex
->buffer
, whandle
, tex
->stride
);
692 i915_texture_destroy(struct pipe_screen
*screen
,
693 struct pipe_resource
*pt
)
695 struct i915_texture
*tex
= i915_texture(pt
);
696 struct i915_winsys
*iws
= i915_screen(screen
)->iws
;
700 iws
->buffer_destroy(iws
, tex
->buffer
);
702 for (i
= 0; i
< Elements(tex
->image_offset
); i
++)
703 FREE(tex
->image_offset
[i
]);
709 i915_texture_transfer_map(struct pipe_context
*pipe
,
710 struct pipe_resource
*resource
,
713 const struct pipe_box
*box
,
714 struct pipe_transfer
**ptransfer
)
716 struct i915_context
*i915
= i915_context(pipe
);
717 struct i915_texture
*tex
= i915_texture(resource
);
718 struct i915_transfer
*transfer
= util_slab_alloc(&i915
->texture_transfer_pool
);
719 boolean use_staging_texture
= FALSE
;
720 struct i915_winsys
*iws
= i915_screen(pipe
->screen
)->iws
;
721 enum pipe_format format
= resource
->format
;
725 if (transfer
== NULL
)
728 transfer
->b
.resource
= resource
;
729 transfer
->b
.level
= level
;
730 transfer
->b
.usage
= usage
;
731 transfer
->b
.box
= *box
;
732 transfer
->b
.stride
= tex
->stride
;
733 transfer
->staging_texture
= NULL
;
734 /* XXX: handle depth textures everyhwere*/
735 transfer
->b
.layer_stride
= 0;
737 /* if we use staging transfers, only support textures we can render to,
738 * because we need that for u_blitter */
740 util_blitter_is_copy_supported(i915
->blitter
, resource
, resource
,
742 (usage
& PIPE_TRANSFER_WRITE
) &&
743 !(usage
& (PIPE_TRANSFER_READ
| PIPE_TRANSFER_DONTBLOCK
| PIPE_TRANSFER_UNSYNCHRONIZED
)))
744 use_staging_texture
= TRUE
;
746 use_staging_texture
= FALSE
;
748 if (use_staging_texture
) {
750 * Allocate the untiled staging texture.
751 * If the alloc fails, transfer->staging_texture is NULL and we fallback to a map()
753 transfer
->staging_texture
= i915_texture_create(pipe
->screen
, resource
, TRUE
);
756 if (resource
->target
!= PIPE_TEXTURE_3D
&&
757 resource
->target
!= PIPE_TEXTURE_CUBE
)
760 if (transfer
->staging_texture
) {
761 tex
= i915_texture(transfer
->staging_texture
);
763 /* TODO this is a sledgehammer */
764 tex
= i915_texture(resource
);
765 pipe
->flush(pipe
, NULL
, 0);
768 offset
= i915_texture_offset(tex
, transfer
->b
.level
, box
->z
);
770 map
= iws
->buffer_map(iws
, tex
->buffer
,
771 (transfer
->b
.usage
& PIPE_TRANSFER_WRITE
) ? TRUE
: FALSE
);
773 pipe_resource_reference(&transfer
->staging_texture
, NULL
);
778 *ptransfer
= &transfer
->b
;
780 return map
+ offset
+
781 box
->y
/ util_format_get_blockheight(format
) * transfer
->b
.stride
+
782 box
->x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
786 i915_texture_transfer_unmap(struct pipe_context
*pipe
,
787 struct pipe_transfer
*transfer
)
789 struct i915_context
*i915
= i915_context(pipe
);
790 struct i915_transfer
*itransfer
= (struct i915_transfer
*)transfer
;
791 struct i915_texture
*tex
= i915_texture(itransfer
->b
.resource
);
792 struct i915_winsys
*iws
= i915_screen(tex
->b
.b
.screen
)->iws
;
794 if (itransfer
->staging_texture
)
795 tex
= i915_texture(itransfer
->staging_texture
);
797 iws
->buffer_unmap(iws
, tex
->buffer
);
799 if ((itransfer
->staging_texture
) &&
800 (transfer
->usage
& PIPE_TRANSFER_WRITE
)) {
801 struct pipe_box sbox
;
803 u_box_origin_2d(itransfer
->b
.box
.width
, itransfer
->b
.box
.height
, &sbox
);
804 pipe
->resource_copy_region(pipe
, itransfer
->b
.resource
, itransfer
->b
.level
,
805 itransfer
->b
.box
.x
, itransfer
->b
.box
.y
, itransfer
->b
.box
.z
,
806 itransfer
->staging_texture
,
808 pipe
->flush(pipe
, NULL
, 0);
809 pipe_resource_reference(&itransfer
->staging_texture
, NULL
);
812 util_slab_free(&i915
->texture_transfer_pool
, itransfer
);
816 static void i915_transfer_inline_write( struct pipe_context
*pipe
,
817 struct pipe_resource
*resource
,
820 const struct pipe_box
*box
,
823 unsigned layer_stride
)
825 struct pipe_transfer
*transfer
= NULL
;
826 struct i915_transfer
*itransfer
= NULL
;
827 const uint8_t *src_data
= data
;
830 transfer
= pipe
->transfer_get(pipe
,
835 if (transfer
== NULL
)
838 itransfer
= (struct i915_transfer
*)transfer
;
840 if (itransfer
->staging_texture
) {
841 struct i915_texture
*tex
= i915_texture(itransfer
->staging_texture
);
842 enum pipe_format format
= tex
->b
.b
.format
;
843 struct i915_winsys
*iws
= i915_screen(tex
->b
.b
.screen
)->iws
;
847 offset
= i915_texture_offset(tex
, transfer
->level
, transfer
->box
.z
);
849 for (i
= 0; i
< box
->depth
; i
++) {
850 if (!tex
->b
.b
.last_level
&&
851 tex
->b
.b
.width0
== transfer
->box
.width
) {
852 unsigned nby
= util_format_get_nblocksy(format
, transfer
->box
.y
);
854 assert(!transfer
->box
.x
);
855 assert(tex
->stride
== transfer
->stride
);
857 offset
+= tex
->stride
* nby
;
858 size
= util_format_get_2d_size(format
, transfer
->stride
,
859 transfer
->box
.height
);
860 iws
->buffer_write(iws
, tex
->buffer
, offset
, size
, transfer
->data
);
863 unsigned nby
= util_format_get_nblocksy(format
, transfer
->box
.y
);
865 offset
+= util_format_get_stride(format
, transfer
->box
.x
);
866 size
= transfer
->stride
;
868 for (i
= 0; i
< nby
; i
++) {
869 iws
->buffer_write(iws
, tex
->buffer
, offset
, size
, transfer
->data
);
870 offset
+= tex
->stride
;
873 offset
+= layer_stride
;
876 uint8_t *map
= pipe_transfer_map(pipe
, &itransfer
->b
);
880 for (i
= 0; i
< box
->depth
; i
++) {
883 itransfer
->b
.stride
, /* bytes */
890 map
+= itransfer
->b
.layer_stride
;
891 src_data
+= layer_stride
;
895 pipe_transfer_unmap(pipe
, &itransfer
->b
);
900 pipe_transfer_destroy(pipe
, &itransfer
->b
);
904 struct u_resource_vtbl i915_texture_vtbl
=
906 i915_texture_get_handle
, /* get_handle */
907 i915_texture_destroy
, /* resource_destroy */
908 i915_texture_transfer_map
, /* transfer_map */
909 u_default_transfer_flush_region
, /* transfer_flush_region */
910 i915_texture_transfer_unmap
, /* transfer_unmap */
911 u_default_transfer_inline_write
/* transfer_inline_write */
915 struct pipe_resource
*
916 i915_texture_create(struct pipe_screen
*screen
,
917 const struct pipe_resource
*template,
918 boolean force_untiled
)
920 struct i915_screen
*is
= i915_screen(screen
);
921 struct i915_winsys
*iws
= is
->iws
;
922 struct i915_texture
*tex
= CALLOC_STRUCT(i915_texture
);
923 unsigned buf_usage
= 0;
928 tex
->b
.b
= *template;
929 tex
->b
.vtbl
= &i915_texture_vtbl
;
930 pipe_reference_init(&tex
->b
.b
.reference
, 1);
931 tex
->b
.b
.screen
= screen
;
933 if ( (force_untiled
) || (template->usage
== PIPE_USAGE_STREAM
) )
934 tex
->tiling
= I915_TILE_NONE
;
936 tex
->tiling
= i915_texture_tiling(is
, tex
);
939 if (!i945_texture_layout(tex
))
942 if (!i915_texture_layout(tex
))
946 /* for scanouts and cursors, cursors arn't scanouts */
948 /* XXX: use a custom flag for cursors, don't rely on magically
949 * guessing that this is Xorg asking for a cursor
951 if ((template->bind
& PIPE_BIND_SCANOUT
) && template->width0
!= 64)
952 buf_usage
= I915_NEW_SCANOUT
;
954 buf_usage
= I915_NEW_TEXTURE
;
956 tex
->buffer
= iws
->buffer_create_tiled(iws
, &tex
->stride
, tex
->total_nblocksy
,
957 &tex
->tiling
, buf_usage
);
961 I915_DBG(DBG_TEXTURE
, "%s: %p stride %u, blocks (%u, %u) tiling %s\n", __func__
,
963 tex
->stride
/ util_format_get_blocksize(tex
->b
.b
.format
),
964 tex
->total_nblocksy
, get_tiling_string(tex
->tiling
));
973 struct pipe_resource
*
974 i915_texture_from_handle(struct pipe_screen
* screen
,
975 const struct pipe_resource
*template,
976 struct winsys_handle
*whandle
)
978 struct i915_screen
*is
= i915_screen(screen
);
979 struct i915_texture
*tex
;
980 struct i915_winsys
*iws
= is
->iws
;
981 struct i915_winsys_buffer
*buffer
;
983 enum i915_winsys_buffer_tile tiling
;
987 buffer
= iws
->buffer_from_handle(iws
, whandle
, &tiling
, &stride
);
989 /* Only supports one type */
990 if ((template->target
!= PIPE_TEXTURE_2D
&&
991 template->target
!= PIPE_TEXTURE_RECT
) ||
992 template->last_level
!= 0 ||
993 template->depth0
!= 1) {
997 tex
= CALLOC_STRUCT(i915_texture
);
1001 tex
->b
.b
= *template;
1002 tex
->b
.vtbl
= &i915_texture_vtbl
;
1003 pipe_reference_init(&tex
->b
.b
.reference
, 1);
1004 tex
->b
.b
.screen
= screen
;
1006 tex
->stride
= stride
;
1007 tex
->tiling
= tiling
;
1008 tex
->total_nblocksy
= align_nblocksy(tex
->b
.b
.format
, tex
->b
.b
.height0
, 8);
1010 i915_texture_set_level_info(tex
, 0, 1);
1011 i915_texture_set_image_offset(tex
, 0, 0, 0, 0);
1013 tex
->buffer
= buffer
;
1015 I915_DBG(DBG_TEXTURE
, "%s: %p stride %u, blocks (%u, %u) tiling %s\n", __func__
,
1017 tex
->stride
/ util_format_get_blocksize(tex
->b
.b
.format
),
1018 tex
->total_nblocksy
, get_tiling_string(tex
->tiling
));