util: Split the pack/unpack functions out of the format desc.
[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
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
82 def write_format_table(formats):
83 print('/* This file is autogenerated by u_format_table.py from u_format.csv. Do not edit directly. */')
84 print()
85 # This will print the copyright message on the top of this file
86 print(CopyRight.strip())
87 print()
88 print('#include "u_format.h"')
89 print('#include "u_format_bptc.h"')
90 print('#include "u_format_s3tc.h"')
91 print('#include "u_format_rgtc.h"')
92 print('#include "u_format_latc.h"')
93 print('#include "u_format_etc.h"')
94 print()
95
96 u_format_pack.generate(formats)
97
98 def do_channel_array(channels, swizzles):
99 print(" {")
100 for i in range(4):
101 channel = channels[i]
102 if i < 3:
103 sep = ","
104 else:
105 sep = ""
106 if channel.size:
107 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))
108 else:
109 print(" {0, 0, 0, 0, 0}%s" % (sep,))
110 print(" },")
111
112 def do_swizzle_array(channels, swizzles):
113 print(" {")
114 for i in range(4):
115 swizzle = swizzles[i]
116 if i < 3:
117 sep = ","
118 else:
119 sep = ""
120 try:
121 comment = colorspace_channels_map[format.colorspace][i]
122 except (KeyError, IndexError):
123 comment = 'ignored'
124 print(" %s%s\t/* %s */" % (swizzle_map[swizzle], sep, comment))
125 print(" },")
126
127 def generate_table_getter(type):
128 print("const struct util_format_%sdescription *" % type)
129 print("util_format_%sdescription(enum pipe_format format)" % type)
130 print("{")
131 print(" if (format >= PIPE_FORMAT_COUNT) {")
132 print(" return NULL;")
133 print(" }")
134 print()
135 print(" switch (format) {")
136 for format in formats:
137 print(" case %s:" % format.name)
138 print(" return &util_format_%s_%sdescription;" % (format.short_name(), type))
139 print(" default:")
140 print(" return NULL;")
141 print(" }")
142 print("}")
143 print()
144
145
146 for format in formats:
147 sn = format.short_name()
148
149 print('const struct util_format_description')
150 print('util_format_%s_description = {' % (sn,))
151 print(" %s," % (format.name,))
152 print(" \"%s\"," % (format.name,))
153 print(" \"%s\"," % (sn,))
154 print(" {%u, %u, %u, %u},\t/* block */" % (format.block_width, format.block_height, format.block_depth, format.block_size()))
155 print(" %s," % (layout_map(format.layout),))
156 print(" %u,\t/* nr_channels */" % (format.nr_channels(),))
157 print(" %s,\t/* is_array */" % (bool_map(format.is_array()),))
158 print(" %s,\t/* is_bitmask */" % (bool_map(format.is_bitmask()),))
159 print(" %s,\t/* is_mixed */" % (bool_map(format.is_mixed()),))
160 print(" %s,\t/* is_unorm */" % (bool_map(format.is_unorm()),))
161 print(" %s,\t/* is_snorm */" % (bool_map(format.is_snorm()),))
162 u_format_pack.print_channels(format, do_channel_array)
163 u_format_pack.print_channels(format, do_swizzle_array)
164 print(" %s," % (colorspace_map(format.colorspace),))
165 print("};")
166 print()
167
168 # We don't generate code for YUV formats, and many of the new ones lack pack/unpack
169 # functions for softpipe/llvmpipe.
170 noaccess_formats = [
171 'yv12',
172 'yv16',
173 'iyuv',
174 'nv12',
175 'nv16',
176 'nv21',
177 'p010',
178 'p012',
179 'p016',
180 'xyuv',
181 'ayuv',
182 'r8g8_r8b8_unorm',
183 'g8r8_b8r8_unorm',
184 'g8r8_g8b8_unorm',
185 'y8_u8_v8_422_unorm',
186 'y8_u8v8_422_unorm',
187 'y8_u8_v8_444_unorm',
188 'y16_u16_v16_420_unorm',
189 'y16_u16_v16_422_unorm',
190 'y16_u16v16_422_unorm',
191 'y16_u16_v16_444_unorm',
192 ]
193 access = sn not in noaccess_formats
194 if format.layout in ('astc', 'atc', 'fxt1'):
195 access = False
196 if format.layout == 'etc' and sn != 'etc1_rgb8':
197 access = False
198
199 print('const struct util_format_pack_description')
200 print('util_format_%s_pack_description = {' % sn)
201 if format.colorspace != ZS and not format.is_pure_color() and access:
202 print(" .pack_rgba_8unorm = &util_format_%s_pack_rgba_8unorm," % sn)
203 print(" .pack_rgba_float = &util_format_%s_pack_rgba_float," % sn)
204
205 if format.has_depth():
206 print(" .pack_z_32unorm = &util_format_%s_pack_z_32unorm," % sn)
207 print(" .pack_z_float = &util_format_%s_pack_z_float," % sn)
208
209 if format.has_stencil():
210 print(" .pack_s_8uint = &util_format_%s_pack_s_8uint," % sn)
211
212 if format.is_pure_unsigned() or format.is_pure_signed():
213 print(" .pack_rgba_uint = &util_format_%s_pack_unsigned," % sn)
214 print(" .pack_rgba_sint = &util_format_%s_pack_signed," % sn)
215 print("};")
216 print()
217
218 print('const struct util_format_unpack_description')
219 print('util_format_%s_unpack_description = {' % sn)
220 if format.colorspace != ZS and not format.is_pure_color() and access:
221 print(" .unpack_rgba_8unorm = &util_format_%s_unpack_rgba_8unorm," % sn)
222 if format.layout == 's3tc' or format.layout == 'rgtc':
223 print(" .fetch_rgba_8unorm = &util_format_%s_fetch_rgba_8unorm," % sn)
224 print(" .unpack_rgba = &util_format_%s_unpack_rgba_float," % sn)
225 print(" .fetch_rgba_float = &util_format_%s_fetch_rgba_float," % sn)
226
227 if format.has_depth():
228 print(" .unpack_z_32unorm = &util_format_%s_unpack_z_32unorm," % sn)
229 print(" .unpack_z_float = &util_format_%s_unpack_z_float," % sn)
230
231 if format.has_stencil():
232 print(" .unpack_s_8uint = &util_format_%s_unpack_s_8uint," % sn)
233
234 if format.is_pure_unsigned():
235 print(" .unpack_rgba = &util_format_%s_unpack_unsigned," % sn)
236 print(" .fetch_rgba_uint = &util_format_%s_fetch_unsigned," % sn)
237 elif format.is_pure_signed():
238 print(" .unpack_rgba = &util_format_%s_unpack_signed," % sn)
239 print(" .fetch_rgba_sint = &util_format_%s_fetch_signed," % sn)
240 print("};")
241 print()
242
243 generate_table_getter("")
244 generate_table_getter("pack_")
245 generate_table_getter("unpack_")
246
247 def main():
248
249 formats = []
250 for arg in sys.argv[1:]:
251 formats.extend(parse(arg))
252 write_format_table(formats)
253
254
255 if __name__ == '__main__':
256 main()