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