• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2014 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 (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
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
21  * IN THE SOFTWARE.
22  */
23 
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"
32 
33 /**
34  * Helper function for handling mirror image blits.
35  *
36  * If coord0 > coord1, swap them and invert the "mirror" boolean.
37  */
38 static inline void
fixup_mirroring(bool * mirror,float * coord0,float * coord1)39 fixup_mirroring(bool *mirror, float *coord0, float *coord1)
40 {
41    if (*coord0 > *coord1) {
42       *mirror = !*mirror;
43       float tmp = *coord0;
44       *coord0 = *coord1;
45       *coord1 = tmp;
46    }
47 }
48 
49 /**
50  * Compute the number of pixels to clip for each side of a rect
51  *
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
64  *
65  * \return false if we clip everything away, true otherwise
66  */
67 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)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)
71 {
72    /* If we are going to clip everything away, stop. */
73    if (!(min_x <= max_x &&
74          min_y <= max_y &&
75          x0 <= max_x &&
76          y0 <= max_y &&
77          min_x <= x1 &&
78          min_y <= y1 &&
79          x0 <= x1 &&
80          y0 <= y1)) {
81       return false;
82    }
83 
84    if (x0 < min_x)
85       *clipped_x0 = min_x - x0;
86    else
87       *clipped_x0 = 0;
88    if (max_x < x1)
89       *clipped_x1 = x1 - max_x;
90    else
91       *clipped_x1 = 0;
92 
93    if (y0 < min_y)
94       *clipped_y0 = min_y - y0;
95    else
96       *clipped_y0 = 0;
97    if (max_y < y1)
98       *clipped_y1 = y1 - max_y;
99    else
100       *clipped_y1 = 0;
101 
102    return true;
103 }
104 
105 /**
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.
109  *
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
119  *        of the rect.
120  */
121 static inline void
clip_coordinates(bool mirror,float * src,float * dst0,float * dst1,float clipped_src0,float clipped_dst0,float clipped_dst1,float scale,bool isLeftOrBottom)122 clip_coordinates(bool mirror,
123                  float *src, float *dst0, float *dst1,
124                  float clipped_src0,
125                  float clipped_dst0,
126                  float clipped_dst1,
127                  float scale,
128                  bool isLeftOrBottom)
129 {
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
134     * subtract.
135     */
136    int mult = isLeftOrBottom ? 1 : -1;
137 
138    if (!mirror) {
139       if (clipped_src0 >= clipped_dst0 * scale) {
140          *src += clipped_src0 * mult;
141          *dst0 += clipped_src0 / scale * mult;
142       } else {
143          *dst0 += clipped_dst0 * mult;
144          *src += clipped_dst0 * scale * mult;
145       }
146    } else {
147       if (clipped_src0 >= clipped_dst1 * scale) {
148          *src += clipped_src0 * mult;
149          *dst1 -= clipped_src0 / scale * mult;
150       } else {
151          *dst1 -= clipped_dst1 * mult;
152          *src += clipped_dst1 * scale * mult;
153       }
154    }
155 }
156 
157 bool
brw_meta_mirror_clip_and_scissor(const struct gl_context * ctx,const struct gl_framebuffer * read_fb,const struct gl_framebuffer * draw_fb,GLfloat * srcX0,GLfloat * srcY0,GLfloat * srcX1,GLfloat * srcY1,GLfloat * dstX0,GLfloat * dstY0,GLfloat * dstX1,GLfloat * dstY1,bool * mirror_x,bool * mirror_y)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)
166 {
167    *mirror_x = false;
168    *mirror_y = false;
169 
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);
175 
176    /* Compute number of pixels to clip for each side of both rects. Return
177     * early if we are going to clip everything away.
178     */
179    float clip_src_x0;
180    float clip_src_x1;
181    float clip_src_y0;
182    float clip_src_y1;
183    float clip_dst_x0;
184    float clip_dst_x1;
185    float clip_dst_y0;
186    float clip_dst_y1;
187 
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))
191       return true;
192 
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))
196       return true;
197 
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.
202     *
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.
212     *
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.
221     */
222 
223    float scaleX = (float) (*srcX1 - *srcX0) / (*dstX1 - *dstX0);
224    float scaleY = (float) (*srcY1 - *srcY0) / (*dstY1 - *dstY0);
225 
226    /* Clip left side */
227    clip_coordinates(*mirror_x,
228                     srcX0, dstX0, dstX1,
229                     clip_src_x0, clip_dst_x0, clip_dst_x1,
230                     scaleX, true);
231 
232    /* Clip right side */
233    clip_coordinates(*mirror_x,
234                     srcX1, dstX1, dstX0,
235                     clip_src_x1, clip_dst_x1, clip_dst_x0,
236                     scaleX, false);
237 
238    /* Clip bottom side */
239    clip_coordinates(*mirror_y,
240                     srcY0, dstY0, dstY1,
241                     clip_src_y0, clip_dst_y0, clip_dst_y1,
242                     scaleY, true);
243 
244    /* Clip top side */
245    clip_coordinates(*mirror_y,
246                     srcY1, dstY1, dstY0,
247                     clip_src_y1, clip_dst_y1, clip_dst_y0,
248                     scaleY, false);
249 
250    /* Account for the fact that in the system framebuffer, the origin is at
251     * the lower left.
252     */
253    if (_mesa_is_winsys_fbo(read_fb)) {
254       GLint tmp = read_fb->Height - *srcY0;
255       *srcY0 = read_fb->Height - *srcY1;
256       *srcY1 = tmp;
257       *mirror_y = !*mirror_y;
258    }
259    if (_mesa_is_winsys_fbo(draw_fb)) {
260       GLint tmp = draw_fb->Height - *dstY0;
261       *dstY0 = draw_fb->Height - *dstY1;
262       *dstY1 = tmp;
263       *mirror_y = !*mirror_y;
264    }
265 
266    return false;
267 }
268 
269 /**
270  * Determine if fast color clear supports the given clear color.
271  *
272  * Fast color clear can only clear to color values of 1.0 or 0.0.  At the
273  * moment we only support floating point, unorm, and snorm buffers.
274  */
275 bool
brw_is_color_fast_clear_compatible(struct brw_context * brw,const struct intel_mipmap_tree * mt,const union gl_color_union * color)276 brw_is_color_fast_clear_compatible(struct brw_context *brw,
277                                    const struct intel_mipmap_tree *mt,
278                                    const union gl_color_union *color)
279 {
280    const struct gen_device_info *devinfo = &brw->screen->devinfo;
281    const struct gl_context *ctx = &brw->ctx;
282 
283    /* If we're mapping the render format to a different format than the
284     * format we use for texturing then it is a bit questionable whether it
285     * should be possible to use a fast clear. Although we only actually
286     * render using a renderable format, without the override workaround it
287     * wouldn't be possible to have a non-renderable surface in a fast clear
288     * state so the hardware probably legitimately doesn't need to support
289     * this case. At least on Gen9 this really does seem to cause problems.
290     */
291    if (devinfo->gen >= 9 &&
292        brw_isl_format_for_mesa_format(mt->format) !=
293        brw->mesa_to_isl_render_format[mt->format])
294       return false;
295 
296    const bool srgb_rb = _mesa_get_srgb_format_linear(mt->format) != mt->format;
297   /* Gen10 doesn't automatically decode the clear color of sRGB buffers. Since
298    * we currently don't perform this decode in software, avoid a fast-clear
299    * altogether. TODO: Do this in software.
300    */
301    const mesa_format format = _mesa_get_render_format(ctx, mt->format);
302    if (devinfo->gen >= 10 && srgb_rb) {
303       perf_debug("sRGB fast clear not enabled for (%s)",
304                  _mesa_get_format_name(format));
305       return false;
306    }
307 
308    if (_mesa_is_format_integer_color(format)) {
309       if (devinfo->gen >= 8) {
310          perf_debug("Integer fast clear not enabled for (%s)",
311                     _mesa_get_format_name(format));
312       }
313       return false;
314    }
315 
316    for (int i = 0; i < 4; i++) {
317       if (!_mesa_format_has_color_component(format, i)) {
318          continue;
319       }
320 
321       if (devinfo->gen < 9 &&
322           color->f[i] != 0.0f && color->f[i] != 1.0f) {
323          return false;
324       }
325    }
326    return true;
327 }
328 
329 /**
330  * Convert the given color to a bitfield suitable for ORing into DWORD 7 of
331  * SURFACE_STATE (DWORD 12-15 on SKL+).
332  */
333 union isl_color_value
brw_meta_convert_fast_clear_color(const struct brw_context * brw,const struct intel_mipmap_tree * mt,const union gl_color_union * color)334 brw_meta_convert_fast_clear_color(const struct brw_context *brw,
335                                   const struct intel_mipmap_tree *mt,
336                                   const union gl_color_union *color)
337 {
338    union isl_color_value override_color = {
339       .u32 = {
340          color->ui[0],
341          color->ui[1],
342          color->ui[2],
343          color->ui[3],
344       },
345    };
346 
347    /* The sampler doesn't look at the format of the surface when the fast
348     * clear color is used so we need to implement luminance, intensity and
349     * missing components manually.
350     */
351    switch (_mesa_get_format_base_format(mt->format)) {
352    case GL_INTENSITY:
353       override_color.u32[3] = override_color.u32[0];
354       /* flow through */
355    case GL_LUMINANCE:
356    case GL_LUMINANCE_ALPHA:
357       override_color.u32[1] = override_color.u32[0];
358       override_color.u32[2] = override_color.u32[0];
359       break;
360    default:
361       for (int i = 0; i < 3; i++) {
362          if (!_mesa_format_has_color_component(mt->format, i))
363             override_color.u32[i] = 0;
364       }
365       break;
366    }
367 
368    switch (_mesa_get_format_datatype(mt->format)) {
369    case GL_UNSIGNED_NORMALIZED:
370       for (int i = 0; i < 4; i++)
371          override_color.f32[i] = CLAMP(override_color.f32[i], 0.0f, 1.0f);
372       break;
373 
374    case GL_SIGNED_NORMALIZED:
375       for (int i = 0; i < 4; i++)
376          override_color.f32[i] = CLAMP(override_color.f32[i], -1.0f, 1.0f);
377       break;
378 
379    case GL_UNSIGNED_INT:
380       for (int i = 0; i < 4; i++) {
381          unsigned bits = _mesa_get_format_bits(mt->format, GL_RED_BITS + i);
382          if (bits < 32) {
383             uint32_t max = (1u << bits) - 1;
384             override_color.u32[i] = MIN2(override_color.u32[i], max);
385          }
386       }
387       break;
388 
389    case GL_INT:
390       for (int i = 0; i < 4; i++) {
391          unsigned bits = _mesa_get_format_bits(mt->format, GL_RED_BITS + i);
392          if (bits < 32) {
393             int32_t max = (1 << (bits - 1)) - 1;
394             int32_t min = -(1 << (bits - 1));
395             override_color.i32[i] = CLAMP(override_color.i32[i], min, max);
396          }
397       }
398       break;
399 
400    case GL_FLOAT:
401       if (!_mesa_is_format_signed(mt->format)) {
402          for (int i = 0; i < 4; i++)
403             override_color.f32[i] = MAX2(override_color.f32[i], 0.0f);
404       }
405       break;
406    }
407 
408    if (!_mesa_format_has_color_component(mt->format, 3)) {
409       if (_mesa_is_format_integer_color(mt->format))
410          override_color.u32[3] = 1;
411       else
412          override_color.f32[3] = 1.0f;
413    }
414 
415    /* Handle linear to SRGB conversion */
416    if (brw->ctx.Color.sRGBEnabled &&
417        _mesa_get_srgb_format_linear(mt->format) != mt->format) {
418       for (int i = 0; i < 3; i++) {
419          override_color.f32[i] =
420             util_format_linear_to_srgb_float(override_color.f32[i]);
421       }
422    }
423 
424    return override_color;
425 }
426