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