[Mesa-dev] [PATCH 1/3] mesa: Add _mesa_format_fallback_rgbx_to_rgba() [v2]

Chad Versace chadversary at chromium.org
Tue Jun 27 18:00:46 UTC 2017


The new function takes a mesa_format and, if the format is an alpha
format with a non-alpha variant, returns the non-alpha format.
Otherwise, it returns the original format.

Example:
  input -> output

  // Fallback exists
  MESA_FORMAT_R8G8B8X8_UNORM -> MESA_FORMAT_R8G8B8A8_UNORM
  MESA_FORMAT_RGBX_UNORM16 -> MESA_FORMAT_RGBA_UNORM16

  // No fallback
  MESA_FORMAT_R8G8B8A8_UNORM -> MESA_FORMAT_R8G8B8A8_UNORM
  MESA_FORMAT_Z_FLOAT32 -> MESA_FORMAT_Z_FLOAT32

i965 will use this for EGLImages and DRIimages.

v2 (Jason Ekstrand):
 - Use mako
 - Rework to be easier to read
 - Write directly to the output file
---
 src/mesa/Android.gen.mk          |  12 +++++
 src/mesa/Makefile.am             |   7 +++
 src/mesa/Makefile.sources        |   1 +
 src/mesa/main/.gitignore         |   1 +
 src/mesa/main/format_fallback.py | 104 +++++++++++++++++++++++++++++++++++++++
 src/mesa/main/formats.h          |   6 +++
 6 files changed, 131 insertions(+)
 create mode 100644 src/mesa/main/format_fallback.py

diff --git a/src/mesa/Android.gen.mk b/src/mesa/Android.gen.mk
index 366a6b1036e..8d242600f54 100644
--- a/src/mesa/Android.gen.mk
+++ b/src/mesa/Android.gen.mk
@@ -34,6 +34,7 @@ sources := \
 	main/enums.c \
 	main/api_exec.c \
 	main/dispatch.h \
+	main/format_fallback.c \
 	main/format_pack.c \
 	main/format_unpack.c \
 	main/format_info.h \
@@ -123,6 +124,17 @@ $(intermediates)/main/get_hash.h: $(glapi)/gl_and_es_API.xml \
                $(LOCAL_PATH)/main/get_hash_params.py $(GET_HASH_GEN)
 	$(call es-gen)
 
+FORMAT_FALLBACK := $(LOCAL_PATH)/main/format_fallback.py
+format_fallback_deps := \
+	$(LOCAL_PATH)/main/formats.csv \
+	$(LOCAL_PATH)/main/format_parser.py \
+	$(FORMAT_FALLBACK)
+
+$(intermediates)/main/format_fallback.c: PRIVATE_SCRIPT := $(MESA_PYTHON2) $(FORMAT_FALLBACK)
+$(intermediates)/main/format_fallback.c: PRIVATE_XML :=
+$(intermediates)/main/format_fallback.c: $(format_fallback_deps)
+	$(call es-gen, $<)
+
 FORMAT_INFO := $(LOCAL_PATH)/main/format_info.py
 format_info_deps := \
 	$(LOCAL_PATH)/main/formats.csv \
diff --git a/src/mesa/Makefile.am b/src/mesa/Makefile.am
index 53f311d2a97..97a9bbd8c25 100644
--- a/src/mesa/Makefile.am
+++ b/src/mesa/Makefile.am
@@ -37,6 +37,7 @@ include Makefile.sources
 
 EXTRA_DIST = \
 	drivers/SConscript \
+	main/format_fallback.py \
 	main/format_info.py \
 	main/format_pack.py \
 	main/format_parser.py \
@@ -54,6 +55,7 @@ EXTRA_DIST = \
 
 BUILT_SOURCES = \
 	main/get_hash.h \
+	main/format_fallback.c \
 	main/format_info.h \
 	main/format_pack.c \
 	main/format_unpack.c \
@@ -70,6 +72,11 @@ main/get_hash.h: ../mapi/glapi/gen/gl_and_es_API.xml main/get_hash_params.py \
 	$(PYTHON_GEN) $(srcdir)/main/get_hash_generator.py \
 		-f $(srcdir)/../mapi/glapi/gen/gl_and_es_API.xml > $@
 
+main/format_fallback.c: main/format_fallback.py \
+                        main/format_parser.py \
+	                main/formats.csv
+	$(PYTHON_GEN) $(srcdir)/main/format_fallback.py $(srcdir)/main/formats.csv $@
+
 main/format_info.h: main/formats.csv \
                     main/format_parser.py main/format_info.py
 	$(PYTHON_GEN) $(srcdir)/main/format_info.py $(srcdir)/main/formats.csv > $@
diff --git a/src/mesa/Makefile.sources b/src/mesa/Makefile.sources
index b80882fb8de..86fbf3974ed 100644
--- a/src/mesa/Makefile.sources
+++ b/src/mesa/Makefile.sources
@@ -94,6 +94,7 @@ MAIN_FILES = \
 	main/ffvertex_prog.h \
 	main/fog.c \
 	main/fog.h \
+	main/format_fallback.c \
 	main/format_info.h \
 	main/format_pack.h \
 	main/format_pack.c \
diff --git a/src/mesa/main/.gitignore b/src/mesa/main/.gitignore
index 836d8f104a8..8cc33cfee68 100644
--- a/src/mesa/main/.gitignore
+++ b/src/mesa/main/.gitignore
@@ -4,6 +4,7 @@ enums.c
 remap_helper.h
 get_hash.h
 get_hash.h.tmp
+format_fallback.c
 format_info.h
 format_info.c
 format_pack.c
diff --git a/src/mesa/main/format_fallback.py b/src/mesa/main/format_fallback.py
new file mode 100644
index 00000000000..e3b9916f6ee
--- /dev/null
+++ b/src/mesa/main/format_fallback.py
@@ -0,0 +1,104 @@
+COPYRIGHT = """\
+/*
+ * Copyright 2017 Google
+ *
+ * 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, sub license, 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 NON-INFRINGEMENT.
+ * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS 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.
+ */
+"""
+
+# stdlib
+import argparse
+from sys import stdout
+from mako.template import Template
+
+# local
+import format_parser
+
+def parse_args():
+    p = argparse.ArgumentParser()
+    p.add_argument("csv")
+    p.add_argument("out")
+    return p.parse_args()
+
+def get_rgbx_to_rgba_map(formats):
+    names = {fmt.name for fmt in formats}
+
+    for fmt in formats:
+        if not fmt.has_channel('r') or not fmt.has_channel('x'):
+            continue
+
+        # The condition above will still let MESA_FORMAT_R9G9B9E5_FLOAT
+        # through.  We need to ensure it actually has an X in the name.
+        if not 'X' in fmt.name:
+            continue
+
+        rgbx_name = fmt.name
+        rgba_name = rgbx_name.replace("X", "A")
+        if rgba_name not in names:
+            continue;
+
+        yield rgbx_name, rgba_name
+
+TEMPLATE = Template(COPYRIGHT + """
+#include "formats.h"
+
+/**
+ * If the format has an alpha channel, and there exists a non-alpha
+ * variant of the format with an identical bit layout, then return
+ * the non-alpha format. Otherwise return the original format.
+ *
+ * Examples:
+ *    Fallback exists:
+ *       MESA_FORMAT_R8G8B8X8_UNORM -> MESA_FORMAT_R8G8B8A8_UNORM
+ *       MESA_FORMAT_RGBX_UNORM16 -> MESA_FORMAT_RGBA_UNORM16
+ *
+ *    No fallback:
+ *       MESA_FORMAT_R8G8B8A8_UNORM -> MESA_FORMAT_R8G8B8A8_UNORM
+ *       MESA_FORMAT_Z_FLOAT32 -> MESA_FORMAT_Z_FLOAT32
+ */
+mesa_format
+_mesa_format_fallback_rgbx_to_rgba(mesa_format format)
+{
+   switch (format) {
+%for rgbx, rgba in rgbx_to_rgba_map:
+   case ${rgbx}:
+      return ${rgba};
+%endfor
+   default:
+      return format;
+   }
+}
+""");
+
+def main():
+    pargs = parse_args()
+
+    formats = list(format_parser.parse(pargs.csv))
+
+    template_env = {
+        'rgbx_to_rgba_map': list(get_rgbx_to_rgba_map(formats)),
+    }
+
+    with open(pargs.out, 'w') as f:
+        f.write(TEMPLATE.render(**template_env))
+
+if __name__ == "__main__":
+    main()
diff --git a/src/mesa/main/formats.h b/src/mesa/main/formats.h
index b88466f0368..3d240a05fa6 100644
--- a/src/mesa/main/formats.h
+++ b/src/mesa/main/formats.h
@@ -748,6 +748,9 @@ _mesa_test_formats(void);
 extern mesa_format
 _mesa_get_srgb_format_linear(mesa_format format);
 
+extern mesa_format
+_mesa_get_rgbx_format_rgba(mesa_format format);
+
 extern mesa_format
 _mesa_get_uncompressed_format(mesa_format format);
 
@@ -762,6 +765,9 @@ _mesa_format_matches_format_and_type(mesa_format mesa_format,
 				     GLenum format, GLenum type,
 				     GLboolean swapBytes, GLenum *error);
 
+mesa_format
+_mesa_format_fallback_rgbx_to_rgba(mesa_format format);
+
 #ifdef __cplusplus
 }
 #endif
-- 
2.13.0



More information about the mesa-dev mailing list