1from __future__ import print_function
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, os
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:    "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",
79}
80
81def 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.
84    noaccess_formats = [
85        'r1_unorm',
86        'yv12',
87        'yv16',
88        'iyuv',
89        'nv12',
90        'nv16',
91        'nv21',
92        'p010',
93        'p012',
94        'p016',
95        'xyuv',
96        'ayuv',
97        'r8g8_r8b8_unorm',
98        'g8r8_b8r8_unorm',
99        'g8r8_g8b8_unorm',
100        'y8_u8_v8_422_unorm',
101        'y8_u8v8_422_unorm',
102        'y8_u8_v8_444_unorm',
103        'y16_u16_v16_420_unorm',
104        'y16_u16_v16_422_unorm',
105        'y16_u16v16_422_unorm',
106        'y16_u16_v16_444_unorm',
107        'r8_g8b8_420_unorm',
108    ]
109    if format.short_name() in noaccess_formats:
110        return False
111    if format.layout in ('astc', 'atc', 'fxt1'):
112        return False
113    if format.layout == 'etc' and format.short_name() != 'etc1_rgb8':
114        return False
115    return True
116
117def write_format_table_header(file):
118    print('/* This file is autogenerated by u_format_table.py from u_format.csv. Do not edit directly. */', file=file)
119    print(file=file)
120    # This will print the copyright message on the top of this file
121    print(CopyRight.strip(), file=file)
122    print(file=file)
123    print('#include "util/format/u_format.h"', file=file)
124
125def write_format_table(formats):
126    write_format_table_header(sys.stdout)
127    print('#include "u_format_bptc.h"')
128    print('#include "u_format_s3tc.h"')
129    print('#include "u_format_rgtc.h"')
130    print('#include "u_format_latc.h"')
131    print('#include "u_format_etc.h"')
132    print()
133
134    write_format_table_header(sys.stdout2)
135
136    u_format_pack.generate(formats)
137
138    def do_channel_array(channels, swizzles):
139        print("   {")
140        for i in range(4):
141            channel = channels[i]
142            if i < 3:
143                sep = ","
144            else:
145                sep = ""
146            if channel.size:
147                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))
148            else:
149                print("      {0, 0, 0, 0, 0}%s" % (sep,))
150        print("   },")
151
152    def do_swizzle_array(channels, swizzles):
153        print("   {")
154        for i in range(4):
155            swizzle = swizzles[i]
156            if i < 3:
157                sep = ","
158            else:
159                sep = ""
160            try:
161                comment = colorspace_channels_map[format.colorspace][i]
162            except (KeyError, IndexError):
163                comment = 'ignored'
164            print("      %s%s\t/* %s */" % (swizzle_map[swizzle], sep, comment))
165        print("   },")
166
167    def generate_table_getter(type):
168        print("const struct util_format_%sdescription *" % type)
169        print("util_format_%sdescription(enum pipe_format format)" % type)
170        print("{")
171        print("   if (format >= ARRAY_SIZE(util_format_%sdescriptions))" % (type))
172        print("      return NULL;")
173        print()
174        print("   return &util_format_%sdescriptions[format];" % (type))
175        print("}")
176        print()
177
178    def generate_function_getter(func):
179        print("util_format_%s_func_ptr" % func)
180        print("util_format_%s_func(enum pipe_format format)" % (func))
181        print("{")
182        print("   if (format >= ARRAY_SIZE(util_format_%s_table))" % (func))
183        print("      return NULL;")
184        print()
185        print("   return util_format_%s_table[format];" % (func))
186        print("}")
187        print()
188
189    print('static const struct util_format_description')
190    print('util_format_descriptions[] = {')
191    for format in formats:
192        sn = format.short_name()
193
194        print("   [%s] = {" % (format.name,))
195        print("      %s," % (format.name,))
196        print("      \"%s\"," % (format.name,))
197        print("      \"%s\"," % (sn,))
198        print("      {%u, %u, %u, %u},\t/* block */" % (format.block_width, format.block_height, format.block_depth, format.block_size()))
199        print("      %s," % (layout_map(format.layout),))
200        print("      %u,\t/* nr_channels */" % (format.nr_channels(),))
201        print("      %s,\t/* is_array */" % (bool_map(format.is_array()),))
202        print("      %s,\t/* is_bitmask */" % (bool_map(format.is_bitmask()),))
203        print("      %s,\t/* is_mixed */" % (bool_map(format.is_mixed()),))
204        print("      %s,\t/* is_unorm */" % (bool_map(format.is_unorm()),))
205        print("      %s,\t/* is_snorm */" % (bool_map(format.is_snorm()),))
206        u_format_pack.print_channels(format, do_channel_array)
207        u_format_pack.print_channels(format, do_swizzle_array)
208        print("      %s," % (colorspace_map(format.colorspace),))
209        print("   },")
210        print()
211    print("};")
212    print()
213    generate_table_getter("")
214
215    print('static const struct util_format_pack_description')
216    print('util_format_pack_descriptions[] = {')
217    for format in formats:
218        sn = format.short_name()
219
220        if not has_access(format):
221            print("   [%s] = { 0 }," % (format.name,))
222            continue
223
224        print("   [%s] = {" % (format.name,))
225        if format.colorspace != ZS and not format.is_pure_color():
226            print("      .pack_rgba_8unorm = &util_format_%s_pack_rgba_8unorm," % sn)
227            print("      .pack_rgba_float = &util_format_%s_pack_rgba_float," % sn)
228
229        if format.has_depth():
230            print("      .pack_z_32unorm = &util_format_%s_pack_z_32unorm," % sn)
231            print("      .pack_z_float = &util_format_%s_pack_z_float," % sn)
232
233        if format.has_stencil():
234            print("      .pack_s_8uint = &util_format_%s_pack_s_8uint," % sn)
235
236        if format.is_pure_unsigned() or format.is_pure_signed():
237            print("      .pack_rgba_uint = &util_format_%s_pack_unsigned," % sn)
238            print("      .pack_rgba_sint = &util_format_%s_pack_signed," % sn)
239        print("   },")
240        print()
241    print("};")
242    print()
243    generate_table_getter("pack_")
244
245    print('static const struct util_format_unpack_description')
246    print('util_format_unpack_descriptions[] = {')
247    for format in formats:
248        sn = format.short_name()
249
250        if not has_access(format):
251            print("   [%s] = { 0 }," % (format.name,))
252            continue
253
254        print("   [%s] = {" % (format.name,))
255
256        if format.colorspace != ZS and not format.is_pure_color():
257            print("      .unpack_rgba_8unorm = &util_format_%s_unpack_rgba_8unorm," % sn)
258            if format.layout == 's3tc' or format.layout == 'rgtc':
259                print("      .fetch_rgba_8unorm = &util_format_%s_fetch_rgba_8unorm," % sn)
260            print("      .unpack_rgba = &util_format_%s_unpack_rgba_float," % sn)
261
262        if format.has_depth():
263            print("      .unpack_z_32unorm = &util_format_%s_unpack_z_32unorm," % sn)
264            print("      .unpack_z_float = &util_format_%s_unpack_z_float," % sn)
265
266        if format.has_stencil():
267            print("      .unpack_s_8uint = &util_format_%s_unpack_s_8uint," % sn)
268
269        if format.is_pure_unsigned():
270            print("      .unpack_rgba = &util_format_%s_unpack_unsigned," % sn)
271        elif format.is_pure_signed():
272            print("      .unpack_rgba = &util_format_%s_unpack_signed," % sn)
273        print("   },")
274    print("};")
275    print()
276
277    generate_table_getter("unpack_")
278
279    print('static const util_format_fetch_rgba_func_ptr util_format_fetch_rgba_table[] = {')
280    for format in formats:
281        sn = format.short_name()
282
283        if format.colorspace != ZS and has_access(format):
284            print("  [%s] = &util_format_%s_fetch_rgba," % (format.name, sn))
285        else:
286            print("  [%s] = NULL," % format.name)
287
288    print("};")
289    print()
290
291    generate_function_getter("fetch_rgba")
292
293def main():
294    formats = []
295
296    sys.stdout2 = open(os.devnull, "w")
297
298    for arg in sys.argv[1:]:
299        if arg == '--header':
300            sys.stdout2 = sys.stdout
301            sys.stdout = open(os.devnull, "w")
302            continue
303
304        formats.extend(parse(arg))
305
306    write_format_table(formats)
307
308if __name__ == '__main__':
309    main()
310