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
,
55 memset(surf
, 0, sizeof(*surf
));
57 surf
->format
= texture
->format
;
58 /* XXX should filter out all non-rt/ds bind flags ? */
63 * Helper to quickly create an RGBA rendering surface of a certain size.
64 * \param textureOut returns the new texture
65 * \param surfaceOut returns the new surface
66 * \return TRUE for success, FALSE if failure
69 util_create_rgba_texture(struct pipe_context
*pipe
,
70 uint width
, uint height
, uint bind
,
71 struct pipe_resource
**textureOut
)
73 static const enum pipe_format rgbaFormats
[] = {
74 PIPE_FORMAT_B8G8R8A8_UNORM
,
75 PIPE_FORMAT_A8R8G8B8_UNORM
,
76 PIPE_FORMAT_A8B8G8R8_UNORM
,
79 const uint target
= PIPE_TEXTURE_2D
;
80 enum pipe_format format
= PIPE_FORMAT_NONE
;
81 struct pipe_resource templ
;
82 struct pipe_surface surf_templ
;
83 struct pipe_screen
*screen
= pipe
->screen
;
86 /* Choose surface format */
87 for (i
= 0; rgbaFormats
[i
]; i
++) {
88 if (screen
->is_format_supported(screen
, rgbaFormats
[i
],
90 format
= rgbaFormats
[i
];
94 if (format
== PIPE_FORMAT_NONE
)
95 return FALSE
; /* unable to get an rgba format!?! */
98 memset(&templ
, 0, sizeof(templ
));
99 templ
.target
= target
;
100 templ
.format
= format
;
101 templ
.last_level
= 0;
102 templ
.width0
= width
;
103 templ
.height0
= height
;
105 templ
.array_size
= 1;
108 *textureOut
= screen
->resource_create(screen
, &templ
);
113 u_surface_default_template(&surf_templ
, *textureOut
, bind
);
119 * Copy 2D rect from one place to another.
120 * Position and sizes are in pixels.
121 * src_stride may be negative to do vertical flip of pixels from source.
124 util_copy_rect(ubyte
* dst
,
125 enum pipe_format format
,
137 int src_stride_pos
= src_stride
< 0 ? -src_stride
: src_stride
;
138 int blocksize
= util_format_get_blocksize(format
);
139 int blockwidth
= util_format_get_blockwidth(format
);
140 int blockheight
= util_format_get_blockheight(format
);
142 assert(blocksize
> 0);
143 assert(blockwidth
> 0);
144 assert(blockheight
> 0);
147 dst_y
/= blockheight
;
148 width
= (width
+ blockwidth
- 1)/blockwidth
;
149 height
= (height
+ blockheight
- 1)/blockheight
;
151 src_y
/= blockheight
;
153 dst
+= dst_x
* blocksize
;
154 src
+= src_x
* blocksize
;
155 dst
+= dst_y
* dst_stride
;
156 src
+= src_y
* src_stride_pos
;
159 if (width
== dst_stride
&& width
== src_stride
)
160 memcpy(dst
, src
, height
* width
);
162 for (i
= 0; i
< height
; i
++) {
163 memcpy(dst
, src
, width
);
172 * Copy 3D box from one place to another.
173 * Position and sizes are in pixels.
176 util_copy_box(ubyte
* dst
,
177 enum pipe_format format
,
178 unsigned dst_stride
, unsigned dst_slice_stride
,
179 unsigned dst_x
, unsigned dst_y
, unsigned dst_z
,
180 unsigned width
, unsigned height
, unsigned depth
,
182 int src_stride
, unsigned src_slice_stride
,
183 unsigned src_x
, unsigned src_y
, unsigned src_z
)
186 dst
+= dst_z
* dst_slice_stride
;
187 src
+= src_z
* src_slice_stride
;
188 for (z
= 0; z
< depth
; ++z
) {
198 dst
+= dst_slice_stride
;
199 src
+= src_slice_stride
;
205 util_fill_rect(ubyte
* dst
,
206 enum pipe_format format
,
212 union util_color
*uc
)
214 const struct util_format_description
*desc
= util_format_description(format
);
217 int blocksize
= desc
->block
.bits
/ 8;
218 int blockwidth
= desc
->block
.width
;
219 int blockheight
= desc
->block
.height
;
221 assert(blocksize
> 0);
222 assert(blockwidth
> 0);
223 assert(blockheight
> 0);
226 dst_y
/= blockheight
;
227 width
= (width
+ blockwidth
- 1)/blockwidth
;
228 height
= (height
+ blockheight
- 1)/blockheight
;
230 dst
+= dst_x
* blocksize
;
231 dst
+= dst_y
* dst_stride
;
232 width_size
= width
* blocksize
;
236 if(dst_stride
== width_size
)
237 memset(dst
, uc
->ub
, height
* width_size
);
239 for (i
= 0; i
< height
; i
++) {
240 memset(dst
, uc
->ub
, width_size
);
246 for (i
= 0; i
< height
; i
++) {
247 uint16_t *row
= (uint16_t *)dst
;
248 for (j
= 0; j
< width
; j
++)
254 for (i
= 0; i
< height
; i
++) {
255 uint32_t *row
= (uint32_t *)dst
;
256 for (j
= 0; j
< width
; j
++)
262 for (i
= 0; i
< height
; i
++) {
264 for (j
= 0; j
< width
; j
++) {
265 memcpy(row
, uc
, blocksize
);
276 * Fallback function for pipe->resource_copy_region().
277 * Note: (X,Y)=(0,0) is always the upper-left corner.
280 util_resource_copy_region(struct pipe_context
*pipe
,
281 struct pipe_resource
*dst
,
283 unsigned dst_x
, unsigned dst_y
, unsigned dst_z
,
284 struct pipe_resource
*src
,
286 const struct pipe_box
*src_box
)
288 struct pipe_transfer
*src_trans
, *dst_trans
;
290 const uint8_t *src_map
;
291 enum pipe_format src_format
, dst_format
;
292 struct pipe_box dst_box
;
298 assert((src
->target
== PIPE_BUFFER
&& dst
->target
== PIPE_BUFFER
) ||
299 (src
->target
!= PIPE_BUFFER
&& dst
->target
!= PIPE_BUFFER
));
301 src_format
= src
->format
;
302 dst_format
= dst
->format
;
304 assert(util_format_get_blocksize(dst_format
) == util_format_get_blocksize(src_format
));
305 assert(util_format_get_blockwidth(dst_format
) == util_format_get_blockwidth(src_format
));
306 assert(util_format_get_blockheight(dst_format
) == util_format_get_blockheight(src_format
));
308 src_map
= pipe
->transfer_map(pipe
,
312 src_box
, &src_trans
);
321 dst_box
.width
= src_box
->width
;
322 dst_box
.height
= src_box
->height
;
323 dst_box
.depth
= src_box
->depth
;
325 dst_map
= pipe
->transfer_map(pipe
,
328 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD_RANGE
,
329 &dst_box
, &dst_trans
);
335 if (dst
->target
== PIPE_BUFFER
&& src
->target
== PIPE_BUFFER
) {
336 assert(src_box
->height
== 1);
337 assert(src_box
->depth
== 1);
338 memcpy(dst_map
, src_map
, src_box
->width
);
340 util_copy_box(dst_map
,
342 dst_trans
->stride
, dst_trans
->layer_stride
,
344 src_box
->width
, src_box
->height
, src_box
->depth
,
346 src_trans
->stride
, src_trans
->layer_stride
,
350 pipe
->transfer_unmap(pipe
, dst_trans
);
352 pipe
->transfer_unmap(pipe
, src_trans
);
359 #define UBYTE_TO_USHORT(B) ((B) | ((B) << 8))
363 * Fallback for pipe->clear_render_target() function.
364 * XXX this looks too hackish to be really useful.
365 * cpp > 4 looks like a gross hack at best...
366 * Plus can't use these transfer fallbacks when clearing
367 * multisampled surfaces for instance.
370 util_clear_render_target(struct pipe_context
*pipe
,
371 struct pipe_surface
*dst
,
372 const union pipe_color_union
*color
,
373 unsigned dstx
, unsigned dsty
,
374 unsigned width
, unsigned height
)
376 struct pipe_transfer
*dst_trans
;
380 assert(dst
->texture
);
383 /* XXX: should handle multiple layers */
384 dst_map
= pipe_transfer_map(pipe
,
387 dst
->u
.tex
.first_layer
,
389 dstx
, dsty
, width
, height
, &dst_trans
);
394 assert(dst_trans
->stride
> 0);
396 util_pack_color(color
->f
, dst
->texture
->format
, &uc
);
397 util_fill_rect(dst_map
, dst
->texture
->format
,
399 0, 0, width
, height
, &uc
);
401 pipe
->transfer_unmap(pipe
, dst_trans
);
406 * Fallback for pipe->clear_stencil() function.
407 * sw fallback doesn't look terribly useful here.
408 * Plus can't use these transfer fallbacks when clearing
409 * multisampled surfaces for instance.
412 util_clear_depth_stencil(struct pipe_context
*pipe
,
413 struct pipe_surface
*dst
,
414 unsigned clear_flags
,
417 unsigned dstx
, unsigned dsty
,
418 unsigned width
, unsigned height
)
420 enum pipe_format format
= dst
->format
;
421 struct pipe_transfer
*dst_trans
;
423 boolean need_rmw
= FALSE
;
425 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) &&
426 ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) != PIPE_CLEAR_DEPTHSTENCIL
) &&
427 util_format_is_depth_and_stencil(format
))
430 assert(dst
->texture
);
433 dst_map
= pipe_transfer_map(pipe
,
436 dst
->u
.tex
.first_layer
,
437 (need_rmw
? PIPE_TRANSFER_READ_WRITE
:
438 PIPE_TRANSFER_WRITE
),
439 dstx
, dsty
, width
, height
, &dst_trans
);
443 unsigned dst_stride
= dst_trans
->stride
;
444 uint64_t zstencil
= util_pack64_z_stencil(format
,
447 assert(dst_trans
->stride
> 0);
449 switch (util_format_get_blocksize(format
)) {
451 assert(format
== PIPE_FORMAT_S8_UINT
);
452 if(dst_stride
== width
)
453 memset(dst_map
, (uint8_t) zstencil
, height
* width
);
455 for (i
= 0; i
< height
; i
++) {
456 memset(dst_map
, (uint8_t) zstencil
, width
);
457 dst_map
+= dst_stride
;
462 assert(format
== PIPE_FORMAT_Z16_UNORM
);
463 for (i
= 0; i
< height
; i
++) {
464 uint16_t *row
= (uint16_t *)dst_map
;
465 for (j
= 0; j
< width
; j
++)
466 *row
++ = (uint16_t) zstencil
;
467 dst_map
+= dst_stride
;
472 for (i
= 0; i
< height
; i
++) {
473 uint32_t *row
= (uint32_t *)dst_map
;
474 for (j
= 0; j
< width
; j
++)
475 *row
++ = (uint32_t) zstencil
;
476 dst_map
+= dst_stride
;
481 if (format
== PIPE_FORMAT_Z24_UNORM_S8_UINT
)
482 dst_mask
= 0xffffff00;
484 assert(format
== PIPE_FORMAT_S8_UINT_Z24_UNORM
);
487 if (clear_flags
& PIPE_CLEAR_DEPTH
)
488 dst_mask
= ~dst_mask
;
489 for (i
= 0; i
< height
; i
++) {
490 uint32_t *row
= (uint32_t *)dst_map
;
491 for (j
= 0; j
< width
; j
++) {
492 uint32_t tmp
= *row
& dst_mask
;
493 *row
++ = tmp
| ((uint32_t) zstencil
& ~dst_mask
);
495 dst_map
+= dst_stride
;
501 for (i
= 0; i
< height
; i
++) {
502 uint64_t *row
= (uint64_t *)dst_map
;
503 for (j
= 0; j
< width
; j
++)
505 dst_map
+= dst_stride
;
511 if (clear_flags
& PIPE_CLEAR_DEPTH
)
512 src_mask
= 0x00000000ffffffffull
;
514 src_mask
= 0x000000ff00000000ull
;
516 for (i
= 0; i
< height
; i
++) {
517 uint64_t *row
= (uint64_t *)dst_map
;
518 for (j
= 0; j
< width
; j
++) {
519 uint64_t tmp
= *row
& ~src_mask
;
520 *row
++ = tmp
| (zstencil
& src_mask
);
522 dst_map
+= dst_stride
;
531 pipe
->transfer_unmap(pipe
, dst_trans
);
536 /* Return whether this is an RGBA, Z, S, or combined ZS format.
539 get_format_mask(enum pipe_format format
)
541 const struct util_format_description
*desc
= util_format_description(format
);
545 if (util_format_has_depth(desc
)) {
546 if (util_format_has_stencil(desc
)) {
552 if (util_format_has_stencil(desc
)) {
555 return PIPE_MASK_RGBA
;
560 /* Return if the box is totally inside the resource.
563 is_box_inside_resource(const struct pipe_resource
*res
,
564 const struct pipe_box
*box
,
567 unsigned width
= 1, height
= 1, depth
= 1;
569 switch (res
->target
) {
575 case PIPE_TEXTURE_1D
:
576 width
= u_minify(res
->width0
, level
);
580 case PIPE_TEXTURE_2D
:
581 case PIPE_TEXTURE_RECT
:
582 width
= u_minify(res
->width0
, level
);
583 height
= u_minify(res
->height0
, level
);
586 case PIPE_TEXTURE_3D
:
587 width
= u_minify(res
->width0
, level
);
588 height
= u_minify(res
->height0
, level
);
589 depth
= u_minify(res
->depth0
, level
);
591 case PIPE_TEXTURE_CUBE
:
592 width
= u_minify(res
->width0
, level
);
593 height
= u_minify(res
->height0
, level
);
596 case PIPE_TEXTURE_1D_ARRAY
:
597 width
= u_minify(res
->width0
, level
);
599 depth
= res
->array_size
;
601 case PIPE_TEXTURE_2D_ARRAY
:
602 width
= u_minify(res
->width0
, level
);
603 height
= u_minify(res
->height0
, level
);
604 depth
= res
->array_size
;
606 case PIPE_TEXTURE_CUBE_ARRAY
:
607 width
= u_minify(res
->width0
, level
);
608 height
= u_minify(res
->height0
, level
);
609 depth
= res
->array_size
;
610 assert(res
->array_size
% 6 == 0);
612 case PIPE_MAX_TEXTURE_TYPES
:;
615 return box
->x
>= 0 &&
616 box
->x
+ box
->width
<= (int) width
&&
618 box
->y
+ box
->height
<= (int) height
&&
620 box
->z
+ box
->depth
<= (int) depth
;
624 get_sample_count(const struct pipe_resource
*res
)
626 return res
->nr_samples
? res
->nr_samples
: 1;
630 * Try to do a blit using resource_copy_region. The function calls
631 * resource_copy_region if the blit description is compatible with it.
633 * It returns TRUE if the blit was done using resource_copy_region.
635 * It returns FALSE otherwise and the caller must fall back to a more generic
636 * codepath for the blit operation. (e.g. by using u_blitter)
639 util_try_blit_via_copy_region(struct pipe_context
*ctx
,
640 const struct pipe_blit_info
*blit
)
642 unsigned mask
= get_format_mask(blit
->dst
.format
);
644 /* No format conversions. */
645 if (blit
->src
.resource
->format
!= blit
->src
.format
||
646 blit
->dst
.resource
->format
!= blit
->dst
.format
||
647 !util_is_format_compatible(
648 util_format_description(blit
->src
.resource
->format
),
649 util_format_description(blit
->dst
.resource
->format
))) {
653 /* No masks, no filtering, no scissor. */
654 if ((blit
->mask
& mask
) != mask
||
655 blit
->filter
!= PIPE_TEX_FILTER_NEAREST
||
656 blit
->scissor_enable
) {
661 if (blit
->src
.box
.width
< 0 ||
662 blit
->src
.box
.height
< 0 ||
663 blit
->src
.box
.depth
< 0) {
668 if (blit
->src
.box
.width
!= blit
->dst
.box
.width
||
669 blit
->src
.box
.height
!= blit
->dst
.box
.height
||
670 blit
->src
.box
.depth
!= blit
->dst
.box
.depth
) {
674 /* No out-of-bounds access. */
675 if (!is_box_inside_resource(blit
->src
.resource
, &blit
->src
.box
,
677 !is_box_inside_resource(blit
->dst
.resource
, &blit
->dst
.box
,
682 /* Sample counts must match. */
683 if (get_sample_count(blit
->src
.resource
) !=
684 get_sample_count(blit
->dst
.resource
)) {
688 ctx
->resource_copy_region(ctx
, blit
->dst
.resource
, blit
->dst
.level
,
689 blit
->dst
.box
.x
, blit
->dst
.box
.y
, blit
->dst
.box
.z
,
690 blit
->src
.resource
, blit
->src
.level
,