util/format: expose generated format packing functions through a header
[mesa.git] / src / util / format / u_format_table.py
1 from __future__ import print_function
2
3 CopyRight = '''
4 /**************************************************************************
5 *
6 * Copyright 2010 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, os
34
35 from u_format_parse import *
36 import u_format_pack
37
38
39 def layout_map(layout):
40 return 'UTIL_FORMAT_LAYOUT_' + str(layout).upper()
41
42
43 def colorspace_map(colorspace):
44 return 'UTIL_FORMAT_COLORSPACE_' + str(colorspace).upper()
45
46
47 colorspace_channels_map = {
48 'rgb': ['r', 'g', 'b', 'a'],
49 'srgb': ['sr', 'sg', 'sb', 'a'],
50 'zs': ['z', 's'],
51 'yuv': ['y', 'u', 'v'],
52 }
53
54
55 type_map = {
56 VOID: "UTIL_FORMAT_TYPE_VOID",
57 UNSIGNED: "UTIL_FORMAT_TYPE_UNSIGNED",
58 SIGNED: "UTIL_FORMAT_TYPE_SIGNED",
59 FIXED: "UTIL_FORMAT_TYPE_FIXED",
60 FLOAT: "UTIL_FORMAT_TYPE_FLOAT",
61 }
62
63
64 def bool_map(value):
65 if value:
66 return "TRUE"
67 else:
68 return "FALSE"
69
70
71 swizzle_map = {
72 SWIZZLE_X: "PIPE_SWIZZLE_X",
73 SWIZZLE_Y: "PIPE_SWIZZLE_Y",
74 SWIZZLE_Z: "PIPE_SWIZZLE_Z",
75 SWIZZLE_W: "PIPE_SWIZZLE_W",
76 SWIZZLE_0: "PIPE_SWIZZLE_0",
77 SWIZZLE_1: "PIPE_SWIZZLE_1",
78 SWIZZLE_NONE: "PIPE_SWIZZLE_NONE",
79 }
80
81 def has_access(format):
82 # We don't generate code for YUV formats, and many of the new ones lack
83 # pack/unpack functions for softpipe/llvmpipe.
84 noaccess_formats = [
85 'yv12',
86 'yv16',
87 'iyuv',
88 'nv12',
89 'nv16',
90 'nv21',
91 'p010',
92 'p012',
93 'p016',
94 'xyuv',
95 'ayuv',
96 'r8g8_r8b8_unorm',
97 'g8r8_b8r8_unorm',
98 'g8r8_g8b8_unorm',
99 'y8_u8_v8_422_unorm',
100 'y8_u8v8_422_unorm',
101 'y8_u8_v8_444_unorm',
102 'y16_u16_v16_420_unorm',
103 'y16_u16_v16_422_unorm',
104 'y16_u16v16_422_unorm',
105 'y16_u16_v16_444_unorm',
106 ]
107 if format.short_name() in noaccess_formats:
108 return False
109 if format.layout in ('astc', 'atc', 'fxt1'):
110 return False
111 if format.layout == 'etc' and format.short_name() != 'etc1_rgb8':
112 return False
113 return True
114
115 def write_format_table_header(file):
116 print('/* This file is autogenerated by u_format_table.py from u_format.csv. Do not edit directly. */', file=file)
117 print(file=file)
118 # This will print the copyright message on the top of this file
119 print(CopyRight.strip(), file=file)
120 print(file=file)
121 print('#include "util/format/u_format.h"', file=file)
122
123 def write_format_table(formats):
124 write_format_table_header(sys.stdout)
125 print('#include "u_format_bptc.h"')
126 print('#include "u_format_s3tc.h"')
127 print('#include "u_format_rgtc.h"')
128 print('#include "u_format_latc.h"')
129 print('#include "u_format_etc.h"')
130 print()
131
132 write_format_table_header(sys.stdout2)
133
134 u_format_pack.generate(formats)
135
136 def do_channel_array(channels, swizzles):
137 print(" {")
138 for i in range(4):
139 channel = channels[i]
140 if i < 3:
141 sep = ","
142 else:
143 sep = ""
144 if channel.size:
145 print(" {%s, %s, %s, %u, %u}%s\t/* %s = %s */" % (type_map[channel.type], bool_map(channel.norm), bool_map(channel.pure), channel.size, channel.shift, sep, "xyzw"[i], channel.name))
146 else:
147 print(" {0, 0, 0, 0, 0}%s" % (sep,))
148 print(" },")
149
150 def do_swizzle_array(channels, swizzles):
151 print(" {")
152 for i in range(4):
153 swizzle = swizzles[i]
154 if i < 3:
155 sep = ","
156 else:
157 sep = ""
158 try:
159 comment = colorspace_channels_map[format.colorspace][i]
160 except (KeyError, IndexError):
161 comment = 'ignored'
162 print(" %s%s\t/* %s */" % (swizzle_map[swizzle], sep, comment))
163 print(" },")
164
165 def generate_table_getter(type):
166 print("const struct util_format_%sdescription *" % type)
167 print("util_format_%sdescription(enum pipe_format format)" % type)
168 print("{")
169 print(" if (format >= ARRAY_SIZE(util_format_%sdescriptions))" % (type))
170 print(" return NULL;")
171 print()
172 print(" return &util_format_%sdescriptions[format];" % (type))
173 print("}")
174 print()
175
176 print('static const struct util_format_description')
177 print('util_format_descriptions[] = {')
178 for format in formats:
179 sn = format.short_name()
180
181 print(" [%s] = {" % (format.name,))
182 print(" %s," % (format.name,))
183 print(" \"%s\"," % (format.name,))
184 print(" \"%s\"," % (sn,))
185 print(" {%u, %u, %u, %u},\t/* block */" % (format.block_width, format.block_height, format.block_depth, format.block_size()))
186 print(" %s," % (layout_map(format.layout),))
187 print(" %u,\t/* nr_channels */" % (format.nr_channels(),))
188 print(" %s,\t/* is_array */" % (bool_map(format.is_array()),))
189 print(" %s,\t/* is_bitmask */" % (bool_map(format.is_bitmask()),))
190 print(" %s,\t/* is_mixed */" % (bool_map(format.is_mixed()),))
191 print(" %s,\t/* is_unorm */" % (bool_map(format.is_unorm()),))
192 print(" %s,\t/* is_snorm */" % (bool_map(format.is_snorm()),))
193 u_format_pack.print_channels(format, do_channel_array)
194 u_format_pack.print_channels(format, do_swizzle_array)
195 print(" %s," % (colorspace_map(format.colorspace),))
196 print(" },")
197 print()
198 print("};")
199 print()
200 generate_table_getter("")
201
202 print('static const struct util_format_pack_description')
203 print('util_format_pack_descriptions[] = {')
204 for format in formats:
205 sn = format.short_name()
206
207 if not has_access(format):
208 print(" [%s] = { 0 }," % (format.name,))
209 continue
210
211 print(" [%s] = {" % (format.name,))
212 if format.colorspace != ZS and not format.is_pure_color():
213 print(" .pack_rgba_8unorm = &util_format_%s_pack_rgba_8unorm," % sn)
214 print(" .pack_rgba_float = &util_format_%s_pack_rgba_float," % sn)
215
216 if format.has_depth():
217 print(" .pack_z_32unorm = &util_format_%s_pack_z_32unorm," % sn)
218 print(" .pack_z_float = &util_format_%s_pack_z_float," % sn)
219
220 if format.has_stencil():
221 print(" .pack_s_8uint = &util_format_%s_pack_s_8uint," % sn)
222
223 if format.is_pure_unsigned() or format.is_pure_signed():
224 print(" .pack_rgba_uint = &util_format_%s_pack_unsigned," % sn)
225 print(" .pack_rgba_sint = &util_format_%s_pack_signed," % sn)
226 print(" },")
227 print()
228 print("};")
229 print()
230 generate_table_getter("pack_")
231
232 print('static const struct util_format_unpack_description')
233 print('util_format_unpack_descriptions[] = {')
234 for format in formats:
235 sn = format.short_name()
236
237 if not has_access(format):
238 print(" [%s] = { 0 }," % (format.name,))
239 continue
240
241 print(" [%s] = {" % (format.name,))
242 if format.colorspace != ZS and not format.is_pure_color():
243 print(" .unpack_rgba_8unorm = &util_format_%s_unpack_rgba_8unorm," % sn)
244 if format.layout == 's3tc' or format.layout == 'rgtc':
245 print(" .fetch_rgba_8unorm = &util_format_%s_fetch_rgba_8unorm," % sn)
246 print(" .unpack_rgba = &util_format_%s_unpack_rgba_float," % sn)
247 print(" .fetch_rgba_float = &util_format_%s_fetch_rgba_float," % sn)
248
249 if format.has_depth():
250 print(" .unpack_z_32unorm = &util_format_%s_unpack_z_32unorm," % sn)
251 print(" .unpack_z_float = &util_format_%s_unpack_z_float," % sn)
252
253 if format.has_stencil():
254 print(" .unpack_s_8uint = &util_format_%s_unpack_s_8uint," % sn)
255
256 if format.is_pure_unsigned():
257 print(" .unpack_rgba = &util_format_%s_unpack_unsigned," % sn)
258 print(" .fetch_rgba_uint = &util_format_%s_fetch_unsigned," % sn)
259 elif format.is_pure_signed():
260 print(" .unpack_rgba = &util_format_%s_unpack_signed," % sn)
261 print(" .fetch_rgba_sint = &util_format_%s_fetch_signed," % sn)
262 print(" },")
263 print("};")
264 print()
265
266 generate_table_getter("unpack_")
267
268 def main():
269 formats = []
270
271 sys.stdout2 = open(os.devnull, "w")
272
273 for arg in sys.argv[1:]:
274 if arg == '--header':
275 sys.stdout2 = sys.stdout
276 sys.stdout = open(os.devnull, "w")
277 continue
278
279 formats.extend(parse(arg))
280
281 write_format_table(formats)
282
283 if __name__ == '__main__':
284 main()