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 /* can't copy between different resource types */
372 if (svga_resource_type(blit_info->src.resource->target) !=
373 svga_resource_type(blit_info->dst.resource->target))
374 return false;
375
376 stex = svga_texture(blit_info->src.resource);
377 dtex = svga_texture(blit_info->dst.resource);
378
379 if (!svga_have_vgpu10(svga))
380 return false;
381
382 if (stex->handle == dtex->handle)
383 return false;
384
385 return formats_compatible(svga_screen(svga->pipe.screen),
386 stex->key.format,
387 dtex->key.format);
388 }
389
390
391 /**
392 * Check whether we can blit using the surface_copy command.
393 */
394 static bool
can_blit_via_surface_copy(struct svga_context * svga,const struct pipe_blit_info * blit_info)395 can_blit_via_surface_copy(struct svga_context *svga,
396 const struct pipe_blit_info *blit_info)
397 {
398 struct svga_texture *dtex, *stex;
399
400 /* Mimic the format tests in util_can_blit_via_copy_region(), but
401 * skip the other tests that have already been performed.
402 */
403 if (blit_info->src.format != blit_info->dst.format) {
404 const struct util_format_description *src_desc, *dst_desc;
405
406 src_desc = util_format_description(blit_info->src.resource->format);
407 dst_desc = util_format_description(blit_info->dst.resource->format);
408
409 if (blit_info->src.resource->format != blit_info->src.format ||
410 blit_info->dst.resource->format != blit_info->dst.format ||
411 !util_is_format_compatible(src_desc, dst_desc))
412 return false;
413 }
414
415 if (svga->render_condition && blit_info->render_condition_enable)
416 return false;
417
418 /* can't copy between different resource types */
419 if (svga_resource_type(blit_info->src.resource->target) !=
420 svga_resource_type(blit_info->dst.resource->target))
421 return false;
422
423 stex = svga_texture(blit_info->src.resource);
424 dtex = svga_texture(blit_info->dst.resource);
425
426 if (stex->handle == dtex->handle)
427 return false;
428
429 /*
430 * This is what we've been using before, but it can probably be
431 * relaxed. The device checks are less stringent.
432 */
433 return (stex->b.format == dtex->b.format);
434 }
435
436
437 /**
438 * Try region copy using one of the region copy commands
439 */
440 static bool
try_copy_region(struct svga_context * svga,const struct pipe_blit_info * blit)441 try_copy_region(struct svga_context *svga,
442 const struct pipe_blit_info *blit)
443 {
444 unsigned src_layer_face, src_z, dst_layer_face, dst_z;
445
446 if (!can_blit_via_svga_copy_region(svga, blit))
447 return false;
448
449 adjust_z_layer(blit->src.resource->target, blit->src.box.z,
450 &src_layer_face, &src_z);
451
452 adjust_z_layer(blit->dst.resource->target, blit->dst.box.z,
453 &dst_layer_face, &dst_z);
454
455 if (can_blit_via_copy_region_vgpu10(svga, blit)) {
456 svga_toggle_render_condition(svga, blit->render_condition_enable, false);
457
458 copy_region_vgpu10(svga,
459 blit->src.resource,
460 blit->src.box.x, blit->src.box.y, src_z,
461 blit->src.level, src_layer_face,
462 blit->dst.resource,
463 blit->dst.box.x, blit->dst.box.y, dst_z,
464 blit->dst.level, dst_layer_face,
465 blit->src.box.width, blit->src.box.height,
466 blit->src.box.depth);
467
468 svga_toggle_render_condition(svga, blit->render_condition_enable, true);
469
470 return true;
471 }
472
473 if (can_blit_via_surface_copy(svga, blit)) {
474 struct svga_texture *stex = svga_texture(blit->src.resource);
475 struct svga_texture *dtex = svga_texture(blit->dst.resource);
476
477 svga_surfaces_flush(svga);
478
479 svga_texture_copy_handle(svga,
480 stex->handle,
481 blit->src.box.x, blit->src.box.y, src_z,
482 blit->src.level, src_layer_face,
483 dtex->handle,
484 blit->dst.box.x, blit->dst.box.y, dst_z,
485 blit->dst.level, dst_layer_face,
486 blit->src.box.width, blit->src.box.height,
487 blit->src.box.depth);
488
489 svga_define_texture_level(dtex, dst_layer_face, blit->dst.level);
490 svga_set_texture_rendered_to(dtex);
491
492 return true;
493 }
494
495 if (can_blit_via_intra_surface_copy(svga, blit)) {
496 intra_surface_copy(svga,
497 blit->src.resource,
498 blit->src.box.x, blit->src.box.y, src_z,
499 blit->src.level, src_layer_face,
500 blit->dst.box.x, blit->dst.box.y, dst_z,
501 blit->src.box.width, blit->src.box.height,
502 blit->src.box.depth);
503 return true;
504 }
505
506 return false;
507 }
508
509
510 /**
511 * A helper function to determine if the specified view format
512 * is compatible with the surface format.
513 * It is compatible if the view format is the same as the surface format,
514 * or the associated svga format for the surface is a typeless format, or
515 * the view format is an adjusted format for BGRX/BGRA resource.
516 */
517 static bool
is_view_format_compatible(enum pipe_format surf_fmt,SVGA3dSurfaceFormat surf_svga_fmt,enum pipe_format view_fmt)518 is_view_format_compatible(enum pipe_format surf_fmt,
519 SVGA3dSurfaceFormat surf_svga_fmt,
520 enum pipe_format view_fmt)
521 {
522 if (surf_fmt == view_fmt || svga_format_is_typeless(surf_svga_fmt))
523 return true;
524
525 if ((surf_fmt == PIPE_FORMAT_B8G8R8X8_UNORM &&
526 view_fmt == PIPE_FORMAT_B8G8R8A8_UNORM) ||
527 (surf_fmt == PIPE_FORMAT_B8G8R8A8_UNORM &&
528 view_fmt == PIPE_FORMAT_B8G8R8X8_UNORM))
529 return true;
530
531 return false;
532 }
533
534
535 /**
536 * Try issuing a quad blit.
537 */
538 static bool
try_blit(struct svga_context * svga,const struct pipe_blit_info * blit_info)539 try_blit(struct svga_context *svga, const struct pipe_blit_info *blit_info)
540 {
541 struct svga_winsys_screen *sws = svga_screen(svga->pipe.screen)->sws;
542 struct pipe_resource *src = blit_info->src.resource;
543 struct pipe_resource *dst = blit_info->dst.resource;
544 struct pipe_resource *newSrc = NULL;
545 struct pipe_resource *newDst = NULL;
546 bool can_create_src_view;
547 bool can_create_dst_view;
548 bool ret = true;
549 struct pipe_blit_info blit = *blit_info;
550
551 SVGA_STATS_TIME_PUSH(sws, SVGA_STATS_TIME_BLITBLITTER);
552
553 /**
554 * Avoid using util_blitter_blit() for these depth formats on non-vgpu10
555 * devices because these depth formats only support comparison mode
556 * and not ordinary sampling.
557 */
558 if (!svga_have_vgpu10(svga) && (blit.mask & PIPE_MASK_Z) &&
559 (svga_texture(dst)->key.format == SVGA3D_Z_D16 ||
560 svga_texture(dst)->key.format == SVGA3D_Z_D24X8 ||
561 svga_texture(dst)->key.format == SVGA3D_Z_D24S8)) {
562 ret = false;
563 goto done;
564 }
565
566 /**
567 * If format is srgb and blend is enabled then color values need
568 * to be converted into linear format.
569 */
570 if (is_blending_enabled(svga, &blit)) {
571 blit.src.format = util_format_linear(blit.src.format);
572 blit.dst.format = util_format_linear(blit.dst.format);
573 }
574
575 /* Check if we can create shader resource view and
576 * render target view for the quad blitter to work
577 */
578 can_create_src_view =
579 is_view_format_compatible(src->format, svga_texture(src)->key.format,
580 blit.src.format);
581
582 can_create_dst_view =
583 is_view_format_compatible(dst->format, svga_texture(dst)->key.format,
584 blit.dst.format);
585
586 if ((blit.mask & PIPE_MASK_S) ||
587 ((!can_create_dst_view || !can_create_src_view)
588 && !svga_have_vgpu10(svga))) {
589 /* Can't do stencil blits with textured quad blitter */
590 debug_warn_once("using software stencil blit");
591 ret = false;
592 goto done;
593 }
594
595 if (!util_blitter_is_blit_supported(svga->blitter, &blit)) {
596 debug_printf("svga: blit unsupported %s -> %s\n",
597 util_format_short_name(blit.src.resource->format),
598 util_format_short_name(blit.dst.resource->format));
599 ret = false;
600 goto done;
601 }
602
603 /* XXX turn off occlusion and streamout queries */
604
605 util_blitter_save_vertex_buffers(svga->blitter, svga->curr.vb,
606 svga->curr.num_vertex_buffers);
607 util_blitter_save_vertex_elements(svga->blitter, (void*)svga->curr.velems);
608 util_blitter_save_vertex_shader(svga->blitter, svga->curr.vs);
609 util_blitter_save_geometry_shader(svga->blitter, svga->curr.user_gs);
610 util_blitter_save_tessctrl_shader(svga->blitter, svga->curr.tcs);
611 util_blitter_save_tesseval_shader(svga->blitter, svga->curr.tes);
612 util_blitter_save_so_targets(svga->blitter, svga->num_so_targets,
613 (struct pipe_stream_output_target**)svga->so_targets);
614 util_blitter_save_rasterizer(svga->blitter, (void*)svga->curr.rast);
615 util_blitter_save_viewport(svga->blitter, &svga->curr.viewport[0]);
616 util_blitter_save_scissor(svga->blitter, &svga->curr.scissor[0]);
617 util_blitter_save_fragment_shader(svga->blitter, svga->curr.fs);
618 util_blitter_save_blend(svga->blitter, (void*)svga->curr.blend);
619 util_blitter_save_depth_stencil_alpha(svga->blitter,
620 (void*)svga->curr.depth);
621 util_blitter_save_stencil_ref(svga->blitter, &svga->curr.stencil_ref);
622 util_blitter_save_sample_mask(svga->blitter, svga->curr.sample_mask, 0);
623 util_blitter_save_framebuffer(svga->blitter, &svga->curr.framebuffer);
624 util_blitter_save_fragment_sampler_states(svga->blitter,
625 svga->curr.num_samplers[PIPE_SHADER_FRAGMENT],
626 (void**)svga->curr.sampler[PIPE_SHADER_FRAGMENT]);
627 util_blitter_save_fragment_sampler_views(svga->blitter,
628 svga->curr.num_sampler_views[PIPE_SHADER_FRAGMENT],
629 svga->curr.sampler_views[PIPE_SHADER_FRAGMENT]);
630
631 if (!can_create_src_view) {
632 struct pipe_resource template;
633 struct pipe_blit_info copy_region_blit;
634
635 /**
636 * If the source blit format is not compatible with the source resource
637 * format, we will not be able to create a shader resource view.
638 * In order to avoid falling back to software blit, we'll create
639 * a new resource in the blit format, and use DXCopyResource to
640 * copy from the original format to the new format. The new
641 * resource will be used for the blit in util_blitter_blit().
642 */
643 template = *src;
644 template.format = blit.src.format;
645 newSrc = svga_texture_create(svga->pipe.screen, &template);
646 if (newSrc == NULL) {
647 debug_printf("svga_blit: fails to create temporary src\n");
648 ret = false;
649 goto done;
650 }
651
652 /* increment the mksStats for blitter with extra copy */
653 SVGA_STATS_COUNT_INC(sws, SVGA_STATS_COUNT_BLITBLITTERCOPY);
654 build_blit_info(newSrc,
655 blit.src.level, blit.src.box.x,
656 blit.src.box.y, blit.src.box.z,
657 blit.src.resource,
658 blit.src.level, &blit.src.box,
659 ©_region_blit);
660 if (!try_copy_region(svga, ©_region_blit)) {
661 debug_printf("svga: Source blit format conversion failed.\n");
662 ret = false;
663 goto done;
664 }
665
666 blit.src.resource = newSrc;
667 }
668
669 if (!can_create_dst_view) {
670 struct pipe_resource template;
671
672 /*
673 * If the destination blit format is not compatible with the destination
674 * resource format, we will not be able to create a render target view.
675 * In order to avoid falling back to software blit, we'll create
676 * a new resource in the blit format, and use DXPredCopyRegion
677 * after the blit to copy from the blit format back to the resource
678 * format.
679 */
680 template = *dst;
681 template.format = blit.dst.format;
682 newDst = svga_texture_create(svga->pipe.screen, &template);
683 if (newDst == NULL) {
684 debug_printf("svga_blit: fails to create temporary dst\n");
685 ret = false;
686 goto done;
687 }
688
689 blit.dst.resource = newDst;
690 }
691
692 svga_toggle_render_condition(svga, blit.render_condition_enable, false);
693
694 util_blitter_blit(svga->blitter, &blit, NULL);
695
696 svga_toggle_render_condition(svga, blit.render_condition_enable, true);
697
698 if (blit.dst.resource != dst) {
699 struct pipe_blit_info copy_region_blit;
700
701 /* increment the mksStats for blitter with extra copy */
702 SVGA_STATS_COUNT_INC(sws, SVGA_STATS_COUNT_BLITBLITTERCOPY);
703
704 /*
705 * A temporary resource was created for the blit, we need to
706 * copy from the temporary resource back to the original destination.
707 */
708 build_blit_info(dst,
709 blit.dst.level, blit.dst.box.x,
710 blit.dst.box.y, blit.dst.box.z,
711 newDst,
712 blit.dst.level, &blit.dst.box,
713 ©_region_blit);
714 if (!try_copy_region(svga, ©_region_blit)) {
715 debug_printf("svga: Destination blit format conversion failed.\n");
716 ret = false;
717 goto done;
718 }
719 }
720
721 done:
722 /* unreference the temporary resources if needed */
723 pipe_resource_reference(&newDst, NULL);
724 pipe_resource_reference(&newSrc, NULL);
725
726 SVGA_STATS_TIME_POP(sws); /* SVGA_STATS_TIME_BLITBLITTER */
727 (void) sws;
728
729 return ret;
730 }
731
732
733 /**
734 * Try a cpu copy_region fallback.
735 */
736 static bool
try_cpu_copy_region(struct svga_context * svga,const struct pipe_blit_info * blit)737 try_cpu_copy_region(struct svga_context *svga,
738 const struct pipe_blit_info *blit)
739 {
740 if (util_can_blit_via_copy_region(blit, true, svga->render_condition) ||
741 util_can_blit_via_copy_region(blit, false, svga->render_condition)) {
742
743 if (svga->render_condition && blit->render_condition_enable) {
744 debug_warning("CPU copy_region doesn't support "
745 "conditional rendering.\n");
746 return false;
747 }
748
749 copy_region_fallback(svga, blit->dst.resource,
750 blit->dst.level,
751 blit->dst.box.x, blit->dst.box.y,
752 blit->dst.box.z, blit->src.resource,
753 blit->src.level, &blit->src.box);
754 return true;
755 }
756
757 return false;
758 }
759
760 /**
761 * A helper function to resolve a multisampled surface to a single-sampled
762 * surface using SVGA command ResolveCopy.
763 */
764 static bool
try_resolve_copy(struct svga_context * svga,const struct pipe_blit_info * blit)765 try_resolve_copy(struct svga_context *svga,
766 const struct pipe_blit_info *blit)
767 {
768 enum pipe_error ret;
769 struct svga_texture *src_tex = svga_texture(blit->src.resource);
770 struct svga_texture *dst_tex = svga_texture(blit->dst.resource);
771
772 /* check if formats are compatible for resolve copy */
773 if (!formats_compatible(svga_screen(svga->pipe.screen),
774 src_tex->key.format, dst_tex->key.format))
775 return false;
776
777 /* check if the copy dimensions are the same */
778 if ((blit->src.box.x || blit->src.box.y || blit->src.box.z) ||
779 (blit->dst.box.x || blit->dst.box.y || blit->dst.box.z) ||
780 (blit->src.box.width != blit->dst.box.width) ||
781 (blit->src.box.height != blit->dst.box.height) ||
782 (blit->src.box.depth != blit->dst.box.depth))
783 return false;
784
785 ret = SVGA3D_vgpu10_ResolveCopy(svga->swc, 0, dst_tex->handle,
786 0, src_tex->handle, dst_tex->key.format);
787 if (ret != PIPE_OK) {
788 svga_context_flush(svga, NULL);
789 ret = SVGA3D_vgpu10_ResolveCopy(svga->swc, 0, dst_tex->handle,
790 0, src_tex->handle, dst_tex->key.format);
791 }
792
793 /* Mark surface state as RENDERED */
794 dst_tex->surface_state = SVGA_SURFACE_STATE_RENDERED;
795
796 return (ret == PIPE_OK);
797 }
798
799
800 /**
801 * Returns FALSE if the resource does not have data to copy.
802 */
803 static bool
is_texture_valid_to_copy(struct svga_context * svga,struct pipe_resource * resource)804 is_texture_valid_to_copy(struct svga_context *svga,
805 struct pipe_resource *resource)
806 {
807 if (resource->target == PIPE_BUFFER) {
808 struct svga_buffer *buf = svga_buffer(resource);
809 struct svga_buffer_surface *bufsurf = buf->bufsurf;
810
811 if (!bufsurf) {
812 if (svga_buffer_validate_host_surface(svga, buf, buf->bind_flags)
813 != PIPE_OK)
814 return false;
815 bufsurf = buf->bufsurf;
816 }
817
818 return (bufsurf &&
819 bufsurf->surface_state >= SVGA_SURFACE_STATE_UPDATED);
820 } else {
821 struct svga_texture *tex = svga_texture(resource);
822 return ((tex->surface_state >= SVGA_SURFACE_STATE_UPDATED) ||
823 (resource->bind & PIPE_BIND_SHARED));
824 }
825 }
826
827
828 /**
829 * The pipe::blit member.
830 */
831 static void
svga_blit(struct pipe_context * pipe,const struct pipe_blit_info * blit)832 svga_blit(struct pipe_context *pipe,
833 const struct pipe_blit_info *blit)
834 {
835 struct svga_context *svga = svga_context(pipe);
836 struct svga_winsys_screen *sws = svga_screen(pipe->screen)->sws;
837
838 if (!svga_have_vgpu10(svga) &&
839 blit->src.resource->nr_samples > 1 &&
840 blit->dst.resource->nr_samples <= 1 &&
841 !util_format_is_depth_or_stencil(blit->src.resource->format) &&
842 !util_format_is_pure_integer(blit->src.resource->format)) {
843 debug_printf("svga: color resolve unimplemented\n");
844 return;
845 }
846
847 SVGA_STATS_TIME_PUSH(sws, SVGA_STATS_TIME_BLIT);
848
849 if (!is_texture_valid_to_copy(svga, blit->src.resource)) {
850 debug_printf("%s: texture is not defined to copy\n",
851 __func__);
852 goto done;
853 }
854
855 if (svga_have_sm4_1(svga) &&
856 blit->src.resource->nr_samples > 1 &&
857 blit->dst.resource->nr_samples <=1 &&
858 (blit->dst.resource->bind & PIPE_BIND_DISPLAY_TARGET)) {
859 if (try_resolve_copy(svga, blit))
860 goto done;
861 }
862
863 if (try_copy_region(svga, blit))
864 goto done;
865
866 if (try_blit(svga, blit))
867 goto done;
868
869 if (!try_cpu_copy_region(svga, blit))
870 debug_printf("svga: Blit failed.\n");
871
872 done:
873 SVGA_STATS_TIME_POP(sws); /* SVGA_STATS_TIME_BLIT */
874 (void) sws;
875 }
876
877
878 /**
879 * The pipe::resource_copy_region member.
880 */
881 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)882 svga_resource_copy_region(struct pipe_context *pipe,
883 struct pipe_resource *dst_tex,
884 unsigned dst_level,
885 unsigned dstx, unsigned dsty, unsigned dstz,
886 struct pipe_resource *src_tex,
887 unsigned src_level,
888 const struct pipe_box *src_box)
889 {
890 struct svga_context *svga = svga_context(pipe);
891 struct svga_winsys_screen *sws = svga_screen(svga->pipe.screen)->sws;
892
893 SVGA_STATS_TIME_PUSH(sws, SVGA_STATS_TIME_COPYREGION);
894
895 if (!is_texture_valid_to_copy(svga, src_tex)) {
896 debug_printf("%s: texture is not defined to copy\n",
897 __func__);
898 goto done;
899 }
900
901 if (dst_tex->target == PIPE_BUFFER && src_tex->target == PIPE_BUFFER) {
902 /* can't copy within the same buffer, unfortunately */
903 if (svga_have_vgpu10(svga) && src_tex != dst_tex) {
904 struct svga_winsys_surface *src_surf;
905 struct svga_winsys_surface *dst_surf;
906 struct svga_buffer *dbuffer = svga_buffer(dst_tex);
907 struct svga_buffer *sbuffer = svga_buffer(src_tex);
908
909 src_surf = svga_buffer_handle(svga, src_tex, sbuffer->bind_flags);
910 dst_surf = svga_buffer_handle(svga, dst_tex, dbuffer->bind_flags);
911
912 SVGA_RETRY(svga, SVGA3D_vgpu10_BufferCopy(svga->swc, src_surf,
913 dst_surf, src_box->x, dstx,
914 src_box->width));
915 dbuffer->dirty = true;
916
917 /* Mark the buffer surface as RENDERED */
918 assert(dbuffer->bufsurf);
919 dbuffer->bufsurf->surface_state = SVGA_SURFACE_STATE_RENDERED;
920 }
921 else {
922 /* use map/memcpy fallback */
923 copy_region_fallback(svga, dst_tex, dst_level, dstx,
924 dsty, dstz, src_tex, src_level, src_box);
925 }
926 } else {
927 struct pipe_blit_info blit;
928
929 build_blit_info(dst_tex, dst_level, dstx, dsty, dstz,
930 src_tex, src_level, src_box, &blit);
931
932 if (try_copy_region(svga, &blit))
933 goto done;
934
935 /* Blits are format-converting which is not what we want, so perform a
936 * strict format-check.
937 * FIXME: Need to figure out why srgb blits (tf2) and
938 * 3D blits (piglit) are broken here. Perhaps we set up the
939 * struct pipe_blit_info incorrectly.
940 */
941 if (src_tex->format == dst_tex->format &&
942 !util_format_is_srgb(src_tex->format) &&
943 svga_resource_type(src_tex->target) != SVGA3D_RESOURCE_TEXTURE3D &&
944 try_blit(svga, &blit))
945 goto done;
946
947 copy_region_fallback(svga, dst_tex, dst_level, dstx, dsty, dstz,
948 src_tex, src_level, src_box);
949 }
950
951 done:
952 SVGA_STATS_TIME_POP(sws);
953 (void) sws;
954 }
955
956
957 /**
958 * The pipe::flush_resource member.
959 */
960 static void
svga_flush_resource(struct pipe_context * pipe,struct pipe_resource * resource)961 svga_flush_resource(struct pipe_context *pipe,
962 struct pipe_resource *resource)
963 {
964 }
965
966
967 /**
968 * Setup the pipe blit, resource_copy_region and flush_resource members.
969 */
970 void
svga_init_blit_functions(struct svga_context * svga)971 svga_init_blit_functions(struct svga_context *svga)
972 {
973 svga->pipe.resource_copy_region = svga_resource_copy_region;
974 svga->pipe.blit = svga_blit;
975 svga->pipe.flush_resource = svga_flush_resource;
976 }
977