2 * Copyright © 2014 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 (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #include "brw_context.h"
25 #include "brw_defines.h"
26 #include "intel_fbo.h"
27 #include "brw_meta_util.h"
28 #include "brw_state.h"
29 #include "main/blend.h"
30 #include "main/fbobject.h"
31 #include "util/format_srgb.h"
34 * Helper function for handling mirror image blits.
36 * If coord0 > coord1, swap them and invert the "mirror" boolean.
39 fixup_mirroring(bool *mirror
, float *coord0
, float *coord1
)
41 if (*coord0
> *coord1
) {
50 * Compute the number of pixels to clip for each side of a rect
52 * \param x0 The rect's left coordinate
53 * \param y0 The rect's bottom coordinate
54 * \param x1 The rect's right coordinate
55 * \param y1 The rect's top coordinate
56 * \param min_x The clipping region's left coordinate
57 * \param min_y The clipping region's bottom coordinate
58 * \param max_x The clipping region's right coordinate
59 * \param max_y The clipping region's top coordinate
60 * \param clipped_x0 The number of pixels to clip from the left side
61 * \param clipped_y0 The number of pixels to clip from the bottom side
62 * \param clipped_x1 The number of pixels to clip from the right side
63 * \param clipped_y1 The number of pixels to clip from the top side
65 * \return false if we clip everything away, true otherwise
68 compute_pixels_clipped(float x0
, float y0
, float x1
, float y1
,
69 float min_x
, float min_y
, float max_x
, float max_y
,
70 float *clipped_x0
, float *clipped_y0
, float *clipped_x1
, float *clipped_y1
)
72 /* If we are going to clip everything away, stop. */
73 if (!(min_x
<= max_x
&&
85 *clipped_x0
= min_x
- x0
;
89 *clipped_x1
= x1
- max_x
;
94 *clipped_y0
= min_y
- y0
;
98 *clipped_y1
= y1
- max_y
;
106 * Clips a coordinate (left, right, top or bottom) for the src or dst rect
107 * (whichever requires the largest clip) and adjusts the coordinate
108 * for the other rect accordingly.
110 * \param mirror true if mirroring is required
111 * \param src the source rect coordinate (for example srcX0)
112 * \param dst0 the dst rect coordinate (for example dstX0)
113 * \param dst1 the opposite dst rect coordinate (for example dstX1)
114 * \param clipped_src0 number of pixels to clip from the src coordinate
115 * \param clipped_dst0 number of pixels to clip from the dst coordinate
116 * \param clipped_dst1 number of pixels to clip from the opposite dst coordinate
117 * \param scale the src vs dst scale involved for that coordinate
118 * \param isLeftOrBottom true if we are clipping the left or bottom sides
122 clip_coordinates(bool mirror
,
123 float *src
, float *dst0
, float *dst1
,
130 /* When clipping we need to add or subtract pixels from the original
131 * coordinates depending on whether we are acting on the left/bottom
132 * or right/top sides of the rect respectively. We assume we have to
133 * add them in the code below, and multiply by -1 when we should
136 int mult
= isLeftOrBottom
? 1 : -1;
139 if (clipped_src0
>= clipped_dst0
* scale
) {
140 *src
+= clipped_src0
* mult
;
141 *dst0
+= clipped_src0
/ scale
* mult
;
143 *dst0
+= clipped_dst0
* mult
;
144 *src
+= clipped_dst0
* scale
* mult
;
147 if (clipped_src0
>= clipped_dst1
* scale
) {
148 *src
+= clipped_src0
* mult
;
149 *dst1
-= clipped_src0
/ scale
* mult
;
151 *dst1
-= clipped_dst1
* mult
;
152 *src
+= clipped_dst1
* scale
* mult
;
158 brw_meta_mirror_clip_and_scissor(const struct gl_context
*ctx
,
159 const struct gl_framebuffer
*read_fb
,
160 const struct gl_framebuffer
*draw_fb
,
161 GLfloat
*srcX0
, GLfloat
*srcY0
,
162 GLfloat
*srcX1
, GLfloat
*srcY1
,
163 GLfloat
*dstX0
, GLfloat
*dstY0
,
164 GLfloat
*dstX1
, GLfloat
*dstY1
,
165 bool *mirror_x
, bool *mirror_y
)
170 /* Detect if the blit needs to be mirrored */
171 fixup_mirroring(mirror_x
, srcX0
, srcX1
);
172 fixup_mirroring(mirror_x
, dstX0
, dstX1
);
173 fixup_mirroring(mirror_y
, srcY0
, srcY1
);
174 fixup_mirroring(mirror_y
, dstY0
, dstY1
);
176 /* Compute number of pixels to clip for each side of both rects. Return
177 * early if we are going to clip everything away.
188 if (!compute_pixels_clipped(*srcX0
, *srcY0
, *srcX1
, *srcY1
,
189 0, 0, read_fb
->Width
, read_fb
->Height
,
190 &clip_src_x0
, &clip_src_y0
, &clip_src_x1
, &clip_src_y1
))
193 if (!compute_pixels_clipped(*dstX0
, *dstY0
, *dstX1
, *dstY1
,
194 draw_fb
->_Xmin
, draw_fb
->_Ymin
, draw_fb
->_Xmax
, draw_fb
->_Ymax
,
195 &clip_dst_x0
, &clip_dst_y0
, &clip_dst_x1
, &clip_dst_y1
))
198 /* When clipping any of the two rects we need to adjust the coordinates in
199 * the other rect considering the scaling factor involved. To obtain the best
200 * precision we want to make sure that we only clip once per side to avoid
201 * accumulating errors due to the scaling adjustment.
203 * For example, if srcX0 and dstX0 need both to be clipped we want to avoid
204 * the situation where we clip srcX0 first, then adjust dstX0 accordingly
205 * but then we realize that the resulting dstX0 still needs to be clipped,
206 * so we clip dstX0 and adjust srcX0 again. Because we are applying scaling
207 * factors to adjust the coordinates in each clipping pass we lose some
208 * precision and that can affect the results of the blorp blit operation
209 * slightly. What we want to do here is detect the rect that we should
210 * clip first for each side so that when we adjust the other rect we ensure
211 * the resulting coordinate does not need to be clipped again.
213 * The code below implements this by comparing the number of pixels that
214 * we need to clip for each side of both rects considering the scales
215 * involved. For example, clip_src_x0 represents the number of pixels to be
216 * clipped for the src rect's left side, so if clip_src_x0 = 5,
217 * clip_dst_x0 = 4 and scaleX = 2 it means that we are clipping more from
218 * the dst rect so we should clip dstX0 only and adjust srcX0. This is
219 * because clipping 4 pixels in the dst is equivalent to clipping
220 * 4 * 2 = 8 > 5 in the src.
223 float scaleX
= (float) (*srcX1
- *srcX0
) / (*dstX1
- *dstX0
);
224 float scaleY
= (float) (*srcY1
- *srcY0
) / (*dstY1
- *dstY0
);
227 clip_coordinates(*mirror_x
,
229 clip_src_x0
, clip_dst_x0
, clip_dst_x1
,
232 /* Clip right side */
233 clip_coordinates(*mirror_x
,
235 clip_src_x1
, clip_dst_x1
, clip_dst_x0
,
238 /* Clip bottom side */
239 clip_coordinates(*mirror_y
,
241 clip_src_y0
, clip_dst_y0
, clip_dst_y1
,
245 clip_coordinates(*mirror_y
,
247 clip_src_y1
, clip_dst_y1
, clip_dst_y0
,
250 /* Account for the fact that in the system framebuffer, the origin is at
253 if (_mesa_is_winsys_fbo(read_fb
)) {
254 GLint tmp
= read_fb
->Height
- *srcY0
;
255 *srcY0
= read_fb
->Height
- *srcY1
;
257 *mirror_y
= !*mirror_y
;
259 if (_mesa_is_winsys_fbo(draw_fb
)) {
260 GLint tmp
= draw_fb
->Height
- *dstY0
;
261 *dstY0
= draw_fb
->Height
- *dstY1
;
263 *mirror_y
= !*mirror_y
;
270 * Creates a new named renderbuffer that wraps the first slice
271 * of an existing miptree.
273 * Clobbers the current renderbuffer binding (ctx->CurrentRenderbuffer).
275 struct gl_renderbuffer
*
276 brw_get_rb_for_slice(struct brw_context
*brw
,
277 struct intel_mipmap_tree
*mt
,
278 unsigned level
, unsigned layer
, bool flat
)
280 struct gl_context
*ctx
= &brw
->ctx
;
281 struct gl_renderbuffer
*rb
= ctx
->Driver
.NewRenderbuffer(ctx
, 0xDEADBEEF);
282 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
285 rb
->Format
= mt
->format
;
286 rb
->_BaseFormat
= _mesa_get_format_base_format(mt
->format
);
288 /* Program takes care of msaa and mip-level access manually for stencil.
289 * The surface is also treated as Y-tiled instead of as W-tiled calling for
290 * twice the width and half the height in dimensions.
293 const unsigned halign_stencil
= 8;
296 rb
->Width
= ALIGN(mt
->total_width
, halign_stencil
) * 2;
297 rb
->Height
= (mt
->total_height
/ mt
->physical_depth0
) / 2;
300 rb
->NumSamples
= mt
->num_samples
;
301 rb
->Width
= mt
->logical_width0
;
302 rb
->Height
= mt
->logical_height0
;
303 irb
->mt_level
= level
;
306 irb
->mt_layer
= layer
;
308 intel_miptree_reference(&irb
->mt
, mt
);
314 * Determine if fast color clear supports the given clear color.
316 * Fast color clear can only clear to color values of 1.0 or 0.0. At the
317 * moment we only support floating point, unorm, and snorm buffers.
320 brw_is_color_fast_clear_compatible(struct brw_context
*brw
,
321 const struct intel_mipmap_tree
*mt
,
322 const union gl_color_union
*color
)
324 const struct gl_context
*ctx
= &brw
->ctx
;
326 /* If we're mapping the render format to a different format than the
327 * format we use for texturing then it is a bit questionable whether it
328 * should be possible to use a fast clear. Although we only actually
329 * render using a renderable format, without the override workaround it
330 * wouldn't be possible to have a non-renderable surface in a fast clear
331 * state so the hardware probably legitimately doesn't need to support
332 * this case. At least on Gen9 this really does seem to cause problems.
335 brw_format_for_mesa_format(mt
->format
) !=
336 brw
->render_target_format
[mt
->format
])
339 /* Gen9 doesn't support fast clear on single-sampled SRGB buffers. When
340 * GL_FRAMEBUFFER_SRGB is enabled any color renderbuffers will be
341 * resolved in intel_update_state. In that case it's pointless to do a
342 * fast clear because it's very likely to be immediately resolved.
345 mt
->num_samples
<= 1 &&
346 ctx
->Color
.sRGBEnabled
&&
347 _mesa_get_srgb_format_linear(mt
->format
) != mt
->format
)
350 const mesa_format format
= _mesa_get_render_format(ctx
, mt
->format
);
351 if (_mesa_is_format_integer_color(format
)) {
353 perf_debug("Integer fast clear not enabled for (%s)",
354 _mesa_get_format_name(format
));
359 for (int i
= 0; i
< 4; i
++) {
360 if (!_mesa_format_has_color_component(format
, i
)) {
365 color
->f
[i
] != 0.0f
&& color
->f
[i
] != 1.0f
) {
373 * Convert the given color to a bitfield suitable for ORing into DWORD 7 of
374 * SURFACE_STATE (DWORD 12-15 on SKL+).
376 * Returned boolean tells if the given color differs from the stored.
379 brw_meta_set_fast_clear_color(struct brw_context
*brw
,
380 struct intel_mipmap_tree
*mt
,
381 const union gl_color_union
*color
)
383 union gl_color_union override_color
= *color
;
385 /* The sampler doesn't look at the format of the surface when the fast
386 * clear color is used so we need to implement luminance, intensity and
387 * missing components manually.
389 switch (_mesa_get_format_base_format(mt
->format
)) {
391 override_color
.ui
[3] = override_color
.ui
[0];
394 case GL_LUMINANCE_ALPHA
:
395 override_color
.ui
[1] = override_color
.ui
[0];
396 override_color
.ui
[2] = override_color
.ui
[0];
399 for (int i
= 0; i
< 3; i
++) {
400 if (!_mesa_format_has_color_component(mt
->format
, i
))
401 override_color
.ui
[i
] = 0;
406 if (!_mesa_format_has_color_component(mt
->format
, 3)) {
407 if (_mesa_is_format_integer_color(mt
->format
))
408 override_color
.ui
[3] = 1;
410 override_color
.f
[3] = 1.0f
;
413 /* Handle linear→SRGB conversion */
414 if (brw
->ctx
.Color
.sRGBEnabled
&&
415 _mesa_get_srgb_format_linear(mt
->format
) != mt
->format
) {
416 for (int i
= 0; i
< 3; i
++) {
417 override_color
.f
[i
] =
418 util_format_linear_to_srgb_float(override_color
.f
[i
]);
424 updated
= memcmp(&mt
->gen9_fast_clear_color
, &override_color
,
425 sizeof(mt
->gen9_fast_clear_color
));
426 mt
->gen9_fast_clear_color
= override_color
;
428 const uint32_t old_color_value
= mt
->fast_clear_color_value
;
430 mt
->fast_clear_color_value
= 0;
431 for (int i
= 0; i
< 4; i
++) {
432 /* Testing for non-0 works for integer and float colors */
433 if (override_color
.f
[i
] != 0.0f
) {
434 mt
->fast_clear_color_value
|=
435 1 << (GEN7_SURFACE_CLEAR_COLOR_SHIFT
+ (3 - i
));
439 updated
= (old_color_value
!= mt
->fast_clear_color_value
);
445 /* The x0, y0, x1, and y1 parameters must already be populated with the render
446 * area of the framebuffer to be cleared.
449 brw_get_fast_clear_rect(const struct brw_context
*brw
,
450 const struct isl_surf
*aux_surf
,
451 unsigned *x0
, unsigned *y0
,
452 unsigned *x1
, unsigned *y1
)
454 unsigned int x_align
, y_align
;
455 unsigned int x_scaledown
, y_scaledown
;
457 /* Only single sampled surfaces need to (and actually can) be resolved. */
458 if (aux_surf
->usage
== ISL_SURF_USAGE_CCS_BIT
) {
459 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
460 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
462 * Clear pass must have a clear rectangle that must follow
463 * alignment rules in terms of pixels and lines as shown in the
464 * table below. Further, the clear-rectangle height and width
465 * must be multiple of the following dimensions. If the height
466 * and width of the render target being cleared do not meet these
467 * requirements, an MCS buffer can be created such that it
468 * follows the requirement and covers the RT.
470 * The alignment size in the table that follows is related to the
471 * alignment size that is baked into the CCS surface format but with X
472 * alignment multiplied by 16 and Y alignment multiplied by 32.
474 x_align
= isl_format_get_layout(aux_surf
->format
)->bw
;
475 y_align
= isl_format_get_layout(aux_surf
->format
)->bh
;
479 /* SKL+ line alignment requirement for Y-tiled are half those of the prior
487 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
488 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
490 * In order to optimize the performance MCS buffer (when bound to
491 * 1X RT) clear similarly to MCS buffer clear for MSRT case,
492 * clear rect is required to be scaled by the following factors
493 * in the horizontal and vertical directions:
495 * The X and Y scale down factors in the table that follows are each
496 * equal to half the alignment value computed above.
498 x_scaledown
= x_align
/ 2;
499 y_scaledown
= y_align
/ 2;
501 /* From BSpec: 3D-Media-GPGPU Engine > 3D Pipeline > Pixel > Pixel
502 * Backend > MCS Buffer for Render Target(s) [DevIVB+] > Table "Color
503 * Clear of Non-MultiSampled Render Target Restrictions":
505 * Clear rectangle must be aligned to two times the number of
506 * pixels in the table shown below due to 16x16 hashing across the
512 assert(aux_surf
->usage
== ISL_SURF_USAGE_MCS_BIT
);
514 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
515 * Target(s)", beneath the "MSAA Compression" bullet (p326):
517 * Clear pass for this case requires that scaled down primitive
518 * is sent down with upper left co-ordinate to coincide with
519 * actual rectangle being cleared. For MSAA, clear rectangle’s
520 * height and width need to as show in the following table in
521 * terms of (width,height) of the RT.
523 * MSAA Width of Clear Rect Height of Clear Rect
524 * 2X Ceil(1/8*width) Ceil(1/2*height)
525 * 4X Ceil(1/8*width) Ceil(1/2*height)
526 * 8X Ceil(1/2*width) Ceil(1/2*height)
527 * 16X width Ceil(1/2*height)
529 * The text "with upper left co-ordinate to coincide with actual
530 * rectangle being cleared" is a little confusing--it seems to imply
531 * that to clear a rectangle from (x,y) to (x+w,y+h), one needs to
532 * feed the pipeline using the rectangle (x,y) to
533 * (x+Ceil(w/N),y+Ceil(h/2)), where N is either 2 or 8 depending on
534 * the number of samples. Experiments indicate that this is not
535 * quite correct; actually, what the hardware appears to do is to
536 * align whatever rectangle is sent down the pipeline to the nearest
537 * multiple of 2x2 blocks, and then scale it up by a factor of N
538 * horizontally and 2 vertically. So the resulting alignment is 4
539 * vertically and either 4 or 16 horizontally, and the scaledown
540 * factor is 2 vertically and either 2 or 8 horizontally.
542 switch (aux_surf
->format
) {
543 case ISL_FORMAT_MCS_2X
:
544 case ISL_FORMAT_MCS_4X
:
547 case ISL_FORMAT_MCS_8X
:
550 case ISL_FORMAT_MCS_16X
:
554 unreachable("Unexpected MCS format for fast clear");
557 x_align
= x_scaledown
* 2;
558 y_align
= y_scaledown
* 2;
561 *x0
= ROUND_DOWN_TO(*x0
, x_align
) / x_scaledown
;
562 *y0
= ROUND_DOWN_TO(*y0
, y_align
) / y_scaledown
;
563 *x1
= ALIGN(*x1
, x_align
) / x_scaledown
;
564 *y1
= ALIGN(*y1
, y_align
) / y_scaledown
;
568 brw_get_ccs_resolve_rect(const struct isl_device
*dev
,
569 const struct isl_surf
*ccs_surf
,
570 unsigned *x0
, unsigned *y0
,
571 unsigned *x1
, unsigned *y1
)
573 unsigned x_scaledown
, y_scaledown
;
575 /* From the Ivy Bridge PRM, Vol2 Part1 11.9 "Render Target Resolve":
577 * A rectangle primitive must be scaled down by the following factors
578 * with respect to render target being resolved.
580 * The scaledown factors in the table that follows are related to the block
581 * size of the CCS format. For IVB and HSW, we divide by two, for BDW we
582 * multiply by 8 and 16. On Sky Lake, we multiply by 8.
584 const struct isl_format_layout
*fmtl
=
585 isl_format_get_layout(ccs_surf
->format
);
586 assert(fmtl
->txc
== ISL_TXC_CCS
);
588 if (ISL_DEV_GEN(dev
) >= 9) {
589 x_scaledown
= fmtl
->bw
* 8;
590 y_scaledown
= fmtl
->bh
* 8;
591 } else if (ISL_DEV_GEN(dev
) >= 8) {
592 x_scaledown
= fmtl
->bw
* 8;
593 y_scaledown
= fmtl
->bh
* 16;
595 x_scaledown
= fmtl
->bw
/ 2;
596 y_scaledown
= fmtl
->bh
/ 2;
599 *x1
= ALIGN(ccs_surf
->logical_level0_px
.width
, x_scaledown
) / x_scaledown
;
600 *y1
= ALIGN(ccs_surf
->logical_level0_px
.height
, y_scaledown
) / y_scaledown
;