X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Fmesa%2Fmain%2Fformat_parser.py;h=6cd2fbca0e80e365b25fb45fa78e8e755dac17df;hb=cfff74629765d1d06313705f5ca80138c2182ef0;hp=5e45c74de3e09aea1ef6df7073ac960edfd81d2f;hpb=d4c780e052c9cc361bed5958b72b42d8151800c2;p=mesa.git diff --git a/src/mesa/main/format_parser.py b/src/mesa/main/format_parser.py index 5e45c74de3e..6cd2fbca0e8 100755 --- a/src/mesa/main/format_parser.py +++ b/src/mesa/main/format_parser.py @@ -24,6 +24,8 @@ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +import sys + VOID = 'x' UNSIGNED = 'u' SIGNED = 's' @@ -38,9 +40,6 @@ SRGB = 'srgb' YUV = 'yuv' ZS = 'zs' -def is_power_of_two(x): - return not bool(x & (x - 1)) - VERY_LARGE = 99999999999999999999999 class Channel: @@ -98,9 +97,9 @@ class Channel: else: return 1 - def is_power_of_two(self): - """Returns true if the size of this channel is a power of two.""" - return is_power_of_two(self.size) + def datatype(self): + """Returns the datatype corresponding to a channel type and size""" + return _get_datatype(self.type, self.size) class Swizzle: """Describes a swizzle operation. @@ -228,7 +227,7 @@ class Swizzle: class Format: """Describes a pixel format.""" - def __init__(self, name, layout, block_width, block_height, channels, swizzle, colorspace): + def __init__(self, name, layout, block_width, block_height, block_depth, channels, swizzle, colorspace): """Constructs a Format from some metadata and a list of channels. The channel objects must be unique to this Format and should not be @@ -242,6 +241,7 @@ class Format: layout -- One of 'array', 'packed' 'other', or a compressed layout block_width -- The block width if the format is compressed, 1 otherwise block_height -- The block height if the format is compressed, 1 otherwise + block_depth -- The block depth if the format is compressed, 1 otherwise channels -- A list of Channel objects swizzle -- A Swizzle from this format to rgba colorspace -- one of 'rgb', 'srgb', 'yuv', or 'zs' @@ -250,6 +250,7 @@ class Format: self.layout = layout self.block_width = block_width self.block_height = block_height + self.block_depth = block_depth self.channels = channels assert isinstance(swizzle, Swizzle) self.swizzle = swizzle @@ -362,7 +363,7 @@ class Format: def is_compressed(self): """Returns true if this is a compressed format.""" - return self.block_width != 1 or self.block_height != 1 + return self.block_width != 1 or self.block_height != 1 or self.block_depth != 1 def is_int(self): """Returns true if this format is an integer format. @@ -469,6 +470,49 @@ class Format: return channel return None + def datatype(self): + """Returns the datatype corresponding to a format's channel type and size""" + if self.layout == PACKED: + if self.block_size() == 8: + return 'uint8_t' + if self.block_size() == 16: + return 'uint16_t' + if self.block_size() == 32: + return 'uint32_t' + else: + assert False + else: + return _get_datatype(self.channel_type(), self.channel_size()) + +def _get_datatype(type, size): + if type == FLOAT: + if size == 32: + return 'float' + elif size == 16: + return 'uint16_t' + else: + assert False + elif type == UNSIGNED: + if size <= 8: + return 'uint8_t' + elif size <= 16: + return 'uint16_t' + elif size <= 32: + return 'uint32_t' + else: + assert False + elif type == SIGNED: + if size <= 8: + return 'int8_t' + elif size <= 16: + return 'int16_t' + elif size <= 32: + return 'int32_t' + else: + assert False + else: + assert False + def _parse_channels(fields, layout, colorspace, swizzle): channels = [] for field in fields: @@ -490,7 +534,7 @@ def _parse_channels(fields, layout, colorspace, swizzle): return channels def parse(filename): - """Parse a format descrition in CSV format. + """Parse a format description in CSV format. This function parses the given CSV file and returns an iterable of channels.""" @@ -513,9 +557,13 @@ def parse(filename): layout = fields[1] block_width = int(fields[2]) block_height = int(fields[3]) - colorspace = fields[9] + block_depth = int(fields[4]) + colorspace = fields[10] - swizzle = Swizzle(fields[8]) - channels = _parse_channels(fields[4:8], layout, colorspace, swizzle) + try: + swizzle = Swizzle(fields[9]) + except: + sys.exit("error parsing swizzle for format " + name) + channels = _parse_channels(fields[5:9], layout, colorspace, swizzle) - yield Format(name, layout, block_width, block_height, channels, swizzle, colorspace) + yield Format(name, layout, block_width, block_height, block_depth, channels, swizzle, colorspace)