util: add new helper util_format_rgb_to_bgr
authorMarek Olšák <marek.olsak@amd.com>
Mon, 15 Mar 2021 23:55:35 +0000 (19:55 -0400)
committerMarge Bot <eric+marge@anholt.net>
Fri, 19 Mar 2021 16:05:02 +0000 (16:05 +0000)
We have BGR formats for all RGB formats where it matters except
USCALED/SSCALED. radeonsi will use it.

Reviewed-by: Bas Nieuwenhuizen <bas@basnieuwenhuizen.nl>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/9615>

src/util/format/u_format.c
src/util/format/u_format.h

index dd7247f..43f57ca 100644 (file)
@@ -994,3 +994,104 @@ util_format_snorm8_to_sint8(enum pipe_format format)
       return format;
    }
 }
+
+/**
+ * If the format is RGB, return BGR. If the format is BGR, return RGB.
+ * This may fail by returning PIPE_FORMAT_NONE.
+ */
+enum pipe_format
+util_format_rgb_to_bgr(enum pipe_format format)
+{
+#define REMAP_RGB_ONE(r, rs, g, gs, b, bs, type) \
+   case PIPE_FORMAT_##r##rs##g##gs##b##bs##_##type: \
+      return PIPE_FORMAT_##b##bs##g##gs##r##rs##_##type;
+
+#define REMAP_RGB(rs, gs, bs, type) \
+   REMAP_RGB_ONE(R, rs, G, gs, B, bs, type) \
+   REMAP_RGB_ONE(B, bs, G, gs, R, rs, type) \
+
+#define REMAP_RGBA_ONE(r, rs, g, gs, b, bs, a, as, type) \
+   case PIPE_FORMAT_##r##rs##g##gs##b##bs##a##as##_##type: \
+      return PIPE_FORMAT_##b##bs##g##gs##r##rs##a##as##_##type;
+
+#define REMAP_ARGB_ONE(a, as, r, rs, g, gs, b, bs, type) \
+   case PIPE_FORMAT_##a##as##r##rs##g##gs##b##bs##_##type: \
+      return PIPE_FORMAT_##a##as##b##bs##g##gs##r##rs##_##type;
+
+#define REMAP_RGB_AX(A, rs, gs, bs, as, type) \
+   REMAP_RGBA_ONE(R, rs, G, gs, B, bs, A, as, type) \
+   REMAP_RGBA_ONE(B, bs, G, gs, R, rs, A, as, type) \
+
+#define REMAP_AX_RGB(A, rs, gs, bs, as, type) \
+   REMAP_ARGB_ONE(A, as, R, rs, G, gs, B, bs, type) \
+   REMAP_ARGB_ONE(A, as, B, bs, G, gs, R, rs, type) \
+
+#define REMAP_RGBA(rs, gs, bs, as, type) REMAP_RGB_AX(A, rs, gs, bs, as, type)
+#define REMAP_RGBX(rs, gs, bs, as, type) REMAP_RGB_AX(X, rs, gs, bs, as, type)
+#define REMAP_ARGB(rs, gs, bs, as, type) REMAP_AX_RGB(A, rs, gs, bs, as, type)
+#define REMAP_XRGB(rs, gs, bs, as, type) REMAP_AX_RGB(X, rs, gs, bs, as, type)
+
+#define REMAP_RGBA_ALL(rs, gs, bs, as, type) \
+   REMAP_RGBA(rs, gs, bs, as, type) \
+   REMAP_RGBX(rs, gs, bs, as, type) \
+   REMAP_ARGB(rs, gs, bs, as, type) \
+   REMAP_XRGB(rs, gs, bs, as, type)
+
+   switch (format) {
+   REMAP_RGB(3, 3, 2, UNORM);
+   REMAP_RGB(3, 3, 2, UINT);
+   REMAP_RGB(5, 6, 5, SRGB);
+   REMAP_RGB(5, 6, 5, UNORM);
+   REMAP_RGB(5, 6, 5, UINT);
+   REMAP_RGB(8, 8, 8, SRGB);
+   REMAP_RGB(8, 8, 8, UNORM);
+   REMAP_RGB(8, 8, 8, SNORM);
+   REMAP_RGB(8, 8, 8, UINT);
+   REMAP_RGB(8, 8, 8, SINT);
+   REMAP_RGB(8, 8, 8, USCALED);
+   REMAP_RGB(8, 8, 8, SSCALED);
+
+   /* Complete format sets. */
+   REMAP_RGBA_ALL(5, 5, 5, 1, UNORM);
+   REMAP_RGBA_ALL(8, 8, 8, 8, SRGB);
+   REMAP_RGBA_ALL(8, 8, 8, 8, UNORM);
+   REMAP_RGBA_ALL(8, 8, 8, 8, SNORM);
+   REMAP_RGBA_ALL(8, 8, 8, 8, SINT);
+
+   /* Format sets missing XRGB/XBGR. */
+   REMAP_RGBA(4, 4, 4, 4, UNORM);
+   REMAP_RGBX(4, 4, 4, 4, UNORM);
+   REMAP_ARGB(4, 4, 4, 4, UNORM);
+
+   REMAP_RGBA(8, 8, 8, 8, UINT);
+   REMAP_RGBX(8, 8, 8, 8, UINT);
+   REMAP_ARGB(8, 8, 8, 8, UINT);
+
+   REMAP_RGBA(10, 10, 10, 2, UNORM);
+   REMAP_RGBX(10, 10, 10, 2, UNORM);
+   REMAP_ARGB(10, 10, 10, 2, UNORM);
+
+   /* Format sets missing a half of combinations. */
+   REMAP_RGBA(4, 4, 4, 4, UINT);
+   REMAP_ARGB(4, 4, 4, 4, UINT);
+
+   REMAP_RGBA(5, 5, 5, 1, UINT);
+   REMAP_ARGB(5, 5, 5, 1, UINT);
+
+   REMAP_RGBA(10, 10, 10, 2, SNORM);
+   REMAP_RGBX(10, 10, 10, 2, SNORM);
+
+   REMAP_RGBA(10, 10, 10, 2, UINT);
+   REMAP_ARGB(10, 10, 10, 2, UINT);
+
+   /* Format sets having only RGBA/BGRA. */
+   REMAP_RGBA(8, 8, 8, 8, USCALED);
+   REMAP_RGBA(8, 8, 8, 8, SSCALED);
+   REMAP_RGBA(10, 10, 10, 2, SINT);
+   REMAP_RGBA(10, 10, 10, 2, USCALED);
+   REMAP_RGBA(10, 10, 10, 2, SSCALED);
+
+   default:
+      return PIPE_FORMAT_NONE;
+   }
+}
index 984f846..9911ff5 100644 (file)
@@ -1625,6 +1625,13 @@ util_copy_rect(ubyte * dst, enum pipe_format format,
                unsigned width, unsigned height, const ubyte * src,
                int src_stride, unsigned src_x, unsigned src_y);
 
+/**
+ * If the format is RGB, return BGR. If the format is BGR, return RGB.
+ * This may fail by returning PIPE_FORMAT_NONE.
+ */
+enum pipe_format
+util_format_rgb_to_bgr(enum pipe_format format);
+
 #ifdef __cplusplus
 } // extern "C" {
 #endif