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 32from __future__ import division 33 34 35VOID, UNSIGNED, SIGNED, FIXED, FLOAT = range(5) 36 37SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_0, SWIZZLE_1, SWIZZLE_NONE, = range(7) 38 39PLAIN = 'plain' 40 41RGB = 'rgb' 42SRGB = 'srgb' 43YUV = 'yuv' 44ZS = 'zs' 45 46 47def is_pot(x): 48 return (x & (x - 1)) == 0 49 50 51VERY_LARGE = 99999999999999999999999 52 53 54class Channel: 55 '''Describe the channel of a color channel.''' 56 57 def __init__(self, type, norm, pure, size, name = ''): 58 self.type = type 59 self.norm = norm 60 self.pure = pure 61 self.size = size 62 self.sign = type in (SIGNED, FIXED, FLOAT) 63 self.name = name 64 65 def __str__(self): 66 s = str(self.type) 67 if self.norm: 68 s += 'n' 69 if self.pure: 70 s += 'p' 71 s += str(self.size) 72 return s 73 74 def __eq__(self, other): 75 if other is None: 76 return False 77 78 return self.type == other.type and self.norm == other.norm and self.pure == other.pure and self.size == other.size 79 80 def __ne__(self, other): 81 return not self == other 82 83 def max(self): 84 '''Maximum representable number.''' 85 if self.type == FLOAT: 86 return VERY_LARGE 87 if self.type == FIXED: 88 return (1 << (self.size // 2)) - 1 89 if self.norm: 90 return 1 91 if self.type == UNSIGNED: 92 return (1 << self.size) - 1 93 if self.type == SIGNED: 94 return (1 << (self.size - 1)) - 1 95 assert False 96 97 def min(self): 98 '''Minimum representable number.''' 99 if self.type == FLOAT: 100 return -VERY_LARGE 101 if self.type == FIXED: 102 return -(1 << (self.size // 2)) 103 if self.type == UNSIGNED: 104 return 0 105 if self.norm: 106 return -1 107 if self.type == SIGNED: 108 return -(1 << (self.size - 1)) 109 assert False 110 111 112class Format: 113 '''Describe a pixel format.''' 114 115 def __init__(self, name, layout, block_width, block_height, le_channels, le_swizzles, be_channels, be_swizzles, colorspace): 116 self.name = name 117 self.layout = layout 118 self.block_width = block_width 119 self.block_height = block_height 120 self.le_channels = le_channels 121 self.le_swizzles = le_swizzles 122 self.be_channels = be_channels 123 self.be_swizzles = be_swizzles 124 self.name = name 125 self.colorspace = colorspace 126 127 def __str__(self): 128 return self.name 129 130 def short_name(self): 131 '''Make up a short norm for a format, suitable to be used as suffix in 132 function names.''' 133 134 name = self.name 135 if name.startswith('PIPE_FORMAT_'): 136 name = name[len('PIPE_FORMAT_'):] 137 name = name.lower() 138 return name 139 140 def block_size(self): 141 size = 0 142 for channel in self.le_channels: 143 size += channel.size 144 return size 145 146 def nr_channels(self): 147 nr_channels = 0 148 for channel in self.le_channels: 149 if channel.size: 150 nr_channels += 1 151 return nr_channels 152 153 def array_element(self): 154 if self.layout != PLAIN: 155 return None 156 ref_channel = self.le_channels[0] 157 if ref_channel.type == VOID: 158 ref_channel = self.le_channels[1] 159 for channel in self.le_channels: 160 if channel.size and (channel.size != ref_channel.size or channel.size % 8): 161 return None 162 if channel.type != VOID: 163 if channel.type != ref_channel.type: 164 return None 165 if channel.norm != ref_channel.norm: 166 return None 167 if channel.pure != ref_channel.pure: 168 return None 169 return ref_channel 170 171 def is_array(self): 172 return self.array_element() != None 173 174 def is_mixed(self): 175 if self.layout != PLAIN: 176 return False 177 ref_channel = self.le_channels[0] 178 if ref_channel.type == VOID: 179 ref_channel = self.le_channels[1] 180 for channel in self.le_channels[1:]: 181 if channel.type != VOID: 182 if channel.type != ref_channel.type: 183 return True 184 if channel.norm != ref_channel.norm: 185 return True 186 if channel.pure != ref_channel.pure: 187 return True 188 return False 189 190 def is_compressed(self): 191 for channel in self.le_channels: 192 if channel.type != VOID: 193 return False 194 return True 195 196 def is_unorm(self): 197 # Non-compressed formats all have unorm or srgb in their name. 198 for keyword in ['_UNORM', '_SRGB']: 199 if keyword in self.name: 200 return True 201 202 # All the compressed formats in GLES3.2 and GL4.6 ("Table 8.14: Generic 203 # and specific compressed internal formats.") that aren't snorm for 204 # border colors are unorm, other than BPTC_*_FLOAT. 205 return self.is_compressed() and not ('FLOAT' in self.name or self.is_snorm()) 206 207 def is_snorm(self): 208 return '_SNORM' in self.name 209 210 def is_pot(self): 211 return is_pot(self.block_size()) 212 213 def is_int(self): 214 if self.layout != PLAIN: 215 return False 216 for channel in self.le_channels: 217 if channel.type not in (VOID, UNSIGNED, SIGNED): 218 return False 219 return True 220 221 def is_float(self): 222 if self.layout != PLAIN: 223 return False 224 for channel in self.le_channels: 225 if channel.type not in (VOID, FLOAT): 226 return False 227 return True 228 229 def is_bitmask(self): 230 if self.layout != PLAIN: 231 return False 232 if self.block_size() not in (8, 16, 32): 233 return False 234 for channel in self.le_channels: 235 if channel.type not in (VOID, UNSIGNED, SIGNED): 236 return False 237 return True 238 239 def is_pure_color(self): 240 if self.layout != PLAIN or self.colorspace == ZS: 241 return False 242 pures = [channel.pure 243 for channel in self.le_channels 244 if channel.type != VOID] 245 for x in pures: 246 assert x == pures[0] 247 return pures[0] 248 249 def channel_type(self): 250 types = [channel.type 251 for channel in self.le_channels 252 if channel.type != VOID] 253 for x in types: 254 assert x == types[0] 255 return types[0] 256 257 def is_pure_signed(self): 258 return self.is_pure_color() and self.channel_type() == SIGNED 259 260 def is_pure_unsigned(self): 261 return self.is_pure_color() and self.channel_type() == UNSIGNED 262 263 def has_channel(self, id): 264 return self.le_swizzles[id] != SWIZZLE_NONE 265 266 def has_depth(self): 267 return self.colorspace == ZS and self.has_channel(0) 268 269 def has_stencil(self): 270 return self.colorspace == ZS and self.has_channel(1) 271 272 def stride(self): 273 return self.block_size()/8 274 275 276_type_parse_map = { 277 '': VOID, 278 'x': VOID, 279 'u': UNSIGNED, 280 's': SIGNED, 281 'h': FIXED, 282 'f': FLOAT, 283} 284 285_swizzle_parse_map = { 286 'x': SWIZZLE_X, 287 'y': SWIZZLE_Y, 288 'z': SWIZZLE_Z, 289 'w': SWIZZLE_W, 290 '0': SWIZZLE_0, 291 '1': SWIZZLE_1, 292 '_': SWIZZLE_NONE, 293} 294 295def _parse_channels(fields, layout, colorspace, swizzles): 296 if layout == PLAIN: 297 names = ['']*4 298 if colorspace in (RGB, SRGB): 299 for i in range(4): 300 swizzle = swizzles[i] 301 if swizzle < 4: 302 names[swizzle] += 'rgba'[i] 303 elif colorspace == ZS: 304 for i in range(4): 305 swizzle = swizzles[i] 306 if swizzle < 4: 307 names[swizzle] += 'zs'[i] 308 else: 309 assert False 310 for i in range(4): 311 if names[i] == '': 312 names[i] = 'x' 313 else: 314 names = ['x', 'y', 'z', 'w'] 315 316 channels = [] 317 for i in range(0, 4): 318 field = fields[i] 319 if field: 320 type = _type_parse_map[field[0]] 321 if field[1] == 'n': 322 norm = True 323 pure = False 324 size = int(field[2:]) 325 elif field[1] == 'p': 326 pure = True 327 norm = False 328 size = int(field[2:]) 329 else: 330 norm = False 331 pure = False 332 size = int(field[1:]) 333 else: 334 type = VOID 335 norm = False 336 pure = False 337 size = 0 338 channel = Channel(type, norm, pure, size, names[i]) 339 channels.append(channel) 340 341 return channels 342 343def parse(filename): 344 '''Parse the format description in CSV format in terms of the 345 Channel and Format classes above.''' 346 347 stream = open(filename) 348 formats = [] 349 for line in stream: 350 try: 351 comment = line.index('#') 352 except ValueError: 353 pass 354 else: 355 line = line[:comment] 356 line = line.strip() 357 if not line: 358 continue 359 360 fields = [field.strip() for field in line.split(',')] 361 if len (fields) == 10: 362 fields += fields[4:9] 363 assert len (fields) == 15 364 365 name = fields[0] 366 layout = fields[1] 367 block_width, block_height = map(int, fields[2:4]) 368 colorspace = fields[9] 369 370 le_swizzles = [_swizzle_parse_map[swizzle] for swizzle in fields[8]] 371 le_channels = _parse_channels(fields[4:8], layout, colorspace, le_swizzles) 372 373 be_swizzles = [_swizzle_parse_map[swizzle] for swizzle in fields[14]] 374 be_channels = _parse_channels(fields[10:14], layout, colorspace, be_swizzles) 375 376 le_shift = 0 377 for channel in le_channels: 378 channel.shift = le_shift 379 le_shift += channel.size 380 381 be_shift = 0 382 for channel in be_channels[3::-1]: 383 channel.shift = be_shift 384 be_shift += channel.size 385 386 assert le_shift == be_shift 387 for i in range(4): 388 assert (le_swizzles[i] != SWIZZLE_NONE) == (be_swizzles[i] != SWIZZLE_NONE) 389 390 format = Format(name, layout, block_width, block_height, le_channels, le_swizzles, be_channels, be_swizzles, colorspace) 391 formats.append(format) 392 return formats 393 394