radeonsi: Don't modify PA_SC_RASTER_CONFIG register value if rb_mask == 0
[mesa.git] / src / gallium / drivers / radeonsi / si_blit.c
index aa3177a9cbec35caa1d96cdfb6b5f5430cf6a667..4744154c7e21763dee4d29c057a77dda3ed7c183 100644 (file)
@@ -22,8 +22,8 @@
  */
 
 #include "si_pipe.h"
-#include "util/u_blitter.h"
 #include "util/u_format.h"
+#include "util/u_surface.h"
 
 enum si_blitter_op /* bitmask */
 {
@@ -38,12 +38,11 @@ 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)
@@ -57,26 +56,32 @@ static void si_blitter_begin(struct pipe_context *ctx, enum si_blitter_op op)
        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) {
                util_blitter_save_viewport(sctx->blitter, &sctx->queued.named.viewport->viewport);
        }
+       if (sctx->queued.named.scissor) {
+               util_blitter_save_scissor(sctx->blitter, &sctx->queued.named.scissor->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(sctx->blitter, &sctx->framebuffer);
+               util_blitter_save_framebuffer(sctx->blitter, &sctx->framebuffer.state);
 
        if (op & SI_SAVE_TEXTURES) {
                util_blitter_save_fragment_sampler_states(
-                       sctx->blitter, sctx->samplers[PIPE_SHADER_FRAGMENT].n_samplers,
-                       (void**)sctx->samplers[PIPE_SHADER_FRAGMENT].samplers);
+                       sctx->blitter, 2,
+                       sctx->samplers[PIPE_SHADER_FRAGMENT].states.saved_states);
 
-               util_blitter_save_fragment_sampler_views(sctx->blitter,
-                       util_last_bit(sctx->samplers[PIPE_SHADER_FRAGMENT].views.desc.enabled_mask &
-                                     ((1 << NUM_TEX_UNITS) - 1)),
+               util_blitter_save_fragment_sampler_views(sctx->blitter, 2,
                        sctx->samplers[PIPE_SHADER_FRAGMENT].views.views);
        }
 
@@ -110,7 +115,6 @@ static void si_blit_decompress_depth(struct pipe_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;
 
@@ -120,20 +124,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 = sctx->custom_dsa_flush_depth_stencil;
-               break;
-       case 2:
-               custom_dsa = sctx->custom_dsa_flush_stencil;
-               break;
-       case 1:
-               custom_dsa = sctx->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)))
@@ -148,6 +145,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;
@@ -161,7 +161,7 @@ static void si_blit_decompress_depth(struct pipe_context *ctx,
 
                                si_blitter_begin(ctx, SI_DECOMPRESS);
                                util_blitter_custom_depth_stencil(sctx->blitter, zsurf, cbsurf, 1 << sample,
-                                                                 custom_dsa[sample], depth);
+                                                                 sctx->custom_dsa_flush, depth);
                                si_blitter_end(ctx);
 
                                pipe_surface_reference(&zsurf, NULL);
@@ -177,6 +177,10 @@ 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 *sctx,
@@ -187,6 +191,9 @@ static void si_blit_decompress_depth_in_place(struct si_context *sctx,
        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++) {
@@ -208,7 +215,7 @@ static void si_blit_decompress_depth_in_place(struct si_context *sctx,
 
                        si_blitter_begin(&sctx->b.b, SI_DECOMPRESS);
                        util_blitter_custom_depth_stencil(sctx->blitter, zsurf, NULL, ~0,
-                                                         sctx->custom_dsa_flush_inplace,
+                                                         sctx->custom_dsa_flush,
                                                          1.0f);
                        si_blitter_end(&sctx->b.b);
 
@@ -221,23 +228,28 @@ static void si_blit_decompress_depth_in_place(struct si_context *sctx,
                        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 *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(sctx, tex,
                                                  view->u.tex.first_level, view->u.tex.last_level,
@@ -276,7 +288,8 @@ static void si_blit_decompress_color(struct pipe_context *ctx,
 
                        si_blitter_begin(ctx, SI_DECOMPRESS);
                        util_blitter_custom_color(sctx->blitter, cbsurf,
-                                                 sctx->custom_blend_decompress);
+                               rtex->fmask.size ? sctx->custom_blend_decompress :
+                                                  sctx->custom_blend_fastclear);
                        si_blitter_end(ctx);
 
                        pipe_surface_reference(&cbsurf, NULL);
@@ -319,13 +332,65 @@ static void si_clear(struct pipe_context *ctx, unsigned buffers,
                     double depth, unsigned stencil)
 {
        struct si_context *sctx = (struct si_context *)ctx;
-       struct pipe_framebuffer_state *fb = &sctx->framebuffer;
+       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(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,
@@ -473,18 +538,20 @@ static void si_reset_blittable_to_orig(struct pipe_resource *tex,
        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)
+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 *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];
-       struct pipe_box sbox;
-       const struct pipe_box *psbox = src_box;
+       struct pipe_box sbox, dstbox;
        boolean restore_orig[2];
 
        /* Fallback for buffers. */
@@ -512,7 +579,7 @@ static void si_resource_copy_region(struct pipe_context *ctx,
                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;
+               src_box = &sbox;
 
                si_compressed_to_blittable(dst, dst_level, &orig_info[1]);
                restore_orig[1] = TRUE;
@@ -520,53 +587,88 @@ static void si_resource_copy_region(struct pipe_context *ctx,
                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)) {
-               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:
+               if (util_format_is_subsampled_422(src->format)) {
+                       /* XXX untested */
                        si_change_format(src, src_level, &orig_info[0],
-                                        PIPE_FORMAT_R32G32B32A32_UINT);
+                                        PIPE_FORMAT_R8G8B8A8_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);
+                                        PIPE_FORMAT_R8G8B8A8_UINT);
+
+                       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);
+                       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);
+                               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);
+                       }
+                       restore_orig[0] = TRUE;
+                       restore_orig[1] = TRUE;
                }
-               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);
+
+       /* Initialize the sampler view. */
+       util_blitter_default_src_texture(&src_templ, src, src_level);
+       src_view = ctx->create_sampler_view(ctx, src, &src_templ);
+
+       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(sctx->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);
 
+       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]);
 
@@ -642,8 +744,10 @@ 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);
+           !(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,
@@ -673,14 +777,27 @@ 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);
+       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 *sctx)