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