util: Make pixel format layout more meaningful.
[mesa.git] / src / gallium / auxiliary / util / u_format_table.py
1 #!/usr/bin/env python
2
3 '''
4 /**************************************************************************
5 *
6 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 UNSIGNED, SIGNED, FIXED, FLOAT = 'u', 's', 'h', 'f'
37
38
39 class Type:
40
41 def __init__(self, kind, norm, size):
42 self.kind = kind
43 self.norm = norm
44 self.size = size
45
46 def __str__(self):
47 s = str(self.kind)
48 if norm:
49 s += 'n'
50 s += str(self.size)
51 return s
52
53
54 class Format:
55
56 def __init__(self, name, layout, block_width, block_height, in_types, out_swizzle, colorspace):
57 self.name = name
58 self.layout = layout
59 self.block_width = block_width
60 self.block_height = block_height
61 self.in_types = in_types
62 self.out_swizzle = out_swizzle
63 self.name = name
64 self.colorspace = colorspace
65
66 def __str__(self):
67 return self.name
68
69 def block_size(self):
70 size = 0
71 for type in self.in_types:
72 size += type.size
73 return size
74
75
76 def parse(filename):
77 stream = open(filename)
78 formats = []
79 for line in stream:
80 line = line.rstrip()
81 fields = [field.strip() for field in line.split(',')]
82 name = fields[0]
83 layout = fields[1]
84 block_width, block_height = map(int, fields[2:4])
85 in_types = []
86 for field in fields[4:8]:
87 if field:
88 kind = field[0]
89 if field[1] == 'n':
90 norm = True
91 size = int(field[2:])
92 else:
93 norm = False
94 size = int(field[1:])
95 else:
96 kind = ''
97 norm = False
98 size = 0
99 in_type = Type(kind, norm, size)
100 in_types.append(in_type)
101 out_swizzle = fields[8]
102 colorspace = fields[9]
103 formats.append(Format(name, layout, block_width, block_height, in_types, out_swizzle, colorspace))
104 return formats
105
106
107 def layout_map(layout):
108 return 'UTIL_FORMAT_LAYOUT_' + str(layout).upper()
109
110
111 def colorspace_map(colorspace):
112 return 'UTIL_FORMAT_COLORSPACE_' + str(colorspace).upper()
113
114
115 colorspace_channels_map = {
116 'rgb': 'rgba',
117 'rgba': 'rgba',
118 'zs': 'zs',
119 'yuv': ['y1', 'y2', 'u', 'v'],
120 'dxt': []
121 }
122
123
124 kind_map = {
125 '': "UTIL_FORMAT_TYPE_VOID",
126 'x': "UTIL_FORMAT_TYPE_VOID",
127 UNSIGNED: "UTIL_FORMAT_TYPE_UNSIGNED",
128 SIGNED: "UTIL_FORMAT_TYPE_SIGNED",
129 FIXED: "UTIL_FORMAT_TYPE_FIXED",
130 FLOAT: "UTIL_FORMAT_TYPE_FLOAT",
131 }
132
133
134 def bool_map(value):
135 if value:
136 return "TRUE"
137 else:
138 return "FALSE"
139
140
141 swizzle_map = {
142 'x': "UTIL_FORMAT_SWIZZLE_X",
143 'y': "UTIL_FORMAT_SWIZZLE_Y",
144 'z': "UTIL_FORMAT_SWIZZLE_Z",
145 'w': "UTIL_FORMAT_SWIZZLE_W",
146 '0': "UTIL_FORMAT_SWIZZLE_0",
147 '1': "UTIL_FORMAT_SWIZZLE_1",
148 '_': "UTIL_FORMAT_SWIZZLE_NONE",
149 }
150
151
152 def write_format_table(formats):
153 print '/* This file is autogenerated by u_format_table.py from u_format.csv. Do not edit directly. */'
154 print
155 # This will print the copyright message on the top of this file
156 print __doc__.strip()
157 print
158 print '#include "u_format.h"'
159 print
160 print 'const struct util_format_description'
161 print 'util_format_description_table[] = '
162 print "{"
163 for format in formats:
164 print " {"
165 print " %s," % (format.name,)
166 print " \"%s\"," % (format.name,)
167 print " {%u, %u, %u}, /* block */" % (format.block_width, format.block_height, format.block_size())
168 print " %s," % (layout_map(format.layout),)
169 print " {"
170 for i in range(4):
171 type = format.in_types[i]
172 if i < 3:
173 sep = ","
174 else:
175 sep = ""
176 print " {%s, %s, %u}%s /* %s */" % (kind_map[type.kind], bool_map(type.norm), type.size, sep, "xyzw"[i])
177 print " },"
178 print " {"
179 for i in range(4):
180 swizzle = format.out_swizzle[i]
181 if i < 3:
182 sep = ","
183 else:
184 sep = ""
185 try:
186 comment = layout_channels_map[format.layout][i]
187 except:
188 comment = 'ignored'
189 print " %s%s /* %s */" % (swizzle_map[swizzle], sep, comment)
190 print " },"
191 print " %s," % (colorspace_map(format.colorspace),)
192 print " },"
193 print " {"
194 print " PIPE_FORMAT_NONE,"
195 print " \"PIPE_FORMAT_NONE\","
196 print " {0, 0, 0},"
197 print " 0,"
198 print " {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}},"
199 print " {0, 0, 0, 0},"
200 print " 0"
201 print " },"
202 print "};"
203
204
205 def main():
206
207 formats = []
208 for arg in sys.argv[1:]:
209 formats.extend(parse(arg))
210 write_format_table(formats)
211
212
213 if __name__ == '__main__':
214 main()