iris: Allow HiZ for copy_region sources
[mesa.git] / src / gallium / drivers / iris / iris_blit.c
1 /*
2 * Copyright © 2017 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 #include <stdio.h>
24 #include "pipe/p_defines.h"
25 #include "pipe/p_state.h"
26 #include "pipe/p_context.h"
27 #include "pipe/p_screen.h"
28 #include "util/format/u_format.h"
29 #include "util/u_inlines.h"
30 #include "util/ralloc.h"
31 #include "intel/blorp/blorp.h"
32 #include "iris_context.h"
33 #include "iris_resource.h"
34 #include "iris_screen.h"
35
36 /**
37 * Helper function for handling mirror image blits.
38 *
39 * If coord0 > coord1, swap them and return "true" (mirrored).
40 */
41 static bool
42 apply_mirror(float *coord0, float *coord1)
43 {
44 if (*coord0 > *coord1) {
45 float tmp = *coord0;
46 *coord0 = *coord1;
47 *coord1 = tmp;
48 return true;
49 }
50 return false;
51 }
52
53 /**
54 * Compute the number of pixels to clip for each side of a rect
55 *
56 * \param x0 The rect's left coordinate
57 * \param y0 The rect's bottom coordinate
58 * \param x1 The rect's right coordinate
59 * \param y1 The rect's top coordinate
60 * \param min_x The clipping region's left coordinate
61 * \param min_y The clipping region's bottom coordinate
62 * \param max_x The clipping region's right coordinate
63 * \param max_y The clipping region's top coordinate
64 * \param clipped_x0 The number of pixels to clip from the left side
65 * \param clipped_y0 The number of pixels to clip from the bottom side
66 * \param clipped_x1 The number of pixels to clip from the right side
67 * \param clipped_y1 The number of pixels to clip from the top side
68 *
69 * \return false if we clip everything away, true otherwise
70 */
71 static inline bool
72 compute_pixels_clipped(float x0, float y0, float x1, float y1,
73 float min_x, float min_y, float max_x, float max_y,
74 float *clipped_x0, float *clipped_y0,
75 float *clipped_x1, float *clipped_y1)
76 {
77 /* If we are going to clip everything away, stop. */
78 if (!(min_x <= max_x &&
79 min_y <= max_y &&
80 x0 <= max_x &&
81 y0 <= max_y &&
82 min_x <= x1 &&
83 min_y <= y1 &&
84 x0 <= x1 &&
85 y0 <= y1)) {
86 return false;
87 }
88
89 if (x0 < min_x)
90 *clipped_x0 = min_x - x0;
91 else
92 *clipped_x0 = 0;
93 if (max_x < x1)
94 *clipped_x1 = x1 - max_x;
95 else
96 *clipped_x1 = 0;
97
98 if (y0 < min_y)
99 *clipped_y0 = min_y - y0;
100 else
101 *clipped_y0 = 0;
102 if (max_y < y1)
103 *clipped_y1 = y1 - max_y;
104 else
105 *clipped_y1 = 0;
106
107 return true;
108 }
109
110 /**
111 * Clips a coordinate (left, right, top or bottom) for the src or dst rect
112 * (whichever requires the largest clip) and adjusts the coordinate
113 * for the other rect accordingly.
114 *
115 * \param mirror true if mirroring is required
116 * \param src the source rect coordinate (for example src_x0)
117 * \param dst0 the dst rect coordinate (for example dst_x0)
118 * \param dst1 the opposite dst rect coordinate (for example dst_x1)
119 * \param clipped_dst0 number of pixels to clip from the dst coordinate
120 * \param clipped_dst1 number of pixels to clip from the opposite dst coordinate
121 * \param scale the src vs dst scale involved for that coordinate
122 * \param is_left_or_bottom true if we are clipping the left or bottom sides
123 * of the rect.
124 */
125 static void
126 clip_coordinates(bool mirror,
127 float *src, float *dst0, float *dst1,
128 float clipped_dst0,
129 float clipped_dst1,
130 float scale,
131 bool is_left_or_bottom)
132 {
133 /* When clipping we need to add or subtract pixels from the original
134 * coordinates depending on whether we are acting on the left/bottom
135 * or right/top sides of the rect respectively. We assume we have to
136 * add them in the code below, and multiply by -1 when we should
137 * subtract.
138 */
139 int mult = is_left_or_bottom ? 1 : -1;
140
141 if (!mirror) {
142 *dst0 += clipped_dst0 * mult;
143 *src += clipped_dst0 * scale * mult;
144 } else {
145 *dst1 -= clipped_dst1 * mult;
146 *src += clipped_dst1 * scale * mult;
147 }
148 }
149
150 /**
151 * Apply a scissor rectangle to blit coordinates.
152 *
153 * Returns true if the blit was entirely scissored away.
154 */
155 static bool
156 apply_blit_scissor(const struct pipe_scissor_state *scissor,
157 float *src_x0, float *src_y0,
158 float *src_x1, float *src_y1,
159 float *dst_x0, float *dst_y0,
160 float *dst_x1, float *dst_y1,
161 bool mirror_x, bool mirror_y)
162 {
163 float clip_dst_x0, clip_dst_x1, clip_dst_y0, clip_dst_y1;
164
165 /* Compute number of pixels to scissor away. */
166 if (!compute_pixels_clipped(*dst_x0, *dst_y0, *dst_x1, *dst_y1,
167 scissor->minx, scissor->miny,
168 scissor->maxx, scissor->maxy,
169 &clip_dst_x0, &clip_dst_y0,
170 &clip_dst_x1, &clip_dst_y1))
171 return true;
172
173 // XXX: comments assume source clipping, which we don't do
174
175 /* When clipping any of the two rects we need to adjust the coordinates
176 * in the other rect considering the scaling factor involved. To obtain
177 * the best precision we want to make sure that we only clip once per
178 * side to avoid accumulating errors due to the scaling adjustment.
179 *
180 * For example, if src_x0 and dst_x0 need both to be clipped we want to
181 * avoid the situation where we clip src_x0 first, then adjust dst_x0
182 * accordingly but then we realize that the resulting dst_x0 still needs
183 * to be clipped, so we clip dst_x0 and adjust src_x0 again. Because we are
184 * applying scaling factors to adjust the coordinates in each clipping
185 * pass we lose some precision and that can affect the results of the
186 * blorp blit operation slightly. What we want to do here is detect the
187 * rect that we should clip first for each side so that when we adjust
188 * the other rect we ensure the resulting coordinate does not need to be
189 * clipped again.
190 *
191 * The code below implements this by comparing the number of pixels that
192 * we need to clip for each side of both rects considering the scales
193 * involved. For example, clip_src_x0 represents the number of pixels
194 * to be clipped for the src rect's left side, so if clip_src_x0 = 5,
195 * clip_dst_x0 = 4 and scale_x = 2 it means that we are clipping more
196 * from the dst rect so we should clip dst_x0 only and adjust src_x0.
197 * This is because clipping 4 pixels in the dst is equivalent to
198 * clipping 4 * 2 = 8 > 5 in the src.
199 */
200
201 if (*src_x0 == *src_x1 || *src_y0 == *src_y1
202 || *dst_x0 == *dst_x1 || *dst_y0 == *dst_y1)
203 return true;
204
205 float scale_x = (float) (*src_x1 - *src_x0) / (*dst_x1 - *dst_x0);
206 float scale_y = (float) (*src_y1 - *src_y0) / (*dst_y1 - *dst_y0);
207
208 /* Clip left side */
209 clip_coordinates(mirror_x, src_x0, dst_x0, dst_x1,
210 clip_dst_x0, clip_dst_x1, scale_x, true);
211
212 /* Clip right side */
213 clip_coordinates(mirror_x, src_x1, dst_x1, dst_x0,
214 clip_dst_x1, clip_dst_x0, scale_x, false);
215
216 /* Clip bottom side */
217 clip_coordinates(mirror_y, src_y0, dst_y0, dst_y1,
218 clip_dst_y0, clip_dst_y1, scale_y, true);
219
220 /* Clip top side */
221 clip_coordinates(mirror_y, src_y1, dst_y1, dst_y0,
222 clip_dst_y1, clip_dst_y0, scale_y, false);
223
224 /* Check for invalid bounds
225 * Can't blit for 0-dimensions
226 */
227 return *src_x0 == *src_x1 || *src_y0 == *src_y1
228 || *dst_x0 == *dst_x1 || *dst_y0 == *dst_y1;
229 }
230
231 void
232 iris_blorp_surf_for_resource(struct iris_vtable *vtbl,
233 struct isl_device *isl_dev,
234 struct blorp_surf *surf,
235 struct pipe_resource *p_res,
236 enum isl_aux_usage aux_usage,
237 unsigned level,
238 bool is_render_target)
239 {
240 struct iris_resource *res = (void *) p_res;
241
242 assert(!iris_resource_unfinished_aux_import(res));
243
244 if (isl_aux_usage_has_hiz(aux_usage) &&
245 !iris_resource_level_has_hiz(res, level))
246 aux_usage = ISL_AUX_USAGE_NONE;
247
248 *surf = (struct blorp_surf) {
249 .surf = &res->surf,
250 .addr = (struct blorp_address) {
251 .buffer = res->bo,
252 .offset = res->offset,
253 .reloc_flags = is_render_target ? EXEC_OBJECT_WRITE : 0,
254 .mocs = vtbl->mocs(res->bo, isl_dev),
255 },
256 .aux_usage = aux_usage,
257 };
258
259 if (aux_usage != ISL_AUX_USAGE_NONE) {
260 surf->aux_surf = &res->aux.surf;
261 surf->aux_addr = (struct blorp_address) {
262 .buffer = res->aux.bo,
263 .offset = res->aux.offset,
264 .reloc_flags = is_render_target ? EXEC_OBJECT_WRITE : 0,
265 .mocs = vtbl->mocs(res->bo, isl_dev),
266 };
267 surf->clear_color =
268 iris_resource_get_clear_color(res, NULL, NULL);
269 surf->clear_color_addr = (struct blorp_address) {
270 .buffer = res->aux.clear_color_bo,
271 .offset = res->aux.clear_color_offset,
272 .reloc_flags = 0,
273 .mocs = vtbl->mocs(res->aux.clear_color_bo, isl_dev),
274 };
275 }
276 }
277
278 static bool
279 is_astc(enum isl_format format)
280 {
281 return format != ISL_FORMAT_UNSUPPORTED &&
282 isl_format_get_layout(format)->txc == ISL_TXC_ASTC;
283 }
284
285 static void
286 tex_cache_flush_hack(struct iris_batch *batch,
287 enum isl_format view_format,
288 enum isl_format surf_format)
289 {
290 const struct gen_device_info *devinfo = &batch->screen->devinfo;
291
292 /* The WaSamplerCacheFlushBetweenRedescribedSurfaceReads workaround says:
293 *
294 * "Currently Sampler assumes that a surface would not have two
295 * different format associate with it. It will not properly cache
296 * the different views in the MT cache, causing a data corruption."
297 *
298 * We may need to handle this for texture views in general someday, but
299 * for now we handle it here, as it hurts copies and blits particularly
300 * badly because they ofter reinterpret formats.
301 *
302 * If the BO hasn't been referenced yet this batch, we assume that the
303 * texture cache doesn't contain any relevant data nor need flushing.
304 *
305 * Icelake (Gen11+) claims to fix this issue, but seems to still have
306 * issues with ASTC formats.
307 */
308 bool need_flush = devinfo->gen >= 11 ?
309 is_astc(surf_format) != is_astc(view_format) :
310 view_format != surf_format;
311 if (!need_flush)
312 return;
313
314 const char *reason =
315 "workaround: WaSamplerCacheFlushBetweenRedescribedSurfaceReads";
316
317 iris_emit_pipe_control_flush(batch, reason, PIPE_CONTROL_CS_STALL);
318 iris_emit_pipe_control_flush(batch, reason,
319 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
320 }
321
322 /**
323 * The pipe->blit() driver hook.
324 *
325 * This performs a blit between two surfaces, which copies data but may
326 * also perform format conversion, scaling, flipping, and so on.
327 */
328 static void
329 iris_blit(struct pipe_context *ctx, const struct pipe_blit_info *info)
330 {
331 struct iris_context *ice = (void *) ctx;
332 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
333 const struct gen_device_info *devinfo = &screen->devinfo;
334 struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
335 enum blorp_batch_flags blorp_flags = 0;
336 struct iris_resource *src_res = (void *) info->src.resource;
337 struct iris_resource *dst_res = (void *) info->dst.resource;
338
339 /* We don't support color masking. */
340 assert((info->mask & PIPE_MASK_RGBA) == PIPE_MASK_RGBA ||
341 (info->mask & PIPE_MASK_RGBA) == 0);
342
343 if (info->render_condition_enable) {
344 if (ice->state.predicate == IRIS_PREDICATE_STATE_DONT_RENDER)
345 return;
346
347 if (ice->state.predicate == IRIS_PREDICATE_STATE_USE_BIT)
348 blorp_flags |= BLORP_BATCH_PREDICATE_ENABLE;
349 }
350
351 struct iris_format_info src_fmt =
352 iris_format_for_usage(devinfo, info->src.format,
353 ISL_SURF_USAGE_TEXTURE_BIT);
354 enum isl_aux_usage src_aux_usage =
355 iris_resource_texture_aux_usage(ice, src_res, src_fmt.fmt);
356
357 if (iris_resource_level_has_hiz(src_res, info->src.level))
358 src_aux_usage = ISL_AUX_USAGE_NONE;
359
360 bool src_clear_supported = src_aux_usage != ISL_AUX_USAGE_NONE &&
361 src_res->surf.format == src_fmt.fmt;
362
363 iris_resource_prepare_access(ice, batch, src_res, info->src.level, 1,
364 info->src.box.z, info->src.box.depth,
365 src_aux_usage, src_clear_supported);
366
367 struct iris_format_info dst_fmt =
368 iris_format_for_usage(devinfo, info->dst.format,
369 ISL_SURF_USAGE_RENDER_TARGET_BIT);
370 enum isl_aux_usage dst_aux_usage =
371 iris_resource_render_aux_usage(ice, dst_res, dst_fmt.fmt, false, false);
372 bool dst_clear_supported = dst_aux_usage != ISL_AUX_USAGE_NONE;
373
374 struct blorp_surf src_surf, dst_surf;
375 iris_blorp_surf_for_resource(&ice->vtbl, &screen->isl_dev, &src_surf,
376 info->src.resource, src_aux_usage,
377 info->src.level, false);
378 iris_blorp_surf_for_resource(&ice->vtbl, &screen->isl_dev, &dst_surf,
379 info->dst.resource, dst_aux_usage,
380 info->dst.level, true);
381
382 iris_resource_prepare_access(ice, batch, dst_res, info->dst.level, 1,
383 info->dst.box.z, info->dst.box.depth,
384 dst_aux_usage, dst_clear_supported);
385
386 float src_x0 = info->src.box.x;
387 float src_x1 = info->src.box.x + info->src.box.width;
388 float src_y0 = info->src.box.y;
389 float src_y1 = info->src.box.y + info->src.box.height;
390 float dst_x0 = info->dst.box.x;
391 float dst_x1 = info->dst.box.x + info->dst.box.width;
392 float dst_y0 = info->dst.box.y;
393 float dst_y1 = info->dst.box.y + info->dst.box.height;
394 bool mirror_x = apply_mirror(&src_x0, &src_x1);
395 bool mirror_y = apply_mirror(&src_y0, &src_y1);
396 enum blorp_filter filter;
397
398 if (info->scissor_enable) {
399 bool noop = apply_blit_scissor(&info->scissor,
400 &src_x0, &src_y0, &src_x1, &src_y1,
401 &dst_x0, &dst_y0, &dst_x1, &dst_y1,
402 mirror_x, mirror_y);
403 if (noop)
404 return;
405 }
406
407 if (abs(info->dst.box.width) == abs(info->src.box.width) &&
408 abs(info->dst.box.height) == abs(info->src.box.height)) {
409 if (src_surf.surf->samples > 1 && dst_surf.surf->samples <= 1) {
410 /* The OpenGL ES 3.2 specification, section 16.2.1, says:
411 *
412 * "If the read framebuffer is multisampled (its effective
413 * value of SAMPLE_BUFFERS is one) and the draw framebuffer
414 * is not (its value of SAMPLE_BUFFERS is zero), the samples
415 * corresponding to each pixel location in the source are
416 * converted to a single sample before being written to the
417 * destination. The filter parameter is ignored. If the
418 * source formats are integer types or stencil values, a
419 * single sample’s value is selected for each pixel. If the
420 * source formats are floating-point or normalized types,
421 * the sample values for each pixel are resolved in an
422 * implementation-dependent manner. If the source formats
423 * are depth values, sample values are resolved in an
424 * implementation-dependent manner where the result will be
425 * between the minimum and maximum depth values in the pixel."
426 *
427 * When selecting a single sample, we always choose sample 0.
428 */
429 if (util_format_is_depth_or_stencil(info->src.format) ||
430 util_format_is_pure_integer(info->src.format)) {
431 filter = BLORP_FILTER_SAMPLE_0;
432 } else {
433 filter = BLORP_FILTER_AVERAGE;
434 }
435 } else {
436 /* The OpenGL 4.6 specification, section 18.3.1, says:
437 *
438 * "If the source and destination dimensions are identical,
439 * no filtering is applied."
440 *
441 * Using BLORP_FILTER_NONE will also handle the upsample case by
442 * replicating the one value in the source to all values in the
443 * destination.
444 */
445 filter = BLORP_FILTER_NONE;
446 }
447 } else if (info->filter == PIPE_TEX_FILTER_LINEAR) {
448 filter = BLORP_FILTER_BILINEAR;
449 } else {
450 filter = BLORP_FILTER_NEAREST;
451 }
452
453 if (iris_batch_references(batch, src_res->bo))
454 tex_cache_flush_hack(batch, src_fmt.fmt, src_res->surf.format);
455
456 if (dst_res->base.target == PIPE_BUFFER)
457 util_range_add(&dst_res->base, &dst_res->valid_buffer_range, dst_x0, dst_x1);
458
459 struct blorp_batch blorp_batch;
460 blorp_batch_init(&ice->blorp, &blorp_batch, batch, blorp_flags);
461
462 unsigned main_mask;
463 if (util_format_is_depth_or_stencil(info->dst.format))
464 main_mask = PIPE_MASK_Z;
465 else
466 main_mask = PIPE_MASK_RGBA;
467
468 if (info->mask & main_mask) {
469 for (int slice = 0; slice < info->dst.box.depth; slice++) {
470 iris_batch_maybe_flush(batch, 1500);
471
472 blorp_blit(&blorp_batch,
473 &src_surf, info->src.level, info->src.box.z + slice,
474 src_fmt.fmt, src_fmt.swizzle,
475 &dst_surf, info->dst.level, info->dst.box.z + slice,
476 dst_fmt.fmt, dst_fmt.swizzle,
477 src_x0, src_y0, src_x1, src_y1,
478 dst_x0, dst_y0, dst_x1, dst_y1,
479 filter, mirror_x, mirror_y);
480 }
481 }
482
483 struct iris_resource *stc_dst = NULL;
484 enum isl_aux_usage stc_src_aux_usage, stc_dst_aux_usage;
485 if ((info->mask & PIPE_MASK_S) &&
486 util_format_has_stencil(util_format_description(info->dst.format)) &&
487 util_format_has_stencil(util_format_description(info->src.format))) {
488 struct iris_resource *src_res, *junk;
489 struct blorp_surf src_surf, dst_surf;
490 iris_get_depth_stencil_resources(info->src.resource, &junk, &src_res);
491 iris_get_depth_stencil_resources(info->dst.resource, &junk, &stc_dst);
492
493 struct iris_format_info src_fmt =
494 iris_format_for_usage(devinfo, src_res->base.format,
495 ISL_SURF_USAGE_TEXTURE_BIT);
496 stc_src_aux_usage =
497 iris_resource_texture_aux_usage(ice, src_res, src_fmt.fmt);
498
499 struct iris_format_info dst_fmt =
500 iris_format_for_usage(devinfo, stc_dst->base.format,
501 ISL_SURF_USAGE_RENDER_TARGET_BIT);
502 stc_dst_aux_usage =
503 iris_resource_render_aux_usage(ice, stc_dst, dst_fmt.fmt, false, false);
504
505 /* Resolve destination surface before blit because :
506 * 1. when we try to blit from the same surface, we can't read and
507 * write to the same surfaces at the same time when we have
508 * compression enabled so it's safe to resolve surface first and then
509 * do blit.
510 * 2. While bliting from one surface to another surface, we might be
511 * mixing compression formats, Our experiments shows that if after
512 * blit if we set DepthStencilResource flag to 0, blit passes but
513 * clear fails.
514 *
515 * XXX: In second case by destructing the compression, we might lose
516 * some performance.
517 */
518 if (devinfo->gen >= 12)
519 stc_dst_aux_usage = ISL_AUX_USAGE_NONE;
520
521 iris_resource_prepare_access(ice, batch, src_res, info->src.level, 1,
522 info->src.box.z, info->src.box.depth,
523 stc_src_aux_usage, false);
524 iris_resource_prepare_access(ice, batch, stc_dst, info->dst.level, 1,
525 info->dst.box.z, info->dst.box.depth,
526 stc_dst_aux_usage, false);
527 iris_blorp_surf_for_resource(&ice->vtbl, &screen->isl_dev, &src_surf,
528 &src_res->base, stc_src_aux_usage,
529 info->src.level, false);
530 iris_blorp_surf_for_resource(&ice->vtbl, &screen->isl_dev, &dst_surf,
531 &stc_dst->base, stc_dst_aux_usage,
532 info->dst.level, true);
533
534 for (int slice = 0; slice < info->dst.box.depth; slice++) {
535 iris_batch_maybe_flush(batch, 1500);
536
537 blorp_blit(&blorp_batch,
538 &src_surf, info->src.level, info->src.box.z + slice,
539 ISL_FORMAT_R8_UINT, ISL_SWIZZLE_IDENTITY,
540 &dst_surf, info->dst.level, info->dst.box.z + slice,
541 ISL_FORMAT_R8_UINT, ISL_SWIZZLE_IDENTITY,
542 src_x0, src_y0, src_x1, src_y1,
543 dst_x0, dst_y0, dst_x1, dst_y1,
544 filter, mirror_x, mirror_y);
545 }
546 }
547
548 blorp_batch_finish(&blorp_batch);
549
550 tex_cache_flush_hack(batch, src_fmt.fmt, src_res->surf.format);
551
552 if (info->mask & main_mask) {
553 iris_resource_finish_write(ice, dst_res, info->dst.level, info->dst.box.z,
554 info->dst.box.depth, dst_aux_usage);
555 }
556
557 if (stc_dst) {
558 iris_resource_finish_write(ice, stc_dst, info->dst.level, info->dst.box.z,
559 info->dst.box.depth, stc_dst_aux_usage);
560 }
561
562 iris_flush_and_dirty_for_history(ice, batch, (struct iris_resource *)
563 info->dst.resource,
564 PIPE_CONTROL_RENDER_TARGET_FLUSH,
565 "cache history: post-blit");
566 }
567
568 static void
569 get_copy_region_aux_settings(const struct gen_device_info *devinfo,
570 struct iris_resource *res,
571 enum isl_aux_usage *out_aux_usage,
572 bool *out_clear_supported,
573 bool is_render_target)
574 {
575 switch (res->aux.usage) {
576 case ISL_AUX_USAGE_HIZ:
577 if (!is_render_target && iris_sample_with_depth_aux(devinfo, res)) {
578 *out_aux_usage = ISL_AUX_USAGE_HIZ;
579 *out_clear_supported = true;
580 } else {
581 *out_aux_usage = ISL_AUX_USAGE_NONE;
582 *out_clear_supported = false;
583 }
584 break;
585 case ISL_AUX_USAGE_MCS:
586 case ISL_AUX_USAGE_MCS_CCS:
587 case ISL_AUX_USAGE_CCS_E:
588 /* A stencil resolve operation must be performed prior to doing resource
589 * copies or used by CPU.
590 * (see HSD 1209978162)
591 */
592 if (is_render_target && isl_surf_usage_is_stencil(res->surf.usage)) {
593 *out_aux_usage = ISL_AUX_USAGE_NONE;
594 *out_clear_supported = false;
595 } else {
596 *out_aux_usage = res->aux.usage;
597 /* Prior to Gen9, fast-clear only supported 0/1 clear colors. Since
598 * we're going to re-interpret the format as an integer format possibly
599 * with a different number of components, we can't handle clear colors
600 * until Gen9.
601 */
602 *out_clear_supported = devinfo->gen >= 9;
603 }
604 break;
605 default:
606 *out_aux_usage = ISL_AUX_USAGE_NONE;
607 *out_clear_supported = false;
608 break;
609 }
610 }
611
612 /**
613 * Perform a GPU-based raw memory copy between compatible view classes.
614 *
615 * Does not perform any flushing - the new data may still be left in the
616 * render cache, and old data may remain in other caches.
617 *
618 * Wraps blorp_copy() and blorp_buffer_copy().
619 */
620 void
621 iris_copy_region(struct blorp_context *blorp,
622 struct iris_batch *batch,
623 struct pipe_resource *dst,
624 unsigned dst_level,
625 unsigned dstx, unsigned dsty, unsigned dstz,
626 struct pipe_resource *src,
627 unsigned src_level,
628 const struct pipe_box *src_box)
629 {
630 struct blorp_batch blorp_batch;
631 struct iris_context *ice = blorp->driver_ctx;
632 struct iris_screen *screen = (void *) ice->ctx.screen;
633 const struct gen_device_info *devinfo = &screen->devinfo;
634 struct iris_resource *src_res = (void *) src;
635 struct iris_resource *dst_res = (void *) dst;
636
637 enum isl_aux_usage src_aux_usage, dst_aux_usage;
638 bool src_clear_supported, dst_clear_supported;
639 get_copy_region_aux_settings(devinfo, src_res, &src_aux_usage,
640 &src_clear_supported, false);
641 get_copy_region_aux_settings(devinfo, dst_res, &dst_aux_usage,
642 &dst_clear_supported, true);
643
644 if (iris_batch_references(batch, src_res->bo))
645 tex_cache_flush_hack(batch, ISL_FORMAT_UNSUPPORTED, src_res->surf.format);
646
647 if (dst->target == PIPE_BUFFER)
648 util_range_add(&dst_res->base, &dst_res->valid_buffer_range, dstx, dstx + src_box->width);
649
650 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
651 struct blorp_address src_addr = {
652 .buffer = iris_resource_bo(src), .offset = src_box->x,
653 };
654 struct blorp_address dst_addr = {
655 .buffer = iris_resource_bo(dst), .offset = dstx,
656 .reloc_flags = EXEC_OBJECT_WRITE,
657 };
658
659 iris_batch_maybe_flush(batch, 1500);
660
661 blorp_batch_init(&ice->blorp, &blorp_batch, batch, 0);
662 blorp_buffer_copy(&blorp_batch, src_addr, dst_addr, src_box->width);
663 blorp_batch_finish(&blorp_batch);
664 } else {
665 // XXX: what about one surface being a buffer and not the other?
666
667 struct blorp_surf src_surf, dst_surf;
668 iris_blorp_surf_for_resource(&ice->vtbl, &screen->isl_dev, &src_surf,
669 src, src_aux_usage, src_level, false);
670 iris_blorp_surf_for_resource(&ice->vtbl, &screen->isl_dev, &dst_surf,
671 dst, dst_aux_usage, dst_level, true);
672
673 iris_resource_prepare_access(ice, batch, src_res, src_level, 1,
674 src_box->z, src_box->depth,
675 src_aux_usage, src_clear_supported);
676 iris_resource_prepare_access(ice, batch, dst_res, dst_level, 1,
677 dstz, src_box->depth,
678 dst_aux_usage, dst_clear_supported);
679
680 blorp_batch_init(&ice->blorp, &blorp_batch, batch, 0);
681
682 for (int slice = 0; slice < src_box->depth; slice++) {
683 iris_batch_maybe_flush(batch, 1500);
684
685 blorp_copy(&blorp_batch, &src_surf, src_level, src_box->z + slice,
686 &dst_surf, dst_level, dstz + slice,
687 src_box->x, src_box->y, dstx, dsty,
688 src_box->width, src_box->height);
689 }
690 blorp_batch_finish(&blorp_batch);
691
692 iris_resource_finish_write(ice, dst_res, dst_level, dstz,
693 src_box->depth, dst_aux_usage);
694 }
695
696 tex_cache_flush_hack(batch, ISL_FORMAT_UNSUPPORTED, src_res->surf.format);
697 }
698
699 static struct iris_batch *
700 get_preferred_batch(struct iris_context *ice, struct iris_bo *bo)
701 {
702 /* If the compute batch is already using this buffer, we'd prefer to
703 * continue queueing in the compute batch.
704 */
705 if (iris_batch_references(&ice->batches[IRIS_BATCH_COMPUTE], bo))
706 return &ice->batches[IRIS_BATCH_COMPUTE];
707
708 /* Otherwise default to the render batch. */
709 return &ice->batches[IRIS_BATCH_RENDER];
710 }
711
712
713 /**
714 * The pipe->resource_copy_region() driver hook.
715 *
716 * This implements ARB_copy_image semantics - a raw memory copy between
717 * compatible view classes.
718 */
719 static void
720 iris_resource_copy_region(struct pipe_context *ctx,
721 struct pipe_resource *dst,
722 unsigned dst_level,
723 unsigned dstx, unsigned dsty, unsigned dstz,
724 struct pipe_resource *src,
725 unsigned src_level,
726 const struct pipe_box *src_box)
727 {
728 struct iris_context *ice = (void *) ctx;
729 struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
730
731 /* Use MI_COPY_MEM_MEM for tiny (<= 16 byte, % 4) buffer copies. */
732 if (src->target == PIPE_BUFFER && dst->target == PIPE_BUFFER &&
733 (src_box->width % 4 == 0) && src_box->width <= 16) {
734 struct iris_bo *dst_bo = iris_resource_bo(dst);
735 batch = get_preferred_batch(ice, dst_bo);
736 iris_batch_maybe_flush(batch, 24 + 5 * (src_box->width / 4));
737 iris_emit_pipe_control_flush(batch,
738 "stall for MI_COPY_MEM_MEM copy_region",
739 PIPE_CONTROL_CS_STALL);
740 ice->vtbl.copy_mem_mem(batch, dst_bo, dstx, iris_resource_bo(src),
741 src_box->x, src_box->width);
742 return;
743 }
744
745 iris_copy_region(&ice->blorp, batch, dst, dst_level, dstx, dsty, dstz,
746 src, src_level, src_box);
747
748 if (util_format_is_depth_and_stencil(dst->format) &&
749 util_format_has_stencil(util_format_description(src->format))) {
750 struct iris_resource *junk, *s_src_res, *s_dst_res;
751 iris_get_depth_stencil_resources(src, &junk, &s_src_res);
752 iris_get_depth_stencil_resources(dst, &junk, &s_dst_res);
753
754 iris_copy_region(&ice->blorp, batch, &s_dst_res->base, dst_level, dstx,
755 dsty, dstz, &s_src_res->base, src_level, src_box);
756 }
757
758 iris_flush_and_dirty_for_history(ice, batch, (struct iris_resource *) dst,
759 PIPE_CONTROL_RENDER_TARGET_FLUSH,
760 "cache history: post copy_region");
761 }
762
763 void
764 iris_init_blit_functions(struct pipe_context *ctx)
765 {
766 ctx->blit = iris_blit;
767 ctx->resource_copy_region = iris_resource_copy_region;
768 }