gallium: add CONSTBUF type to tgsi_file_type
[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 {
54 memset(surf, 0, sizeof(*surf));
55
56 surf->format = texture->format;
57 }
58
59
60 /**
61 * Copy 2D rect from one place to another.
62 * Position and sizes are in pixels.
63 * src_stride may be negative to do vertical flip of pixels from source.
64 */
65 void
66 util_copy_rect(ubyte * dst,
67 enum pipe_format format,
68 unsigned dst_stride,
69 unsigned dst_x,
70 unsigned dst_y,
71 unsigned width,
72 unsigned height,
73 const ubyte * src,
74 int src_stride,
75 unsigned src_x,
76 unsigned src_y)
77 {
78 unsigned i;
79 int src_stride_pos = src_stride < 0 ? -src_stride : src_stride;
80 int blocksize = util_format_get_blocksize(format);
81 int blockwidth = util_format_get_blockwidth(format);
82 int blockheight = util_format_get_blockheight(format);
83
84 assert(blocksize > 0);
85 assert(blockwidth > 0);
86 assert(blockheight > 0);
87
88 dst_x /= blockwidth;
89 dst_y /= blockheight;
90 width = (width + blockwidth - 1)/blockwidth;
91 height = (height + blockheight - 1)/blockheight;
92 src_x /= blockwidth;
93 src_y /= blockheight;
94
95 dst += dst_x * blocksize;
96 src += src_x * blocksize;
97 dst += dst_y * dst_stride;
98 src += src_y * src_stride_pos;
99 width *= blocksize;
100
101 if (width == dst_stride && width == src_stride)
102 memcpy(dst, src, height * width);
103 else {
104 for (i = 0; i < height; i++) {
105 memcpy(dst, src, width);
106 dst += dst_stride;
107 src += src_stride;
108 }
109 }
110 }
111
112
113 /**
114 * Copy 3D box from one place to another.
115 * Position and sizes are in pixels.
116 */
117 void
118 util_copy_box(ubyte * dst,
119 enum pipe_format format,
120 unsigned dst_stride, unsigned dst_slice_stride,
121 unsigned dst_x, unsigned dst_y, unsigned dst_z,
122 unsigned width, unsigned height, unsigned depth,
123 const ubyte * src,
124 int src_stride, unsigned src_slice_stride,
125 unsigned src_x, unsigned src_y, unsigned src_z)
126 {
127 unsigned z;
128 dst += dst_z * dst_slice_stride;
129 src += src_z * src_slice_stride;
130 for (z = 0; z < depth; ++z) {
131 util_copy_rect(dst,
132 format,
133 dst_stride,
134 dst_x, dst_y,
135 width, height,
136 src,
137 src_stride,
138 src_x, src_y);
139
140 dst += dst_slice_stride;
141 src += src_slice_stride;
142 }
143 }
144
145
146 void
147 util_fill_rect(ubyte * dst,
148 enum pipe_format format,
149 unsigned dst_stride,
150 unsigned dst_x,
151 unsigned dst_y,
152 unsigned width,
153 unsigned height,
154 union util_color *uc)
155 {
156 const struct util_format_description *desc = util_format_description(format);
157 unsigned i, j;
158 unsigned width_size;
159 int blocksize = desc->block.bits / 8;
160 int blockwidth = desc->block.width;
161 int blockheight = desc->block.height;
162
163 assert(blocksize > 0);
164 assert(blockwidth > 0);
165 assert(blockheight > 0);
166
167 dst_x /= blockwidth;
168 dst_y /= blockheight;
169 width = (width + blockwidth - 1)/blockwidth;
170 height = (height + blockheight - 1)/blockheight;
171
172 dst += dst_x * blocksize;
173 dst += dst_y * dst_stride;
174 width_size = width * blocksize;
175
176 switch (blocksize) {
177 case 1:
178 if(dst_stride == width_size)
179 memset(dst, uc->ub, height * width_size);
180 else {
181 for (i = 0; i < height; i++) {
182 memset(dst, uc->ub, width_size);
183 dst += dst_stride;
184 }
185 }
186 break;
187 case 2:
188 for (i = 0; i < height; i++) {
189 uint16_t *row = (uint16_t *)dst;
190 for (j = 0; j < width; j++)
191 *row++ = uc->us;
192 dst += dst_stride;
193 }
194 break;
195 case 4:
196 for (i = 0; i < height; i++) {
197 uint32_t *row = (uint32_t *)dst;
198 for (j = 0; j < width; j++)
199 *row++ = uc->ui[0];
200 dst += dst_stride;
201 }
202 break;
203 default:
204 for (i = 0; i < height; i++) {
205 ubyte *row = dst;
206 for (j = 0; j < width; j++) {
207 memcpy(row, uc, blocksize);
208 row += blocksize;
209 }
210 dst += dst_stride;
211 }
212 break;
213 }
214 }
215
216
217 void
218 util_fill_box(ubyte * dst,
219 enum pipe_format format,
220 unsigned stride,
221 unsigned layer_stride,
222 unsigned x,
223 unsigned y,
224 unsigned z,
225 unsigned width,
226 unsigned height,
227 unsigned depth,
228 union util_color *uc)
229 {
230 unsigned layer;
231 dst += z * layer_stride;
232 for (layer = z; layer < depth; layer++) {
233 util_fill_rect(dst, format,
234 stride,
235 x, y, width, height, uc);
236 dst += layer_stride;
237 }
238 }
239
240
241 /**
242 * Fallback function for pipe->resource_copy_region().
243 * We support copying between different formats (including compressed/
244 * uncompressed) if the bytes per block or pixel matches. If copying
245 * compressed -> uncompressed, the dst region is reduced by the block
246 * width, height. If copying uncompressed -> compressed, the dest region
247 * is expanded by the block width, height. See GL_ARB_copy_image.
248 * Note: (X,Y)=(0,0) is always the upper-left corner.
249 */
250 void
251 util_resource_copy_region(struct pipe_context *pipe,
252 struct pipe_resource *dst,
253 unsigned dst_level,
254 unsigned dst_x, unsigned dst_y, unsigned dst_z,
255 struct pipe_resource *src,
256 unsigned src_level,
257 const struct pipe_box *src_box_in)
258 {
259 struct pipe_transfer *src_trans, *dst_trans;
260 uint8_t *dst_map;
261 const uint8_t *src_map;
262 MAYBE_UNUSED enum pipe_format src_format;
263 enum pipe_format dst_format;
264 struct pipe_box src_box, dst_box;
265 unsigned src_bs, dst_bs, src_bw, dst_bw, src_bh, dst_bh;
266
267 assert(src && dst);
268 if (!src || !dst)
269 return;
270
271 assert((src->target == PIPE_BUFFER && dst->target == PIPE_BUFFER) ||
272 (src->target != PIPE_BUFFER && dst->target != PIPE_BUFFER));
273
274 src_format = src->format;
275 dst_format = dst->format;
276
277 /* init src box */
278 src_box = *src_box_in;
279
280 /* init dst box */
281 dst_box.x = dst_x;
282 dst_box.y = dst_y;
283 dst_box.z = dst_z;
284 dst_box.width = src_box.width;
285 dst_box.height = src_box.height;
286 dst_box.depth = src_box.depth;
287
288 src_bs = util_format_get_blocksize(src_format);
289 src_bw = util_format_get_blockwidth(src_format);
290 src_bh = util_format_get_blockheight(src_format);
291 dst_bs = util_format_get_blocksize(dst_format);
292 dst_bw = util_format_get_blockwidth(dst_format);
293 dst_bh = util_format_get_blockheight(dst_format);
294
295 /* Note: all box positions and sizes are in pixels */
296 if (src_bw > 1 && dst_bw == 1) {
297 /* Copy from compressed to uncompressed.
298 * Shrink dest box by the src block size.
299 */
300 dst_box.width /= src_bw;
301 dst_box.height /= src_bh;
302 }
303 else if (src_bw == 1 && dst_bw > 1) {
304 /* Copy from uncompressed to compressed.
305 * Expand dest box by the dest block size.
306 */
307 dst_box.width *= dst_bw;
308 dst_box.height *= dst_bh;
309 }
310 else {
311 /* compressed -> compressed or uncompressed -> uncompressed copy */
312 assert(src_bw == dst_bw);
313 assert(src_bh == dst_bh);
314 }
315
316 assert(src_bs == dst_bs);
317 if (src_bs != dst_bs) {
318 /* This can happen if we fail to do format checking before hand.
319 * Don't crash below.
320 */
321 return;
322 }
323
324 /* check that region boxes are block aligned */
325 assert(src_box.x % src_bw == 0);
326 assert(src_box.y % src_bh == 0);
327 assert(src_box.width % src_bw == 0 ||
328 src_box.x + src_box.width == u_minify(src->width0, src_level));
329 assert(src_box.height % src_bh == 0 ||
330 src_box.y + src_box.height == u_minify(src->height0, src_level));
331 assert(dst_box.x % dst_bw == 0);
332 assert(dst_box.y % dst_bh == 0);
333 assert(dst_box.width % dst_bw == 0 ||
334 dst_box.x + dst_box.width == u_minify(dst->width0, dst_level));
335 assert(dst_box.height % dst_bh == 0 ||
336 dst_box.y + dst_box.height == u_minify(dst->height0, dst_level));
337
338 /* check that region boxes are not out of bounds */
339 assert(src_box.x + src_box.width <= u_minify(src->width0, src_level));
340 assert(src_box.y + src_box.height <= u_minify(src->height0, src_level));
341 assert(dst_box.x + dst_box.width <= u_minify(dst->width0, dst_level));
342 assert(dst_box.y + dst_box.height <= u_minify(dst->height0, dst_level));
343
344 /* check that total number of src, dest bytes match */
345 assert((src_box.width / src_bw) * (src_box.height / src_bh) * src_bs ==
346 (dst_box.width / dst_bw) * (dst_box.height / dst_bh) * dst_bs);
347
348 src_map = pipe->transfer_map(pipe,
349 src,
350 src_level,
351 PIPE_TRANSFER_READ,
352 &src_box, &src_trans);
353 assert(src_map);
354 if (!src_map) {
355 goto no_src_map;
356 }
357
358 dst_map = pipe->transfer_map(pipe,
359 dst,
360 dst_level,
361 PIPE_TRANSFER_WRITE |
362 PIPE_TRANSFER_DISCARD_RANGE, &dst_box,
363 &dst_trans);
364 assert(dst_map);
365 if (!dst_map) {
366 goto no_dst_map;
367 }
368
369 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
370 assert(src_box.height == 1);
371 assert(src_box.depth == 1);
372 memcpy(dst_map, src_map, src_box.width);
373 } else {
374 util_copy_box(dst_map,
375 src_format,
376 dst_trans->stride, dst_trans->layer_stride,
377 0, 0, 0,
378 src_box.width, src_box.height, src_box.depth,
379 src_map,
380 src_trans->stride, src_trans->layer_stride,
381 0, 0, 0);
382 }
383
384 pipe->transfer_unmap(pipe, dst_trans);
385 no_dst_map:
386 pipe->transfer_unmap(pipe, src_trans);
387 no_src_map:
388 ;
389 }
390
391 static void
392 util_clear_color_texture_helper(struct pipe_transfer *dst_trans,
393 ubyte *dst_map,
394 enum pipe_format format,
395 const union pipe_color_union *color,
396 unsigned width, unsigned height, unsigned depth)
397 {
398 union util_color uc;
399
400 assert(dst_trans->stride > 0);
401
402 if (util_format_is_pure_integer(format)) {
403 /*
404 * We expect int/uint clear values here, though some APIs
405 * might disagree (but in any case util_pack_color()
406 * couldn't handle it)...
407 */
408 if (util_format_is_pure_sint(format)) {
409 util_format_write_4i(format, color->i, 0, &uc, 0, 0, 0, 1, 1);
410 } else {
411 assert(util_format_is_pure_uint(format));
412 util_format_write_4ui(format, color->ui, 0, &uc, 0, 0, 0, 1, 1);
413 }
414 } else {
415 util_pack_color(color->f, format, &uc);
416 }
417
418 util_fill_box(dst_map, format,
419 dst_trans->stride, dst_trans->layer_stride,
420 0, 0, 0, width, height, depth, &uc);
421 }
422
423 static void
424 util_clear_color_texture(struct pipe_context *pipe,
425 struct pipe_resource *texture,
426 enum pipe_format format,
427 const union pipe_color_union *color,
428 unsigned level,
429 unsigned dstx, unsigned dsty, unsigned dstz,
430 unsigned width, unsigned height, unsigned depth)
431 {
432 struct pipe_transfer *dst_trans;
433 ubyte *dst_map;
434
435 dst_map = pipe_transfer_map_3d(pipe,
436 texture,
437 level,
438 PIPE_TRANSFER_WRITE,
439 dstx, dsty, dstz,
440 width, height, depth,
441 &dst_trans);
442 if (!dst_map)
443 return;
444
445 if (dst_trans->stride > 0) {
446 util_clear_color_texture_helper(dst_trans, dst_map, format, color,
447 width, height, depth);
448 }
449 pipe->transfer_unmap(pipe, dst_trans);
450 }
451
452
453 #define UBYTE_TO_USHORT(B) ((B) | ((B) << 8))
454
455
456 /**
457 * Fallback for pipe->clear_render_target() function.
458 * XXX this looks too hackish to be really useful.
459 * cpp > 4 looks like a gross hack at best...
460 * Plus can't use these transfer fallbacks when clearing
461 * multisampled surfaces for instance.
462 * Clears all bound layers.
463 */
464 void
465 util_clear_render_target(struct pipe_context *pipe,
466 struct pipe_surface *dst,
467 const union pipe_color_union *color,
468 unsigned dstx, unsigned dsty,
469 unsigned width, unsigned height)
470 {
471 struct pipe_transfer *dst_trans;
472 ubyte *dst_map;
473
474 assert(dst->texture);
475 if (!dst->texture)
476 return;
477
478 if (dst->texture->target == PIPE_BUFFER) {
479 /*
480 * The fill naturally works on the surface format, however
481 * the transfer uses resource format which is just bytes for buffers.
482 */
483 unsigned dx, w;
484 unsigned pixstride = util_format_get_blocksize(dst->format);
485 dx = (dst->u.buf.first_element + dstx) * pixstride;
486 w = width * pixstride;
487 dst_map = pipe_transfer_map(pipe,
488 dst->texture,
489 0, 0,
490 PIPE_TRANSFER_WRITE,
491 dx, 0, w, 1,
492 &dst_trans);
493 if (dst_map) {
494 util_clear_color_texture_helper(dst_trans, dst_map, dst->format,
495 color, width, height, 1);
496 pipe->transfer_unmap(pipe, dst_trans);
497 }
498 }
499 else {
500 unsigned depth = dst->u.tex.last_layer - dst->u.tex.first_layer + 1;
501 util_clear_color_texture(pipe, dst->texture, dst->format, color,
502 dst->u.tex.level, dstx, dsty,
503 dst->u.tex.first_layer, width, height, depth);
504 }
505 }
506
507 static void
508 util_clear_depth_stencil_texture(struct pipe_context *pipe,
509 struct pipe_resource *texture,
510 enum pipe_format format,
511 unsigned clear_flags,
512 uint64_t zstencil, unsigned level,
513 unsigned dstx, unsigned dsty, unsigned dstz,
514 unsigned width, unsigned height, unsigned depth)
515 {
516 struct pipe_transfer *dst_trans;
517 ubyte *dst_map;
518 boolean need_rmw = FALSE;
519 unsigned dst_stride;
520 ubyte *dst_layer;
521 unsigned i, j, layer;
522
523 if ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) &&
524 ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL) &&
525 util_format_is_depth_and_stencil(format))
526 need_rmw = TRUE;
527
528 dst_map = pipe_transfer_map_3d(pipe,
529 texture,
530 level,
531 (need_rmw ? PIPE_TRANSFER_READ_WRITE :
532 PIPE_TRANSFER_WRITE),
533 dstx, dsty, dstz,
534 width, height, depth, &dst_trans);
535 assert(dst_map);
536 if (!dst_map)
537 return;
538
539 dst_stride = dst_trans->stride;
540 dst_layer = dst_map;
541 assert(dst_trans->stride > 0);
542
543 for (layer = 0; layer < depth; layer++) {
544 dst_map = dst_layer;
545
546 switch (util_format_get_blocksize(format)) {
547 case 1:
548 assert(format == PIPE_FORMAT_S8_UINT);
549 if(dst_stride == width)
550 memset(dst_map, (uint8_t) zstencil, height * width);
551 else {
552 for (i = 0; i < height; i++) {
553 memset(dst_map, (uint8_t) zstencil, width);
554 dst_map += dst_stride;
555 }
556 }
557 break;
558 case 2:
559 assert(format == PIPE_FORMAT_Z16_UNORM);
560 for (i = 0; i < height; i++) {
561 uint16_t *row = (uint16_t *)dst_map;
562 for (j = 0; j < width; j++)
563 *row++ = (uint16_t) zstencil;
564 dst_map += dst_stride;
565 }
566 break;
567 case 4:
568 if (!need_rmw) {
569 for (i = 0; i < height; i++) {
570 uint32_t *row = (uint32_t *)dst_map;
571 for (j = 0; j < width; j++)
572 *row++ = (uint32_t) zstencil;
573 dst_map += dst_stride;
574 }
575 }
576 else {
577 uint32_t dst_mask;
578 if (format == PIPE_FORMAT_Z24_UNORM_S8_UINT)
579 dst_mask = 0x00ffffff;
580 else {
581 assert(format == PIPE_FORMAT_S8_UINT_Z24_UNORM);
582 dst_mask = 0xffffff00;
583 }
584 if (clear_flags & PIPE_CLEAR_DEPTH)
585 dst_mask = ~dst_mask;
586 for (i = 0; i < height; i++) {
587 uint32_t *row = (uint32_t *)dst_map;
588 for (j = 0; j < width; j++) {
589 uint32_t tmp = *row & dst_mask;
590 *row++ = tmp | ((uint32_t) zstencil & ~dst_mask);
591 }
592 dst_map += dst_stride;
593 }
594 }
595 break;
596 case 8:
597 if (!need_rmw) {
598 for (i = 0; i < height; i++) {
599 uint64_t *row = (uint64_t *)dst_map;
600 for (j = 0; j < width; j++)
601 *row++ = zstencil;
602 dst_map += dst_stride;
603 }
604 }
605 else {
606 uint64_t src_mask;
607
608 if (clear_flags & PIPE_CLEAR_DEPTH)
609 src_mask = 0x00000000ffffffffull;
610 else
611 src_mask = 0x000000ff00000000ull;
612
613 for (i = 0; i < height; i++) {
614 uint64_t *row = (uint64_t *)dst_map;
615 for (j = 0; j < width; j++) {
616 uint64_t tmp = *row & ~src_mask;
617 *row++ = tmp | (zstencil & src_mask);
618 }
619 dst_map += dst_stride;
620 }
621 }
622 break;
623 default:
624 assert(0);
625 break;
626 }
627 dst_layer += dst_trans->layer_stride;
628 }
629
630 pipe->transfer_unmap(pipe, dst_trans);
631 }
632
633
634 void
635 util_clear_texture(struct pipe_context *pipe,
636 struct pipe_resource *tex,
637 unsigned level,
638 const struct pipe_box *box,
639 const void *data)
640 {
641 const struct util_format_description *desc =
642 util_format_description(tex->format);
643
644 if (level > tex->last_level)
645 return;
646
647 if (util_format_is_depth_or_stencil(tex->format)) {
648 unsigned clear = 0;
649 float depth = 0.0f;
650 uint8_t stencil = 0;
651 uint64_t zstencil;
652
653 if (util_format_has_depth(desc)) {
654 clear |= PIPE_CLEAR_DEPTH;
655 desc->unpack_z_float(&depth, 0, data, 0, 1, 1);
656 }
657
658 if (util_format_has_stencil(desc)) {
659 clear |= PIPE_CLEAR_STENCIL;
660 desc->unpack_s_8uint(&stencil, 0, data, 0, 1, 1);
661 }
662
663 zstencil = util_pack64_z_stencil(tex->format, depth, stencil);
664
665 util_clear_depth_stencil_texture(pipe, tex, tex->format, clear, zstencil,
666 level, box->x, box->y, box->z,
667 box->width, box->height, box->depth);
668 } else {
669 union pipe_color_union color;
670 if (util_format_is_pure_uint(tex->format))
671 desc->unpack_rgba_uint(color.ui, 0, data, 0, 1, 1);
672 else if (util_format_is_pure_sint(tex->format))
673 desc->unpack_rgba_sint(color.i, 0, data, 0, 1, 1);
674 else
675 desc->unpack_rgba_float(color.f, 0, data, 0, 1, 1);
676
677 util_clear_color_texture(pipe, tex, tex->format, &color, level,
678 box->x, box->y, box->z,
679 box->width, box->height, box->depth);
680 }
681 }
682
683
684 /**
685 * Fallback for pipe->clear_stencil() function.
686 * sw fallback doesn't look terribly useful here.
687 * Plus can't use these transfer fallbacks when clearing
688 * multisampled surfaces for instance.
689 * Clears all bound layers.
690 */
691 void
692 util_clear_depth_stencil(struct pipe_context *pipe,
693 struct pipe_surface *dst,
694 unsigned clear_flags,
695 double depth,
696 unsigned stencil,
697 unsigned dstx, unsigned dsty,
698 unsigned width, unsigned height)
699 {
700 uint64_t zstencil;
701 unsigned max_layer;
702
703 assert(dst->texture);
704 if (!dst->texture)
705 return;
706
707 zstencil = util_pack64_z_stencil(dst->format, depth, stencil);
708 max_layer = dst->u.tex.last_layer - dst->u.tex.first_layer;
709 util_clear_depth_stencil_texture(pipe, dst->texture, dst->format,
710 clear_flags, zstencil, dst->u.tex.level,
711 dstx, dsty, dst->u.tex.first_layer,
712 width, height, max_layer + 1);
713 }
714
715
716 /* Return if the box is totally inside the resource.
717 */
718 static boolean
719 is_box_inside_resource(const struct pipe_resource *res,
720 const struct pipe_box *box,
721 unsigned level)
722 {
723 unsigned width = 1, height = 1, depth = 1;
724
725 switch (res->target) {
726 case PIPE_BUFFER:
727 width = res->width0;
728 height = 1;
729 depth = 1;
730 break;
731 case PIPE_TEXTURE_1D:
732 width = u_minify(res->width0, level);
733 height = 1;
734 depth = 1;
735 break;
736 case PIPE_TEXTURE_2D:
737 case PIPE_TEXTURE_RECT:
738 width = u_minify(res->width0, level);
739 height = u_minify(res->height0, level);
740 depth = 1;
741 break;
742 case PIPE_TEXTURE_3D:
743 width = u_minify(res->width0, level);
744 height = u_minify(res->height0, level);
745 depth = u_minify(res->depth0, level);
746 break;
747 case PIPE_TEXTURE_CUBE:
748 width = u_minify(res->width0, level);
749 height = u_minify(res->height0, level);
750 depth = 6;
751 break;
752 case PIPE_TEXTURE_1D_ARRAY:
753 width = u_minify(res->width0, level);
754 height = 1;
755 depth = res->array_size;
756 break;
757 case PIPE_TEXTURE_2D_ARRAY:
758 width = u_minify(res->width0, level);
759 height = u_minify(res->height0, level);
760 depth = res->array_size;
761 break;
762 case PIPE_TEXTURE_CUBE_ARRAY:
763 width = u_minify(res->width0, level);
764 height = u_minify(res->height0, level);
765 depth = res->array_size;
766 assert(res->array_size % 6 == 0);
767 break;
768 case PIPE_MAX_TEXTURE_TYPES:
769 break;
770 }
771
772 return box->x >= 0 &&
773 box->x + box->width <= (int) width &&
774 box->y >= 0 &&
775 box->y + box->height <= (int) height &&
776 box->z >= 0 &&
777 box->z + box->depth <= (int) depth;
778 }
779
780 static unsigned
781 get_sample_count(const struct pipe_resource *res)
782 {
783 return res->nr_samples ? res->nr_samples : 1;
784 }
785
786
787 /**
788 * Check if a blit() command can be implemented with a resource_copy_region().
789 * If tight_format_check is true, only allow the resource_copy_region() if
790 * the blit src/dst formats are identical, ignoring the resource formats.
791 * Otherwise, check for format casting and compatibility.
792 */
793 boolean
794 util_can_blit_via_copy_region(const struct pipe_blit_info *blit,
795 boolean tight_format_check)
796 {
797 const struct util_format_description *src_desc, *dst_desc;
798
799 src_desc = util_format_description(blit->src.resource->format);
800 dst_desc = util_format_description(blit->dst.resource->format);
801
802 if (tight_format_check) {
803 /* no format conversions allowed */
804 if (blit->src.format != blit->dst.format) {
805 return FALSE;
806 }
807 }
808 else {
809 /* do loose format compatibility checking */
810 if (blit->src.resource->format != blit->src.format ||
811 blit->dst.resource->format != blit->dst.format ||
812 !util_is_format_compatible(src_desc, dst_desc)) {
813 return FALSE;
814 }
815 }
816
817 unsigned mask = util_format_get_mask(blit->dst.format);
818
819 /* No masks, no filtering, no scissor, no blending */
820 if ((blit->mask & mask) != mask ||
821 blit->filter != PIPE_TEX_FILTER_NEAREST ||
822 blit->scissor_enable ||
823 blit->num_window_rectangles > 0 ||
824 blit->alpha_blend) {
825 return FALSE;
826 }
827
828 /* Only the src box can have negative dims for flipping */
829 assert(blit->dst.box.width >= 1);
830 assert(blit->dst.box.height >= 1);
831 assert(blit->dst.box.depth >= 1);
832
833 /* No scaling or flipping */
834 if (blit->src.box.width != blit->dst.box.width ||
835 blit->src.box.height != blit->dst.box.height ||
836 blit->src.box.depth != blit->dst.box.depth) {
837 return FALSE;
838 }
839
840 /* No out-of-bounds access. */
841 if (!is_box_inside_resource(blit->src.resource, &blit->src.box,
842 blit->src.level) ||
843 !is_box_inside_resource(blit->dst.resource, &blit->dst.box,
844 blit->dst.level)) {
845 return FALSE;
846 }
847
848 /* Sample counts must match. */
849 if (get_sample_count(blit->src.resource) !=
850 get_sample_count(blit->dst.resource)) {
851 return FALSE;
852 }
853
854 return TRUE;
855 }
856
857
858 /**
859 * Try to do a blit using resource_copy_region. The function calls
860 * resource_copy_region if the blit description is compatible with it.
861 *
862 * It returns TRUE if the blit was done using resource_copy_region.
863 *
864 * It returns FALSE otherwise and the caller must fall back to a more generic
865 * codepath for the blit operation. (e.g. by using u_blitter)
866 */
867 boolean
868 util_try_blit_via_copy_region(struct pipe_context *ctx,
869 const struct pipe_blit_info *blit)
870 {
871 if (util_can_blit_via_copy_region(blit, FALSE)) {
872 ctx->resource_copy_region(ctx, blit->dst.resource, blit->dst.level,
873 blit->dst.box.x, blit->dst.box.y,
874 blit->dst.box.z,
875 blit->src.resource, blit->src.level,
876 &blit->src.box);
877 return TRUE;
878 }
879 else {
880 return FALSE;
881 }
882 }