u_surface.c revision 6dd284f7c8fac22f64c13fdf9909094f5ec59086
1/************************************************************************** 2 * 3 * Copyright 2009 VMware, Inc. All Rights Reserved. 4 * 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: 12 * 13 * The above copyright notice and this permission notice (including the 14 * next paragraph) shall be included in all copies or substantial portions 15 * of the Software. 16 * 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. 24 * 25 **************************************************************************/ 26 27/** 28 * @file 29 * Surface utility functions. 30 * 31 * @author Brian Paul 32 */ 33 34 35#include "pipe/p_defines.h" 36#include "pipe/p_screen.h" 37#include "pipe/p_state.h" 38 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" 44 45void 46u_surface_default_template(struct pipe_surface *view, 47 const struct pipe_resource *texture, 48 unsigned bind) 49{ 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 ? */ 55 view->usage = bind; 56} 57 58/** 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 63 */ 64boolean 65util_create_rgba_surface(struct pipe_context *pipe, 66 uint width, uint height, 67 uint bind, 68 struct pipe_resource **textureOut, 69 struct pipe_surface **surfaceOut) 70{ 71 static const enum pipe_format rgbaFormats[] = { 72 PIPE_FORMAT_B8G8R8A8_UNORM, 73 PIPE_FORMAT_A8R8G8B8_UNORM, 74 PIPE_FORMAT_A8B8G8R8_UNORM, 75 PIPE_FORMAT_NONE 76 }; 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; 82 uint i; 83 84 /* Choose surface format */ 85 for (i = 0; rgbaFormats[i]; i++) { 86 if (screen->is_format_supported(screen, rgbaFormats[i], 87 target, 0, bind)) { 88 format = rgbaFormats[i]; 89 break; 90 } 91 } 92 if (format == PIPE_FORMAT_NONE) 93 return FALSE; /* unable to get an rgba format!?! */ 94 95 /* create texture */ 96 memset(&templ, 0, sizeof(templ)); 97 templ.target = target; 98 templ.format = format; 99 templ.last_level = 0; 100 templ.width0 = width; 101 templ.height0 = height; 102 templ.depth0 = 1; 103 templ.array_size = 1; 104 templ.bind = bind; 105 106 *textureOut = screen->resource_create(screen, &templ); 107 if (!*textureOut) 108 return FALSE; 109 110 /* create surface */ 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, 115 *textureOut, 116 &surf_templ); 117 if (!*surfaceOut) { 118 pipe_resource_reference(textureOut, NULL); 119 return FALSE; 120 } 121 122 return TRUE; 123} 124 125 126/** 127 * Release the surface and texture from util_create_rgba_surface(). 128 */ 129void 130util_destroy_rgba_surface(struct pipe_resource *texture, 131 struct pipe_surface *surface) 132{ 133 pipe_surface_reference(&surface, NULL); 134 pipe_resource_reference(&texture, NULL); 135} 136 137 138 139/** 140 * Fallback function for pipe->resource_copy_region(). 141 * Note: (X,Y)=(0,0) is always the upper-left corner. 142 */ 143void 144util_resource_copy_region(struct pipe_context *pipe, 145 struct pipe_resource *dst, 146 unsigned dst_level, 147 unsigned dst_x, unsigned dst_y, unsigned dst_z, 148 struct pipe_resource *src, 149 unsigned src_level, 150 const struct pipe_box *src_box) 151{ 152 struct pipe_transfer *src_trans, *dst_trans; 153 void *dst_map; 154 const void *src_map; 155 enum pipe_format src_format, dst_format; 156 unsigned w = src_box->width; 157 unsigned h = src_box->height; 158 159 assert(src && dst); 160 assert((src->target == PIPE_BUFFER && dst->target == PIPE_BUFFER) || 161 (src->target != PIPE_BUFFER && dst->target != PIPE_BUFFER)); 162 163 if (!src || !dst) 164 return; 165 166 src_format = src->format; 167 dst_format = dst->format; 168 169 src_trans = pipe_get_transfer(pipe, 170 src, 171 src_level, 172 src_box->z, 173 PIPE_TRANSFER_READ, 174 src_box->x, src_box->y, w, h); 175 176 dst_trans = pipe_get_transfer(pipe, 177 dst, 178 dst_level, 179 dst_z, 180 PIPE_TRANSFER_WRITE, 181 dst_x, dst_y, w, h); 182 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)); 186 187 src_map = pipe->transfer_map(pipe, src_trans); 188 dst_map = pipe->transfer_map(pipe, dst_trans); 189 190 assert(src_map); 191 assert(dst_map); 192 193 if (src_map && dst_map) { 194 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) { 195 memcpy(dst_map, src_map, w); 196 } else { 197 util_copy_rect(dst_map, 198 dst_format, 199 dst_trans->stride, 200 0, 0, 201 w, h, 202 src_map, 203 src_trans->stride, 204 0, 205 0); 206 } 207 } 208 209 pipe->transfer_unmap(pipe, src_trans); 210 pipe->transfer_unmap(pipe, dst_trans); 211 212 pipe->transfer_destroy(pipe, src_trans); 213 pipe->transfer_destroy(pipe, dst_trans); 214} 215 216 217 218#define UBYTE_TO_USHORT(B) ((B) | ((B) << 8)) 219 220 221/** 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. 227 */ 228void 229util_clear_render_target(struct pipe_context *pipe, 230 struct pipe_surface *dst, 231 const union pipe_color_union *color, 232 unsigned dstx, unsigned dsty, 233 unsigned width, unsigned height) 234{ 235 struct pipe_transfer *dst_trans; 236 void *dst_map; 237 union util_color uc; 238 239 assert(dst->texture); 240 if (!dst->texture) 241 return; 242 /* XXX: should handle multiple layers */ 243 dst_trans = pipe_get_transfer(pipe, 244 dst->texture, 245 dst->u.tex.level, 246 dst->u.tex.first_layer, 247 PIPE_TRANSFER_WRITE, 248 dstx, dsty, width, height); 249 250 dst_map = pipe->transfer_map(pipe, dst_trans); 251 252 assert(dst_map); 253 254 if (dst_map) { 255 assert(dst_trans->stride > 0); 256 257 util_pack_color(color->f, dst->texture->format, &uc); 258 util_fill_rect(dst_map, dst->texture->format, 259 dst_trans->stride, 260 0, 0, width, height, &uc); 261 } 262 263 pipe->transfer_unmap(pipe, dst_trans); 264 pipe->transfer_destroy(pipe, dst_trans); 265} 266 267/** 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. 272 */ 273void 274util_clear_depth_stencil(struct pipe_context *pipe, 275 struct pipe_surface *dst, 276 unsigned clear_flags, 277 double depth, 278 unsigned stencil, 279 unsigned dstx, unsigned dsty, 280 unsigned width, unsigned height) 281{ 282 struct pipe_transfer *dst_trans; 283 ubyte *dst_map; 284 boolean need_rmw = FALSE; 285 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)) 289 need_rmw = TRUE; 290 291 assert(dst->texture); 292 if (!dst->texture) 293 return; 294 dst_trans = pipe_get_transfer(pipe, 295 dst->texture, 296 dst->u.tex.level, 297 dst->u.tex.first_layer, 298 (need_rmw ? PIPE_TRANSFER_READ_WRITE : 299 PIPE_TRANSFER_WRITE), 300 dstx, dsty, width, height); 301 302 dst_map = pipe->transfer_map(pipe, dst_trans); 303 304 assert(dst_map); 305 306 if (dst_map) { 307 unsigned dst_stride = dst_trans->stride; 308 unsigned zstencil = util_pack_z_stencil(dst->texture->format, depth, stencil); 309 unsigned i, j; 310 assert(dst_trans->stride > 0); 311 312 switch (util_format_get_blocksize(dst->format)) { 313 case 1: 314 assert(dst->format == PIPE_FORMAT_S8_USCALED); 315 if(dst_stride == width) 316 memset(dst_map, (ubyte) zstencil, height * width); 317 else { 318 for (i = 0; i < height; i++) { 319 memset(dst_map, (ubyte) zstencil, width); 320 dst_map += dst_stride; 321 } 322 } 323 break; 324 case 2: 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; 331 } 332 break; 333 case 4: 334 if (!need_rmw) { 335 for (i = 0; i < height; i++) { 336 uint32_t *row = (uint32_t *)dst_map; 337 for (j = 0; j < width; j++) 338 *row++ = zstencil; 339 dst_map += dst_stride; 340 } 341 } 342 else { 343 uint32_t dst_mask; 344 if (dst->format == PIPE_FORMAT_Z24_UNORM_S8_USCALED) 345 dst_mask = 0xffffff00; 346 else { 347 assert(dst->format == PIPE_FORMAT_S8_USCALED_Z24_UNORM); 348 dst_mask = 0xffffff; 349 } 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); 357 } 358 dst_map += dst_stride; 359 } 360 } 361 break; 362 case 8: 363 { 364 uint64_t zstencil = util_pack64_z_stencil(dst->texture->format, 365 depth, stencil); 366 367 assert(dst->format == PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED); 368 369 if (!need_rmw) { 370 for (i = 0; i < height; i++) { 371 uint64_t *row = (uint64_t *)dst_map; 372 for (j = 0; j < width; j++) 373 *row++ = zstencil; 374 dst_map += dst_stride; 375 } 376 } 377 else { 378 uint64_t src_mask; 379 380 if (clear_flags & PIPE_CLEAR_DEPTH) 381 src_mask = 0x00000000ffffffffull; 382 else 383 src_mask = 0x000000ff00000000ull; 384 385 for (i = 0; i < height; i++) { 386 uint64_t *row = (uint64_t *)dst_map; 387 for (j = 0; j < width; j++) { 388 uint64_t tmp = *row & ~src_mask; 389 *row++ = tmp | (zstencil & src_mask); 390 } 391 dst_map += dst_stride; 392 } 393 } 394 break; 395 } 396 default: 397 assert(0); 398 break; 399 } 400 } 401 402 pipe->transfer_unmap(pipe, dst_trans); 403 pipe->transfer_destroy(pipe, dst_trans); 404} 405