radv: initialize levels without DCC during layout transitions
[mesa.git] / src / amd / vulkan / radv_cmd_buffer.c
index a6b1f767d4672147ea0ac6888733489dc5c0e243..f311b978b30a22220010beb5bc78a4e410252c57 100644 (file)
@@ -1255,6 +1255,15 @@ radv_emit_fb_color_state(struct radv_cmd_buffer *cmd_buffer,
                cb_color_info &= C_028C70_DCC_ENABLE;
        }
 
+       if (radv_image_is_tc_compat_cmask(image) &&
+           (radv_is_fmask_decompress_pipeline(cmd_buffer) ||
+            radv_is_dcc_decompress_pipeline(cmd_buffer))) {
+               /* If this bit is set, the FMASK decompression operation
+                * doesn't occur (DCC_COMPRESS also implies FMASK_DECOMPRESS).
+                */
+               cb_color_info &= C_028C70_FMASK_COMPRESS_1FRAG_ONLY;
+       }
+
        if (cmd_buffer->device->physical_device->rad_info.chip_class >= GFX9) {
                radeon_set_context_reg_seq(cmd_buffer->cs, R_028C60_CB_COLOR0_BASE + index * 0x3c, 11);
                radeon_emit(cmd_buffer->cs, cb->cb_color_base);
@@ -1294,7 +1303,7 @@ radv_emit_fb_color_state(struct radv_cmd_buffer *cmd_buffer,
                }
        }
 
-       if (radv_image_has_dcc(image)) {
+       if (radv_dcc_enabled(image, iview->base_mip)) {
                /* Drawing with DCC enabled also compresses colorbuffers. */
                VkImageSubresourceRange range = {
                        .aspectMask = iview->aspect_mask,
@@ -1624,7 +1633,7 @@ radv_update_fce_metadata(struct radv_cmd_buffer *cmd_buffer,
        uint32_t level_count = radv_get_levelCount(image, range);
        uint32_t count = 2 * level_count;
 
-       assert(radv_image_has_dcc(image));
+       assert(radv_dcc_enabled(image, range->baseMipLevel));
 
        radeon_emit(cmd_buffer->cs, PKT3(PKT3_WRITE_DATA, 2 + count, 0));
        radeon_emit(cmd_buffer->cs, S_370_DST_SEL(V_370_MEM) |
@@ -1652,7 +1661,7 @@ radv_update_dcc_metadata(struct radv_cmd_buffer *cmd_buffer,
        uint32_t level_count = radv_get_levelCount(image, range);
        uint32_t count = 2 * level_count;
 
-       assert(radv_image_has_dcc(image));
+       assert(radv_dcc_enabled(image, range->baseMipLevel));
 
        radeon_emit(cmd_buffer->cs, PKT3(PKT3_WRITE_DATA, 2 + count, 0));
        radeon_emit(cmd_buffer->cs, S_370_DST_SEL(V_370_MEM) |
@@ -1714,7 +1723,8 @@ radv_set_color_clear_metadata(struct radv_cmd_buffer *cmd_buffer,
        uint32_t level_count = radv_get_levelCount(image, range);
        uint32_t count = 2 * level_count;
 
-       assert(radv_image_has_cmask(image) || radv_image_has_dcc(image));
+       assert(radv_image_has_cmask(image) ||
+              radv_dcc_enabled(image, range->baseMipLevel));
 
        radeon_emit(cs, PKT3(PKT3_WRITE_DATA, 2 + count, cmd_buffer->state.predicating));
        radeon_emit(cs, S_370_DST_SEL(V_370_MEM) |
@@ -1747,7 +1757,8 @@ radv_update_color_clear_metadata(struct radv_cmd_buffer *cmd_buffer,
                .layerCount = iview->layer_count,
        };
 
-       assert(radv_image_has_cmask(image) || radv_image_has_dcc(image));
+       assert(radv_image_has_cmask(image) ||
+              radv_dcc_enabled(image, iview->base_mip));
 
        radv_set_color_clear_metadata(cmd_buffer, image, &range, color_values);
 
@@ -1760,15 +1771,15 @@ radv_update_color_clear_metadata(struct radv_cmd_buffer *cmd_buffer,
  */
 static void
 radv_load_color_clear_metadata(struct radv_cmd_buffer *cmd_buffer,
-                              struct radv_image *image,
+                              struct radv_image_view *iview,
                               int cb_idx)
 {
        struct radeon_cmdbuf *cs = cmd_buffer->cs;
-       uint64_t va = radv_buffer_get_va(image->bo);
-
-       va += image->offset + image->clear_value_offset;
+       struct radv_image *image = iview->image;
+       uint64_t va = radv_image_get_fast_clear_va(image, iview->base_mip);
 
-       if (!radv_image_has_cmask(image) && !radv_image_has_dcc(image))
+       if (!radv_image_has_cmask(image) &&
+           !radv_dcc_enabled(image, iview->base_mip))
                return;
 
        uint32_t reg = R_028C8C_CB_COLOR0_CLEAR_WORD0 + cb_idx * 0x3c;
@@ -1815,7 +1826,8 @@ radv_emit_framebuffer_state(struct radv_cmd_buffer *cmd_buffer)
 
                int idx = subpass->color_attachments[i].attachment;
                struct radv_attachment_info *att = &framebuffer->attachments[idx];
-               struct radv_image *image = att->attachment->image;
+               struct radv_image_view *iview = att->attachment;
+               struct radv_image *image = iview->image;
                VkImageLayout layout = subpass->color_attachments[i].layout;
 
                radv_cs_add_buffer(cmd_buffer->device->ws, cmd_buffer->cs, att->attachment->bo);
@@ -1824,7 +1836,7 @@ radv_emit_framebuffer_state(struct radv_cmd_buffer *cmd_buffer)
                                                       VK_IMAGE_ASPECT_PLANE_1_BIT | VK_IMAGE_ASPECT_PLANE_2_BIT));
                radv_emit_fb_color_state(cmd_buffer, i, att, iview, layout);
 
-               radv_load_color_clear_metadata(cmd_buffer, image, i);
+               radv_load_color_clear_metadata(cmd_buffer, iview, i);
 
                if (image->planes[0].surface.bpe >= 8)
                        num_bpp64_colorbufs++;
@@ -4909,11 +4921,58 @@ void radv_initialize_dcc(struct radv_cmd_buffer *cmd_buffer,
                         const VkImageSubresourceRange *range, uint32_t value)
 {
        struct radv_cmd_state *state = &cmd_buffer->state;
+       uint32_t level_count = radv_get_levelCount(image, range);
+       unsigned size = 0;
 
        state->flush_bits |= RADV_CMD_FLAG_FLUSH_AND_INV_CB |
                             RADV_CMD_FLAG_FLUSH_AND_INV_CB_META;
 
-       state->flush_bits |= radv_clear_dcc(cmd_buffer, image, range, value);
+       if (cmd_buffer->device->physical_device->rad_info.chip_class >= GFX9) {
+               /* Mipmap level aren't implemented. */
+               assert(level_count == 1);
+               state->flush_bits |= radv_clear_dcc(cmd_buffer, image,
+                                                   range, value);
+       } else {
+               /* Initialize the mipmap levels with DCC first. */
+               for (unsigned l = 0; l < level_count; l++) {
+                       uint32_t level = range->baseMipLevel + l;
+                       struct legacy_surf_level *surf_level =
+                               &image->planes[0].surface.u.legacy.level[level];
+
+                       if (!surf_level->dcc_fast_clear_size)
+                               break;
+
+                       state->flush_bits |=
+                               radv_dcc_clear_level(cmd_buffer, image,
+                                                    level, value);
+               }
+
+               /* When DCC is enabled with mipmaps, some levels might not
+                * support fast clears and we have to initialize them as "fully
+                * expanded".
+                */
+               if (image->planes[0].surface.num_dcc_levels > 1) {
+                       /* Compute the size of all fast clearable DCC levels. */
+                       for (unsigned i = 0; i < image->planes[0].surface.num_dcc_levels; i++) {
+                               struct legacy_surf_level *surf_level =
+                                       &image->planes[0].surface.u.legacy.level[i];
+
+                               if (!surf_level->dcc_fast_clear_size)
+                                       break;
+
+                               size = surf_level->dcc_offset + surf_level->dcc_fast_clear_size;
+                       }
+
+                       /* Initialize the mipmap levels without DCC. */
+                       if (size != image->planes[0].surface.dcc_size) {
+                               state->flush_bits |=
+                                       radv_fill_buffer(cmd_buffer, image->bo,
+                                                        image->offset + image->dcc_offset + size,
+                                                        image->planes[0].surface.dcc_size - size,
+                                                        0xffffffff);
+                       }
+               }
+       }
 
        state->flush_bits |= RADV_CMD_FLAG_FLUSH_AND_INV_CB |
                             RADV_CMD_FLAG_FLUSH_AND_INV_CB_META;
@@ -4945,7 +5004,7 @@ static void radv_init_color_image_metadata(struct radv_cmd_buffer *cmd_buffer,
                radv_initialize_fmask(cmd_buffer, image);
        }
 
-       if (radv_image_has_dcc(image)) {
+       if (radv_dcc_enabled(image, range->baseMipLevel)) {
                uint32_t value = 0xffffffffu; /* Fully expanded mode. */
                bool need_decompress_pass = false;
 
@@ -4961,7 +5020,8 @@ static void radv_init_color_image_metadata(struct radv_cmd_buffer *cmd_buffer,
                                         need_decompress_pass);
        }
 
-       if (radv_image_has_cmask(image) || radv_image_has_dcc(image)) {
+       if (radv_image_has_cmask(image) ||
+           radv_dcc_enabled(image, range->baseMipLevel)) {
                uint32_t color_values[2] = {};
                radv_set_color_clear_metadata(cmd_buffer, image, range,
                                              color_values);
@@ -4987,7 +5047,7 @@ static void radv_handle_color_image_transition(struct radv_cmd_buffer *cmd_buffe
                return;
        }
 
-       if (radv_image_has_dcc(image)) {
+       if (radv_dcc_enabled(image, range->baseMipLevel)) {
                if (src_layout == VK_IMAGE_LAYOUT_PREINITIALIZED) {
                        radv_initialize_dcc(cmd_buffer, image, range, 0xffffffffu);
                } else if (radv_layout_dcc_compressed(image, src_layout, src_queue_mask) &&