[Mesa-dev] [PATCH 09/20] panfrost: Merge AFBC slab with BO backing

Alyssa Rosenzweig alyssa.rosenzweig at collabora.com
Mon Jun 24 17:39:12 UTC 2019


Rather than tracking AFBC memory "specially", just use the same codepath
as linear and tiled. Less things to mess up, I figure. This allows us to
use the standard setup_slices() call with AFBC resources, allowing
mipmapped AFBC resources.

Unfortunately, we do have to disable AFBC (and checksumming) in the
meantime to avoid functional regressions, as we don't know _a priori_ if
we'll need to access a resource from software (which is not yet hooked
up with AFBC) and we don't yet have routines to switch the layout of a
BO at runtime.

Signed-off-by: Alyssa Rosenzweig <alyssa.rosenzweig at collabora.com>
---
 src/gallium/drivers/panfrost/pan_afbc.c     | 43 ++++-----------------
 src/gallium/drivers/panfrost/pan_context.c  | 26 +++----------
 src/gallium/drivers/panfrost/pan_mfbd.c     | 23 +++++------
 src/gallium/drivers/panfrost/pan_resource.c | 29 +++++++-------
 src/gallium/drivers/panfrost/pan_resource.h | 16 +++-----
 5 files changed, 46 insertions(+), 91 deletions(-)

diff --git a/src/gallium/drivers/panfrost/pan_afbc.c b/src/gallium/drivers/panfrost/pan_afbc.c
index 5621d1f333a..c9487b5e943 100644
--- a/src/gallium/drivers/panfrost/pan_afbc.c
+++ b/src/gallium/drivers/panfrost/pan_afbc.c
@@ -101,22 +101,9 @@ panfrost_format_supports_afbc(enum pipe_format format)
         return false;
 }
 
-/* AFBC is enabled on a per-resource basis (AFBC enabling is theoretically
- * indepdent between color buffers and depth/stencil). To enable, we allocate
- * the AFBC metadata buffer and mark that it is enabled. We do -not- actually
- * edit the fragment job here. This routine should be called ONCE per
- * AFBC-compressed buffer, rather than on every frame. */
-
-void
-panfrost_enable_afbc(struct panfrost_context *ctx, struct panfrost_resource *rsrc, bool ds)
+unsigned
+panfrost_afbc_header_size(unsigned width, unsigned height)
 {
-        struct pipe_context *gallium = (struct pipe_context *) ctx;
-        struct panfrost_screen *screen = pan_screen(gallium->screen);
-
-        unsigned width  = rsrc->base.width0;
-        unsigned height = rsrc->base.height0;
-        unsigned bytes_per_pixel = util_format_get_blocksize(rsrc->base.format);
-
         /* Align to tile */
         unsigned aligned_width  = ALIGN(width,  AFBC_TILE_WIDTH);
         unsigned aligned_height = ALIGN(height, AFBC_TILE_HEIGHT);
@@ -126,26 +113,10 @@ panfrost_enable_afbc(struct panfrost_context *ctx, struct panfrost_resource *rsr
         unsigned tile_count_y = aligned_height / AFBC_TILE_HEIGHT;
         unsigned tile_count = tile_count_x * tile_count_y;
 
+        /* Multiply to find the header size */
         unsigned header_bytes = tile_count * AFBC_HEADER_BYTES_PER_TILE;
-        unsigned header_size = ALIGN(header_bytes, AFBC_CACHE_ALIGN);
-
-        /* The stride is a normal stride, but aligned */
-        unsigned unaligned_stride = aligned_width * bytes_per_pixel;
-        unsigned stride = ALIGN(unaligned_stride, AFBC_CACHE_ALIGN);
-
-        /* Compute the entire buffer size */
-        unsigned body_size = stride * aligned_height;
-        unsigned buffer_size = header_size + body_size;
-
-        /* Allocate the AFBC slab itself, large enough to hold the above */
-        panfrost_drm_allocate_slab(screen, &rsrc->bo->afbc_slab,
-                               ALIGN(buffer_size, 4096) / 4096,
-                               true, 0, 0, 0);
-
-        /* Compressed textured reads use a tagged pointer to the metadata */
-        rsrc->bo->layout = PAN_AFBC;
-        rsrc->bo->gpu = rsrc->bo->afbc_slab.gpu | (ds ? 0 : 1);
-        rsrc->bo->cpu = rsrc->bo->afbc_slab.cpu;
-        rsrc->bo->gem_handle = rsrc->bo->afbc_slab.gem_handle;
-        rsrc->bo->afbc_metadata_size = header_size;
+
+        /* Align and go */
+        return ALIGN(header_bytes, AFBC_CACHE_ALIGN);
+
 }
diff --git a/src/gallium/drivers/panfrost/pan_context.c b/src/gallium/drivers/panfrost/pan_context.c
index 6bda78e96dc..28a1f598934 100644
--- a/src/gallium/drivers/panfrost/pan_context.c
+++ b/src/gallium/drivers/panfrost/pan_context.c
@@ -813,7 +813,12 @@ panfrost_get_texture_address(
         unsigned level_offset = rsrc->bo->slices[level].offset;
         unsigned face_offset = face * rsrc->bo->cubemap_stride;
 
-        return rsrc->bo->gpu + level_offset + face_offset;
+        /* Lower-bit is set when sampling from colour AFBC */
+        bool is_afbc = rsrc->bo->layout == PAN_AFBC;
+        bool is_zs = rsrc->base.bind & PIPE_BIND_DEPTH_STENCIL;
+        unsigned afbc_bit = (is_afbc && !is_zs) ? 1 : 0;
+
+        return rsrc->bo->gpu + level_offset + face_offset + afbc_bit;
 
 }
 
@@ -2213,18 +2218,6 @@ panfrost_set_framebuffer_state(struct pipe_context *pctx,
                         ctx->vt_framebuffer_mfbd = panfrost_emit_mfbd(ctx, ~0);
 
                 panfrost_attach_vt_framebuffer(ctx);
-
-                struct panfrost_resource *tex = ((struct panfrost_resource *) ctx->pipe_framebuffer.cbufs[i]->texture);
-                enum pipe_format format = ctx->pipe_framebuffer.cbufs[i]->format;
-
-                bool can_afbc = panfrost_format_supports_afbc(format);
-                bool is_scanout = panfrost_is_scanout(ctx);
-
-                if (!is_scanout && tex->bo->layout != PAN_AFBC && can_afbc)
-                        panfrost_enable_afbc(ctx, tex, false);
-
-                if (!is_scanout && !tex->bo->has_checksum)
-                        panfrost_enable_checksum(ctx, tex);
         }
 
         {
@@ -2240,13 +2233,6 @@ panfrost_set_framebuffer_state(struct pipe_context *pctx,
                                         ctx->vt_framebuffer_mfbd = panfrost_emit_mfbd(ctx, ~0);
 
                                 panfrost_attach_vt_framebuffer(ctx);
-
-                                struct panfrost_resource *tex = pan_resource(zb->texture);
-                                bool can_afbc = panfrost_format_supports_afbc(zb->format);
-                                bool is_scanout = panfrost_is_scanout(ctx);
-
-                                if (!is_scanout && tex->bo->layout != PAN_AFBC && can_afbc)
-                                        panfrost_enable_afbc(ctx, tex, true);
                         }
                 }
         }
diff --git a/src/gallium/drivers/panfrost/pan_mfbd.c b/src/gallium/drivers/panfrost/pan_mfbd.c
index c082d0b91a3..0ebfecc1200 100644
--- a/src/gallium/drivers/panfrost/pan_mfbd.c
+++ b/src/gallium/drivers/panfrost/pan_mfbd.c
@@ -112,15 +112,15 @@ panfrost_mfbd_set_cbuf(
                 rt->framebuffer = rsrc->bo->gpu + offset;
                 rt->framebuffer_stride = stride;
         } else if (rsrc->bo->layout == PAN_AFBC) {
-                assert(level == 0);
-                rt->afbc.metadata = rsrc->bo->afbc_slab.gpu;
-                rt->afbc.stride = 0;
-                rt->afbc.unk = 0x30009;
-
                 rt->format.block = MALI_MFBD_BLOCK_AFBC;
 
-                mali_ptr afbc_main = rsrc->bo->afbc_slab.gpu + rsrc->bo->afbc_metadata_size;
-                rt->framebuffer = afbc_main;
+                mali_ptr base = rsrc->bo->gpu + offset;
+                unsigned header_size = rsrc->bo->slices[level].header_size;
+
+                rt->framebuffer = base + header_size;
+                rt->afbc.metadata = base;
+                rt->afbc.stride = 0;
+                rt->afbc.unk = 0x30009;
 
                 /* TODO: Investigate shift */
                 rt->framebuffer_stride = stride << 1;
@@ -144,7 +144,9 @@ panfrost_mfbd_set_zsbuf(
         unsigned offset = rsrc->bo->slices[level].offset;
 
         if (rsrc->bo->layout == PAN_AFBC) {
-                assert(level == 0);
+                mali_ptr base = rsrc->bo->gpu + offset;
+                unsigned header_size = rsrc->bo->slices[level].header_size;
+
                 fb->mfbd_flags |= MALI_MFBD_EXTRA;
 
                 fbx->flags =
@@ -154,11 +156,10 @@ panfrost_mfbd_set_zsbuf(
                         MALI_EXTRA_ZS |
                         0x1; /* unknown */
 
-                fbx->ds_afbc.depth_stencil_afbc_metadata = rsrc->bo->afbc_slab.gpu;
+                fbx->ds_afbc.depth_stencil = base + header_size;
+                fbx->ds_afbc.depth_stencil_afbc_metadata = base;
                 fbx->ds_afbc.depth_stencil_afbc_stride = 0;
 
-                fbx->ds_afbc.depth_stencil = rsrc->bo->afbc_slab.gpu + rsrc->bo->afbc_metadata_size;
-
                 fbx->ds_afbc.zero1 = 0x10009;
                 fbx->ds_afbc.padding = 0x1000;
         } else if (rsrc->bo->layout == PAN_LINEAR) {
diff --git a/src/gallium/drivers/panfrost/pan_resource.c b/src/gallium/drivers/panfrost/pan_resource.c
index 961cecf8cf0..d8f1a9b521f 100644
--- a/src/gallium/drivers/panfrost/pan_resource.c
+++ b/src/gallium/drivers/panfrost/pan_resource.c
@@ -197,6 +197,7 @@ panfrost_setup_slices(const struct pipe_resource *tmpl, struct panfrost_bo *bo)
 
         bool renderable = tmpl->bind &
                 (PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL);
+        bool afbc = bo->layout == PAN_AFBC;
         bool tiled = bo->layout == PAN_TILED;
         bool should_align = renderable || tiled;
 
@@ -243,6 +244,14 @@ panfrost_setup_slices(const struct pipe_resource *tmpl, struct panfrost_bo *bo)
                 if (l == 0)
                         size_2d = slice_one_size;
 
+                /* Compute AFBC sizes if necessary */
+                if (afbc) {
+                        slice->header_size =
+                                panfrost_afbc_header_size(width, height);
+
+                        offset += slice->header_size;
+                }
+
                 offset += slice_full_size;
 
                 width = u_minify(width, 1);
@@ -303,15 +312,13 @@ panfrost_create_bo(struct panfrost_screen *screen, const struct pipe_resource *t
 
         panfrost_setup_slices(template, bo);
 
-        if (bo->layout == PAN_TILED || bo->layout == PAN_LINEAR) {
-                struct panfrost_memory mem;
+        struct panfrost_memory mem;
 
-                panfrost_drm_allocate_slab(screen, &mem, bo->size / 4096, true, 0, 0, 0);
+        panfrost_drm_allocate_slab(screen, &mem, bo->size / 4096, true, 0, 0, 0);
 
-                bo->cpu = mem.cpu;
-                bo->gpu = mem.gpu;
-                bo->gem_handle = mem.gem_handle;
-        }
+        bo->cpu = mem.cpu;
+        bo->gpu = mem.gpu;
+        bo->gem_handle = mem.gem_handle;
 
         return bo;
 }
@@ -378,8 +385,7 @@ panfrost_resource_create(struct pipe_screen *screen,
 static void
 panfrost_destroy_bo(struct panfrost_screen *screen, struct panfrost_bo *bo)
 {
-        if ((bo->layout == PAN_LINEAR || bo->layout == PAN_TILED) &&
-                        !bo->imported) {
+        if (!bo->imported) {
                 struct panfrost_memory mem = {
                         .cpu = bo->cpu,
                         .gpu = bo->gpu,
@@ -390,11 +396,6 @@ panfrost_destroy_bo(struct panfrost_screen *screen, struct panfrost_bo *bo)
                 panfrost_drm_free_slab(screen, &mem);
         }
 
-        if (bo->layout == PAN_AFBC) {
-                /* TODO */
-                DBG("--leaking afbc (%d bytes)--\n", bo->afbc_metadata_size);
-        }
-
         if (bo->has_checksum) {
                 struct panfrost_memory mem = {
                         .cpu = bo->checksum_slab.cpu,
diff --git a/src/gallium/drivers/panfrost/pan_resource.h b/src/gallium/drivers/panfrost/pan_resource.h
index 632250fa2aa..a0bb5e962d9 100644
--- a/src/gallium/drivers/panfrost/pan_resource.h
+++ b/src/gallium/drivers/panfrost/pan_resource.h
@@ -44,6 +44,10 @@ struct panfrost_slice {
         unsigned offset;
         unsigned stride;
 
+        /* If there is a header preceding each slice, how big is that header?
+         * Used for AFBC */
+        unsigned header_size;
+
         /* Has anything been written to this slice? */
         bool initialized;
 };
@@ -72,14 +76,6 @@ struct panfrost_bo {
         /* Internal layout (tiled?) */
         enum panfrost_memory_layout layout;
 
-        /* If AFBC is enabled for this resource, we lug around an AFBC
-         * metadata buffer as well. The actual AFBC resource is also in
-         * afbc_slab (only defined for AFBC) at position afbc_main_offset
-         */
-
-        struct panfrost_memory afbc_slab;
-        int afbc_metadata_size;
-
         /* If transaciton elimination is enabled, we have a dedicated
          * buffer for that as well. */
 
@@ -134,8 +130,8 @@ void panfrost_resource_context_init(struct pipe_context *pctx);
 bool
 panfrost_format_supports_afbc(enum pipe_format format);
 
-void
-panfrost_enable_afbc(struct panfrost_context *ctx, struct panfrost_resource *rsrc, bool ds);
+unsigned
+panfrost_afbc_header_size(unsigned width, unsigned height);
 
 /* Blitting */
 
-- 
2.20.1



More information about the mesa-dev mailing list