+ /* Check for invalid bounds
+ * Can't blit for 0-dimensions
+ */
+ return *srcX0 == *srcX1 || *srcY0 == *srcY1
+ || *dstX0 == *dstX1 || *dstY0 == *dstY1;
+}
+
+/**
+ * Determine if fast color clear supports the given clear color.
+ *
+ * Fast color clear can only clear to color values of 1.0 or 0.0. At the
+ * moment we only support floating point, unorm, and snorm buffers.
+ */
+bool
+brw_is_color_fast_clear_compatible(struct brw_context *brw,
+ const struct intel_mipmap_tree *mt,
+ const union gl_color_union *color)
+{
+ const struct gen_device_info *devinfo = &brw->screen->devinfo;
+ const struct gl_context *ctx = &brw->ctx;
+
+ /* If we're mapping the render format to a different format than the
+ * format we use for texturing then it is a bit questionable whether it
+ * should be possible to use a fast clear. Although we only actually
+ * render using a renderable format, without the override workaround it
+ * wouldn't be possible to have a non-renderable surface in a fast clear
+ * state so the hardware probably legitimately doesn't need to support
+ * this case. At least on Gen9 this really does seem to cause problems.
+ */
+ if (devinfo->gen >= 9 &&
+ brw_isl_format_for_mesa_format(mt->format) !=
+ brw->mesa_to_isl_render_format[mt->format])
+ return false;
+
+ const mesa_format format = _mesa_get_render_format(ctx, mt->format);
+ if (_mesa_is_format_integer_color(format)) {
+ if (devinfo->gen >= 8) {
+ perf_debug("Integer fast clear not enabled for (%s)",
+ _mesa_get_format_name(format));
+ }
+ return false;
+ }
+
+ for (int i = 0; i < 4; i++) {
+ if (!_mesa_format_has_color_component(format, i)) {
+ continue;
+ }
+
+ if (devinfo->gen < 9 &&
+ color->f[i] != 0.0f && color->f[i] != 1.0f) {
+ return false;
+ }
+ }
+ return true;
+}
+
+/**
+ * Convert the given color to a bitfield suitable for ORing into DWORD 7 of
+ * SURFACE_STATE (DWORD 12-15 on SKL+).
+ */
+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)
+{
+ union isl_color_value override_color = {
+ .u32 = {
+ color->ui[0],
+ color->ui[1],
+ color->ui[2],
+ color->ui[3],
+ },
+ };
+
+ /* The sampler doesn't look at the format of the surface when the fast
+ * clear color is used so we need to implement luminance, intensity and
+ * missing components manually.
+ */
+ switch (_mesa_get_format_base_format(mt->format)) {
+ case GL_INTENSITY:
+ override_color.u32[3] = override_color.u32[0];
+ /* flow through */
+ case GL_LUMINANCE:
+ case GL_LUMINANCE_ALPHA:
+ override_color.u32[1] = override_color.u32[0];
+ override_color.u32[2] = override_color.u32[0];
+ break;
+ default:
+ for (int i = 0; i < 3; i++) {
+ if (!_mesa_format_has_color_component(mt->format, i))
+ override_color.u32[i] = 0;
+ }
+ break;
+ }
+
+ switch (_mesa_get_format_datatype(mt->format)) {
+ case GL_UNSIGNED_NORMALIZED:
+ for (int i = 0; i < 4; i++)
+ override_color.f32[i] = CLAMP(override_color.f32[i], 0.0f, 1.0f);
+ break;
+
+ case GL_SIGNED_NORMALIZED:
+ for (int i = 0; i < 4; i++)
+ override_color.f32[i] = CLAMP(override_color.f32[i], -1.0f, 1.0f);
+ break;
+
+ case GL_UNSIGNED_INT:
+ for (int i = 0; i < 4; i++) {
+ unsigned bits = _mesa_get_format_bits(mt->format, GL_RED_BITS + i);
+ if (bits < 32) {
+ uint32_t max = (1u << bits) - 1;
+ override_color.u32[i] = MIN2(override_color.u32[i], max);
+ }
+ }
+ break;
+
+ case GL_INT:
+ for (int i = 0; i < 4; i++) {
+ unsigned bits = _mesa_get_format_bits(mt->format, GL_RED_BITS + i);
+ if (bits < 32) {
+ int32_t max = (1 << (bits - 1)) - 1;
+ int32_t min = -(1 << (bits - 1));
+ override_color.i32[i] = CLAMP(override_color.i32[i], min, max);
+ }
+ }
+ break;
+
+ case GL_FLOAT:
+ if (!_mesa_is_format_signed(mt->format)) {
+ for (int i = 0; i < 4; i++)
+ override_color.f32[i] = MAX2(override_color.f32[i], 0.0f);
+ }
+ break;
+ }
+
+ if (!_mesa_format_has_color_component(mt->format, 3)) {
+ if (_mesa_is_format_integer_color(mt->format))
+ override_color.u32[3] = 1;
+ else
+ override_color.f32[3] = 1.0f;
+ }
+
+ /* Handle linear to SRGB conversion */
+ if (brw->ctx.Color.sRGBEnabled &&
+ _mesa_get_srgb_format_linear(mt->format) != mt->format) {
+ for (int i = 0; i < 3; i++) {
+ override_color.f32[i] =
+ util_format_linear_to_srgb_float(override_color.f32[i]);
+ }
+ }
+
+ return override_color;