gallium/util: move util_try_blit_via_copy_region to u_surface.c
[mesa.git] / src / gallium / auxiliary / util / u_surface.c
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
45
46 /**
47 * Initialize a pipe_surface object. 'view' is considered to have
48 * uninitialized contents.
49 */
50 void
51 u_surface_default_template(struct pipe_surface *surf,
52 const struct pipe_resource *texture,
53 unsigned bind)
54 {
55 memset(surf, 0, sizeof(*surf));
56
57 surf->format = texture->format;
58 /* XXX should filter out all non-rt/ds bind flags ? */
59 surf->usage = bind;
60 }
61
62 /**
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
67 */
68 boolean
69 util_create_rgba_texture(struct pipe_context *pipe,
70 uint width, uint height, uint bind,
71 struct pipe_resource **textureOut)
72 {
73 static const enum pipe_format rgbaFormats[] = {
74 PIPE_FORMAT_B8G8R8A8_UNORM,
75 PIPE_FORMAT_A8R8G8B8_UNORM,
76 PIPE_FORMAT_A8B8G8R8_UNORM,
77 PIPE_FORMAT_NONE
78 };
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;
84 uint i;
85
86 /* Choose surface format */
87 for (i = 0; rgbaFormats[i]; i++) {
88 if (screen->is_format_supported(screen, rgbaFormats[i],
89 target, 0, bind)) {
90 format = rgbaFormats[i];
91 break;
92 }
93 }
94 if (format == PIPE_FORMAT_NONE)
95 return FALSE; /* unable to get an rgba format!?! */
96
97 /* create texture */
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;
104 templ.depth0 = 1;
105 templ.array_size = 1;
106 templ.bind = bind;
107
108 *textureOut = screen->resource_create(screen, &templ);
109 if (!*textureOut)
110 return FALSE;
111
112 /* create surface */
113 u_surface_default_template(&surf_templ, *textureOut, bind);
114 return TRUE;
115 }
116
117
118 /**
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.
122 */
123 void
124 util_copy_rect(ubyte * dst,
125 enum pipe_format format,
126 unsigned dst_stride,
127 unsigned dst_x,
128 unsigned dst_y,
129 unsigned width,
130 unsigned height,
131 const ubyte * src,
132 int src_stride,
133 unsigned src_x,
134 unsigned src_y)
135 {
136 unsigned i;
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);
141
142 assert(blocksize > 0);
143 assert(blockwidth > 0);
144 assert(blockheight > 0);
145
146 dst_x /= blockwidth;
147 dst_y /= blockheight;
148 width = (width + blockwidth - 1)/blockwidth;
149 height = (height + blockheight - 1)/blockheight;
150 src_x /= blockwidth;
151 src_y /= blockheight;
152
153 dst += dst_x * blocksize;
154 src += src_x * blocksize;
155 dst += dst_y * dst_stride;
156 src += src_y * src_stride_pos;
157 width *= blocksize;
158
159 if (width == dst_stride && width == src_stride)
160 memcpy(dst, src, height * width);
161 else {
162 for (i = 0; i < height; i++) {
163 memcpy(dst, src, width);
164 dst += dst_stride;
165 src += src_stride;
166 }
167 }
168 }
169
170
171 /**
172 * Copy 3D box from one place to another.
173 * Position and sizes are in pixels.
174 */
175 void
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,
181 const ubyte * src,
182 int src_stride, unsigned src_slice_stride,
183 unsigned src_x, unsigned src_y, unsigned src_z)
184 {
185 unsigned z;
186 dst += dst_z * dst_slice_stride;
187 src += src_z * src_slice_stride;
188 for (z = 0; z < depth; ++z) {
189 util_copy_rect(dst,
190 format,
191 dst_stride,
192 dst_x, dst_y,
193 width, height,
194 src,
195 src_stride,
196 src_x, src_y);
197
198 dst += dst_slice_stride;
199 src += src_slice_stride;
200 }
201 }
202
203
204 void
205 util_fill_rect(ubyte * dst,
206 enum pipe_format format,
207 unsigned dst_stride,
208 unsigned dst_x,
209 unsigned dst_y,
210 unsigned width,
211 unsigned height,
212 union util_color *uc)
213 {
214 const struct util_format_description *desc = util_format_description(format);
215 unsigned i, j;
216 unsigned width_size;
217 int blocksize = desc->block.bits / 8;
218 int blockwidth = desc->block.width;
219 int blockheight = desc->block.height;
220
221 assert(blocksize > 0);
222 assert(blockwidth > 0);
223 assert(blockheight > 0);
224
225 dst_x /= blockwidth;
226 dst_y /= blockheight;
227 width = (width + blockwidth - 1)/blockwidth;
228 height = (height + blockheight - 1)/blockheight;
229
230 dst += dst_x * blocksize;
231 dst += dst_y * dst_stride;
232 width_size = width * blocksize;
233
234 switch (blocksize) {
235 case 1:
236 if(dst_stride == width_size)
237 memset(dst, uc->ub, height * width_size);
238 else {
239 for (i = 0; i < height; i++) {
240 memset(dst, uc->ub, width_size);
241 dst += dst_stride;
242 }
243 }
244 break;
245 case 2:
246 for (i = 0; i < height; i++) {
247 uint16_t *row = (uint16_t *)dst;
248 for (j = 0; j < width; j++)
249 *row++ = uc->us;
250 dst += dst_stride;
251 }
252 break;
253 case 4:
254 for (i = 0; i < height; i++) {
255 uint32_t *row = (uint32_t *)dst;
256 for (j = 0; j < width; j++)
257 *row++ = uc->ui;
258 dst += dst_stride;
259 }
260 break;
261 default:
262 for (i = 0; i < height; i++) {
263 ubyte *row = dst;
264 for (j = 0; j < width; j++) {
265 memcpy(row, uc, blocksize);
266 row += blocksize;
267 }
268 dst += dst_stride;
269 }
270 break;
271 }
272 }
273
274
275 /**
276 * Fallback function for pipe->resource_copy_region().
277 * Note: (X,Y)=(0,0) is always the upper-left corner.
278 */
279 void
280 util_resource_copy_region(struct pipe_context *pipe,
281 struct pipe_resource *dst,
282 unsigned dst_level,
283 unsigned dst_x, unsigned dst_y, unsigned dst_z,
284 struct pipe_resource *src,
285 unsigned src_level,
286 const struct pipe_box *src_box)
287 {
288 struct pipe_transfer *src_trans, *dst_trans;
289 uint8_t *dst_map;
290 const uint8_t *src_map;
291 enum pipe_format src_format, dst_format;
292 struct pipe_box dst_box;
293
294 assert(src && dst);
295 if (!src || !dst)
296 return;
297
298 assert((src->target == PIPE_BUFFER && dst->target == PIPE_BUFFER) ||
299 (src->target != PIPE_BUFFER && dst->target != PIPE_BUFFER));
300
301 src_format = src->format;
302 dst_format = dst->format;
303
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));
307
308 src_map = pipe->transfer_map(pipe,
309 src,
310 src_level,
311 PIPE_TRANSFER_READ,
312 src_box, &src_trans);
313 assert(src_map);
314 if (!src_map) {
315 goto no_src_map;
316 }
317
318 dst_box.x = dst_x;
319 dst_box.y = dst_y;
320 dst_box.z = dst_z;
321 dst_box.width = src_box->width;
322 dst_box.height = src_box->height;
323 dst_box.depth = src_box->depth;
324
325 dst_map = pipe->transfer_map(pipe,
326 dst,
327 dst_level,
328 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD_RANGE,
329 &dst_box, &dst_trans);
330 assert(dst_map);
331 if (!dst_map) {
332 goto no_dst_map;
333 }
334
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);
339 } else {
340 util_copy_box(dst_map,
341 dst_format,
342 dst_trans->stride, dst_trans->layer_stride,
343 0, 0, 0,
344 src_box->width, src_box->height, src_box->depth,
345 src_map,
346 src_trans->stride, src_trans->layer_stride,
347 0, 0, 0);
348 }
349
350 pipe->transfer_unmap(pipe, dst_trans);
351 no_dst_map:
352 pipe->transfer_unmap(pipe, src_trans);
353 no_src_map:
354 ;
355 }
356
357
358
359 #define UBYTE_TO_USHORT(B) ((B) | ((B) << 8))
360
361
362 /**
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.
368 */
369 void
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)
375 {
376 struct pipe_transfer *dst_trans;
377 void *dst_map;
378 union util_color uc;
379
380 assert(dst->texture);
381 if (!dst->texture)
382 return;
383 /* XXX: should handle multiple layers */
384 dst_map = pipe_transfer_map(pipe,
385 dst->texture,
386 dst->u.tex.level,
387 dst->u.tex.first_layer,
388 PIPE_TRANSFER_WRITE,
389 dstx, dsty, width, height, &dst_trans);
390
391 assert(dst_map);
392
393 if (dst_map) {
394 assert(dst_trans->stride > 0);
395
396 util_pack_color(color->f, dst->texture->format, &uc);
397 util_fill_rect(dst_map, dst->texture->format,
398 dst_trans->stride,
399 0, 0, width, height, &uc);
400
401 pipe->transfer_unmap(pipe, dst_trans);
402 }
403 }
404
405 /**
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.
410 */
411 void
412 util_clear_depth_stencil(struct pipe_context *pipe,
413 struct pipe_surface *dst,
414 unsigned clear_flags,
415 double depth,
416 unsigned stencil,
417 unsigned dstx, unsigned dsty,
418 unsigned width, unsigned height)
419 {
420 enum pipe_format format = dst->format;
421 struct pipe_transfer *dst_trans;
422 ubyte *dst_map;
423 boolean need_rmw = FALSE;
424
425 if ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) &&
426 ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL) &&
427 util_format_is_depth_and_stencil(format))
428 need_rmw = TRUE;
429
430 assert(dst->texture);
431 if (!dst->texture)
432 return;
433 dst_map = pipe_transfer_map(pipe,
434 dst->texture,
435 dst->u.tex.level,
436 dst->u.tex.first_layer,
437 (need_rmw ? PIPE_TRANSFER_READ_WRITE :
438 PIPE_TRANSFER_WRITE),
439 dstx, dsty, width, height, &dst_trans);
440 assert(dst_map);
441
442 if (dst_map) {
443 unsigned dst_stride = dst_trans->stride;
444 uint64_t zstencil = util_pack64_z_stencil(format,
445 depth, stencil);
446 unsigned i, j;
447 assert(dst_trans->stride > 0);
448
449 switch (util_format_get_blocksize(format)) {
450 case 1:
451 assert(format == PIPE_FORMAT_S8_UINT);
452 if(dst_stride == width)
453 memset(dst_map, (uint8_t) zstencil, height * width);
454 else {
455 for (i = 0; i < height; i++) {
456 memset(dst_map, (uint8_t) zstencil, width);
457 dst_map += dst_stride;
458 }
459 }
460 break;
461 case 2:
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;
468 }
469 break;
470 case 4:
471 if (!need_rmw) {
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;
477 }
478 }
479 else {
480 uint32_t dst_mask;
481 if (format == PIPE_FORMAT_Z24_UNORM_S8_UINT)
482 dst_mask = 0xffffff00;
483 else {
484 assert(format == PIPE_FORMAT_S8_UINT_Z24_UNORM);
485 dst_mask = 0xffffff;
486 }
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);
494 }
495 dst_map += dst_stride;
496 }
497 }
498 break;
499 case 8:
500 if (!need_rmw) {
501 for (i = 0; i < height; i++) {
502 uint64_t *row = (uint64_t *)dst_map;
503 for (j = 0; j < width; j++)
504 *row++ = zstencil;
505 dst_map += dst_stride;
506 }
507 }
508 else {
509 uint64_t src_mask;
510
511 if (clear_flags & PIPE_CLEAR_DEPTH)
512 src_mask = 0x00000000ffffffffull;
513 else
514 src_mask = 0x000000ff00000000ull;
515
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);
521 }
522 dst_map += dst_stride;
523 }
524 }
525 break;
526 default:
527 assert(0);
528 break;
529 }
530
531 pipe->transfer_unmap(pipe, dst_trans);
532 }
533 }
534
535
536 /* Return whether this is an RGBA, Z, S, or combined ZS format.
537 */
538 static unsigned
539 get_format_mask(enum pipe_format format)
540 {
541 const struct util_format_description *desc = util_format_description(format);
542
543 assert(desc);
544
545 if (util_format_has_depth(desc)) {
546 if (util_format_has_stencil(desc)) {
547 return PIPE_MASK_ZS;
548 } else {
549 return PIPE_MASK_Z;
550 }
551 } else {
552 if (util_format_has_stencil(desc)) {
553 return PIPE_MASK_S;
554 } else {
555 return PIPE_MASK_RGBA;
556 }
557 }
558 }
559
560 /* Return if the box is totally inside the resource.
561 */
562 static boolean
563 is_box_inside_resource(const struct pipe_resource *res,
564 const struct pipe_box *box,
565 unsigned level)
566 {
567 unsigned width = 1, height = 1, depth = 1;
568
569 switch (res->target) {
570 case PIPE_BUFFER:
571 width = res->width0;
572 height = 1;
573 depth = 1;
574 break;
575 case PIPE_TEXTURE_1D:
576 width = u_minify(res->width0, level);
577 height = 1;
578 depth = 1;
579 break;
580 case PIPE_TEXTURE_2D:
581 case PIPE_TEXTURE_RECT:
582 width = u_minify(res->width0, level);
583 height = u_minify(res->height0, level);
584 depth = 1;
585 break;
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);
590 break;
591 case PIPE_TEXTURE_CUBE:
592 width = u_minify(res->width0, level);
593 height = u_minify(res->height0, level);
594 depth = 6;
595 break;
596 case PIPE_TEXTURE_1D_ARRAY:
597 width = u_minify(res->width0, level);
598 height = 1;
599 depth = res->array_size;
600 break;
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;
605 break;
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);
611 break;
612 case PIPE_MAX_TEXTURE_TYPES:;
613 }
614
615 return box->x >= 0 &&
616 box->x + box->width <= (int) width &&
617 box->y >= 0 &&
618 box->y + box->height <= (int) height &&
619 box->z >= 0 &&
620 box->z + box->depth <= (int) depth;
621 }
622
623 static unsigned
624 get_sample_count(const struct pipe_resource *res)
625 {
626 return res->nr_samples ? res->nr_samples : 1;
627 }
628
629 /**
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.
632 *
633 * It returns TRUE if the blit was done using resource_copy_region.
634 *
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)
637 */
638 boolean
639 util_try_blit_via_copy_region(struct pipe_context *ctx,
640 const struct pipe_blit_info *blit)
641 {
642 unsigned mask = get_format_mask(blit->dst.format);
643
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))) {
650 return FALSE;
651 }
652
653 /* No masks, no filtering, no scissor. */
654 if ((blit->mask & mask) != mask ||
655 blit->filter != PIPE_TEX_FILTER_NEAREST ||
656 blit->scissor_enable) {
657 return FALSE;
658 }
659
660 /* No flipping. */
661 if (blit->src.box.width < 0 ||
662 blit->src.box.height < 0 ||
663 blit->src.box.depth < 0) {
664 return FALSE;
665 }
666
667 /* No scaling. */
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) {
671 return FALSE;
672 }
673
674 /* No out-of-bounds access. */
675 if (!is_box_inside_resource(blit->src.resource, &blit->src.box,
676 blit->src.level) ||
677 !is_box_inside_resource(blit->dst.resource, &blit->dst.box,
678 blit->dst.level)) {
679 return FALSE;
680 }
681
682 /* Sample counts must match. */
683 if (get_sample_count(blit->src.resource) !=
684 get_sample_count(blit->dst.resource)) {
685 return FALSE;
686 }
687
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,
691 &blit->src.box);
692 return TRUE;
693 }