radeonsi: set some colorbuffer register fields at emit time
[mesa.git] / src / gallium / drivers / radeon / r600_texture.c
index 88b799d87ee30c7f504712075521cb335e1ffe45..af10182e4ff791af4752a7a72f5ac5b0152e5d91 100644 (file)
 #include <errno.h>
 #include <inttypes.h>
 
+static void r600_texture_discard_dcc(struct r600_common_screen *rscreen,
+                                    struct r600_texture *rtex);
+static void r600_texture_discard_cmask(struct r600_common_screen *rscreen,
+                                      struct r600_texture *rtex);
+
+
+bool r600_prepare_for_dma_blit(struct r600_common_context *rctx,
+                              struct r600_texture *rdst,
+                              unsigned dst_level, unsigned dstx,
+                              unsigned dsty, unsigned dstz,
+                              struct r600_texture *rsrc,
+                              unsigned src_level,
+                              const struct pipe_box *src_box)
+{
+       if (!rctx->dma.cs)
+               return false;
+
+       if (util_format_get_blocksizebits(rdst->resource.b.b.format) !=
+           util_format_get_blocksizebits(rsrc->resource.b.b.format))
+               return false;
+
+       /* MSAA: Blits don't exist in the real world. */
+       if (rsrc->resource.b.b.nr_samples > 1 ||
+           rdst->resource.b.b.nr_samples > 1)
+               return false;
+
+       /* Depth-stencil surfaces:
+        *   When dst is linear, the DB->CB copy preserves HTILE.
+        *   When dst is tiled, the 3D path must be used to update HTILE.
+        */
+       if (rsrc->is_depth || rdst->is_depth)
+               return false;
+
+       /* DCC as:
+        *   src: Use the 3D path. DCC decompression is expensive.
+        *   dst: If overwriting the whole texture, discard DCC and use SDMA.
+        *        Otherwise, use the 3D path.
+        */
+       if (rsrc->dcc_offset)
+               return false;
+
+       if (rdst->dcc_offset) {
+               /* We can't discard DCC if the texture has been exported.
+                * We can only discard DCC for the entire texture.
+                */
+               if (rdst->resource.is_shared ||
+                   rdst->resource.b.b.last_level > 0 ||
+                   !util_texrange_covers_whole_level(&rdst->resource.b.b, dst_level,
+                                                     dstx, dsty, dstz, src_box->width,
+                                                     src_box->height, src_box->depth))
+                       return false;
+
+               r600_texture_discard_dcc(rctx->screen, rdst);
+       }
+
+       /* CMASK as:
+        *   src: Both texture and SDMA paths need decompression. Use SDMA.
+        *   dst: If overwriting the whole texture, discard CMASK and use
+        *        SDMA. Otherwise, use the 3D path.
+        */
+       if (rdst->cmask.size && rdst->dirty_level_mask & (1 << dst_level)) {
+               /* The CMASK clear is only enabled for the first level. */
+               assert(dst_level == 0);
+               if (!util_texrange_covers_whole_level(&rdst->resource.b.b, dst_level,
+                                                     dstx, dsty, dstz, src_box->width,
+                                                     src_box->height, src_box->depth))
+                       return false;
+
+               r600_texture_discard_cmask(rctx->screen, rdst);
+       }
+
+       /* All requirements are met. Prepare textures for SDMA. */
+       if (rsrc->cmask.size && rsrc->dirty_level_mask & (1 << src_level))
+               rctx->b.flush_resource(&rctx->b, &rsrc->resource.b.b);
+
+       assert(!(rsrc->dirty_level_mask & (1 << src_level)));
+       assert(!(rdst->dirty_level_mask & (1 << dst_level)));
+
+       return true;
+}
+
 /* Same as resource_copy_region, except that both upsampling and downsampling are allowed. */
 static void r600_copy_region_with_blit(struct pipe_context *pipe,
                                       struct pipe_resource *dst,
@@ -169,8 +250,9 @@ static int r600_init_surface(struct r600_common_screen *rscreen,
                surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_1D_ARRAY, TYPE);
                surface->array_size = ptex->array_size;
                break;
-       case PIPE_TEXTURE_2D_ARRAY:
        case PIPE_TEXTURE_CUBE_ARRAY: /* cube array layout like 2d array */
+               assert(ptex->array_size % 6 == 0);
+       case PIPE_TEXTURE_2D_ARRAY:
                surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_2D_ARRAY, TYPE);
                surface->array_size = ptex->array_size;
                break;
@@ -201,9 +283,11 @@ static int r600_init_surface(struct r600_common_screen *rscreen,
 
 static int r600_setup_surface(struct pipe_screen *screen,
                              struct r600_texture *rtex,
-                             unsigned pitch_in_bytes_override)
+                             unsigned pitch_in_bytes_override,
+                             unsigned offset)
 {
        struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
+       unsigned i;
        int r;
 
        r = rscreen->ws->surface_init(rscreen->ws, &rtex->surface);
@@ -220,39 +304,173 @@ static int r600_setup_surface(struct pipe_screen *screen,
                rtex->surface.level[0].nblk_x = pitch_in_bytes_override / rtex->surface.bpe;
                rtex->surface.level[0].pitch_bytes = pitch_in_bytes_override;
                rtex->surface.level[0].slice_size = pitch_in_bytes_override * rtex->surface.level[0].nblk_y;
-               if (rtex->surface.flags & RADEON_SURF_SBUFFER) {
-                       rtex->surface.stencil_offset =
-                       rtex->surface.stencil_level[0].offset = rtex->surface.level[0].slice_size;
-               }
+       }
+
+       if (offset) {
+               for (i = 0; i < ARRAY_SIZE(rtex->surface.level); ++i)
+                       rtex->surface.level[i].offset += offset;
        }
        return 0;
 }
 
-static boolean r600_texture_get_handle(struct pipe_screen* screen,
-                                      struct pipe_resource *ptex,
-                                      struct winsys_handle *whandle)
+static void r600_texture_init_metadata(struct r600_texture *rtex,
+                                      struct radeon_bo_metadata *metadata)
 {
-       struct r600_texture *rtex = (struct r600_texture*)ptex;
-       struct r600_resource *resource = &rtex->resource;
        struct radeon_surf *surface = &rtex->surface;
+
+       memset(metadata, 0, sizeof(*metadata));
+       metadata->microtile = surface->level[0].mode >= RADEON_SURF_MODE_1D ?
+                                  RADEON_LAYOUT_TILED : RADEON_LAYOUT_LINEAR;
+       metadata->macrotile = surface->level[0].mode >= RADEON_SURF_MODE_2D ?
+                                  RADEON_LAYOUT_TILED : RADEON_LAYOUT_LINEAR;
+       metadata->pipe_config = surface->pipe_config;
+       metadata->bankw = surface->bankw;
+       metadata->bankh = surface->bankh;
+       metadata->tile_split = surface->tile_split;
+       metadata->mtilea = surface->mtilea;
+       metadata->num_banks = surface->num_banks;
+       metadata->stride = surface->level[0].pitch_bytes;
+       metadata->scanout = (surface->flags & RADEON_SURF_SCANOUT) != 0;
+}
+
+static void r600_dirty_all_framebuffer_states(struct r600_common_screen *rscreen)
+{
+       p_atomic_inc(&rscreen->dirty_fb_counter);
+}
+
+static void r600_eliminate_fast_color_clear(struct r600_common_screen *rscreen,
+                                     struct r600_texture *rtex)
+{
+       struct pipe_context *ctx = rscreen->aux_context;
+
+       pipe_mutex_lock(rscreen->aux_context_lock);
+       ctx->flush_resource(ctx, &rtex->resource.b.b);
+       ctx->flush(ctx, NULL, 0);
+       pipe_mutex_unlock(rscreen->aux_context_lock);
+}
+
+static void r600_texture_discard_cmask(struct r600_common_screen *rscreen,
+                                      struct r600_texture *rtex)
+{
+       if (!rtex->cmask.size)
+               return;
+
+       assert(rtex->resource.b.b.nr_samples <= 1);
+
+       /* Disable CMASK. */
+       memset(&rtex->cmask, 0, sizeof(rtex->cmask));
+       rtex->cmask.base_address_reg = rtex->resource.gpu_address >> 8;
+
+       if (rscreen->chip_class >= SI)
+               rtex->cb_color_info &= ~SI_S_028C70_FAST_CLEAR(1);
+       else
+               rtex->cb_color_info &= ~EG_S_028C70_FAST_CLEAR(1);
+
+       if (rtex->cmask_buffer != &rtex->resource)
+           pipe_resource_reference((struct pipe_resource**)&rtex->cmask_buffer, NULL);
+
+       /* Notify all contexts about the change. */
+       r600_dirty_all_framebuffer_states(rscreen);
+       p_atomic_inc(&rscreen->compressed_colortex_counter);
+}
+
+static void r600_texture_discard_dcc(struct r600_common_screen *rscreen,
+                                    struct r600_texture *rtex)
+{
+       /* Disable DCC. */
+       rtex->dcc_offset = 0;
+       rtex->cb_color_info &= ~VI_S_028C70_DCC_ENABLE(1);
+
+       /* Notify all contexts about the change. */
+       r600_dirty_all_framebuffer_states(rscreen);
+}
+
+void r600_texture_disable_dcc(struct r600_common_screen *rscreen,
+                             struct r600_texture *rtex)
+{
+       struct r600_common_context *rctx =
+               (struct r600_common_context *)rscreen->aux_context;
+
+       if (!rtex->dcc_offset)
+               return;
+
+       /* Decompress DCC. */
+       pipe_mutex_lock(rscreen->aux_context_lock);
+       rctx->decompress_dcc(&rctx->b, rtex);
+       rctx->b.flush(&rctx->b, NULL, 0);
+       pipe_mutex_unlock(rscreen->aux_context_lock);
+
+       r600_texture_discard_dcc(rscreen, rtex);
+}
+
+static boolean r600_texture_get_handle(struct pipe_screen* screen,
+                                      struct pipe_resource *resource,
+                                      struct winsys_handle *whandle,
+                                       unsigned usage)
+{
        struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
+       struct r600_resource *res = (struct r600_resource*)resource;
+       struct r600_texture *rtex = (struct r600_texture*)resource;
+       struct radeon_bo_metadata metadata;
+       bool update_metadata = false;
 
-       rscreen->ws->buffer_set_tiling(resource->buf,
-                                      NULL,
-                                      surface->level[0].mode >= RADEON_SURF_MODE_1D ?
-                                      RADEON_LAYOUT_TILED : RADEON_LAYOUT_LINEAR,
-                                      surface->level[0].mode >= RADEON_SURF_MODE_2D ?
-                                      RADEON_LAYOUT_TILED : RADEON_LAYOUT_LINEAR,
-                                      surface->pipe_config,
-                                      surface->bankw, surface->bankh,
-                                      surface->tile_split,
-                                      surface->stencil_tile_split,
-                                      surface->mtilea, surface->num_banks,
-                                      surface->level[0].pitch_bytes,
-                                      (surface->flags & RADEON_SURF_SCANOUT) != 0);
-
-       return rscreen->ws->buffer_get_handle(resource->buf,
-                                               surface->level[0].pitch_bytes, whandle);
+       /* This is not supported now, but it might be required for OpenCL
+        * interop in the future.
+        */
+       if (resource->target != PIPE_BUFFER &&
+           (resource->nr_samples > 1 || rtex->is_depth))
+               return false;
+
+       if (resource->target != PIPE_BUFFER) {
+               /* Since shader image stores don't support DCC on VI,
+                * disable it for external clients that want write
+                * access.
+                */
+               if (usage & PIPE_HANDLE_USAGE_WRITE && rtex->dcc_offset) {
+                       r600_texture_disable_dcc(rscreen, rtex);
+                       update_metadata = true;
+               }
+
+               if (!(usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH) &&
+                   rtex->cmask.size) {
+                       /* Eliminate fast clear (both CMASK and DCC) */
+                       r600_eliminate_fast_color_clear(rscreen, rtex);
+
+                       /* Disable CMASK if flush_resource isn't going
+                        * to be called.
+                        */
+                       r600_texture_discard_cmask(rscreen, rtex);
+                       update_metadata = true;
+               }
+
+               /* Set metadata. */
+               if (!res->is_shared || update_metadata) {
+                       r600_texture_init_metadata(rtex, &metadata);
+                       if (rscreen->query_opaque_metadata)
+                               rscreen->query_opaque_metadata(rscreen, rtex,
+                                                              &metadata);
+
+                       rscreen->ws->buffer_set_metadata(res->buf, &metadata);
+               }
+       }
+
+       if (res->is_shared) {
+               /* USAGE_EXPLICIT_FLUSH must be cleared if at least one user
+                * doesn't set it.
+                */
+               res->external_usage |= usage & ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH;
+               if (!(usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH))
+                       res->external_usage &= ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH;
+       } else {
+               res->is_shared = true;
+               res->external_usage = usage;
+       }
+
+       return rscreen->ws->buffer_get_handle(res->buf,
+                                             rtex->surface.level[0].pitch_bytes,
+                                             rtex->surface.level[0].offset,
+                                             rtex->surface.level[0].slice_size,
+                                             whandle);
 }
 
 static void r600_texture_destroy(struct pipe_screen *screen,
@@ -268,7 +486,6 @@ static void r600_texture_destroy(struct pipe_screen *screen,
        if (rtex->cmask_buffer != &rtex->resource) {
            pipe_resource_reference((struct pipe_resource**)&rtex->cmask_buffer, NULL);
        }
-       pipe_resource_reference((struct pipe_resource**)&rtex->dcc_buffer, NULL);
        pb_reference(&resource->buf, NULL);
        FREE(rtex);
 }
@@ -348,7 +565,7 @@ static void r600_texture_allocate_fmask(struct r600_common_screen *rscreen,
        r600_texture_get_fmask_info(rscreen, rtex,
                                    rtex->resource.b.b.nr_samples, &rtex->fmask);
 
-       rtex->fmask.offset = align(rtex->size, rtex->fmask.alignment);
+       rtex->fmask.offset = align64(rtex->size, rtex->fmask.alignment);
        rtex->size = rtex->fmask.offset + rtex->fmask.size;
 }
 
@@ -361,8 +578,8 @@ void r600_texture_get_cmask_info(struct r600_common_screen *rscreen,
        unsigned cmask_tile_elements = cmask_tile_width * cmask_tile_height;
        unsigned element_bits = 4;
        unsigned cmask_cache_bits = 1024;
-       unsigned num_pipes = rscreen->tiling_info.num_channels;
-       unsigned pipe_interleave_bytes = rscreen->tiling_info.group_bytes;
+       unsigned num_pipes = rscreen->info.num_tile_pipes;
+       unsigned pipe_interleave_bytes = rscreen->info.pipe_interleave_bytes;
 
        unsigned elements_per_macro_tile = (cmask_cache_bits / element_bits) * num_pipes;
        unsigned pixels_per_macro_tile = elements_per_macro_tile * cmask_tile_elements;
@@ -394,8 +611,8 @@ static void si_texture_get_cmask_info(struct r600_common_screen *rscreen,
                                      struct r600_texture *rtex,
                                      struct r600_cmask_info *out)
 {
-       unsigned pipe_interleave_bytes = rscreen->tiling_info.group_bytes;
-       unsigned num_pipes = rscreen->tiling_info.num_channels;
+       unsigned pipe_interleave_bytes = rscreen->info.pipe_interleave_bytes;
+       unsigned num_pipes = rscreen->info.num_tile_pipes;
        unsigned cl_width, cl_height;
 
        switch (num_pipes) {
@@ -451,7 +668,7 @@ static void r600_texture_allocate_cmask(struct r600_common_screen *rscreen,
                r600_texture_get_cmask_info(rscreen, rtex, &rtex->cmask);
        }
 
-       rtex->cmask.offset = align(rtex->size, rtex->cmask.alignment);
+       rtex->cmask.offset = align64(rtex->size, rtex->cmask.alignment);
        rtex->size = rtex->cmask.offset + rtex->cmask.size;
 
        if (rscreen->chip_class >= SI)
@@ -489,25 +706,8 @@ static void r600_texture_alloc_cmask_separate(struct r600_common_screen *rscreen
                rtex->cb_color_info |= SI_S_028C70_FAST_CLEAR(1);
        else
                rtex->cb_color_info |= EG_S_028C70_FAST_CLEAR(1);
-}
-
-static void vi_texture_alloc_dcc_separate(struct r600_common_screen *rscreen,
-                                             struct r600_texture *rtex)
-{
-       if (rscreen->debug_flags & DBG_NO_DCC)
-               return;
 
-       rtex->dcc_buffer = (struct r600_resource *)
-               r600_aligned_buffer_create(&rscreen->b, PIPE_BIND_CUSTOM,
-                                  PIPE_USAGE_DEFAULT, rtex->surface.dcc_size, rtex->surface.dcc_alignment);
-       if (rtex->dcc_buffer == NULL) {
-               return;
-       }
-
-       r600_screen_clear_buffer(rscreen, &rtex->dcc_buffer->b.b, 0, rtex->surface.dcc_size,
-                                0xFFFFFFFF, true);
-
-       rtex->cb_color_info |= VI_S_028C70_DCC_ENABLE(1);
+       p_atomic_inc(&rscreen->compressed_colortex_counter);
 }
 
 static unsigned r600_texture_get_htile_size(struct r600_common_screen *rscreen,
@@ -515,7 +715,7 @@ static unsigned r600_texture_get_htile_size(struct r600_common_screen *rscreen,
 {
        unsigned cl_width, cl_height, width, height;
        unsigned slice_elements, slice_bytes, pipe_interleave_bytes, base_align;
-       unsigned num_pipes = rscreen->tiling_info.num_channels;
+       unsigned num_pipes = rscreen->info.num_tile_pipes;
 
        if (rscreen->chip_class <= EVERGREEN &&
            rscreen->info.drm_major == 2 && rscreen->info.drm_minor < 26)
@@ -533,6 +733,16 @@ static unsigned r600_texture_get_htile_size(struct r600_common_screen *rscreen,
            rscreen->info.drm_major == 2 && rscreen->info.drm_minor < 38)
                return 0;
 
+       /* Overalign HTILE on P2 configs to work around GPU hangs in
+        * piglit/depthstencil-render-miplevels 585.
+        *
+        * This has been confirmed to help Kabini & Stoney, where the hangs
+        * are always reproducible. I think I have seen the test hang
+        * on Carrizo too, though it was very rare there.
+        */
+       if (rscreen->chip_class >= CIK && num_pipes < 4)
+               num_pipes = 4;
+
        switch (num_pipes) {
        case 1:
                cl_width = 32;
@@ -565,7 +775,7 @@ static unsigned r600_texture_get_htile_size(struct r600_common_screen *rscreen,
        slice_elements = (width * height) / (8 * 8);
        slice_bytes = slice_elements * 4;
 
-       pipe_interleave_bytes = rscreen->tiling_info.group_bytes;
+       pipe_interleave_bytes = rscreen->info.pipe_interleave_bytes;
        base_align = num_pipes * pipe_interleave_bytes;
 
        rtex->htile.pitch = width;
@@ -594,7 +804,7 @@ static void r600_texture_allocate_htile(struct r600_common_screen *rscreen,
                R600_ERR("Failed to create buffer object for htile buffer.\n");
        } else {
                r600_screen_clear_buffer(rscreen, &rtex->htile_buffer->b.b, 0,
-                                        htile_size, 0, true);
+                                        htile_size, 0, R600_COHERENCY_NONE);
        }
 }
 
@@ -620,14 +830,14 @@ void r600_print_texture_info(struct r600_texture *rtex, FILE *f)
                (rtex->surface.flags & RADEON_SURF_SCANOUT) != 0);
 
        if (rtex->fmask.size)
-               fprintf(f, "  FMask: offset=%u, size=%u, alignment=%u, pitch_in_pixels=%u, "
+               fprintf(f, "  FMask: offset=%"PRIu64", size=%"PRIu64", alignment=%u, pitch_in_pixels=%u, "
                        "bankh=%u, slice_tile_max=%u, tile_mode_index=%u\n",
                        rtex->fmask.offset, rtex->fmask.size, rtex->fmask.alignment,
                        rtex->fmask.pitch_in_pixels, rtex->fmask.bank_height,
                        rtex->fmask.slice_tile_max, rtex->fmask.tile_mode_index);
 
        if (rtex->cmask.size)
-               fprintf(f, "  CMask: offset=%u, size=%u, alignment=%u, pitch=%u, "
+               fprintf(f, "  CMask: offset=%"PRIu64", size=%"PRIu64", alignment=%u, pitch=%u, "
                        "height=%u, xalign=%u, yalign=%u, slice_tile_max=%u\n",
                        rtex->cmask.offset, rtex->cmask.size, rtex->cmask.alignment,
                        rtex->cmask.pitch, rtex->cmask.height, rtex->cmask.xalign,
@@ -640,10 +850,10 @@ void r600_print_texture_info(struct r600_texture *rtex, FILE *f)
                        rtex->htile_buffer->buf->alignment, rtex->htile.pitch,
                        rtex->htile.height, rtex->htile.xalign, rtex->htile.yalign);
 
-       if (rtex->dcc_buffer) {
-               fprintf(f, "  DCC: size=%u, alignment=%u\n",
-                       rtex->dcc_buffer->b.b.width0,
-                       rtex->dcc_buffer->buf->alignment);
+       if (rtex->dcc_offset) {
+               fprintf(f, "  DCC: offset=%"PRIu64", size=%"PRIu64", alignment=%"PRIu64"\n",
+                       rtex->dcc_offset, rtex->surface.dcc_size,
+                       rtex->surface.dcc_alignment);
                for (i = 0; i <= rtex->surface.last_level; i++)
                        fprintf(f, "  DCCLevel[%i]: offset=%"PRIu64"\n",
                                i, rtex->surface.level[i].dcc_offset);
@@ -691,6 +901,7 @@ static struct r600_texture *
 r600_texture_create_object(struct pipe_screen *screen,
                           const struct pipe_resource *base,
                           unsigned pitch_in_bytes_override,
+                          unsigned offset,
                           struct pb_buffer *buf,
                           struct radeon_surf *surface)
 {
@@ -699,7 +910,7 @@ r600_texture_create_object(struct pipe_screen *screen,
        struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
 
        rtex = CALLOC_STRUCT(r600_texture);
-       if (rtex == NULL)
+       if (!rtex)
                return NULL;
 
        resource = &rtex->resource;
@@ -712,7 +923,7 @@ r600_texture_create_object(struct pipe_screen *screen,
        rtex->is_depth = util_format_has_depth(util_format_description(rtex->resource.b.b.format));
 
        rtex->surface = *surface;
-       if (r600_setup_surface(screen, rtex, pitch_in_bytes_override)) {
+       if (r600_setup_surface(screen, rtex, pitch_in_bytes_override, offset)) {
                FREE(rtex);
                return NULL;
        }
@@ -741,29 +952,40 @@ r600_texture_create_object(struct pipe_screen *screen,
                                return NULL;
                        }
                }
-               if (rtex->surface.dcc_size)
-                       vi_texture_alloc_dcc_separate(rscreen, rtex);
+
+               if (!buf && rtex->surface.dcc_size &&
+                   !(rscreen->debug_flags & DBG_NO_DCC)) {
+                       /* Reserve space for the DCC buffer. */
+                       rtex->dcc_offset = align64(rtex->size, rtex->surface.dcc_alignment);
+                       rtex->size = rtex->dcc_offset + rtex->surface.dcc_size;
+                       rtex->cb_color_info |= VI_S_028C70_DCC_ENABLE(1);
+               }
        }
 
        /* Now create the backing buffer. */
        if (!buf) {
                if (!r600_init_resource(rscreen, resource, rtex->size,
-                                       rtex->surface.bo_alignment, TRUE)) {
+                                       rtex->surface.bo_alignment)) {
                        FREE(rtex);
                        return NULL;
                }
        } else {
                resource->buf = buf;
-               resource->cs_buf = rscreen->ws->buffer_get_cs_handle(buf);
-               resource->gpu_address = rscreen->ws->buffer_get_virtual_address(resource->cs_buf);
-               resource->domains = rscreen->ws->buffer_get_initial_domain(resource->cs_buf);
+               resource->gpu_address = rscreen->ws->buffer_get_virtual_address(resource->buf);
+               resource->domains = rscreen->ws->buffer_get_initial_domain(resource->buf);
        }
 
        if (rtex->cmask.size) {
                /* Initialize the cmask to 0xCC (= compressed state). */
                r600_screen_clear_buffer(rscreen, &rtex->cmask_buffer->b.b,
                                         rtex->cmask.offset, rtex->cmask.size,
-                                        0xCCCCCCCC, true);
+                                        0xCCCCCCCC, R600_COHERENCY_NONE);
+       }
+       if (rtex->dcc_offset) {
+               r600_screen_clear_buffer(rscreen, &rtex->resource.b.b,
+                                        rtex->dcc_offset,
+                                        rtex->surface.dcc_size,
+                                        0xFFFFFFFF, R600_COHERENCY_NONE);
        }
 
        /* Initialize the CMASK base register value. */
@@ -808,13 +1030,12 @@ static unsigned r600_choose_tiling(struct r600_common_screen *rscreen,
                force_tiling = true;
 
        /* Handle common candidates for the linear mode.
-        * Compressed textures must always be tiled. */
-       if (!force_tiling && !util_format_is_compressed(templ->format)) {
-               /* Not everything can be linear, so we cannot enforce it
-                * for all textures. */
-               if ((rscreen->debug_flags & DBG_NO_TILING) &&
-                   (!util_format_is_depth_or_stencil(templ->format) ||
-                    !(templ->flags & R600_RESOURCE_FLAG_FLUSHED_DEPTH)))
+        * Compressed textures and DB surfaces must always be tiled.
+        */
+       if (!force_tiling && !util_format_is_compressed(templ->format) &&
+           (!util_format_is_depth_or_stencil(templ->format) ||
+            templ->flags & R600_RESOURCE_FLAG_FLUSHED_DEPTH)) {
+               if (rscreen->debug_flags & DBG_NO_TILING)
                        return RADEON_SURF_MODE_LINEAR_ALIGNED;
 
                /* Tiling doesn't work with the 422 (SUBSAMPLED) formats on R600+. */
@@ -868,41 +1089,45 @@ struct pipe_resource *r600_texture_create(struct pipe_screen *screen,
        if (r) {
                return NULL;
        }
-       return (struct pipe_resource *)r600_texture_create_object(screen, templ,
+       return (struct pipe_resource *)r600_texture_create_object(screen, templ, 0,
                                                                  0, NULL, &surface);
 }
 
 static struct pipe_resource *r600_texture_from_handle(struct pipe_screen *screen,
                                                      const struct pipe_resource *templ,
-                                                     struct winsys_handle *whandle)
+                                                     struct winsys_handle *whandle,
+                                                      unsigned usage)
 {
        struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
        struct pb_buffer *buf = NULL;
-       unsigned stride = 0;
+       unsigned stride = 0, offset = 0;
        unsigned array_mode;
-       enum radeon_bo_layout micro, macro;
        struct radeon_surf surface;
-       bool scanout;
        int r;
+       struct radeon_bo_metadata metadata = {};
+       struct r600_texture *rtex;
 
        /* Support only 2D textures without mipmaps */
        if ((templ->target != PIPE_TEXTURE_2D && templ->target != PIPE_TEXTURE_RECT) ||
              templ->depth0 != 1 || templ->last_level != 0)
                return NULL;
 
-       buf = rscreen->ws->buffer_from_handle(rscreen->ws, whandle, &stride);
+       buf = rscreen->ws->buffer_from_handle(rscreen->ws, whandle, &stride, &offset);
        if (!buf)
                return NULL;
 
-       rscreen->ws->buffer_get_tiling(buf, &micro, &macro,
-                                      &surface.bankw, &surface.bankh,
-                                      &surface.tile_split,
-                                      &surface.stencil_tile_split,
-                                      &surface.mtilea, &scanout);
+       rscreen->ws->buffer_get_metadata(buf, &metadata);
 
-       if (macro == RADEON_LAYOUT_TILED)
+       surface.pipe_config = metadata.pipe_config;
+       surface.bankw = metadata.bankw;
+       surface.bankh = metadata.bankh;
+       surface.tile_split = metadata.tile_split;
+       surface.mtilea = metadata.mtilea;
+       surface.num_banks = metadata.num_banks;
+
+       if (metadata.macrotile == RADEON_LAYOUT_TILED)
                array_mode = RADEON_SURF_MODE_2D;
-       else if (micro == RADEON_LAYOUT_TILED)
+       else if (metadata.microtile == RADEON_LAYOUT_TILED)
                array_mode = RADEON_SURF_MODE_1D;
        else
                array_mode = RADEON_SURF_MODE_LINEAR_ALIGNED;
@@ -912,11 +1137,17 @@ static struct pipe_resource *r600_texture_from_handle(struct pipe_screen *screen
                return NULL;
        }
 
-       if (scanout)
+       if (metadata.scanout)
                surface.flags |= RADEON_SURF_SCANOUT;
 
-       return (struct pipe_resource *)r600_texture_create_object(screen, templ,
-                                                                 stride, buf, &surface);
+       rtex = r600_texture_create_object(screen, templ, stride,
+                                         offset, buf, &surface);
+       if (!rtex)
+               return NULL;
+
+       rtex->resource.is_shared = true;
+       rtex->resource.external_usage = usage;
+       return &rtex->resource.b.b;
 }
 
 bool r600_init_flushed_depth_texture(struct pipe_context *ctx,
@@ -977,21 +1208,11 @@ static void r600_init_temp_resource_from_box(struct pipe_resource *res,
        res->flags = flags;
 
        /* We must set the correct texture target and dimensions for a 3D box. */
-       if (box->depth > 1 && util_max_layer(orig, level) > 0)
-               res->target = orig->target;
-       else
-               res->target = PIPE_TEXTURE_2D;
-
-       switch (res->target) {
-       case PIPE_TEXTURE_1D_ARRAY:
-       case PIPE_TEXTURE_2D_ARRAY:
-       case PIPE_TEXTURE_CUBE_ARRAY:
+       if (box->depth > 1 && util_max_layer(orig, level) > 0) {
+               res->target = PIPE_TEXTURE_2D_ARRAY;
                res->array_size = box->depth;
-               break;
-       case PIPE_TEXTURE_3D:
-               res->depth0 = box->depth;
-               break;
-       default:;
+       } else {
+               res->target = PIPE_TEXTURE_2D;
        }
 }
 
@@ -1010,6 +1231,8 @@ static void *r600_texture_transfer_map(struct pipe_context *ctx,
        unsigned offset = 0;
        char *map;
 
+       assert(!(texture->flags & R600_RESOURCE_FLAG_TRANSFER));
+
        /* We cannot map a tiled texture directly because the data is
         * in a different order, therefore we do detiling using a blit.
         *
@@ -1019,27 +1242,19 @@ static void *r600_texture_transfer_map(struct pipe_context *ctx,
         */
        if (rtex->surface.level[0].mode >= RADEON_SURF_MODE_1D) {
                use_staging_texture = TRUE;
-       } else if ((usage & PIPE_TRANSFER_READ) && !(usage & PIPE_TRANSFER_MAP_DIRECTLY) &&
-           (rtex->resource.domains == RADEON_DOMAIN_VRAM)) {
+       } else if ((usage & PIPE_TRANSFER_READ) &&
+                  rtex->resource.domains & RADEON_DOMAIN_VRAM) {
                /* Untiled buffers in VRAM, which is slow for CPU reads */
                use_staging_texture = TRUE;
        } else if (!(usage & PIPE_TRANSFER_READ) &&
-           (r600_rings_is_buffer_referenced(rctx, rtex->resource.cs_buf, RADEON_USAGE_READWRITE) ||
+           (r600_rings_is_buffer_referenced(rctx, rtex->resource.buf, RADEON_USAGE_READWRITE) ||
             !rctx->ws->buffer_wait(rtex->resource.buf, 0, RADEON_USAGE_READWRITE))) {
                /* Use a staging texture for uploads if the underlying BO is busy. */
                use_staging_texture = TRUE;
        }
 
-       if (texture->flags & R600_RESOURCE_FLAG_TRANSFER) {
-               use_staging_texture = FALSE;
-       }
-
-       if (use_staging_texture && (usage & PIPE_TRANSFER_MAP_DIRECTLY)) {
-               return NULL;
-       }
-
        trans = CALLOC_STRUCT(r600_transfer);
-       if (trans == NULL)
+       if (!trans)
                return NULL;
        trans->transfer.resource = texture;
        trans->transfer.level = level;
@@ -1104,6 +1319,7 @@ static void *r600_texture_transfer_map(struct pipe_context *ctx,
                trans->transfer.stride = staging_depth->surface.level[level].pitch_bytes;
                trans->transfer.layer_stride = staging_depth->surface.level[level].slice_size;
                trans->staging = (struct r600_resource*)staging_depth;
+               buf = trans->staging;
        } else if (use_staging_texture) {
                struct pipe_resource resource;
                struct r600_texture *staging;
@@ -1115,7 +1331,7 @@ static void *r600_texture_transfer_map(struct pipe_context *ctx,
 
                /* Create the temporary texture. */
                staging = (struct r600_texture*)ctx->screen->resource_create(ctx->screen, &resource);
-               if (staging == NULL) {
+               if (!staging) {
                        R600_ERR("failed to create temporary texture to hold untiled copy\n");
                        FREE(trans);
                        return NULL;
@@ -1123,21 +1339,18 @@ static void *r600_texture_transfer_map(struct pipe_context *ctx,
                trans->staging = &staging->resource;
                trans->transfer.stride = staging->surface.level[0].pitch_bytes;
                trans->transfer.layer_stride = staging->surface.level[0].slice_size;
-               if (usage & PIPE_TRANSFER_READ) {
+
+               if (usage & PIPE_TRANSFER_READ)
                        r600_copy_to_staging_texture(ctx, trans);
-               }
+               else
+                       usage |= PIPE_TRANSFER_UNSYNCHRONIZED;
+
+               buf = trans->staging;
        } else {
                /* the resource is mapped directly */
                trans->transfer.stride = rtex->surface.level[level].pitch_bytes;
                trans->transfer.layer_stride = rtex->surface.level[level].slice_size;
                offset = r600_texture_get_offset(rtex, level, box);
-       }
-
-       if (trans->staging) {
-               buf = trans->staging;
-               if (!rtex->is_depth && !(usage & PIPE_TRANSFER_READ))
-                       usage |= PIPE_TRANSFER_UNSYNCHRONIZED;
-       } else {
                buf = &rtex->resource;
        }
 
@@ -1190,9 +1403,10 @@ struct pipe_surface *r600_create_surface_custom(struct pipe_context *pipe,
                                                const struct pipe_surface *templ,
                                                unsigned width, unsigned height)
 {
+       struct r600_texture *rtex = (struct r600_texture*)texture;
        struct r600_surface *surface = CALLOC_STRUCT(r600_surface);
 
-       if (surface == NULL)
+       if (!surface)
                return NULL;
 
        assert(templ->u.tex.first_layer <= util_max_layer(texture, templ->u.tex.level));
@@ -1205,6 +1419,7 @@ struct pipe_surface *r600_create_surface_custom(struct pipe_context *pipe,
        surface->base.width = width;
        surface->base.height = height;
        surface->base.u = templ->u;
+       surface->level_info = &rtex->surface.level[templ->u.tex.level];
        return &surface->base;
 }
 
@@ -1213,10 +1428,30 @@ static struct pipe_surface *r600_create_surface(struct pipe_context *pipe,
                                                const struct pipe_surface *templ)
 {
        unsigned level = templ->u.tex.level;
+       unsigned width = u_minify(tex->width0, level);
+       unsigned height = u_minify(tex->height0, level);
+
+       if (tex->target != PIPE_BUFFER && templ->format != tex->format) {
+               const struct util_format_description *tex_desc
+                       = util_format_description(tex->format);
+               const struct util_format_description *templ_desc
+                       = util_format_description(templ->format);
+
+               assert(tex_desc->block.bits == templ_desc->block.bits);
+
+               /* Adjust size of surface if and only if the block width or
+                * height is changed. */
+               if (tex_desc->block.width != templ_desc->block.width ||
+                   tex_desc->block.height != templ_desc->block.height) {
+                       unsigned nblks_x = util_format_get_nblocksx(tex->format, width);
+                       unsigned nblks_y = util_format_get_nblocksy(tex->format, height);
+
+                       width = nblks_x * templ_desc->block.width;
+                       height = nblks_y * templ_desc->block.height;
+               }
+       }
 
-       return r600_create_surface_custom(pipe, tex, templ,
-                                         u_minify(tex->width0, level),
-                                         u_minify(tex->height0, level));
+       return r600_create_surface_custom(pipe, tex, templ, width, height);
 }
 
 static void r600_surface_destroy(struct pipe_context *pipe,
@@ -1229,11 +1464,11 @@ static void r600_surface_destroy(struct pipe_context *pipe,
        FREE(surface);
 }
 
-unsigned r600_translate_colorswap(enum pipe_format format)
+unsigned r600_translate_colorswap(enum pipe_format format, bool do_endian_swap)
 {
        const struct util_format_description *desc = util_format_description(format);
 
-#define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == UTIL_FORMAT_SWIZZLE_##swz)
+#define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == PIPE_SWIZZLE_##swz)
 
        if (format == PIPE_FORMAT_R11G11B10_FLOAT) /* isn't plain */
                return V_0280A0_SWAP_STD;
@@ -1256,7 +1491,8 @@ unsigned r600_translate_colorswap(enum pipe_format format)
                else if ((HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,X)) ||
                         (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,NONE)) ||
                         (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,X)))
-                       return V_0280A0_SWAP_STD_REV; /* YX__ */
+                       /* YX__ */
+                       return (do_endian_swap ? V_0280A0_SWAP_STD : V_0280A0_SWAP_STD_REV);
                else if (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(3,Y))
                        return V_0280A0_SWAP_ALT; /* X__Y */
                else if (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(3,X))
@@ -1264,20 +1500,25 @@ unsigned r600_translate_colorswap(enum pipe_format format)
                break;
        case 3:
                if (HAS_SWIZZLE(0,X))
-                       return V_0280A0_SWAP_STD; /* XYZ */
+                       return (do_endian_swap ? V_0280A0_SWAP_STD_REV : V_0280A0_SWAP_STD);
                else if (HAS_SWIZZLE(0,Z))
                        return V_0280A0_SWAP_STD_REV; /* ZYX */
                break;
        case 4:
                /* check the middle channels, the 1st and 4th channel can be NONE */
-               if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,Z))
+               if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,Z)) {
                        return V_0280A0_SWAP_STD; /* XYZW */
-               else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,Y))
+               } else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,Y)) {
                        return V_0280A0_SWAP_STD_REV; /* WZYX */
-               else if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,X))
+               } else if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,X)) {
                        return V_0280A0_SWAP_ALT; /* ZYXW */
-               else if (HAS_SWIZZLE(1,X) && HAS_SWIZZLE(2,Y))
-                       return V_0280A0_SWAP_ALT_REV; /* WXYZ */
+               } else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,W)) {
+                       /* YZWX */
+                       if (desc->is_array)
+                               return V_0280A0_SWAP_ALT_REV;
+                       else
+                               return (do_endian_swap ? V_0280A0_SWAP_ALT : V_0280A0_SWAP_ALT_REV);
+               }
                break;
        }
        return ~0U;
@@ -1328,7 +1569,7 @@ static void vi_get_fast_clear_parameters(enum pipe_format surface_format,
            surface_format == PIPE_FORMAT_B5G6R5_SRGB) {
                extra_channel = -1;
        } else if (desc->layout == UTIL_FORMAT_LAYOUT_PLAIN) {
-               if(r600_translate_colorswap(surface_format) <= 1)
+               if(r600_translate_colorswap(surface_format, FALSE) <= 1)
                        extra_channel = desc->nr_channels - 1;
                else
                        extra_channel = 0;
@@ -1336,10 +1577,10 @@ static void vi_get_fast_clear_parameters(enum pipe_format surface_format,
                return;
 
        for (i = 0; i < 4; ++i) {
-               int index = desc->swizzle[i] - UTIL_FORMAT_SWIZZLE_X;
+               int index = desc->swizzle[i] - PIPE_SWIZZLE_X;
 
-               if (desc->swizzle[i] < UTIL_FORMAT_SWIZZLE_X ||
-                   desc->swizzle[i] > UTIL_FORMAT_SWIZZLE_W)
+               if (desc->swizzle[i] < PIPE_SWIZZLE_X ||
+                   desc->swizzle[i] > PIPE_SWIZZLE_W)
                        continue;
 
                if (util_format_is_pure_sint(surface_format)) {
@@ -1364,9 +1605,9 @@ static void vi_get_fast_clear_parameters(enum pipe_format surface_format,
 
        for (int i = 0; i < 4; ++i)
                if (values[i] != main_value &&
-                   desc->swizzle[i] - UTIL_FORMAT_SWIZZLE_X != extra_channel &&
-                   desc->swizzle[i] >= UTIL_FORMAT_SWIZZLE_X &&
-                   desc->swizzle[i] <= UTIL_FORMAT_SWIZZLE_W)
+                   desc->swizzle[i] - PIPE_SWIZZLE_X != extra_channel &&
+                   desc->swizzle[i] >= PIPE_SWIZZLE_X &&
+                   desc->swizzle[i] <= PIPE_SWIZZLE_W)
                        return;
 
        *clear_words_needed = false;
@@ -1385,6 +1626,11 @@ void evergreen_do_fast_color_clear(struct r600_common_context *rctx,
 {
        int i;
 
+       /* This function is broken in BE, so just disable this path for now */
+#ifdef PIPE_ARCH_BIG_ENDIAN
+       return;
+#endif
+
        if (rctx->render_cond)
                return;
 
@@ -1422,6 +1668,14 @@ void evergreen_do_fast_color_clear(struct r600_common_context *rctx,
                        continue;
                }
 
+               /* shared textures can't use fast clear without an explicit flush,
+                * because there is no way to communicate the clear color among
+                * all clients
+                */
+               if (tex->resource.is_shared &&
+                   !(tex->resource.external_usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH))
+                       continue;
+
                /* fast color clear with 1D tiling doesn't work on old kernels and CIK */
                if (tex->surface.level[0].mode == RADEON_SURF_MODE_1D &&
                    rctx->chip_class >= CIK &&
@@ -1430,7 +1684,7 @@ void evergreen_do_fast_color_clear(struct r600_common_context *rctx,
                        continue;
                }
 
-               if (tex->dcc_buffer) {
+               if (tex->dcc_offset) {
                        uint32_t reset_value;
                        bool clear_words_needed;
 
@@ -1439,12 +1693,17 @@ void evergreen_do_fast_color_clear(struct r600_common_context *rctx,
 
                        vi_get_fast_clear_parameters(fb->cbufs[i]->format, color, &reset_value, &clear_words_needed);
 
-                       rctx->clear_buffer(&rctx->b, &tex->dcc_buffer->b.b,
-                                       0, tex->surface.dcc_size, reset_value, true);
+                       rctx->clear_buffer(&rctx->b, &tex->resource.b.b,
+                                          tex->dcc_offset, tex->surface.dcc_size,
+                                          reset_value, R600_COHERENCY_CB_META);
 
                        if (clear_words_needed)
                                tex->dirty_level_mask |= 1 << fb->cbufs[i]->u.tex.level;
                } else {
+                       /* Stoney/RB+ doesn't work with CMASK fast clear. */
+                       if (rctx->family == CHIP_STONEY)
+                               continue;
+
                        /* ensure CMASK is enabled */
                        r600_texture_alloc_cmask_separate(rctx->screen, tex);
                        if (tex->cmask.size == 0) {
@@ -1453,7 +1712,8 @@ void evergreen_do_fast_color_clear(struct r600_common_context *rctx,
 
                        /* Do the fast clear. */
                        rctx->clear_buffer(&rctx->b, &tex->cmask_buffer->b.b,
-                                       tex->cmask.offset, tex->cmask.size, 0, true);
+                                          tex->cmask.offset, tex->cmask.size, 0,
+                                          R600_COHERENCY_CB_META);
 
                        tex->dirty_level_mask |= 1 << fb->cbufs[i]->u.tex.level;
                }