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