• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 subresource as rendered-to. */
107    svga_set_texture_rendered_to(stex, layer_face, level);
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 subresource as rendered-to. */
143    svga_set_texture_rendered_to(dtex, dst_layer_face, dst_level);
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))
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.b.format == dtex->b.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, dst_layer_face, blit->dst.level);
508       return true;
509    }
510 
511    if (can_blit_via_intra_surface_copy(svga, blit)) {
512       intra_surface_copy(svga,
513                          blit->src.resource,
514                          blit->src.box.x, blit->src.box.y, src_z,
515                          blit->src.level, src_layer_face,
516                          blit->dst.box.x, blit->dst.box.y, dst_z,
517                          blit->src.box.width, blit->src.box.height,
518                          blit->src.box.depth);
519       return true;
520    }
521 
522    return false;
523 }
524 
525 
526 /**
527  * A helper function to determine if the specified view format
528  * is compatible with the surface format.
529  * It is compatible if the view format is the same as the surface format,
530  * or the associated svga format for the surface is a typeless format, or
531  * the view format is an adjusted format for BGRX/BGRA resource.
532  */
533 static bool
is_view_format_compatible(enum pipe_format surf_fmt,SVGA3dSurfaceFormat surf_svga_fmt,enum pipe_format view_fmt)534 is_view_format_compatible(enum pipe_format surf_fmt,
535                           SVGA3dSurfaceFormat surf_svga_fmt,
536                           enum pipe_format view_fmt)
537 {
538    if (surf_fmt == view_fmt || svga_format_is_typeless(surf_svga_fmt))
539       return true;
540 
541    if ((surf_fmt == PIPE_FORMAT_B8G8R8X8_UNORM &&
542         view_fmt == PIPE_FORMAT_B8G8R8A8_UNORM) ||
543        (surf_fmt == PIPE_FORMAT_B8G8R8A8_UNORM &&
544         view_fmt == PIPE_FORMAT_B8G8R8X8_UNORM))
545       return true;
546 
547    return false;
548 }
549 
550 
551 /**
552  * Try issuing a quad blit.
553  */
554 static bool
try_blit(struct svga_context * svga,const struct pipe_blit_info * blit_info)555 try_blit(struct svga_context *svga, const struct pipe_blit_info *blit_info)
556 {
557    struct svga_winsys_screen *sws = svga_screen(svga->pipe.screen)->sws;
558    struct pipe_resource *src = blit_info->src.resource;
559    struct pipe_resource *dst = blit_info->dst.resource;
560    struct pipe_resource *newSrc = NULL;
561    struct pipe_resource *newDst = NULL;
562    bool can_create_src_view;
563    bool can_create_dst_view;
564    bool ret = true;
565    struct pipe_blit_info blit = *blit_info;
566 
567    SVGA_STATS_TIME_PUSH(sws, SVGA_STATS_TIME_BLITBLITTER);
568 
569    /**
570     * Avoid using util_blitter_blit() for these depth formats on non-vgpu10
571     * devices because these depth formats only support comparison mode
572     * and not ordinary sampling.
573     */
574    if (!svga_have_vgpu10(svga) && (blit.mask & PIPE_MASK_Z) &&
575        (svga_texture(dst)->key.format == SVGA3D_Z_D16 ||
576        svga_texture(dst)->key.format == SVGA3D_Z_D24X8 ||
577        svga_texture(dst)->key.format == SVGA3D_Z_D24S8)) {
578       ret = false;
579       goto done;
580   }
581 
582    /**
583     * If format is srgb and blend is enabled then color values need
584     * to be converted into linear format.
585     */
586    if (is_blending_enabled(svga, &blit)) {
587       blit.src.format = util_format_linear(blit.src.format);
588       blit.dst.format = util_format_linear(blit.dst.format);
589    }
590 
591    /* Check if we can create shader resource view and
592     * render target view for the quad blitter to work
593     */
594    can_create_src_view =
595       is_view_format_compatible(src->format, svga_texture(src)->key.format,
596                                 blit.src.format);
597 
598    can_create_dst_view =
599       is_view_format_compatible(dst->format, svga_texture(dst)->key.format,
600                                 blit.dst.format);
601 
602    if ((blit.mask & PIPE_MASK_S) ||
603        ((!can_create_dst_view || !can_create_src_view)
604         && !svga_have_vgpu10(svga))) {
605       /* Can't do stencil blits with textured quad blitter */
606       debug_warn_once("using software stencil blit");
607       ret = false;
608       goto done;
609    }
610 
611    if (!util_blitter_is_blit_supported(svga->blitter, &blit)) {
612       debug_printf("svga: blit unsupported %s -> %s\n",
613                    util_format_short_name(blit.src.resource->format),
614                    util_format_short_name(blit.dst.resource->format));
615       ret = false;
616       goto done;
617    }
618 
619    /* XXX turn off occlusion and streamout queries */
620 
621    util_blitter_save_vertex_buffer_slot(svga->blitter, svga->curr.vb);
622    util_blitter_save_vertex_elements(svga->blitter, (void*)svga->curr.velems);
623    util_blitter_save_vertex_shader(svga->blitter, svga->curr.vs);
624    util_blitter_save_geometry_shader(svga->blitter, svga->curr.user_gs);
625    util_blitter_save_tessctrl_shader(svga->blitter, svga->curr.tcs);
626    util_blitter_save_tesseval_shader(svga->blitter, svga->curr.tes);
627    util_blitter_save_so_targets(svga->blitter, svga->num_so_targets,
628                      (struct pipe_stream_output_target**)svga->so_targets);
629    util_blitter_save_rasterizer(svga->blitter, (void*)svga->curr.rast);
630    util_blitter_save_viewport(svga->blitter, &svga->curr.viewport[0]);
631    util_blitter_save_scissor(svga->blitter, &svga->curr.scissor[0]);
632    util_blitter_save_fragment_shader(svga->blitter, svga->curr.fs);
633    util_blitter_save_blend(svga->blitter, (void*)svga->curr.blend);
634    util_blitter_save_depth_stencil_alpha(svga->blitter,
635                                          (void*)svga->curr.depth);
636    util_blitter_save_stencil_ref(svga->blitter, &svga->curr.stencil_ref);
637    util_blitter_save_sample_mask(svga->blitter, svga->curr.sample_mask);
638    util_blitter_save_framebuffer(svga->blitter, &svga->curr.framebuffer);
639    util_blitter_save_fragment_sampler_states(svga->blitter,
640                      svga->curr.num_samplers[PIPE_SHADER_FRAGMENT],
641                      (void**)svga->curr.sampler[PIPE_SHADER_FRAGMENT]);
642    util_blitter_save_fragment_sampler_views(svga->blitter,
643                      svga->curr.num_sampler_views[PIPE_SHADER_FRAGMENT],
644                      svga->curr.sampler_views[PIPE_SHADER_FRAGMENT]);
645 
646    if (!can_create_src_view) {
647       struct pipe_resource template;
648       struct pipe_blit_info copy_region_blit;
649 
650       /**
651        * If the source blit format is not compatible with the source resource
652        * format, we will not be able to create a shader resource view.
653        * In order to avoid falling back to software blit, we'll create
654        * a new resource in the blit format, and use DXCopyResource to
655        * copy from the original format to the new format. The new
656        * resource will be used for the blit in util_blitter_blit().
657        */
658       template = *src;
659       template.format = blit.src.format;
660       newSrc = svga_texture_create(svga->pipe.screen, &template);
661       if (newSrc == NULL) {
662          debug_printf("svga_blit: fails to create temporary src\n");
663          ret = false;
664          goto done;
665       }
666 
667       /* increment the mksStats for blitter with extra copy */
668       SVGA_STATS_COUNT_INC(sws, SVGA_STATS_COUNT_BLITBLITTERCOPY);
669       build_blit_info(newSrc,
670                       blit.src.level, blit.src.box.x,
671                       blit.src.box.y, blit.src.box.z,
672                       blit.src.resource,
673                       blit.src.level, &blit.src.box,
674                       &copy_region_blit);
675       if (!try_copy_region(svga, &copy_region_blit)) {
676          debug_printf("svga: Source blit format conversion failed.\n");
677          ret = false;
678          goto done;
679       }
680 
681       blit.src.resource = newSrc;
682    }
683 
684    if (!can_create_dst_view) {
685       struct pipe_resource template;
686 
687       /*
688        * If the destination blit format is not compatible with the destination
689        * resource format, we will not be able to create a render target view.
690        * In order to avoid falling back to software blit, we'll create
691        * a new resource in the blit format, and use DXPredCopyRegion
692        * after the blit to copy from the blit format back to the resource
693        * format.
694        */
695       template = *dst;
696       template.format = blit.dst.format;
697       newDst = svga_texture_create(svga->pipe.screen, &template);
698       if (newDst == NULL) {
699          debug_printf("svga_blit: fails to create temporary dst\n");
700          ret = false;
701          goto done;
702       }
703 
704       blit.dst.resource = newDst;
705    }
706 
707    svga_toggle_render_condition(svga, blit.render_condition_enable, FALSE);
708 
709    util_blitter_blit(svga->blitter, &blit);
710 
711    svga_toggle_render_condition(svga, blit.render_condition_enable, TRUE);
712 
713    if (blit.dst.resource != dst) {
714       struct pipe_blit_info copy_region_blit;
715 
716       /* increment the mksStats for blitter with extra copy */
717       SVGA_STATS_COUNT_INC(sws, SVGA_STATS_COUNT_BLITBLITTERCOPY);
718 
719       /*
720        * A temporary resource was created for the blit, we need to
721        * copy from the temporary resource back to the original destination.
722        */
723       build_blit_info(dst,
724                       blit.dst.level, blit.dst.box.x,
725                       blit.dst.box.y, blit.dst.box.z,
726                       newDst,
727                       blit.dst.level, &blit.dst.box,
728                       &copy_region_blit);
729       if (!try_copy_region(svga, &copy_region_blit)) {
730          debug_printf("svga: Destination blit format conversion failed.\n");
731          ret = false;
732          goto done;
733       }
734    }
735 
736 done:
737    /* unreference the temporary resources if needed */
738    pipe_resource_reference(&newDst, NULL);
739    pipe_resource_reference(&newSrc, NULL);
740 
741    SVGA_STATS_TIME_POP(sws);  /* SVGA_STATS_TIME_BLITBLITTER */
742    (void) sws;
743 
744    return ret;
745 }
746 
747 
748 /**
749  * Try a cpu copy_region fallback.
750  */
751 static bool
try_cpu_copy_region(struct svga_context * svga,const struct pipe_blit_info * blit)752 try_cpu_copy_region(struct svga_context *svga,
753                     const struct pipe_blit_info *blit)
754 {
755    if (util_can_blit_via_copy_region(blit, TRUE) ||
756        util_can_blit_via_copy_region(blit, FALSE)) {
757 
758       if (svga->render_condition && blit->render_condition_enable) {
759          debug_warning("CPU copy_region doesn't support "
760                        "conditional rendering.\n");
761          return false;
762       }
763 
764       copy_region_fallback(svga, blit->dst.resource,
765                            blit->dst.level,
766                            blit->dst.box.x, blit->dst.box.y,
767                            blit->dst.box.z, blit->src.resource,
768                            blit->src.level, &blit->src.box);
769       return true;
770    }
771 
772    return false;
773 }
774 
775 
776 /**
777  * The pipe::blit member.
778  */
779 static void
svga_blit(struct pipe_context * pipe,const struct pipe_blit_info * blit)780 svga_blit(struct pipe_context *pipe,
781           const struct pipe_blit_info *blit)
782 {
783    struct svga_context *svga = svga_context(pipe);
784    struct svga_winsys_screen *sws = svga_screen(pipe->screen)->sws;
785 
786    if (!svga_have_vgpu10(svga) &&
787        blit->src.resource->nr_samples > 1 &&
788        blit->dst.resource->nr_samples <= 1 &&
789        !util_format_is_depth_or_stencil(blit->src.resource->format) &&
790        !util_format_is_pure_integer(blit->src.resource->format)) {
791       debug_printf("svga: color resolve unimplemented\n");
792       return;
793    }
794 
795    SVGA_STATS_TIME_PUSH(sws, SVGA_STATS_TIME_BLIT);
796 
797    if (try_copy_region(svga, blit))
798       goto done;
799 
800    if (try_blit(svga, blit))
801       goto done;
802 
803    if (!try_cpu_copy_region(svga, blit))
804       debug_printf("svga: Blit failed.\n");
805 
806 done:
807    SVGA_STATS_TIME_POP(sws);  /* SVGA_STATS_TIME_BLIT */
808    (void) sws;
809 }
810 
811 
812 /**
813  * The pipe::resource_copy_region member.
814  */
815 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)816 svga_resource_copy_region(struct pipe_context *pipe,
817                           struct pipe_resource *dst_tex,
818                           unsigned dst_level,
819                           unsigned dstx, unsigned dsty, unsigned dstz,
820                           struct pipe_resource *src_tex,
821                           unsigned src_level,
822                           const struct pipe_box *src_box)
823 {
824    struct svga_context *svga = svga_context(pipe);
825    struct svga_winsys_screen *sws = svga_screen(svga->pipe.screen)->sws;
826 
827    SVGA_STATS_TIME_PUSH(sws, SVGA_STATS_TIME_COPYREGION);
828 
829    if (dst_tex->target == PIPE_BUFFER && src_tex->target == PIPE_BUFFER) {
830       /* can't copy within the same buffer, unfortunately */
831       if (svga_have_vgpu10(svga) && src_tex != dst_tex) {
832          struct svga_winsys_surface *src_surf;
833          struct svga_winsys_surface *dst_surf;
834          struct svga_buffer *dbuffer = svga_buffer(dst_tex);
835          struct svga_buffer *sbuffer = svga_buffer(src_tex);
836 
837          src_surf = svga_buffer_handle(svga, src_tex, sbuffer->bind_flags);
838          dst_surf = svga_buffer_handle(svga, dst_tex, dbuffer->bind_flags);
839 
840          SVGA_RETRY(svga, SVGA3D_vgpu10_BufferCopy(svga->swc, src_surf,
841                                                    dst_surf, src_box->x, dstx,
842                                                    src_box->width));
843          dbuffer->dirty = TRUE;
844       }
845       else {
846          /* use map/memcpy fallback */
847          copy_region_fallback(svga, dst_tex, dst_level, dstx,
848                               dsty, dstz, src_tex, src_level, src_box);
849       }
850    } else {
851       struct pipe_blit_info blit;
852 
853       build_blit_info(dst_tex, dst_level, dstx, dsty, dstz,
854                       src_tex, src_level, src_box, &blit);
855 
856       if (try_copy_region(svga, &blit))
857          goto done;
858 
859       /* Blits are format-converting which is not what we want, so perform a
860        * strict format-check.
861        * FIXME: Need to figure out why srgb blits (tf2) and
862        * 3D blits (piglit) are broken here. Perhaps we set up the
863        * struct pipe_blit_info incorrectly.
864        */
865       if (src_tex->format == dst_tex->format &&
866           !util_format_is_srgb(src_tex->format) &&
867           svga_resource_type(src_tex->target) != SVGA3D_RESOURCE_TEXTURE3D &&
868           try_blit(svga, &blit))
869          goto done;
870 
871       copy_region_fallback(svga, dst_tex, dst_level, dstx, dsty, dstz,
872                            src_tex, src_level, src_box);
873    }
874 
875 done:
876    SVGA_STATS_TIME_POP(sws);
877    (void) sws;
878 }
879 
880 
881 /**
882  * The pipe::flush_resource member.
883  */
884 static void
svga_flush_resource(struct pipe_context * pipe,struct pipe_resource * resource)885 svga_flush_resource(struct pipe_context *pipe,
886                     struct pipe_resource *resource)
887 {
888 }
889 
890 
891 /**
892  * Setup the pipe blit, resource_copy_region and flush_resource members.
893  */
894 void
svga_init_blit_functions(struct svga_context * svga)895 svga_init_blit_functions(struct svga_context *svga)
896 {
897    svga->pipe.resource_copy_region = svga_resource_copy_region;
898    svga->pipe.blit = svga_blit;
899    svga->pipe.flush_resource = svga_flush_resource;
900 }
901