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