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 vk_format_parse import *
36
37def layout_map(layout):
38    return 'VK_FORMAT_LAYOUT_' + str(layout).upper()
39
40
41def colorspace_map(colorspace):
42    return 'VK_FORMAT_COLORSPACE_' + str(colorspace).upper()
43
44
45colorspace_channels_map = {
46    'rgb': ['r', 'g', 'b', 'a'],
47    'srgb': ['sr', 'sg', 'sb', 'a'],
48    'zs': ['z', 's'],
49    'yuv': ['y', 'u', 'v'],
50}
51
52
53type_map = {
54    VOID:     "VK_FORMAT_TYPE_VOID",
55    UNSIGNED: "VK_FORMAT_TYPE_UNSIGNED",
56    SIGNED:   "VK_FORMAT_TYPE_SIGNED",
57    FIXED:    "VK_FORMAT_TYPE_FIXED",
58    FLOAT:    "VK_FORMAT_TYPE_FLOAT",
59}
60
61
62def bool_map(value):
63    if value:
64        return "true"
65    else:
66        return "false"
67
68
69swizzle_map = {
70    SWIZZLE_X:    "VK_SWIZZLE_X",
71    SWIZZLE_Y:    "VK_SWIZZLE_Y",
72    SWIZZLE_Z:    "VK_SWIZZLE_Z",
73    SWIZZLE_W:    "VK_SWIZZLE_W",
74    SWIZZLE_0:    "VK_SWIZZLE_0",
75    SWIZZLE_1:    "VK_SWIZZLE_1",
76    SWIZZLE_NONE: "VK_SWIZZLE_NONE",
77}
78
79def print_channels(format, func):
80    if format.nr_channels() <= 1:
81        func(format.le_channels, format.le_swizzles)
82    else:
83        print '#ifdef PIPE_ARCH_BIG_ENDIAN'
84        func(format.be_channels, format.be_swizzles)
85        print '#else'
86        func(format.le_channels, format.le_swizzles)
87        print '#endif'
88
89def write_format_table(formats):
90    print '/* This file is autogenerated by u_format_table.py from u_format.csv. Do not edit directly. */'
91    print
92    # This will print the copyright message on the top of this file
93    print CopyRight.strip()
94    print
95    print '#include "stdbool.h"'
96    print '#include "vk_format.h"'
97    print
98
99    def do_channel_array(channels, swizzles):
100        print "   {"
101        for i in range(4):
102            channel = channels[i]
103            if i < 3:
104                sep = ","
105            else:
106                sep = ""
107            if channel.size:
108                print "      {%s, %s, %s, %s, %u, %u}%s\t/* %s = %s */" % (type_map[channel.type], bool_map(channel.norm), bool_map(channel.pure), bool_map(channel.scaled), channel.size, channel.shift, sep, "xyzw"[i], channel.name)
109            else:
110                print "      {0, 0, 0, 0, 0}%s" % (sep,)
111        print "   },"
112
113    def do_swizzle_array(channels, swizzles):
114        print "   {"
115        for i in range(4):
116            swizzle = swizzles[i]
117            if i < 3:
118                sep = ","
119            else:
120                sep = ""
121            try:
122                comment = colorspace_channels_map[format.colorspace][i]
123            except (KeyError, IndexError):
124                comment = 'ignored'
125            print "      %s%s\t/* %s */" % (swizzle_map[swizzle], sep, comment)
126        print "   },"
127
128    for format in formats:
129        print 'const struct vk_format_description'
130        print 'vk_format_%s_description = {' % (format.short_name(),)
131        print "   %s," % (format.name,)
132        print "   \"%s\"," % (format.name,)
133        print "   \"%s\"," % (format.short_name(),)
134        print "   {%u, %u, %u},\t/* block */" % (format.block_width, format.block_height, format.block_size())
135        print "   %s," % (layout_map(format.layout),)
136        print "   %u,\t/* nr_channels */" % (format.nr_channels(),)
137        print "   %s,\t/* is_array */" % (bool_map(format.is_array()),)
138        print "   %s,\t/* is_bitmask */" % (bool_map(format.is_bitmask()),)
139        print "   %s,\t/* is_mixed */" % (bool_map(format.is_mixed()),)
140        print_channels(format, do_channel_array)
141        print_channels(format, do_swizzle_array)
142        print "   %s," % (colorspace_map(format.colorspace),)
143        print "};"
144        print
145
146    print "const struct vk_format_description *"
147    print "vk_format_description(VkFormat format)"
148    print "{"
149    print "   if (format > VK_FORMAT_END_RANGE) {"
150    print "      return NULL;"
151    print "   }"
152    print
153    print "   switch (format) {"
154    for format in formats:
155        print "   case %s:" % format.name
156        print "      return &vk_format_%s_description;" % (format.short_name(),)
157    print "   default:"
158    print "      return NULL;"
159    print "   }"
160    print "}"
161    print
162
163
164def main():
165
166    formats = []
167    for arg in sys.argv[1:]:
168        formats.extend(parse(arg))
169    write_format_table(formats)
170
171
172if __name__ == '__main__':
173    main()
174