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/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 2D rect from one place to another.
62 * Position and sizes are in pixels.
63 * src_stride may be negative to do vertical flip of pixels from source.
66 util_copy_rect(ubyte
* dst
,
67 enum pipe_format format
,
79 int src_stride_pos
= src_stride
< 0 ? -src_stride
: src_stride
;
80 int blocksize
= util_format_get_blocksize(format
);
81 int blockwidth
= util_format_get_blockwidth(format
);
82 int blockheight
= util_format_get_blockheight(format
);
84 assert(blocksize
> 0);
85 assert(blockwidth
> 0);
86 assert(blockheight
> 0);
90 width
= (width
+ blockwidth
- 1)/blockwidth
;
91 height
= (height
+ blockheight
- 1)/blockheight
;
95 dst
+= dst_x
* blocksize
;
96 src
+= src_x
* blocksize
;
97 dst
+= dst_y
* dst_stride
;
98 src
+= src_y
* src_stride_pos
;
101 if (width
== dst_stride
&& width
== src_stride
)
102 memcpy(dst
, src
, height
* width
);
104 for (i
= 0; i
< height
; i
++) {
105 memcpy(dst
, src
, width
);
114 * Copy 3D box from one place to another.
115 * Position and sizes are in pixels.
118 util_copy_box(ubyte
* dst
,
119 enum pipe_format format
,
120 unsigned dst_stride
, unsigned dst_slice_stride
,
121 unsigned dst_x
, unsigned dst_y
, unsigned dst_z
,
122 unsigned width
, unsigned height
, unsigned depth
,
124 int src_stride
, unsigned src_slice_stride
,
125 unsigned src_x
, unsigned src_y
, unsigned src_z
)
128 dst
+= dst_z
* dst_slice_stride
;
129 src
+= src_z
* src_slice_stride
;
130 for (z
= 0; z
< depth
; ++z
) {
140 dst
+= dst_slice_stride
;
141 src
+= src_slice_stride
;
147 util_fill_rect(ubyte
* dst
,
148 enum pipe_format format
,
154 union util_color
*uc
)
156 const struct util_format_description
*desc
= util_format_description(format
);
159 int blocksize
= desc
->block
.bits
/ 8;
160 int blockwidth
= desc
->block
.width
;
161 int blockheight
= desc
->block
.height
;
163 assert(blocksize
> 0);
164 assert(blockwidth
> 0);
165 assert(blockheight
> 0);
168 dst_y
/= blockheight
;
169 width
= (width
+ blockwidth
- 1)/blockwidth
;
170 height
= (height
+ blockheight
- 1)/blockheight
;
172 dst
+= dst_x
* blocksize
;
173 dst
+= dst_y
* dst_stride
;
174 width_size
= width
* blocksize
;
178 if(dst_stride
== width_size
)
179 memset(dst
, uc
->ub
, height
* width_size
);
181 for (i
= 0; i
< height
; i
++) {
182 memset(dst
, uc
->ub
, width_size
);
188 for (i
= 0; i
< height
; i
++) {
189 uint16_t *row
= (uint16_t *)dst
;
190 for (j
= 0; j
< width
; j
++)
196 for (i
= 0; i
< height
; i
++) {
197 uint32_t *row
= (uint32_t *)dst
;
198 for (j
= 0; j
< width
; j
++)
204 for (i
= 0; i
< height
; i
++) {
206 for (j
= 0; j
< width
; j
++) {
207 memcpy(row
, uc
, blocksize
);
218 util_fill_box(ubyte
* dst
,
219 enum pipe_format format
,
221 unsigned layer_stride
,
228 union util_color
*uc
)
231 dst
+= z
* layer_stride
;
232 for (layer
= z
; layer
< depth
; layer
++) {
233 util_fill_rect(dst
, format
,
235 x
, y
, width
, height
, uc
);
242 * Fallback function for pipe->resource_copy_region().
243 * Note: (X,Y)=(0,0) is always the upper-left corner.
246 util_resource_copy_region(struct pipe_context
*pipe
,
247 struct pipe_resource
*dst
,
249 unsigned dst_x
, unsigned dst_y
, unsigned dst_z
,
250 struct pipe_resource
*src
,
252 const struct pipe_box
*src_box
)
254 struct pipe_transfer
*src_trans
, *dst_trans
;
256 const uint8_t *src_map
;
257 MAYBE_UNUSED
enum pipe_format src_format
;
258 enum pipe_format dst_format
;
259 struct pipe_box dst_box
;
265 assert((src
->target
== PIPE_BUFFER
&& dst
->target
== PIPE_BUFFER
) ||
266 (src
->target
!= PIPE_BUFFER
&& dst
->target
!= PIPE_BUFFER
));
268 src_format
= src
->format
;
269 dst_format
= dst
->format
;
271 assert(util_format_get_blocksize(dst_format
) == util_format_get_blocksize(src_format
));
272 assert(util_format_get_blockwidth(dst_format
) == util_format_get_blockwidth(src_format
));
273 assert(util_format_get_blockheight(dst_format
) == util_format_get_blockheight(src_format
));
275 src_map
= pipe
->transfer_map(pipe
,
279 src_box
, &src_trans
);
288 dst_box
.width
= src_box
->width
;
289 dst_box
.height
= src_box
->height
;
290 dst_box
.depth
= src_box
->depth
;
292 dst_map
= pipe
->transfer_map(pipe
,
295 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD_RANGE
,
296 &dst_box
, &dst_trans
);
302 if (dst
->target
== PIPE_BUFFER
&& src
->target
== PIPE_BUFFER
) {
303 assert(src_box
->height
== 1);
304 assert(src_box
->depth
== 1);
305 memcpy(dst_map
, src_map
, src_box
->width
);
307 util_copy_box(dst_map
,
309 dst_trans
->stride
, dst_trans
->layer_stride
,
311 src_box
->width
, src_box
->height
, src_box
->depth
,
313 src_trans
->stride
, src_trans
->layer_stride
,
317 pipe
->transfer_unmap(pipe
, dst_trans
);
319 pipe
->transfer_unmap(pipe
, src_trans
);
326 #define UBYTE_TO_USHORT(B) ((B) | ((B) << 8))
330 * Fallback for pipe->clear_render_target() function.
331 * XXX this looks too hackish to be really useful.
332 * cpp > 4 looks like a gross hack at best...
333 * Plus can't use these transfer fallbacks when clearing
334 * multisampled surfaces for instance.
335 * Clears all bound layers.
338 util_clear_render_target(struct pipe_context
*pipe
,
339 struct pipe_surface
*dst
,
340 const union pipe_color_union
*color
,
341 unsigned dstx
, unsigned dsty
,
342 unsigned width
, unsigned height
)
344 struct pipe_transfer
*dst_trans
;
349 assert(dst
->texture
);
353 if (dst
->texture
->target
== PIPE_BUFFER
) {
355 * The fill naturally works on the surface format, however
356 * the transfer uses resource format which is just bytes for buffers.
359 unsigned pixstride
= util_format_get_blocksize(dst
->format
);
360 dx
= (dst
->u
.buf
.first_element
+ dstx
) * pixstride
;
361 w
= width
* pixstride
;
363 dst_map
= pipe_transfer_map(pipe
,
371 max_layer
= dst
->u
.tex
.last_layer
- dst
->u
.tex
.first_layer
;
372 dst_map
= pipe_transfer_map_3d(pipe
,
376 dstx
, dsty
, dst
->u
.tex
.first_layer
,
377 width
, height
, max_layer
+ 1, &dst_trans
);
383 enum pipe_format format
= dst
->format
;
384 assert(dst_trans
->stride
> 0);
386 if (util_format_is_pure_integer(format
)) {
388 * We expect int/uint clear values here, though some APIs
389 * might disagree (but in any case util_pack_color()
390 * couldn't handle it)...
392 if (util_format_is_pure_sint(format
)) {
393 util_format_write_4i(format
, color
->i
, 0, &uc
, 0, 0, 0, 1, 1);
396 assert(util_format_is_pure_uint(format
));
397 util_format_write_4ui(format
, color
->ui
, 0, &uc
, 0, 0, 0, 1, 1);
401 util_pack_color(color
->f
, format
, &uc
);
404 util_fill_box(dst_map
, dst
->format
,
405 dst_trans
->stride
, dst_trans
->layer_stride
,
406 0, 0, 0, width
, height
, max_layer
+ 1, &uc
);
408 pipe
->transfer_unmap(pipe
, dst_trans
);
413 * Fallback for pipe->clear_stencil() function.
414 * sw fallback doesn't look terribly useful here.
415 * Plus can't use these transfer fallbacks when clearing
416 * multisampled surfaces for instance.
417 * Clears all bound layers.
420 util_clear_depth_stencil(struct pipe_context
*pipe
,
421 struct pipe_surface
*dst
,
422 unsigned clear_flags
,
425 unsigned dstx
, unsigned dsty
,
426 unsigned width
, unsigned height
)
428 enum pipe_format format
= dst
->format
;
429 struct pipe_transfer
*dst_trans
;
431 boolean need_rmw
= FALSE
;
432 unsigned max_layer
, layer
;
434 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) &&
435 ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) != PIPE_CLEAR_DEPTHSTENCIL
) &&
436 util_format_is_depth_and_stencil(format
))
439 assert(dst
->texture
);
443 max_layer
= dst
->u
.tex
.last_layer
- dst
->u
.tex
.first_layer
;
444 dst_map
= pipe_transfer_map_3d(pipe
,
447 (need_rmw
? PIPE_TRANSFER_READ_WRITE
:
448 PIPE_TRANSFER_WRITE
),
449 dstx
, dsty
, dst
->u
.tex
.first_layer
,
450 width
, height
, max_layer
+ 1, &dst_trans
);
454 unsigned dst_stride
= dst_trans
->stride
;
455 uint64_t zstencil
= util_pack64_z_stencil(format
, depth
, stencil
);
456 ubyte
*dst_layer
= dst_map
;
458 assert(dst_trans
->stride
> 0);
460 for (layer
= 0; layer
<= max_layer
; layer
++) {
463 switch (util_format_get_blocksize(format
)) {
465 assert(format
== PIPE_FORMAT_S8_UINT
);
466 if(dst_stride
== width
)
467 memset(dst_map
, (uint8_t) zstencil
, height
* width
);
469 for (i
= 0; i
< height
; i
++) {
470 memset(dst_map
, (uint8_t) zstencil
, width
);
471 dst_map
+= dst_stride
;
476 assert(format
== PIPE_FORMAT_Z16_UNORM
);
477 for (i
= 0; i
< height
; i
++) {
478 uint16_t *row
= (uint16_t *)dst_map
;
479 for (j
= 0; j
< width
; j
++)
480 *row
++ = (uint16_t) zstencil
;
481 dst_map
+= dst_stride
;
486 for (i
= 0; i
< height
; i
++) {
487 uint32_t *row
= (uint32_t *)dst_map
;
488 for (j
= 0; j
< width
; j
++)
489 *row
++ = (uint32_t) zstencil
;
490 dst_map
+= dst_stride
;
495 if (format
== PIPE_FORMAT_Z24_UNORM_S8_UINT
)
496 dst_mask
= 0x00ffffff;
498 assert(format
== PIPE_FORMAT_S8_UINT_Z24_UNORM
);
499 dst_mask
= 0xffffff00;
501 if (clear_flags
& PIPE_CLEAR_DEPTH
)
502 dst_mask
= ~dst_mask
;
503 for (i
= 0; i
< height
; i
++) {
504 uint32_t *row
= (uint32_t *)dst_map
;
505 for (j
= 0; j
< width
; j
++) {
506 uint32_t tmp
= *row
& dst_mask
;
507 *row
++ = tmp
| ((uint32_t) zstencil
& ~dst_mask
);
509 dst_map
+= dst_stride
;
515 for (i
= 0; i
< height
; i
++) {
516 uint64_t *row
= (uint64_t *)dst_map
;
517 for (j
= 0; j
< width
; j
++)
519 dst_map
+= dst_stride
;
525 if (clear_flags
& PIPE_CLEAR_DEPTH
)
526 src_mask
= 0x00000000ffffffffull
;
528 src_mask
= 0x000000ff00000000ull
;
530 for (i
= 0; i
< height
; i
++) {
531 uint64_t *row
= (uint64_t *)dst_map
;
532 for (j
= 0; j
< width
; j
++) {
533 uint64_t tmp
= *row
& ~src_mask
;
534 *row
++ = tmp
| (zstencil
& src_mask
);
536 dst_map
+= dst_stride
;
544 dst_layer
+= dst_trans
->layer_stride
;
547 pipe
->transfer_unmap(pipe
, dst_trans
);
552 /* Return if the box is totally inside the resource.
555 is_box_inside_resource(const struct pipe_resource
*res
,
556 const struct pipe_box
*box
,
559 unsigned width
= 1, height
= 1, depth
= 1;
561 switch (res
->target
) {
567 case PIPE_TEXTURE_1D
:
568 width
= u_minify(res
->width0
, level
);
572 case PIPE_TEXTURE_2D
:
573 case PIPE_TEXTURE_RECT
:
574 width
= u_minify(res
->width0
, level
);
575 height
= u_minify(res
->height0
, level
);
578 case PIPE_TEXTURE_3D
:
579 width
= u_minify(res
->width0
, level
);
580 height
= u_minify(res
->height0
, level
);
581 depth
= u_minify(res
->depth0
, level
);
583 case PIPE_TEXTURE_CUBE
:
584 width
= u_minify(res
->width0
, level
);
585 height
= u_minify(res
->height0
, level
);
588 case PIPE_TEXTURE_1D_ARRAY
:
589 width
= u_minify(res
->width0
, level
);
591 depth
= res
->array_size
;
593 case PIPE_TEXTURE_2D_ARRAY
:
594 width
= u_minify(res
->width0
, level
);
595 height
= u_minify(res
->height0
, level
);
596 depth
= res
->array_size
;
598 case PIPE_TEXTURE_CUBE_ARRAY
:
599 width
= u_minify(res
->width0
, level
);
600 height
= u_minify(res
->height0
, level
);
601 depth
= res
->array_size
;
602 assert(res
->array_size
% 6 == 0);
604 case PIPE_MAX_TEXTURE_TYPES
:
608 return box
->x
>= 0 &&
609 box
->x
+ box
->width
<= (int) width
&&
611 box
->y
+ box
->height
<= (int) height
&&
613 box
->z
+ box
->depth
<= (int) depth
;
617 get_sample_count(const struct pipe_resource
*res
)
619 return res
->nr_samples
? res
->nr_samples
: 1;
623 * Try to do a blit using resource_copy_region. The function calls
624 * resource_copy_region if the blit description is compatible with it.
626 * It returns TRUE if the blit was done using resource_copy_region.
628 * It returns FALSE otherwise and the caller must fall back to a more generic
629 * codepath for the blit operation. (e.g. by using u_blitter)
632 util_try_blit_via_copy_region(struct pipe_context
*ctx
,
633 const struct pipe_blit_info
*blit
)
635 unsigned mask
= util_format_get_mask(blit
->dst
.format
);
637 /* No format conversions. */
638 if (blit
->src
.resource
->format
!= blit
->src
.format
||
639 blit
->dst
.resource
->format
!= blit
->dst
.format
||
640 !util_is_format_compatible(
641 util_format_description(blit
->src
.resource
->format
),
642 util_format_description(blit
->dst
.resource
->format
))) {
646 /* No masks, no filtering, no scissor. */
647 if ((blit
->mask
& mask
) != mask
||
648 blit
->filter
!= PIPE_TEX_FILTER_NEAREST
||
649 blit
->scissor_enable
) {
654 if (blit
->src
.box
.width
< 0 ||
655 blit
->src
.box
.height
< 0 ||
656 blit
->src
.box
.depth
< 0) {
661 if (blit
->src
.box
.width
!= blit
->dst
.box
.width
||
662 blit
->src
.box
.height
!= blit
->dst
.box
.height
||
663 blit
->src
.box
.depth
!= blit
->dst
.box
.depth
) {
667 /* No out-of-bounds access. */
668 if (!is_box_inside_resource(blit
->src
.resource
, &blit
->src
.box
,
670 !is_box_inside_resource(blit
->dst
.resource
, &blit
->dst
.box
,
675 /* Sample counts must match. */
676 if (get_sample_count(blit
->src
.resource
) !=
677 get_sample_count(blit
->dst
.resource
)) {
681 if (blit
->alpha_blend
)
684 ctx
->resource_copy_region(ctx
, blit
->dst
.resource
, blit
->dst
.level
,
685 blit
->dst
.box
.x
, blit
->dst
.box
.y
, blit
->dst
.box
.z
,
686 blit
->src
.resource
, blit
->src
.level
,