python: Fix inequality comparisons
[mesa.git] / src / amd / vulkan / vk_format_parse.py
1
2 '''
3 /**************************************************************************
4 *
5 * Copyright 2009 VMware, Inc.
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
18 * of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
24 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 *
28 **************************************************************************/
29 '''
30
31
32 VOID, UNSIGNED, SIGNED, FIXED, FLOAT = range(5)
33
34 SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_0, SWIZZLE_1, SWIZZLE_NONE, = range(7)
35
36 PLAIN = 'plain'
37 SCALED = 'scaled'
38
39 RGB = 'rgb'
40 SRGB = 'srgb'
41 YUV = 'yuv'
42 ZS = 'zs'
43
44
45 def is_pot(x):
46 return (x & (x - 1)) == 0
47
48
49 VERY_LARGE = 99999999999999999999999
50
51
52 class Channel:
53 '''Describe the channel of a color channel.'''
54
55 def __init__(self, type, norm, pure, scaled, size, name = ''):
56 self.type = type
57 self.norm = norm
58 self.pure = pure
59 self.size = size
60 self.scaled = scaled
61 self.sign = type in (SIGNED, FIXED, FLOAT)
62 self.name = name
63
64 def __str__(self):
65 s = str(self.type)
66 if self.norm:
67 s += 'n'
68 if self.pure:
69 s += 'p'
70 if self.scaled:
71 s += 's'
72 s += str(self.size)
73 return s
74
75 def __eq__(self, other):
76 if other is None:
77 return False
78
79 return self.type == other.type and self.norm == other.norm and self.pure == other.pure and self.size == other.size and self.scaled == other.scaled
80
81 def __ne__(self, other):
82 return not self == other
83
84 def max(self):
85 '''Maximum representable number.'''
86 if self.type == FLOAT:
87 return VERY_LARGE
88 if self.type == FIXED:
89 return (1 << (self.size/2)) - 1
90 if self.norm:
91 return 1
92 if self.type == UNSIGNED:
93 return (1 << self.size) - 1
94 if self.type == SIGNED:
95 return (1 << (self.size - 1)) - 1
96 assert False
97
98 def min(self):
99 '''Minimum representable number.'''
100 if self.type == FLOAT:
101 return -VERY_LARGE
102 if self.type == FIXED:
103 return -(1 << (self.size/2))
104 if self.type == UNSIGNED:
105 return 0
106 if self.norm:
107 return -1
108 if self.type == SIGNED:
109 return -(1 << (self.size - 1))
110 assert False
111
112
113 class Format:
114 '''Describe a pixel format.'''
115
116 def __init__(self, name, layout, block_width, block_height, le_channels, le_swizzles, be_channels, be_swizzles, colorspace):
117 self.name = name
118 self.layout = layout
119 self.block_width = block_width
120 self.block_height = block_height
121 self.le_channels = le_channels
122 self.le_swizzles = le_swizzles
123 self.be_channels = be_channels
124 self.be_swizzles = be_swizzles
125 self.name = name
126 self.colorspace = colorspace
127
128 def __str__(self):
129 return self.name
130
131 def short_name(self):
132 '''Make up a short norm for a format, suitable to be used as suffix in
133 function names.'''
134
135 name = self.name
136 if name.startswith('VK_FORMAT_'):
137 name = name[len('VK_FORMAT_'):]
138 name = name.lower()
139 return name
140
141 def block_size(self):
142 size = 0
143 for channel in self.le_channels:
144 size += channel.size
145 return size
146
147 def nr_channels(self):
148 nr_channels = 0
149 for channel in self.le_channels:
150 if channel.size:
151 nr_channels += 1
152 return nr_channels
153
154 def array_element(self):
155 if self.layout != PLAIN:
156 return None
157 ref_channel = self.le_channels[0]
158 if ref_channel.type == VOID:
159 ref_channel = self.le_channels[1]
160 for channel in self.le_channels:
161 if channel.size and (channel.size != ref_channel.size or channel.size % 8):
162 return None
163 if channel.type != VOID:
164 if channel.type != ref_channel.type:
165 return None
166 if channel.norm != ref_channel.norm:
167 return None
168 if channel.pure != ref_channel.pure:
169 return None
170 if channel.scaled != ref_channel.scaled:
171 return None
172 return ref_channel
173
174 def is_array(self):
175 return self.array_element() != None
176
177 def is_mixed(self):
178 if self.layout != PLAIN:
179 return False
180 ref_channel = self.le_channels[0]
181 if ref_channel.type == VOID:
182 ref_channel = self.le_channels[1]
183 for channel in self.le_channels[1:]:
184 if channel.type != VOID:
185 if channel.type != ref_channel.type:
186 return True
187 if channel.norm != ref_channel.norm:
188 return True
189 if channel.pure != ref_channel.pure:
190 return True
191 if channel.scaled != ref_channel.scaled:
192 return True
193 return False
194
195 def is_pot(self):
196 return is_pot(self.block_size())
197
198 def is_int(self):
199 if self.layout != PLAIN:
200 return False
201 for channel in self.le_channels:
202 if channel.type not in (VOID, UNSIGNED, SIGNED):
203 return False
204 return True
205
206 def is_float(self):
207 if self.layout != PLAIN:
208 return False
209 for channel in self.le_channels:
210 if channel.type not in (VOID, FLOAT):
211 return False
212 return True
213
214 def is_bitmask(self):
215 if self.layout != PLAIN:
216 return False
217 if self.block_size() not in (8, 16, 32):
218 return False
219 for channel in self.le_channels:
220 if channel.type not in (VOID, UNSIGNED, SIGNED):
221 return False
222 return True
223
224 def is_pure_color(self):
225 if self.layout != PLAIN or self.colorspace == ZS:
226 return False
227 pures = [channel.pure
228 for channel in self.le_channels
229 if channel.type != VOID]
230 for x in pures:
231 assert x == pures[0]
232 return pures[0]
233
234 def channel_type(self):
235 types = [channel.type
236 for channel in self.le_channels
237 if channel.type != VOID]
238 for x in types:
239 assert x == types[0]
240 return types[0]
241
242 def is_pure_signed(self):
243 return self.is_pure_color() and self.channel_type() == SIGNED
244
245 def is_pure_unsigned(self):
246 return self.is_pure_color() and self.channel_type() == UNSIGNED
247
248 def has_channel(self, id):
249 return self.le_swizzles[id] != SWIZZLE_NONE
250
251 def has_depth(self):
252 return self.colorspace == ZS and self.has_channel(0)
253
254 def has_stencil(self):
255 return self.colorspace == ZS and self.has_channel(1)
256
257 def stride(self):
258 return self.block_size()/8
259
260
261 _type_parse_map = {
262 '': VOID,
263 'x': VOID,
264 'u': UNSIGNED,
265 's': SIGNED,
266 'h': FIXED,
267 'f': FLOAT,
268 }
269
270 _swizzle_parse_map = {
271 'x': SWIZZLE_X,
272 'y': SWIZZLE_Y,
273 'z': SWIZZLE_Z,
274 'w': SWIZZLE_W,
275 '0': SWIZZLE_0,
276 '1': SWIZZLE_1,
277 '_': SWIZZLE_NONE,
278 }
279
280 def _parse_channels(fields, layout, colorspace, swizzles):
281 if layout == PLAIN:
282 names = ['']*4
283 if colorspace in (RGB, SRGB):
284 for i in range(4):
285 swizzle = swizzles[i]
286 if swizzle < 4:
287 names[swizzle] += 'rgba'[i]
288 elif colorspace == ZS:
289 for i in range(4):
290 swizzle = swizzles[i]
291 if swizzle < 4:
292 names[swizzle] += 'zs'[i]
293 else:
294 assert False
295 for i in range(4):
296 if names[i] == '':
297 names[i] = 'x'
298 else:
299 names = ['x', 'y', 'z', 'w']
300
301 channels = []
302 for i in range(0, 4):
303 field = fields[i]
304 if field:
305 type = _type_parse_map[field[0]]
306 if field[1] == 'n':
307 norm = True
308 pure = False
309 scaled = False
310 size = int(field[2:])
311 elif field[1] == 'p':
312 pure = True
313 norm = False
314 scaled = False
315 size = int(field[2:])
316 elif field[1] == 's':
317 pure = False
318 norm = False
319 scaled = True
320 size = int(field[2:])
321 else:
322 norm = False
323 pure = False
324 scaled = False
325 size = int(field[1:])
326 else:
327 type = VOID
328 norm = False
329 pure = False
330 scaled = False
331 size = 0
332 channel = Channel(type, norm, pure, scaled, size, names[i])
333 channels.append(channel)
334
335 return channels
336
337 def parse(filename):
338 '''Parse the format description in CSV format in terms of the
339 Channel and Format classes above.'''
340
341 stream = open(filename)
342 formats = []
343 for line in stream:
344 try:
345 comment = line.index('#')
346 except ValueError:
347 pass
348 else:
349 line = line[:comment]
350 line = line.strip()
351 if not line:
352 continue
353
354 fields = [field.strip() for field in line.split(',')]
355 if len (fields) < 10:
356 continue
357 if len (fields) == 10:
358 fields += fields[4:9]
359 assert len (fields) == 15
360
361 name = fields[0]
362 layout = fields[1]
363 block_width, block_height = map(int, fields[2:4])
364 colorspace = fields[9]
365
366 le_swizzles = [_swizzle_parse_map[swizzle] for swizzle in fields[8]]
367 le_channels = _parse_channels(fields[4:8], layout, colorspace, le_swizzles)
368
369 be_swizzles = [_swizzle_parse_map[swizzle] for swizzle in fields[14]]
370 be_channels = _parse_channels(fields[10:14], layout, colorspace, be_swizzles)
371
372 le_shift = 0
373 for channel in le_channels:
374 channel.shift = le_shift
375 le_shift += channel.size
376
377 be_shift = 0
378 for channel in be_channels[3::-1]:
379 channel.shift = be_shift
380 be_shift += channel.size
381
382 assert le_shift == be_shift
383 for i in range(4):
384 assert (le_swizzles[i] != SWIZZLE_NONE) == (be_swizzles[i] != SWIZZLE_NONE)
385
386 format = Format(name, layout, block_width, block_height, le_channels, le_swizzles, be_channels, be_swizzles, colorspace)
387 formats.append(format)
388 return formats
389