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"
46 u_surface_default_template(struct pipe_surface *view,
47 const struct pipe_resource *texture,
50 view->format = texture->format;
51 view->u.tex.level = 0;
52 view->u.tex.first_layer = 0;
53 view->u.tex.last_layer = 0;
54 /* XXX should filter out all non-rt/ds bind flags ? */
59 * Helper to quickly create an RGBA rendering surface of a certain size.
60 * \param textureOut returns the new texture
61 * \param surfaceOut returns the new surface
62 * \return TRUE for success, FALSE if failure
65 util_create_rgba_surface(struct pipe_context *pipe,
66 uint width, uint height,
68 struct pipe_resource **textureOut,
69 struct pipe_surface **surfaceOut)
71 static const enum pipe_format rgbaFormats[] = {
72 PIPE_FORMAT_B8G8R8A8_UNORM,
73 PIPE_FORMAT_A8R8G8B8_UNORM,
74 PIPE_FORMAT_A8B8G8R8_UNORM,
77 const uint target = PIPE_TEXTURE_2D;
78 enum pipe_format format = PIPE_FORMAT_NONE;
79 struct pipe_resource templ;
80 struct pipe_surface surf_templ;
81 struct pipe_screen *screen = pipe->screen;
84 /* Choose surface format */
85 for (i = 0; rgbaFormats[i]; i++) {
86 if (screen->is_format_supported(screen, rgbaFormats[i],
88 format = rgbaFormats[i];
92 if (format == PIPE_FORMAT_NONE)
93 return FALSE; /* unable to get an rgba format!?! */
96 memset(&templ, 0, sizeof(templ));
97 templ.target = target;
98 templ.format = format;
100 templ.width0 = width;
101 templ.height0 = height;
103 templ.array_size = 1;
106 *textureOut = screen->resource_create(screen, &templ);
111 memset(&surf_templ, 0, sizeof(surf_templ));
112 u_surface_default_template(&surf_templ, *textureOut, bind);
113 /* create surface / view into texture */
114 *surfaceOut = pipe->create_surface(pipe,
118 pipe_resource_reference(textureOut, NULL);
127 * Release the surface and texture from util_create_rgba_surface().
130 util_destroy_rgba_surface(struct pipe_resource *texture,
131 struct pipe_surface *surface)
133 pipe_surface_reference(&surface, NULL);
134 pipe_resource_reference(&texture, NULL);
140 * Fallback function for pipe->resource_copy_region().
141 * Note: (X,Y)=(0,0) is always the upper-left corner.
144 util_resource_copy_region(struct pipe_context *pipe,
145 struct pipe_resource *dst,
147 unsigned dst_x, unsigned dst_y, unsigned dst_z,
148 struct pipe_resource *src,
150 const struct pipe_box *src_box)
152 struct pipe_transfer *src_trans, *dst_trans;
155 enum pipe_format src_format, dst_format;
156 unsigned w = src_box->width;
157 unsigned h = src_box->height;
160 assert((src->target == PIPE_BUFFER && dst->target == PIPE_BUFFER) ||
161 (src->target != PIPE_BUFFER && dst->target != PIPE_BUFFER));
166 src_format = src->format;
167 dst_format = dst->format;
169 src_trans = pipe_get_transfer(pipe,
174 src_box->x, src_box->y, w, h);
176 dst_trans = pipe_get_transfer(pipe,
183 assert(util_format_get_blocksize(dst_format) == util_format_get_blocksize(src_format));
184 assert(util_format_get_blockwidth(dst_format) == util_format_get_blockwidth(src_format));
185 assert(util_format_get_blockheight(dst_format) == util_format_get_blockheight(src_format));
187 src_map = pipe->transfer_map(pipe, src_trans);
188 dst_map = pipe->transfer_map(pipe, dst_trans);
193 if (src_map && dst_map) {
194 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
195 memcpy(dst_map, src_map, w);
197 util_copy_rect(dst_map,
209 pipe->transfer_unmap(pipe, src_trans);
210 pipe->transfer_unmap(pipe, dst_trans);
212 pipe->transfer_destroy(pipe, src_trans);
213 pipe->transfer_destroy(pipe, dst_trans);
218 #define UBYTE_TO_USHORT(B) ((B) | ((B) << 8))
222 * Fallback for pipe->clear_render_target() function.
223 * XXX this looks too hackish to be really useful.
224 * cpp > 4 looks like a gross hack at best...
225 * Plus can't use these transfer fallbacks when clearing
226 * multisampled surfaces for instance.
229 util_clear_render_target(struct pipe_context *pipe,
230 struct pipe_surface *dst,
232 unsigned dstx, unsigned dsty,
233 unsigned width, unsigned height)
235 struct pipe_transfer *dst_trans;
239 assert(dst->texture);
242 /* XXX: should handle multiple layers */
243 dst_trans = pipe_get_transfer(pipe,
246 dst->u.tex.first_layer,
248 dstx, dsty, width, height);
250 dst_map = pipe->transfer_map(pipe, dst_trans);
255 assert(dst_trans->stride > 0);
257 util_pack_color(rgba, dst->texture->format, &uc);
258 util_fill_rect(dst_map, dst->texture->format,
260 0, 0, width, height, &uc);
263 pipe->transfer_unmap(pipe, dst_trans);
264 pipe->transfer_destroy(pipe, dst_trans);
268 * Fallback for pipe->clear_stencil() function.
269 * sw fallback doesn't look terribly useful here.
270 * Plus can't use these transfer fallbacks when clearing
271 * multisampled surfaces for instance.
274 util_clear_depth_stencil(struct pipe_context *pipe,
275 struct pipe_surface *dst,
276 unsigned clear_flags,
279 unsigned dstx, unsigned dsty,
280 unsigned width, unsigned height)
282 struct pipe_transfer *dst_trans;
284 boolean need_rmw = FALSE;
286 if ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) &&
287 ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL) &&
288 util_format_is_depth_and_stencil(dst->format))
291 assert(dst->texture);
294 dst_trans = pipe_get_transfer(pipe,
297 dst->u.tex.first_layer,
298 (need_rmw ? PIPE_TRANSFER_READ_WRITE :
299 PIPE_TRANSFER_WRITE),
300 dstx, dsty, width, height);
302 dst_map = pipe->transfer_map(pipe, dst_trans);
307 unsigned dst_stride = dst_trans->stride;
308 unsigned zstencil = util_pack_z_stencil(dst->texture->format, depth, stencil);
310 assert(dst_trans->stride > 0);
312 switch (util_format_get_blocksize(dst->format)) {
314 assert(dst->format == PIPE_FORMAT_S8_USCALED);
315 if(dst_stride == width)
316 memset(dst_map, (ubyte) zstencil, height * width);
318 for (i = 0; i < height; i++) {
319 memset(dst_map, (ubyte) zstencil, width);
320 dst_map += dst_stride;
325 assert(dst->format == PIPE_FORMAT_Z16_UNORM);
326 for (i = 0; i < height; i++) {
327 uint16_t *row = (uint16_t *)dst_map;
328 for (j = 0; j < width; j++)
329 *row++ = (uint16_t) zstencil;
330 dst_map += dst_stride;
335 for (i = 0; i < height; i++) {
336 uint32_t *row = (uint32_t *)dst_map;
337 for (j = 0; j < width; j++)
339 dst_map += dst_stride;
344 if (dst->format == PIPE_FORMAT_Z24_UNORM_S8_USCALED)
345 dst_mask = 0xffffff00;
347 assert(dst->format == PIPE_FORMAT_S8_USCALED_Z24_UNORM);
350 if (clear_flags & PIPE_CLEAR_DEPTH)
351 dst_mask = ~dst_mask;
352 for (i = 0; i < height; i++) {
353 uint32_t *row = (uint32_t *)dst_map;
354 for (j = 0; j < width; j++) {
355 uint32_t tmp = *row & dst_mask;
356 *row++ = tmp | (zstencil & ~dst_mask);
358 dst_map += dst_stride;
369 pipe->transfer_unmap(pipe, dst_trans);
370 pipe->transfer_destroy(pipe, dst_trans);