iris: Implement multi-slice copy_region
[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/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 float scale_x = (float) (*src_x1 - *src_x0) / (*dst_x1 - *dst_x0);
202 float scale_y = (float) (*src_y1 - *src_y0) / (*dst_y1 - *dst_y0);
203
204 /* Clip left side */
205 clip_coordinates(mirror_x, src_x0, dst_x0, dst_x1,
206 clip_dst_x0, clip_dst_x1, scale_x, true);
207
208 /* Clip right side */
209 clip_coordinates(mirror_x, src_x1, dst_x1, dst_x0,
210 clip_dst_x1, clip_dst_x0, scale_x, false);
211
212 /* Clip bottom side */
213 clip_coordinates(mirror_y, src_y0, dst_y0, dst_y1,
214 clip_dst_y0, clip_dst_y1, scale_y, true);
215
216 /* Clip top side */
217 clip_coordinates(mirror_y, src_y1, dst_y1, dst_y0,
218 clip_dst_y1, clip_dst_y0, scale_y, false);
219
220 return false;
221 }
222
223 void
224 iris_blorp_surf_for_resource(struct blorp_surf *surf,
225 struct pipe_resource *p_res,
226 enum isl_aux_usage aux_usage,
227 bool is_render_target)
228 {
229 struct iris_resource *res = (void *) p_res;
230
231 *surf = (struct blorp_surf) {
232 .surf = &res->surf,
233 .addr = (struct blorp_address) {
234 .buffer = res->bo,
235 .offset = 0, // XXX: ???
236 .reloc_flags = is_render_target ? EXEC_OBJECT_WRITE : 0,
237 .mocs = I915_MOCS_CACHED, // XXX: BDW MOCS, PTE MOCS
238 },
239 .aux_usage = aux_usage,
240 };
241
242 assert(surf->aux_usage == ISL_AUX_USAGE_NONE);
243 }
244
245 /**
246 * The pipe->blit() driver hook.
247 *
248 * This performs a blit between two surfaces, which copies data but may
249 * also perform format conversion, scaling, flipping, and so on.
250 */
251 static void
252 iris_blit(struct pipe_context *ctx, const struct pipe_blit_info *info)
253 {
254 struct iris_context *ice = (void *) ctx;
255 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
256 const struct gen_device_info *devinfo = &screen->devinfo;
257 enum blorp_batch_flags blorp_flags = 0;
258
259 if (info->render_condition_enable) {
260 if (ice->state.predicate == IRIS_PREDICATE_STATE_DONT_RENDER)
261 return;
262
263 if (ice->state.predicate == IRIS_PREDICATE_STATE_USE_BIT)
264 blorp_flags |= BLORP_BATCH_PREDICATE_ENABLE;
265 }
266
267 struct blorp_surf src_surf, dst_surf;
268 iris_blorp_surf_for_resource(&src_surf, info->src.resource,
269 ISL_AUX_USAGE_NONE, false);
270 iris_blorp_surf_for_resource(&dst_surf, info->dst.resource,
271 ISL_AUX_USAGE_NONE, true);
272
273 struct iris_format_info src_fmt =
274 iris_format_for_usage(devinfo, info->src.format,
275 ISL_SURF_USAGE_TEXTURE_BIT);
276 struct iris_format_info dst_fmt =
277 iris_format_for_usage(devinfo, info->dst.format,
278 ISL_SURF_USAGE_RENDER_TARGET_BIT);
279
280 float src_x0 = info->src.box.x;
281 float src_x1 = info->src.box.x + info->src.box.width;
282 float src_y0 = info->src.box.y;
283 float src_y1 = info->src.box.y + info->src.box.height;
284 float dst_x0 = info->dst.box.x;
285 float dst_x1 = info->dst.box.x + info->dst.box.width;
286 float dst_y0 = info->dst.box.y;
287 float dst_y1 = info->dst.box.y + info->dst.box.height;
288 bool mirror_x = apply_mirror(&src_x0, &src_x1);
289 bool mirror_y = apply_mirror(&src_y0, &src_y1);
290 enum blorp_filter filter;
291
292 if (info->scissor_enable) {
293 bool noop = apply_blit_scissor(&info->scissor,
294 &src_x0, &src_y0, &src_x1, &src_y1,
295 &dst_x0, &dst_y0, &dst_x1, &dst_y1,
296 mirror_x, mirror_y);
297 if (noop)
298 return;
299 }
300
301 if (abs(info->dst.box.width) == abs(info->src.box.width) &&
302 abs(info->dst.box.height) == abs(info->src.box.height)) {
303 if (src_surf.surf->samples > 1 && dst_surf.surf->samples <= 1) {
304 /* The OpenGL ES 3.2 specification, section 16.2.1, says:
305 *
306 * "If the read framebuffer is multisampled (its effective
307 * value of SAMPLE_BUFFERS is one) and the draw framebuffer
308 * is not (its value of SAMPLE_BUFFERS is zero), the samples
309 * corresponding to each pixel location in the source are
310 * converted to a single sample before being written to the
311 * destination. The filter parameter is ignored. If the
312 * source formats are integer types or stencil values, a
313 * single sample’s value is selected for each pixel. If the
314 * source formats are floating-point or normalized types,
315 * the sample values for each pixel are resolved in an
316 * implementation-dependent manner. If the source formats
317 * are depth values, sample values are resolved in an
318 * implementation-dependent manner where the result will be
319 * between the minimum and maximum depth values in the pixel."
320 *
321 * When selecting a single sample, we always choose sample 0.
322 */
323 if (util_format_is_depth_or_stencil(info->src.format) ||
324 util_format_is_pure_integer(info->src.format)) {
325 filter = BLORP_FILTER_SAMPLE_0;
326 } else {
327 filter = BLORP_FILTER_AVERAGE;
328 }
329 } else {
330 /* The OpenGL 4.6 specification, section 18.3.1, says:
331 *
332 * "If the source and destination dimensions are identical,
333 * no filtering is applied."
334 *
335 * Using BLORP_FILTER_NONE will also handle the upsample case by
336 * replicating the one value in the source to all values in the
337 * destination.
338 */
339 filter = BLORP_FILTER_NONE;
340 }
341 } else if (info->filter == PIPE_TEX_FILTER_LINEAR) {
342 filter = BLORP_FILTER_BILINEAR;
343 } else {
344 filter = BLORP_FILTER_NEAREST;
345 }
346
347 struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
348
349 struct blorp_batch blorp_batch;
350 blorp_batch_init(&ice->blorp, &blorp_batch, batch, blorp_flags);
351
352 for (int slice = 0; slice < info->dst.box.depth; slice++) {
353 iris_batch_maybe_flush(batch, 1500);
354
355 blorp_blit(&blorp_batch,
356 &src_surf, info->src.level, info->src.box.z + slice,
357 src_fmt.fmt, src_fmt.swizzle,
358 &dst_surf, info->dst.level, info->dst.box.z + slice,
359 dst_fmt.fmt, ISL_SWIZZLE_IDENTITY,
360 src_x0, src_y0, src_x1, src_y1,
361 dst_x0, dst_y0, dst_x1, dst_y1,
362 filter, mirror_x, mirror_y);
363 }
364
365 if (util_format_is_depth_and_stencil(info->dst.format) &&
366 util_format_has_stencil(util_format_description(info->src.format))) {
367 struct iris_resource *src_res, *dst_res, *junk;
368 iris_get_depth_stencil_resources(info->src.resource, &junk, &src_res);
369 iris_get_depth_stencil_resources(info->dst.resource, &junk, &dst_res);
370 iris_blorp_surf_for_resource(&src_surf, &src_res->base,
371 ISL_AUX_USAGE_NONE, false);
372 iris_blorp_surf_for_resource(&dst_surf, &dst_res->base,
373 ISL_AUX_USAGE_NONE, true);
374
375 for (int slice = 0; slice < info->dst.box.depth; slice++) {
376 iris_batch_maybe_flush(batch, 1500);
377
378 blorp_blit(&blorp_batch,
379 &src_surf, info->src.level, info->src.box.z + slice,
380 ISL_FORMAT_R8_UINT, ISL_SWIZZLE_IDENTITY,
381 &dst_surf, info->dst.level, info->dst.box.z + slice,
382 ISL_FORMAT_R8_UINT, ISL_SWIZZLE_IDENTITY,
383 src_x0, src_y0, src_x1, src_y1,
384 dst_x0, dst_y0, dst_x1, dst_y1,
385 filter, mirror_x, mirror_y);
386 }
387 }
388
389 blorp_batch_finish(&blorp_batch);
390
391 iris_flush_and_dirty_for_history(ice, batch, (struct iris_resource *)
392 info->dst.resource);
393 }
394
395 /**
396 * The pipe->resource_copy_region() driver hook.
397 *
398 * This implements ARB_copy_image semantics - a raw memory copy between
399 * compatible view classes.
400 */
401 static void
402 iris_resource_copy_region(struct pipe_context *ctx,
403 struct pipe_resource *dst,
404 unsigned dst_level,
405 unsigned dstx, unsigned dsty, unsigned dstz,
406 struct pipe_resource *src,
407 unsigned src_level,
408 const struct pipe_box *src_box)
409 {
410 struct blorp_batch blorp_batch;
411 struct iris_context *ice = (void *) ctx;
412 struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
413
414 blorp_batch_init(&ice->blorp, &blorp_batch, batch, 0);
415
416 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
417 struct blorp_address src_addr = {
418 .buffer = iris_resource_bo(src), .offset = src_box->x,
419 };
420 struct blorp_address dst_addr = {
421 .buffer = iris_resource_bo(dst), .offset = dstx,
422 };
423
424 iris_batch_maybe_flush(batch, 1500);
425
426 blorp_buffer_copy(&blorp_batch, src_addr, dst_addr, src_box->width);
427 } else {
428 // XXX: what about one surface being a buffer and not the other?
429
430 struct blorp_surf src_surf, dst_surf;
431 iris_blorp_surf_for_resource(&src_surf, src, ISL_AUX_USAGE_NONE, false);
432 iris_blorp_surf_for_resource(&dst_surf, dst, ISL_AUX_USAGE_NONE, true);
433
434 for (int slice = 0; slice < src_box->depth; slice++) {
435 iris_batch_maybe_flush(batch, 1500);
436
437 blorp_copy(&blorp_batch, &src_surf, src_level, src_box->z + slice,
438 &dst_surf, dst_level, dstz + slice,
439 src_box->x, src_box->y, dstx, dsty,
440 src_box->width, src_box->height);
441 }
442 }
443
444 blorp_batch_finish(&blorp_batch);
445
446 iris_flush_and_dirty_for_history(ice, batch, (struct iris_resource *) dst);
447 }
448
449 void
450 iris_init_blit_functions(struct pipe_context *ctx)
451 {
452 ctx->blit = iris_blit;
453 ctx->resource_copy_region = iris_resource_copy_region;
454 }