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