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