Path: blob/21.2-virgl/src/util/format/u_format_parse.py
7075 views
1'''2/**************************************************************************3*4* Copyright 2009 VMware, Inc.5* All Rights Reserved.6*7* Permission is hereby granted, free of charge, to any person obtaining a8* copy of this software and associated documentation files (the9* "Software"), to deal in the Software without restriction, including10* without limitation the rights to use, copy, modify, merge, publish,11* distribute, sub license, and/or sell copies of the Software, and to12* permit persons to whom the Software is furnished to do so, subject to13* the following conditions:14*15* The above copyright notice and this permission notice (including the16* next paragraph) shall be included in all copies or substantial portions17* of the Software.18*19* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS20* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF21* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.22* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR23* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,24* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE25* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.26*27**************************************************************************/28'''293031from __future__ import division32import copy3334VOID, UNSIGNED, SIGNED, FIXED, FLOAT = range(5)3536SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_0, SWIZZLE_1, SWIZZLE_NONE, = range(7)3738PLAIN = 'plain'3940RGB = 'rgb'41SRGB = 'srgb'42YUV = 'yuv'43ZS = 'zs'444546def is_pot(x):47return (x & (x - 1)) == 0484950VERY_LARGE = 99999999999999999999999515253class Channel:54'''Describe the channel of a color channel.'''5556def __init__(self, type, norm, pure, size, name=''):57self.type = type58self.norm = norm59self.pure = pure60self.size = size61self.sign = type in (SIGNED, FIXED, FLOAT)62self.name = name6364def __str__(self):65s = str(self.type)66if self.norm:67s += 'n'68if self.pure:69s += 'p'70s += str(self.size)71return s7273def __repr__(self):74return "Channel({})".format(self.__str__())7576def __eq__(self, other):77if other is None:78return False7980return self.type == other.type and self.norm == other.norm and self.pure == other.pure and self.size == other.size8182def __ne__(self, other):83return not self == other8485def max(self):86'''Maximum representable number.'''87if self.type == FLOAT:88return VERY_LARGE89if self.type == FIXED:90return (1 << (self.size // 2)) - 191if self.norm:92return 193if self.type == UNSIGNED:94return (1 << self.size) - 195if self.type == SIGNED:96return (1 << (self.size - 1)) - 197assert False9899def min(self):100'''Minimum representable number.'''101if self.type == FLOAT:102return -VERY_LARGE103if self.type == FIXED:104return -(1 << (self.size // 2))105if self.type == UNSIGNED:106return 0107if self.norm:108return -1109if self.type == SIGNED:110return -(1 << (self.size - 1))111assert False112113114class Format:115'''Describe a pixel format.'''116117def __init__(self, name, layout, block_width, block_height, block_depth, le_channels, le_swizzles, be_channels, be_swizzles, colorspace):118self.name = name119self.layout = layout120self.block_width = block_width121self.block_height = block_height122self.block_depth = block_depth123self.colorspace = colorspace124125self.le_channels = le_channels126self.le_swizzles = le_swizzles127128le_shift = 0129for channel in self.le_channels:130channel.shift = le_shift131le_shift += channel.size132133if be_channels:134if self.is_array():135print(136"{} is an array format and should not include BE swizzles in the CSV".format(self.name))137exit(1)138if self.is_bitmask():139print(140"{} is a bitmask format and should not include BE swizzles in the CSV".format(self.name))141exit(1)142self.be_channels = be_channels143self.be_swizzles = be_swizzles144elif self.is_bitmask() and not self.is_array():145# Bitmask formats are "load a word the size of the block and146# bitshift channels out of it." However, the channel shifts147# defined in u_format_table.c are numbered right-to-left on BE148# for some historical reason (see below), which is hard to149# change due to llvmpipe, so we also have to flip the channel150# order and the channel-to-rgba swizzle values to read151# right-to-left from the defined (non-VOID) channels so that the152# correct shifts happen.153#154# This is nonsense, but it's the nonsense that makes155# u_format_test pass and you get the right colors in softpipe at156# least.157chans = self.nr_channels()158self.be_channels = self.le_channels[chans -1591::-1] + self.le_channels[chans:4]160161xyzw = [SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W]162chan_map = {SWIZZLE_X: xyzw[chans - 1] if chans >= 1 else SWIZZLE_X,163SWIZZLE_Y: xyzw[chans - 2] if chans >= 2 else SWIZZLE_X,164SWIZZLE_Z: xyzw[chans - 3] if chans >= 3 else SWIZZLE_X,165SWIZZLE_W: xyzw[chans - 4] if chans >= 4 else SWIZZLE_X,166SWIZZLE_1: SWIZZLE_1,167SWIZZLE_0: SWIZZLE_0,168SWIZZLE_NONE: SWIZZLE_NONE}169self.be_swizzles = [chan_map[s] for s in self.le_swizzles]170else:171self.be_channels = copy.deepcopy(le_channels)172self.be_swizzles = le_swizzles173174be_shift = 0175for channel in reversed(self.be_channels):176channel.shift = be_shift177be_shift += channel.size178179assert le_shift == be_shift180for i in range(4):181assert (self.le_swizzles[i] != SWIZZLE_NONE) == (182self.be_swizzles[i] != SWIZZLE_NONE)183184def __str__(self):185return self.name186187def short_name(self):188'''Make up a short norm for a format, suitable to be used as suffix in189function names.'''190191name = self.name192if name.startswith('PIPE_FORMAT_'):193name = name[len('PIPE_FORMAT_'):]194name = name.lower()195return name196197def block_size(self):198size = 0199for channel in self.le_channels:200size += channel.size201return size202203def nr_channels(self):204nr_channels = 0205for channel in self.le_channels:206if channel.size:207nr_channels += 1208return nr_channels209210def array_element(self):211if self.layout != PLAIN:212return None213ref_channel = self.le_channels[0]214if ref_channel.type == VOID:215ref_channel = self.le_channels[1]216for channel in self.le_channels:217if channel.size and (channel.size != ref_channel.size or channel.size % 8):218return None219if channel.type != VOID:220if channel.type != ref_channel.type:221return None222if channel.norm != ref_channel.norm:223return None224if channel.pure != ref_channel.pure:225return None226return ref_channel227228def is_array(self):229return self.array_element() != None230231def is_mixed(self):232if self.layout != PLAIN:233return False234ref_channel = self.le_channels[0]235if ref_channel.type == VOID:236ref_channel = self.le_channels[1]237for channel in self.le_channels[1:]:238if channel.type != VOID:239if channel.type != ref_channel.type:240return True241if channel.norm != ref_channel.norm:242return True243if channel.pure != ref_channel.pure:244return True245return False246247def is_compressed(self):248for channel in self.le_channels:249if channel.type != VOID:250return False251return True252253def is_unorm(self):254# Non-compressed formats all have unorm or srgb in their name.255for keyword in ['_UNORM', '_SRGB']:256if keyword in self.name:257return True258259# All the compressed formats in GLES3.2 and GL4.6 ("Table 8.14: Generic260# and specific compressed internal formats.") that aren't snorm for261# border colors are unorm, other than BPTC_*_FLOAT.262return self.is_compressed() and not ('FLOAT' in self.name or self.is_snorm())263264def is_snorm(self):265return '_SNORM' in self.name266267def is_pot(self):268return is_pot(self.block_size())269270def is_int(self):271if self.layout != PLAIN:272return False273for channel in self.le_channels:274if channel.type not in (VOID, UNSIGNED, SIGNED):275return False276return True277278def is_float(self):279if self.layout != PLAIN:280return False281for channel in self.le_channels:282if channel.type not in (VOID, FLOAT):283return False284return True285286def is_bitmask(self):287if self.layout != PLAIN:288return False289if self.block_size() not in (8, 16, 32):290return False291for channel in self.le_channels:292if channel.type not in (VOID, UNSIGNED, SIGNED):293return False294return True295296def is_pure_color(self):297if self.layout != PLAIN or self.colorspace == ZS:298return False299pures = [channel.pure300for channel in self.le_channels301if channel.type != VOID]302for x in pures:303assert x == pures[0]304return pures[0]305306def channel_type(self):307types = [channel.type308for channel in self.le_channels309if channel.type != VOID]310for x in types:311assert x == types[0]312return types[0]313314def is_pure_signed(self):315return self.is_pure_color() and self.channel_type() == SIGNED316317def is_pure_unsigned(self):318return self.is_pure_color() and self.channel_type() == UNSIGNED319320def has_channel(self, id):321return self.le_swizzles[id] != SWIZZLE_NONE322323def has_depth(self):324return self.colorspace == ZS and self.has_channel(0)325326def has_stencil(self):327return self.colorspace == ZS and self.has_channel(1)328329def stride(self):330return self.block_size()/8331332333_type_parse_map = {334'': VOID,335'x': VOID,336'u': UNSIGNED,337's': SIGNED,338'h': FIXED,339'f': FLOAT,340}341342_swizzle_parse_map = {343'x': SWIZZLE_X,344'y': SWIZZLE_Y,345'z': SWIZZLE_Z,346'w': SWIZZLE_W,347'0': SWIZZLE_0,348'1': SWIZZLE_1,349'_': SWIZZLE_NONE,350}351352353def _parse_channels(fields, layout, colorspace, swizzles):354if layout == PLAIN:355names = ['']*4356if colorspace in (RGB, SRGB):357for i in range(4):358swizzle = swizzles[i]359if swizzle < 4:360names[swizzle] += 'rgba'[i]361elif colorspace == ZS:362for i in range(4):363swizzle = swizzles[i]364if swizzle < 4:365names[swizzle] += 'zs'[i]366else:367assert False368for i in range(4):369if names[i] == '':370names[i] = 'x'371else:372names = ['x', 'y', 'z', 'w']373374channels = []375for i in range(0, 4):376field = fields[i]377if field:378type = _type_parse_map[field[0]]379if field[1] == 'n':380norm = True381pure = False382size = int(field[2:])383elif field[1] == 'p':384pure = True385norm = False386size = int(field[2:])387else:388norm = False389pure = False390size = int(field[1:])391else:392type = VOID393norm = False394pure = False395size = 0396channel = Channel(type, norm, pure, size, names[i])397channels.append(channel)398399return channels400401402def parse(filename):403'''Parse the format description in CSV format in terms of the404Channel and Format classes above.'''405406stream = open(filename)407formats = []408for line in stream:409try:410comment = line.index('#')411except ValueError:412pass413else:414line = line[:comment]415line = line.strip()416if not line:417continue418419fields = [field.strip() for field in line.split(',')]420assert(len(fields) == 11 or len(fields) == 16)421422name = fields[0]423layout = fields[1]424block_width, block_height, block_depth = map(int, fields[2:5])425colorspace = fields[10]426427le_swizzles = [_swizzle_parse_map[swizzle] for swizzle in fields[9]]428le_channels = _parse_channels(fields[5:9], layout, colorspace, le_swizzles)429430be_swizzles = None431be_channels = None432if len(fields) == 16:433be_swizzles = [_swizzle_parse_map[swizzle]434435for swizzle in fields[15]]436be_channels = _parse_channels(437438fields[11:15], layout, colorspace, be_swizzles)439440format = Format(name, layout, block_width, block_height, block_depth, le_channels, le_swizzles, be_channels, be_swizzles, colorspace)441formats.append(format)442return formats443444445