+
+/**
+ * Creates a new named renderbuffer that wraps the first slice
+ * of an existing miptree.
+ *
+ * Clobbers the current renderbuffer binding (ctx->CurrentRenderbuffer).
+ */
+struct gl_renderbuffer *
+brw_get_rb_for_slice(struct brw_context *brw,
+ struct intel_mipmap_tree *mt,
+ unsigned level, unsigned layer, bool flat)
+{
+ struct gl_context *ctx = &brw->ctx;
+ struct gl_renderbuffer *rb = ctx->Driver.NewRenderbuffer(ctx, 0xDEADBEEF);
+ struct intel_renderbuffer *irb = intel_renderbuffer(rb);
+
+ rb->RefCount = 1;
+ rb->Format = mt->format;
+ rb->_BaseFormat = _mesa_get_format_base_format(mt->format);
+
+ /* Program takes care of msaa and mip-level access manually for stencil.
+ * The surface is also treated as Y-tiled instead of as W-tiled calling for
+ * twice the width and half the height in dimensions.
+ */
+ if (flat) {
+ const unsigned halign_stencil = 8;
+
+ rb->NumSamples = 0;
+ rb->Width = ALIGN(mt->total_width, halign_stencil) * 2;
+ rb->Height = (mt->total_height / mt->physical_depth0) / 2;
+ irb->mt_level = 0;
+ } else {
+ rb->NumSamples = mt->num_samples;
+ rb->Width = mt->logical_width0;
+ rb->Height = mt->logical_height0;
+ irb->mt_level = level;
+ }
+
+ irb->mt_layer = layer;
+
+ intel_miptree_reference(&irb->mt, mt);
+
+ return rb;
+}
+
+/**
+ * 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 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 (brw->gen >= 9 &&
+ brw_format_for_mesa_format(mt->format) !=
+ brw->render_target_format[mt->format])
+ return false;
+
+ /* Gen9 doesn't support fast clear on single-sampled SRGB buffers. When
+ * GL_FRAMEBUFFER_SRGB is enabled any color renderbuffers will be
+ * resolved in intel_update_state. In that case it's pointless to do a
+ * fast clear because it's very likely to be immediately resolved.
+ */
+ if (brw->gen >= 9 &&
+ mt->num_samples <= 1 &&
+ ctx->Color.sRGBEnabled &&
+ _mesa_get_srgb_format_linear(mt->format) != mt->format)
+ return false;
+
+ const mesa_format format = _mesa_get_render_format(ctx, mt->format);
+ if (_mesa_is_format_integer_color(format)) {
+ if (brw->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 (brw->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+).
+ *
+ * Returned boolean tells if the given color differs from the stored.
+ */
+bool
+brw_meta_set_fast_clear_color(struct brw_context *brw,
+ struct intel_mipmap_tree *mt,
+ const union gl_color_union *color)
+{
+ union gl_color_union override_color = *color;
+
+ /* 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.ui[3] = override_color.ui[0];
+ /* flow through */
+ case GL_LUMINANCE:
+ case GL_LUMINANCE_ALPHA:
+ override_color.ui[1] = override_color.ui[0];
+ override_color.ui[2] = override_color.ui[0];
+ break;
+ default:
+ for (int i = 0; i < 3; i++) {
+ if (!_mesa_format_has_color_component(mt->format, i))
+ override_color.ui[i] = 0;
+ }
+ break;
+ }
+
+ if (!_mesa_format_has_color_component(mt->format, 3)) {
+ if (_mesa_is_format_integer_color(mt->format))
+ override_color.ui[3] = 1;
+ else
+ override_color.f[3] = 1.0f;
+ }
+
+ /* Handle linear→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.f[i] =
+ util_format_linear_to_srgb_float(override_color.f[i]);
+ }
+ }
+
+ bool updated;
+ if (brw->gen >= 9) {
+ updated = memcmp(&mt->gen9_fast_clear_color, &override_color,
+ sizeof(mt->gen9_fast_clear_color));
+ mt->gen9_fast_clear_color = override_color;
+ } else {
+ const uint32_t old_color_value = mt->fast_clear_color_value;
+
+ mt->fast_clear_color_value = 0;
+ for (int i = 0; i < 4; i++) {
+ /* Testing for non-0 works for integer and float colors */
+ if (override_color.f[i] != 0.0f) {
+ mt->fast_clear_color_value |=
+ 1 << (GEN7_SURFACE_CLEAR_COLOR_SHIFT + (3 - i));
+ }
+ }
+
+ updated = (old_color_value != mt->fast_clear_color_value);
+ }
+
+ return updated;
+}