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