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 * Fallback function for pipe->resource_copy_region().
120 * Note: (X,Y)=(0,0) is always the upper-left corner.
123 util_resource_copy_region(struct pipe_context
*pipe
,
124 struct pipe_resource
*dst
,
126 unsigned dst_x
, unsigned dst_y
, unsigned dst_z
,
127 struct pipe_resource
*src
,
129 const struct pipe_box
*src_box
)
131 struct pipe_transfer
*src_trans
, *dst_trans
;
134 enum pipe_format src_format
, dst_format
;
135 unsigned w
= src_box
->width
;
136 unsigned h
= src_box
->height
;
142 assert((src
->target
== PIPE_BUFFER
&& dst
->target
== PIPE_BUFFER
) ||
143 (src
->target
!= PIPE_BUFFER
&& dst
->target
!= PIPE_BUFFER
));
145 src_format
= src
->format
;
146 dst_format
= dst
->format
;
148 src_map
= pipe_transfer_map(pipe
,
153 src_box
->x
, src_box
->y
, w
, h
, &src_trans
);
155 dst_map
= pipe_transfer_map(pipe
,
160 dst_x
, dst_y
, w
, h
, &dst_trans
);
162 assert(util_format_get_blocksize(dst_format
) == util_format_get_blocksize(src_format
));
163 assert(util_format_get_blockwidth(dst_format
) == util_format_get_blockwidth(src_format
));
164 assert(util_format_get_blockheight(dst_format
) == util_format_get_blockheight(src_format
));
168 if (src_map
&& dst_map
) {
169 if (dst
->target
== PIPE_BUFFER
&& src
->target
== PIPE_BUFFER
) {
170 memcpy(dst_map
, src_map
, w
);
172 util_copy_rect(dst_map
,
184 pipe
->transfer_unmap(pipe
, src_trans
);
185 pipe
->transfer_unmap(pipe
, dst_trans
);
190 #define UBYTE_TO_USHORT(B) ((B) | ((B) << 8))
194 * Fallback for pipe->clear_render_target() function.
195 * XXX this looks too hackish to be really useful.
196 * cpp > 4 looks like a gross hack at best...
197 * Plus can't use these transfer fallbacks when clearing
198 * multisampled surfaces for instance.
201 util_clear_render_target(struct pipe_context
*pipe
,
202 struct pipe_surface
*dst
,
203 const union pipe_color_union
*color
,
204 unsigned dstx
, unsigned dsty
,
205 unsigned width
, unsigned height
)
207 struct pipe_transfer
*dst_trans
;
211 assert(dst
->texture
);
214 /* XXX: should handle multiple layers */
215 dst_map
= pipe_transfer_map(pipe
,
218 dst
->u
.tex
.first_layer
,
220 dstx
, dsty
, width
, height
, &dst_trans
);
225 assert(dst_trans
->stride
> 0);
227 util_pack_color(color
->f
, dst
->texture
->format
, &uc
);
228 util_fill_rect(dst_map
, dst
->texture
->format
,
230 0, 0, width
, height
, &uc
);
232 pipe
->transfer_unmap(pipe
, dst_trans
);
237 * Fallback for pipe->clear_stencil() function.
238 * sw fallback doesn't look terribly useful here.
239 * Plus can't use these transfer fallbacks when clearing
240 * multisampled surfaces for instance.
243 util_clear_depth_stencil(struct pipe_context
*pipe
,
244 struct pipe_surface
*dst
,
245 unsigned clear_flags
,
248 unsigned dstx
, unsigned dsty
,
249 unsigned width
, unsigned height
)
251 struct pipe_transfer
*dst_trans
;
253 boolean need_rmw
= FALSE
;
255 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) &&
256 ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) != PIPE_CLEAR_DEPTHSTENCIL
) &&
257 util_format_is_depth_and_stencil(dst
->format
))
260 assert(dst
->texture
);
263 dst_map
= pipe_transfer_map(pipe
,
266 dst
->u
.tex
.first_layer
,
267 (need_rmw
? PIPE_TRANSFER_READ_WRITE
:
268 PIPE_TRANSFER_WRITE
),
269 dstx
, dsty
, width
, height
, &dst_trans
);
273 unsigned dst_stride
= dst_trans
->stride
;
274 uint64_t zstencil
= util_pack64_z_stencil(dst
->texture
->format
,
277 assert(dst_trans
->stride
> 0);
279 switch (util_format_get_blocksize(dst
->format
)) {
281 assert(dst
->format
== PIPE_FORMAT_S8_UINT
);
282 if(dst_stride
== width
)
283 memset(dst_map
, (uint8_t) zstencil
, height
* width
);
285 for (i
= 0; i
< height
; i
++) {
286 memset(dst_map
, (uint8_t) zstencil
, width
);
287 dst_map
+= dst_stride
;
292 assert(dst
->format
== PIPE_FORMAT_Z16_UNORM
);
293 for (i
= 0; i
< height
; i
++) {
294 uint16_t *row
= (uint16_t *)dst_map
;
295 for (j
= 0; j
< width
; j
++)
296 *row
++ = (uint16_t) zstencil
;
297 dst_map
+= dst_stride
;
302 for (i
= 0; i
< height
; i
++) {
303 uint32_t *row
= (uint32_t *)dst_map
;
304 for (j
= 0; j
< width
; j
++)
305 *row
++ = (uint32_t) zstencil
;
306 dst_map
+= dst_stride
;
311 if (dst
->format
== PIPE_FORMAT_Z24_UNORM_S8_UINT
)
312 dst_mask
= 0xffffff00;
314 assert(dst
->format
== PIPE_FORMAT_S8_UINT_Z24_UNORM
);
317 if (clear_flags
& PIPE_CLEAR_DEPTH
)
318 dst_mask
= ~dst_mask
;
319 for (i
= 0; i
< height
; i
++) {
320 uint32_t *row
= (uint32_t *)dst_map
;
321 for (j
= 0; j
< width
; j
++) {
322 uint32_t tmp
= *row
& dst_mask
;
323 *row
++ = tmp
| ((uint32_t) zstencil
& ~dst_mask
);
325 dst_map
+= dst_stride
;
331 for (i
= 0; i
< height
; i
++) {
332 uint64_t *row
= (uint64_t *)dst_map
;
333 for (j
= 0; j
< width
; j
++)
335 dst_map
+= dst_stride
;
341 if (clear_flags
& PIPE_CLEAR_DEPTH
)
342 src_mask
= 0x00000000ffffffffull
;
344 src_mask
= 0x000000ff00000000ull
;
346 for (i
= 0; i
< height
; i
++) {
347 uint64_t *row
= (uint64_t *)dst_map
;
348 for (j
= 0; j
< width
; j
++) {
349 uint64_t tmp
= *row
& ~src_mask
;
350 *row
++ = tmp
| (zstencil
& src_mask
);
352 dst_map
+= dst_stride
;
361 pipe
->transfer_unmap(pipe
, dst_trans
);