svga: draw round points when msaa is enabled
[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 /* src surface cannot be multisample */
354 if (blit_info->src.resource->nr_samples > 1)
355 return false;
356
357 if (!sws->have_intra_surface_copy)
358 return false;
359
360 stex = svga_texture(blit_info->src.resource);
361 dtex = svga_texture(blit_info->dst.resource);
362
363 if (stex->handle != dtex->handle)
364 return false;
365
366 if (blit_info->src.level != blit_info->dst.level)
367 return false;
368
369 if (has_layer_face_index_in_z(blit_info->src.resource->target)){
370 if (blit_info->src.box.z != blit_info->dst.box.z)
371 return false;
372 }
373
374 /* check that the blit src/dst regions are same size, no flipping, etc. */
375 if (blit_info->src.box.width != blit_info->dst.box.width ||
376 blit_info->src.box.height != blit_info->dst.box.height)
377 return false;
378
379 /* For depth+stencil formats, copy with mask != PIPE_MASK_ZS is not
380 * supported
381 */
382 if (util_format_is_depth_and_stencil(blit_info->src.format) &&
383 blit_info->mask != (PIPE_MASK_ZS))
384 return false;
385
386 if (blit_info->alpha_blend ||
387 (svga->render_condition && blit_info->render_condition_enable) ||
388 blit_info->scissor_enable)
389 return false;
390
391 return !(is_blending_enabled(svga, blit_info) &&
392 util_format_is_srgb(blit_info->src.resource->format));
393 }
394
395
396 /**
397 * The state tracker implements some resource copies with blits (for
398 * GL_ARB_copy_image). This function checks if we should really do the blit
399 * with a VGPU10 CopyRegion command or software fallback (for incompatible
400 * src/dst formats).
401 */
402 static bool
403 can_blit_via_copy_region_vgpu10(struct svga_context *svga,
404 const struct pipe_blit_info *blit_info)
405 {
406 struct svga_texture *dtex, *stex;
407
408 /* can't copy between different resource types */
409 if (svga_resource_type(blit_info->src.resource->target) !=
410 svga_resource_type(blit_info->dst.resource->target))
411 return false;
412
413 stex = svga_texture(blit_info->src.resource);
414 dtex = svga_texture(blit_info->dst.resource);
415
416 if (!svga_have_vgpu10(svga))
417 return false;
418
419 if (stex->handle == dtex->handle)
420 return false;
421
422 return formats_compatible(svga_screen(svga->pipe.screen),
423 stex->key.format,
424 dtex->key.format);
425 }
426
427
428 /**
429 * Check whether we can blit using the surface_copy command.
430 */
431 static bool
432 can_blit_via_surface_copy(struct svga_context *svga,
433 const struct pipe_blit_info *blit_info)
434 {
435 struct svga_texture *dtex, *stex;
436
437 /* Mimic the format tests in util_can_blit_via_copy_region(), but
438 * skip the other tests that have already been performed.
439 */
440 if (blit_info->src.format != blit_info->dst.format) {
441 const struct util_format_description *src_desc, *dst_desc;
442
443 src_desc = util_format_description(blit_info->src.resource->format);
444 dst_desc = util_format_description(blit_info->dst.resource->format);
445
446 if (blit_info->src.resource->format != blit_info->src.format ||
447 blit_info->dst.resource->format != blit_info->dst.format ||
448 !util_is_format_compatible(src_desc, dst_desc))
449 return false;
450 }
451
452 if (svga->render_condition && blit_info->render_condition_enable)
453 return false;
454
455 /* can't copy between different resource types */
456 if (svga_resource_type(blit_info->src.resource->target) !=
457 svga_resource_type(blit_info->dst.resource->target))
458 return false;
459
460 stex = svga_texture(blit_info->src.resource);
461 dtex = svga_texture(blit_info->dst.resource);
462
463 if (stex->handle == dtex->handle)
464 return false;
465
466 /*
467 * This is what we've been using before, but it can probably be
468 * relaxed. The device checks are less stringent.
469 */
470 return (stex->b.b.format == dtex->b.b.format);
471 }
472
473
474 /**
475 * Try region copy using one of the region copy commands
476 */
477 static bool
478 try_copy_region(struct svga_context *svga,
479 const struct pipe_blit_info *blit)
480 {
481 unsigned src_layer_face, src_z, dst_layer_face, dst_z;
482
483 if (!can_blit_via_svga_copy_region(svga, blit))
484 return false;
485
486 adjust_z_layer(blit->src.resource->target, blit->src.box.z,
487 &src_layer_face, &src_z);
488
489 adjust_z_layer(blit->dst.resource->target, blit->dst.box.z,
490 &dst_layer_face, &dst_z);
491
492 if (can_blit_via_copy_region_vgpu10(svga, blit)) {
493 svga_toggle_render_condition(svga, blit->render_condition_enable, FALSE);
494
495 copy_region_vgpu10(svga,
496 blit->src.resource,
497 blit->src.box.x, blit->src.box.y, src_z,
498 blit->src.level, src_layer_face,
499 blit->dst.resource,
500 blit->dst.box.x, blit->dst.box.y, dst_z,
501 blit->dst.level, dst_layer_face,
502 blit->src.box.width, blit->src.box.height,
503 blit->src.box.depth);
504
505 svga_toggle_render_condition(svga, blit->render_condition_enable, TRUE);
506
507 return true;
508 }
509
510 if (can_blit_via_surface_copy(svga, blit)) {
511 struct svga_texture *stex = svga_texture(blit->src.resource);
512 struct svga_texture *dtex = svga_texture(blit->dst.resource);
513
514 svga_surfaces_flush(svga);
515
516 svga_texture_copy_handle(svga,
517 stex->handle,
518 blit->src.box.x, blit->src.box.y, src_z,
519 blit->src.level, src_layer_face,
520 dtex->handle,
521 blit->dst.box.x, blit->dst.box.y, dst_z,
522 blit->dst.level, dst_layer_face,
523 blit->src.box.width, blit->src.box.height,
524 blit->src.box.depth);
525
526 svga_define_texture_level(dtex, dst_layer_face, blit->dst.level);
527 svga_set_texture_rendered_to(dtex, dst_layer_face, blit->dst.level);
528 return true;
529 }
530
531 if (can_blit_via_intra_surface_copy(svga, blit)) {
532 intra_surface_copy(svga,
533 blit->src.resource,
534 blit->src.box.x, blit->src.box.y, src_z,
535 blit->src.level, src_layer_face,
536 blit->dst.box.x, blit->dst.box.y, dst_z,
537 blit->src.box.width, blit->src.box.height,
538 blit->src.box.depth);
539 return true;
540 }
541
542 return false;
543 }
544
545
546 /**
547 * A helper function to determine if the specified view format
548 * is compatible with the surface format.
549 * It is compatible if the view format is the same as the surface format,
550 * or the associated svga format for the surface is a typeless format, or
551 * the view format is an adjusted format for BGRX/BGRA resource.
552 */
553 static bool
554 is_view_format_compatible(enum pipe_format surf_fmt,
555 SVGA3dSurfaceFormat surf_svga_fmt,
556 enum pipe_format view_fmt)
557 {
558 if (surf_fmt == view_fmt || svga_format_is_typeless(surf_svga_fmt))
559 return true;
560
561 if ((surf_fmt == PIPE_FORMAT_B8G8R8X8_UNORM &&
562 view_fmt == PIPE_FORMAT_B8G8R8A8_UNORM) ||
563 (surf_fmt == PIPE_FORMAT_B8G8R8A8_UNORM &&
564 view_fmt == PIPE_FORMAT_B8G8R8X8_UNORM))
565 return true;
566
567 return false;
568 }
569
570
571 /**
572 * Try issuing a quad blit.
573 */
574 static bool
575 try_blit(struct svga_context *svga, const struct pipe_blit_info *blit_info)
576 {
577 struct svga_winsys_screen *sws = svga_screen(svga->pipe.screen)->sws;
578 struct pipe_resource *src = blit_info->src.resource;
579 struct pipe_resource *dst = blit_info->dst.resource;
580 struct pipe_resource *newSrc = NULL;
581 struct pipe_resource *newDst = NULL;
582 bool can_create_src_view;
583 bool can_create_dst_view;
584 bool ret = true;
585 struct pipe_blit_info blit = *blit_info;
586
587 SVGA_STATS_TIME_PUSH(sws, SVGA_STATS_TIME_BLITBLITTER);
588
589 /**
590 * Avoid using util_blitter_blit() for these depth formats on non-vgpu10
591 * devices because these depth formats only support comparison mode
592 * and not ordinary sampling.
593 */
594 if (!svga_have_vgpu10(svga) && (blit.mask & PIPE_MASK_Z) &&
595 (svga_texture(dst)->key.format == SVGA3D_Z_D16 ||
596 svga_texture(dst)->key.format == SVGA3D_Z_D24X8 ||
597 svga_texture(dst)->key.format == SVGA3D_Z_D24S8)) {
598 ret = false;
599 goto done;
600 }
601
602 /**
603 * If format is srgb and blend is enabled then color values need
604 * to be converted into linear format.
605 */
606 if (is_blending_enabled(svga, &blit)) {
607 blit.src.format = util_format_linear(blit.src.format);
608 blit.dst.format = util_format_linear(blit.dst.format);
609 }
610
611 /* Check if we can create shader resource view and
612 * render target view for the quad blitter to work
613 */
614 can_create_src_view =
615 is_view_format_compatible(src->format, svga_texture(src)->key.format,
616 blit.src.format);
617
618 can_create_dst_view =
619 is_view_format_compatible(dst->format, svga_texture(dst)->key.format,
620 blit.dst.format);
621
622 if ((blit.mask & PIPE_MASK_S) ||
623 ((!can_create_dst_view || !can_create_src_view)
624 && !svga_have_vgpu10(svga))) {
625 /* Can't do stencil blits with textured quad blitter */
626 debug_warn_once("using software stencil blit");
627 ret = false;
628 goto done;
629 }
630
631 if (!util_blitter_is_blit_supported(svga->blitter, &blit)) {
632 debug_printf("svga: blit unsupported %s -> %s\n",
633 util_format_short_name(blit.src.resource->format),
634 util_format_short_name(blit.dst.resource->format));
635 ret = false;
636 goto done;
637 }
638
639 /* XXX turn off occlusion and streamout queries */
640
641 util_blitter_save_vertex_buffer_slot(svga->blitter, svga->curr.vb);
642 util_blitter_save_vertex_elements(svga->blitter, (void*)svga->curr.velems);
643 util_blitter_save_vertex_shader(svga->blitter, svga->curr.vs);
644 util_blitter_save_geometry_shader(svga->blitter, svga->curr.user_gs);
645 util_blitter_save_so_targets(svga->blitter, svga->num_so_targets,
646 (struct pipe_stream_output_target**)svga->so_targets);
647 util_blitter_save_rasterizer(svga->blitter, (void*)svga->curr.rast);
648 util_blitter_save_viewport(svga->blitter, &svga->curr.viewport);
649 util_blitter_save_scissor(svga->blitter, &svga->curr.scissor);
650 util_blitter_save_fragment_shader(svga->blitter, svga->curr.fs);
651 util_blitter_save_blend(svga->blitter, (void*)svga->curr.blend);
652 util_blitter_save_depth_stencil_alpha(svga->blitter,
653 (void*)svga->curr.depth);
654 util_blitter_save_stencil_ref(svga->blitter, &svga->curr.stencil_ref);
655 util_blitter_save_sample_mask(svga->blitter, svga->curr.sample_mask);
656 util_blitter_save_framebuffer(svga->blitter, &svga->curr.framebuffer);
657 util_blitter_save_fragment_sampler_states(svga->blitter,
658 svga->curr.num_samplers[PIPE_SHADER_FRAGMENT],
659 (void**)svga->curr.sampler[PIPE_SHADER_FRAGMENT]);
660 util_blitter_save_fragment_sampler_views(svga->blitter,
661 svga->curr.num_sampler_views[PIPE_SHADER_FRAGMENT],
662 svga->curr.sampler_views[PIPE_SHADER_FRAGMENT]);
663
664 if (!can_create_src_view) {
665 struct pipe_resource template;
666 struct pipe_blit_info copy_region_blit;
667
668 /**
669 * If the source blit format is not compatible with the source resource
670 * format, we will not be able to create a shader resource view.
671 * In order to avoid falling back to software blit, we'll create
672 * a new resource in the blit format, and use DXCopyResource to
673 * copy from the original format to the new format. The new
674 * resource will be used for the blit in util_blitter_blit().
675 */
676 template = *src;
677 template.format = blit.src.format;
678 newSrc = svga_texture_create(svga->pipe.screen, &template);
679 if (newSrc == NULL) {
680 debug_printf("svga_blit: fails to create temporary src\n");
681 ret = false;
682 goto done;
683 }
684
685 /* increment the mksStats for blitter with extra copy */
686 SVGA_STATS_COUNT_INC(sws, SVGA_STATS_COUNT_BLITBLITTERCOPY);
687 build_blit_info(newSrc,
688 blit.src.level, blit.src.box.x,
689 blit.src.box.y, blit.src.box.z,
690 blit.src.resource,
691 blit.src.level, &blit.src.box,
692 &copy_region_blit);
693 if (!try_copy_region(svga, &copy_region_blit)) {
694 debug_printf("svga: Source blit format conversion failed.\n");
695 ret = false;
696 goto done;
697 }
698
699 blit.src.resource = newSrc;
700 }
701
702 if (!can_create_dst_view) {
703 struct pipe_resource template;
704
705 /*
706 * If the destination blit format is not compatible with the destination
707 * resource format, we will not be able to create a render target view.
708 * In order to avoid falling back to software blit, we'll create
709 * a new resource in the blit format, and use DXPredCopyRegion
710 * after the blit to copy from the blit format back to the resource
711 * format.
712 */
713 template = *dst;
714 template.format = blit.dst.format;
715 newDst = svga_texture_create(svga->pipe.screen, &template);
716 if (newDst == NULL) {
717 debug_printf("svga_blit: fails to create temporary dst\n");
718 ret = false;
719 goto done;
720 }
721
722 blit.dst.resource = newDst;
723 }
724
725 svga_toggle_render_condition(svga, blit.render_condition_enable, FALSE);
726
727 util_blitter_blit(svga->blitter, &blit);
728
729 svga_toggle_render_condition(svga, blit.render_condition_enable, TRUE);
730
731 if (blit.dst.resource != dst) {
732 struct pipe_blit_info copy_region_blit;
733
734 /* increment the mksStats for blitter with extra copy */
735 SVGA_STATS_COUNT_INC(sws, SVGA_STATS_COUNT_BLITBLITTERCOPY);
736
737 /*
738 * A temporary resource was created for the blit, we need to
739 * copy from the temporary resource back to the original destination.
740 */
741 build_blit_info(dst,
742 blit.dst.level, blit.dst.box.x,
743 blit.dst.box.y, blit.dst.box.z,
744 newDst,
745 blit.dst.level, &blit.dst.box,
746 &copy_region_blit);
747 if (!try_copy_region(svga, &copy_region_blit)) {
748 debug_printf("svga: Destination blit format conversion failed.\n");
749 ret = false;
750 goto done;
751 }
752 }
753
754 done:
755 /* unreference the temporary resources if needed */
756 pipe_resource_reference(&newDst, NULL);
757 pipe_resource_reference(&newSrc, NULL);
758
759 SVGA_STATS_TIME_POP(sws); /* SVGA_STATS_TIME_BLITBLITTER */
760 (void) sws;
761
762 return ret;
763 }
764
765
766 /**
767 * Try a cpu copy_region fallback.
768 */
769 static bool
770 try_cpu_copy_region(struct svga_context *svga,
771 const struct pipe_blit_info *blit)
772 {
773 if (util_can_blit_via_copy_region(blit, TRUE) ||
774 util_can_blit_via_copy_region(blit, FALSE)) {
775
776 if (svga->render_condition && blit->render_condition_enable) {
777 debug_warning("CPU copy_region doesn't support "
778 "conditional rendering.\n");
779 return false;
780 }
781
782 copy_region_fallback(svga, blit->dst.resource,
783 blit->dst.level,
784 blit->dst.box.x, blit->dst.box.y,
785 blit->dst.box.z, blit->src.resource,
786 blit->src.level, &blit->src.box);
787 return true;
788 }
789
790 return false;
791 }
792
793
794 /**
795 * The pipe::blit member.
796 */
797 static void
798 svga_blit(struct pipe_context *pipe,
799 const struct pipe_blit_info *blit)
800 {
801 struct svga_context *svga = svga_context(pipe);
802 struct svga_winsys_screen *sws = svga_screen(pipe->screen)->sws;
803
804 if (!svga_have_vgpu10(svga) &&
805 blit->src.resource->nr_samples > 1 &&
806 blit->dst.resource->nr_samples <= 1 &&
807 !util_format_is_depth_or_stencil(blit->src.resource->format) &&
808 !util_format_is_pure_integer(blit->src.resource->format)) {
809 debug_printf("svga: color resolve unimplemented\n");
810 return;
811 }
812
813 SVGA_STATS_TIME_PUSH(sws, SVGA_STATS_TIME_BLIT);
814
815 if (try_copy_region(svga, blit))
816 goto done;
817
818 if (try_blit(svga, blit))
819 goto done;
820
821 if (!try_cpu_copy_region(svga, blit))
822 debug_printf("svga: Blit failed.\n");
823
824 done:
825 SVGA_STATS_TIME_POP(sws); /* SVGA_STATS_TIME_BLIT */
826 (void) sws;
827 }
828
829
830 /**
831 * The pipe::resource_copy_region member.
832 */
833 static void
834 svga_resource_copy_region(struct pipe_context *pipe,
835 struct pipe_resource *dst_tex,
836 unsigned dst_level,
837 unsigned dstx, unsigned dsty, unsigned dstz,
838 struct pipe_resource *src_tex,
839 unsigned src_level,
840 const struct pipe_box *src_box)
841 {
842 struct svga_context *svga = svga_context(pipe);
843 struct svga_winsys_screen *sws = svga_screen(svga->pipe.screen)->sws;
844
845 SVGA_STATS_TIME_PUSH(sws, SVGA_STATS_TIME_COPYREGION);
846
847 if (dst_tex->target == PIPE_BUFFER && src_tex->target == PIPE_BUFFER) {
848 /* can't copy within the same buffer, unfortunately */
849 if (svga_have_vgpu10(svga) && src_tex != dst_tex) {
850 enum pipe_error ret;
851 struct svga_winsys_surface *src_surf;
852 struct svga_winsys_surface *dst_surf;
853 struct svga_buffer *dbuffer = svga_buffer(dst_tex);
854 struct svga_buffer *sbuffer = svga_buffer(src_tex);
855
856 src_surf = svga_buffer_handle(svga, src_tex, sbuffer->bind_flags);
857 dst_surf = svga_buffer_handle(svga, dst_tex, dbuffer->bind_flags);
858
859 ret = SVGA3D_vgpu10_BufferCopy(svga->swc, src_surf, dst_surf,
860 src_box->x, dstx, src_box->width);
861 if (ret != PIPE_OK) {
862 svga_context_flush(svga, NULL);
863 ret = SVGA3D_vgpu10_BufferCopy(svga->swc, src_surf, dst_surf,
864 src_box->x, dstx, src_box->width);
865 assert(ret == PIPE_OK);
866 }
867
868 dbuffer->dirty = TRUE;
869 }
870 else {
871 /* use map/memcpy fallback */
872 copy_region_fallback(svga, dst_tex, dst_level, dstx,
873 dsty, dstz, src_tex, src_level, src_box);
874 }
875 } else {
876 struct pipe_blit_info blit;
877
878 build_blit_info(dst_tex, dst_level, dstx, dsty, dstz,
879 src_tex, src_level, src_box, &blit);
880
881 if (try_copy_region(svga, &blit))
882 goto done;
883
884 /* Blits are format-converting which is not what we want, so perform a
885 * strict format-check.
886 * FIXME: Need to figure out why srgb blits (tf2) and
887 * 3D blits (piglit) are broken here. Perhaps we set up the
888 * struct pipe_blit_info incorrectly.
889 */
890 if (src_tex->format == dst_tex->format &&
891 !util_format_is_srgb(src_tex->format) &&
892 svga_resource_type(src_tex->target) != SVGA3D_RESOURCE_TEXTURE3D &&
893 try_blit(svga, &blit))
894 goto done;
895
896 copy_region_fallback(svga, dst_tex, dst_level, dstx, dsty, dstz,
897 src_tex, src_level, src_box);
898 }
899
900 done:
901 SVGA_STATS_TIME_POP(sws);
902 (void) sws;
903 }
904
905
906 /**
907 * The pipe::flush_resource member.
908 */
909 static void
910 svga_flush_resource(struct pipe_context *pipe,
911 struct pipe_resource *resource)
912 {
913 }
914
915
916 /**
917 * Setup the pipe blit, resource_copy_region and flush_resource members.
918 */
919 void
920 svga_init_blit_functions(struct svga_context *svga)
921 {
922 svga->pipe.resource_copy_region = svga_resource_copy_region;
923 svga->pipe.blit = svga_blit;
924 svga->pipe.flush_resource = svga_flush_resource;
925 }