gallium/radeon: eliminate fast color clear before sharing
[mesa.git] / src / gallium / drivers / radeonsi / si_blit.c
index 6c7b383a4a377d321b3e4f4146aa859cd45f64ca..53c67055a5ec78646509f9e18bd42456d2b7a27d 100644 (file)
@@ -29,20 +29,23 @@ enum si_blitter_op /* bitmask */
 {
        SI_SAVE_TEXTURES      = 1,
        SI_SAVE_FRAMEBUFFER   = 2,
-       SI_DISABLE_RENDER_COND = 4,
+       SI_SAVE_FRAGMENT_STATE = 4,
+       SI_DISABLE_RENDER_COND = 8,
 
-       SI_CLEAR         = 0,
+       SI_CLEAR         = SI_SAVE_FRAGMENT_STATE,
 
-       SI_CLEAR_SURFACE = SI_SAVE_FRAMEBUFFER,
+       SI_CLEAR_SURFACE = SI_SAVE_FRAMEBUFFER | SI_SAVE_FRAGMENT_STATE,
 
        SI_COPY          = SI_SAVE_FRAMEBUFFER | SI_SAVE_TEXTURES |
-                          SI_DISABLE_RENDER_COND,
+                          SI_SAVE_FRAGMENT_STATE | SI_DISABLE_RENDER_COND,
 
-       SI_BLIT          = SI_SAVE_FRAMEBUFFER | SI_SAVE_TEXTURES,
+       SI_BLIT          = SI_SAVE_FRAMEBUFFER | SI_SAVE_TEXTURES |
+                          SI_SAVE_FRAGMENT_STATE,
 
-       SI_DECOMPRESS    = SI_SAVE_FRAMEBUFFER | SI_DISABLE_RENDER_COND,
+       SI_DECOMPRESS    = SI_SAVE_FRAMEBUFFER | SI_SAVE_FRAGMENT_STATE |
+                          SI_DISABLE_RENDER_COND,
 
-       SI_COLOR_RESOLVE = SI_SAVE_FRAMEBUFFER
+       SI_COLOR_RESOLVE = SI_SAVE_FRAMEBUFFER | SI_SAVE_FRAGMENT_STATE
 };
 
 static void si_blitter_begin(struct pipe_context *ctx, enum si_blitter_op op)
@@ -51,27 +54,25 @@ static void si_blitter_begin(struct pipe_context *ctx, enum si_blitter_op op)
 
        r600_suspend_nontimer_queries(&sctx->b);
 
-       util_blitter_save_blend(sctx->blitter, sctx->queued.named.blend);
-       util_blitter_save_depth_stencil_alpha(sctx->blitter, sctx->queued.named.dsa);
-       util_blitter_save_stencil_ref(sctx->blitter, &sctx->stencil_ref);
-       util_blitter_save_rasterizer(sctx->blitter, sctx->queued.named.rasterizer);
-       util_blitter_save_fragment_shader(sctx->blitter, sctx->ps_shader);
-       util_blitter_save_geometry_shader(sctx->blitter, sctx->gs_shader);
-       util_blitter_save_vertex_shader(sctx->blitter, sctx->vs_shader);
-       util_blitter_save_vertex_elements(sctx->blitter, sctx->vertex_elements);
-       if (sctx->queued.named.sample_mask) {
-               util_blitter_save_sample_mask(sctx->blitter,
-                                             sctx->queued.named.sample_mask->sample_mask);
-       }
-       if (sctx->queued.named.viewport[0]) {
-               util_blitter_save_viewport(sctx->blitter, &sctx->queued.named.viewport[0]->viewport);
-       }
-       if (sctx->queued.named.scissor[0]) {
-               util_blitter_save_scissor(sctx->blitter, &sctx->queued.named.scissor[0]->scissor);
-       }
        util_blitter_save_vertex_buffer_slot(sctx->blitter, sctx->vertex_buffer);
+       util_blitter_save_vertex_elements(sctx->blitter, sctx->vertex_elements);
+       util_blitter_save_vertex_shader(sctx->blitter, sctx->vs_shader.cso);
+       util_blitter_save_tessctrl_shader(sctx->blitter, sctx->tcs_shader.cso);
+       util_blitter_save_tesseval_shader(sctx->blitter, sctx->tes_shader.cso);
+       util_blitter_save_geometry_shader(sctx->blitter, sctx->gs_shader.cso);
        util_blitter_save_so_targets(sctx->blitter, sctx->b.streamout.num_targets,
                                     (struct pipe_stream_output_target**)sctx->b.streamout.targets);
+       util_blitter_save_rasterizer(sctx->blitter, sctx->queued.named.rasterizer);
+
+       if (op & SI_SAVE_FRAGMENT_STATE) {
+               util_blitter_save_blend(sctx->blitter, sctx->queued.named.blend);
+               util_blitter_save_depth_stencil_alpha(sctx->blitter, sctx->queued.named.dsa);
+               util_blitter_save_stencil_ref(sctx->blitter, &sctx->stencil_ref.state);
+               util_blitter_save_fragment_shader(sctx->blitter, sctx->ps_shader.cso);
+               util_blitter_save_sample_mask(sctx->blitter, sctx->sample_mask.sample_mask);
+               util_blitter_save_viewport(sctx->blitter, &sctx->viewports.states[0]);
+               util_blitter_save_scissor(sctx->blitter, &sctx->scissors.states[0]);
+       }
 
        if (op & SI_SAVE_FRAMEBUFFER)
                util_blitter_save_framebuffer(sctx->blitter, &sctx->framebuffer.state);
@@ -79,23 +80,21 @@ static void si_blitter_begin(struct pipe_context *ctx, enum si_blitter_op op)
        if (op & SI_SAVE_TEXTURES) {
                util_blitter_save_fragment_sampler_states(
                        sctx->blitter, 2,
-                       sctx->samplers[PIPE_SHADER_FRAGMENT].states.saved_states);
+                       sctx->samplers[PIPE_SHADER_FRAGMENT].views.sampler_states);
 
                util_blitter_save_fragment_sampler_views(sctx->blitter, 2,
                        sctx->samplers[PIPE_SHADER_FRAGMENT].views.views);
        }
 
-       if ((op & SI_DISABLE_RENDER_COND) && sctx->b.current_render_cond) {
-               util_blitter_save_render_condition(sctx->blitter,
-                                                   sctx->b.current_render_cond,
-                                                   sctx->b.current_render_cond_cond,
-                                                   sctx->b.current_render_cond_mode);
-       }
+       if (op & SI_DISABLE_RENDER_COND)
+               sctx->b.render_cond_force_off = true;
 }
 
 static void si_blitter_end(struct pipe_context *ctx)
 {
        struct si_context *sctx = (struct si_context *)ctx;
+
+       sctx->b.render_cond_force_off = false;
        r600_resume_nontimer_queries(&sctx->b);
 }
 
@@ -146,7 +145,7 @@ static void si_blit_decompress_depth(struct pipe_context *ctx,
                                struct pipe_surface *zsurf, *cbsurf, surf_tmpl;
 
                                sctx->dbcb_copy_sample = sample;
-                               sctx->db_render_state.dirty = true;
+                               si_mark_atom_dirty(sctx, &sctx->db_render_state);
 
                                surf_tmpl.format = texture->resource.b.b.format;
                                surf_tmpl.u.tex.level = level;
@@ -180,24 +179,32 @@ static void si_blit_decompress_depth(struct pipe_context *ctx,
 
        sctx->dbcb_depth_copy_enabled = false;
        sctx->dbcb_stencil_copy_enabled = false;
-       sctx->db_render_state.dirty = true;
+       si_mark_atom_dirty(sctx, &sctx->db_render_state);
 }
 
 static void si_blit_decompress_depth_in_place(struct si_context *sctx,
                                               struct r600_texture *texture,
+                                             bool is_stencil_sampler,
                                               unsigned first_level, unsigned last_level,
                                               unsigned first_layer, unsigned last_layer)
 {
        struct pipe_surface *zsurf, surf_tmpl = {{0}};
        unsigned layer, max_layer, checked_last_layer, level;
-
-       sctx->db_inplace_flush_enabled = true;
-       sctx->db_render_state.dirty = true;
+       unsigned *dirty_level_mask;
+
+       if (is_stencil_sampler) {
+               sctx->db_flush_stencil_inplace = true;
+               dirty_level_mask = &texture->stencil_dirty_level_mask;
+       } else {
+               sctx->db_flush_depth_inplace = true;
+               dirty_level_mask = &texture->dirty_level_mask;
+       }
+       si_mark_atom_dirty(sctx, &sctx->db_render_state);
 
        surf_tmpl.format = texture->resource.b.b.format;
 
        for (level = first_level; level <= last_level; level++) {
-               if (!(texture->dirty_level_mask & (1 << level)))
+               if (!(*dirty_level_mask & (1 << level)))
                        continue;
 
                surf_tmpl.u.tex.level = level;
@@ -225,12 +232,13 @@ static void si_blit_decompress_depth_in_place(struct si_context *sctx,
                /* The texture will always be dirty if some layers aren't flushed.
                 * I don't think this case occurs often though. */
                if (first_layer == 0 && last_layer == max_layer) {
-                       texture->dirty_level_mask &= ~(1 << level);
+                       *dirty_level_mask &= ~(1 << level);
                }
        }
 
-       sctx->db_inplace_flush_enabled = false;
-       sctx->db_render_state.dirty = true;
+       sctx->db_flush_depth_inplace = false;
+       sctx->db_flush_stencil_inplace = false;
+       si_mark_atom_dirty(sctx, &sctx->db_render_state);
 }
 
 void si_flush_depth_textures(struct si_context *sctx,
@@ -241,17 +249,20 @@ void si_flush_depth_textures(struct si_context *sctx,
 
        while (mask) {
                struct pipe_sampler_view *view;
+               struct si_sampler_view *sview;
                struct r600_texture *tex;
 
                i = u_bit_scan(&mask);
 
                view = textures->views.views[i];
                assert(view);
+               sview = (struct si_sampler_view*)view;
 
                tex = (struct r600_texture *)view->texture;
                assert(tex->is_depth && !tex->is_flushing_texture);
 
                si_blit_decompress_depth_in_place(sctx, tex,
+                                                 sview->is_stencil_sampler,
                                                  view->u.tex.first_level, view->u.tex.last_level,
                                                  0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));
        }
@@ -319,7 +330,7 @@ void si_decompress_color_textures(struct si_context *sctx,
                assert(view);
 
                tex = (struct r600_texture *)view->texture;
-               assert(tex->cmask.size || tex->fmask.size);
+               assert(tex->cmask.size || tex->fmask.size || tex->dcc_buffer);
 
                si_blit_decompress_color(&sctx->b.b, tex,
                                         view->u.tex.first_level, view->u.tex.last_level,
@@ -338,8 +349,12 @@ static void si_clear(struct pipe_context *ctx, unsigned buffers,
                zsbuf ? (struct r600_texture*)zsbuf->texture : NULL;
 
        if (buffers & PIPE_CLEAR_COLOR) {
-               evergreen_do_fast_color_clear(&sctx->b, fb, &sctx->framebuffer.atom,
-                                             &buffers, color);
+               evergreen_do_fast_color_clear(&sctx->b, fb,
+                                             &sctx->framebuffer.atom, &buffers,
+                                             &sctx->framebuffer.dirty_cbufs,
+                                             color);
+               if (!buffers)
+                       return; /* all buffers have been fast cleared */
        }
 
        if (buffers & PIPE_CLEAR_COLOR) {
@@ -362,21 +377,39 @@ static void si_clear(struct pipe_context *ctx, unsigned buffers,
                }
        }
 
-       if (buffers & PIPE_CLEAR_DEPTH &&
-           zstex && zstex->htile_buffer &&
+       if (zstex && zstex->htile_buffer &&
            zsbuf->u.tex.level == 0 &&
            zsbuf->u.tex.first_layer == 0 &&
            zsbuf->u.tex.last_layer == util_max_layer(&zstex->resource.b.b, 0)) {
-               /* Need to disable EXPCLEAR temporarily if clearing
-                * to a new value. */
-               if (zstex->depth_cleared && zstex->depth_clear_value != depth) {
-                       sctx->db_depth_disable_expclear = true;
+               if (buffers & PIPE_CLEAR_DEPTH) {
+                       /* Need to disable EXPCLEAR temporarily if clearing
+                        * to a new value. */
+                       if (zstex->depth_cleared && zstex->depth_clear_value != depth) {
+                               sctx->db_depth_disable_expclear = true;
+                       }
+
+                       zstex->depth_clear_value = depth;
+                       sctx->framebuffer.dirty_zsbuf = true;
+                       si_mark_atom_dirty(sctx, &sctx->framebuffer.atom); /* updates DB_DEPTH_CLEAR */
+                       sctx->db_depth_clear = true;
+                       si_mark_atom_dirty(sctx, &sctx->db_render_state);
                }
 
-               zstex->depth_clear_value = depth;
-               sctx->framebuffer.atom.dirty = true; /* updates DB_DEPTH_CLEAR */
-               sctx->db_depth_clear = true;
-               sctx->db_render_state.dirty = true;
+               if (buffers & PIPE_CLEAR_STENCIL) {
+                       stencil &= 0xff;
+
+                       /* Need to disable EXPCLEAR temporarily if clearing
+                        * to a new value. */
+                       if (zstex->stencil_cleared && zstex->stencil_clear_value != stencil) {
+                               sctx->db_stencil_disable_expclear = true;
+                       }
+
+                       zstex->stencil_clear_value = stencil;
+                       sctx->framebuffer.dirty_zsbuf = true;
+                       si_mark_atom_dirty(sctx, &sctx->framebuffer.atom); /* updates DB_STENCIL_CLEAR */
+                       sctx->db_stencil_clear = true;
+                       si_mark_atom_dirty(sctx, &sctx->db_render_state);
+               }
        }
 
        si_blitter_begin(ctx, SI_CLEAR);
@@ -389,7 +422,14 @@ static void si_clear(struct pipe_context *ctx, unsigned buffers,
                sctx->db_depth_clear = false;
                sctx->db_depth_disable_expclear = false;
                zstex->depth_cleared = true;
-               sctx->db_render_state.dirty = true;
+               si_mark_atom_dirty(sctx, &sctx->db_render_state);
+       }
+
+       if (sctx->db_stencil_clear) {
+               sctx->db_stencil_clear = false;
+               sctx->db_stencil_disable_expclear = false;
+               zstex->stencil_cleared = true;
+               si_mark_atom_dirty(sctx, &sctx->db_render_state);
        }
 }
 
@@ -436,10 +476,14 @@ static void si_decompress_subresource(struct pipe_context *ctx,
        struct r600_texture *rtex = (struct r600_texture*)tex;
 
        if (rtex->is_depth && !rtex->is_flushing_texture) {
-               si_blit_decompress_depth_in_place(sctx, rtex,
+               si_blit_decompress_depth_in_place(sctx, rtex, false,
                                                  level, level,
                                                  first_layer, last_layer);
-       } else if (rtex->fmask.size || rtex->cmask.size) {
+               if (rtex->surface.flags & RADEON_SURF_SBUFFER)
+                       si_blit_decompress_depth_in_place(sctx, rtex, true,
+                                                         level, level,
+                                                         first_layer, last_layer);
+       } else if (rtex->fmask.size || rtex->cmask.size || rtex->dcc_buffer) {
                si_blit_decompress_color(ctx, rtex, level, level,
                                         first_layer, last_layer);
        }
@@ -455,89 +499,6 @@ struct texture_orig_info {
        unsigned npix0_y;
 };
 
-static void si_compressed_to_blittable(struct pipe_resource *tex,
-                                      unsigned level,
-                                      struct texture_orig_info *orig)
-{
-       struct r600_texture *rtex = (struct r600_texture*)tex;
-       unsigned pixsize = util_format_get_blocksize(rtex->resource.b.b.format);
-       int new_format;
-       int new_height, new_width;
-
-       orig->format = tex->format;
-       orig->width0 = tex->width0;
-       orig->height0 = tex->height0;
-       orig->npix0_x = rtex->surface.level[0].npix_x;
-       orig->npix0_y = rtex->surface.level[0].npix_y;
-       orig->npix_x = rtex->surface.level[level].npix_x;
-       orig->npix_y = rtex->surface.level[level].npix_y;
-
-       if (pixsize == 8)
-               new_format = PIPE_FORMAT_R16G16B16A16_UINT; /* 64-bit block */
-       else
-               new_format = PIPE_FORMAT_R32G32B32A32_UINT; /* 128-bit block */
-
-       new_width = util_format_get_nblocksx(tex->format, orig->width0);
-       new_height = util_format_get_nblocksy(tex->format, orig->height0);
-
-       tex->width0 = new_width;
-       tex->height0 = new_height;
-       tex->format = new_format;
-       rtex->surface.level[0].npix_x = util_format_get_nblocksx(orig->format, orig->npix0_x);
-       rtex->surface.level[0].npix_y = util_format_get_nblocksy(orig->format, orig->npix0_y);
-       rtex->surface.level[level].npix_x = util_format_get_nblocksx(orig->format, orig->npix_x);
-       rtex->surface.level[level].npix_y = util_format_get_nblocksy(orig->format, orig->npix_y);
-
-       /* By dividing the dimensions by 4, we effectively decrement
-        * last_level by 2, therefore the last 2 mipmap levels disappear and
-        * aren't blittable. Note that the last 3 mipmap levels (4x4, 2x2,
-        * 1x1) have equal slice sizes, which is an important assumption
-        * for this to work.
-        *
-        * In order to make the last 2 mipmap levels blittable, we have to
-        * add the slice size of the last mipmap level to the texture
-        * address, so that even though the hw thinks it reads last_level-2,
-        * it will actually read last_level-1, and if we add the slice size*2,
-        * it will read last_level. That's how this workaround works.
-        */
-       if (level > rtex->resource.b.b.last_level-2)
-               rtex->mipmap_shift = level - (rtex->resource.b.b.last_level-2);
-}
-
-static void si_change_format(struct pipe_resource *tex,
-                            unsigned level,
-                            struct texture_orig_info *orig,
-                            enum pipe_format format)
-{
-       struct r600_texture *rtex = (struct r600_texture*)tex;
-
-       orig->format = tex->format;
-       orig->width0 = tex->width0;
-       orig->height0 = tex->height0;
-       orig->npix0_x = rtex->surface.level[0].npix_x;
-       orig->npix0_y = rtex->surface.level[0].npix_y;
-       orig->npix_x = rtex->surface.level[level].npix_x;
-       orig->npix_y = rtex->surface.level[level].npix_y;
-
-       tex->format = format;
-}
-
-static void si_reset_blittable_to_orig(struct pipe_resource *tex,
-                                      unsigned level,
-                                      struct texture_orig_info *orig)
-{
-       struct r600_texture *rtex = (struct r600_texture*)tex;
-
-       tex->format = orig->format;
-       tex->width0 = orig->width0;
-       tex->height0 = orig->height0;
-       rtex->surface.level[0].npix_x = orig->npix0_x;
-       rtex->surface.level[0].npix_y = orig->npix0_y;
-       rtex->surface.level[level].npix_x = orig->npix_x;
-       rtex->surface.level[level].npix_y = orig->npix_y;
-       rtex->mipmap_shift = 0;
-}
-
 void si_resource_copy_region(struct pipe_context *ctx,
                             struct pipe_resource *dst,
                             unsigned dst_level,
@@ -547,114 +508,116 @@ void si_resource_copy_region(struct pipe_context *ctx,
                             const struct pipe_box *src_box)
 {
        struct si_context *sctx = (struct si_context *)ctx;
-       struct r600_texture *rdst = (struct r600_texture*)dst;
        struct pipe_surface *dst_view, dst_templ;
        struct pipe_sampler_view src_templ, *src_view;
-       struct texture_orig_info orig_info[2];
+       unsigned dst_width, dst_height, src_width0, src_height0;
+       unsigned src_force_level = 0;
        struct pipe_box sbox, dstbox;
-       boolean restore_orig[2];
 
-       /* Fallback for buffers. */
+       /* Handle buffers first. */
        if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
                si_copy_buffer(sctx, dst, src, dstx, src_box->x, src_box->width, false);
                return;
        }
 
-       memset(orig_info, 0, sizeof(orig_info));
+       assert(u_max_sample(dst) == u_max_sample(src));
 
        /* The driver doesn't decompress resources automatically while
         * u_blitter is rendering. */
        si_decompress_subresource(ctx, src, src_level,
                                  src_box->z, src_box->z + src_box->depth - 1);
 
-       restore_orig[0] = restore_orig[1] = FALSE;
+       dst_width = u_minify(dst->width0, dst_level);
+       dst_height = u_minify(dst->height0, dst_level);
+       src_width0 = src->width0;
+       src_height0 = src->height0;
+
+       util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
+       util_blitter_default_src_texture(&src_templ, src, src_level);
 
-       if (util_format_is_compressed(src->format) &&
+       if (util_format_is_compressed(src->format) ||
            util_format_is_compressed(dst->format)) {
-               si_compressed_to_blittable(src, src_level, &orig_info[0]);
-               restore_orig[0] = TRUE;
-               sbox.x = util_format_get_nblocksx(orig_info[0].format, src_box->x);
-               sbox.y = util_format_get_nblocksy(orig_info[0].format, src_box->y);
+               unsigned blocksize = util_format_get_blocksize(src->format);
+
+               if (blocksize == 8)
+                       src_templ.format = PIPE_FORMAT_R16G16B16A16_UINT; /* 64-bit block */
+               else
+                       src_templ.format = PIPE_FORMAT_R32G32B32A32_UINT; /* 128-bit block */
+               dst_templ.format = src_templ.format;
+
+               dst_width = util_format_get_nblocksx(dst->format, dst_width);
+               dst_height = util_format_get_nblocksy(dst->format, dst_height);
+               src_width0 = util_format_get_nblocksx(src->format, src_width0);
+               src_height0 = util_format_get_nblocksy(src->format, src_height0);
+
+               dstx = util_format_get_nblocksx(dst->format, dstx);
+               dsty = util_format_get_nblocksy(dst->format, dsty);
+
+               sbox.x = util_format_get_nblocksx(src->format, src_box->x);
+               sbox.y = util_format_get_nblocksy(src->format, src_box->y);
                sbox.z = src_box->z;
-               sbox.width = util_format_get_nblocksx(orig_info[0].format, src_box->width);
-               sbox.height = util_format_get_nblocksy(orig_info[0].format, src_box->height);
+               sbox.width = util_format_get_nblocksx(src->format, src_box->width);
+               sbox.height = util_format_get_nblocksy(src->format, src_box->height);
                sbox.depth = src_box->depth;
                src_box = &sbox;
 
-               si_compressed_to_blittable(dst, dst_level, &orig_info[1]);
-               restore_orig[1] = TRUE;
-               /* translate the dst box as well */
-               dstx = util_format_get_nblocksx(orig_info[1].format, dstx);
-               dsty = util_format_get_nblocksy(orig_info[1].format, dsty);
-       } else if (!util_blitter_is_copy_supported(sctx->blitter, dst, src)) {
+               src_force_level = src_level;
+       } else if (!util_blitter_is_copy_supported(sctx->blitter, dst, src) ||
+                  /* also *8_SNORM has precision issues, use UNORM instead */
+                  util_format_is_snorm8(src->format)) {
                if (util_format_is_subsampled_422(src->format)) {
-                       /* XXX untested */
-                       si_change_format(src, src_level, &orig_info[0],
-                                        PIPE_FORMAT_R8G8B8A8_UINT);
-                       si_change_format(dst, dst_level, &orig_info[1],
-                                        PIPE_FORMAT_R8G8B8A8_UINT);
+                       src_templ.format = PIPE_FORMAT_R8G8B8A8_UINT;
+                       dst_templ.format = PIPE_FORMAT_R8G8B8A8_UINT;
+
+                       dst_width = util_format_get_nblocksx(dst->format, dst_width);
+                       src_width0 = util_format_get_nblocksx(src->format, src_width0);
+
+                       dstx = util_format_get_nblocksx(dst->format, dstx);
 
                        sbox = *src_box;
-                       sbox.x = util_format_get_nblocksx(orig_info[0].format, src_box->x);
-                       sbox.width = util_format_get_nblocksx(orig_info[0].format, src_box->width);
+                       sbox.x = util_format_get_nblocksx(src->format, src_box->x);
+                       sbox.width = util_format_get_nblocksx(src->format, src_box->width);
                        src_box = &sbox;
-                       dstx = util_format_get_nblocksx(orig_info[1].format, dstx);
-
-                       restore_orig[0] = TRUE;
-                       restore_orig[1] = TRUE;
                } else {
                        unsigned blocksize = util_format_get_blocksize(src->format);
 
                        switch (blocksize) {
                        case 1:
-                               si_change_format(src, src_level, &orig_info[0],
-                                               PIPE_FORMAT_R8_UNORM);
-                               si_change_format(dst, dst_level, &orig_info[1],
-                                               PIPE_FORMAT_R8_UNORM);
+                               dst_templ.format = PIPE_FORMAT_R8_UNORM;
+                               src_templ.format = PIPE_FORMAT_R8_UNORM;
                                break;
                        case 2:
-                               si_change_format(src, src_level, &orig_info[0],
-                                               PIPE_FORMAT_R8G8_UNORM);
-                               si_change_format(dst, dst_level, &orig_info[1],
-                                               PIPE_FORMAT_R8G8_UNORM);
+                               dst_templ.format = PIPE_FORMAT_R8G8_UNORM;
+                               src_templ.format = PIPE_FORMAT_R8G8_UNORM;
                                break;
                        case 4:
-                               si_change_format(src, src_level, &orig_info[0],
-                                               PIPE_FORMAT_R8G8B8A8_UNORM);
-                               si_change_format(dst, dst_level, &orig_info[1],
-                                               PIPE_FORMAT_R8G8B8A8_UNORM);
+                               dst_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
+                               src_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
                                break;
                        case 8:
-                               si_change_format(src, src_level, &orig_info[0],
-                                               PIPE_FORMAT_R16G16B16A16_UINT);
-                               si_change_format(dst, dst_level, &orig_info[1],
-                                               PIPE_FORMAT_R16G16B16A16_UINT);
+                               dst_templ.format = PIPE_FORMAT_R16G16B16A16_UINT;
+                               src_templ.format = PIPE_FORMAT_R16G16B16A16_UINT;
                                break;
                        case 16:
-                               si_change_format(src, src_level, &orig_info[0],
-                                               PIPE_FORMAT_R32G32B32A32_UINT);
-                               si_change_format(dst, dst_level, &orig_info[1],
-                                               PIPE_FORMAT_R32G32B32A32_UINT);
+                               dst_templ.format = PIPE_FORMAT_R32G32B32A32_UINT;
+                               src_templ.format = PIPE_FORMAT_R32G32B32A32_UINT;
                                break;
                        default:
                                fprintf(stderr, "Unhandled format %s with blocksize %u\n",
                                        util_format_short_name(src->format), blocksize);
                                assert(0);
                        }
-                       restore_orig[0] = TRUE;
-                       restore_orig[1] = TRUE;
                }
        }
 
        /* Initialize the surface. */
-       util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
        dst_view = r600_create_surface_custom(ctx, dst, &dst_templ,
-                                             rdst->surface.level[dst_level].npix_x,
-                                             rdst->surface.level[dst_level].npix_y);
+                                             dst_width, dst_height);
 
        /* Initialize the sampler view. */
-       util_blitter_default_src_texture(&src_templ, src, src_level);
-       src_view = ctx->create_sampler_view(ctx, src, &src_templ);
+       src_view = si_create_sampler_view_custom(ctx, src, &src_templ,
+                                                src_width0, src_height0,
+                                                src_force_level);
 
        u_box_3d(dstx, dsty, dstz, abs(src_box->width), abs(src_box->height),
                 abs(src_box->depth), &dstbox);
@@ -662,18 +625,13 @@ void si_resource_copy_region(struct pipe_context *ctx,
        /* Copy. */
        si_blitter_begin(ctx, SI_COPY);
        util_blitter_blit_generic(sctx->blitter, dst_view, &dstbox,
-                                 src_view, src_box, src->width0, src->height0,
-                                 PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL);
+                                 src_view, src_box, src_width0, src_height0,
+                                 PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL,
+                                 FALSE);
        si_blitter_end(ctx);
 
        pipe_surface_reference(&dst_view, NULL);
        pipe_sampler_view_reference(&src_view, NULL);
-
-       if (restore_orig[0])
-               si_reset_blittable_to_orig(src, src_level, &orig_info[0]);
-
-       if (restore_orig[1])
-               si_reset_blittable_to_orig(dst, dst_level, &orig_info[1]);
 }
 
 /* For MSAA integer resolving to work, we change the format to NORM using this function. */
@@ -722,6 +680,14 @@ static bool do_hardware_msaa_resolve(struct pipe_context *ctx,
        enum pipe_format format = int_to_norm_format(info->dst.format);
        unsigned sample_mask = ~0;
 
+       /* Hardware MSAA resolve doesn't work if SPI format = NORM16_ABGR and
+        * the format is R16G16. Use R16A16, which does work.
+        */
+       if (format == PIPE_FORMAT_R16G16_UNORM)
+               format = PIPE_FORMAT_R16A16_UNORM;
+       if (format == PIPE_FORMAT_R16G16_SNORM)
+               format = PIPE_FORMAT_R16A16_SNORM;
+
        if (info->src.resource->nr_samples > 1 &&
            info->dst.resource->nr_samples <= 1 &&
            util_max_layer(info->src.resource, 0) == 0 &&
@@ -745,7 +711,8 @@ static bool do_hardware_msaa_resolve(struct pipe_context *ctx,
            info->src.box.depth == 1 &&
            dst->surface.level[info->dst.level].mode >= RADEON_SURF_MODE_1D &&
            !(dst->surface.flags & RADEON_SURF_SCANOUT) &&
-           (!dst->cmask.size || !dst->dirty_level_mask) /* dst cannot be fast-cleared */) {
+           (!dst->cmask.size || !dst->dirty_level_mask) && /* dst cannot be fast-cleared */
+           !dst->dcc_buffer) {
                si_blitter_begin(ctx, SI_COLOR_RESOLVE |
                                 (info->render_condition_enable ? 0 : SI_DISABLE_RENDER_COND));
                util_blitter_custom_resolve_color(sctx->blitter,
@@ -794,15 +761,75 @@ static void si_flush_resource(struct pipe_context *ctx,
 
        assert(res->target != PIPE_BUFFER);
 
-       if (!rtex->is_depth && rtex->cmask.size) {
+       if (!rtex->is_depth && (rtex->cmask.size || rtex->dcc_buffer)) {
                si_blit_decompress_color(ctx, rtex, 0, res->last_level,
                                         0, util_max_layer(res, 0));
        }
 }
 
+static void si_pipe_clear_buffer(struct pipe_context *ctx,
+                                struct pipe_resource *dst,
+                                unsigned offset, unsigned size,
+                                const void *clear_value_ptr,
+                                int clear_value_size)
+{
+       struct si_context *sctx = (struct si_context*)ctx;
+       uint32_t dword_value;
+       unsigned i;
+
+       assert(offset % clear_value_size == 0);
+       assert(size % clear_value_size == 0);
+
+       if (clear_value_size > 4) {
+               const uint32_t *u32 = clear_value_ptr;
+               bool clear_dword_duplicated = true;
+
+               /* See if we can lower large fills to dword fills. */
+               for (i = 1; i < clear_value_size / 4; i++)
+                       if (u32[0] != u32[i]) {
+                               clear_dword_duplicated = false;
+                               break;
+                       }
+
+               if (!clear_dword_duplicated) {
+                       /* Use transform feedback for 64-bit, 96-bit, and
+                        * 128-bit fills.
+                        */
+                       union pipe_color_union clear_value;
+
+                       memcpy(&clear_value, clear_value_ptr, clear_value_size);
+                       si_blitter_begin(ctx, SI_DISABLE_RENDER_COND);
+                       util_blitter_clear_buffer(sctx->blitter, dst, offset,
+                                                 size, clear_value_size / 4,
+                                                 &clear_value);
+                       si_blitter_end(ctx);
+                       return;
+               }
+       }
+
+       /* Expand the clear value to a dword. */
+       switch (clear_value_size) {
+       case 1:
+               dword_value = *(uint8_t*)clear_value_ptr;
+               dword_value |= (dword_value << 8) |
+                              (dword_value << 16) |
+                              (dword_value << 24);
+               break;
+       case 2:
+               dword_value = *(uint16_t*)clear_value_ptr;
+               dword_value |= dword_value << 16;
+               break;
+       default:
+               dword_value = *(uint32_t*)clear_value_ptr;
+       }
+
+       sctx->b.clear_buffer(ctx, dst, offset, size, dword_value, false);
+}
+
 void si_init_blit_functions(struct si_context *sctx)
 {
        sctx->b.b.clear = si_clear;
+       sctx->b.b.clear_buffer = si_pipe_clear_buffer;
        sctx->b.b.clear_render_target = si_clear_render_target;
        sctx->b.b.clear_depth_stencil = si_clear_depth_stencil;
        sctx->b.b.resource_copy_region = si_resource_copy_region;