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