• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 *
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 "pipe/p_compiler.h"')
89    print('#include "u_format.h"')
90    print('#include "u_half.h"')
91    print('#include "u_math.h"')
92    print()
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    def print_channels(format, func):
124        if format.nr_channels() <= 1:
125            func(format.le_channels, format.le_swizzles)
126        else:
127            print('#ifdef PIPE_ARCH_BIG_ENDIAN')
128            func(format.be_channels, format.be_swizzles)
129            print('#else')
130            func(format.le_channels, format.le_swizzles)
131            print('#endif')
132
133    for format in formats:
134        print('const struct util_format_description')
135        print('util_format_%s_description = {' % (format.short_name(),))
136        print("   %s," % (format.name,))
137        print("   \"%s\"," % (format.name,))
138        print("   \"%s\"," % (format.short_name(),))
139        print("   {%u, %u, %u},\t/* block */" % (format.block_width, format.block_height, format.block_size()))
140        print("   %s," % (layout_map(format.layout),))
141        print("   %u,\t/* nr_channels */" % (format.nr_channels(),))
142        print("   %s,\t/* is_array */" % (bool_map(format.is_array()),))
143        print("   %s,\t/* is_bitmask */" % (bool_map(format.is_bitmask()),))
144        print("   %s,\t/* is_mixed */" % (bool_map(format.is_mixed()),))
145        print_channels(format, do_channel_array)
146        print_channels(format, do_swizzle_array)
147        print("   %s," % (colorspace_map(format.colorspace),))
148        print("};")
149        print()
150
151    print("const struct util_format_description *")
152    print("util_format_description(enum pipe_format format)")
153    print("{")
154    print("   if (format >= PIPE_FORMAT_COUNT) {")
155    print("      return NULL;")
156    print("   }")
157    print()
158    print("   switch (format) {")
159    for format in formats:
160        print("   case %s:" % format.name)
161        print("      return &util_format_%s_description;" % (format.short_name(),))
162    print("   default:")
163    print("      return NULL;")
164    print("   }")
165    print("}")
166    print()
167
168
169def main():
170
171    formats = []
172    for arg in sys.argv[1:]:
173        formats.extend(parse(arg))
174    write_format_table(formats)
175
176
177if __name__ == '__main__':
178    main()
179