Merge branch 'gallium-no-rhw-position'
[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.norm:
77 return 1
78 if self.type == UNSIGNED:
79 return (1 << self.size) - 1
80 if self.type == SIGNED:
81 return self.size - 1
82 assert False
83
84 def min(self):
85 '''Minimum representable number.'''
86 if self.type == FLOAT:
87 return -VERY_LARGE
88 if self.type == UNSIGNED:
89 return 0
90 if self.norm:
91 return -1
92 if self.type == SIGNED:
93 return -(1 << (self.size - 1))
94 assert False
95
96
97 class Format:
98 '''Describe a pixel format.'''
99
100 def __init__(self, name, layout, block_width, block_height, channels, swizzles, colorspace):
101 self.name = name
102 self.layout = layout
103 self.block_width = block_width
104 self.block_height = block_height
105 self.channels = channels
106 self.swizzles = swizzles
107 self.name = name
108 self.colorspace = colorspace
109
110 def __str__(self):
111 return self.name
112
113 def short_name(self):
114 '''Make up a short norm for a format, suitable to be used as suffix in
115 function names.'''
116
117 name = self.name
118 if name.startswith('PIPE_FORMAT_'):
119 name = name[len('PIPE_FORMAT_'):]
120 name = name.lower()
121 return name
122
123 def block_size(self):
124 size = 0
125 for channel in self.channels:
126 size += channel.size
127 return size
128
129 def nr_channels(self):
130 nr_channels = 0
131 for channel in self.channels:
132 if channel.size:
133 nr_channels += 1
134 return nr_channels
135
136 def is_array(self):
137 ref_channel = self.channels[0]
138 for channel in self.channels[1:]:
139 if channel.size and (channel.size != ref_channel.size or channel.size % 8):
140 return False
141 return True
142
143 def is_mixed(self):
144 ref_channel = self.channels[0]
145 for channel in self.channels[1:]:
146 if channel.type != VOID:
147 if channel.type != ref_channel.type:
148 return True
149 if channel.norm != ref_channel.norm:
150 return True
151 return False
152
153 def is_pot(self):
154 return is_pot(self.block_size())
155
156 def is_int(self):
157 for channel in self.channels:
158 if channel.type not in (VOID, UNSIGNED, SIGNED):
159 return False
160 return True
161
162 def is_float(self):
163 for channel in self.channels:
164 if channel.type not in (VOID, FLOAT):
165 return False
166 return True
167
168 def is_bitmask(self):
169 if self.block_size() > 32:
170 return False
171 if not self.is_pot():
172 return False
173 for channel in self.channels:
174 if not is_pot(channel.size):
175 return True
176 if channel.type not in (VOID, UNSIGNED, SIGNED):
177 return False
178 if channel.size >= 32:
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