pan/decode: Include address in union mali_attr
[mesa.git] / src / panfrost / pandecode / decode.c
index 208f4103c756cbb2ff69c2059aa20db0e2d80c1d..148be10fdc47bd5a95397825821d8e3c00bc2c82 100644 (file)
@@ -29,6 +29,7 @@
 #include <memory.h>
 #include <stdbool.h>
 #include <stdarg.h>
+#include <ctype.h>
 #include "decode.h"
 #include "util/macros.h"
 #include "util/u_math.h"
@@ -431,27 +432,6 @@ static char *pandecode_attr_mode(enum mali_attr_mode mode)
 
 #undef DEFINE_CASE
 
-#define DEFINE_CASE(name) case MALI_CHANNEL_## name: return "MALI_CHANNEL_" #name
-static char *
-pandecode_channel(enum mali_channel channel)
-{
-        switch (channel) {
-                DEFINE_CASE(RED);
-                DEFINE_CASE(GREEN);
-                DEFINE_CASE(BLUE);
-                DEFINE_CASE(ALPHA);
-                DEFINE_CASE(ZERO);
-                DEFINE_CASE(ONE);
-                DEFINE_CASE(RESERVED_0);
-                DEFINE_CASE(RESERVED_1);
-
-        default:
-                pandecode_msg("XXX: invalid channel %X\n", channel);
-                return "";
-        }
-}
-#undef DEFINE_CASE
-
 #define DEFINE_CASE(name) case MALI_WRAP_## name: return "MALI_WRAP_" #name
 static char *
 pandecode_wrap_mode(enum mali_wrap_mode op)
@@ -469,22 +449,6 @@ pandecode_wrap_mode(enum mali_wrap_mode op)
 }
 #undef DEFINE_CASE
 
-#define DEFINE_CASE(name) case MALI_TEX_## name: return "MALI_TEX_" #name
-static char *
-pandecode_texture_type(enum mali_texture_type type)
-{
-        switch (type) {
-                DEFINE_CASE(1D);
-                DEFINE_CASE(2D);
-                DEFINE_CASE(3D);
-                DEFINE_CASE(CUBE);
-
-        default:
-                unreachable("Unknown case");
-        }
-}
-#undef DEFINE_CASE
-
 #define DEFINE_CASE(name) case MALI_MFBD_BLOCK_## name: return "MALI_MFBD_BLOCK_" #name
 static char *
 pandecode_mfbd_block_format(enum mali_mfbd_block_format fmt)
@@ -755,14 +719,126 @@ pandecode_compute_fbd(uint64_t gpu_va, int job_no)
         printf("},\n");
 }
 
+/* Extracts the number of components associated with a Mali format */
+
+static unsigned
+pandecode_format_component_count(enum mali_format fmt)
+{
+        /* Mask out the format class */
+        unsigned top = fmt & 0b11100000;
+
+        switch (top) {
+        case MALI_FORMAT_SNORM:
+        case MALI_FORMAT_UINT:
+        case MALI_FORMAT_UNORM:
+        case MALI_FORMAT_SINT:
+                return ((fmt >> 3) & 3) + 1;
+        default:
+                /* TODO: Validate */
+                return 4;
+        }
+}
+
+/* Extracts a mask of accessed components from a 12-bit Mali swizzle */
+
+static unsigned
+pandecode_access_mask_from_channel_swizzle(unsigned swizzle)
+{
+        unsigned mask = 0;
+        assert(MALI_CHANNEL_RED == 0);
+
+        for (unsigned c = 0; c < 4; ++c) {
+                enum mali_channel chan = (swizzle >> (3*c)) & 0x7;
+
+                if (chan <= MALI_CHANNEL_ALPHA)
+                        mask |= (1 << chan);
+        }
+
+        return mask;
+}
+
+/* Validates that a (format, swizzle) pair is valid, in the sense that the
+ * swizzle doesn't access any components that are undefined in the format.
+ * Returns whether the swizzle is trivial (doesn't do any swizzling) and can be
+ * omitted */
+
+static bool
+pandecode_validate_format_swizzle(enum mali_format fmt, unsigned swizzle)
+{
+        unsigned nr_comp = pandecode_format_component_count(fmt);
+        unsigned access_mask = pandecode_access_mask_from_channel_swizzle(swizzle);
+        unsigned valid_mask = (1 << nr_comp) - 1;
+        unsigned invalid_mask = ~valid_mask;
+
+        if (access_mask & invalid_mask) {
+                pandecode_msg("XXX: invalid components accessed\n");
+                return false;
+        }
+
+        /* Check for the default non-swizzling swizzle so we can suppress
+         * useless printing for the defaults */
+
+        unsigned default_swizzles[4] = {
+                MALI_CHANNEL_RED | (MALI_CHANNEL_ZERO  << 3) | (MALI_CHANNEL_ZERO << 6) | (MALI_CHANNEL_ONE   << 9),
+                MALI_CHANNEL_RED | (MALI_CHANNEL_GREEN << 3) | (MALI_CHANNEL_ZERO << 6) | (MALI_CHANNEL_ONE   << 9),
+                MALI_CHANNEL_RED | (MALI_CHANNEL_GREEN << 3) | (MALI_CHANNEL_BLUE << 6) | (MALI_CHANNEL_ONE   << 9),
+                MALI_CHANNEL_RED | (MALI_CHANNEL_GREEN << 3) | (MALI_CHANNEL_BLUE << 6) | (MALI_CHANNEL_ALPHA << 9)
+        };
+
+        return (swizzle == default_swizzles[nr_comp - 1]);
+}
+
+/* Maps MALI_RGBA32F to rgba32f, etc */
+
 static void
-pandecode_swizzle(unsigned swizzle)
+pandecode_format_short(enum mali_format fmt, bool srgb)
 {
-        pandecode_prop("swizzle = %s | (%s << 3) | (%s << 6) | (%s << 9)",
-                       pandecode_channel((swizzle >> 0) & 0x7),
-                       pandecode_channel((swizzle >> 3) & 0x7),
-                       pandecode_channel((swizzle >> 6) & 0x7),
-                       pandecode_channel((swizzle >> 9) & 0x7));
+        /* We want a type-like format, so cut off the initial MALI_ */
+        char *format = pandecode_format(fmt);
+        format += strlen("MALI_");
+
+        unsigned len = strlen(format);
+        char *lower_format = calloc(1, len + 1);
+
+        for (unsigned i = 0; i < len; ++i)
+                lower_format[i] = tolower(format[i]);
+
+        /* Sanity check sRGB flag is applied to RGB, per the name */
+        if (srgb && lower_format[0] != 'r')
+                pandecode_msg("XXX: sRGB applied to non-colour format\n");
+
+        /* Just prefix with an s, so you get formats like srgba8_unorm */
+        if (srgb)
+                pandecode_log_cont("s");
+
+        pandecode_log_cont("%s", lower_format);
+        free(lower_format);
+}
+
+static void
+pandecode_swizzle(unsigned swizzle, enum mali_format format)
+{
+        /* First, do some validation */
+        bool trivial_swizzle = pandecode_validate_format_swizzle(
+                        format, swizzle);
+
+        if (trivial_swizzle)
+                return;
+
+        /* Next, print the swizzle */
+        pandecode_log_cont(".");
+
+        static const char components[] = "rgba01";
+
+        for (unsigned c = 0; c < 4; ++c) {
+                enum mali_channel chan = (swizzle >> (3 * c)) & 0x7;
+
+                if (chan >= MALI_CHANNEL_RESERVED_0) {
+                        pandecode_log("XXX: invalid swizzle channel %d\n", chan);
+                        continue;
+                }
+                pandecode_log_cont("%c", components[chan]);
+        }
 }
 
 static void
@@ -778,6 +854,11 @@ pandecode_rt_format(struct mali_rt_format format)
         pandecode_prop("block = %s",
                        pandecode_mfbd_block_format(format.block));
 
+        /* TODO: Map formats so we can check swizzles and print nicely */
+        pandecode_log("swizzle");
+        pandecode_swizzle(format.swizzle, MALI_RGBA8_UNORM);
+        pandecode_log_cont(",\n");
+
         pandecode_prop("nr_channels = MALI_POSITIVE(%d)",
                        MALI_NEGATIVE(format.nr_channels));
 
@@ -785,8 +866,6 @@ pandecode_rt_format(struct mali_rt_format format)
         pandecode_log_decoded_flags(mfbd_fmt_flag_info, format.flags);
         pandecode_log_cont(",\n");
 
-        pandecode_swizzle(format.swizzle);
-
         /* In theory, the no_preload bit can be cleared to enable MFBD preload,
          * which is a faster hardware-based alternative to the wallpaper method
          * to preserve framebuffer contents across frames. In practice, MFBD
@@ -1150,35 +1229,23 @@ pandecode_attributes(const struct pandecode_mapped_memory *mem,
 
         union mali_attr *attr = pandecode_fetch_gpu_mem(mem, addr, sizeof(union mali_attr) * count);
 
-        char base[128];
-        snprintf(base, sizeof(base), "%s_data_%d%s", prefix, job_no, suffix);
+        pandecode_log("union mali_attr %s_%d[] = {\n", prefix, job_no);
+        pandecode_indent++;
 
         for (int i = 0; i < count; ++i) {
+                pandecode_log("{\n");
+                pandecode_indent++;
+
                 enum mali_attr_mode mode = attr[i].elements & 7;
 
                 if (mode == MALI_ATTR_UNUSED)
-                        continue;
+                        pandecode_msg("XXX: unused attribute record\n");
 
                 mali_ptr raw_elements = attr[i].elements & ~7;
-
-                /* TODO: Do we maybe want to dump the attribute values
-                 * themselves given the specified format? Or is that too hard?
-                 * */
-
                 char *a = pointer_as_memory_reference(raw_elements);
-                pandecode_log("mali_ptr %s_%d_p = %s;\n", base, i, a);
+                pandecode_prop("elements = (%s) | %s", a, pandecode_attr_mode(mode));
                 free(a);
-        }
 
-        pandecode_log("union mali_attr %s_%d[] = {\n", prefix, job_no);
-        pandecode_indent++;
-
-        for (int i = 0; i < count; ++i) {
-                pandecode_log("{\n");
-                pandecode_indent++;
-
-                unsigned mode = attr[i].elements & 7;
-                pandecode_prop("elements = (%s_%d_p) | %s", base, i, pandecode_attr_mode(mode));
                 pandecode_prop("shift = %d", attr[i].shift);
                 pandecode_prop("extra_flags = %d", attr[i].extra_flags);
                 pandecode_prop("stride = 0x%" PRIx32, attr[i].stride);
@@ -1367,6 +1434,11 @@ pandecode_midgard_blend_mrt(void *descs, int job_no, int rt_no)
         return shader;
 }
 
+/* Attributes and varyings have descriptor records, which contain information
+ * about their format and ordering with the attribute/varying buffers. We'll
+ * want to validate that the combinations specified are self-consistent.
+ */
+
 static int
 pandecode_attribute_meta(int job_no, int count, const struct mali_vertex_tiler_postfix *v, bool varying, char *suffix)
 {
@@ -1419,14 +1491,8 @@ pandecode_attribute_meta(int job_no, int count, const struct mali_vertex_tiler_p
                         }
                 }
 
-                pandecode_log("{\n");
-                pandecode_indent++;
-                pandecode_prop("index = %d", attr_meta->index);
-
                 if (attr_meta->index > max_index)
                         max_index = attr_meta->index;
-                pandecode_swizzle(attr_meta->swizzle);
-                pandecode_prop("format = %s", pandecode_format(attr_meta->format));
 
                 if (attr_meta->unknown1 != 0x2) {
                         pandecode_msg("XXX: expected unknown1 = 0x2\n");
@@ -1438,10 +1504,16 @@ pandecode_attribute_meta(int job_no, int count, const struct mali_vertex_tiler_p
                         pandecode_prop("unknown3 = 0x%" PRIx64, (u64) attr_meta->unknown3);
                 }
 
-                pandecode_prop("src_offset = %d", attr_meta->src_offset);
-                pandecode_indent--;
-                pandecode_log("},\n");
+                pandecode_make_indent();
+                pandecode_format_short(attr_meta->format, false);
+                pandecode_log_cont(" %s_%u", prefix, attr_meta->index);
+
+                if (attr_meta->src_offset)
+                        pandecode_log_cont("[%u]", attr_meta->src_offset);
+
+                pandecode_swizzle(attr_meta->swizzle, attr_meta->format);
 
+                pandecode_log_cont(";\n");
         }
 
         pandecode_indent--;
@@ -1658,7 +1730,161 @@ pandecode_shader_disassemble(mali_ptr shader_ptr, int shader_no, int type,
 }
 
 static void
-pandecode_vertex_tiler_postfix_pre(const struct mali_vertex_tiler_postfix *p,
+pandecode_texture(mali_ptr u,
+                struct pandecode_mapped_memory *tmem,
+                unsigned job_no, unsigned tex)
+{
+        struct mali_texture_descriptor *PANDECODE_PTR_VAR(t, tmem, u);
+
+        pandecode_log("struct mali_texture_descriptor texture_descriptor_%"PRIx64"_%d_%d = {\n", u, job_no, tex);
+        pandecode_indent++;
+
+        struct mali_texture_format f = t->format;
+
+        /* See the definiton of enum mali_texture_type */
+
+        bool is_cube = f.type == MALI_TEX_CUBE;
+        unsigned dimension = is_cube ? 2 : f.type;
+
+        pandecode_make_indent();
+
+        /* TODO: Are there others? */
+        bool is_zs = f.format == MALI_Z32_UNORM;
+
+        /* Recall Z/S switched the meaning of linear/tiled .. */
+        if (is_zs && f.layout == MALI_TEXTURE_LINEAR)
+                pandecode_msg("XXX: depth/stencil cannot be tiled\n");
+
+        /* Print the layout. Default is linear; a modifier can denote AFBC or
+         * u-interleaved/tiled modes */
+
+        if (f.layout == MALI_TEXTURE_AFBC)
+                pandecode_log_cont("afbc");
+        else if (f.layout == MALI_TEXTURE_TILED)
+                pandecode_log_cont(is_zs ? "linear" : "tiled");
+        else if (f.layout == MALI_TEXTURE_LINEAR)
+                pandecode_log_cont("linear");
+        else
+                pandecode_msg("XXX: invalid texture layout 0x%X\n", f.layout);
+
+        pandecode_swizzle(t->swizzle, f.format);
+        pandecode_log_cont(" ");
+
+        /* Distinguish cube/2D with modifier */
+
+        if (is_cube)
+                pandecode_log_cont("cube ");
+
+        pandecode_format_short(f.format, f.srgb);
+        pandecode_swizzle(f.swizzle, f.format);
+
+        /* All four width/height/depth/array_size dimensions are present
+         * regardless of the type of texture, but it is an error to have
+         * non-zero dimensions for unused dimensions. Verify this. array_size
+         * can always be set, as can width. */
+
+        if (t->height && dimension < 2)
+                pandecode_msg("XXX: nonzero height for <2D texture\n");
+
+        if (t->depth && dimension < 3)
+                pandecode_msg("XXX: nonzero depth for <2D texture\n");
+
+        /* Print only the dimensions that are actually there */
+
+        pandecode_log_cont(": %d", t->width + 1);
+
+        if (dimension >= 2)
+                pandecode_log_cont("x%u", t->height + 1);
+
+        if (dimension >= 3)
+                pandecode_log_cont("x%u", t->depth + 1);
+
+        if (t->array_size)
+                pandecode_log_cont("[%u]", t->array_size + 1);
+
+        if (t->levels)
+                pandecode_log_cont(" mip %u", t->levels);
+
+        pandecode_log_cont("\n");
+
+        if (f.unknown1 | f.zero) {
+                pandecode_msg("XXX: texture format zero tripped\n");
+                pandecode_prop("unknown1 = %" PRId32, f.unknown1);
+                pandecode_prop("zero = %" PRId32, f.zero);
+        }
+
+        if (!f.unknown2) {
+                pandecode_msg("XXX: expected unknown texture bit set\n");
+                pandecode_prop("unknown2 = %" PRId32, f.unknown1);
+        }
+
+        if (t->swizzle_zero) {
+                pandecode_msg("XXX: swizzle zero tripped\n");
+                pandecode_prop("swizzle_zero = %d", t->swizzle_zero);
+        }
+
+        if (t->unknown3 | t->unknown3A | t->unknown5 | t->unknown6 | t->unknown7) {
+                pandecode_msg("XXX: texture zero tripped\n");
+                pandecode_prop("unknown3 = %" PRId16, t->unknown3);
+                pandecode_prop("unknown3A = %" PRId8, t->unknown3A);
+                pandecode_prop("unknown5 = 0x%" PRIx32, t->unknown5);
+                pandecode_prop("unknown6 = 0x%" PRIx32, t->unknown6);
+                pandecode_prop("unknown7 = 0x%" PRIx32, t->unknown7);
+        }
+
+        pandecode_log(".payload = {\n");
+        pandecode_indent++;
+
+        /* A bunch of bitmap pointers follow.
+         * We work out the correct number,
+         * based on the mipmap/cubemap
+         * properties, but dump extra
+         * possibilities to futureproof */
+
+        int bitmap_count = MALI_NEGATIVE(t->levels);
+
+        /* Miptree for each face */
+        if (f.type == MALI_TEX_CUBE)
+                bitmap_count *= 6;
+
+        /* Array of textures */
+        bitmap_count *= MALI_NEGATIVE(t->array_size);
+
+        /* Stride for each element */
+        if (f.manual_stride)
+                bitmap_count *= 2;
+
+        /* Sanity check the size */
+        int max_count = sizeof(t->payload) / sizeof(t->payload[0]);
+        assert (bitmap_count <= max_count);
+
+        for (int i = 0; i < bitmap_count; ++i) {
+                /* How we dump depends if this is a stride or a pointer */
+
+                if (f.manual_stride && (i & 1)) {
+                        /* signed 32-bit snuck in as a 64-bit pointer */
+                        uint64_t stride_set = t->payload[i];
+                        uint32_t clamped_stride = stride_set;
+                        int32_t stride = clamped_stride;
+                        assert(stride_set == clamped_stride);
+                        pandecode_log("(mali_ptr) %d /* stride */, \n", stride);
+                } else {
+                        char *a = pointer_as_memory_reference(t->payload[i]);
+                        pandecode_log("%s, \n", a);
+                        free(a);
+                }
+        }
+
+        pandecode_indent--;
+        pandecode_log("},\n");
+
+        pandecode_indent--;
+        pandecode_log("};\n");
+}
+
+static void
+pandecode_vertex_tiler_postfix_pre(
+                const struct mali_vertex_tiler_postfix *p,
                 int job_no, enum mali_job_type job_type,
                 char *suffix, bool is_bifrost)
 {
@@ -1933,103 +2159,11 @@ pandecode_vertex_tiler_postfix_pre(const struct mali_vertex_tiler_postfix *p,
                         pandecode_log("};\n");
 
                         /* Now, finally, descend down into the texture descriptor */
-                        for (int tex = 0; tex < texture_count; ++tex) {
+                        for (unsigned tex = 0; tex < texture_count; ++tex) {
                                 mali_ptr *PANDECODE_PTR_VAR(u, mmem, p->texture_trampoline + tex * sizeof(mali_ptr));
                                 struct pandecode_mapped_memory *tmem = pandecode_find_mapped_gpu_mem_containing(*u);
-
-                                if (tmem) {
-                                        struct mali_texture_descriptor *PANDECODE_PTR_VAR(t, tmem, *u);
-
-                                        pandecode_log("struct mali_texture_descriptor texture_descriptor_%"PRIx64"_%d_%d = {\n", *u, job_no, tex);
-                                        pandecode_indent++;
-
-                                        pandecode_prop("width = MALI_POSITIVE(%" PRId16 ")", t->width + 1);
-                                        pandecode_prop("height = MALI_POSITIVE(%" PRId16 ")", t->height + 1);
-                                        pandecode_prop("depth = MALI_POSITIVE(%" PRId16 ")", t->depth + 1);
-                                        pandecode_prop("array_size = MALI_POSITIVE(%" PRId16 ")", t->array_size + 1);
-                                        pandecode_prop("unknown3 = %" PRId16, t->unknown3);
-                                        pandecode_prop("unknown3A = %" PRId8, t->unknown3A);
-                                        pandecode_prop("nr_mipmap_levels = %" PRId8, t->nr_mipmap_levels);
-
-                                        struct mali_texture_format f = t->format;
-
-                                        pandecode_log(".format = {\n");
-                                        pandecode_indent++;
-
-                                        pandecode_swizzle(f.swizzle);
-                                        pandecode_prop("format = %s", pandecode_format(f.format));
-                                        pandecode_prop("type = %s", pandecode_texture_type(f.type));
-                                        pandecode_prop("srgb = %" PRId32, f.srgb);
-                                        pandecode_prop("unknown1 = %" PRId32, f.unknown1);
-                                        pandecode_prop("usage2 = 0x%" PRIx32, f.usage2);
-
-                                        pandecode_indent--;
-                                        pandecode_log("},\n");
-
-                                        pandecode_swizzle(t->swizzle);
-
-                                        if (t->swizzle_zero) {
-                                                /* Shouldn't happen */
-                                                pandecode_msg("XXX: swizzle zero tripped\n");
-                                                pandecode_prop("swizzle_zero = %d", t->swizzle_zero);
-                                        }
-
-                                        pandecode_prop("unknown3 = 0x%" PRIx32, t->unknown3);
-
-                                        pandecode_prop("unknown5 = 0x%" PRIx32, t->unknown5);
-                                        pandecode_prop("unknown6 = 0x%" PRIx32, t->unknown6);
-                                        pandecode_prop("unknown7 = 0x%" PRIx32, t->unknown7);
-
-                                        pandecode_log(".payload = {\n");
-                                        pandecode_indent++;
-
-                                        /* A bunch of bitmap pointers follow.
-                                         * We work out the correct number,
-                                         * based on the mipmap/cubemap
-                                         * properties, but dump extra
-                                         * possibilities to futureproof */
-
-                                        int bitmap_count = MALI_NEGATIVE(t->nr_mipmap_levels);
-                                        bool manual_stride = f.usage2 & MALI_TEX_MANUAL_STRIDE;
-
-                                        /* Miptree for each face */
-                                        if (f.type == MALI_TEX_CUBE)
-                                                bitmap_count *= 6;
-
-                                        /* Array of textures */
-                                        bitmap_count *= MALI_NEGATIVE(t->array_size);
-
-                                        /* Stride for each element */
-                                        if (manual_stride)
-                                                bitmap_count *= 2;
-
-                                        /* Sanity check the size */
-                                        int max_count = sizeof(t->payload) / sizeof(t->payload[0]);
-                                        assert (bitmap_count <= max_count);
-
-                                        for (int i = 0; i < bitmap_count; ++i) {
-                                                /* How we dump depends if this is a stride or a pointer */
-
-                                                if ((f.usage2 & MALI_TEX_MANUAL_STRIDE) && (i & 1)) {
-                                                        /* signed 32-bit snuck in as a 64-bit pointer */
-                                                        uint64_t stride_set = t->payload[i];
-                                                        uint32_t clamped_stride = stride_set;
-                                                        int32_t stride = clamped_stride;
-                                                        assert(stride_set == clamped_stride);
-                                                        pandecode_log("(mali_ptr) %d /* stride */, \n", stride);
-                                                } else {
-                                                        char *a = pointer_as_memory_reference(t->payload[i]);
-                                                        pandecode_log("%s, \n", a);
-                                                        free(a);
-                                                }
-                                        }
-
-                                        pandecode_indent--;
-                                        pandecode_log("},\n");
-
-                                        pandecode_indent--;
-                                        pandecode_log("};\n");
-                                }
+                                if (tmem)
+                                        pandecode_texture(*u, tmem, job_no, tex);
                         }
                 }
         }