b1e58d4d4790aaffc75db242ea7606d7bc6aa91d
1 from __future__
import print_function
4 /**************************************************************************
6 * Copyright 2010 VMware, Inc.
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:
17 * The above copyright notice and this permission notice (including the
18 * next paragraph) shall be included in all copies or substantial portions
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.
29 **************************************************************************/
35 from u_format_parse
import *
39 def layout_map(layout
):
40 return 'UTIL_FORMAT_LAYOUT_' + str(layout
).upper()
43 def colorspace_map(colorspace
):
44 return 'UTIL_FORMAT_COLORSPACE_' + str(colorspace
).upper()
47 colorspace_channels_map
= {
48 'rgb': ['r', 'g', 'b', 'a'],
49 'srgb': ['sr', 'sg', 'sb', 'a'],
51 'yuv': ['y', 'u', 'v'],
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",
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",
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.
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',
107 if format
.short_name() in noaccess_formats
:
109 if format
.layout
in ('astc', 'atc', 'fxt1'):
111 if format
.layout
== 'etc' and format
.short_name() != 'etc1_rgb8':
115 def write_format_table(formats
):
116 print('/* This file is autogenerated by u_format_table.py from u_format.csv. Do not edit directly. */')
118 # This will print the copyright message on the top of this file
119 print(CopyRight
.strip())
121 print('#include "u_format.h"')
122 print('#include "u_format_bptc.h"')
123 print('#include "u_format_s3tc.h"')
124 print('#include "u_format_rgtc.h"')
125 print('#include "u_format_latc.h"')
126 print('#include "u_format_etc.h"')
129 u_format_pack
.generate(formats
)
131 def do_channel_array(channels
, swizzles
):
134 channel
= channels
[i
]
140 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
))
142 print(" {0, 0, 0, 0, 0}%s" % (sep
,))
145 def do_swizzle_array(channels
, swizzles
):
148 swizzle
= swizzles
[i
]
154 comment
= colorspace_channels_map
[format
.colorspace
][i
]
155 except (KeyError, IndexError):
157 print(" %s%s\t/* %s */" % (swizzle_map
[swizzle
], sep
, comment
))
160 def generate_table_getter(type):
161 print("const struct util_format_%sdescription *" % type)
162 print("util_format_%sdescription(enum pipe_format format)" % type)
164 print(" if (format >= ARRAY_SIZE(util_format_%sdescriptions))" % (type))
165 print(" return NULL;")
167 print(" return &util_format_%sdescriptions[format];" % (type))
171 print('static const struct util_format_description')
172 print('util_format_descriptions[] = {')
173 for format
in formats
:
174 sn
= format
.short_name()
176 print(" [%s] = {" % (format
.name
,))
177 print(" %s," % (format
.name
,))
178 print(" \"%s\"," % (format
.name
,))
179 print(" \"%s\"," % (sn
,))
180 print(" {%u, %u, %u, %u},\t/* block */" % (format
.block_width
, format
.block_height
, format
.block_depth
, format
.block_size()))
181 print(" %s," % (layout_map(format
.layout
),))
182 print(" %u,\t/* nr_channels */" % (format
.nr_channels(),))
183 print(" %s,\t/* is_array */" % (bool_map(format
.is_array()),))
184 print(" %s,\t/* is_bitmask */" % (bool_map(format
.is_bitmask()),))
185 print(" %s,\t/* is_mixed */" % (bool_map(format
.is_mixed()),))
186 print(" %s,\t/* is_unorm */" % (bool_map(format
.is_unorm()),))
187 print(" %s,\t/* is_snorm */" % (bool_map(format
.is_snorm()),))
188 u_format_pack
.print_channels(format
, do_channel_array
)
189 u_format_pack
.print_channels(format
, do_swizzle_array
)
190 print(" %s," % (colorspace_map(format
.colorspace
),))
195 generate_table_getter("")
197 print('static const struct util_format_pack_description')
198 print('util_format_pack_descriptions[] = {')
199 for format
in formats
:
200 sn
= format
.short_name()
202 if not has_access(format
):
203 print(" [%s] = { 0 }," % (format
.name
,))
206 print(" [%s] = {" % (format
.name
,))
207 if format
.colorspace
!= ZS
and not format
.is_pure_color():
208 print(" .pack_rgba_8unorm = &util_format_%s_pack_rgba_8unorm," % sn
)
209 print(" .pack_rgba_float = &util_format_%s_pack_rgba_float," % sn
)
211 if format
.has_depth():
212 print(" .pack_z_32unorm = &util_format_%s_pack_z_32unorm," % sn
)
213 print(" .pack_z_float = &util_format_%s_pack_z_float," % sn
)
215 if format
.has_stencil():
216 print(" .pack_s_8uint = &util_format_%s_pack_s_8uint," % sn
)
218 if format
.is_pure_unsigned() or format
.is_pure_signed():
219 print(" .pack_rgba_uint = &util_format_%s_pack_unsigned," % sn
)
220 print(" .pack_rgba_sint = &util_format_%s_pack_signed," % sn
)
225 generate_table_getter("pack_")
227 print('static const struct util_format_unpack_description')
228 print('util_format_unpack_descriptions[] = {')
229 for format
in formats
:
230 sn
= format
.short_name()
232 if not has_access(format
):
233 print(" [%s] = { 0 }," % (format
.name
,))
236 print(" [%s] = {" % (format
.name
,))
237 if format
.colorspace
!= ZS
and not format
.is_pure_color():
238 print(" .unpack_rgba_8unorm = &util_format_%s_unpack_rgba_8unorm," % sn
)
239 if format
.layout
== 's3tc' or format
.layout
== 'rgtc':
240 print(" .fetch_rgba_8unorm = &util_format_%s_fetch_rgba_8unorm," % sn
)
241 print(" .unpack_rgba = &util_format_%s_unpack_rgba_float," % sn
)
242 print(" .fetch_rgba_float = &util_format_%s_fetch_rgba_float," % sn
)
244 if format
.has_depth():
245 print(" .unpack_z_32unorm = &util_format_%s_unpack_z_32unorm," % sn
)
246 print(" .unpack_z_float = &util_format_%s_unpack_z_float," % sn
)
248 if format
.has_stencil():
249 print(" .unpack_s_8uint = &util_format_%s_unpack_s_8uint," % sn
)
251 if format
.is_pure_unsigned():
252 print(" .unpack_rgba = &util_format_%s_unpack_unsigned," % sn
)
253 print(" .fetch_rgba_uint = &util_format_%s_fetch_unsigned," % sn
)
254 elif format
.is_pure_signed():
255 print(" .unpack_rgba = &util_format_%s_unpack_signed," % sn
)
256 print(" .fetch_rgba_sint = &util_format_%s_fetch_signed," % sn
)
261 generate_table_getter("unpack_")
266 for arg
in sys
.argv
[1:]:
267 formats
.extend(parse(arg
))
268 write_format_table(formats
)
271 if __name__
== '__main__':