1#!/usr/bin/env python
2
3from __future__ import print_function
4CopyRight = '''
5/**************************************************************************
6 *
7 * Copyright 2010 VMware, Inc.
8 * All Rights Reserved.
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the
12 * "Software"), to deal in the Software without restriction, including
13 * without limitation the rights to use, copy, modify, merge, publish,
14 * distribute, sub license, and/or sell copies of the Software, and to
15 * permit persons to whom the Software is furnished to do so, subject to
16 * the following conditions:
17 *
18 * The above copyright notice and this permission notice (including the
19 * next paragraph) shall be included in all copies or substantial portions
20 * of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
25 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
26 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 *
30 **************************************************************************/
31'''
32
33
34import sys
35
36from u_format_parse import *
37import u_format_pack
38
39
40def layout_map(layout):
41    return 'UTIL_FORMAT_LAYOUT_' + str(layout).upper()
42
43
44def colorspace_map(colorspace):
45    return 'UTIL_FORMAT_COLORSPACE_' + str(colorspace).upper()
46
47
48colorspace_channels_map = {
49    'rgb': ['r', 'g', 'b', 'a'],
50    'srgb': ['sr', 'sg', 'sb', 'a'],
51    'zs': ['z', 's'],
52    'yuv': ['y', 'u', 'v'],
53}
54
55
56type_map = {
57    VOID:     "UTIL_FORMAT_TYPE_VOID",
58    UNSIGNED: "UTIL_FORMAT_TYPE_UNSIGNED",
59    SIGNED:   "UTIL_FORMAT_TYPE_SIGNED",
60    FIXED:    "UTIL_FORMAT_TYPE_FIXED",
61    FLOAT:    "UTIL_FORMAT_TYPE_FLOAT",
62}
63
64
65def bool_map(value):
66    if value:
67        return "TRUE"
68    else:
69        return "FALSE"
70
71
72swizzle_map = {
73    SWIZZLE_X:    "UTIL_FORMAT_SWIZZLE_X",
74    SWIZZLE_Y:    "UTIL_FORMAT_SWIZZLE_Y",
75    SWIZZLE_Z:    "UTIL_FORMAT_SWIZZLE_Z",
76    SWIZZLE_W:    "UTIL_FORMAT_SWIZZLE_W",
77    SWIZZLE_0:    "UTIL_FORMAT_SWIZZLE_0",
78    SWIZZLE_1:    "UTIL_FORMAT_SWIZZLE_1",
79    SWIZZLE_NONE: "UTIL_FORMAT_SWIZZLE_NONE",
80}
81
82
83def write_format_table(formats):
84    print('/* This file is autogenerated by u_format_table.py from u_format.csv. Do not edit directly. */')
85    print()
86    # This will print the copyright message on the top of this file
87    print(CopyRight.strip())
88    print()
89    print('#include "u_format.h"')
90    print()
91
92    u_format_pack.generate(formats)
93
94    def do_channel_array(channels, swizzles):
95        print("   {")
96        for i in range(4):
97            channel = channels[i]
98            if i < 3:
99                sep = ","
100            else:
101                sep = ""
102            if channel.size:
103                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))
104            else:
105                print("      {0, 0, 0, 0, 0}%s" % (sep,))
106        print("   },")
107
108    def do_swizzle_array(channels, swizzles):
109        print("   {")
110        for i in range(4):
111            swizzle = swizzles[i]
112            if i < 3:
113                sep = ","
114            else:
115                sep = ""
116            try:
117                comment = colorspace_channels_map[format.colorspace][i]
118            except (KeyError, IndexError):
119                comment = 'ignored'
120            print("      %s%s\t/* %s */" % (swizzle_map[swizzle], sep, comment))
121        print("   },")
122
123    for format in formats:
124        print('const struct util_format_description')
125        print('util_format_%s_description = {' % (format.short_name(),))
126        print("   %s," % (format.name,))
127        print("   \"%s\"," % (format.name,))
128        print("   \"%s\"," % (format.short_name(),))
129        print("   {%u, %u, %u},\t/* block */" % (format.block_width, format.block_height, format.block_size()))
130        print("   %s," % (layout_map(format.layout),))
131        print("   %u,\t/* nr_channels */" % (format.nr_channels(),))
132        print("   %s,\t/* is_array */" % (bool_map(format.is_array()),))
133        print("   %s,\t/* is_bitmask */" % (bool_map(format.is_bitmask()),))
134        print("   %s,\t/* is_mixed */" % (bool_map(format.is_mixed()),))
135        u_format_pack.print_channels(format, do_channel_array)
136        u_format_pack.print_channels(format, do_swizzle_array)
137        print("   %s," % (colorspace_map(format.colorspace),))
138        print("};")
139        print()
140
141    print("const struct util_format_description *")
142    print("util_format_description(enum pipe_format format)")
143    print("{")
144    print("   if (format >= PIPE_FORMAT_COUNT) {")
145    print("      return NULL;")
146    print("   }")
147    print()
148    print("   switch (format) {")
149    for format in formats:
150        print("   case %s:" % format.name)
151        print("      return &util_format_%s_description;" % (format.short_name(),))
152    print("   default:")
153    print("      return NULL;")
154    print("   }")
155    print("}")
156    print()
157
158
159def main():
160
161    formats = []
162    for arg in sys.argv[1:]:
163        formats.extend(parse(arg))
164    write_format_table(formats)
165
166
167if __name__ == '__main__':
168    main()
169