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
apply_mirror(float * coord0,float * coord1)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
compute_pixels_clipped(float x0,float y0,float x1,float y1,float min_x,float min_y,float max_x,float max_y,float * clipped_x0,float * clipped_y0,float * clipped_x1,float * clipped_y1)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
clip_coordinates(bool mirror,float * src,float * dst0,float * dst1,float clipped_dst0,float clipped_dst1,float scale,bool is_left_or_bottom)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
apply_blit_scissor(const struct pipe_scissor_state * scissor,float * src_x0,float * src_y0,float * src_x1,float * src_y1,float * dst_x0,float * dst_y0,float * dst_x1,float * dst_y1,bool mirror_x,bool mirror_y)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
iris_blorp_surf_for_resource(struct isl_device * isl_dev,struct blorp_surf * surf,struct pipe_resource * p_res,enum isl_aux_usage aux_usage,unsigned level,bool is_render_target)232 iris_blorp_surf_for_resource(struct isl_device *isl_dev,
233 struct blorp_surf *surf,
234 struct pipe_resource *p_res,
235 enum isl_aux_usage aux_usage,
236 unsigned level,
237 bool is_render_target)
238 {
239 struct iris_resource *res = (void *) p_res;
240
241 assert(!iris_resource_unfinished_aux_import(res));
242
243 if (isl_aux_usage_has_hiz(aux_usage) &&
244 !iris_resource_level_has_hiz(res, level))
245 aux_usage = ISL_AUX_USAGE_NONE;
246
247 *surf = (struct blorp_surf) {
248 .surf = &res->surf,
249 .addr = (struct blorp_address) {
250 .buffer = res->bo,
251 .offset = res->offset,
252 .reloc_flags = is_render_target ? EXEC_OBJECT_WRITE : 0,
253 .mocs = iris_mocs(res->bo, isl_dev,
254 is_render_target ? ISL_SURF_USAGE_RENDER_TARGET_BIT
255 : ISL_SURF_USAGE_TEXTURE_BIT),
256 },
257 .aux_usage = aux_usage,
258 };
259
260 if (aux_usage != ISL_AUX_USAGE_NONE) {
261 surf->aux_surf = &res->aux.surf;
262 surf->aux_addr = (struct blorp_address) {
263 .buffer = res->aux.bo,
264 .offset = res->aux.offset,
265 .reloc_flags = is_render_target ? EXEC_OBJECT_WRITE : 0,
266 .mocs = iris_mocs(res->bo, isl_dev, 0),
267 };
268 surf->clear_color =
269 iris_resource_get_clear_color(res, NULL, NULL);
270 surf->clear_color_addr = (struct blorp_address) {
271 .buffer = res->aux.clear_color_bo,
272 .offset = res->aux.clear_color_offset,
273 .reloc_flags = 0,
274 .mocs = iris_mocs(res->aux.clear_color_bo, isl_dev, 0),
275 };
276 }
277 }
278
279 static bool
is_astc(enum isl_format format)280 is_astc(enum isl_format format)
281 {
282 return format != ISL_FORMAT_UNSUPPORTED &&
283 isl_format_get_layout(format)->txc == ISL_TXC_ASTC;
284 }
285
286 static void
tex_cache_flush_hack(struct iris_batch * batch,enum isl_format view_format,enum isl_format surf_format)287 tex_cache_flush_hack(struct iris_batch *batch,
288 enum isl_format view_format,
289 enum isl_format surf_format)
290 {
291 const struct gen_device_info *devinfo = &batch->screen->devinfo;
292
293 /* The WaSamplerCacheFlushBetweenRedescribedSurfaceReads workaround says:
294 *
295 * "Currently Sampler assumes that a surface would not have two
296 * different format associate with it. It will not properly cache
297 * the different views in the MT cache, causing a data corruption."
298 *
299 * We may need to handle this for texture views in general someday, but
300 * for now we handle it here, as it hurts copies and blits particularly
301 * badly because they ofter reinterpret formats.
302 *
303 * If the BO hasn't been referenced yet this batch, we assume that the
304 * texture cache doesn't contain any relevant data nor need flushing.
305 *
306 * Icelake (Gen11+) claims to fix this issue, but seems to still have
307 * issues with ASTC formats.
308 */
309 bool need_flush = devinfo->gen >= 11 ?
310 is_astc(surf_format) != is_astc(view_format) :
311 view_format != surf_format;
312 if (!need_flush)
313 return;
314
315 const char *reason =
316 "workaround: WaSamplerCacheFlushBetweenRedescribedSurfaceReads";
317
318 iris_emit_pipe_control_flush(batch, reason, PIPE_CONTROL_CS_STALL);
319 iris_emit_pipe_control_flush(batch, reason,
320 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
321 }
322
323 static enum isl_aux_usage
iris_resource_blorp_write_aux_usage(struct iris_context * ice,struct iris_resource * res,enum isl_format render_format)324 iris_resource_blorp_write_aux_usage(struct iris_context *ice,
325 struct iris_resource *res,
326 enum isl_format render_format)
327 {
328 if (res->surf.usage & (ISL_SURF_USAGE_DEPTH_BIT |
329 ISL_SURF_USAGE_STENCIL_BIT)) {
330 assert(render_format == res->surf.format);
331 return res->aux.usage;
332 } else {
333 return iris_resource_render_aux_usage(ice, res, render_format, false);
334 }
335 }
336
337 /**
338 * The pipe->blit() driver hook.
339 *
340 * This performs a blit between two surfaces, which copies data but may
341 * also perform format conversion, scaling, flipping, and so on.
342 */
343 static void
iris_blit(struct pipe_context * ctx,const struct pipe_blit_info * info)344 iris_blit(struct pipe_context *ctx, const struct pipe_blit_info *info)
345 {
346 struct iris_context *ice = (void *) ctx;
347 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
348 const struct gen_device_info *devinfo = &screen->devinfo;
349 struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
350 enum blorp_batch_flags blorp_flags = 0;
351 struct iris_resource *src_res = (void *) info->src.resource;
352 struct iris_resource *dst_res = (void *) info->dst.resource;
353
354 /* We don't support color masking. */
355 assert((info->mask & PIPE_MASK_RGBA) == PIPE_MASK_RGBA ||
356 (info->mask & PIPE_MASK_RGBA) == 0);
357
358 if (info->render_condition_enable) {
359 if (ice->state.predicate == IRIS_PREDICATE_STATE_DONT_RENDER)
360 return;
361
362 if (ice->state.predicate == IRIS_PREDICATE_STATE_USE_BIT)
363 blorp_flags |= BLORP_BATCH_PREDICATE_ENABLE;
364 }
365
366 float src_x0 = info->src.box.x;
367 float src_x1 = info->src.box.x + info->src.box.width;
368 float src_y0 = info->src.box.y;
369 float src_y1 = info->src.box.y + info->src.box.height;
370 float dst_x0 = info->dst.box.x;
371 float dst_x1 = info->dst.box.x + info->dst.box.width;
372 float dst_y0 = info->dst.box.y;
373 float dst_y1 = info->dst.box.y + info->dst.box.height;
374 bool mirror_x = apply_mirror(&src_x0, &src_x1);
375 bool mirror_y = apply_mirror(&src_y0, &src_y1);
376 enum blorp_filter filter;
377
378 if (info->scissor_enable) {
379 bool noop = apply_blit_scissor(&info->scissor,
380 &src_x0, &src_y0, &src_x1, &src_y1,
381 &dst_x0, &dst_y0, &dst_x1, &dst_y1,
382 mirror_x, mirror_y);
383 if (noop)
384 return;
385 }
386
387 if (iris_resource_unfinished_aux_import(src_res))
388 iris_resource_finish_aux_import(ctx->screen, src_res);
389 if (iris_resource_unfinished_aux_import(dst_res))
390 iris_resource_finish_aux_import(ctx->screen, dst_res);
391
392 struct iris_format_info src_fmt =
393 iris_format_for_usage(devinfo, info->src.format,
394 ISL_SURF_USAGE_TEXTURE_BIT);
395 enum isl_aux_usage src_aux_usage =
396 iris_resource_texture_aux_usage(ice, src_res, src_fmt.fmt);
397
398 if (iris_resource_level_has_hiz(src_res, info->src.level))
399 assert(src_res->surf.format == src_fmt.fmt);
400
401 bool src_clear_supported = isl_aux_usage_has_fast_clears(src_aux_usage) &&
402 src_res->surf.format == src_fmt.fmt;
403
404 iris_resource_prepare_access(ice, src_res, info->src.level, 1,
405 info->src.box.z, info->src.box.depth,
406 src_aux_usage, src_clear_supported);
407 iris_emit_buffer_barrier_for(batch, src_res->bo, IRIS_DOMAIN_OTHER_READ);
408
409 struct iris_format_info dst_fmt =
410 iris_format_for_usage(devinfo, info->dst.format,
411 ISL_SURF_USAGE_RENDER_TARGET_BIT);
412 enum isl_aux_usage dst_aux_usage =
413 iris_resource_blorp_write_aux_usage(ice, dst_res, dst_fmt.fmt);
414 bool dst_clear_supported = isl_aux_usage_has_fast_clears(dst_aux_usage);
415
416 struct blorp_surf src_surf, dst_surf;
417 iris_blorp_surf_for_resource(&screen->isl_dev, &src_surf,
418 info->src.resource, src_aux_usage,
419 info->src.level, false);
420 iris_blorp_surf_for_resource(&screen->isl_dev, &dst_surf,
421 info->dst.resource, dst_aux_usage,
422 info->dst.level, true);
423
424 iris_resource_prepare_access(ice, dst_res, info->dst.level, 1,
425 info->dst.box.z, info->dst.box.depth,
426 dst_aux_usage, dst_clear_supported);
427 iris_emit_buffer_barrier_for(batch, dst_res->bo, IRIS_DOMAIN_RENDER_WRITE);
428
429 if (abs(info->dst.box.width) == abs(info->src.box.width) &&
430 abs(info->dst.box.height) == abs(info->src.box.height)) {
431 if (src_surf.surf->samples > 1 && dst_surf.surf->samples <= 1) {
432 /* The OpenGL ES 3.2 specification, section 16.2.1, says:
433 *
434 * "If the read framebuffer is multisampled (its effective
435 * value of SAMPLE_BUFFERS is one) and the draw framebuffer
436 * is not (its value of SAMPLE_BUFFERS is zero), the samples
437 * corresponding to each pixel location in the source are
438 * converted to a single sample before being written to the
439 * destination. The filter parameter is ignored. If the
440 * source formats are integer types or stencil values, a
441 * single sample’s value is selected for each pixel. If the
442 * source formats are floating-point or normalized types,
443 * the sample values for each pixel are resolved in an
444 * implementation-dependent manner. If the source formats
445 * are depth values, sample values are resolved in an
446 * implementation-dependent manner where the result will be
447 * between the minimum and maximum depth values in the pixel."
448 *
449 * When selecting a single sample, we always choose sample 0.
450 */
451 if (util_format_is_depth_or_stencil(info->src.format) ||
452 util_format_is_pure_integer(info->src.format)) {
453 filter = BLORP_FILTER_SAMPLE_0;
454 } else {
455 filter = BLORP_FILTER_AVERAGE;
456 }
457 } else {
458 /* The OpenGL 4.6 specification, section 18.3.1, says:
459 *
460 * "If the source and destination dimensions are identical,
461 * no filtering is applied."
462 *
463 * Using BLORP_FILTER_NONE will also handle the upsample case by
464 * replicating the one value in the source to all values in the
465 * destination.
466 */
467 filter = BLORP_FILTER_NONE;
468 }
469 } else if (info->filter == PIPE_TEX_FILTER_LINEAR) {
470 filter = BLORP_FILTER_BILINEAR;
471 } else {
472 filter = BLORP_FILTER_NEAREST;
473 }
474
475 if (iris_batch_references(batch, src_res->bo))
476 tex_cache_flush_hack(batch, src_fmt.fmt, src_res->surf.format);
477
478 if (dst_res->base.target == PIPE_BUFFER)
479 util_range_add(&dst_res->base, &dst_res->valid_buffer_range, dst_x0, dst_x1);
480
481 struct blorp_batch blorp_batch;
482 blorp_batch_init(&ice->blorp, &blorp_batch, batch, blorp_flags);
483
484 unsigned main_mask;
485 if (util_format_is_depth_or_stencil(info->dst.format))
486 main_mask = PIPE_MASK_Z;
487 else
488 main_mask = PIPE_MASK_RGBA;
489
490 float src_z_step = (float)info->src.box.depth / (float)info->dst.box.depth;
491
492 /* There is no interpolation to the pixel center during rendering, so
493 * add the 0.5 offset ourselves here.
494 */
495 float depth_center_offset = 0;
496 if (src_res->surf.dim == ISL_SURF_DIM_3D)
497 depth_center_offset = 0.5 / info->dst.box.depth * info->src.box.depth;
498
499 if (info->mask & main_mask) {
500 for (int slice = 0; slice < info->dst.box.depth; slice++) {
501 unsigned dst_z = info->dst.box.z + slice;
502 float src_z = info->src.box.z + slice * src_z_step +
503 depth_center_offset;
504
505 iris_batch_maybe_flush(batch, 1500);
506 iris_batch_sync_region_start(batch);
507
508 blorp_blit(&blorp_batch,
509 &src_surf, info->src.level, src_z,
510 src_fmt.fmt, src_fmt.swizzle,
511 &dst_surf, info->dst.level, dst_z,
512 dst_fmt.fmt, dst_fmt.swizzle,
513 src_x0, src_y0, src_x1, src_y1,
514 dst_x0, dst_y0, dst_x1, dst_y1,
515 filter, mirror_x, mirror_y);
516
517 iris_batch_sync_region_end(batch);
518 }
519 }
520
521 struct iris_resource *stc_dst = NULL;
522 enum isl_aux_usage stc_dst_aux_usage = ISL_AUX_USAGE_NONE;
523 if ((info->mask & PIPE_MASK_S) &&
524 util_format_has_stencil(util_format_description(info->dst.format)) &&
525 util_format_has_stencil(util_format_description(info->src.format))) {
526 struct iris_resource *src_res, *junk;
527 struct blorp_surf src_surf, dst_surf;
528 iris_get_depth_stencil_resources(info->src.resource, &junk, &src_res);
529 iris_get_depth_stencil_resources(info->dst.resource, &junk, &stc_dst);
530
531 struct iris_format_info src_fmt =
532 iris_format_for_usage(devinfo, src_res->base.format,
533 ISL_SURF_USAGE_TEXTURE_BIT);
534 enum isl_aux_usage stc_src_aux_usage =
535 iris_resource_texture_aux_usage(ice, src_res, src_fmt.fmt);
536
537 struct iris_format_info dst_fmt =
538 iris_format_for_usage(devinfo, stc_dst->base.format,
539 ISL_SURF_USAGE_RENDER_TARGET_BIT);
540 stc_dst_aux_usage =
541 iris_resource_blorp_write_aux_usage(ice, stc_dst, dst_fmt.fmt);
542
543 iris_resource_prepare_access(ice, src_res, info->src.level, 1,
544 info->src.box.z, info->src.box.depth,
545 stc_src_aux_usage, false);
546 iris_emit_buffer_barrier_for(batch, src_res->bo, IRIS_DOMAIN_OTHER_READ);
547 iris_resource_prepare_access(ice, stc_dst, info->dst.level, 1,
548 info->dst.box.z, info->dst.box.depth,
549 stc_dst_aux_usage, false);
550 iris_emit_buffer_barrier_for(batch, stc_dst->bo, IRIS_DOMAIN_RENDER_WRITE);
551 iris_blorp_surf_for_resource(&screen->isl_dev, &src_surf,
552 &src_res->base, stc_src_aux_usage,
553 info->src.level, false);
554 iris_blorp_surf_for_resource(&screen->isl_dev, &dst_surf,
555 &stc_dst->base, stc_dst_aux_usage,
556 info->dst.level, true);
557
558 for (int slice = 0; slice < info->dst.box.depth; slice++) {
559 iris_batch_maybe_flush(batch, 1500);
560 iris_batch_sync_region_start(batch);
561
562 blorp_blit(&blorp_batch,
563 &src_surf, info->src.level, info->src.box.z + slice,
564 ISL_FORMAT_R8_UINT, ISL_SWIZZLE_IDENTITY,
565 &dst_surf, info->dst.level, info->dst.box.z + slice,
566 ISL_FORMAT_R8_UINT, ISL_SWIZZLE_IDENTITY,
567 src_x0, src_y0, src_x1, src_y1,
568 dst_x0, dst_y0, dst_x1, dst_y1,
569 filter, mirror_x, mirror_y);
570
571 iris_batch_sync_region_end(batch);
572 }
573 }
574
575 blorp_batch_finish(&blorp_batch);
576
577 tex_cache_flush_hack(batch, src_fmt.fmt, src_res->surf.format);
578
579 if (info->mask & main_mask) {
580 iris_resource_finish_write(ice, dst_res, info->dst.level, info->dst.box.z,
581 info->dst.box.depth, dst_aux_usage);
582 }
583
584 if (stc_dst) {
585 iris_resource_finish_write(ice, stc_dst, info->dst.level, info->dst.box.z,
586 info->dst.box.depth, stc_dst_aux_usage);
587 }
588
589 iris_flush_and_dirty_for_history(ice, batch, (struct iris_resource *)
590 info->dst.resource,
591 PIPE_CONTROL_RENDER_TARGET_FLUSH,
592 "cache history: post-blit");
593 }
594
595 static void
get_copy_region_aux_settings(struct iris_context * ice,struct iris_resource * res,enum isl_aux_usage * out_aux_usage,bool * out_clear_supported,bool is_render_target)596 get_copy_region_aux_settings(struct iris_context *ice,
597 struct iris_resource *res,
598 enum isl_aux_usage *out_aux_usage,
599 bool *out_clear_supported,
600 bool is_render_target)
601 {
602 struct iris_screen *screen = (void *) ice->ctx.screen;
603 const struct gen_device_info *devinfo = &screen->devinfo;
604
605 switch (res->aux.usage) {
606 case ISL_AUX_USAGE_HIZ:
607 case ISL_AUX_USAGE_HIZ_CCS:
608 case ISL_AUX_USAGE_HIZ_CCS_WT:
609 if (is_render_target) {
610 *out_aux_usage = res->aux.usage;
611 } else {
612 *out_aux_usage = iris_resource_texture_aux_usage(ice, res,
613 res->surf.format);
614 }
615 *out_clear_supported = (*out_aux_usage != ISL_AUX_USAGE_NONE);
616 break;
617 case ISL_AUX_USAGE_MCS:
618 case ISL_AUX_USAGE_MCS_CCS:
619 case ISL_AUX_USAGE_CCS_E:
620 case ISL_AUX_USAGE_GEN12_CCS_E:
621 *out_aux_usage = res->aux.usage;
622 /* Prior to Gen9, fast-clear only supported 0/1 clear colors. Since
623 * we're going to re-interpret the format as an integer format possibly
624 * with a different number of components, we can't handle clear colors
625 * until Gen9.
626 */
627 *out_clear_supported = devinfo->gen >= 9;
628 break;
629 case ISL_AUX_USAGE_STC_CCS:
630 *out_aux_usage = res->aux.usage;
631 *out_clear_supported = false;
632 break;
633 default:
634 *out_aux_usage = ISL_AUX_USAGE_NONE;
635 *out_clear_supported = false;
636 break;
637 }
638 }
639
640 /**
641 * Perform a GPU-based raw memory copy between compatible view classes.
642 *
643 * Does not perform any flushing - the new data may still be left in the
644 * render cache, and old data may remain in other caches.
645 *
646 * Wraps blorp_copy() and blorp_buffer_copy().
647 */
648 void
iris_copy_region(struct blorp_context * blorp,struct iris_batch * batch,struct pipe_resource * dst,unsigned dst_level,unsigned dstx,unsigned dsty,unsigned dstz,struct pipe_resource * src,unsigned src_level,const struct pipe_box * src_box)649 iris_copy_region(struct blorp_context *blorp,
650 struct iris_batch *batch,
651 struct pipe_resource *dst,
652 unsigned dst_level,
653 unsigned dstx, unsigned dsty, unsigned dstz,
654 struct pipe_resource *src,
655 unsigned src_level,
656 const struct pipe_box *src_box)
657 {
658 struct blorp_batch blorp_batch;
659 struct iris_context *ice = blorp->driver_ctx;
660 struct iris_screen *screen = (void *) ice->ctx.screen;
661 struct iris_resource *src_res = (void *) src;
662 struct iris_resource *dst_res = (void *) dst;
663
664 enum isl_aux_usage src_aux_usage, dst_aux_usage;
665 bool src_clear_supported, dst_clear_supported;
666 get_copy_region_aux_settings(ice, src_res, &src_aux_usage,
667 &src_clear_supported, false);
668 get_copy_region_aux_settings(ice, dst_res, &dst_aux_usage,
669 &dst_clear_supported, true);
670
671 if (iris_batch_references(batch, src_res->bo))
672 tex_cache_flush_hack(batch, ISL_FORMAT_UNSUPPORTED, src_res->surf.format);
673
674 if (dst->target == PIPE_BUFFER)
675 util_range_add(&dst_res->base, &dst_res->valid_buffer_range, dstx, dstx + src_box->width);
676
677 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
678 struct blorp_address src_addr = {
679 .buffer = iris_resource_bo(src), .offset = src_box->x,
680 };
681 struct blorp_address dst_addr = {
682 .buffer = iris_resource_bo(dst), .offset = dstx,
683 .reloc_flags = EXEC_OBJECT_WRITE,
684 };
685
686 iris_emit_buffer_barrier_for(batch, iris_resource_bo(src),
687 IRIS_DOMAIN_OTHER_READ);
688 iris_emit_buffer_barrier_for(batch, iris_resource_bo(dst),
689 IRIS_DOMAIN_RENDER_WRITE);
690
691 iris_batch_maybe_flush(batch, 1500);
692
693 iris_batch_sync_region_start(batch);
694 blorp_batch_init(&ice->blorp, &blorp_batch, batch, 0);
695 blorp_buffer_copy(&blorp_batch, src_addr, dst_addr, src_box->width);
696 blorp_batch_finish(&blorp_batch);
697 iris_batch_sync_region_end(batch);
698 } else {
699 // XXX: what about one surface being a buffer and not the other?
700
701 struct blorp_surf src_surf, dst_surf;
702 iris_blorp_surf_for_resource(&screen->isl_dev, &src_surf,
703 src, src_aux_usage, src_level, false);
704 iris_blorp_surf_for_resource(&screen->isl_dev, &dst_surf,
705 dst, dst_aux_usage, dst_level, true);
706
707 iris_resource_prepare_access(ice, src_res, src_level, 1,
708 src_box->z, src_box->depth,
709 src_aux_usage, src_clear_supported);
710 iris_resource_prepare_access(ice, dst_res, dst_level, 1,
711 dstz, src_box->depth,
712 dst_aux_usage, dst_clear_supported);
713
714 iris_emit_buffer_barrier_for(batch, iris_resource_bo(src),
715 IRIS_DOMAIN_OTHER_READ);
716 iris_emit_buffer_barrier_for(batch, iris_resource_bo(dst),
717 IRIS_DOMAIN_RENDER_WRITE);
718
719 blorp_batch_init(&ice->blorp, &blorp_batch, batch, 0);
720
721 for (int slice = 0; slice < src_box->depth; slice++) {
722 iris_batch_maybe_flush(batch, 1500);
723
724 iris_batch_sync_region_start(batch);
725 blorp_copy(&blorp_batch, &src_surf, src_level, src_box->z + slice,
726 &dst_surf, dst_level, dstz + slice,
727 src_box->x, src_box->y, dstx, dsty,
728 src_box->width, src_box->height);
729 iris_batch_sync_region_end(batch);
730 }
731 blorp_batch_finish(&blorp_batch);
732
733 iris_resource_finish_write(ice, dst_res, dst_level, dstz,
734 src_box->depth, dst_aux_usage);
735 }
736
737 tex_cache_flush_hack(batch, ISL_FORMAT_UNSUPPORTED, src_res->surf.format);
738 }
739
740 static struct iris_batch *
get_preferred_batch(struct iris_context * ice,struct iris_bo * bo)741 get_preferred_batch(struct iris_context *ice, struct iris_bo *bo)
742 {
743 /* If the compute batch is already using this buffer, we'd prefer to
744 * continue queueing in the compute batch.
745 */
746 if (iris_batch_references(&ice->batches[IRIS_BATCH_COMPUTE], bo))
747 return &ice->batches[IRIS_BATCH_COMPUTE];
748
749 /* Otherwise default to the render batch. */
750 return &ice->batches[IRIS_BATCH_RENDER];
751 }
752
753
754 /**
755 * The pipe->resource_copy_region() driver hook.
756 *
757 * This implements ARB_copy_image semantics - a raw memory copy between
758 * compatible view classes.
759 */
760 static void
iris_resource_copy_region(struct pipe_context * ctx,struct pipe_resource * p_dst,unsigned dst_level,unsigned dstx,unsigned dsty,unsigned dstz,struct pipe_resource * p_src,unsigned src_level,const struct pipe_box * src_box)761 iris_resource_copy_region(struct pipe_context *ctx,
762 struct pipe_resource *p_dst,
763 unsigned dst_level,
764 unsigned dstx, unsigned dsty, unsigned dstz,
765 struct pipe_resource *p_src,
766 unsigned src_level,
767 const struct pipe_box *src_box)
768 {
769 struct iris_context *ice = (void *) ctx;
770 struct iris_screen *screen = (void *) ctx->screen;
771 struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
772 struct iris_resource *src = (void *) p_src;
773 struct iris_resource *dst = (void *) p_dst;
774
775 if (iris_resource_unfinished_aux_import(src))
776 iris_resource_finish_aux_import(ctx->screen, src);
777 if (iris_resource_unfinished_aux_import(dst))
778 iris_resource_finish_aux_import(ctx->screen, dst);
779
780 /* Use MI_COPY_MEM_MEM for tiny (<= 16 byte, % 4) buffer copies. */
781 if (p_src->target == PIPE_BUFFER && p_dst->target == PIPE_BUFFER &&
782 (src_box->width % 4 == 0) && src_box->width <= 16) {
783 struct iris_bo *dst_bo = iris_resource_bo(p_dst);
784 batch = get_preferred_batch(ice, dst_bo);
785 iris_batch_maybe_flush(batch, 24 + 5 * (src_box->width / 4));
786 iris_emit_pipe_control_flush(batch,
787 "stall for MI_COPY_MEM_MEM copy_region",
788 PIPE_CONTROL_CS_STALL);
789 screen->vtbl.copy_mem_mem(batch, dst_bo, dstx, iris_resource_bo(p_src),
790 src_box->x, src_box->width);
791 return;
792 }
793
794 iris_copy_region(&ice->blorp, batch, p_dst, dst_level, dstx, dsty, dstz,
795 p_src, src_level, src_box);
796
797 if (util_format_is_depth_and_stencil(p_dst->format) &&
798 util_format_has_stencil(util_format_description(p_src->format))) {
799 struct iris_resource *junk, *s_src_res, *s_dst_res;
800 iris_get_depth_stencil_resources(p_src, &junk, &s_src_res);
801 iris_get_depth_stencil_resources(p_dst, &junk, &s_dst_res);
802
803 iris_copy_region(&ice->blorp, batch, &s_dst_res->base, dst_level, dstx,
804 dsty, dstz, &s_src_res->base, src_level, src_box);
805 }
806
807 iris_flush_and_dirty_for_history(ice, batch, dst,
808 PIPE_CONTROL_RENDER_TARGET_FLUSH,
809 "cache history: post copy_region");
810 }
811
812 void
iris_init_blit_functions(struct pipe_context * ctx)813 iris_init_blit_functions(struct pipe_context *ctx)
814 {
815 ctx->blit = iris_blit;
816 ctx->resource_copy_region = iris_resource_copy_region;
817 }
818