• 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/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 /**
74  * Copy an image between textures with the vgpu10 CopyRegion command.
75  */
76 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_face,struct pipe_resource * dst_tex,unsigned dst_x,unsigned dst_y,unsigned dst_z,unsigned dst_level,unsigned dst_face,unsigned width,unsigned height,unsigned depth)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
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)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
adjust_z_layer(enum pipe_texture_target target,int z_in,unsigned * layer_out,unsigned * z_out)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
formats_compatible(const struct svga_screen * ss,SVGA3dSurfaceFormat src_svga_fmt,SVGA3dSurfaceFormat dst_svga_fmt)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
is_blending_enabled(struct svga_context * svga,const struct pipe_blit_info * blit)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
check_blending_and_srgb_cond(struct svga_context * svga,const struct pipe_blit_info * blit)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
can_blit_via_svga_copy_region(struct svga_context * svga,const struct pipe_blit_info * blit_info)275 can_blit_via_svga_copy_region(struct svga_context *svga,
276                               const struct pipe_blit_info *blit_info)
277 {
278    struct pipe_blit_info local_blit = *blit_info;
279 
280    /* First basic checks to catch incompatibilities in new or locally unchecked
281     * struct pipe_blit_info members but bypass the format check here.
282     * Also since util_can_blit_via_copy_region() requires a dimension match,
283     * PIPE_FILTER_LINEAR should be equal to PIPE_FILTER_NEAREST.
284     */
285    local_blit.dst.format = local_blit.src.format;
286    if (local_blit.filter == PIPE_TEX_FILTER_LINEAR)
287       local_blit.filter = PIPE_TEX_FILTER_NEAREST;
288    if (!util_can_blit_via_copy_region(&local_blit, TRUE))
289       return false;
290 
291    /* For depth+stencil formats, copy with mask != PIPE_MASK_ZS is not
292     * supported
293     */
294    if (util_format_is_depth_and_stencil(blit_info->src.format) &&
295       blit_info->mask != (PIPE_MASK_ZS))
296      return false;
297 
298    return check_blending_and_srgb_cond(svga, blit_info);
299 }
300 
301 
302 /**
303  * The state tracker implements some resource copies with blits (for
304  * GL_ARB_copy_image).  This function checks if we should really do the blit
305  * with a VGPU10 CopyRegion command or software fallback (for incompatible
306  * src/dst formats).
307  */
308 static bool
can_blit_via_copy_region_vgpu10(struct svga_context * svga,const struct pipe_blit_info * blit_info)309 can_blit_via_copy_region_vgpu10(struct svga_context *svga,
310                                 const struct pipe_blit_info *blit_info)
311 {
312    struct svga_texture *dtex, *stex;
313 
314    /* can't copy between different resource types */
315    if (svga_resource_type(blit_info->src.resource->target) !=
316        svga_resource_type(blit_info->dst.resource->target))
317       return false;
318 
319    stex = svga_texture(blit_info->src.resource);
320    dtex = svga_texture(blit_info->dst.resource);
321 
322    if (!svga_have_vgpu10(svga))
323       return false;
324 
325    if (stex->handle == dtex->handle)
326       return false;
327 
328    return formats_compatible(svga_screen(svga->pipe.screen),
329                              stex->key.format,
330                              dtex->key.format);
331 }
332 
333 
334 /**
335  * Check whether we can blit using the surface_copy command.
336  */
337 static bool
can_blit_via_surface_copy(struct svga_context * svga,const struct pipe_blit_info * blit_info)338 can_blit_via_surface_copy(struct svga_context *svga,
339                           const struct pipe_blit_info *blit_info)
340 {
341    struct svga_texture *dtex, *stex;
342 
343    /* Mimic the format tests in util_can_blit_via_copy_region(), but
344     * skip the other tests that have already been performed.
345     */
346    if (blit_info->src.format != blit_info->dst.format) {
347       const struct util_format_description *src_desc, *dst_desc;
348 
349       src_desc = util_format_description(blit_info->src.resource->format);
350       dst_desc = util_format_description(blit_info->dst.resource->format);
351 
352       if (blit_info->src.resource->format != blit_info->src.format ||
353           blit_info->dst.resource->format != blit_info->dst.format ||
354           !util_is_format_compatible(src_desc, dst_desc))
355          return false;
356    }
357 
358    if (svga->render_condition && blit_info->render_condition_enable)
359       return false;
360 
361    /* can't copy between different resource types */
362    if (svga_resource_type(blit_info->src.resource->target) !=
363        svga_resource_type(blit_info->dst.resource->target))
364       return false;
365 
366    stex = svga_texture(blit_info->src.resource);
367    dtex = svga_texture(blit_info->dst.resource);
368 
369    if (stex->handle == dtex->handle)
370       return false;
371 
372    /*
373     * This is what we've been using before, but it can probably be
374     * relaxed. The device checks are less stringent.
375     */
376    return (stex->b.b.format == dtex->b.b.format);
377 }
378 
379 
380 /**
381  * Try region copy using one of the region copy commands
382  */
383 static bool
try_copy_region(struct svga_context * svga,const struct pipe_blit_info * blit)384 try_copy_region(struct svga_context *svga,
385                 const struct pipe_blit_info *blit)
386 {
387    unsigned src_face, src_z, dst_face, dst_z;
388 
389    if (!can_blit_via_svga_copy_region(svga, blit))
390       return false;
391 
392    adjust_z_layer(blit->src.resource->target, blit->src.box.z,
393                   &src_face, &src_z);
394 
395    adjust_z_layer(blit->dst.resource->target, blit->dst.box.z,
396                   &dst_face, &dst_z);
397 
398    if (can_blit_via_copy_region_vgpu10(svga, blit)) {
399       svga_toggle_render_condition(svga, blit->render_condition_enable, FALSE);
400 
401       copy_region_vgpu10(svga,
402                          blit->src.resource,
403                          blit->src.box.x, blit->src.box.y, src_z,
404                          blit->src.level, src_face,
405                          blit->dst.resource,
406                          blit->dst.box.x, blit->dst.box.y, dst_z,
407                          blit->dst.level, dst_face,
408                          blit->src.box.width, blit->src.box.height,
409                          blit->src.box.depth);
410 
411       svga_toggle_render_condition(svga, blit->render_condition_enable, TRUE);
412 
413       return true;
414    }
415 
416    if (can_blit_via_surface_copy(svga, blit)) {
417       struct svga_texture *stex = svga_texture(blit->src.resource);
418       struct svga_texture *dtex = svga_texture(blit->dst.resource);
419 
420       svga_surfaces_flush(svga);
421 
422       svga_texture_copy_handle(svga,
423                                stex->handle,
424                                blit->src.box.x, blit->src.box.y, src_z,
425                                blit->src.level, src_face,
426                                dtex->handle,
427                                blit->dst.box.x, blit->dst.box.y, dst_z,
428                                blit->dst.level, dst_face,
429                                blit->src.box.width, blit->src.box.height,
430                                blit->src.box.depth);
431 
432       svga_define_texture_level(dtex, dst_face, blit->dst.level);
433       svga_set_texture_rendered_to(dtex, dst_face, blit->dst.level);
434       return true;
435    }
436 
437    return false;
438 }
439 
440 
441 /**
442  * A helper function to determine if the specified view format
443  * is compatible with the surface format.
444  * It is compatible if the view format is the same as the surface format,
445  * or the associated svga format for the surface is a typeless format, or
446  * the view format is an adjusted format for BGRX/BGRA resource.
447  */
448 static bool
is_view_format_compatible(enum pipe_format surf_fmt,SVGA3dSurfaceFormat surf_svga_fmt,enum pipe_format view_fmt)449 is_view_format_compatible(enum pipe_format surf_fmt,
450                           SVGA3dSurfaceFormat surf_svga_fmt,
451                           enum pipe_format view_fmt)
452 {
453    if (surf_fmt == view_fmt || svga_format_is_typeless(surf_svga_fmt))
454       return true;
455 
456    if ((surf_fmt == PIPE_FORMAT_B8G8R8X8_UNORM &&
457         view_fmt == PIPE_FORMAT_B8G8R8A8_UNORM) ||
458        (surf_fmt == PIPE_FORMAT_B8G8R8A8_UNORM &&
459         view_fmt == PIPE_FORMAT_B8G8R8X8_UNORM))
460       return true;
461 
462    return false;
463 }
464 
465 
466 /**
467  * Try issuing a quad blit.
468  */
469 static bool
try_blit(struct svga_context * svga,const struct pipe_blit_info * blit_info)470 try_blit(struct svga_context *svga, const struct pipe_blit_info *blit_info)
471 {
472    struct svga_winsys_screen *sws = svga_screen(svga->pipe.screen)->sws;
473    struct pipe_resource *src = blit_info->src.resource;
474    struct pipe_resource *dst = blit_info->dst.resource;
475    struct pipe_resource *newSrc = NULL;
476    struct pipe_resource *newDst = NULL;
477    bool can_create_src_view;
478    bool can_create_dst_view;
479    bool ret = true;
480    struct pipe_blit_info blit = *blit_info;
481 
482    SVGA_STATS_TIME_PUSH(sws, SVGA_STATS_TIME_BLITBLITTER);
483 
484    /**
485     * If format is srgb and blend is enabled then color values need
486     * to be converted into linear format.
487     */
488    if (is_blending_enabled(svga, &blit))
489       blit.src.format = util_format_linear(blit.src.format);
490 
491    /* Check if we can create shader resource view and
492     * render target view for the quad blitter to work
493     */
494    can_create_src_view =
495       is_view_format_compatible(src->format, svga_texture(src)->key.format,
496                                 blit.src.format);
497 
498    can_create_dst_view =
499       is_view_format_compatible(dst->format, svga_texture(dst)->key.format,
500                                 blit.dst.format);
501 
502    if ((blit.mask & PIPE_MASK_S) ||
503        ((!can_create_dst_view || !can_create_src_view)
504         && !svga_have_vgpu10(svga))) {
505       /* Can't do stencil blits with textured quad blitter */
506       debug_warn_once("using software stencil blit");
507       ret = false;
508       goto done;
509    }
510 
511    if (!util_blitter_is_blit_supported(svga->blitter, &blit)) {
512       debug_printf("svga: blit unsupported %s -> %s\n",
513                    util_format_short_name(blit.src.resource->format),
514                    util_format_short_name(blit.dst.resource->format));
515       ret = false;
516       goto done;
517    }
518 
519    /* XXX turn off occlusion and streamout queries */
520 
521    util_blitter_save_vertex_buffer_slot(svga->blitter, svga->curr.vb);
522    util_blitter_save_vertex_elements(svga->blitter, (void*)svga->curr.velems);
523    util_blitter_save_vertex_shader(svga->blitter, svga->curr.vs);
524    util_blitter_save_geometry_shader(svga->blitter, svga->curr.user_gs);
525    util_blitter_save_so_targets(svga->blitter, svga->num_so_targets,
526                      (struct pipe_stream_output_target**)svga->so_targets);
527    util_blitter_save_rasterizer(svga->blitter, (void*)svga->curr.rast);
528    util_blitter_save_viewport(svga->blitter, &svga->curr.viewport);
529    util_blitter_save_scissor(svga->blitter, &svga->curr.scissor);
530    util_blitter_save_fragment_shader(svga->blitter, svga->curr.fs);
531    util_blitter_save_blend(svga->blitter, (void*)svga->curr.blend);
532    util_blitter_save_depth_stencil_alpha(svga->blitter,
533                                          (void*)svga->curr.depth);
534    util_blitter_save_stencil_ref(svga->blitter, &svga->curr.stencil_ref);
535    util_blitter_save_sample_mask(svga->blitter, svga->curr.sample_mask);
536    util_blitter_save_framebuffer(svga->blitter, &svga->curr.framebuffer);
537    util_blitter_save_fragment_sampler_states(svga->blitter,
538                      svga->curr.num_samplers[PIPE_SHADER_FRAGMENT],
539                      (void**)svga->curr.sampler[PIPE_SHADER_FRAGMENT]);
540    util_blitter_save_fragment_sampler_views(svga->blitter,
541                      svga->curr.num_sampler_views[PIPE_SHADER_FRAGMENT],
542                      svga->curr.sampler_views[PIPE_SHADER_FRAGMENT]);
543 
544    if (!can_create_src_view) {
545       struct pipe_resource template;
546       struct pipe_blit_info copy_region_blit;
547 
548       /**
549        * If the source blit format is not compatible with the source resource
550        * format, we will not be able to create a shader resource view.
551        * In order to avoid falling back to software blit, we'll create
552        * a new resource in the blit format, and use DXCopyResource to
553        * copy from the original format to the new format. The new
554        * resource will be used for the blit in util_blitter_blit().
555        */
556       template = *src;
557       template.format = blit.src.format;
558       newSrc = svga_texture_create(svga->pipe.screen, &template);
559       if (newSrc == NULL) {
560          debug_printf("svga_blit: fails to create temporary src\n");
561          ret = false;
562          goto done;
563       }
564 
565       /* increment the mksStats for blitter with extra copy */
566       SVGA_STATS_COUNT_INC(sws, SVGA_STATS_COUNT_BLITBLITTERCOPY);
567       build_blit_info(newSrc,
568                       blit.src.level, blit.src.box.x,
569                       blit.src.box.y, blit.src.box.z,
570                       blit.src.resource,
571                       blit.src.level, &blit.src.box,
572                       &copy_region_blit);
573       if (!try_copy_region(svga, &copy_region_blit)) {
574          debug_printf("svga: Source blit format conversion failed.\n");
575          ret = false;
576          goto done;
577       }
578 
579       blit.src.resource = newSrc;
580    }
581 
582    if (!can_create_dst_view) {
583       struct pipe_resource template;
584 
585       /*
586        * If the destination blit format is not compatible with the destination
587        * resource format, we will not be able to create a render target view.
588        * In order to avoid falling back to software blit, we'll create
589        * a new resource in the blit format, and use DXPredCopyRegion
590        * after the blit to copy from the blit format back to the resource
591        * format.
592        */
593       template = *dst;
594       template.format = blit.dst.format;
595       newDst = svga_texture_create(svga->pipe.screen, &template);
596       if (newDst == NULL) {
597          debug_printf("svga_blit: fails to create temporary dst\n");
598          ret = false;
599          goto done;
600       }
601 
602       blit.dst.resource = newDst;
603    }
604 
605    svga_toggle_render_condition(svga, blit.render_condition_enable, FALSE);
606 
607    util_blitter_blit(svga->blitter, &blit);
608 
609    svga_toggle_render_condition(svga, blit.render_condition_enable, TRUE);
610 
611    if (blit.dst.resource != dst) {
612       struct pipe_blit_info copy_region_blit;
613 
614       /* increment the mksStats for blitter with extra copy */
615       SVGA_STATS_COUNT_INC(sws, SVGA_STATS_COUNT_BLITBLITTERCOPY);
616 
617       /*
618        * A temporary resource was created for the blit, we need to
619        * copy from the temporary resource back to the original destination.
620        */
621       build_blit_info(dst,
622                       blit.dst.level, blit.dst.box.x,
623                       blit.dst.box.y, blit.dst.box.z,
624                       newDst,
625                       blit.dst.level, &blit.dst.box,
626                       &copy_region_blit);
627       if (!try_copy_region(svga, &copy_region_blit)) {
628          debug_printf("svga: Destination blit format conversion failed.\n");
629          ret = false;
630          goto done;
631       }
632    }
633 
634 done:
635    /* unreference the temporary resources if needed */
636    pipe_resource_reference(&newDst, NULL);
637    pipe_resource_reference(&newSrc, NULL);
638 
639    SVGA_STATS_TIME_POP(sws);  /* SVGA_STATS_TIME_BLITBLITTER */
640    (void) sws;
641 
642    return ret;
643 }
644 
645 
646 /**
647  * Try a cpu copy_region fallback.
648  */
649 static bool
try_cpu_copy_region(struct svga_context * svga,const struct pipe_blit_info * blit)650 try_cpu_copy_region(struct svga_context *svga,
651                     const struct pipe_blit_info *blit)
652 {
653    if (util_can_blit_via_copy_region(blit, TRUE) ||
654        util_can_blit_via_copy_region(blit, FALSE)) {
655 
656       if (svga->render_condition && blit->render_condition_enable) {
657          debug_warning("CPU copy_region doesn't support "
658                        "conditional rendering.\n");
659          return false;
660       }
661 
662       copy_region_fallback(svga, blit->dst.resource,
663                            blit->dst.level,
664                            blit->dst.box.x, blit->dst.box.y,
665                            blit->dst.box.z, blit->src.resource,
666                            blit->src.level, &blit->src.box);
667       return true;
668    }
669 
670    return false;
671 }
672 
673 
674 /**
675  * The pipe::blit member.
676  */
677 static void
svga_blit(struct pipe_context * pipe,const struct pipe_blit_info * blit)678 svga_blit(struct pipe_context *pipe,
679           const struct pipe_blit_info *blit)
680 {
681    struct svga_context *svga = svga_context(pipe);
682    struct svga_winsys_screen *sws = svga_screen(pipe->screen)->sws;
683 
684    if (!svga_have_vgpu10(svga) &&
685        blit->src.resource->nr_samples > 1 &&
686        blit->dst.resource->nr_samples <= 1 &&
687        !util_format_is_depth_or_stencil(blit->src.resource->format) &&
688        !util_format_is_pure_integer(blit->src.resource->format)) {
689       debug_printf("svga: color resolve unimplemented\n");
690       return;
691    }
692 
693    SVGA_STATS_TIME_PUSH(sws, SVGA_STATS_TIME_BLIT);
694 
695    if (try_copy_region(svga, blit))
696       goto done;
697 
698    if (try_blit(svga, blit))
699       goto done;
700 
701    if (!try_cpu_copy_region(svga, blit))
702       debug_printf("svga: Blit failed.\n");
703 
704 done:
705    SVGA_STATS_TIME_POP(sws);  /* SVGA_STATS_TIME_BLIT */
706    (void) sws;
707 }
708 
709 
710 /**
711  * The pipe::resource_copy_region member.
712  */
713 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)714 svga_resource_copy_region(struct pipe_context *pipe,
715                           struct pipe_resource *dst_tex,
716                           unsigned dst_level,
717                           unsigned dstx, unsigned dsty, unsigned dstz,
718                           struct pipe_resource *src_tex,
719                           unsigned src_level,
720                           const struct pipe_box *src_box)
721 {
722    struct svga_context *svga = svga_context(pipe);
723    struct svga_winsys_screen *sws = svga_screen(svga->pipe.screen)->sws;
724 
725    SVGA_STATS_TIME_PUSH(sws, SVGA_STATS_TIME_COPYREGION);
726 
727    if (dst_tex->target == PIPE_BUFFER && src_tex->target == PIPE_BUFFER) {
728       /* can't copy within the same buffer, unfortunately */
729       if (svga_have_vgpu10(svga) && src_tex != dst_tex) {
730          enum pipe_error ret;
731          struct svga_winsys_surface *src_surf;
732          struct svga_winsys_surface *dst_surf;
733          struct svga_buffer *dbuffer = svga_buffer(dst_tex);
734          struct svga_buffer *sbuffer = svga_buffer(src_tex);
735 
736          src_surf = svga_buffer_handle(svga, src_tex, sbuffer->bind_flags);
737          dst_surf = svga_buffer_handle(svga, dst_tex, dbuffer->bind_flags);
738 
739          ret = SVGA3D_vgpu10_BufferCopy(svga->swc, src_surf, dst_surf,
740                                         src_box->x, dstx, src_box->width);
741          if (ret != PIPE_OK) {
742             svga_context_flush(svga, NULL);
743             ret = SVGA3D_vgpu10_BufferCopy(svga->swc, src_surf, dst_surf,
744                                            src_box->x, dstx, src_box->width);
745             assert(ret == PIPE_OK);
746          }
747 
748          dbuffer->dirty = TRUE;
749       }
750       else {
751          /* use map/memcpy fallback */
752          copy_region_fallback(svga, dst_tex, dst_level, dstx,
753                               dsty, dstz, src_tex, src_level, src_box);
754       }
755    } else {
756       struct pipe_blit_info blit;
757 
758       build_blit_info(dst_tex, dst_level, dstx, dsty, dstz,
759                       src_tex, src_level, src_box, &blit);
760 
761       if (try_copy_region(svga, &blit))
762          goto done;
763 
764       /* Blits are format-converting which is not what we want, so perform a
765        * strict format-check.
766        * FIXME: Need to figure out why srgb blits (tf2) and
767        * 3D blits (piglit) are broken here. Perhaps we set up the
768        * struct pipe_blit_info incorrectly.
769        */
770       if (src_tex->format == dst_tex->format &&
771           !util_format_is_srgb(src_tex->format) &&
772           svga_resource_type(src_tex->target) != SVGA3D_RESOURCE_TEXTURE3D &&
773           try_blit(svga, &blit))
774          goto done;
775 
776       copy_region_fallback(svga, dst_tex, dst_level, dstx, dsty, dstz,
777                            src_tex, src_level, src_box);
778    }
779 
780 done:
781    SVGA_STATS_TIME_POP(sws);
782    (void) sws;
783 }
784 
785 
786 /**
787  * The pipe::flush_resource member.
788  */
789 static void
svga_flush_resource(struct pipe_context * pipe,struct pipe_resource * resource)790 svga_flush_resource(struct pipe_context *pipe,
791                     struct pipe_resource *resource)
792 {
793 }
794 
795 
796 /**
797  * Setup the pipe blit, resource_copy_region and flush_resource members.
798  */
799 void
svga_init_blit_functions(struct svga_context * svga)800 svga_init_blit_functions(struct svga_context *svga)
801 {
802    svga->pipe.resource_copy_region = svga_resource_copy_region;
803    svga->pipe.blit = svga_blit;
804    svga->pipe.flush_resource = svga_flush_resource;
805 }
806