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