svga: add support for cubemap array
[mesa.git] / src / gallium / drivers / svga / svga_pipe_blit.c
1 /**********************************************************
2 * Copyright 2008-2017 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #include "svga_context.h"
27 #include "svga_debug.h"
28 #include "svga_cmd.h"
29 #include "svga_format.h"
30 #include "svga_resource_buffer.h"
31 #include "svga_resource_texture.h"
32 #include "svga_surface.h"
33
34 //#include "util/u_blit_sw.h"
35 #include "util/u_format.h"
36 #include "util/u_surface.h"
37
38 #define FILE_DEBUG_FLAG DEBUG_BLIT
39
40
41 /**
42 * Build a struct pipe_blit_info object from the arguments used by the
43 * pipe::resource_copy_region() function.
44 */
45 static void
46 build_blit_info(struct pipe_resource *dst_tex,
47 unsigned dst_level,
48 unsigned dst_x,
49 unsigned dst_y,
50 unsigned dst_z,
51 struct pipe_resource *src_tex,
52 unsigned src_level,
53 const struct pipe_box *src_box,
54 struct pipe_blit_info *blit)
55 {
56 memset(blit, 0, sizeof(*blit));
57
58 blit->src.format = src_tex->format;
59 blit->dst.format = dst_tex->format;
60
61 blit->mask = util_format_get_mask(blit->dst.format);
62 blit->filter = PIPE_TEX_FILTER_NEAREST;
63 blit->src.resource = src_tex;
64 blit->src.level = src_level;
65 blit->dst.resource = dst_tex;
66 blit->dst.level = dst_level;
67 blit->src.box = *src_box;
68 u_box_3d(dst_x, dst_y, dst_z, src_box->width, src_box->height,
69 src_box->depth, &blit->dst.box);
70 }
71
72
73 /**
74 * Copy when src texture and dst texture are same with IntraSurfaceCopy
75 * command.
76 */
77 static void
78 intra_surface_copy(struct svga_context *svga, struct pipe_resource *tex,
79 unsigned src_x, unsigned src_y, unsigned src_z,
80 unsigned level, unsigned layer_face,
81 unsigned dst_x, unsigned dst_y, unsigned dst_z,
82 unsigned width, unsigned height, unsigned depth)
83 {
84 enum pipe_error ret;
85 SVGA3dCopyBox box;
86 struct svga_texture *stex;
87
88 stex = svga_texture(tex);
89
90 box.x = dst_x;
91 box.y = dst_y;
92 box.z = dst_z;
93 box.w = width;
94 box.h = height;
95 box.d = depth;
96 box.srcx = src_x;
97 box.srcy = src_y;
98 box.srcz = src_z;
99
100 ret = SVGA3D_vgpu10_IntraSurfaceCopy(svga->swc,
101 stex->handle, level, layer_face, &box);
102 if (ret != PIPE_OK) {
103 svga_context_flush(svga, NULL);
104 ret = SVGA3D_vgpu10_IntraSurfaceCopy(svga->swc,
105 stex->handle, level, layer_face, &box);
106 assert(ret == PIPE_OK);
107 }
108
109 /* Mark the texture subresource as rendered-to. */
110 svga_set_texture_rendered_to(stex, layer_face, level);
111 }
112
113 /**
114 * Copy an image between textures with the vgpu10 CopyRegion command.
115 */
116 static void
117 copy_region_vgpu10(struct svga_context *svga, struct pipe_resource *src_tex,
118 unsigned src_x, unsigned src_y, unsigned src_z,
119 unsigned src_level, unsigned src_layer_face,
120 struct pipe_resource *dst_tex,
121 unsigned dst_x, unsigned dst_y, unsigned dst_z,
122 unsigned dst_level, unsigned dst_layer_face,
123 unsigned width, unsigned height, unsigned depth)
124 {
125 enum pipe_error ret;
126 uint32 srcSubResource, dstSubResource;
127 struct svga_texture *dtex, *stex;
128 SVGA3dCopyBox box;
129
130 stex = svga_texture(src_tex);
131 dtex = svga_texture(dst_tex);
132
133 svga_surfaces_flush(svga);
134
135 box.x = dst_x;
136 box.y = dst_y;
137 box.z = dst_z;
138 box.w = width;
139 box.h = height;
140 box.d = depth;
141 box.srcx = src_x;
142 box.srcy = src_y;
143 box.srcz = src_z;
144
145 srcSubResource = src_layer_face * (src_tex->last_level + 1) + src_level;
146 dstSubResource = dst_layer_face * (dst_tex->last_level + 1) + dst_level;
147
148 ret = SVGA3D_vgpu10_PredCopyRegion(svga->swc,
149 dtex->handle, dstSubResource,
150 stex->handle, srcSubResource, &box);
151 if (ret != PIPE_OK) {
152 svga_context_flush(svga, NULL);
153 ret = SVGA3D_vgpu10_PredCopyRegion(svga->swc,
154 dtex->handle, dstSubResource,
155 stex->handle, srcSubResource, &box);
156 assert(ret == PIPE_OK);
157 }
158
159 /* Mark the texture subresource as defined. */
160 svga_define_texture_level(dtex, dst_layer_face, dst_level);
161
162 /* Mark the texture subresource as rendered-to. */
163 svga_set_texture_rendered_to(dtex, dst_layer_face, dst_level);
164 }
165
166
167 /**
168 * Fallback to the copy region utility which uses map/memcpy for the copy
169 */
170 static void
171 copy_region_fallback(struct svga_context *svga,
172 struct pipe_resource *dst_tex, unsigned dst_level,
173 unsigned dstx, unsigned dsty, unsigned dstz,
174 struct pipe_resource *src_tex, unsigned src_level,
175 const struct pipe_box *src_box)
176 {
177 struct svga_winsys_screen *sws = svga_screen(svga->pipe.screen)->sws;
178
179 SVGA_STATS_TIME_PUSH(sws, SVGA_STATS_TIME_COPYREGIONFALLBACK);
180 util_resource_copy_region(&svga->pipe, dst_tex, dst_level, dstx,
181 dsty, dstz, src_tex, src_level, src_box);
182 SVGA_STATS_TIME_POP(sws);
183 (void) sws;
184 }
185
186
187 /**
188 * Whether the layer_face index is given by the Z coordinate.
189 */
190 static bool
191 has_layer_face_index_in_z(enum pipe_texture_target target)
192 {
193 if (target == PIPE_TEXTURE_CUBE ||
194 target == PIPE_TEXTURE_1D_ARRAY ||
195 target == PIPE_TEXTURE_2D_ARRAY ||
196 target == PIPE_TEXTURE_CUBE_ARRAY)
197 return true;
198 else
199 return false;
200 }
201
202
203 /**
204 * For some texture types, we need to move the z (slice) coordinate
205 * to the layer value. For example, to select the z=3 slice of a 2D ARRAY
206 * texture, we need to use layer=3 and set z=0.
207 */
208 static void
209 adjust_z_layer(enum pipe_texture_target target,
210 int z_in, unsigned *layer_out, unsigned *z_out)
211 {
212 if (target == PIPE_TEXTURE_CUBE ||
213 target == PIPE_TEXTURE_1D_ARRAY ||
214 target == PIPE_TEXTURE_2D_ARRAY ||
215 target == PIPE_TEXTURE_CUBE_ARRAY) {
216 *layer_out = z_in;
217 *z_out = 0;
218 }
219 else {
220 *layer_out = 0;
221 *z_out = z_in;
222 }
223 }
224
225
226 /**
227 * Are the given SVGA3D formats compatible, in terms of vgpu10's
228 * PredCopyRegion() command?
229 */
230 static bool
231 formats_compatible(const struct svga_screen *ss,
232 SVGA3dSurfaceFormat src_svga_fmt,
233 SVGA3dSurfaceFormat dst_svga_fmt)
234 {
235 src_svga_fmt = svga_typeless_format(src_svga_fmt);
236 dst_svga_fmt = svga_typeless_format(dst_svga_fmt);
237
238 return src_svga_fmt == dst_svga_fmt;
239 }
240
241
242 /**
243 * Check whether the blending is enabled or not
244 */
245 static bool
246 is_blending_enabled(struct svga_context *svga,
247 const struct pipe_blit_info *blit)
248 {
249 bool blend_enable = false;
250 int i;
251 if (svga->curr.blend) {
252 if (svga->curr.blend->independent_blend_enable) {
253 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
254 if (svga->curr.framebuffer.cbufs[i]->texture == blit->dst.resource) {
255 if (svga->curr.blend->rt[i].blend_enable) {
256 blend_enable = true;
257 }
258 break;
259 }
260 }
261 }
262 else {
263 if (svga->curr.blend->rt[0].blend_enable)
264 blend_enable = true;
265 }
266 }
267 return blend_enable;
268 }
269
270
271 /**
272 * If GL_FRAMEBUFFER_SRGB is enabled, then output colorspace is
273 * expected to be sRGB if blending is not enabled.
274 * If GL_FRAMEBUFFER_SRGB is disabled, then we can use
275 * copy_region_vgpu10()
276 * Following table basically tells when copy_region_vgpu10 can be
277 * used if GL_FRAMEBUFFER_SRGB is enabled.
278 * ______________________________________________________________
279 * | src fmt | dst_fmt | blending |Can use |
280 * | | | |copy_region |
281 * ______________________________________________________________
282 * | linear | linear | N | Y |
283 * | linear | linear | Y | Y |
284 * | linear | sRGB | N | N |
285 * | linear | sRGB | Y | Y |
286 * | sRGB | linear | N | N |
287 * | sRGB | linear | Y | N |
288 * | sRGB | sRGB | N | Y |
289 * | sRGB | sRGB | Y | N |
290 * ______________________________________________________________
291 *
292 */
293 static bool
294 check_blending_and_srgb_cond(struct svga_context *svga,
295 const struct pipe_blit_info *blit)
296 {
297 enum pipe_format sFmt = blit->src.format;
298 enum pipe_format dFmt = blit->dst.format;
299
300 if (is_blending_enabled(svga, blit)) {
301 if (!util_format_is_srgb(blit->src.format))
302 return true;
303 }
304 else {
305 if (util_format_is_srgb(sFmt) && util_format_is_srgb(dFmt))
306 return true;
307 else if (!util_format_is_srgb(sFmt)){
308 if (!util_format_is_srgb(dFmt))
309 return true;
310 else {
311 /**
312 * State tracker converts all sRGB src blit format
313 * to linear if GL_FRAMEBUFFER_SRGB is disabled.
314 * So if src resource format is sRGB and
315 * blit format is linear then it means,
316 * GL_FRAMEBUFFER_SRGB is disabled. In this case also
317 * we can use copy_region_vgpu10().
318 */
319
320 if (util_format_is_srgb(blit->src.resource->format))
321 return true;
322 }
323 }
324 }
325 return false;
326 }
327
328 /**
329 * Do common checks for svga surface copy.
330 */
331 static bool
332 can_blit_via_svga_copy_region(struct svga_context *svga,
333 const struct pipe_blit_info *blit_info)
334 {
335 struct pipe_blit_info local_blit = *blit_info;
336
337 /* First basic checks to catch incompatibilities in new or locally unchecked
338 * struct pipe_blit_info members but bypass the format check here.
339 * Also since util_can_blit_via_copy_region() requires a dimension match,
340 * PIPE_FILTER_LINEAR should be equal to PIPE_FILTER_NEAREST.
341 */
342 local_blit.dst.format = local_blit.src.format;
343 if (local_blit.filter == PIPE_TEX_FILTER_LINEAR)
344 local_blit.filter = PIPE_TEX_FILTER_NEAREST;
345 if (!util_can_blit_via_copy_region(&local_blit, TRUE))
346 return false;
347
348 /* For depth+stencil formats, copy with mask != PIPE_MASK_ZS is not
349 * supported
350 */
351 if (util_format_is_depth_and_stencil(blit_info->src.format) &&
352 blit_info->mask != (PIPE_MASK_ZS))
353 return false;
354
355 return check_blending_and_srgb_cond(svga, blit_info);
356 }
357
358
359 static bool
360 can_blit_via_intra_surface_copy(struct svga_context *svga,
361 const struct pipe_blit_info *blit_info)
362 {
363 struct svga_texture *dtex, *stex;
364 struct svga_winsys_screen *sws = svga_screen(svga->pipe.screen)->sws;
365
366 if (!svga_have_vgpu10(svga))
367 return false;
368
369 if (!sws->have_intra_surface_copy)
370 return false;
371
372 stex = svga_texture(blit_info->src.resource);
373 dtex = svga_texture(blit_info->dst.resource);
374
375 if (stex->handle != dtex->handle)
376 return false;
377
378 if (blit_info->src.level != blit_info->dst.level)
379 return false;
380
381 if (has_layer_face_index_in_z(blit_info->src.resource->target)){
382 if (blit_info->src.box.z != blit_info->dst.box.z)
383 return false;
384 }
385
386 /* check that the blit src/dst regions are same size, no flipping, etc. */
387 if (blit_info->src.box.width != blit_info->dst.box.width ||
388 blit_info->src.box.height != blit_info->dst.box.height)
389 return false;
390
391 /* For depth+stencil formats, copy with mask != PIPE_MASK_ZS is not
392 * supported
393 */
394 if (util_format_is_depth_and_stencil(blit_info->src.format) &&
395 blit_info->mask != (PIPE_MASK_ZS))
396 return false;
397
398 if (blit_info->alpha_blend ||
399 (svga->render_condition && blit_info->render_condition_enable) ||
400 blit_info->scissor_enable)
401 return false;
402
403 return !(is_blending_enabled(svga, blit_info) &&
404 util_format_is_srgb(blit_info->src.resource->format));
405 }
406
407
408 /**
409 * The state tracker implements some resource copies with blits (for
410 * GL_ARB_copy_image). This function checks if we should really do the blit
411 * with a VGPU10 CopyRegion command or software fallback (for incompatible
412 * src/dst formats).
413 */
414 static bool
415 can_blit_via_copy_region_vgpu10(struct svga_context *svga,
416 const struct pipe_blit_info *blit_info)
417 {
418 struct svga_texture *dtex, *stex;
419
420 /* can't copy between different resource types */
421 if (svga_resource_type(blit_info->src.resource->target) !=
422 svga_resource_type(blit_info->dst.resource->target))
423 return false;
424
425 stex = svga_texture(blit_info->src.resource);
426 dtex = svga_texture(blit_info->dst.resource);
427
428 if (!svga_have_vgpu10(svga))
429 return false;
430
431 if (stex->handle == dtex->handle)
432 return false;
433
434 return formats_compatible(svga_screen(svga->pipe.screen),
435 stex->key.format,
436 dtex->key.format);
437 }
438
439
440 /**
441 * Check whether we can blit using the surface_copy command.
442 */
443 static bool
444 can_blit_via_surface_copy(struct svga_context *svga,
445 const struct pipe_blit_info *blit_info)
446 {
447 struct svga_texture *dtex, *stex;
448
449 /* Mimic the format tests in util_can_blit_via_copy_region(), but
450 * skip the other tests that have already been performed.
451 */
452 if (blit_info->src.format != blit_info->dst.format) {
453 const struct util_format_description *src_desc, *dst_desc;
454
455 src_desc = util_format_description(blit_info->src.resource->format);
456 dst_desc = util_format_description(blit_info->dst.resource->format);
457
458 if (blit_info->src.resource->format != blit_info->src.format ||
459 blit_info->dst.resource->format != blit_info->dst.format ||
460 !util_is_format_compatible(src_desc, dst_desc))
461 return false;
462 }
463
464 if (svga->render_condition && blit_info->render_condition_enable)
465 return false;
466
467 /* can't copy between different resource types */
468 if (svga_resource_type(blit_info->src.resource->target) !=
469 svga_resource_type(blit_info->dst.resource->target))
470 return false;
471
472 stex = svga_texture(blit_info->src.resource);
473 dtex = svga_texture(blit_info->dst.resource);
474
475 if (stex->handle == dtex->handle)
476 return false;
477
478 /*
479 * This is what we've been using before, but it can probably be
480 * relaxed. The device checks are less stringent.
481 */
482 return (stex->b.b.format == dtex->b.b.format);
483 }
484
485
486 /**
487 * Try region copy using one of the region copy commands
488 */
489 static bool
490 try_copy_region(struct svga_context *svga,
491 const struct pipe_blit_info *blit)
492 {
493 unsigned src_layer_face, src_z, dst_layer_face, dst_z;
494
495 if (!can_blit_via_svga_copy_region(svga, blit))
496 return false;
497
498 adjust_z_layer(blit->src.resource->target, blit->src.box.z,
499 &src_layer_face, &src_z);
500
501 adjust_z_layer(blit->dst.resource->target, blit->dst.box.z,
502 &dst_layer_face, &dst_z);
503
504 if (can_blit_via_copy_region_vgpu10(svga, blit)) {
505 svga_toggle_render_condition(svga, blit->render_condition_enable, FALSE);
506
507 copy_region_vgpu10(svga,
508 blit->src.resource,
509 blit->src.box.x, blit->src.box.y, src_z,
510 blit->src.level, src_layer_face,
511 blit->dst.resource,
512 blit->dst.box.x, blit->dst.box.y, dst_z,
513 blit->dst.level, dst_layer_face,
514 blit->src.box.width, blit->src.box.height,
515 blit->src.box.depth);
516
517 svga_toggle_render_condition(svga, blit->render_condition_enable, TRUE);
518
519 return true;
520 }
521
522 if (can_blit_via_surface_copy(svga, blit)) {
523 struct svga_texture *stex = svga_texture(blit->src.resource);
524 struct svga_texture *dtex = svga_texture(blit->dst.resource);
525
526 svga_surfaces_flush(svga);
527
528 svga_texture_copy_handle(svga,
529 stex->handle,
530 blit->src.box.x, blit->src.box.y, src_z,
531 blit->src.level, src_layer_face,
532 dtex->handle,
533 blit->dst.box.x, blit->dst.box.y, dst_z,
534 blit->dst.level, dst_layer_face,
535 blit->src.box.width, blit->src.box.height,
536 blit->src.box.depth);
537
538 svga_define_texture_level(dtex, dst_layer_face, blit->dst.level);
539 svga_set_texture_rendered_to(dtex, dst_layer_face, blit->dst.level);
540 return true;
541 }
542
543 if (can_blit_via_intra_surface_copy(svga, blit)) {
544 intra_surface_copy(svga,
545 blit->src.resource,
546 blit->src.box.x, blit->src.box.y, src_z,
547 blit->src.level, src_layer_face,
548 blit->dst.box.x, blit->dst.box.y, dst_z,
549 blit->src.box.width, blit->src.box.height,
550 blit->src.box.depth);
551 return true;
552 }
553
554 return false;
555 }
556
557
558 /**
559 * A helper function to determine if the specified view format
560 * is compatible with the surface format.
561 * It is compatible if the view format is the same as the surface format,
562 * or the associated svga format for the surface is a typeless format, or
563 * the view format is an adjusted format for BGRX/BGRA resource.
564 */
565 static bool
566 is_view_format_compatible(enum pipe_format surf_fmt,
567 SVGA3dSurfaceFormat surf_svga_fmt,
568 enum pipe_format view_fmt)
569 {
570 if (surf_fmt == view_fmt || svga_format_is_typeless(surf_svga_fmt))
571 return true;
572
573 if ((surf_fmt == PIPE_FORMAT_B8G8R8X8_UNORM &&
574 view_fmt == PIPE_FORMAT_B8G8R8A8_UNORM) ||
575 (surf_fmt == PIPE_FORMAT_B8G8R8A8_UNORM &&
576 view_fmt == PIPE_FORMAT_B8G8R8X8_UNORM))
577 return true;
578
579 return false;
580 }
581
582
583 /**
584 * Try issuing a quad blit.
585 */
586 static bool
587 try_blit(struct svga_context *svga, const struct pipe_blit_info *blit_info)
588 {
589 struct svga_winsys_screen *sws = svga_screen(svga->pipe.screen)->sws;
590 struct pipe_resource *src = blit_info->src.resource;
591 struct pipe_resource *dst = blit_info->dst.resource;
592 struct pipe_resource *newSrc = NULL;
593 struct pipe_resource *newDst = NULL;
594 bool can_create_src_view;
595 bool can_create_dst_view;
596 bool ret = true;
597 struct pipe_blit_info blit = *blit_info;
598
599 SVGA_STATS_TIME_PUSH(sws, SVGA_STATS_TIME_BLITBLITTER);
600
601 /**
602 * Avoid using util_blitter_blit() for these depth formats on non-vgpu10
603 * devices because these depth formats only support comparison mode
604 * and not ordinary sampling.
605 */
606 if (!svga_have_vgpu10(svga) && (blit.mask & PIPE_MASK_Z) &&
607 (svga_texture(dst)->key.format == SVGA3D_Z_D16 ||
608 svga_texture(dst)->key.format == SVGA3D_Z_D24X8 ||
609 svga_texture(dst)->key.format == SVGA3D_Z_D24S8)) {
610 ret = false;
611 goto done;
612 }
613
614 /**
615 * If format is srgb and blend is enabled then color values need
616 * to be converted into linear format.
617 */
618 if (is_blending_enabled(svga, &blit)) {
619 blit.src.format = util_format_linear(blit.src.format);
620 blit.dst.format = util_format_linear(blit.dst.format);
621 }
622
623 /* Check if we can create shader resource view and
624 * render target view for the quad blitter to work
625 */
626 can_create_src_view =
627 is_view_format_compatible(src->format, svga_texture(src)->key.format,
628 blit.src.format);
629
630 can_create_dst_view =
631 is_view_format_compatible(dst->format, svga_texture(dst)->key.format,
632 blit.dst.format);
633
634 if ((blit.mask & PIPE_MASK_S) ||
635 ((!can_create_dst_view || !can_create_src_view)
636 && !svga_have_vgpu10(svga))) {
637 /* Can't do stencil blits with textured quad blitter */
638 debug_warn_once("using software stencil blit");
639 ret = false;
640 goto done;
641 }
642
643 if (!util_blitter_is_blit_supported(svga->blitter, &blit)) {
644 debug_printf("svga: blit unsupported %s -> %s\n",
645 util_format_short_name(blit.src.resource->format),
646 util_format_short_name(blit.dst.resource->format));
647 ret = false;
648 goto done;
649 }
650
651 /* XXX turn off occlusion and streamout queries */
652
653 util_blitter_save_vertex_buffer_slot(svga->blitter, svga->curr.vb);
654 util_blitter_save_vertex_elements(svga->blitter, (void*)svga->curr.velems);
655 util_blitter_save_vertex_shader(svga->blitter, svga->curr.vs);
656 util_blitter_save_geometry_shader(svga->blitter, svga->curr.user_gs);
657 util_blitter_save_so_targets(svga->blitter, svga->num_so_targets,
658 (struct pipe_stream_output_target**)svga->so_targets);
659 util_blitter_save_rasterizer(svga->blitter, (void*)svga->curr.rast);
660 util_blitter_save_viewport(svga->blitter, &svga->curr.viewport);
661 util_blitter_save_scissor(svga->blitter, &svga->curr.scissor);
662 util_blitter_save_fragment_shader(svga->blitter, svga->curr.fs);
663 util_blitter_save_blend(svga->blitter, (void*)svga->curr.blend);
664 util_blitter_save_depth_stencil_alpha(svga->blitter,
665 (void*)svga->curr.depth);
666 util_blitter_save_stencil_ref(svga->blitter, &svga->curr.stencil_ref);
667 util_blitter_save_sample_mask(svga->blitter, svga->curr.sample_mask);
668 util_blitter_save_framebuffer(svga->blitter, &svga->curr.framebuffer);
669 util_blitter_save_fragment_sampler_states(svga->blitter,
670 svga->curr.num_samplers[PIPE_SHADER_FRAGMENT],
671 (void**)svga->curr.sampler[PIPE_SHADER_FRAGMENT]);
672 util_blitter_save_fragment_sampler_views(svga->blitter,
673 svga->curr.num_sampler_views[PIPE_SHADER_FRAGMENT],
674 svga->curr.sampler_views[PIPE_SHADER_FRAGMENT]);
675
676 if (!can_create_src_view) {
677 struct pipe_resource template;
678 struct pipe_blit_info copy_region_blit;
679
680 /**
681 * If the source blit format is not compatible with the source resource
682 * format, we will not be able to create a shader resource view.
683 * In order to avoid falling back to software blit, we'll create
684 * a new resource in the blit format, and use DXCopyResource to
685 * copy from the original format to the new format. The new
686 * resource will be used for the blit in util_blitter_blit().
687 */
688 template = *src;
689 template.format = blit.src.format;
690 newSrc = svga_texture_create(svga->pipe.screen, &template);
691 if (newSrc == NULL) {
692 debug_printf("svga_blit: fails to create temporary src\n");
693 ret = false;
694 goto done;
695 }
696
697 /* increment the mksStats for blitter with extra copy */
698 SVGA_STATS_COUNT_INC(sws, SVGA_STATS_COUNT_BLITBLITTERCOPY);
699 build_blit_info(newSrc,
700 blit.src.level, blit.src.box.x,
701 blit.src.box.y, blit.src.box.z,
702 blit.src.resource,
703 blit.src.level, &blit.src.box,
704 &copy_region_blit);
705 if (!try_copy_region(svga, &copy_region_blit)) {
706 debug_printf("svga: Source blit format conversion failed.\n");
707 ret = false;
708 goto done;
709 }
710
711 blit.src.resource = newSrc;
712 }
713
714 if (!can_create_dst_view) {
715 struct pipe_resource template;
716
717 /*
718 * If the destination blit format is not compatible with the destination
719 * resource format, we will not be able to create a render target view.
720 * In order to avoid falling back to software blit, we'll create
721 * a new resource in the blit format, and use DXPredCopyRegion
722 * after the blit to copy from the blit format back to the resource
723 * format.
724 */
725 template = *dst;
726 template.format = blit.dst.format;
727 newDst = svga_texture_create(svga->pipe.screen, &template);
728 if (newDst == NULL) {
729 debug_printf("svga_blit: fails to create temporary dst\n");
730 ret = false;
731 goto done;
732 }
733
734 blit.dst.resource = newDst;
735 }
736
737 svga_toggle_render_condition(svga, blit.render_condition_enable, FALSE);
738
739 util_blitter_blit(svga->blitter, &blit);
740
741 svga_toggle_render_condition(svga, blit.render_condition_enable, TRUE);
742
743 if (blit.dst.resource != dst) {
744 struct pipe_blit_info copy_region_blit;
745
746 /* increment the mksStats for blitter with extra copy */
747 SVGA_STATS_COUNT_INC(sws, SVGA_STATS_COUNT_BLITBLITTERCOPY);
748
749 /*
750 * A temporary resource was created for the blit, we need to
751 * copy from the temporary resource back to the original destination.
752 */
753 build_blit_info(dst,
754 blit.dst.level, blit.dst.box.x,
755 blit.dst.box.y, blit.dst.box.z,
756 newDst,
757 blit.dst.level, &blit.dst.box,
758 &copy_region_blit);
759 if (!try_copy_region(svga, &copy_region_blit)) {
760 debug_printf("svga: Destination blit format conversion failed.\n");
761 ret = false;
762 goto done;
763 }
764 }
765
766 done:
767 /* unreference the temporary resources if needed */
768 pipe_resource_reference(&newDst, NULL);
769 pipe_resource_reference(&newSrc, NULL);
770
771 SVGA_STATS_TIME_POP(sws); /* SVGA_STATS_TIME_BLITBLITTER */
772 (void) sws;
773
774 return ret;
775 }
776
777
778 /**
779 * Try a cpu copy_region fallback.
780 */
781 static bool
782 try_cpu_copy_region(struct svga_context *svga,
783 const struct pipe_blit_info *blit)
784 {
785 if (util_can_blit_via_copy_region(blit, TRUE) ||
786 util_can_blit_via_copy_region(blit, FALSE)) {
787
788 if (svga->render_condition && blit->render_condition_enable) {
789 debug_warning("CPU copy_region doesn't support "
790 "conditional rendering.\n");
791 return false;
792 }
793
794 copy_region_fallback(svga, blit->dst.resource,
795 blit->dst.level,
796 blit->dst.box.x, blit->dst.box.y,
797 blit->dst.box.z, blit->src.resource,
798 blit->src.level, &blit->src.box);
799 return true;
800 }
801
802 return false;
803 }
804
805
806 /**
807 * The pipe::blit member.
808 */
809 static void
810 svga_blit(struct pipe_context *pipe,
811 const struct pipe_blit_info *blit)
812 {
813 struct svga_context *svga = svga_context(pipe);
814 struct svga_winsys_screen *sws = svga_screen(pipe->screen)->sws;
815
816 if (!svga_have_vgpu10(svga) &&
817 blit->src.resource->nr_samples > 1 &&
818 blit->dst.resource->nr_samples <= 1 &&
819 !util_format_is_depth_or_stencil(blit->src.resource->format) &&
820 !util_format_is_pure_integer(blit->src.resource->format)) {
821 debug_printf("svga: color resolve unimplemented\n");
822 return;
823 }
824
825 SVGA_STATS_TIME_PUSH(sws, SVGA_STATS_TIME_BLIT);
826
827 if (try_copy_region(svga, blit))
828 goto done;
829
830 if (try_blit(svga, blit))
831 goto done;
832
833 if (!try_cpu_copy_region(svga, blit))
834 debug_printf("svga: Blit failed.\n");
835
836 done:
837 SVGA_STATS_TIME_POP(sws); /* SVGA_STATS_TIME_BLIT */
838 (void) sws;
839 }
840
841
842 /**
843 * The pipe::resource_copy_region member.
844 */
845 static void
846 svga_resource_copy_region(struct pipe_context *pipe,
847 struct pipe_resource *dst_tex,
848 unsigned dst_level,
849 unsigned dstx, unsigned dsty, unsigned dstz,
850 struct pipe_resource *src_tex,
851 unsigned src_level,
852 const struct pipe_box *src_box)
853 {
854 struct svga_context *svga = svga_context(pipe);
855 struct svga_winsys_screen *sws = svga_screen(svga->pipe.screen)->sws;
856
857 SVGA_STATS_TIME_PUSH(sws, SVGA_STATS_TIME_COPYREGION);
858
859 if (dst_tex->target == PIPE_BUFFER && src_tex->target == PIPE_BUFFER) {
860 /* can't copy within the same buffer, unfortunately */
861 if (svga_have_vgpu10(svga) && src_tex != dst_tex) {
862 enum pipe_error ret;
863 struct svga_winsys_surface *src_surf;
864 struct svga_winsys_surface *dst_surf;
865 struct svga_buffer *dbuffer = svga_buffer(dst_tex);
866 struct svga_buffer *sbuffer = svga_buffer(src_tex);
867
868 src_surf = svga_buffer_handle(svga, src_tex, sbuffer->bind_flags);
869 dst_surf = svga_buffer_handle(svga, dst_tex, dbuffer->bind_flags);
870
871 ret = SVGA3D_vgpu10_BufferCopy(svga->swc, src_surf, dst_surf,
872 src_box->x, dstx, src_box->width);
873 if (ret != PIPE_OK) {
874 svga_context_flush(svga, NULL);
875 ret = SVGA3D_vgpu10_BufferCopy(svga->swc, src_surf, dst_surf,
876 src_box->x, dstx, src_box->width);
877 assert(ret == PIPE_OK);
878 }
879
880 dbuffer->dirty = TRUE;
881 }
882 else {
883 /* use map/memcpy fallback */
884 copy_region_fallback(svga, dst_tex, dst_level, dstx,
885 dsty, dstz, src_tex, src_level, src_box);
886 }
887 } else {
888 struct pipe_blit_info blit;
889
890 build_blit_info(dst_tex, dst_level, dstx, dsty, dstz,
891 src_tex, src_level, src_box, &blit);
892
893 if (try_copy_region(svga, &blit))
894 goto done;
895
896 /* Blits are format-converting which is not what we want, so perform a
897 * strict format-check.
898 * FIXME: Need to figure out why srgb blits (tf2) and
899 * 3D blits (piglit) are broken here. Perhaps we set up the
900 * struct pipe_blit_info incorrectly.
901 */
902 if (src_tex->format == dst_tex->format &&
903 !util_format_is_srgb(src_tex->format) &&
904 svga_resource_type(src_tex->target) != SVGA3D_RESOURCE_TEXTURE3D &&
905 try_blit(svga, &blit))
906 goto done;
907
908 copy_region_fallback(svga, dst_tex, dst_level, dstx, dsty, dstz,
909 src_tex, src_level, src_box);
910 }
911
912 done:
913 SVGA_STATS_TIME_POP(sws);
914 (void) sws;
915 }
916
917
918 /**
919 * The pipe::flush_resource member.
920 */
921 static void
922 svga_flush_resource(struct pipe_context *pipe,
923 struct pipe_resource *resource)
924 {
925 }
926
927
928 /**
929 * Setup the pipe blit, resource_copy_region and flush_resource members.
930 */
931 void
932 svga_init_blit_functions(struct svga_context *svga)
933 {
934 svga->pipe.resource_copy_region = svga_resource_copy_region;
935 svga->pipe.blit = svga_blit;
936 svga->pipe.flush_resource = svga_flush_resource;
937 }