Mesa (master): panfrost: Refactor AFBC code

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Mon Jun 10 14:06:02 UTC 2019


Module: Mesa
Branch: master
Commit: d878753efa0f568f6f14a2eaf51e3ed98e3c0101
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=d878753efa0f568f6f14a2eaf51e3ed98e3c0101

Author: Alyssa Rosenzweig <alyssa.rosenzweig at collabora.com>
Date:   Fri Jun  7 09:39:31 2019 -0700

panfrost: Refactor AFBC code

This patch does a substantial cleanup of the code for handling AFBC,
moving various disparate misplaced functions into a new central
pan_afbc.c file.

Signed-off-by: Alyssa Rosenzweig <alyssa.rosenzweig at collabora.com>

---

 src/gallium/drivers/panfrost/meson.build    |   1 +
 src/gallium/drivers/panfrost/pan_afbc.c     | 142 ++++++++++++++++++++++++++++
 src/gallium/drivers/panfrost/pan_context.c  |  67 +------------
 src/gallium/drivers/panfrost/pan_resource.h |   8 ++
 4 files changed, 154 insertions(+), 64 deletions(-)

diff --git a/src/gallium/drivers/panfrost/meson.build b/src/gallium/drivers/panfrost/meson.build
index cf4b0c2d241..eda7abb7d22 100644
--- a/src/gallium/drivers/panfrost/meson.build
+++ b/src/gallium/drivers/panfrost/meson.build
@@ -43,6 +43,7 @@ files_panfrost = files(
   'bifrost/disassemble.c',
 
   'pan_context.c',
+  'pan_afbc.c',
   'pan_job.c',
   'pan_trace.c',
   'pan_drm.c',
diff --git a/src/gallium/drivers/panfrost/pan_afbc.c b/src/gallium/drivers/panfrost/pan_afbc.c
new file mode 100644
index 00000000000..83d93a14742
--- /dev/null
+++ b/src/gallium/drivers/panfrost/pan_afbc.c
@@ -0,0 +1,142 @@
+/*
+ * Copyright (C) 2019 Collabora
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * Authors:
+ *   Alyssa Rosenzweig <alyssa.rosenzweig at collabora.com>
+ */
+
+#include "pan_resource.h"
+#include "util/u_format.h"
+
+/* Arm FrameBuffer Compression (AFBC) is a lossless compression scheme natively
+ * implemented in Mali GPUs (as well as many display controllers paired with
+ * Mali GPUs, etc). Where possible, Panfrost prefers to use AFBC for both
+ * rendering and texturing. In most cases, this is a performance-win due to a
+ * dramatic reduction in memory bandwidth and cache locality compared to a
+ * linear resources.
+ *
+ * AFBC divides the framebuffer into 16x16 tiles (other sizes possible, TODO:
+ * do we need to support this?). So, the width and height each must be aligned
+ * up to 16 pixels. This is inherently good for performance; note that for a 4
+ * byte-per-pixel format like RGBA8888, that means that rows are 16*4=64 byte
+ * aligned, which is the cache-line size.
+ *
+ * For each AFBC-compressed resource, there is a single contiguous
+ * (CPU/GPU-shared) buffer. This buffer itself is divided into two parts:
+ * header and body, placed immediately after each other.
+ *
+ * The AFBC header contains 16 bytes of metadata per tile.
+ *
+ * The AFBC body is the same size as the original linear resource (padded to
+ * the nearest tile). Although the body comes immediately after the header, it
+ * must also be cache-line aligned, so there can sometimes be a bit of padding
+ * between the header and body.
+ *
+ * As an example, a 64x64 RGBA framebuffer contains 64/16 = 4 tiles horizontally and
+ * 4 tiles vertically. There are 4*4=16 tiles in total, each containing 16
+ * bytes of metadata, so there is a 16*16=256 byte header. 64x64 is already
+ * tile aligned, so the body is 64*64 * 4 bytes per pixel = 16384 bytes of
+ * body.
+ *
+ * From userspace, Panfrost needs to be able to calculate these sizes. It
+ * explicitly does not and can not know the format of the data contained within
+ * this header and body. The GPU has native support for AFBC encode/decode. For
+ * an internal FBO or a framebuffer used for scanout with an AFBC-compatible
+ * winsys/display-controller, the buffer is maintained AFBC throughout flight,
+ * and the driver never needs to know the internal data. For edge cases where
+ * the driver really does need to read/write from the AFBC resource, we
+ * generate a linear staging buffer and use the GPU to blit AFBC<--->linear.
+ * TODO: Implement me. */
+
+#define AFBC_TILE_WIDTH 16
+#define AFBC_TILE_HEIGHT 16
+#define AFBC_HEADER_BYTES_PER_TILE 16
+#define AFBC_CACHE_ALIGN 64
+
+/* Is it possible to AFBC compress a particular format? Common formats (and
+ * YUV) are compressible. Some obscure formats are not and fallback on linear,
+ * at a performance hit. Also, if you need to disable AFBC entirely in the
+ * driver for debug/profiling, just always return false here. */
+
+bool
+panfrost_format_supports_afbc(enum pipe_format format)
+{
+        const struct util_format_description *desc =
+                util_format_description(format);
+
+        if (util_format_is_rgba8_variant(desc))
+                return true;
+
+        /* TODO: AFBC of other formats */
+        /* TODO: AFBC of ZS */
+
+        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)
+{
+        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);
+
+        /* Compute size in tiles, rather than pixels */
+        unsigned tile_count_x = aligned_width  / AFBC_TILE_WIDTH;
+        unsigned tile_count_y = aligned_height / AFBC_TILE_HEIGHT;
+        unsigned tile_count = tile_count_x * tile_count_y;
+
+        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 */
+        screen->driver->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;
+}
diff --git a/src/gallium/drivers/panfrost/pan_context.c b/src/gallium/drivers/panfrost/pan_context.c
index b15cf5f38a4..21e56eff3b3 100644
--- a/src/gallium/drivers/panfrost/pan_context.c
+++ b/src/gallium/drivers/panfrost/pan_context.c
@@ -53,62 +53,6 @@ extern const char *pan_counters_base;
 /* Do not actually send anything to the GPU; merely generate the cmdstream as fast as possible. Disables framebuffer writes */
 //#define DRY_RUN
 
-/* Can a given format support AFBC? Not all can. */
-
-static bool
-panfrost_can_afbc(enum pipe_format format)
-{
-        const struct util_format_description *desc =
-                util_format_description(format);
-
-        if (util_format_is_rgba8_variant(desc))
-                return true;
-
-        /* TODO: AFBC of other formats */
-
-        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. */
-
-static void
-panfrost_enable_afbc(struct panfrost_context *ctx, struct panfrost_resource *rsrc, bool ds)
-{
-        if (ctx->require_sfbd) {
-                DBG("AFBC not supported yet on SFBD\n");
-                assert(0);
-        }
-
-        struct pipe_context *gallium = (struct pipe_context *) ctx;
-        struct panfrost_screen *screen = pan_screen(gallium->screen);
-       /* AFBC metadata is 16 bytes per tile */
-        int tile_w = (rsrc->base.width0 + (MALI_TILE_LENGTH - 1)) >> MALI_TILE_SHIFT;
-        int tile_h = (rsrc->base.height0 + (MALI_TILE_LENGTH - 1)) >> MALI_TILE_SHIFT;
-        int bytes_per_pixel = util_format_get_blocksize(rsrc->base.format);
-        int stride = bytes_per_pixel * ALIGN(rsrc->base.width0, 16);
-
-        stride *= 2;  /* TODO: Should this be carried over? */
-        int main_size = stride * rsrc->base.height0;
-        rsrc->bo->afbc_metadata_size = tile_w * tile_h * 16;
-
-        /* Allocate the AFBC slab itself, large enough to hold the above */
-        screen->driver->allocate_slab(screen, &rsrc->bo->afbc_slab,
-                               (rsrc->bo->afbc_metadata_size + main_size + 4095) / 4096,
-                               true, 0, 0, 0);
-
-        rsrc->bo->layout = PAN_AFBC;
-
-        /* Compressed textured reads use a tagged pointer to the metadata */
-
-        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;
-}
-
 static void
 panfrost_enable_checksum(struct panfrost_context *ctx, struct panfrost_resource *rsrc)
 {
@@ -2175,18 +2119,13 @@ panfrost_set_framebuffer_state(struct pipe_context *pctx,
                 enum pipe_format format = ctx->pipe_framebuffer.cbufs[i]->format;
                 bool is_scanout = panfrost_is_scanout(ctx);
 
-                if (!is_scanout && tex->bo->layout != PAN_AFBC && panfrost_can_afbc(format)) {
-                        /* The blob is aggressive about enabling AFBC. As such,
-                         * it's pretty much necessary to use it here, since we
-                         * have no traces of non-compressed FBO. */
+                bool can_afbc = panfrost_format_supports_afbc(format);
 
+                if (!is_scanout && tex->bo->layout != PAN_AFBC && can_afbc)
                         panfrost_enable_afbc(ctx, tex, false);
-                }
 
-                if (!is_scanout && !tex->bo->has_checksum) {
-                        /* Enable transaction elimination if we can */
+                if (!is_scanout && !tex->bo->has_checksum)
                         panfrost_enable_checksum(ctx, tex);
-                }
         }
 
         {
diff --git a/src/gallium/drivers/panfrost/pan_resource.h b/src/gallium/drivers/panfrost/pan_resource.h
index 269268e9d22..feb6da15669 100644
--- a/src/gallium/drivers/panfrost/pan_resource.h
+++ b/src/gallium/drivers/panfrost/pan_resource.h
@@ -125,4 +125,12 @@ void panfrost_resource_screen_init(struct panfrost_screen *screen);
 
 void panfrost_resource_context_init(struct pipe_context *pctx);
 
+/* AFBC */
+
+bool
+panfrost_format_supports_afbc(enum pipe_format format);
+
+void
+panfrost_enable_afbc(struct panfrost_context *ctx, struct panfrost_resource *rsrc, bool ds);
+
 #endif /* PAN_RESOURCE_H */




More information about the mesa-commit mailing list