u_blitter: add a msaa parameter to util_blitter_clear
[mesa.git] / src / gallium / drivers / radeonsi / si_clear.c
index fa8ae5d2566df999af5fe01475c3cc6ce6dc84be..a83f65c6f1be5cb5cbcca9e834816b82413f7c1c 100644 (file)
@@ -99,18 +99,23 @@ enum pipe_format si_simplify_cb_format(enum pipe_format format)
        return util_format_intensity_to_red(format);
 }
 
-bool vi_alpha_is_on_msb(enum pipe_format format)
+bool vi_alpha_is_on_msb(struct si_screen *sscreen, enum pipe_format format)
 {
        format = si_simplify_cb_format(format);
+       const struct util_format_description *desc = util_format_description(format);
 
        /* Formats with 3 channels can't have alpha. */
-       if (util_format_description(format)->nr_channels == 3)
+       if (desc->nr_channels == 3)
                return true; /* same as xxxA; is any value OK here? */
 
+       if (sscreen->info.chip_class >= GFX10 && desc->nr_channels == 1)
+               return desc->swizzle[3] == PIPE_SWIZZLE_X;
+
        return si_translate_colorswap(format, false) <= 1;
 }
 
-static bool vi_get_fast_clear_parameters(enum pipe_format base_format,
+static bool vi_get_fast_clear_parameters(struct si_screen *sscreen,
+                                        enum pipe_format base_format,
                                         enum pipe_format surface_format,
                                         const union pipe_color_union *color,
                                         uint32_t* clear_value,
@@ -142,8 +147,8 @@ static bool vi_get_fast_clear_parameters(enum pipe_format base_format,
        if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
                return true; /* need ELIMINATE_FAST_CLEAR */
 
-       bool base_alpha_is_on_msb = vi_alpha_is_on_msb(base_format);
-       bool surf_alpha_is_on_msb = vi_alpha_is_on_msb(surface_format);
+       bool base_alpha_is_on_msb = vi_alpha_is_on_msb(sscreen, base_format);
+       bool surf_alpha_is_on_msb = vi_alpha_is_on_msb(sscreen, surface_format);
 
        /* Formats with 3 channels can't have alpha. */
        if (desc->nr_channels == 3)
@@ -226,7 +231,7 @@ static bool vi_get_fast_clear_parameters(enum pipe_format base_format,
        return true;
 }
 
-void vi_dcc_clear_level(struct si_context *sctx,
+bool vi_dcc_clear_level(struct si_context *sctx,
                        struct si_texture *tex,
                        unsigned level, unsigned clear_value)
 {
@@ -245,21 +250,28 @@ void vi_dcc_clear_level(struct si_context *sctx,
 
        if (sctx->chip_class >= GFX9) {
                /* Mipmap level clears aren't implemented. */
-               assert(tex->buffer.b.b.last_level == 0);
+               if (tex->buffer.b.b.last_level > 0)
+                       return false;
+
                /* 4x and 8x MSAA needs a sophisticated compute shader for
                 * the clear. See AMDVLK. */
-               assert(tex->buffer.b.b.nr_storage_samples <= 2);
+               if (tex->buffer.b.b.nr_storage_samples >= 4)
+                       return false;
+
                clear_size = tex->surface.dcc_size;
        } else {
                unsigned num_layers = util_num_layers(&tex->buffer.b.b, level);
 
                /* If this is 0, fast clear isn't possible. (can occur with MSAA) */
-               assert(tex->surface.u.legacy.level[level].dcc_fast_clear_size);
+               if (!tex->surface.u.legacy.level[level].dcc_fast_clear_size)
+                       return false;
+
                /* Layered 4x and 8x MSAA DCC fast clears need to clear
                 * dcc_fast_clear_size bytes for each layer. A compute shader
                 * would be more efficient than separate per-layer clear operations.
                 */
-               assert(tex->buffer.b.b.nr_storage_samples <= 2 || num_layers == 1);
+               if (tex->buffer.b.b.nr_storage_samples >= 4 && num_layers > 1)
+                       return false;
 
                dcc_offset += tex->surface.u.legacy.level[level].dcc_offset;
                clear_size = tex->surface.u.legacy.level[level].dcc_fast_clear_size *
@@ -268,6 +280,7 @@ void vi_dcc_clear_level(struct si_context *sctx,
 
        si_clear_buffer(sctx, dcc_buffer, dcc_offset, clear_size,
                        &clear_value, 4, SI_COHERENCY_CB_META, false);
+       return true;
 }
 
 /* Set the same micro tile mode as the destination of the last MSAA resolve.
@@ -478,12 +491,8 @@ static void si_do_fast_color_clear(struct si_context *sctx,
                        if (sctx->screen->debug_flags & DBG(NO_DCC_CLEAR))
                                continue;
 
-                       /* This can happen with mipmapping or MSAA. */
-                       if (sctx->chip_class == GFX8 &&
-                           !tex->surface.u.legacy.level[level].dcc_fast_clear_size)
-                               continue;
-
-                       if (!vi_get_fast_clear_parameters(tex->buffer.b.b.format,
+                       if (!vi_get_fast_clear_parameters(sctx->screen,
+                                                         tex->buffer.b.b.format,
                                                          fb->cbufs[i]->format,
                                                          color, &reset_value,
                                                          &eliminate_needed))
@@ -492,21 +501,24 @@ static void si_do_fast_color_clear(struct si_context *sctx,
                        if (eliminate_needed && too_small)
                                continue;
 
+                       /* TODO: This DCC+CMASK clear doesn't work with MSAA. */
+                       if (tex->buffer.b.b.nr_samples >= 2 && tex->cmask_buffer &&
+                           eliminate_needed)
+                               continue;
+
+                       if (!vi_dcc_clear_level(sctx, tex, 0, reset_value))
+                               continue;
+
+                       tex->separate_dcc_dirty = true;
+
                        /* DCC fast clear with MSAA should clear CMASK to 0xC. */
                        if (tex->buffer.b.b.nr_samples >= 2 && tex->cmask_buffer) {
-                               /* TODO: This doesn't work with MSAA. */
-                               if (eliminate_needed)
-                                       continue;
-
                                uint32_t clear_value = 0xCCCCCCCC;
                                si_clear_buffer(sctx, &tex->cmask_buffer->b.b,
                                                tex->cmask_offset, tex->surface.cmask_size,
                                                &clear_value, 4, SI_COHERENCY_CB_META, false);
                                fmask_decompress_needed = true;
                        }
-
-                       vi_dcc_clear_level(sctx, tex, 0, reset_value);
-                       tex->separate_dcc_dirty = true;
                } else {
                        if (too_small)
                                continue;
@@ -587,11 +599,11 @@ static void si_clear(struct pipe_context *ctx, unsigned buffers,
        }
 
        if (zstex &&
-           si_htile_enabled(zstex, zsbuf->u.tex.level) &&
            zsbuf->u.tex.first_layer == 0 &&
            zsbuf->u.tex.last_layer == util_max_layer(&zstex->buffer.b.b, 0)) {
                /* TC-compatible HTILE only supports depth clears to 0 or 1. */
                if (buffers & PIPE_CLEAR_DEPTH &&
+                   si_htile_enabled(zstex, zsbuf->u.tex.level, PIPE_MASK_Z) &&
                    (!zstex->tc_compatible_htile ||
                     depth == 0 || depth == 1)) {
                        /* Need to disable EXPCLEAR temporarily if clearing
@@ -612,6 +624,7 @@ static void si_clear(struct pipe_context *ctx, unsigned buffers,
 
                /* TC-compatible HTILE only supports stencil clears to 0. */
                if (buffers & PIPE_CLEAR_STENCIL &&
+                   si_htile_enabled(zstex, zsbuf->u.tex.level, PIPE_MASK_S) &&
                    (!zstex->tc_compatible_htile || stencil == 0)) {
                        stencil &= 0xff;
 
@@ -650,7 +663,8 @@ static void si_clear(struct pipe_context *ctx, unsigned buffers,
        si_blitter_begin(sctx, SI_CLEAR);
        util_blitter_clear(sctx->blitter, fb->width, fb->height,
                           util_framebuffer_get_num_layers(fb),
-                          buffers, color, depth, stencil);
+                          buffers, color, depth, stencil,
+                          sctx->framebuffer.nr_samples > 1);
        si_blitter_end(sctx);
 
        if (sctx->db_depth_clear) {