mesa: add ARB_texture_buffer_range glTextureBufferRangeEXT function
[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_unorm_to_srgb_map(formats):
42 names = set(fmt.name for fmt in formats)
43
44 for fmt in formats:
45 if fmt.colorspace != 'srgb':
46 continue;
47
48 replacements = [
49 ('SRGB', 'RGB'),
50 ('SRGB', 'UNORM'),
51 ('SRGB8_ALPHA8', 'RGBA'),
52 ('SRGB8_ALPHA8', 'RGBA8'),
53 ('SRGB_ALPHA_UNORM', 'RGBA_UNORM'),
54 ]
55 found_unorm_name = False
56 for rep in replacements:
57 if fmt.name.find(rep[0]) == -1:
58 continue
59
60 unorm_name = fmt.name.replace(rep[0], rep[1])
61 if unorm_name in names:
62 yield unorm_name, fmt.name
63 found_unorm_name = True
64 break
65
66 # Every sRGB format MUST have a UNORM equivalent
67 assert found_unorm_name
68
69 def get_rgbx_to_rgba_map(formats):
70 names = set(fmt.name for fmt in formats)
71
72 for fmt in formats:
73 if not fmt.has_channel('r') or not fmt.has_channel('x'):
74 continue
75
76 # The condition above will still let MESA_FORMAT_R9G9B9E5_FLOAT
77 # through. We need to ensure it actually has an X in the name.
78 if not 'X' in fmt.name:
79 continue
80
81 rgbx_name = fmt.name
82 rgba_name = rgbx_name.replace("X", "A")
83 if rgba_name not in names:
84 continue;
85
86 yield rgbx_name, rgba_name
87
88 def get_intensity_to_red_map(formats):
89 names = set(fmt.name for fmt in formats)
90
91 for fmt in formats:
92 if str(fmt.swizzle) != 'xxxx':
93 continue
94
95 i_name = fmt.name
96 r_name = i_name.replace("_I_", "_R_")
97
98 assert r_name in names
99
100 yield i_name, r_name
101
102 TEMPLATE = Template(COPYRIGHT + """
103 #include "formats.h"
104 #include "util/macros.h"
105
106 /**
107 * For an sRGB format, return the corresponding linear color space format.
108 * For non-sRGB formats, return the format as-is.
109 */
110 mesa_format
111 _mesa_get_srgb_format_linear(mesa_format format)
112 {
113 switch (format) {
114 %for unorm, srgb in unorm_to_srgb_map:
115 case ${srgb}:
116 return ${unorm};
117 %endfor
118 default:
119 return format;
120 }
121 }
122
123 /**
124 * For a linear format, return the corresponding sRGB color space format.
125 * For an sRGB format, return the format as-is.
126 * Assert-fails if the format is not sRGB and does not have an sRGB equivalent.
127 */
128 mesa_format
129 _mesa_get_linear_format_srgb(mesa_format format)
130 {
131 switch (format) {
132 %for unorm, srgb in unorm_to_srgb_map:
133 case ${unorm}:
134 return ${srgb};
135 %endfor
136 %for unorm, srgb in unorm_to_srgb_map:
137 case ${srgb}:
138 %endfor
139 return format;
140 default:
141 unreachable("Given format does not have an sRGB equivalent");
142 }
143 }
144
145 /**
146 * For an intensity format, return the corresponding red format. For other
147 * formats, return the format as-is.
148 */
149 mesa_format
150 _mesa_get_intensity_format_red(mesa_format format)
151 {
152 switch (format) {
153 %for i, r in intensity_to_red_map:
154 case ${i}:
155 return ${r};
156 %endfor
157 default:
158 return format;
159 }
160 }
161
162 /**
163 * If the format has an alpha channel, and there exists a non-alpha
164 * variant of the format with an identical bit layout, then return
165 * the non-alpha format. Otherwise return the original format.
166 *
167 * Examples:
168 * Fallback exists:
169 * MESA_FORMAT_R8G8B8X8_UNORM -> MESA_FORMAT_R8G8B8A8_UNORM
170 * MESA_FORMAT_RGBX_UNORM16 -> MESA_FORMAT_RGBA_UNORM16
171 *
172 * No fallback:
173 * MESA_FORMAT_R8G8B8A8_UNORM -> MESA_FORMAT_R8G8B8A8_UNORM
174 * MESA_FORMAT_Z_FLOAT32 -> MESA_FORMAT_Z_FLOAT32
175 */
176 mesa_format
177 _mesa_format_fallback_rgbx_to_rgba(mesa_format format)
178 {
179 switch (format) {
180 %for rgbx, rgba in rgbx_to_rgba_map:
181 case ${rgbx}:
182 return ${rgba};
183 %endfor
184 default:
185 return format;
186 }
187 }
188 """);
189
190 def main():
191 pargs = parse_args()
192
193 formats = list(format_parser.parse(pargs.csv))
194
195 template_env = {
196 'unorm_to_srgb_map': list(get_unorm_to_srgb_map(formats)),
197 'rgbx_to_rgba_map': list(get_rgbx_to_rgba_map(formats)),
198 'intensity_to_red_map': list(get_intensity_to_red_map(formats)),
199 }
200
201 with open(pargs.out, 'w') as f:
202 f.write(TEMPLATE.render(**template_env))
203
204 if __name__ == "__main__":
205 main()