2 * Copyright © 2017 Intel Corporation
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:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
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.
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"
37 * Helper function for handling mirror image blits.
39 * If coord0 > coord1, swap them and return "true" (mirrored).
42 apply_mirror(float *coord0
, float *coord1
)
44 if (*coord0
> *coord1
) {
54 * Compute the number of pixels to clip for each side of a rect
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
69 * \return false if we clip everything away, true otherwise
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
)
77 /* If we are going to clip everything away, stop. */
78 if (!(min_x
<= max_x
&&
90 *clipped_x0
= min_x
- x0
;
94 *clipped_x1
= x1
- max_x
;
99 *clipped_y0
= min_y
- y0
;
103 *clipped_y1
= y1
- max_y
;
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.
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
126 clip_coordinates(bool mirror
,
127 float *src
, float *dst0
, float *dst1
,
131 bool is_left_or_bottom
)
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
139 int mult
= is_left_or_bottom
? 1 : -1;
142 *dst0
+= clipped_dst0
* mult
;
143 *src
+= clipped_dst0
* scale
* mult
;
145 *dst1
-= clipped_dst1
* mult
;
146 *src
+= clipped_dst1
* scale
* mult
;
151 * Apply a scissor rectangle to blit coordinates.
153 * Returns true if the blit was entirely scissored away.
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
)
163 float clip_dst_x0
, clip_dst_x1
, clip_dst_y0
, clip_dst_y1
;
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
))
173 // XXX: comments assume source clipping, which we don't do
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.
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
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.
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
);
205 clip_coordinates(mirror_x
, src_x0
, dst_x0
, dst_x1
,
206 clip_dst_x0
, clip_dst_x1
, scale_x
, true);
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);
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);
217 clip_coordinates(mirror_y
, src_y1
, dst_y1
, dst_y0
,
218 clip_dst_y1
, clip_dst_y0
, scale_y
, false);
224 iris_blorp_surf_for_resource(struct iris_vtable
*vtbl
,
225 struct blorp_surf
*surf
,
226 struct pipe_resource
*p_res
,
227 enum isl_aux_usage aux_usage
,
229 bool is_render_target
)
231 struct iris_resource
*res
= (void *) p_res
;
233 if (aux_usage
== ISL_AUX_USAGE_HIZ
&&
234 !iris_resource_level_has_hiz(res
, level
))
235 aux_usage
= ISL_AUX_USAGE_NONE
;
237 *surf
= (struct blorp_surf
) {
239 .addr
= (struct blorp_address
) {
241 .offset
= 0, // XXX: ???
242 .reloc_flags
= is_render_target
? EXEC_OBJECT_WRITE
: 0,
243 .mocs
= vtbl
->mocs(res
->bo
),
245 .aux_usage
= aux_usage
,
248 if (aux_usage
!= ISL_AUX_USAGE_NONE
) {
249 surf
->aux_surf
= &res
->aux
.surf
;
250 surf
->aux_addr
= (struct blorp_address
) {
251 .buffer
= res
->aux
.bo
,
252 .offset
= res
->aux
.offset
,
253 .reloc_flags
= is_render_target
? EXEC_OBJECT_WRITE
: 0,
254 .mocs
= vtbl
->mocs(res
->bo
),
262 * The pipe->blit() driver hook.
264 * This performs a blit between two surfaces, which copies data but may
265 * also perform format conversion, scaling, flipping, and so on.
268 iris_blit(struct pipe_context
*ctx
, const struct pipe_blit_info
*info
)
270 struct iris_context
*ice
= (void *) ctx
;
271 struct iris_screen
*screen
= (struct iris_screen
*)ctx
->screen
;
272 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
273 struct iris_batch
*batch
= &ice
->batches
[IRIS_BATCH_RENDER
];
274 enum blorp_batch_flags blorp_flags
= 0;
275 struct iris_resource
*src_res
= (void *) info
->src
.resource
;
276 struct iris_resource
*dst_res
= (void *) info
->dst
.resource
;
278 /* We don't support color masking. */
279 assert((info
->mask
& PIPE_MASK_RGBA
) == PIPE_MASK_RGBA
||
280 (info
->mask
& PIPE_MASK_RGBA
) == 0);
282 if (info
->render_condition_enable
) {
283 if (ice
->state
.predicate
== IRIS_PREDICATE_STATE_DONT_RENDER
)
286 if (ice
->state
.predicate
== IRIS_PREDICATE_STATE_USE_BIT
)
287 blorp_flags
|= BLORP_BATCH_PREDICATE_ENABLE
;
290 struct iris_format_info src_fmt
=
291 iris_format_for_usage(devinfo
, info
->src
.format
,
292 ISL_SURF_USAGE_TEXTURE_BIT
);
293 enum isl_aux_usage src_aux_usage
=
294 iris_resource_texture_aux_usage(ice
, src_res
, src_fmt
.fmt
, 0);
296 if (src_aux_usage
== ISL_AUX_USAGE_HIZ
)
297 src_aux_usage
= ISL_AUX_USAGE_NONE
;
299 bool src_clear_supported
= src_aux_usage
!= ISL_AUX_USAGE_NONE
&&
300 src_res
->surf
.format
== src_fmt
.fmt
;
302 iris_resource_prepare_access(ice
, batch
, src_res
, info
->src
.level
, 1,
303 info
->src
.box
.z
, info
->src
.box
.depth
,
304 src_aux_usage
, src_clear_supported
);
306 struct iris_format_info dst_fmt
=
307 iris_format_for_usage(devinfo
, info
->dst
.format
,
308 ISL_SURF_USAGE_RENDER_TARGET_BIT
);
309 enum isl_aux_usage dst_aux_usage
=
310 iris_resource_render_aux_usage(ice
, dst_res
, dst_fmt
.fmt
, false, false);
311 bool dst_clear_supported
= dst_aux_usage
!= ISL_AUX_USAGE_NONE
;
313 struct blorp_surf src_surf
, dst_surf
;
314 iris_blorp_surf_for_resource(&ice
->vtbl
, &src_surf
, info
->src
.resource
,
315 src_aux_usage
, info
->src
.level
, false);
316 iris_blorp_surf_for_resource(&ice
->vtbl
, &dst_surf
, info
->dst
.resource
,
317 dst_aux_usage
, info
->dst
.level
, true);
319 iris_resource_prepare_access(ice
, batch
, dst_res
, info
->dst
.level
, 1,
320 info
->dst
.box
.z
, info
->dst
.box
.depth
,
321 dst_aux_usage
, dst_clear_supported
);
323 float src_x0
= info
->src
.box
.x
;
324 float src_x1
= info
->src
.box
.x
+ info
->src
.box
.width
;
325 float src_y0
= info
->src
.box
.y
;
326 float src_y1
= info
->src
.box
.y
+ info
->src
.box
.height
;
327 float dst_x0
= info
->dst
.box
.x
;
328 float dst_x1
= info
->dst
.box
.x
+ info
->dst
.box
.width
;
329 float dst_y0
= info
->dst
.box
.y
;
330 float dst_y1
= info
->dst
.box
.y
+ info
->dst
.box
.height
;
331 bool mirror_x
= apply_mirror(&src_x0
, &src_x1
);
332 bool mirror_y
= apply_mirror(&src_y0
, &src_y1
);
333 enum blorp_filter filter
;
335 if (info
->scissor_enable
) {
336 bool noop
= apply_blit_scissor(&info
->scissor
,
337 &src_x0
, &src_y0
, &src_x1
, &src_y1
,
338 &dst_x0
, &dst_y0
, &dst_x1
, &dst_y1
,
344 if (abs(info
->dst
.box
.width
) == abs(info
->src
.box
.width
) &&
345 abs(info
->dst
.box
.height
) == abs(info
->src
.box
.height
)) {
346 if (src_surf
.surf
->samples
> 1 && dst_surf
.surf
->samples
<= 1) {
347 /* The OpenGL ES 3.2 specification, section 16.2.1, says:
349 * "If the read framebuffer is multisampled (its effective
350 * value of SAMPLE_BUFFERS is one) and the draw framebuffer
351 * is not (its value of SAMPLE_BUFFERS is zero), the samples
352 * corresponding to each pixel location in the source are
353 * converted to a single sample before being written to the
354 * destination. The filter parameter is ignored. If the
355 * source formats are integer types or stencil values, a
356 * single sample’s value is selected for each pixel. If the
357 * source formats are floating-point or normalized types,
358 * the sample values for each pixel are resolved in an
359 * implementation-dependent manner. If the source formats
360 * are depth values, sample values are resolved in an
361 * implementation-dependent manner where the result will be
362 * between the minimum and maximum depth values in the pixel."
364 * When selecting a single sample, we always choose sample 0.
366 if (util_format_is_depth_or_stencil(info
->src
.format
) ||
367 util_format_is_pure_integer(info
->src
.format
)) {
368 filter
= BLORP_FILTER_SAMPLE_0
;
370 filter
= BLORP_FILTER_AVERAGE
;
373 /* The OpenGL 4.6 specification, section 18.3.1, says:
375 * "If the source and destination dimensions are identical,
376 * no filtering is applied."
378 * Using BLORP_FILTER_NONE will also handle the upsample case by
379 * replicating the one value in the source to all values in the
382 filter
= BLORP_FILTER_NONE
;
384 } else if (info
->filter
== PIPE_TEX_FILTER_LINEAR
) {
385 filter
= BLORP_FILTER_BILINEAR
;
387 filter
= BLORP_FILTER_NEAREST
;
390 struct blorp_batch blorp_batch
;
391 blorp_batch_init(&ice
->blorp
, &blorp_batch
, batch
, blorp_flags
);
394 if (info
->dst
.format
== PIPE_FORMAT_S8_UINT
)
395 main_mask
= PIPE_MASK_S
;
396 else if (util_format_is_depth_or_stencil(info
->dst
.format
))
397 main_mask
= PIPE_MASK_Z
;
399 main_mask
= PIPE_MASK_RGBA
;
401 if (info
->mask
& main_mask
) {
402 for (int slice
= 0; slice
< info
->dst
.box
.depth
; slice
++) {
403 iris_batch_maybe_flush(batch
, 1500);
405 blorp_blit(&blorp_batch
,
406 &src_surf
, info
->src
.level
, info
->src
.box
.z
+ slice
,
407 src_fmt
.fmt
, src_fmt
.swizzle
,
408 &dst_surf
, info
->dst
.level
, info
->dst
.box
.z
+ slice
,
409 dst_fmt
.fmt
, ISL_SWIZZLE_IDENTITY
,
410 src_x0
, src_y0
, src_x1
, src_y1
,
411 dst_x0
, dst_y0
, dst_x1
, dst_y1
,
412 filter
, mirror_x
, mirror_y
);
416 if ((info
->mask
& PIPE_MASK_S
) &&
417 util_format_is_depth_and_stencil(info
->dst
.format
) &&
418 util_format_has_stencil(util_format_description(info
->src
.format
))) {
419 struct iris_resource
*src_res
, *dst_res
, *junk
;
420 iris_get_depth_stencil_resources(info
->src
.resource
, &junk
, &src_res
);
421 iris_get_depth_stencil_resources(info
->dst
.resource
, &junk
, &dst_res
);
422 iris_blorp_surf_for_resource(&ice
->vtbl
, &src_surf
, &src_res
->base
,
423 ISL_AUX_USAGE_NONE
, info
->src
.level
, false);
424 iris_blorp_surf_for_resource(&ice
->vtbl
, &dst_surf
, &dst_res
->base
,
425 ISL_AUX_USAGE_NONE
, info
->dst
.level
, true);
427 for (int slice
= 0; slice
< info
->dst
.box
.depth
; slice
++) {
428 iris_batch_maybe_flush(batch
, 1500);
430 blorp_blit(&blorp_batch
,
431 &src_surf
, info
->src
.level
, info
->src
.box
.z
+ slice
,
432 ISL_FORMAT_R8_UINT
, ISL_SWIZZLE_IDENTITY
,
433 &dst_surf
, info
->dst
.level
, info
->dst
.box
.z
+ slice
,
434 ISL_FORMAT_R8_UINT
, ISL_SWIZZLE_IDENTITY
,
435 src_x0
, src_y0
, src_x1
, src_y1
,
436 dst_x0
, dst_y0
, dst_x1
, dst_y1
,
437 filter
, mirror_x
, mirror_y
);
441 blorp_batch_finish(&blorp_batch
);
443 iris_resource_finish_write(ice
, dst_res
, info
->dst
.level
, info
->dst
.box
.z
,
444 info
->dst
.box
.depth
, dst_aux_usage
);
446 iris_flush_and_dirty_for_history(ice
, batch
, (struct iris_resource
*)
451 get_copy_region_aux_settings(const struct gen_device_info
*devinfo
,
452 struct iris_resource
*res
,
453 enum isl_aux_usage
*out_aux_usage
,
454 bool *out_clear_supported
)
456 switch (res
->aux
.usage
) {
457 case ISL_AUX_USAGE_MCS
:
458 case ISL_AUX_USAGE_CCS_E
:
459 *out_aux_usage
= res
->aux
.usage
;
460 /* Prior to Gen9, fast-clear only supported 0/1 clear colors. Since
461 * we're going to re-interpret the format as an integer format possibly
462 * with a different number of components, we can't handle clear colors
465 *out_clear_supported
= devinfo
->gen
>= 9;
468 *out_aux_usage
= ISL_AUX_USAGE_NONE
;
469 *out_clear_supported
= false;
475 * The pipe->resource_copy_region() driver hook.
477 * This implements ARB_copy_image semantics - a raw memory copy between
478 * compatible view classes.
481 iris_resource_copy_region(struct pipe_context
*ctx
,
482 struct pipe_resource
*dst
,
484 unsigned dstx
, unsigned dsty
, unsigned dstz
,
485 struct pipe_resource
*src
,
487 const struct pipe_box
*src_box
)
489 struct iris_screen
*screen
= (void *) ctx
->screen
;
490 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
491 struct blorp_batch blorp_batch
;
492 struct iris_context
*ice
= (void *) ctx
;
493 struct iris_batch
*batch
= &ice
->batches
[IRIS_BATCH_RENDER
];
494 struct iris_resource
*src_res
= (void *) src
;
495 struct iris_resource
*dst_res
= (void *) dst
;
497 enum isl_aux_usage src_aux_usage
, dst_aux_usage
;
498 bool src_clear_supported
, dst_clear_supported
;
499 get_copy_region_aux_settings(devinfo
, src_res
, &src_aux_usage
,
500 &src_clear_supported
);
501 get_copy_region_aux_settings(devinfo
, dst_res
, &dst_aux_usage
,
502 &dst_clear_supported
);
504 if (dst
->target
== PIPE_BUFFER
&& src
->target
== PIPE_BUFFER
) {
505 struct blorp_address src_addr
= {
506 .buffer
= iris_resource_bo(src
), .offset
= src_box
->x
,
508 struct blorp_address dst_addr
= {
509 .buffer
= iris_resource_bo(dst
), .offset
= dstx
,
512 iris_batch_maybe_flush(batch
, 1500);
514 blorp_batch_init(&ice
->blorp
, &blorp_batch
, batch
, 0);
515 blorp_buffer_copy(&blorp_batch
, src_addr
, dst_addr
, src_box
->width
);
516 blorp_batch_finish(&blorp_batch
);
518 iris_flush_and_dirty_for_history(ice
, batch
,
519 (struct iris_resource
*) dst
);
521 // XXX: what about one surface being a buffer and not the other?
523 struct blorp_surf src_surf
, dst_surf
;
524 iris_blorp_surf_for_resource(&ice
->vtbl
, &src_surf
, src
, src_aux_usage
,
526 iris_blorp_surf_for_resource(&ice
->vtbl
, &dst_surf
, dst
, dst_aux_usage
,
529 iris_resource_prepare_access(ice
, batch
, src_res
, src_level
, 1,
530 src_box
->z
, src_box
->depth
,
531 src_aux_usage
, src_clear_supported
);
532 iris_resource_prepare_access(ice
, batch
, dst_res
, dst_level
, 1,
533 dstz
, src_box
->depth
,
534 dst_aux_usage
, dst_clear_supported
);
536 blorp_batch_init(&ice
->blorp
, &blorp_batch
, batch
, 0);
538 for (int slice
= 0; slice
< src_box
->depth
; slice
++) {
539 iris_batch_maybe_flush(batch
, 1500);
541 blorp_copy(&blorp_batch
, &src_surf
, src_level
, src_box
->z
+ slice
,
542 &dst_surf
, dst_level
, dstz
+ slice
,
543 src_box
->x
, src_box
->y
, dstx
, dsty
,
544 src_box
->width
, src_box
->height
);
546 blorp_batch_finish(&blorp_batch
);
548 iris_resource_finish_write(ice
, dst_res
, dst_level
, dstz
,
549 src_box
->depth
, dst_aux_usage
);
555 iris_init_blit_functions(struct pipe_context
*ctx
)
557 ctx
->blit
= iris_blit
;
558 ctx
->resource_copy_region
= iris_resource_copy_region
;