1 /**************************************************************************
3 * Copyright 2009 VMware, Inc. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 **************************************************************************/
29 * Surface utility functions.
35 #include "pipe/p_defines.h"
36 #include "pipe/p_screen.h"
37 #include "pipe/p_state.h"
39 #include "util/format/u_format.h"
40 #include "util/u_inlines.h"
41 #include "util/u_rect.h"
42 #include "util/u_surface.h"
43 #include "util/u_pack_color.h"
47 * Initialize a pipe_surface object. 'view' is considered to have
48 * uninitialized contents.
51 u_surface_default_template(struct pipe_surface
*surf
,
52 const struct pipe_resource
*texture
)
54 memset(surf
, 0, sizeof(*surf
));
56 surf
->format
= texture
->format
;
61 * Copy 3D box from one place to another.
62 * Position and sizes are in pixels.
65 util_copy_box(ubyte
* dst
,
66 enum pipe_format format
,
67 unsigned dst_stride
, unsigned dst_slice_stride
,
68 unsigned dst_x
, unsigned dst_y
, unsigned dst_z
,
69 unsigned width
, unsigned height
, unsigned depth
,
71 int src_stride
, unsigned src_slice_stride
,
72 unsigned src_x
, unsigned src_y
, unsigned src_z
)
75 dst
+= dst_z
* dst_slice_stride
;
76 src
+= src_z
* src_slice_stride
;
77 for (z
= 0; z
< depth
; ++z
) {
87 dst
+= dst_slice_stride
;
88 src
+= src_slice_stride
;
94 util_fill_rect(ubyte
* dst
,
95 enum pipe_format format
,
101 union util_color
*uc
)
103 const struct util_format_description
*desc
= util_format_description(format
);
106 int blocksize
= desc
->block
.bits
/ 8;
107 int blockwidth
= desc
->block
.width
;
108 int blockheight
= desc
->block
.height
;
110 assert(blocksize
> 0);
111 assert(blockwidth
> 0);
112 assert(blockheight
> 0);
115 dst_y
/= blockheight
;
116 width
= (width
+ blockwidth
- 1)/blockwidth
;
117 height
= (height
+ blockheight
- 1)/blockheight
;
119 dst
+= dst_x
* blocksize
;
120 dst
+= dst_y
* dst_stride
;
121 width_size
= width
* blocksize
;
125 if(dst_stride
== width_size
)
126 memset(dst
, uc
->ub
, height
* width_size
);
128 for (i
= 0; i
< height
; i
++) {
129 memset(dst
, uc
->ub
, width_size
);
135 for (i
= 0; i
< height
; i
++) {
136 uint16_t *row
= (uint16_t *)dst
;
137 for (j
= 0; j
< width
; j
++)
143 for (i
= 0; i
< height
; i
++) {
144 uint32_t *row
= (uint32_t *)dst
;
145 for (j
= 0; j
< width
; j
++)
151 for (i
= 0; i
< height
; i
++) {
153 for (j
= 0; j
< width
; j
++) {
154 memcpy(row
, uc
, blocksize
);
165 util_fill_box(ubyte
* dst
,
166 enum pipe_format format
,
168 unsigned layer_stride
,
175 union util_color
*uc
)
178 dst
+= z
* layer_stride
;
179 for (layer
= z
; layer
< depth
; layer
++) {
180 util_fill_rect(dst
, format
,
182 x
, y
, width
, height
, uc
);
189 * Fallback function for pipe->resource_copy_region().
190 * We support copying between different formats (including compressed/
191 * uncompressed) if the bytes per block or pixel matches. If copying
192 * compressed -> uncompressed, the dst region is reduced by the block
193 * width, height. If copying uncompressed -> compressed, the dest region
194 * is expanded by the block width, height. See GL_ARB_copy_image.
195 * Note: (X,Y)=(0,0) is always the upper-left corner.
198 util_resource_copy_region(struct pipe_context
*pipe
,
199 struct pipe_resource
*dst
,
201 unsigned dst_x
, unsigned dst_y
, unsigned dst_z
,
202 struct pipe_resource
*src
,
204 const struct pipe_box
*src_box_in
)
206 struct pipe_transfer
*src_trans
, *dst_trans
;
208 const uint8_t *src_map
;
209 enum pipe_format src_format
;
210 enum pipe_format dst_format
;
211 struct pipe_box src_box
, dst_box
;
212 unsigned src_bs
, dst_bs
, src_bw
, dst_bw
, src_bh
, dst_bh
;
218 assert((src
->target
== PIPE_BUFFER
&& dst
->target
== PIPE_BUFFER
) ||
219 (src
->target
!= PIPE_BUFFER
&& dst
->target
!= PIPE_BUFFER
));
221 src_format
= src
->format
;
222 dst_format
= dst
->format
;
225 src_box
= *src_box_in
;
231 dst_box
.width
= src_box
.width
;
232 dst_box
.height
= src_box
.height
;
233 dst_box
.depth
= src_box
.depth
;
235 src_bs
= util_format_get_blocksize(src_format
);
236 src_bw
= util_format_get_blockwidth(src_format
);
237 src_bh
= util_format_get_blockheight(src_format
);
238 dst_bs
= util_format_get_blocksize(dst_format
);
239 dst_bw
= util_format_get_blockwidth(dst_format
);
240 dst_bh
= util_format_get_blockheight(dst_format
);
242 /* Note: all box positions and sizes are in pixels */
243 if (src_bw
> 1 && dst_bw
== 1) {
244 /* Copy from compressed to uncompressed.
245 * Shrink dest box by the src block size.
247 dst_box
.width
/= src_bw
;
248 dst_box
.height
/= src_bh
;
250 else if (src_bw
== 1 && dst_bw
> 1) {
251 /* Copy from uncompressed to compressed.
252 * Expand dest box by the dest block size.
254 dst_box
.width
*= dst_bw
;
255 dst_box
.height
*= dst_bh
;
258 /* compressed -> compressed or uncompressed -> uncompressed copy */
259 assert(src_bw
== dst_bw
);
260 assert(src_bh
== dst_bh
);
263 assert(src_bs
== dst_bs
);
264 if (src_bs
!= dst_bs
) {
265 /* This can happen if we fail to do format checking before hand.
271 /* check that region boxes are block aligned */
272 assert(src_box
.x
% src_bw
== 0);
273 assert(src_box
.y
% src_bh
== 0);
274 assert(dst_box
.x
% dst_bw
== 0);
275 assert(dst_box
.y
% dst_bh
== 0);
277 /* check that region boxes are not out of bounds */
278 assert(src_box
.x
+ src_box
.width
<= (int)u_minify(src
->width0
, src_level
));
279 assert(src_box
.y
+ src_box
.height
<= (int)u_minify(src
->height0
, src_level
));
280 assert(dst_box
.x
+ dst_box
.width
<= (int)u_minify(dst
->width0
, dst_level
));
281 assert(dst_box
.y
+ dst_box
.height
<= (int)u_minify(dst
->height0
, dst_level
));
283 /* check that total number of src, dest bytes match */
284 assert((src_box
.width
/ src_bw
) * (src_box
.height
/ src_bh
) * src_bs
==
285 (dst_box
.width
/ dst_bw
) * (dst_box
.height
/ dst_bh
) * dst_bs
);
287 src_map
= pipe
->transfer_map(pipe
,
291 &src_box
, &src_trans
);
297 dst_map
= pipe
->transfer_map(pipe
,
300 PIPE_TRANSFER_WRITE
|
301 PIPE_TRANSFER_DISCARD_RANGE
, &dst_box
,
308 if (dst
->target
== PIPE_BUFFER
&& src
->target
== PIPE_BUFFER
) {
309 assert(src_box
.height
== 1);
310 assert(src_box
.depth
== 1);
311 memcpy(dst_map
, src_map
, src_box
.width
);
313 util_copy_box(dst_map
,
315 dst_trans
->stride
, dst_trans
->layer_stride
,
317 src_box
.width
, src_box
.height
, src_box
.depth
,
319 src_trans
->stride
, src_trans
->layer_stride
,
323 pipe
->transfer_unmap(pipe
, dst_trans
);
325 pipe
->transfer_unmap(pipe
, src_trans
);
331 util_clear_color_texture_helper(struct pipe_transfer
*dst_trans
,
333 enum pipe_format format
,
334 const union pipe_color_union
*color
,
335 unsigned width
, unsigned height
, unsigned depth
)
339 assert(dst_trans
->stride
> 0);
341 if (util_format_is_pure_integer(format
)) {
343 * We expect int/uint clear values here, though some APIs
344 * might disagree (but in any case util_pack_color()
345 * couldn't handle it)...
347 if (util_format_is_pure_sint(format
)) {
348 util_format_write_4i(format
, color
->i
, 0, &uc
, 0, 0, 0, 1, 1);
350 assert(util_format_is_pure_uint(format
));
351 util_format_write_4ui(format
, color
->ui
, 0, &uc
, 0, 0, 0, 1, 1);
354 util_pack_color(color
->f
, format
, &uc
);
357 util_fill_box(dst_map
, format
,
358 dst_trans
->stride
, dst_trans
->layer_stride
,
359 0, 0, 0, width
, height
, depth
, &uc
);
363 util_clear_color_texture(struct pipe_context
*pipe
,
364 struct pipe_resource
*texture
,
365 enum pipe_format format
,
366 const union pipe_color_union
*color
,
368 unsigned dstx
, unsigned dsty
, unsigned dstz
,
369 unsigned width
, unsigned height
, unsigned depth
)
371 struct pipe_transfer
*dst_trans
;
374 dst_map
= pipe_transfer_map_3d(pipe
,
379 width
, height
, depth
,
384 if (dst_trans
->stride
> 0) {
385 util_clear_color_texture_helper(dst_trans
, dst_map
, format
, color
,
386 width
, height
, depth
);
388 pipe
->transfer_unmap(pipe
, dst_trans
);
392 #define UBYTE_TO_USHORT(B) ((B) | ((B) << 8))
396 * Fallback for pipe->clear_render_target() function.
397 * XXX this looks too hackish to be really useful.
398 * cpp > 4 looks like a gross hack at best...
399 * Plus can't use these transfer fallbacks when clearing
400 * multisampled surfaces for instance.
401 * Clears all bound layers.
404 util_clear_render_target(struct pipe_context
*pipe
,
405 struct pipe_surface
*dst
,
406 const union pipe_color_union
*color
,
407 unsigned dstx
, unsigned dsty
,
408 unsigned width
, unsigned height
)
410 struct pipe_transfer
*dst_trans
;
413 assert(dst
->texture
);
417 if (dst
->texture
->target
== PIPE_BUFFER
) {
419 * The fill naturally works on the surface format, however
420 * the transfer uses resource format which is just bytes for buffers.
423 unsigned pixstride
= util_format_get_blocksize(dst
->format
);
424 dx
= (dst
->u
.buf
.first_element
+ dstx
) * pixstride
;
425 w
= width
* pixstride
;
426 dst_map
= pipe_transfer_map(pipe
,
433 util_clear_color_texture_helper(dst_trans
, dst_map
, dst
->format
,
434 color
, width
, height
, 1);
435 pipe
->transfer_unmap(pipe
, dst_trans
);
439 unsigned depth
= dst
->u
.tex
.last_layer
- dst
->u
.tex
.first_layer
+ 1;
440 util_clear_color_texture(pipe
, dst
->texture
, dst
->format
, color
,
441 dst
->u
.tex
.level
, dstx
, dsty
,
442 dst
->u
.tex
.first_layer
, width
, height
, depth
);
447 util_clear_depth_stencil_texture(struct pipe_context
*pipe
,
448 struct pipe_resource
*texture
,
449 enum pipe_format format
,
450 unsigned clear_flags
,
451 uint64_t zstencil
, unsigned level
,
452 unsigned dstx
, unsigned dsty
, unsigned dstz
,
453 unsigned width
, unsigned height
, unsigned depth
)
455 struct pipe_transfer
*dst_trans
;
457 boolean need_rmw
= FALSE
;
460 unsigned i
, j
, layer
;
462 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) &&
463 ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) != PIPE_CLEAR_DEPTHSTENCIL
) &&
464 util_format_is_depth_and_stencil(format
))
467 dst_map
= pipe_transfer_map_3d(pipe
,
470 (need_rmw
? PIPE_TRANSFER_READ_WRITE
:
471 PIPE_TRANSFER_WRITE
),
473 width
, height
, depth
, &dst_trans
);
478 dst_stride
= dst_trans
->stride
;
480 assert(dst_trans
->stride
> 0);
482 for (layer
= 0; layer
< depth
; layer
++) {
485 switch (util_format_get_blocksize(format
)) {
487 assert(format
== PIPE_FORMAT_S8_UINT
);
488 if(dst_stride
== width
)
489 memset(dst_map
, (uint8_t) zstencil
, height
* width
);
491 for (i
= 0; i
< height
; i
++) {
492 memset(dst_map
, (uint8_t) zstencil
, width
);
493 dst_map
+= dst_stride
;
498 assert(format
== PIPE_FORMAT_Z16_UNORM
);
499 for (i
= 0; i
< height
; i
++) {
500 uint16_t *row
= (uint16_t *)dst_map
;
501 for (j
= 0; j
< width
; j
++)
502 *row
++ = (uint16_t) zstencil
;
503 dst_map
+= dst_stride
;
508 for (i
= 0; i
< height
; i
++) {
509 uint32_t *row
= (uint32_t *)dst_map
;
510 for (j
= 0; j
< width
; j
++)
511 *row
++ = (uint32_t) zstencil
;
512 dst_map
+= dst_stride
;
517 if (format
== PIPE_FORMAT_Z24_UNORM_S8_UINT
)
518 dst_mask
= 0x00ffffff;
520 assert(format
== PIPE_FORMAT_S8_UINT_Z24_UNORM
);
521 dst_mask
= 0xffffff00;
523 if (clear_flags
& PIPE_CLEAR_DEPTH
)
524 dst_mask
= ~dst_mask
;
525 for (i
= 0; i
< height
; i
++) {
526 uint32_t *row
= (uint32_t *)dst_map
;
527 for (j
= 0; j
< width
; j
++) {
528 uint32_t tmp
= *row
& dst_mask
;
529 *row
++ = tmp
| ((uint32_t) zstencil
& ~dst_mask
);
531 dst_map
+= dst_stride
;
537 for (i
= 0; i
< height
; i
++) {
538 uint64_t *row
= (uint64_t *)dst_map
;
539 for (j
= 0; j
< width
; j
++)
541 dst_map
+= dst_stride
;
547 if (clear_flags
& PIPE_CLEAR_DEPTH
)
548 src_mask
= 0x00000000ffffffffull
;
550 src_mask
= 0x000000ff00000000ull
;
552 for (i
= 0; i
< height
; i
++) {
553 uint64_t *row
= (uint64_t *)dst_map
;
554 for (j
= 0; j
< width
; j
++) {
555 uint64_t tmp
= *row
& ~src_mask
;
556 *row
++ = tmp
| (zstencil
& src_mask
);
558 dst_map
+= dst_stride
;
566 dst_layer
+= dst_trans
->layer_stride
;
569 pipe
->transfer_unmap(pipe
, dst_trans
);
574 util_clear_texture(struct pipe_context
*pipe
,
575 struct pipe_resource
*tex
,
577 const struct pipe_box
*box
,
580 const struct util_format_description
*desc
=
581 util_format_description(tex
->format
);
583 if (level
> tex
->last_level
)
586 if (util_format_is_depth_or_stencil(tex
->format
)) {
592 if (util_format_has_depth(desc
)) {
593 clear
|= PIPE_CLEAR_DEPTH
;
594 desc
->unpack_z_float(&depth
, 0, data
, 0, 1, 1);
597 if (util_format_has_stencil(desc
)) {
598 clear
|= PIPE_CLEAR_STENCIL
;
599 desc
->unpack_s_8uint(&stencil
, 0, data
, 0, 1, 1);
602 zstencil
= util_pack64_z_stencil(tex
->format
, depth
, stencil
);
604 util_clear_depth_stencil_texture(pipe
, tex
, tex
->format
, clear
, zstencil
,
605 level
, box
->x
, box
->y
, box
->z
,
606 box
->width
, box
->height
, box
->depth
);
608 union pipe_color_union color
;
609 if (util_format_is_pure_uint(tex
->format
))
610 desc
->unpack_rgba_uint(color
.ui
, 0, data
, 0, 1, 1);
611 else if (util_format_is_pure_sint(tex
->format
))
612 desc
->unpack_rgba_sint(color
.i
, 0, data
, 0, 1, 1);
614 desc
->unpack_rgba_float(color
.f
, 0, data
, 0, 1, 1);
616 util_clear_color_texture(pipe
, tex
, tex
->format
, &color
, level
,
617 box
->x
, box
->y
, box
->z
,
618 box
->width
, box
->height
, box
->depth
);
624 * Fallback for pipe->clear_stencil() function.
625 * sw fallback doesn't look terribly useful here.
626 * Plus can't use these transfer fallbacks when clearing
627 * multisampled surfaces for instance.
628 * Clears all bound layers.
631 util_clear_depth_stencil(struct pipe_context
*pipe
,
632 struct pipe_surface
*dst
,
633 unsigned clear_flags
,
636 unsigned dstx
, unsigned dsty
,
637 unsigned width
, unsigned height
)
642 assert(dst
->texture
);
646 zstencil
= util_pack64_z_stencil(dst
->format
, depth
, stencil
);
647 max_layer
= dst
->u
.tex
.last_layer
- dst
->u
.tex
.first_layer
;
648 util_clear_depth_stencil_texture(pipe
, dst
->texture
, dst
->format
,
649 clear_flags
, zstencil
, dst
->u
.tex
.level
,
650 dstx
, dsty
, dst
->u
.tex
.first_layer
,
651 width
, height
, max_layer
+ 1);
655 /* Return if the box is totally inside the resource.
658 is_box_inside_resource(const struct pipe_resource
*res
,
659 const struct pipe_box
*box
,
662 unsigned width
= 1, height
= 1, depth
= 1;
664 switch (res
->target
) {
670 case PIPE_TEXTURE_1D
:
671 width
= u_minify(res
->width0
, level
);
675 case PIPE_TEXTURE_2D
:
676 case PIPE_TEXTURE_RECT
:
677 width
= u_minify(res
->width0
, level
);
678 height
= u_minify(res
->height0
, level
);
681 case PIPE_TEXTURE_3D
:
682 width
= u_minify(res
->width0
, level
);
683 height
= u_minify(res
->height0
, level
);
684 depth
= u_minify(res
->depth0
, level
);
686 case PIPE_TEXTURE_CUBE
:
687 width
= u_minify(res
->width0
, level
);
688 height
= u_minify(res
->height0
, level
);
691 case PIPE_TEXTURE_1D_ARRAY
:
692 width
= u_minify(res
->width0
, level
);
694 depth
= res
->array_size
;
696 case PIPE_TEXTURE_2D_ARRAY
:
697 width
= u_minify(res
->width0
, level
);
698 height
= u_minify(res
->height0
, level
);
699 depth
= res
->array_size
;
701 case PIPE_TEXTURE_CUBE_ARRAY
:
702 width
= u_minify(res
->width0
, level
);
703 height
= u_minify(res
->height0
, level
);
704 depth
= res
->array_size
;
705 assert(res
->array_size
% 6 == 0);
707 case PIPE_MAX_TEXTURE_TYPES
:
711 return box
->x
>= 0 &&
712 box
->x
+ box
->width
<= (int) width
&&
714 box
->y
+ box
->height
<= (int) height
&&
716 box
->z
+ box
->depth
<= (int) depth
;
720 get_sample_count(const struct pipe_resource
*res
)
722 return res
->nr_samples
? res
->nr_samples
: 1;
727 * Check if a blit() command can be implemented with a resource_copy_region().
728 * If tight_format_check is true, only allow the resource_copy_region() if
729 * the blit src/dst formats are identical, ignoring the resource formats.
730 * Otherwise, check for format casting and compatibility.
733 util_can_blit_via_copy_region(const struct pipe_blit_info
*blit
,
734 boolean tight_format_check
)
736 const struct util_format_description
*src_desc
, *dst_desc
;
738 src_desc
= util_format_description(blit
->src
.resource
->format
);
739 dst_desc
= util_format_description(blit
->dst
.resource
->format
);
741 if (tight_format_check
) {
742 /* no format conversions allowed */
743 if (blit
->src
.format
!= blit
->dst
.format
) {
748 /* do loose format compatibility checking */
749 if (blit
->src
.resource
->format
!= blit
->src
.format
||
750 blit
->dst
.resource
->format
!= blit
->dst
.format
||
751 !util_is_format_compatible(src_desc
, dst_desc
)) {
756 unsigned mask
= util_format_get_mask(blit
->dst
.format
);
758 /* No masks, no filtering, no scissor, no blending */
759 if ((blit
->mask
& mask
) != mask
||
760 blit
->filter
!= PIPE_TEX_FILTER_NEAREST
||
761 blit
->scissor_enable
||
762 blit
->num_window_rectangles
> 0 ||
767 /* Only the src box can have negative dims for flipping */
768 assert(blit
->dst
.box
.width
>= 1);
769 assert(blit
->dst
.box
.height
>= 1);
770 assert(blit
->dst
.box
.depth
>= 1);
772 /* No scaling or flipping */
773 if (blit
->src
.box
.width
!= blit
->dst
.box
.width
||
774 blit
->src
.box
.height
!= blit
->dst
.box
.height
||
775 blit
->src
.box
.depth
!= blit
->dst
.box
.depth
) {
779 /* No out-of-bounds access. */
780 if (!is_box_inside_resource(blit
->src
.resource
, &blit
->src
.box
,
782 !is_box_inside_resource(blit
->dst
.resource
, &blit
->dst
.box
,
787 /* Sample counts must match. */
788 if (get_sample_count(blit
->src
.resource
) !=
789 get_sample_count(blit
->dst
.resource
)) {
798 * Try to do a blit using resource_copy_region. The function calls
799 * resource_copy_region if the blit description is compatible with it.
801 * It returns TRUE if the blit was done using resource_copy_region.
803 * It returns FALSE otherwise and the caller must fall back to a more generic
804 * codepath for the blit operation. (e.g. by using u_blitter)
807 util_try_blit_via_copy_region(struct pipe_context
*ctx
,
808 const struct pipe_blit_info
*blit
)
810 if (util_can_blit_via_copy_region(blit
, FALSE
)) {
811 ctx
->resource_copy_region(ctx
, blit
->dst
.resource
, blit
->dst
.level
,
812 blit
->dst
.box
.x
, blit
->dst
.box
.y
,
814 blit
->src
.resource
, blit
->src
.level
,