panfrost: Move format translation to root
authorAlyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
Tue, 18 Feb 2020 23:11:42 +0000 (18:11 -0500)
committerAlyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
Fri, 21 Feb 2020 12:27:03 +0000 (07:27 -0500)
Since PIPE formats are now shared across Mesa we can do this, and the
routines themselves are good enough code that I'm happy to move them
here. We'll use them momentarily.

Signed-off-by: Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/merge_requests/3858>

14 files changed:
src/gallium/drivers/panfrost/Makefile.sources
src/gallium/drivers/panfrost/meson.build
src/gallium/drivers/panfrost/pan_assemble.c
src/gallium/drivers/panfrost/pan_context.c
src/gallium/drivers/panfrost/pan_context.h
src/gallium/drivers/panfrost/pan_format.c [deleted file]
src/gallium/drivers/panfrost/pan_format.h [deleted file]
src/gallium/drivers/panfrost/pan_fragment.c
src/gallium/drivers/panfrost/pan_mfbd.c
src/gallium/drivers/panfrost/pan_sfbd.c
src/gallium/drivers/panfrost/pan_varyings.c
src/panfrost/encoder/meson.build
src/panfrost/encoder/pan_format.c [new file with mode: 0644]
src/panfrost/encoder/pan_texture.h

index 2fd578877eff7c78bad39d7bcb15f981b45c3715..835f2255fb619e59e21c6b141b3a8a4c0ecec17b 100644 (file)
@@ -19,8 +19,6 @@ C_SOURCES := \
        pan_compute.c \
        pan_context.c \
        pan_context.h \
-       pan_format.c \
-       pan_format.h \
        pan_fragment.c \
        pan_attributes.c \
        pan_job.c \
index 7b01ee47635fe617c91a0c1a6e22ff2b11f8626f..b79c4c0b8d04fa0d8f7f1c690c34195db9ae729a 100644 (file)
@@ -35,7 +35,6 @@ files_panfrost = files(
   'pan_job.c',
   'pan_allocate.c',
   'pan_assemble.c',
-  'pan_format.c',
   'pan_blending.c',
   'pan_blend_shaders.c',
   'pan_blend_cso.c',
index d6b63219c3a23fef3fcf6ef086c70fed666e092f..1ef81c5e20f91d157b0c028ae52d40f921d9232c 100644 (file)
@@ -27,7 +27,6 @@
 #include <string.h>
 #include "pan_bo.h"
 #include "pan_context.h"
-#include "pan_format.h"
 #include "pan_util.h"
 
 #include "compiler/nir/nir.h"
index f234d9435c367c58755ddf0db84a83d45bd4a75f..0d69036b9a22ea92961118aaebe2d2d0a007755e 100644 (file)
@@ -29,7 +29,6 @@
 
 #include "pan_bo.h"
 #include "pan_context.h"
-#include "pan_format.h"
 #include "panfrost-quirks.h"
 
 #include "util/macros.h"
index 1387c910b9034c24dd810622866f5793e9346502..1a5702613638e95e893868e70d3f49492245decb 100644 (file)
@@ -33,6 +33,7 @@
 #include "pan_job.h"
 #include "pan_blend.h"
 #include "pan_encoder.h"
+#include "pan_texture.h"
 
 #include "pipe/p_compiler.h"
 #include "pipe/p_config.h"
diff --git a/src/gallium/drivers/panfrost/pan_format.c b/src/gallium/drivers/panfrost/pan_format.c
deleted file mode 100644 (file)
index b7f5e44..0000000
+++ /dev/null
@@ -1,292 +0,0 @@
-/*
- * © Copyright 2018 Alyssa Rosenzweig
- *
- * 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.
- *
- */
-
-#include "pan_format.h"
-
-/* From panwrap/panwrap-decoder, but we don't want to bring in all those headers */
-char *panwrap_format_name(enum mali_format format);
-
-/* Translate a Gallium swizzle quad to a 12-bit Mali swizzle code. Gallium
- * swizzles line up with Mali swizzles for the XYZW01, but Gallium has an
- * additional "NONE" field that we have to mask out to zero. Additionally,
- * Gallium swizzles are sparse but Mali swizzles are packed */
-
-unsigned
-panfrost_translate_swizzle_4(const unsigned char swizzle[4])
-{
-        unsigned out = 0;
-
-        for (unsigned i = 0; i < 4; ++i) {
-                unsigned translated = (swizzle[i] > PIPE_SWIZZLE_1) ? PIPE_SWIZZLE_0 : swizzle[i];
-                out |= (translated << (3*i));
-        }
-
-        return out;
-}
-
-static unsigned
-panfrost_translate_channel_width(unsigned size)
-{
-        switch (size) {
-        case 4:
-                return MALI_CHANNEL_4;
-        case 8:
-                return MALI_CHANNEL_8;
-        case 16:
-                return MALI_CHANNEL_16;
-        case 32:
-                return MALI_CHANNEL_32;
-        default: {
-                fprintf(stderr, "Invalid width: %d\n", size);
-                assert(0);
-                return 0;
-        }
-        }
-}
-
-static unsigned
-panfrost_translate_channel_type(unsigned type, unsigned size, bool norm)
-{
-        switch (type) {
-        case UTIL_FORMAT_TYPE_UNSIGNED:
-                return norm ? MALI_FORMAT_UNORM : MALI_FORMAT_UINT;
-
-        case UTIL_FORMAT_TYPE_SIGNED:
-                return norm ? MALI_FORMAT_SNORM : MALI_FORMAT_SINT;
-
-        case UTIL_FORMAT_TYPE_FLOAT:
-                if (size == 16) {
-                        /* With FLOAT, fp16 */
-                        return MALI_FORMAT_SINT;
-                } else if (size == 32) {
-                        /* With FLOAT< fp32 */
-                        return MALI_FORMAT_UNORM;
-                } else {
-                        assert(0);
-                        return 0;
-                }
-
-        default:
-                unreachable("Invalid type");
-        }
-}
-
-/* Constructs a mali_format satisfying the specified Gallium format
- * description */
-
-enum mali_format
-panfrost_find_format(const struct util_format_description *desc) {
-        /* Find first non-VOID channel */
-        struct util_format_channel_description chan = desc->channel[0];
-
-        for (unsigned c = 0; c < 4; ++c)
-        {
-                if (desc->channel[c].type == UTIL_FORMAT_TYPE_VOID)
-                        continue;
-
-                chan = desc->channel[c];
-                break;
-        }
-
-        /* Check for special formats */
-        switch (desc->format)
-        {
-        case PIPE_FORMAT_YV12:
-        case PIPE_FORMAT_YV16:
-        case PIPE_FORMAT_IYUV:
-        case PIPE_FORMAT_NV21:
-                fprintf(stderr, "YUV format type %s (%d) is not yet supported, but it's probably close to NV12!\n", desc->name, desc->format);
-                assert(0);
-                break;
-
-        case PIPE_FORMAT_NV12:
-                return MALI_NV12;
-
-        case PIPE_FORMAT_R10G10B10X2_UNORM:
-        case PIPE_FORMAT_B10G10R10X2_UNORM:
-        case PIPE_FORMAT_R10G10B10A2_UNORM:
-        case PIPE_FORMAT_B10G10R10A2_UNORM:
-                return MALI_RGB10_A2_UNORM;
-
-        case PIPE_FORMAT_R10G10B10X2_SNORM:
-        case PIPE_FORMAT_R10G10B10A2_SNORM:
-        case PIPE_FORMAT_B10G10R10A2_SNORM:
-                return MALI_RGB10_A2_SNORM;
-
-        case PIPE_FORMAT_R10G10B10A2_UINT:
-        case PIPE_FORMAT_B10G10R10A2_UINT:
-        case PIPE_FORMAT_R10G10B10A2_USCALED:
-        case PIPE_FORMAT_B10G10R10A2_USCALED:
-                return MALI_RGB10_A2UI;
-
-        case PIPE_FORMAT_R10G10B10A2_SSCALED:
-        case PIPE_FORMAT_B10G10R10A2_SSCALED:
-                return MALI_RGB10_A2I;
-
-        case PIPE_FORMAT_Z32_UNORM:
-                return MALI_Z32_UNORM;
-
-        case PIPE_FORMAT_Z24X8_UNORM:
-        case PIPE_FORMAT_Z24_UNORM_S8_UINT:
-                /* Midgard has no dedicated samplers for Z24S8 and Z24X8
-                 * formats, and the GPU expects the depth to be encoded in an
-                 * IEEE 32-bit float. Turn all Z24_UNORM variants into R32UI
-                 * and let the shader do the conversion using bfe+fmul
-                 * instructions.
-                 */
-                return MALI_R32UI;
-
-        case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
-                /* Z32F = R32F to the hardware */
-                return MALI_R32F;
-
-        case PIPE_FORMAT_B5G6R5_UNORM:
-                return MALI_RGB565;
-
-        case PIPE_FORMAT_B5G5R5A1_UNORM:
-                return MALI_RGB5_A1_UNORM;
-
-        case PIPE_FORMAT_A1B5G5R5_UNORM:
-        case PIPE_FORMAT_X1B5G5R5_UNORM:
-                /* Not supported - this is backwards from OpenGL! */
-                assert(0);
-                break;
-
-        case PIPE_FORMAT_R32_FIXED:
-                return MALI_R32_FIXED;
-        case PIPE_FORMAT_R32G32_FIXED:
-                return MALI_RG32_FIXED;
-        case PIPE_FORMAT_R32G32B32_FIXED:
-                return MALI_RGB32_FIXED;
-        case PIPE_FORMAT_R32G32B32A32_FIXED:
-                return MALI_RGBA32_FIXED;
-
-        case PIPE_FORMAT_R11G11B10_FLOAT:
-                return MALI_R11F_G11F_B10F;
-        case PIPE_FORMAT_R9G9B9E5_FLOAT:
-                return MALI_R9F_G9F_B9F_E5F;
-
-        case PIPE_FORMAT_ETC1_RGB8:
-        case PIPE_FORMAT_ETC2_RGB8:
-        case PIPE_FORMAT_ETC2_SRGB8:
-                return MALI_ETC2_RGB8;
-
-        case PIPE_FORMAT_ETC2_RGB8A1:
-        case PIPE_FORMAT_ETC2_SRGB8A1:
-                return MALI_ETC2_RGB8A1;
-
-        case PIPE_FORMAT_ETC2_RGBA8:
-        case PIPE_FORMAT_ETC2_SRGBA8:
-                return MALI_ETC2_RGBA8;
-
-        case PIPE_FORMAT_ETC2_R11_UNORM:
-                return MALI_ETC2_R11_UNORM;
-        case PIPE_FORMAT_ETC2_R11_SNORM:
-                return MALI_ETC2_R11_SNORM;
-
-        case PIPE_FORMAT_ETC2_RG11_UNORM:
-                return MALI_ETC2_RG11_UNORM;
-        case PIPE_FORMAT_ETC2_RG11_SNORM:
-                return MALI_ETC2_RG11_SNORM;
-
-        default:
-                /* Fallthrough to default */
-                break;
-        }
-
-        if (desc->layout == UTIL_FORMAT_LAYOUT_ASTC) {
-                if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB)
-                        return MALI_ASTC_SRGB_SUPP;
-                else
-                        return MALI_ASTC_HDR_SUPP;
-        }
-
-        /* Formats must match in channel count */
-        assert(desc->nr_channels >= 1 && desc->nr_channels <= 4);
-        unsigned format = MALI_NR_CHANNELS(desc->nr_channels);
-
-        switch (chan.type)
-        {
-        case UTIL_FORMAT_TYPE_UNSIGNED:
-        case UTIL_FORMAT_TYPE_SIGNED:
-        case UTIL_FORMAT_TYPE_FIXED:
-                /* Channel width */
-                format |= panfrost_translate_channel_width(chan.size);
-
-                /* Channel type */
-                format |= panfrost_translate_channel_type(chan.type, chan.size, chan.normalized);
-                break;
-
-        case UTIL_FORMAT_TYPE_FLOAT:
-                /* Float formats use a special width and encode width
-                 * with type mixed */
-
-                format |= MALI_CHANNEL_FLOAT;
-                format |= panfrost_translate_channel_type(chan.type, chan.size, chan.normalized);
-                break;
-
-        default:
-                printf("%s\n", util_format_name(desc->format));
-                unreachable("Invalid format type");
-        }
-
-        return (enum mali_format) format;
-}
-
-void
-panfrost_invert_swizzle(const unsigned char *in, unsigned char *out)
-{
-        /* First, default to all zeroes to prevent uninitialized junk */
-
-        for (unsigned c = 0; c < 4; ++c)
-                out[c] = PIPE_SWIZZLE_0;
-
-        /* Now "do" what the swizzle says */
-
-        for (unsigned c = 0; c < 4; ++c) {
-                unsigned char i = in[c];
-
-                /* Who cares? */
-                assert(PIPE_SWIZZLE_X == 0);
-                if (i > PIPE_SWIZZLE_W)
-                        continue;
-
-                /* Invert */
-                unsigned idx = i - PIPE_SWIZZLE_X;
-                out[idx] = PIPE_SWIZZLE_X + c;
-        }
-}
-
-/* Is a format encoded like Z24S8 and therefore compatible for render? */
-bool
-panfrost_is_z24s8_variant(enum pipe_format fmt)
-{
-        switch (fmt) {
-                case PIPE_FORMAT_Z24_UNORM_S8_UINT:
-                case PIPE_FORMAT_Z24X8_UNORM:
-                        return true;
-                default:
-                        return false;
-        }
-}
diff --git a/src/gallium/drivers/panfrost/pan_format.h b/src/gallium/drivers/panfrost/pan_format.h
deleted file mode 100644 (file)
index b82493d..0000000
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * © Copyright 2018 Alyssa Rosenzweig
- *
- * 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.
- *
- */
-
-#ifndef __PAN_FORMAT_H__
-#define __PAN_FORMAT_H__
-
-#include "pan_context.h"
-#include "util/format/u_format.h"
-
-unsigned
-panfrost_translate_swizzle_4(const unsigned char swizzle[4]);
-
-enum mali_format
-panfrost_find_format(const struct util_format_description *desc);
-
-void
-panfrost_invert_swizzle(const unsigned char *in, unsigned char *out);
-
-bool
-panfrost_is_z24s8_variant(enum pipe_format fmt);
-
-static inline unsigned
-panfrost_get_default_swizzle(unsigned components)
-{
-        switch (components) {
-        case 1:
-                return (MALI_CHANNEL_RED << 0) | (MALI_CHANNEL_ZERO << 3) |
-                        (MALI_CHANNEL_ZERO << 6) | (MALI_CHANNEL_ONE << 9);
-        case 2:
-                return (MALI_CHANNEL_RED << 0) | (MALI_CHANNEL_GREEN << 3) |
-                        (MALI_CHANNEL_ZERO << 6) | (MALI_CHANNEL_ONE << 9);
-        case 3:
-                return (MALI_CHANNEL_RED << 0) | (MALI_CHANNEL_GREEN << 3) |
-                        (MALI_CHANNEL_BLUE << 6) | (MALI_CHANNEL_ONE << 9);
-        case 4:
-                return (MALI_CHANNEL_RED << 0) | (MALI_CHANNEL_GREEN << 3) |
-                        (MALI_CHANNEL_BLUE << 6) | (MALI_CHANNEL_ALPHA << 9);
-        default:
-                unreachable("Invalid number of components");
-        }
-}
-
-#endif
-
-
index e2d71c57a10b2271b206785a97a1271f5bae205c..80b25a3893cb0e5e7f0381a0ab48234fdf06fbbe 100644 (file)
@@ -24,7 +24,6 @@
 
 #include "pan_context.h"
 #include "pan_util.h"
-#include "pan_format.h"
 #include "panfrost-quirks.h"
 
 #include "util/format/u_format.h"
index c2cc11d111968b6bc6510e5537ebfc6610a1eec6..799fac140847fe1386628849b8d6dd4a9dba943a 100644 (file)
@@ -25,9 +25,6 @@
 #include "pan_bo.h"
 #include "pan_context.h"
 #include "pan_util.h"
-#include "pan_format.h"
-
-#include "util/format/u_format.h"
 
 static struct mali_rt_format
 panfrost_mfbd_format(struct pipe_surface *surf)
index 6e80649abf6496d945decdc81e30f40839088d84..fa62916a104e439d61388c0aa96f9a77baa822d5 100644 (file)
@@ -25,7 +25,6 @@
 #include "pan_bo.h"
 #include "pan_context.h"
 #include "pan_util.h"
-#include "pan_format.h"
 
 #include "util/format/u_format.h"
 
index 52496aa26d7c8fda7e7c495cf8f4e26107b8d94a..0ec4d5633d2f36e558f32eccf5e5c7ba584dbddc 100644 (file)
@@ -25,7 +25,6 @@
 
 #include "pan_bo.h"
 #include "pan_context.h"
-#include "pan_format.h"
 #include "util/u_prim.h"
 
 static mali_ptr
index 650289586fa9185df7c4e5b3230c27dcc64515b6..6a82fb30b96be822b2900c6afd6396cc4f0b5077 100644 (file)
@@ -24,6 +24,7 @@ libpanfrost_encoder_files = files(
 
   'pan_afbc.c',
   'pan_attributes.c',
+  'pan_format.c',
   'pan_invocation.c',
   'pan_sampler.c',
   'pan_tiler.c',
diff --git a/src/panfrost/encoder/pan_format.c b/src/panfrost/encoder/pan_format.c
new file mode 100644 (file)
index 0000000..8025307
--- /dev/null
@@ -0,0 +1,282 @@
+/*
+ * Copyright (C) 2019 Collabora, Ltd.
+ *
+ * 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@collabora.com>
+ */
+
+#include <stdio.h>
+#include "panfrost-job.h"
+#include "pan_texture.h"
+
+static unsigned
+panfrost_translate_channel_width(unsigned size)
+{
+        switch (size) {
+        case 4:
+                return MALI_CHANNEL_4;
+        case 8:
+                return MALI_CHANNEL_8;
+        case 16:
+                return MALI_CHANNEL_16;
+        case 32:
+                return MALI_CHANNEL_32;
+        default:
+                unreachable("Invalid format width\n");
+        }
+}
+
+static unsigned
+panfrost_translate_channel_type(unsigned type, unsigned size, bool norm)
+{
+        switch (type) {
+        case UTIL_FORMAT_TYPE_UNSIGNED:
+                return norm ? MALI_FORMAT_UNORM : MALI_FORMAT_UINT;
+
+        case UTIL_FORMAT_TYPE_SIGNED:
+                return norm ? MALI_FORMAT_SNORM : MALI_FORMAT_SINT;
+
+        case UTIL_FORMAT_TYPE_FLOAT:
+                /* fp16 -- SINT, fp32 -- UNORM ... gotta use those bits */
+
+                if (size == 16)
+                        return MALI_FORMAT_SINT;
+                else if (size == 32)
+                        return MALI_FORMAT_UNORM;
+                else
+                        unreachable("Invalid float size");
+
+        default:
+                unreachable("Invalid type");
+        }
+}
+
+/* Constructs a mali_format satisfying the specified Gallium format
+ * description */
+
+enum mali_format
+panfrost_find_format(const struct util_format_description *desc)
+{
+        /* Find first non-VOID channel */
+        struct util_format_channel_description chan = desc->channel[0];
+
+        for (unsigned c = 0; c < 4; ++c)
+        {
+                if (desc->channel[c].type == UTIL_FORMAT_TYPE_VOID)
+                        continue;
+
+                chan = desc->channel[c];
+                break;
+        }
+
+        /* Check for special formats */
+        switch (desc->format)
+        {
+        case PIPE_FORMAT_NV12:
+                return MALI_NV12;
+
+        case PIPE_FORMAT_R10G10B10X2_UNORM:
+        case PIPE_FORMAT_B10G10R10X2_UNORM:
+        case PIPE_FORMAT_R10G10B10A2_UNORM:
+        case PIPE_FORMAT_B10G10R10A2_UNORM:
+                return MALI_RGB10_A2_UNORM;
+
+        case PIPE_FORMAT_R10G10B10X2_SNORM:
+        case PIPE_FORMAT_R10G10B10A2_SNORM:
+        case PIPE_FORMAT_B10G10R10A2_SNORM:
+                return MALI_RGB10_A2_SNORM;
+
+        case PIPE_FORMAT_R10G10B10A2_UINT:
+        case PIPE_FORMAT_B10G10R10A2_UINT:
+        case PIPE_FORMAT_R10G10B10A2_USCALED:
+        case PIPE_FORMAT_B10G10R10A2_USCALED:
+                return MALI_RGB10_A2UI;
+
+        case PIPE_FORMAT_R10G10B10A2_SSCALED:
+        case PIPE_FORMAT_B10G10R10A2_SSCALED:
+                return MALI_RGB10_A2I;
+
+        case PIPE_FORMAT_Z32_UNORM:
+                return MALI_Z32_UNORM;
+
+        case PIPE_FORMAT_Z24X8_UNORM:
+        case PIPE_FORMAT_Z24_UNORM_S8_UINT:
+                /* Midgard has no dedicated samplers for Z24S8 and Z24X8
+                 * formats, and the GPU expects the depth to be encoded in an
+                 * IEEE 32-bit float. Turn all Z24_UNORM variants into R32UI
+                 * and let the shader do the conversion using bfe+fmul
+                 * instructions.
+                 */
+                return MALI_R32UI;
+
+        case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
+                /* Z32F = R32F to the hardware */
+                return MALI_R32F;
+
+        case PIPE_FORMAT_B5G6R5_UNORM:
+                return MALI_RGB565;
+
+        case PIPE_FORMAT_B5G5R5A1_UNORM:
+                return MALI_RGB5_A1_UNORM;
+
+        case PIPE_FORMAT_A1B5G5R5_UNORM:
+        case PIPE_FORMAT_X1B5G5R5_UNORM:
+                /* Not supported - this is backwards from OpenGL! */
+                assert(0);
+                break;
+
+        case PIPE_FORMAT_R32_FIXED:
+                return MALI_R32_FIXED;
+        case PIPE_FORMAT_R32G32_FIXED:
+                return MALI_RG32_FIXED;
+        case PIPE_FORMAT_R32G32B32_FIXED:
+                return MALI_RGB32_FIXED;
+        case PIPE_FORMAT_R32G32B32A32_FIXED:
+                return MALI_RGBA32_FIXED;
+
+        case PIPE_FORMAT_R11G11B10_FLOAT:
+                return MALI_R11F_G11F_B10F;
+        case PIPE_FORMAT_R9G9B9E5_FLOAT:
+                return MALI_R9F_G9F_B9F_E5F;
+
+        case PIPE_FORMAT_ETC1_RGB8:
+        case PIPE_FORMAT_ETC2_RGB8:
+        case PIPE_FORMAT_ETC2_SRGB8:
+                return MALI_ETC2_RGB8;
+
+        case PIPE_FORMAT_ETC2_RGB8A1:
+        case PIPE_FORMAT_ETC2_SRGB8A1:
+                return MALI_ETC2_RGB8A1;
+
+        case PIPE_FORMAT_ETC2_RGBA8:
+        case PIPE_FORMAT_ETC2_SRGBA8:
+                return MALI_ETC2_RGBA8;
+
+        case PIPE_FORMAT_ETC2_R11_UNORM:
+                return MALI_ETC2_R11_UNORM;
+        case PIPE_FORMAT_ETC2_R11_SNORM:
+                return MALI_ETC2_R11_SNORM;
+
+        case PIPE_FORMAT_ETC2_RG11_UNORM:
+                return MALI_ETC2_RG11_UNORM;
+        case PIPE_FORMAT_ETC2_RG11_SNORM:
+                return MALI_ETC2_RG11_SNORM;
+
+        default:
+                /* Fallthrough to default */
+                break;
+        }
+
+        if (desc->layout == UTIL_FORMAT_LAYOUT_ASTC) {
+                if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB)
+                        return MALI_ASTC_SRGB_SUPP;
+                else
+                        return MALI_ASTC_HDR_SUPP;
+        }
+
+        /* Formats must match in channel count */
+        assert(desc->nr_channels >= 1 && desc->nr_channels <= 4);
+        unsigned format = MALI_NR_CHANNELS(desc->nr_channels);
+
+        switch (chan.type)
+        {
+        case UTIL_FORMAT_TYPE_UNSIGNED:
+        case UTIL_FORMAT_TYPE_SIGNED:
+        case UTIL_FORMAT_TYPE_FIXED:
+                /* Channel width */
+                format |= panfrost_translate_channel_width(chan.size);
+
+                /* Channel type */
+                format |= panfrost_translate_channel_type(chan.type, chan.size, chan.normalized);
+                break;
+
+        case UTIL_FORMAT_TYPE_FLOAT:
+                /* Float formats use a special width and encode width
+                 * with type mixed */
+
+                format |= MALI_CHANNEL_FLOAT;
+                format |= panfrost_translate_channel_type(chan.type, chan.size, chan.normalized);
+                break;
+
+        default:
+                printf("%s\n", util_format_name(desc->format));
+                unreachable("Invalid format type");
+        }
+
+        return (enum mali_format) format;
+}
+
+/* Is a format encoded like Z24S8 and therefore compatible for render? */
+
+bool
+panfrost_is_z24s8_variant(enum pipe_format fmt)
+{
+        switch (fmt) {
+                case PIPE_FORMAT_Z24_UNORM_S8_UINT:
+                case PIPE_FORMAT_Z24X8_UNORM:
+                        return true;
+                default:
+                        return false;
+        }
+}
+
+/* Translate a PIPE swizzle quad to a 12-bit Mali swizzle code. PIPE
+ * swizzles line up with Mali swizzles for the XYZW01, but PIPE swizzles have
+ * an additional "NONE" field that we have to mask out to zero. Additionally,
+ * PIPE swizzles are sparse but Mali swizzles are packed */
+
+unsigned
+panfrost_translate_swizzle_4(const unsigned char swizzle[4])
+{
+        unsigned out = 0;
+
+        for (unsigned i = 0; i < 4; ++i) {
+                unsigned translated = (swizzle[i] > PIPE_SWIZZLE_1) ? PIPE_SWIZZLE_0 : swizzle[i];
+                out |= (translated << (3*i));
+        }
+
+        return out;
+}
+
+void
+panfrost_invert_swizzle(const unsigned char *in, unsigned char *out)
+{
+        /* First, default to all zeroes to prevent uninitialized junk */
+
+        for (unsigned c = 0; c < 4; ++c)
+                out[c] = PIPE_SWIZZLE_0;
+
+        /* Now "do" what the swizzle says */
+
+        for (unsigned c = 0; c < 4; ++c) {
+                unsigned char i = in[c];
+
+                /* Who cares? */
+                assert(PIPE_SWIZZLE_X == 0);
+                if (i > PIPE_SWIZZLE_W)
+                        continue;
+
+                /* Invert */
+                unsigned idx = i - PIPE_SWIZZLE_X;
+                out[idx] = PIPE_SWIZZLE_X + c;
+        }
+}
index 043fcc3af10994b0552e3dcfba26c5946ebc341a..29be91cca830a7fff0b89b5e1d182bd214dc6b14 100644 (file)
@@ -63,4 +63,39 @@ panfrost_format_supports_afbc(enum pipe_format format);
 unsigned
 panfrost_afbc_header_size(unsigned width, unsigned height);
 
+/* Formats */
+
+enum mali_format
+panfrost_find_format(const struct util_format_description *desc);
+
+bool
+panfrost_is_z24s8_variant(enum pipe_format fmt);
+
+unsigned
+panfrost_translate_swizzle_4(const unsigned char swizzle[4]);
+
+void
+panfrost_invert_swizzle(const unsigned char *in, unsigned char *out);
+
+static inline unsigned
+panfrost_get_default_swizzle(unsigned components)
+{
+        switch (components) {
+        case 1:
+                return (MALI_CHANNEL_RED << 0) | (MALI_CHANNEL_ZERO << 3) |
+                        (MALI_CHANNEL_ZERO << 6) | (MALI_CHANNEL_ONE << 9);
+        case 2:
+                return (MALI_CHANNEL_RED << 0) | (MALI_CHANNEL_GREEN << 3) |
+                        (MALI_CHANNEL_ZERO << 6) | (MALI_CHANNEL_ONE << 9);
+        case 3:
+                return (MALI_CHANNEL_RED << 0) | (MALI_CHANNEL_GREEN << 3) |
+                        (MALI_CHANNEL_BLUE << 6) | (MALI_CHANNEL_ONE << 9);
+        case 4:
+                return (MALI_CHANNEL_RED << 0) | (MALI_CHANNEL_GREEN << 3) |
+                        (MALI_CHANNEL_BLUE << 6) | (MALI_CHANNEL_ALPHA << 9);
+        default:
+                unreachable("Invalid number of components");
+        }
+}
+
 #endif