gallium/radeon: remove build_intrinsic and build_tgsi_intrinsic
[mesa.git] / src / gallium / drivers / radeonsi / si_blit.c
index 4a5e5723d6c51b764e1ea996b3213baf05013eef..c3591a7e85a518706921d9df4fac40692b9ac8fd 100644 (file)
  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  * USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
-#include "util/u_surface.h"
-#include "util/u_blitter.h"
-#include "util/u_format.h"
+
 #include "si_pipe.h"
-#include "si_state.h"
+#include "util/u_format.h"
+#include "util/u_surface.h"
 
 enum si_blitter_op /* bitmask */
 {
@@ -39,68 +38,67 @@ enum si_blitter_op /* bitmask */
        SI_COPY          = SI_SAVE_FRAMEBUFFER | SI_SAVE_TEXTURES |
                           SI_DISABLE_RENDER_COND,
 
-       SI_BLIT          = SI_SAVE_FRAMEBUFFER | SI_SAVE_TEXTURES |
-                          SI_DISABLE_RENDER_COND,
+       SI_BLIT          = SI_SAVE_FRAMEBUFFER | SI_SAVE_TEXTURES,
 
        SI_DECOMPRESS    = SI_SAVE_FRAMEBUFFER | SI_DISABLE_RENDER_COND,
 
-       SI_COLOR_RESOLVE = SI_SAVE_FRAMEBUFFER | SI_DISABLE_RENDER_COND
+       SI_COLOR_RESOLVE = SI_SAVE_FRAMEBUFFER
 };
 
 static void si_blitter_begin(struct pipe_context *ctx, enum si_blitter_op op)
 {
-       struct si_context *rctx = (struct si_context *)ctx;
-
-       si_context_queries_suspend(rctx);
-
-       util_blitter_save_blend(rctx->blitter, rctx->queued.named.blend);
-       util_blitter_save_depth_stencil_alpha(rctx->blitter, rctx->queued.named.dsa);
-       util_blitter_save_stencil_ref(rctx->blitter, &rctx->stencil_ref);
-       util_blitter_save_rasterizer(rctx->blitter, rctx->queued.named.rasterizer);
-       util_blitter_save_fragment_shader(rctx->blitter, rctx->ps_shader);
-       util_blitter_save_vertex_shader(rctx->blitter, rctx->vs_shader);
-       util_blitter_save_vertex_elements(rctx->blitter, rctx->vertex_elements);
-       if (rctx->queued.named.viewport) {
-               util_blitter_save_viewport(rctx->blitter, &rctx->queued.named.viewport->viewport);
+       struct si_context *sctx = (struct si_context *)ctx;
+
+       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_tessctrl_shader(sctx->blitter, sctx->tcs_shader);
+       util_blitter_save_tesseval_shader(sctx->blitter, sctx->tes_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);
        }
-       util_blitter_save_vertex_buffer_slot(rctx->blitter, rctx->vertex_buffer);
-       util_blitter_save_so_targets(rctx->blitter, rctx->b.streamout.num_targets,
-                                    (struct pipe_stream_output_target**)rctx->b.streamout.targets);
+       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_so_targets(sctx->blitter, sctx->b.streamout.num_targets,
+                                    (struct pipe_stream_output_target**)sctx->b.streamout.targets);
 
        if (op & SI_SAVE_FRAMEBUFFER)
-               util_blitter_save_framebuffer(rctx->blitter, &rctx->framebuffer);
+               util_blitter_save_framebuffer(sctx->blitter, &sctx->framebuffer.state);
 
        if (op & SI_SAVE_TEXTURES) {
                util_blitter_save_fragment_sampler_states(
-                       rctx->blitter, rctx->samplers[PIPE_SHADER_FRAGMENT].n_samplers,
-                       (void**)rctx->samplers[PIPE_SHADER_FRAGMENT].samplers);
+                       sctx->blitter, 2,
+                       sctx->samplers[PIPE_SHADER_FRAGMENT].states.saved_states);
 
-               util_blitter_save_fragment_sampler_views(rctx->blitter,
-                       util_last_bit(rctx->samplers[PIPE_SHADER_FRAGMENT].views.desc.enabled_mask &
-                                     ((1 << NUM_TEX_UNITS) - 1)),
-                       rctx->samplers[PIPE_SHADER_FRAGMENT].views.views);
+               util_blitter_save_fragment_sampler_views(sctx->blitter, 2,
+                       sctx->samplers[PIPE_SHADER_FRAGMENT].views.views);
        }
 
-       if ((op & SI_DISABLE_RENDER_COND) && rctx->current_render_cond) {
-               rctx->saved_render_cond = rctx->current_render_cond;
-               rctx->saved_render_cond_cond = rctx->current_render_cond_cond;
-               rctx->saved_render_cond_mode = rctx->current_render_cond_mode;
-               rctx->b.b.render_condition(&rctx->b.b, NULL, FALSE, 0);
+       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);
        }
-
 }
 
 static void si_blitter_end(struct pipe_context *ctx)
 {
-       struct si_context *rctx = (struct si_context *)ctx;
-       if (rctx->saved_render_cond) {
-               rctx->b.b.render_condition(&rctx->b.b,
-                                              rctx->saved_render_cond,
-                                              rctx->saved_render_cond_cond,
-                                              rctx->saved_render_cond_mode);
-               rctx->saved_render_cond = NULL;
-       }
-       si_context_queries_resume(rctx);
+       struct si_context *sctx = (struct si_context *)ctx;
+       r600_resume_nontimer_queries(&sctx->b);
 }
 
 static unsigned u_max_sample(struct pipe_resource *r)
@@ -115,11 +113,10 @@ static void si_blit_decompress_depth(struct pipe_context *ctx,
                                     unsigned first_layer, unsigned last_layer,
                                     unsigned first_sample, unsigned last_sample)
 {
-       struct si_context *rctx = (struct si_context *)ctx;
+       struct si_context *sctx = (struct si_context *)ctx;
        unsigned layer, level, sample, checked_last_layer, max_layer, max_sample;
        float depth = 1.0f;
        const struct util_format_description *desc;
-       void **custom_dsa;
        struct r600_texture *flushed_depth_texture = staging ?
                        staging : texture->flushed_depth_texture;
 
@@ -129,20 +126,13 @@ static void si_blit_decompress_depth(struct pipe_context *ctx,
        max_sample = u_max_sample(&texture->resource.b.b);
 
        desc = util_format_description(flushed_depth_texture->resource.b.b.format);
-       switch (util_format_has_depth(desc) | util_format_has_stencil(desc) << 1) {
-       default:
-               assert(!"No depth or stencil to uncompress");
-               return;
-       case 3:
-               custom_dsa = rctx->custom_dsa_flush_depth_stencil;
-               break;
-       case 2:
-               custom_dsa = rctx->custom_dsa_flush_stencil;
-               break;
-       case 1:
-               custom_dsa = rctx->custom_dsa_flush_depth;
-               break;
-       }
+
+       if (util_format_has_depth(desc))
+               sctx->dbcb_depth_copy_enabled = true;
+       if (util_format_has_stencil(desc))
+               sctx->dbcb_stencil_copy_enabled = true;
+
+       assert(sctx->dbcb_depth_copy_enabled || sctx->dbcb_stencil_copy_enabled);
 
        for (level = first_level; level <= last_level; level++) {
                if (!staging && !(texture->dirty_level_mask & (1 << level)))
@@ -157,6 +147,9 @@ static void si_blit_decompress_depth(struct pipe_context *ctx,
                        for (sample = first_sample; sample <= last_sample; sample++) {
                                struct pipe_surface *zsurf, *cbsurf, surf_tmpl;
 
+                               sctx->dbcb_copy_sample = sample;
+                               sctx->db_render_state.dirty = true;
+
                                surf_tmpl.format = texture->resource.b.b.format;
                                surf_tmpl.u.tex.level = level;
                                surf_tmpl.u.tex.first_layer = layer;
@@ -169,8 +162,8 @@ static void si_blit_decompress_depth(struct pipe_context *ctx,
                                                (struct pipe_resource*)flushed_depth_texture, &surf_tmpl);
 
                                si_blitter_begin(ctx, SI_DECOMPRESS);
-                               util_blitter_custom_depth_stencil(rctx->blitter, zsurf, cbsurf, 1 << sample,
-                                                                 custom_dsa[sample], depth);
+                               util_blitter_custom_depth_stencil(sctx->blitter, zsurf, cbsurf, 1 << sample,
+                                                                 sctx->custom_dsa_flush, depth);
                                si_blitter_end(ctx);
 
                                pipe_surface_reference(&zsurf, NULL);
@@ -186,9 +179,13 @@ static void si_blit_decompress_depth(struct pipe_context *ctx,
                        texture->dirty_level_mask &= ~(1 << level);
                }
        }
+
+       sctx->dbcb_depth_copy_enabled = false;
+       sctx->dbcb_stencil_copy_enabled = false;
+       sctx->db_render_state.dirty = true;
 }
 
-static void si_blit_decompress_depth_in_place(struct si_context *rctx,
+static void si_blit_decompress_depth_in_place(struct si_context *sctx,
                                               struct r600_texture *texture,
                                               unsigned first_level, unsigned last_level,
                                               unsigned first_layer, unsigned last_layer)
@@ -196,6 +193,9 @@ static void si_blit_decompress_depth_in_place(struct si_context *rctx,
        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;
+
        surf_tmpl.format = texture->resource.b.b.format;
 
        for (level = first_level; level <= last_level; level++) {
@@ -213,13 +213,13 @@ static void si_blit_decompress_depth_in_place(struct si_context *rctx,
                        surf_tmpl.u.tex.first_layer = layer;
                        surf_tmpl.u.tex.last_layer = layer;
 
-                       zsurf = rctx->b.b.create_surface(&rctx->b.b, &texture->resource.b.b, &surf_tmpl);
+                       zsurf = sctx->b.b.create_surface(&sctx->b.b, &texture->resource.b.b, &surf_tmpl);
 
-                       si_blitter_begin(&rctx->b.b, SI_DECOMPRESS);
-                       util_blitter_custom_depth_stencil(rctx->blitter, zsurf, NULL, ~0,
-                                                         rctx->custom_dsa_flush_inplace,
+                       si_blitter_begin(&sctx->b.b, SI_DECOMPRESS);
+                       util_blitter_custom_depth_stencil(sctx->blitter, zsurf, NULL, ~0,
+                                                         sctx->custom_dsa_flush,
                                                          1.0f);
-                       si_blitter_end(&rctx->b.b);
+                       si_blitter_end(&sctx->b.b);
 
                        pipe_surface_reference(&zsurf, NULL);
                }
@@ -230,25 +230,30 @@ static void si_blit_decompress_depth_in_place(struct si_context *rctx,
                        texture->dirty_level_mask &= ~(1 << level);
                }
        }
+
+       sctx->db_inplace_flush_enabled = false;
+       sctx->db_render_state.dirty = true;
 }
 
-void si_flush_depth_textures(struct si_context *rctx,
+void si_flush_depth_textures(struct si_context *sctx,
                             struct si_textures_info *textures)
 {
        unsigned i;
+       unsigned mask = textures->depth_texture_mask;
 
-       for (i = 0; i < textures->n_views; ++i) {
+       while (mask) {
                struct pipe_sampler_view *view;
                struct r600_texture *tex;
 
+               i = u_bit_scan(&mask);
+
                view = textures->views.views[i];
-               if (!view) continue;
+               assert(view);
 
                tex = (struct r600_texture *)view->texture;
-               if (!tex->is_depth || tex->is_flushing_texture)
-                       continue;
+               assert(tex->is_depth && !tex->is_flushing_texture);
 
-               si_blit_decompress_depth_in_place(rctx, tex,
+               si_blit_decompress_depth_in_place(sctx, tex,
                                                  view->u.tex.first_level, view->u.tex.last_level,
                                                  0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));
        }
@@ -259,7 +264,7 @@ static void si_blit_decompress_color(struct pipe_context *ctx,
                unsigned first_level, unsigned last_level,
                unsigned first_layer, unsigned last_layer)
 {
-       struct si_context *rctx = (struct si_context *)ctx;
+       struct si_context *sctx = (struct si_context *)ctx;
        unsigned layer, level, checked_last_layer, max_layer;
 
        if (!rtex->dirty_level_mask)
@@ -284,8 +289,9 @@ static void si_blit_decompress_color(struct pipe_context *ctx,
                        cbsurf = ctx->create_surface(ctx, &rtex->resource.b.b, &surf_tmpl);
 
                        si_blitter_begin(ctx, SI_DECOMPRESS);
-                       util_blitter_custom_color(rctx->blitter, cbsurf,
-                                                 rctx->custom_blend_decompress);
+                       util_blitter_custom_color(sctx->blitter, cbsurf,
+                               rtex->fmask.size ? sctx->custom_blend_decompress :
+                                                  sctx->custom_blend_fastclear);
                        si_blitter_end(ctx);
 
                        pipe_surface_reference(&cbsurf, NULL);
@@ -299,7 +305,7 @@ static void si_blit_decompress_color(struct pipe_context *ctx,
        }
 }
 
-void si_decompress_color_textures(struct si_context *rctx,
+void si_decompress_color_textures(struct si_context *sctx,
                                  struct si_textures_info *textures)
 {
        unsigned i;
@@ -317,7 +323,7 @@ void si_decompress_color_textures(struct si_context *rctx,
                tex = (struct r600_texture *)view->texture;
                assert(tex->cmask.size || tex->fmask.size);
 
-               si_blit_decompress_color(&rctx->b.b, tex,
+               si_blit_decompress_color(&sctx->b.b, tex,
                                         view->u.tex.first_level, view->u.tex.last_level,
                                         0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));
        }
@@ -327,14 +333,66 @@ static void si_clear(struct pipe_context *ctx, unsigned buffers,
                     const union pipe_color_union *color,
                     double depth, unsigned stencil)
 {
-       struct si_context *rctx = (struct si_context *)ctx;
-       struct pipe_framebuffer_state *fb = &rctx->framebuffer;
+       struct si_context *sctx = (struct si_context *)ctx;
+       struct pipe_framebuffer_state *fb = &sctx->framebuffer.state;
+       struct pipe_surface *zsbuf = fb->zsbuf;
+       struct r600_texture *zstex =
+               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);
+       }
+
+       if (buffers & PIPE_CLEAR_COLOR) {
+               int i;
+
+               /* These buffers cannot use fast clear, make sure to disable expansion. */
+               for (i = 0; i < fb->nr_cbufs; i++) {
+                       struct r600_texture *tex;
+
+                       /* If not clearing this buffer, skip. */
+                       if (!(buffers & (PIPE_CLEAR_COLOR0 << i)))
+                               continue;
+
+                       if (!fb->cbufs[i])
+                               continue;
+
+                       tex = (struct r600_texture *)fb->cbufs[i]->texture;
+                       if (tex->fmask.size == 0)
+                               tex->dirty_level_mask &= ~(1 << fb->cbufs[i]->u.tex.level);
+               }
+       }
+
+       if (buffers & PIPE_CLEAR_DEPTH &&
+           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;
+               }
+
+               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;
+       }
 
        si_blitter_begin(ctx, SI_CLEAR);
-       util_blitter_clear(rctx->blitter, fb->width, fb->height,
+       util_blitter_clear(sctx->blitter, fb->width, fb->height,
                           util_framebuffer_get_num_layers(fb),
                           buffers, color, depth, stencil);
        si_blitter_end(ctx);
+
+       if (sctx->db_depth_clear) {
+               sctx->db_depth_clear = false;
+               sctx->db_depth_disable_expclear = false;
+               zstex->depth_cleared = true;
+               sctx->db_render_state.dirty = true;
+       }
 }
 
 static void si_clear_render_target(struct pipe_context *ctx,
@@ -343,10 +401,10 @@ static void si_clear_render_target(struct pipe_context *ctx,
                                   unsigned dstx, unsigned dsty,
                                   unsigned width, unsigned height)
 {
-       struct si_context *rctx = (struct si_context *)ctx;
+       struct si_context *sctx = (struct si_context *)ctx;
 
        si_blitter_begin(ctx, SI_CLEAR_SURFACE);
-       util_blitter_clear_render_target(rctx->blitter, dst, color,
+       util_blitter_clear_render_target(sctx->blitter, dst, color,
                                         dstx, dsty, width, height);
        si_blitter_end(ctx);
 }
@@ -359,10 +417,10 @@ static void si_clear_depth_stencil(struct pipe_context *ctx,
                                   unsigned dstx, unsigned dsty,
                                   unsigned width, unsigned height)
 {
-       struct si_context *rctx = (struct si_context *)ctx;
+       struct si_context *sctx = (struct si_context *)ctx;
 
        si_blitter_begin(ctx, SI_CLEAR_SURFACE);
-       util_blitter_clear_depth_stencil(rctx->blitter, dst, clear_flags, depth, stencil,
+       util_blitter_clear_depth_stencil(sctx->blitter, dst, clear_flags, depth, stencil,
                                         dstx, dsty, width, height);
        si_blitter_end(ctx);
 }
@@ -376,11 +434,11 @@ static void si_decompress_subresource(struct pipe_context *ctx,
                                      unsigned level,
                                      unsigned first_layer, unsigned last_layer)
 {
-       struct si_context *rctx = (struct si_context *)ctx;
+       struct si_context *sctx = (struct si_context *)ctx;
        struct r600_texture *rtex = (struct r600_texture*)tex;
 
        if (rtex->is_depth && !rtex->is_flushing_texture) {
-               si_blit_decompress_depth_in_place(rctx, rtex,
+               si_blit_decompress_depth_in_place(sctx, rtex,
                                                  level, level,
                                                  first_layer, last_layer);
        } else if (rtex->fmask.size || rtex->cmask.size) {
@@ -399,188 +457,136 @@ 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)
+void si_resource_copy_region(struct pipe_context *ctx,
+                            struct pipe_resource *dst,
+                            unsigned dst_level,
+                            unsigned dstx, unsigned dsty, unsigned dstz,
+                            struct pipe_resource *src,
+                            unsigned src_level,
+                            const struct pipe_box *src_box)
 {
-       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;
-}
-
-static void si_resource_copy_region(struct pipe_context *ctx,
-                                   struct pipe_resource *dst,
-                                   unsigned dst_level,
-                                   unsigned dstx, unsigned dsty, unsigned dstz,
-                                   struct pipe_resource *src,
-                                   unsigned src_level,
-                                   const struct pipe_box *src_box)
-{
-       struct si_context *rctx = (struct si_context *)ctx;
-       struct texture_orig_info orig_info[2];
-       struct pipe_box sbox;
-       const struct pipe_box *psbox = src_box;
-       boolean restore_orig[2];
-
-       /* Fallback for buffers. */
+       struct si_context *sctx = (struct si_context *)ctx;
+       struct pipe_surface *dst_view, dst_templ;
+       struct pipe_sampler_view src_templ, *src_view;
+       unsigned dst_width, dst_height, src_width0, src_height0;
+       unsigned src_force_level = 0;
+       struct pipe_box sbox, dstbox;
+
+       /* Handle buffers first. */
        if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
-               si_copy_buffer(rctx, dst, src, dstx, src_box->x, src_box->width);
+               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) &&
            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);
-               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.depth = src_box->depth;
-               psbox=&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(rctx->blitter, dst, src)) {
                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);
-                       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);
-                       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);
-                       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);
-                       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);
-                       break;
-               default:
-                       fprintf(stderr, "Unhandled format %s with blocksize %u\n",
-                               util_format_short_name(src->format), blocksize);
-                       assert(0);
+               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(src->format, src_box->width);
+               sbox.height = util_format_get_nblocksy(src->format, src_box->height);
+               sbox.depth = src_box->depth;
+               src_box = &sbox;
+
+               src_force_level = src_level;
+       } else if (!util_blitter_is_copy_supported(sctx->blitter, dst, src)) {
+               if (util_format_is_subsampled_422(src->format)) {
+                       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(src->format, src_box->x);
+                       sbox.width = util_format_get_nblocksx(src->format, src_box->width);
+                       src_box = &sbox;
+               } else {
+                       unsigned blocksize = util_format_get_blocksize(src->format);
+
+                       switch (blocksize) {
+                       case 1:
+                               dst_templ.format = PIPE_FORMAT_R8_UNORM;
+                               src_templ.format = PIPE_FORMAT_R8_UNORM;
+                               break;
+                       case 2:
+                               dst_templ.format = PIPE_FORMAT_R8G8_UNORM;
+                               src_templ.format = PIPE_FORMAT_R8G8_UNORM;
+                               break;
+                       case 4:
+                               dst_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
+                               src_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
+                               break;
+                       case 8:
+                               dst_templ.format = PIPE_FORMAT_R16G16B16A16_UINT;
+                               src_templ.format = PIPE_FORMAT_R16G16B16A16_UINT;
+                               break;
+                       case 16:
+                               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. */
+       dst_view = r600_create_surface_custom(ctx, dst, &dst_templ,
+                                             dst_width, dst_height);
+
+       /* Initialize the sampler view. */
+       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);
+
+       /* Copy. */
        si_blitter_begin(ctx, SI_COPY);
-       util_blitter_copy_texture(rctx->blitter, dst, dst_level, dstx, dsty, dstz,
-                                 src, src_level, psbox);
+       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);
        si_blitter_end(ctx);
 
-       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]);
+       pipe_surface_reference(&dst_view, NULL);
+       pipe_sampler_view_reference(&src_view, NULL);
 }
 
 /* For MSAA integer resolving to work, we change the format to NORM using this function. */
@@ -622,7 +628,7 @@ static enum pipe_format int_to_norm_format(enum pipe_format format)
 static bool do_hardware_msaa_resolve(struct pipe_context *ctx,
                                     const struct pipe_blit_info *info)
 {
-       struct si_context *rctx = (struct si_context*)ctx;
+       struct si_context *sctx = (struct si_context*)ctx;
        struct r600_texture *dst = (struct r600_texture*)info->dst.resource;
        unsigned dst_width = u_minify(info->dst.resource->width0, info->dst.level);
        unsigned dst_height = u_minify(info->dst.resource->height0, info->dst.level);
@@ -651,13 +657,15 @@ static bool do_hardware_msaa_resolve(struct pipe_context *ctx,
            info->src.box.height == dst_height &&
            info->src.box.depth == 1 &&
            dst->surface.level[info->dst.level].mode >= RADEON_SURF_MODE_1D &&
-           !(dst->surface.flags & RADEON_SURF_SCANOUT)) {
-               si_blitter_begin(ctx, SI_COLOR_RESOLVE);
-               util_blitter_custom_resolve_color(rctx->blitter,
+           !(dst->surface.flags & RADEON_SURF_SCANOUT) &&
+           (!dst->cmask.size || !dst->dirty_level_mask) /* dst cannot be fast-cleared */) {
+               si_blitter_begin(ctx, SI_COLOR_RESOLVE |
+                                (info->render_condition_enable ? 0 : SI_DISABLE_RENDER_COND));
+               util_blitter_custom_resolve_color(sctx->blitter,
                                                  info->dst.resource, info->dst.level,
                                                  info->dst.box.z,
                                                  info->src.resource, info->src.box.z,
-                                                 sample_mask, rctx->custom_blend_resolve,
+                                                 sample_mask, sctx->custom_blend_resolve,
                                                  format);
                si_blitter_end(ctx);
                return true;
@@ -668,13 +676,13 @@ static bool do_hardware_msaa_resolve(struct pipe_context *ctx,
 static void si_blit(struct pipe_context *ctx,
                    const struct pipe_blit_info *info)
 {
-       struct si_context *rctx = (struct si_context*)ctx;
+       struct si_context *sctx = (struct si_context*)ctx;
 
        if (do_hardware_msaa_resolve(ctx, info)) {
                return;
        }
 
-       assert(util_blitter_is_blit_supported(rctx->blitter, info));
+       assert(util_blitter_is_blit_supported(sctx->blitter, info));
 
        /* The driver doesn't decompress resources automatically while
         * u_blitter is rendering. */
@@ -682,23 +690,36 @@ static void si_blit(struct pipe_context *ctx,
                                  info->src.box.z,
                                  info->src.box.z + info->src.box.depth - 1);
 
-       si_blitter_begin(ctx, SI_BLIT);
-       util_blitter_blit(rctx->blitter, info);
+       if (sctx->screen->b.debug_flags & DBG_FORCE_DMA &&
+           util_try_blit_via_copy_region(ctx, info))
+               return;
+
+       si_blitter_begin(ctx, SI_BLIT |
+                        (info->render_condition_enable ? 0 : SI_DISABLE_RENDER_COND));
+       util_blitter_blit(sctx->blitter, info);
        si_blitter_end(ctx);
 }
 
 static void si_flush_resource(struct pipe_context *ctx,
-                             struct pipe_resource *resource)
+                             struct pipe_resource *res)
 {
+       struct r600_texture *rtex = (struct r600_texture*)res;
+
+       assert(res->target != PIPE_BUFFER);
+
+       if (!rtex->is_depth && rtex->cmask.size) {
+               si_blit_decompress_color(ctx, rtex, 0, res->last_level,
+                                        0, util_max_layer(res, 0));
+       }
 }
 
-void si_init_blit_functions(struct si_context *rctx)
+void si_init_blit_functions(struct si_context *sctx)
 {
-       rctx->b.b.clear = si_clear;
-       rctx->b.b.clear_render_target = si_clear_render_target;
-       rctx->b.b.clear_depth_stencil = si_clear_depth_stencil;
-       rctx->b.b.resource_copy_region = si_resource_copy_region;
-       rctx->b.b.blit = si_blit;
-       rctx->b.b.flush_resource = si_flush_resource;
-       rctx->b.blit_decompress_depth = si_blit_decompress_depth;
+       sctx->b.b.clear = si_clear;
+       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;
+       sctx->b.b.blit = si_blit;
+       sctx->b.b.flush_resource = si_flush_resource;
+       sctx->b.blit_decompress_depth = si_blit_decompress_depth;
 }