mesa: Add _mesa_format_fallback_rgbx_to_rgba() [v2]
[mesa.git] / src / mesa / main / format_fallback.py
1 COPYRIGHT = """\
2 /*
3 * Copyright 2017 Google
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 """
26
27 # stdlib
28 import argparse
29 from sys import stdout
30 from mako.template import Template
31
32 # local
33 import format_parser
34
35 def parse_args():
36 p = argparse.ArgumentParser()
37 p.add_argument("csv")
38 p.add_argument("out")
39 return p.parse_args()
40
41 def get_rgbx_to_rgba_map(formats):
42 names = {fmt.name for fmt in formats}
43
44 for fmt in formats:
45 if not fmt.has_channel('r') or not fmt.has_channel('x'):
46 continue
47
48 # The condition above will still let MESA_FORMAT_R9G9B9E5_FLOAT
49 # through. We need to ensure it actually has an X in the name.
50 if not 'X' in fmt.name:
51 continue
52
53 rgbx_name = fmt.name
54 rgba_name = rgbx_name.replace("X", "A")
55 if rgba_name not in names:
56 continue;
57
58 yield rgbx_name, rgba_name
59
60 TEMPLATE = Template(COPYRIGHT + """
61 #include "formats.h"
62
63 /**
64 * If the format has an alpha channel, and there exists a non-alpha
65 * variant of the format with an identical bit layout, then return
66 * the non-alpha format. Otherwise return the original format.
67 *
68 * Examples:
69 * Fallback exists:
70 * MESA_FORMAT_R8G8B8X8_UNORM -> MESA_FORMAT_R8G8B8A8_UNORM
71 * MESA_FORMAT_RGBX_UNORM16 -> MESA_FORMAT_RGBA_UNORM16
72 *
73 * No fallback:
74 * MESA_FORMAT_R8G8B8A8_UNORM -> MESA_FORMAT_R8G8B8A8_UNORM
75 * MESA_FORMAT_Z_FLOAT32 -> MESA_FORMAT_Z_FLOAT32
76 */
77 mesa_format
78 _mesa_format_fallback_rgbx_to_rgba(mesa_format format)
79 {
80 switch (format) {
81 %for rgbx, rgba in rgbx_to_rgba_map:
82 case ${rgbx}:
83 return ${rgba};
84 %endfor
85 default:
86 return format;
87 }
88 }
89 """);
90
91 def main():
92 pargs = parse_args()
93
94 formats = list(format_parser.parse(pargs.csv))
95
96 template_env = {
97 'rgbx_to_rgba_map': list(get_rgbx_to_rgba_map(formats)),
98 }
99
100 with open(pargs.out, 'w') as f:
101 f.write(TEMPLATE.render(**template_env))
102
103 if __name__ == "__main__":
104 main()