radeonsi: add an si_set_rw_shader_buffer convenience function
[mesa.git] / src / gallium / drivers / r600 / r600_texture.c
index 3515d979d9ef5e25ec1457fea80f5ff39303f60d..71606df38d93a4656ba2377e08b1a007c65b7c0e 100644 (file)
@@ -178,7 +178,8 @@ static unsigned r600_texture_get_offset(struct r600_common_screen *rscreen,
 {
        *stride = rtex->surface.u.legacy.level[level].nblk_x *
                rtex->surface.bpe;
-       *layer_stride = rtex->surface.u.legacy.level[level].slice_size;
+       assert((uint64_t)rtex->surface.u.legacy.level[level].slice_size_dw * 4 <= UINT_MAX);
+       *layer_stride = (uint64_t)rtex->surface.u.legacy.level[level].slice_size_dw * 4;
 
        if (!box)
                return rtex->surface.u.legacy.level[level].offset;
@@ -186,7 +187,7 @@ static unsigned r600_texture_get_offset(struct r600_common_screen *rscreen,
        /* Each texture is an array of mipmap levels. Each level is
         * an array of slices. */
        return rtex->surface.u.legacy.level[level].offset +
-               box->z * rtex->surface.u.legacy.level[level].slice_size +
+               box->z * (uint64_t)rtex->surface.u.legacy.level[level].slice_size_dw * 4 +
                (box->y / rtex->surface.blk_h *
                 rtex->surface.u.legacy.level[level].nblk_x +
                 box->x / rtex->surface.blk_w) * rtex->surface.bpe;
@@ -216,7 +217,7 @@ static int r600_init_surface(struct r600_common_screen *rscreen,
                bpe = 4; /* stencil is allocated separately on evergreen */
        } else {
                bpe = util_format_get_blocksize(ptex->format);
-               assert(util_is_power_of_two(bpe));
+               assert(util_is_power_of_two_or_zero(bpe));
        }
 
        if (!is_flushed_depth && is_depth) {
@@ -244,8 +245,8 @@ static int r600_init_surface(struct r600_common_screen *rscreen,
        if (!(ptex->flags & R600_RESOURCE_FLAG_FORCE_TILING))
                flags |= RADEON_SURF_OPTIMIZE_FOR_SPACE;
 
-       r = rscreen->ws->surface_init(rscreen->ws, ptex, flags, bpe,
-                                     array_mode, surface);
+       r = rscreen->ws->surface_init(rscreen->ws, ptex,
+                                     flags, bpe, array_mode, surface);
        if (r) {
                return r;
        }
@@ -256,8 +257,8 @@ static int r600_init_surface(struct r600_common_screen *rscreen,
                 * for those
                 */
                surface->u.legacy.level[0].nblk_x = pitch_in_bytes_override / bpe;
-               surface->u.legacy.level[0].slice_size = pitch_in_bytes_override *
-                       surface->u.legacy.level[0].nblk_y;
+               surface->u.legacy.level[0].slice_size_dw =
+                       ((uint64_t)pitch_in_bytes_override * surface->u.legacy.level[0].nblk_y) / 4;
        }
 
        if (offset) {
@@ -392,7 +393,7 @@ static void r600_reallocate_texture_inplace(struct r600_common_context *rctx,
 
                        u_box_3d(0, 0, 0,
                                 u_minify(templ.width0, i), u_minify(templ.height0, i),
-                                util_max_layer(&templ, i) + 1, &box);
+                                util_num_layers(&templ, i), &box);
 
                        rctx->dma_copy(&rctx->b, &new_tex->resource.b.b, i, 0, 0, 0,
                                       &rtex->resource.b.b, i, &box);
@@ -502,7 +503,7 @@ static boolean r600_texture_get_handle(struct pipe_screen* screen,
                offset = rtex->surface.u.legacy.level[0].offset;
                stride = rtex->surface.u.legacy.level[0].nblk_x *
                        rtex->surface.bpe;
-               slice_size = rtex->surface.u.legacy.level[0].slice_size;
+               slice_size = (uint64_t)rtex->surface.u.legacy.level[0].slice_size_dw * 4;
        } else {
                /* Move a suballocated buffer into a non-suballocated allocation. */
                if (rscreen->ws->buffer_is_suballocated(res->buf)) {
@@ -559,6 +560,7 @@ static void r600_texture_destroy(struct pipe_screen *screen,
        struct r600_resource *resource = &rtex->resource;
 
        r600_texture_reference(&rtex->flushed_depth_texture, NULL);
+       pipe_resource_reference((struct pipe_resource**)&resource->immed_buffer, NULL);
 
        if (rtex->cmask_buffer != &rtex->resource) {
            r600_resource_reference(&rtex->cmask_buffer, NULL);
@@ -614,8 +616,8 @@ void r600_texture_get_fmask_info(struct r600_common_screen *rscreen,
                bpe *= 2;
        }
 
-       if (rscreen->ws->surface_init(rscreen->ws, &templ, flags, bpe,
-                                     RADEON_SURF_MODE_2D, &fmask)) {
+       if (rscreen->ws->surface_init(rscreen->ws, &templ,
+                                     flags, bpe, RADEON_SURF_MODE_2D, &fmask)) {
                R600_ERR("Got error in surface_init while allocating FMASK.\n");
                return;
        }
@@ -674,7 +676,7 @@ void r600_texture_get_cmask_info(struct r600_common_screen *rscreen,
 
        out->slice_tile_max = ((pitch_elements * height) / (128*128)) - 1;
        out->alignment = MAX2(256, base_align);
-       out->size = (util_max_layer(&rtex->resource.b.b, 0) + 1) *
+       out->size = util_num_layers(&rtex->resource.b.b, 0) *
                    align(slice_bytes, base_align);
 }
 
@@ -718,6 +720,15 @@ static void r600_texture_alloc_cmask_separate(struct r600_common_screen *rscreen
        p_atomic_inc(&rscreen->compressed_colortex_counter);
 }
 
+void eg_resource_alloc_immed(struct r600_common_screen *rscreen,
+                            struct r600_resource *res,
+                            unsigned immed_size)
+{
+       res->immed_buffer = (struct r600_resource *)
+               pipe_buffer_create(&rscreen->b, PIPE_BIND_CUSTOM,
+                                  PIPE_USAGE_DEFAULT, immed_size);
+}
+
 static void r600_texture_get_htile_size(struct r600_common_screen *rscreen,
                                        struct r600_texture *rtex)
 {
@@ -763,8 +774,8 @@ static void r600_texture_get_htile_size(struct r600_common_screen *rscreen,
                return;
        }
 
-       width = align(rtex->resource.b.b.width0, cl_width * 8);
-       height = align(rtex->resource.b.b.height0, cl_height * 8);
+       width = align(rtex->surface.u.legacy.level[0].nblk_x, cl_width * 8);
+       height = align(rtex->surface.u.legacy.level[0].nblk_y, cl_height * 8);
 
        slice_elements = (width * height) / (8 * 8);
        slice_bytes = slice_elements * 4;
@@ -774,7 +785,7 @@ static void r600_texture_get_htile_size(struct r600_common_screen *rscreen,
 
        rtex->surface.htile_alignment = base_align;
        rtex->surface.htile_size =
-               (util_max_layer(&rtex->resource.b.b, 0) + 1) *
+               util_num_layers(&rtex->resource.b.b, 0) *
                align(slice_bytes, base_align);
 }
 
@@ -827,7 +838,7 @@ void r600_print_texture_info(struct r600_common_screen *rscreen,
                        rtex->cmask.slice_tile_max);
 
        if (rtex->htile_offset)
-               u_log_printf(log, "  HTile: offset=%"PRIu64", size=%"PRIu64", "
+               u_log_printf(log, "  HTile: offset=%"PRIu64", size=%u "
                        "alignment=%u\n",
                             rtex->htile_offset, rtex->surface.htile_size,
                             rtex->surface.htile_alignment);
@@ -837,7 +848,7 @@ void r600_print_texture_info(struct r600_common_screen *rscreen,
                        "npix_x=%u, npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
                        "mode=%u, tiling_index = %u\n",
                        i, rtex->surface.u.legacy.level[i].offset,
-                       rtex->surface.u.legacy.level[i].slice_size,
+                       (uint64_t)rtex->surface.u.legacy.level[i].slice_size_dw * 4,
                        u_minify(rtex->resource.b.b.width0, i),
                        u_minify(rtex->resource.b.b.height0, i),
                        u_minify(rtex->resource.b.b.depth0, i),
@@ -855,7 +866,7 @@ void r600_print_texture_info(struct r600_common_screen *rscreen,
                                "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
                                "mode=%u, tiling_index = %u\n",
                                i, rtex->surface.u.legacy.stencil_level[i].offset,
-                               rtex->surface.u.legacy.stencil_level[i].slice_size,
+                               (uint64_t)rtex->surface.u.legacy.stencil_level[i].slice_size_dw * 4,
                                u_minify(rtex->resource.b.b.width0, i),
                                u_minify(rtex->resource.b.b.height0, i),
                                u_minify(rtex->resource.b.b.depth0, i),
@@ -942,10 +953,6 @@ r600_texture_create_object(struct pipe_screen *screen,
                r600_init_resource_fields(rscreen, resource, rtex->size,
                                          rtex->surface.surf_alignment);
 
-               /* Displayable surfaces are not suballocated. */
-               if (resource->b.b.bind & PIPE_BIND_SCANOUT)
-                       resource->flags |= RADEON_FLAG_NO_SUBALLOC;
-
                if (!r600_alloc_resource(rscreen, resource)) {
                        FREE(rtex);
                        return NULL;
@@ -985,7 +992,7 @@ r600_texture_create_object(struct pipe_screen *screen,
                fprintf(stderr, "VM start=0x%"PRIX64"  end=0x%"PRIX64" | Texture %ix%ix%i, %i levels, %i samples, %s\n",
                        rtex->resource.gpu_address,
                        rtex->resource.gpu_address + rtex->resource.buf->size,
-                       base->width0, base->height0, util_max_layer(base, 0)+1, base->last_level+1,
+                       base->width0, base->height0, util_num_layers(base, 0), base->last_level+1,
                        base->nr_samples ? base->nr_samples : 1, util_format_short_name(base->format));
        }
 
@@ -1042,12 +1049,9 @@ r600_choose_tiling(struct r600_common_screen *rscreen,
                if (templ->bind & PIPE_BIND_LINEAR)
                        return RADEON_SURF_MODE_LINEAR_ALIGNED;
 
-               /* Textures with a very small height are recommended to be linear. */
+               /* 1D textures should be linear - fixes image operations on 1d */
                if (templ->target == PIPE_TEXTURE_1D ||
-                   templ->target == PIPE_TEXTURE_1D_ARRAY ||
-                   /* Only very thin and long 2D textures should benefit from
-                    * linear_aligned. */
-                   (templ->width0 > 8 && templ->height0 <= 2))
+                   templ->target == PIPE_TEXTURE_1D_ARRAY)
                        return RADEON_SURF_MODE_LINEAR_ALIGNED;
 
                /* Textures likely to be mapped often. */
@@ -1104,7 +1108,9 @@ static struct pipe_resource *r600_texture_from_handle(struct pipe_screen *screen
              templ->depth0 != 1 || templ->last_level != 0)
                return NULL;
 
-       buf = rscreen->ws->buffer_from_handle(rscreen->ws, whandle, &stride, &offset);
+       buf = rscreen->ws->buffer_from_handle(rscreen->ws, whandle,
+                                             rscreen->info.max_alignment,
+                                             &stride, &offset);
        if (!buf)
                return NULL;
 
@@ -1489,7 +1495,7 @@ static void r600_texture_transfer_unmap(struct pipe_context *ctx,
         * The result is that the kernel memory manager is never a bottleneck.
         */
        if (rctx->num_alloc_tex_transfer_bytes > rctx->screen->info.gart_size / 4) {
-               rctx->gfx.flush(rctx, RADEON_FLUSH_ASYNC, NULL);
+               rctx->gfx.flush(rctx, PIPE_FLUSH_ASYNC, NULL);
                rctx->num_alloc_tex_transfer_bytes = 0;
        }
 
@@ -1632,7 +1638,7 @@ static void r600_clear_texture(struct pipe_context *pipe,
                        desc->unpack_rgba_float(color.f, 0, data, 0, 1, 1);
 
                if (screen->is_format_supported(screen, tex->format,
-                                               tex->target, 0,
+                                               tex->target, 0, 0,
                                                PIPE_BIND_RENDER_TARGET)) {
                        pipe->clear_render_target(pipe, sf, &color,
                                                  box->x, box->y,
@@ -1790,6 +1796,16 @@ void evergreen_do_fast_color_clear(struct r600_common_context *rctx,
                    !(tex->resource.external_usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH))
                        continue;
 
+               /* Use a slow clear for small surfaces where the cost of
+                * the eliminate pass can be higher than the benefit of fast
+                * clear. AMDGPU-pro does this, but the numbers may differ.
+                *
+                * This helps on both dGPUs and APUs, even small ones.
+                */
+               if (tex->resource.b.b.nr_samples <= 1 &&
+                   tex->resource.b.b.width0 * tex->resource.b.b.height0 <= 300 * 300)
+                       continue;
+
                {
                        /* 128-bit formats are unusupported */
                        if (tex->surface.bpe > 8) {
@@ -1838,6 +1854,7 @@ r600_memobj_from_handle(struct pipe_screen *screen,
                return NULL;
 
        buf = rscreen->ws->buffer_from_handle(rscreen->ws, whandle,
+                                             rscreen->info.max_alignment,
                                              &stride, &offset);
        if (!buf) {
                free(memobj);
@@ -1928,7 +1945,7 @@ r600_texture_from_memobj(struct pipe_screen *screen,
        pb_reference(&buf, memobj->buf);
 
        rtex->resource.b.is_shared = true;
-       rtex->resource.external_usage = PIPE_HANDLE_USAGE_READ_WRITE;
+       rtex->resource.external_usage = PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE;
 
        if (rscreen->apply_opaque_metadata)
                rscreen->apply_opaque_metadata(rscreen, rtex, &metadata);