Note that the script is not run automatically as part of the build
process and instead vr-format-table.h is checked into git.
---

I’ve split the script out to make it easier to review.

Thanks for the feedback Dylan.

 tests/vulkan/vkrunner/make-formats.py | 137 ++++++++++++++++++++++++++++++++++
 1 file changed, 137 insertions(+)
 create mode 100755 tests/vulkan/vkrunner/make-formats.py

diff --git a/tests/vulkan/vkrunner/make-formats.py 
b/tests/vulkan/vkrunner/make-formats.py
new file mode 100755
index 000000000..01e9000de
--- /dev/null
+++ b/tests/vulkan/vkrunner/make-formats.py
@@ -0,0 +1,137 @@
+#!/usr/bin/env python
+
+# Copyright (C) 2018 Intel Corporation
+
+# 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.
+
+from __future__ import (
+    absolute_import, division, print_function, unicode_literals
+)
+
+# This script is used to generate vr-format-table.h from vulkan.h. It
+# is not run automatically as part of the build process but if need be
+# it can be used to update the file as follows:
+#
+# ./make-formats.py < /usr/include/vulkan/vulkan.h > vr-format-table.h
+
+import re
+import sys
+from mako.template import Template
+
+FORMAT_RE = re.compile(r'\bVK_FORMAT_([A-Z0-9_]+)\b')
+SKIP_RE = re.compile(r'(?:_BLOCK(?:_IMG)?|_KHR|^UNDEFINED|'
+                     r'^RANGE_SIZE|^MAX_ENUM|_RANGE)$')
+COMPONENT_RE = re.compile('([A-Z]+)([0-9]+)')
+PACK_RE = re.compile('PACK([0-9]+)$')
+
+SWIZZLES = ['RGBA', 'BGRA', 'ARGB', 'ABGR']
+
+TEMPLATE="""\
+/* Automatically generated by make-formats.py */
+static const struct vr_format
+formats[] = {
+% for format in formats:
+       {
+               .vk_format = VK_FORMAT_${format['name']},
+               .name = "${format['name']}",
+               .packed_size = ${format['packed_size']},
+               .swizzle = VR_FORMAT_SWIZZLE_${format['swizzle']},
+               .mode = VR_FORMAT_MODE_${format['mode']},
+               .n_components = ${len(format['components'])},
+               .components = {
+                       % for letter, size in format['components']:
+                       { .bits = ${size} },
+                       % endfor
+               }
+       },
+% endfor
+};"""
+
+
+def get_format_names(data):
+    in_enum = False
+
+    for line in data:
+        if line.startswith('typedef enum VkFormat '):
+            in_enum = True
+        elif line.startswith('}'):
+            in_enum = False
+        if not in_enum:
+            continue
+
+        md = FORMAT_RE.search(line)
+        if md is None:
+            continue
+        name = md.group(1)
+        if SKIP_RE.search(name):
+            continue
+        yield name
+
+
+def get_formats(data):
+    for name in sorted(set(get_format_names(data))):
+        parts = name.split('_')
+
+        components = get_components(parts[0])
+
+        if components is None:
+            continue
+
+        swizzle = get_swizzle(components)
+
+        if len(parts) >= 3:
+            md = PACK_RE.match(parts[2])
+            packed_size = int(md.group(1))
+        else:
+            packed_size = 0
+
+        yield {'name': name,
+               'packed_size': packed_size,
+               'swizzle': swizzle,
+               'mode': parts[1],
+               'components': components}
+
+
+def get_components(name):
+    components = [(md.group(1), int(md.group(2)))
+                  for md in COMPONENT_RE.finditer(name)]
+    for letter, size in components:
+        if letter not in "RGBA":
+            return None
+    return components
+
+
+def get_swizzle(components):
+    component_letters = "".join((letter for letter, size in components))
+    for swizzle in SWIZZLES:
+        if swizzle.startswith(component_letters):
+            return swizzle
+    print("Unknown swizzle {}".format(component_letters),
+          file=sys.stderr)
+    sys.exit(1)
+
+
+def main():
+    template = Template(TEMPLATE)
+    print(template.render(formats = get_formats(sys.stdin)))
+
+
+if __name__ == '__main__':
+    main()
-- 
2.14.3

_______________________________________________
Piglit mailing list
Piglit@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/piglit

Reply via email to