mesa: improve error messaging for format CSV parser
[mesa.git] / src / mesa / main / format_info.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2014 Intel Corporation
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 import format_parser as parser
26 import sys
27
28 def get_gl_base_format(fmat):
29 if fmat.name == 'MESA_FORMAT_NONE':
30 return 'GL_NONE'
31 elif fmat.name in ['MESA_FORMAT_YCBCR', 'MESA_FORMAT_YCBCR_REV']:
32 return 'GL_YCBCR_MESA'
33 elif fmat.has_channel('r'):
34 if fmat.has_channel('g'):
35 if fmat.has_channel('b'):
36 if fmat.has_channel('a'):
37 return 'GL_RGBA'
38 else:
39 return 'GL_RGB'
40 else:
41 return 'GL_RG'
42 else:
43 return 'GL_RED'
44 elif fmat.has_channel('l'):
45 if fmat.has_channel('a'):
46 return 'GL_LUMINANCE_ALPHA'
47 else:
48 return 'GL_LUMINANCE'
49 elif fmat.has_channel('a') and fmat.num_channels() == 1:
50 return 'GL_ALPHA'
51 elif fmat.has_channel('z'):
52 if fmat.has_channel('s'):
53 return 'GL_DEPTH_STENCIL'
54 else:
55 return 'GL_DEPTH_COMPONENT'
56 elif fmat.has_channel('s'):
57 return 'GL_STENCIL_INDEX'
58 elif fmat.has_channel('i') and fmat.num_channels() == 1:
59 return 'GL_INTENSITY'
60 else:
61 sys.exit("error, could not determine base format for {0}, check swizzle".format(fmat.name));
62
63 def get_gl_data_type(fmat):
64 if fmat.is_compressed():
65 if 'FLOAT' in fmat.name:
66 return 'GL_FLOAT'
67 elif 'SIGNED' in fmat.name or 'SNORM' in fmat.name:
68 return 'GL_SIGNED_NORMALIZED'
69 else:
70 return 'GL_UNSIGNED_NORMALIZED'
71 elif fmat.name in ['MESA_FORMAT_YCBCR', 'MESA_FORMAT_YCBCR_REV']:
72 return 'GL_UNSIGNED_NORMALIZED'
73
74 channel = None
75 for chan in fmat.channels:
76 if chan.type == 'x' and len(fmat.channels) > 1:
77 continue # We can do better
78 elif chan.name == 's' and fmat.has_channel('z'):
79 continue # We'll use the type from the depth instead
80
81 channel = chan
82 break;
83
84 if channel.type == parser.UNSIGNED:
85 if channel.norm:
86 return 'GL_UNSIGNED_NORMALIZED'
87 else:
88 return 'GL_UNSIGNED_INT'
89 elif channel.type == parser.SIGNED:
90 if channel.norm:
91 return 'GL_SIGNED_NORMALIZED'
92 else:
93 return 'GL_INT'
94 elif channel.type == parser.FLOAT:
95 return 'GL_FLOAT'
96 elif channel.type == parser.VOID:
97 return 'GL_NONE'
98 else:
99 assert False
100
101 def get_mesa_layout(fmat):
102 if fmat.layout == 'array':
103 return 'MESA_FORMAT_LAYOUT_ARRAY'
104 elif fmat.layout == 'packed':
105 return 'MESA_FORMAT_LAYOUT_PACKED'
106 else:
107 return 'MESA_FORMAT_LAYOUT_OTHER'
108
109 def get_channel_bits(fmat, chan_name):
110 if fmat.is_compressed():
111 # These values are pretty-much bogus, but OpenGL requires that we
112 # return an "approximate" number of bits.
113 if fmat.layout == 's3tc':
114 return 4 if fmat.has_channel(chan_name) else 0
115 elif fmat.layout == 'fxt1':
116 if chan_name in 'rgb':
117 return 4
118 elif chan_name == 'a':
119 return 1 if fmat.has_channel('a') else 0
120 else:
121 return 0
122 elif fmat.layout == 'rgtc':
123 return 8 if fmat.has_channel(chan_name) else 0
124 elif fmat.layout in ('etc1', 'etc2'):
125 if fmat.name.endswith('_ALPHA1') and chan_name == 'a':
126 return 1
127
128 bits = 11 if fmat.name.endswith('11_EAC') else 8
129 return bits if fmat.has_channel(chan_name) else 0
130 elif fmat.layout == 'bptc':
131 bits = 16 if fmat.name.endswith('_FLOAT') else 8
132 return bits if fmat.has_channel(chan_name) else 0
133 else:
134 assert False
135 else:
136 # Uncompressed textures
137 for chan in fmat.channels:
138 if chan.name == chan_name:
139 return chan.size
140 return 0
141
142 formats = parser.parse(sys.argv[1])
143
144 print '''
145 /*
146 * Mesa 3-D graphics library
147 *
148 * Copyright (c) 2014 Intel Corporation
149 *
150 * Permission is hereby granted, free of charge, to any person obtaining a
151 * copy of this software and associated documentation files (the "Software"),
152 * to deal in the Software without restriction, including without limitation
153 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
154 * and/or sell copies of the Software, and to permit persons to whom the
155 * Software is furnished to do so, subject to the following conditions:
156 *
157 * The above copyright notice and this permission notice shall be included
158 * in all copies or substantial portions of the Software.
159 *
160 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
161 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
162 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
163 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
164 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
165 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
166 * OTHER DEALINGS IN THE SOFTWARE.
167 */
168
169 /*
170 * This file is AUTOGENERATED by format_info.py. Do not edit it
171 * manually or commit it into version control.
172 */
173
174 static struct gl_format_info format_info[MESA_FORMAT_COUNT] =
175 {
176 '''
177
178 for fmat in formats:
179 print ' {'
180 print ' {0},'.format(fmat.name)
181 print ' "{0}",'.format(fmat.name)
182 print ' {0},'.format(get_mesa_layout(fmat))
183 print ' {0},'.format(get_gl_base_format(fmat))
184 print ' {0},'.format(get_gl_data_type(fmat))
185
186 bits = [ get_channel_bits(fmat, name) for name in ['r', 'g', 'b', 'a']]
187 print ' {0},'.format(', '.join(map(str, bits)))
188 bits = [ get_channel_bits(fmat, name) for name in ['l', 'i', 'z', 's']]
189 print ' {0},'.format(', '.join(map(str, bits)))
190
191 print ' {0}, {1}, {2},'.format(fmat.block_width, fmat.block_height,
192 int(fmat.block_size() / 8))
193
194 print ' {{ {0} }},'.format(', '.join(map(str, fmat.swizzle)))
195 if fmat.is_array():
196 chan = fmat.array_element()
197 norm = chan.norm or chan.type == parser.FLOAT
198 print ' MESA_ARRAY_FORMAT({0}),'.format(', '.join([
199 str(chan.size / 8),
200 str(int(chan.sign)),
201 str(int(chan.type == parser.FLOAT)),
202 str(int(norm)),
203 str(len(fmat.channels)),
204 str(fmat.swizzle[0]),
205 str(fmat.swizzle[1]),
206 str(fmat.swizzle[2]),
207 str(fmat.swizzle[3]),
208 ]))
209 else:
210 print ' 0,'
211 print ' },'
212
213 print '};'