Merge branch '7.8'
[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 (1 << (self.size - 1)) - 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() not in (8, 16, 32):
170 return False
171 for channel in self.channels:
172 if channel.type not in (VOID, UNSIGNED, SIGNED):
173 return False
174 return True
175
176 def inv_swizzles(self):
177 '''Return an array[4] of inverse swizzle terms'''
178 inv_swizzle = [None]*4
179 for i in range(4):
180 swizzle = self.swizzles[i]
181 if swizzle < 4:
182 inv_swizzle[swizzle] = i
183 return inv_swizzle
184
185 def stride(self):
186 return self.block_size()/8
187
188
189 _type_parse_map = {
190 '': VOID,
191 'x': VOID,
192 'u': UNSIGNED,
193 's': SIGNED,
194 'h': FIXED,
195 'f': FLOAT,
196 }
197
198 _swizzle_parse_map = {
199 'x': SWIZZLE_X,
200 'y': SWIZZLE_Y,
201 'z': SWIZZLE_Z,
202 'w': SWIZZLE_W,
203 '0': SWIZZLE_0,
204 '1': SWIZZLE_1,
205 '_': SWIZZLE_NONE,
206 }
207
208 def parse(filename):
209 '''Parse the format descrition in CSV format in terms of the
210 Channel and Format classes above.'''
211
212 stream = open(filename)
213 formats = []
214 for line in stream:
215 try:
216 comment = line.index('#')
217 except ValueError:
218 pass
219 else:
220 line = line[:comment]
221 line = line.strip()
222 if not line:
223 continue
224
225 fields = [field.strip() for field in line.split(',')]
226
227 name = fields[0]
228 layout = fields[1]
229 block_width, block_height = map(int, fields[2:4])
230
231 swizzles = [_swizzle_parse_map[swizzle] for swizzle in fields[8]]
232 colorspace = fields[9]
233
234 if layout == PLAIN:
235 names = ['']*4
236 if colorspace in (RGB, SRGB):
237 for i in range(4):
238 swizzle = swizzles[i]
239 if swizzle < 4:
240 names[swizzle] += 'rgba'[i]
241 elif colorspace == ZS:
242 for i in range(4):
243 swizzle = swizzles[i]
244 if swizzle < 4:
245 names[swizzle] += 'zs'[i]
246 else:
247 assert False
248 for i in range(4):
249 if names[i] == '':
250 names[i] = 'x'
251 else:
252 names = ['x', 'y', 'z', 'w']
253
254 channels = []
255 for i in range(0, 4):
256 field = fields[4 + i]
257 if field:
258 type = _type_parse_map[field[0]]
259 if field[1] == 'n':
260 norm = True
261 size = int(field[2:])
262 else:
263 norm = False
264 size = int(field[1:])
265 else:
266 type = VOID
267 norm = False
268 size = 0
269 channel = Channel(type, norm, size, names[i])
270 channels.append(channel)
271
272 format = Format(name, layout, block_width, block_height, channels, swizzles, colorspace)
273 formats.append(format)
274 return formats
275