Mesa (master): util: add new helper util_format_rgb_to_bgr

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri Mar 19 16:20:22 UTC 2021


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

Author: Marek Olšák <marek.olsak at amd.com>
Date:   Mon Mar 15 19:55:35 2021 -0400

util: add new helper util_format_rgb_to_bgr

We have BGR formats for all RGB formats where it matters except
USCALED/SSCALED. radeonsi will use it.

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

---

 src/util/format/u_format.c | 101 +++++++++++++++++++++++++++++++++++++++++++++
 src/util/format/u_format.h |   7 ++++
 2 files changed, 108 insertions(+)

diff --git a/src/util/format/u_format.c b/src/util/format/u_format.c
index dd7247f79d3..43f57caa882 100644
--- a/src/util/format/u_format.c
+++ b/src/util/format/u_format.c
@@ -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;
+   }
+}
diff --git a/src/util/format/u_format.h b/src/util/format/u_format.h
index 984f8469ddd..9911ff5a40d 100644
--- a/src/util/format/u_format.h
+++ b/src/util/format/u_format.h
@@ -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



More information about the mesa-commit mailing list