util: Allow '#' comments in u_format.csv.
[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 import sys
34
35
36 VOID, UNSIGNED, SIGNED, FIXED, FLOAT = range(5)
37
38 SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_0, SWIZZLE_1, SWIZZLE_NONE, = range(7)
39
40 ARITH = 'arith'
41 ARRAY = 'array'
42
43
44 class Type:
45 '''Describe the type of a color channel.'''
46
47 def __init__(self, kind, norm, size):
48 self.kind = kind
49 self.norm = norm
50 self.size = size
51 self.sign = kind in (SIGNED, FIXED, FLOAT)
52
53 def __str__(self):
54 s = str(self.kind)
55 if self.norm:
56 s += 'n'
57 s += str(self.size)
58 return s
59
60 def __eq__(self, other):
61 return self.kind == other.kind and self.norm == other.norm and self.size == other.size
62
63
64 class Format:
65 '''Describe a pixel format.'''
66
67 def __init__(self, name, layout, block_width, block_height, in_types, out_swizzle, colorspace):
68 self.name = name
69 self.layout = layout
70 self.block_width = block_width
71 self.block_height = block_height
72 self.in_types = in_types
73 self.out_swizzle = out_swizzle
74 self.name = name
75 self.colorspace = colorspace
76
77 def __str__(self):
78 return self.name
79
80 def block_size(self):
81 size = 0
82 for type in self.in_types:
83 size += type.size
84 return size
85
86 def nr_channels(self):
87 nr_channels = 0
88 for type in self.in_types:
89 if type.size:
90 nr_channels += 1
91 return nr_channels
92
93 def is_array(self):
94 ref_type = self.in_types[0]
95 for type in self.in_types[1:]:
96 if type.size and (type.size != ref_type.size or type.size % 8):
97 return False
98 return True
99
100 def is_mixed(self):
101 ref_type = self.in_types[0]
102 for type in self.in_types[1:]:
103 if type.kind != VOID:
104 if type.kind != ref_type.kind:
105 return True
106 if type.norm != ref_type.norm:
107 return True
108 return False
109
110 def stride(self):
111 return self.block_size()/8
112
113
114 _kind_parse_map = {
115 '': VOID,
116 'x': VOID,
117 'u': UNSIGNED,
118 's': SIGNED,
119 'h': FIXED,
120 'f': FLOAT,
121 }
122
123 _swizzle_parse_map = {
124 'x': SWIZZLE_X,
125 'y': SWIZZLE_Y,
126 'z': SWIZZLE_Z,
127 'w': SWIZZLE_W,
128 '0': SWIZZLE_0,
129 '1': SWIZZLE_1,
130 '_': SWIZZLE_NONE,
131 }
132
133 def parse(filename):
134 '''Parse the format descrition in CSV format in terms of the
135 Type and Format classes above.'''
136
137 stream = open(filename)
138 formats = []
139 for line in stream:
140 try:
141 comment = line.index('#')
142 except ValueError:
143 pass
144 else:
145 line = line[:comment]
146 line = line.strip()
147 if not line:
148 continue
149 fields = [field.strip() for field in line.split(',')]
150 name = fields[0]
151 layout = fields[1]
152 block_width, block_height = map(int, fields[2:4])
153 in_types = []
154 for field in fields[4:8]:
155 if field:
156 kind = _kind_parse_map[field[0]]
157 if field[1] == 'n':
158 norm = True
159 size = int(field[2:])
160 else:
161 norm = False
162 size = int(field[1:])
163 else:
164 kind = VOID
165 norm = False
166 size = 0
167 in_type = Type(kind, norm, size)
168 in_types.append(in_type)
169 out_swizzle = [_swizzle_parse_map[swizzle] for swizzle in fields[8]]
170 colorspace = fields[9]
171 formats.append(Format(name, layout, block_width, block_height, in_types, out_swizzle, colorspace))
172 return formats
173