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