1#!/usr/bin/env python
2
3CopyRight = '''
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
33import sys
34
35from u_format_parse import *
36import u_format_pack
37
38
39def layout_map(layout):
40    return 'UTIL_FORMAT_LAYOUT_' + str(layout).upper()
41
42
43def colorspace_map(colorspace):
44    return 'UTIL_FORMAT_COLORSPACE_' + str(colorspace).upper()
45
46
47colorspace_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
55type_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
64def bool_map(value):
65    if value:
66        return "TRUE"
67    else:
68        return "FALSE"
69
70
71swizzle_map = {
72    SWIZZLE_X:    "UTIL_FORMAT_SWIZZLE_X",
73    SWIZZLE_Y:    "UTIL_FORMAT_SWIZZLE_Y",
74    SWIZZLE_Z:    "UTIL_FORMAT_SWIZZLE_Z",
75    SWIZZLE_W:    "UTIL_FORMAT_SWIZZLE_W",
76    SWIZZLE_0:    "UTIL_FORMAT_SWIZZLE_0",
77    SWIZZLE_1:    "UTIL_FORMAT_SWIZZLE_1",
78    SWIZZLE_NONE: "UTIL_FORMAT_SWIZZLE_NONE",
79}
80
81
82def 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_s3tc.h"'
90    print '#include "u_format_rgtc.h"'
91    print '#include "u_format_latc.h"'
92    print '#include "u_format_etc.h"'
93    print
94
95    u_format_pack.generate(formats)
96
97    for format in formats:
98        print 'const struct util_format_description'
99        print 'util_format_%s_description = {' % (format.short_name(),)
100        print "   %s," % (format.name,)
101        print "   \"%s\"," % (format.name,)
102        print "   \"%s\"," % (format.short_name(),)
103        print "   {%u, %u, %u},\t/* block */" % (format.block_width, format.block_height, format.block_size())
104        print "   %s," % (layout_map(format.layout),)
105        print "   %u,\t/* nr_channels */" % (format.nr_channels(),)
106        print "   %s,\t/* is_array */" % (bool_map(format.is_array()),)
107        print "   %s,\t/* is_bitmask */" % (bool_map(format.is_bitmask()),)
108        print "   %s,\t/* is_mixed */" % (bool_map(format.is_mixed()),)
109        print "   {"
110        for i in range(4):
111            channel = format.channels[i]
112            if i < 3:
113                sep = ","
114            else:
115                sep = ""
116            if channel.size:
117                print "      {%s, %s, %s, %u}%s\t/* %s = %s */" % (type_map[channel.type], bool_map(channel.norm), bool_map(channel.pure), channel.size, sep, "xyzw"[i], channel.name)
118            else:
119                print "      {0, 0, 0, 0}%s" % (sep,)
120        print "   },"
121        print "   {"
122        for i in range(4):
123            swizzle = format.swizzles[i]
124            if i < 3:
125                sep = ","
126            else:
127                sep = ""
128            try:
129                comment = colorspace_channels_map[format.colorspace][i]
130            except (KeyError, IndexError):
131                comment = 'ignored'
132            print "      %s%s\t/* %s */" % (swizzle_map[swizzle], sep, comment)
133        print "   },"
134        print "   %s," % (colorspace_map(format.colorspace),)
135        if format.colorspace != ZS and format.channels[0].pure == False:
136            print "   &util_format_%s_unpack_rgba_8unorm," % format.short_name()
137            print "   &util_format_%s_pack_rgba_8unorm," % format.short_name()
138            if format.layout == 's3tc' or format.layout == 'rgtc':
139                print "   &util_format_%s_fetch_rgba_8unorm," % format.short_name()
140            else:
141                print "   NULL, /* fetch_rgba_8unorm */"
142            print "   &util_format_%s_unpack_rgba_float," % format.short_name()
143            print "   &util_format_%s_pack_rgba_float," % format.short_name()
144            print "   &util_format_%s_fetch_rgba_float," % format.short_name()
145        else:
146            print "   NULL, /* unpack_rgba_8unorm */"
147            print "   NULL, /* pack_rgba_8unorm */"
148            print "   NULL, /* fetch_rgba_8unorm */"
149            print "   NULL, /* unpack_rgba_float */"
150            print "   NULL, /* pack_rgba_float */"
151            print "   NULL, /* fetch_rgba_float */"
152        if format.colorspace == ZS and format.swizzles[0] != SWIZZLE_NONE:
153            print "   &util_format_%s_unpack_z_32unorm," % format.short_name()
154            print "   &util_format_%s_pack_z_32unorm," % format.short_name()
155            print "   &util_format_%s_unpack_z_float," % format.short_name()
156            print "   &util_format_%s_pack_z_float," % format.short_name()
157        else:
158            print "   NULL, /* unpack_z_32unorm */"
159            print "   NULL, /* pack_z_32unorm */"
160            print "   NULL, /* unpack_z_float */"
161            print "   NULL, /* pack_z_float */"
162        if format.colorspace == ZS and format.swizzles[1] != SWIZZLE_NONE:
163            print "   &util_format_%s_unpack_s_8uint," % format.short_name()
164            print "   &util_format_%s_pack_s_8uint," % format.short_name()
165        else:
166            print "   NULL, /* unpack_s_8uint */"
167            print "   NULL, /* pack_s_8uint */"
168        if format.colorspace != ZS and format.channels[0].pure == True and format.channels[0].type == UNSIGNED:
169            print "   &util_format_%s_unpack_unsigned, /* unpack_rgba_uint */" % format.short_name()
170            print "   &util_format_%s_pack_unsigned, /* pack_rgba_uint */" % format.short_name()
171            print "   &util_format_%s_unpack_signed, /* unpack_rgba_sint */" % format.short_name()
172            print "   &util_format_%s_pack_signed,  /* pack_rgba_sint */" % format.short_name()
173            print "   &util_format_%s_fetch_unsigned,  /* fetch_rgba_uint */" % format.short_name()
174            print "   NULL  /* fetch_rgba_sint */"
175        elif format.colorspace != ZS and format.channels[0].pure == True and format.channels[0].type == SIGNED:
176            print "   &util_format_%s_unpack_unsigned, /* unpack_rgba_uint */" % format.short_name()
177            print "   &util_format_%s_pack_unsigned, /* pack_rgba_uint */" % format.short_name()
178            print "   &util_format_%s_unpack_signed, /* unpack_rgba_sint */" % format.short_name()
179            print "   &util_format_%s_pack_signed,  /* pack_rgba_sint */" % format.short_name()
180            print "   NULL,  /* fetch_rgba_uint */"
181            print "   &util_format_%s_fetch_signed  /* fetch_rgba_sint */" % format.short_name()
182        else:
183            print "   NULL, /* unpack_rgba_uint */"
184            print "   NULL, /* pack_rgba_uint */"
185            print "   NULL, /* unpack_rgba_sint */"
186            print "   NULL, /* pack_rgba_sint */"
187            print "   NULL, /* fetch_rgba_uint */"
188            print "   NULL  /* fetch_rgba_sint */"
189        print "};"
190        print
191
192    print "const struct util_format_description *"
193    print "util_format_description(enum pipe_format format)"
194    print "{"
195    print "   if (format >= PIPE_FORMAT_COUNT) {"
196    print "      return NULL;"
197    print "   }"
198    print
199    print "   switch (format) {"
200    for format in formats:
201        print "   case %s:" % format.name
202        print "      return &util_format_%s_description;" % (format.short_name(),)
203    print "   default:"
204    print "      return NULL;"
205    print "   }"
206    print "}"
207    print
208
209
210def main():
211
212    formats = []
213    for arg in sys.argv[1:]:
214        formats.extend(parse(arg))
215    write_format_table(formats)
216
217
218if __name__ == '__main__':
219    main()
220