96e6451d0d259af76ee890e69755d3190609cb6f
[mesa.git] / src / gallium / auxiliary / util / u_format_parse.py
1 #!/usr/bin/env python
2
3 '''
4 /**************************************************************************
5 *
6 * Copyright 2009 VMware, Inc.
7 * All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sub license, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
16 *
17 * The above copyright notice and this permission notice (including the
18 * next paragraph) shall be included in all copies or substantial portions
19 * of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
24 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
25 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 *
29 **************************************************************************/
30 '''
31
32
33 VOID, UNSIGNED, SIGNED, FIXED, FLOAT = range(5)
34
35 SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_0, SWIZZLE_1, SWIZZLE_NONE, = range(7)
36
37 PLAIN = 'plain'
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, size, name = ''):
56 self.type = type
57 self.norm = norm
58 self.size = size
59 self.sign = type in (SIGNED, FIXED, FLOAT)
60 self.name = name
61
62 def __str__(self):
63 s = str(self.type)
64 if self.norm:
65 s += 'n'
66 s += str(self.size)
67 return s
68
69 def __eq__(self, other):
70 return self.type == other.type and self.norm == other.norm and self.size == other.size
71
72 def max(self):
73 '''Maximum representable number.'''
74 if self.type == FLOAT:
75 return VERY_LARGE
76 if self.type == FIXED:
77 return (1 << (self.size/2)) - 1
78 if self.norm:
79 return 1
80 if self.type == UNSIGNED:
81 return (1 << self.size) - 1
82 if self.type == SIGNED:
83 return (1 << (self.size - 1)) - 1
84 assert False
85
86 def min(self):
87 '''Minimum representable number.'''
88 if self.type == FLOAT:
89 return -VERY_LARGE
90 if self.type == FIXED:
91 return -(1 << (self.size/2))
92 if self.type == UNSIGNED:
93 return 0
94 if self.norm:
95 return -1
96 if self.type == SIGNED:
97 return -(1 << (self.size - 1))
98 assert False
99
100
101 class Format:
102 '''Describe a pixel format.'''
103
104 def __init__(self, name, layout, block_width, block_height, channels, swizzles, colorspace):
105 self.name = name
106 self.layout = layout
107 self.block_width = block_width
108 self.block_height = block_height
109 self.channels = channels
110 self.swizzles = swizzles
111 self.name = name
112 self.colorspace = colorspace
113
114 def __str__(self):
115 return self.name
116
117 def short_name(self):
118 '''Make up a short norm for a format, suitable to be used as suffix in
119 function names.'''
120
121 name = self.name
122 if name.startswith('PIPE_FORMAT_'):
123 name = name[len('PIPE_FORMAT_'):]
124 name = name.lower()
125 return name
126
127 def block_size(self):
128 size = 0
129 for channel in self.channels:
130 size += channel.size
131 return size
132
133 def nr_channels(self):
134 nr_channels = 0
135 for channel in self.channels:
136 if channel.size:
137 nr_channels += 1
138 return nr_channels
139
140 def is_array(self):
141 ref_channel = self.channels[0]
142 for channel in self.channels[1:]:
143 if channel.size and (channel.size != ref_channel.size or channel.size % 8):
144 return False
145 return True
146
147 def is_mixed(self):
148 ref_channel = self.channels[0]
149 if ref_channel.type == VOID:
150 ref_channel = self.channels[1]
151 for channel in self.channels[1:]:
152 if channel.type != VOID:
153 if channel.type != ref_channel.type:
154 return True
155 if channel.norm != ref_channel.norm:
156 return True
157 return False
158
159 def is_pot(self):
160 return is_pot(self.block_size())
161
162 def is_int(self):
163 for channel in self.channels:
164 if channel.type not in (VOID, UNSIGNED, SIGNED):
165 return False
166 return True
167
168 def is_float(self):
169 for channel in self.channels:
170 if channel.type not in (VOID, FLOAT):
171 return False
172 return True
173
174 def is_bitmask(self):
175 if self.block_size() not in (8, 16, 32):
176 return False
177 for channel in self.channels:
178 if channel.type not in (VOID, UNSIGNED, SIGNED):
179 return False
180 return True
181
182 def inv_swizzles(self):
183 '''Return an array[4] of inverse swizzle terms'''
184 inv_swizzle = [None]*4
185 for i in range(4):
186 swizzle = self.swizzles[i]
187 if swizzle < 4:
188 inv_swizzle[swizzle] = i
189 return inv_swizzle
190
191 def stride(self):
192 return self.block_size()/8
193
194
195 _type_parse_map = {
196 '': VOID,
197 'x': VOID,
198 'u': UNSIGNED,
199 's': SIGNED,
200 'h': FIXED,
201 'f': FLOAT,
202 }
203
204 _swizzle_parse_map = {
205 'x': SWIZZLE_X,
206 'y': SWIZZLE_Y,
207 'z': SWIZZLE_Z,
208 'w': SWIZZLE_W,
209 '0': SWIZZLE_0,
210 '1': SWIZZLE_1,
211 '_': SWIZZLE_NONE,
212 }
213
214 def parse(filename):
215 '''Parse the format descrition in CSV format in terms of the
216 Channel and Format classes above.'''
217
218 stream = open(filename)
219 formats = []
220 for line in stream:
221 try:
222 comment = line.index('#')
223 except ValueError:
224 pass
225 else:
226 line = line[:comment]
227 line = line.strip()
228 if not line:
229 continue
230
231 fields = [field.strip() for field in line.split(',')]
232
233 name = fields[0]
234 layout = fields[1]
235 block_width, block_height = map(int, fields[2:4])
236
237 swizzles = [_swizzle_parse_map[swizzle] for swizzle in fields[8]]
238 colorspace = fields[9]
239
240 if layout == PLAIN:
241 names = ['']*4
242 if colorspace in (RGB, SRGB):
243 for i in range(4):
244 swizzle = swizzles[i]
245 if swizzle < 4:
246 names[swizzle] += 'rgba'[i]
247 elif colorspace == ZS:
248 for i in range(4):
249 swizzle = swizzles[i]
250 if swizzle < 4:
251 names[swizzle] += 'zs'[i]
252 else:
253 assert False
254 for i in range(4):
255 if names[i] == '':
256 names[i] = 'x'
257 else:
258 names = ['x', 'y', 'z', 'w']
259
260 channels = []
261 for i in range(0, 4):
262 field = fields[4 + i]
263 if field:
264 type = _type_parse_map[field[0]]
265 if field[1] == 'n':
266 norm = True
267 size = int(field[2:])
268 else:
269 norm = False
270 size = int(field[1:])
271 else:
272 type = VOID
273 norm = False
274 size = 0
275 channel = Channel(type, norm, size, names[i])
276 channels.append(channel)
277
278 format = Format(name, layout, block_width, block_height, channels, swizzles, colorspace)
279 formats.append(format)
280 return formats
281