radeonsi: clarify the conditions when FLUSH_AND_INV_DB is needed
[mesa.git] / src / gallium / drivers / radeonsi / si_clear.c
index 3f5375337a195f851329a51149a1fb9dbe8d7c8c..2af778b41ad423ac8ff1937652b00bd21abc6242 100644 (file)
@@ -25,7 +25,7 @@
 #include "si_pipe.h"
 #include "sid.h"
 
-#include "util/u_format.h"
+#include "util/format/u_format.h"
 #include "util/u_pack_color.h"
 #include "util/u_surface.h"
 
@@ -34,19 +34,14 @@ enum {
        SI_CLEAR_SURFACE = SI_SAVE_FRAMEBUFFER | SI_SAVE_FRAGMENT_STATE,
 };
 
-enum si_dcc_clear_code
-{
-       DCC_CLEAR_COLOR_0000   = 0x00000000,
-       DCC_CLEAR_COLOR_0001   = 0x40404040,
-       DCC_CLEAR_COLOR_1110   = 0x80808080,
-       DCC_CLEAR_COLOR_1111   = 0xC0C0C0C0,
-       DCC_CLEAR_COLOR_REG    = 0x20202020,
-};
-
 static void si_alloc_separate_cmask(struct si_screen *sscreen,
                                    struct si_texture *tex)
 {
-       if (tex->cmask_buffer || !tex->surface.cmask_size)
+       /* CMASK for MSAA is allocated in advance or always disabled
+        * by "nofmask" option.
+        */
+       if (tex->cmask_buffer || !tex->surface.cmask_size ||
+           tex->buffer.b.b.nr_samples >= 2)
                 return;
 
        tex->cmask_buffer =
@@ -81,12 +76,8 @@ static bool si_set_clear_color(struct si_texture *tex,
                       color->ui[0] == color->ui[2]);
                uc.ui[0] = color->ui[0];
                uc.ui[1] = color->ui[3];
-       } else if (util_format_is_pure_uint(surface_format)) {
-               util_format_write_4ui(surface_format, color->ui, 0, &uc, 0, 0, 0, 1, 1);
-       } else if (util_format_is_pure_sint(surface_format)) {
-               util_format_write_4i(surface_format, color->i, 0, &uc, 0, 0, 0, 1, 1);
        } else {
-               util_pack_color(color->f, surface_format, &uc);
+               util_pack_color_union(surface_format, &uc, color);
        }
 
        if (memcmp(tex->color_clear_value, &uc, 2 * sizeof(uint32_t)) == 0)
@@ -104,18 +95,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,
@@ -147,8 +143,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)
@@ -212,8 +208,8 @@ static bool vi_get_fast_clear_parameters(enum pipe_format base_format,
        }
 
        /* This doesn't need ELIMINATE_FAST_CLEAR.
-        * CB uses both the DCC clear codes and the CB clear color registers,
-        * so they must match.
+        * On chips predating Raven2, the DCC clear codes and the CB clear
+        * color registers must match.
         */
        *eliminate_needed = false;
 
@@ -231,7 +227,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,26 +241,33 @@ void vi_dcc_clear_level(struct si_context *sctx,
                dcc_offset = 0;
        } else {
                dcc_buffer = &tex->buffer.b.b;
-               dcc_offset = tex->dcc_offset;
+               dcc_offset = tex->surface.dcc_offset;
        }
 
        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 *
@@ -272,7 +275,8 @@ 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);
+                       &clear_value, 4, SI_COHERENCY_CB_META, false);
+       return true;
 }
 
 /* Set the same micro tile mode as the destination of the last MSAA resolve.
@@ -282,7 +286,8 @@ void vi_dcc_clear_level(struct si_context *sctx,
 static void si_set_optimal_micro_tile_mode(struct si_screen *sscreen,
                                           struct si_texture *tex)
 {
-       if (tex->buffer.b.is_shared ||
+       if (sscreen->info.chip_class >= GFX10 ||
+           tex->buffer.b.is_shared ||
            tex->buffer.b.b.nr_samples <= 1 ||
            tex->surface.micro_tile_mode == tex->last_msaa_resolve_target_micro_mode)
                return;
@@ -322,7 +327,7 @@ static void si_set_optimal_micro_tile_mode(struct si_screen *sscreen,
                        assert(!"unexpected micro mode");
                        return;
                }
-       } else if (sscreen->info.chip_class >= CIK) {
+       } else if (sscreen->info.chip_class >= GFX7) {
                /* These magic numbers were copied from addrlib. It doesn't use
                 * any definitions for them either. They are all 2D_TILED_THIN1
                 * modes with different bpp and micro tile mode.
@@ -341,7 +346,7 @@ static void si_set_optimal_micro_tile_mode(struct si_screen *sscreen,
                        assert(!"unexpected micro mode");
                        return;
                }
-       } else { /* SI */
+       } else { /* GFX6 */
                switch (tex->last_msaa_resolve_target_micro_mode) {
                case RADEON_MICRO_MODE_DISPLAY:
                        switch (tex->surface.bpe) {
@@ -391,7 +396,7 @@ static void si_do_fast_color_clear(struct si_context *sctx,
        int i;
 
        /* This function is broken in BE, so just disable this path for now */
-#ifdef PIPE_ARCH_BIG_ENDIAN
+#if UTIL_ARCH_BIG_ENDIAN
        return;
 #endif
 
@@ -443,7 +448,7 @@ static void si_do_fast_color_clear(struct si_context *sctx,
                    !(tex->buffer.external_usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH))
                        continue;
 
-               if (sctx->chip_class <= VI &&
+               if (sctx->chip_class <= GFX8 &&
                    tex->surface.u.legacy.level[0].mode == RADEON_SURF_MODE_1D &&
                    !sctx->screen->info.htile_cmask_support_1d_tiling)
                        continue;
@@ -482,12 +487,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 == VI &&
-                           !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))
@@ -496,21 +497,25 @@ 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;
+                       tex->displayable_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);
+                                               tex->surface.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;
@@ -532,8 +537,8 @@ static void si_do_fast_color_clear(struct si_context *sctx,
                        /* Do the fast clear. */
                        uint32_t clear_value = 0;
                        si_clear_buffer(sctx, &tex->cmask_buffer->b.b,
-                                       tex->cmask_offset, tex->surface.cmask_size,
-                                       &clear_value, 4, SI_COHERENCY_CB_META);
+                                       tex->surface.cmask_offset, tex->surface.cmask_size,
+                                       &clear_value, 4, SI_COHERENCY_CB_META, false);
                        eliminate_needed = true;
                }
 
@@ -548,6 +553,12 @@ static void si_do_fast_color_clear(struct si_context *sctx,
 
                *buffers &= ~clear_bit;
 
+               /* Chips with DCC constant encoding don't need to set the clear
+                * color registers for DCC clear values 0 and 1.
+                */
+               if (sctx->screen->info.has_dcc_constant_encode && !eliminate_needed)
+                       continue;
+
                if (si_set_clear_color(tex, fb->cbufs[i]->format, color)) {
                        sctx->framebuffer.dirty_cbufs |= 1 << i;
                        si_mark_atom_dirty(sctx, &sctx->atoms.s.framebuffer);
@@ -564,6 +575,7 @@ static void si_clear(struct pipe_context *ctx, unsigned buffers,
        struct pipe_surface *zsbuf = fb->zsbuf;
        struct si_texture *zstex =
                zsbuf ? (struct si_texture*)zsbuf->texture : NULL;
+       bool needs_db_flush = false;
 
        if (buffers & PIPE_CLEAR_COLOR) {
                si_do_fast_color_clear(sctx, &buffers, color);
@@ -585,11 +597,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
@@ -599,6 +611,11 @@ static void si_clear(struct pipe_context *ctx, unsigned buffers,
                        }
 
                        if (zstex->depth_clear_value != (float)depth) {
+                               if ((zstex->depth_clear_value != 0) != (depth != 0)) {
+                                       /* ZRANGE_PRECISION register of a bound surface will change so we
+                                        * must flush the DB caches. */
+                                       needs_db_flush = true;
+                               }
                                /* Update DB_DEPTH_CLEAR. */
                                zstex->depth_clear_value = depth;
                                sctx->framebuffer.dirty_zsbuf = true;
@@ -610,6 +627,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;
 
@@ -629,26 +647,15 @@ static void si_clear(struct pipe_context *ctx, unsigned buffers,
                        si_mark_atom_dirty(sctx, &sctx->atoms.s.db_render_state);
                }
 
-               /* TODO: Find out what's wrong here. Fast depth clear leads to
-                * corruption in ARK: Survival Evolved, but that may just be
-                * a coincidence and the root cause is elsewhere.
-                *
-                * The corruption can be fixed by putting the DB flush before
-                * or after the depth clear. (surprisingly)
-                *
-                * https://bugs.freedesktop.org/show_bug.cgi?id=102955 (apitrace)
-                *
-                * This hack decreases back-to-back ClearDepth performance.
-                */
-               if ((sctx->db_depth_clear || sctx->db_stencil_clear) &&
-                   sctx->screen->clear_db_cache_before_clear)
+               if (needs_db_flush)
                        sctx->flags |= SI_CONTEXT_FLUSH_AND_INV_DB;
        }
 
        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) {
@@ -674,6 +681,13 @@ static void si_clear_render_target(struct pipe_context *ctx,
                                   bool render_condition_enabled)
 {
        struct si_context *sctx = (struct si_context *)ctx;
+       struct si_texture *sdst = (struct si_texture*)dst->texture;
+
+       if (dst->texture->nr_samples <= 1 && !sdst->surface.dcc_offset) {
+               si_compute_clear_render_target(ctx, dst, color, dstx, dsty, width,
+                                              height, render_condition_enabled);
+               return;
+       }
 
        si_blitter_begin(sctx, SI_CLEAR_SURFACE |
                         (render_condition_enabled ? 0 : SI_DISABLE_RENDER_COND));
@@ -710,8 +724,6 @@ static void si_clear_texture(struct pipe_context *pipe,
        struct si_texture *stex = (struct si_texture*)tex;
        struct pipe_surface tmpl = {{0}};
        struct pipe_surface *sf;
-       const struct util_format_description *desc =
-               util_format_description(tex->format);
 
        tmpl.format = tex->format;
        tmpl.u.tex.first_layer = box->z;
@@ -728,11 +740,12 @@ static void si_clear_texture(struct pipe_context *pipe,
 
                /* Depth is always present. */
                clear = PIPE_CLEAR_DEPTH;
-               desc->unpack_z_float(&depth, 0, data, 0, 1, 1);
+               util_format_unpack_z_float(tex->format, &depth, data, 1);
 
                if (stex->surface.has_stencil) {
                        clear |= PIPE_CLEAR_STENCIL;
-                       desc->unpack_s_8uint(&stencil, 0, data, 0, 1, 1);
+                       util_format_unpack_s_8uint(tex->format,
+                                                  &stencil, data, 1);
                }
 
                si_clear_depth_stencil(pipe, sf, clear, depth, stencil,
@@ -741,13 +754,7 @@ static void si_clear_texture(struct pipe_context *pipe,
        } else {
                union pipe_color_union color;
 
-               /* pipe_color_union requires the full vec4 representation. */
-               if (util_format_is_pure_uint(tex->format))
-                       desc->unpack_rgba_uint(color.ui, 0, data, 0, 1, 1);
-               else if (util_format_is_pure_sint(tex->format))
-                       desc->unpack_rgba_sint(color.i, 0, data, 0, 1, 1);
-               else
-                       desc->unpack_rgba_float(color.f, 0, data, 0, 1, 1);
+               util_format_unpack_rgba(tex->format, color.ui, data, 1);
 
                if (screen->is_format_supported(screen, tex->format,
                                                tex->target, 0, 0,
@@ -767,8 +774,11 @@ static void si_clear_texture(struct pipe_context *pipe,
 
 void si_init_clear_functions(struct si_context *sctx)
 {
-       sctx->b.clear = si_clear;
        sctx->b.clear_render_target = si_clear_render_target;
-       sctx->b.clear_depth_stencil = si_clear_depth_stencil;
        sctx->b.clear_texture = si_clear_texture;
+
+       if (sctx->has_graphics) {
+               sctx->b.clear = si_clear;
+               sctx->b.clear_depth_stencil = si_clear_depth_stencil;
+       }
 }