Home | History | Annotate | Download | only in util
      1 #!/usr/bin/env python
      2 
      3 '''
      4 /**************************************************************************
      5  *
      6  * Copyright 2009-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  * @file
     33  * Pixel format packing and unpacking functions.
     34  *
     35  * @author Jose Fonseca <jfonseca (at] vmware.com>
     36  */
     37 '''
     38 
     39 
     40 from u_format_parse import *
     41 
     42 
     43 def generate_format_type(format):
     44     '''Generate a structure that describes the format.'''
     45 
     46     assert format.layout == PLAIN
     47     
     48     print 'union util_format_%s {' % format.short_name()
     49     
     50     if format.block_size() in (8, 16, 32, 64):
     51         print '   uint%u_t value;' % (format.block_size(),)
     52 
     53     use_bitfields = False
     54     for channel in format.channels:
     55         if channel.size % 8 or not is_pot(channel.size):
     56             use_bitfields = True
     57 
     58     print '   struct {'
     59     for channel in format.channels:
     60         if use_bitfields:
     61             if channel.type == VOID:
     62                 if channel.size:
     63                     print '      unsigned %s:%u;' % (channel.name, channel.size)
     64             elif channel.type == UNSIGNED:
     65                 print '      unsigned %s:%u;' % (channel.name, channel.size)
     66             elif channel.type in (SIGNED, FIXED):
     67                 print '      int %s:%u;' % (channel.name, channel.size)
     68             elif channel.type == FLOAT:
     69                 if channel.size == 64:
     70                     print '      double %s;' % (channel.name)
     71                 elif channel.size == 32:
     72                     print '      float %s;' % (channel.name)
     73                 else:
     74                     print '      unsigned %s:%u;' % (channel.name, channel.size)
     75             else:
     76                 assert 0
     77         else:
     78             assert channel.size % 8 == 0 and is_pot(channel.size)
     79             if channel.type == VOID:
     80                 if channel.size:
     81                     print '      uint%u_t %s;' % (channel.size, channel.name)
     82             elif channel.type == UNSIGNED:
     83                 print '      uint%u_t %s;' % (channel.size, channel.name)
     84             elif channel.type in (SIGNED, FIXED):
     85                 print '      int%u_t %s;' % (channel.size, channel.name)
     86             elif channel.type == FLOAT:
     87                 if channel.size == 64:
     88                     print '      double %s;' % (channel.name)
     89                 elif channel.size == 32:
     90                     print '      float %s;' % (channel.name)
     91                 elif channel.size == 16:
     92                     print '      uint16_t %s;' % (channel.name)
     93                 else:
     94                     assert 0
     95             else:
     96                 assert 0
     97     print '   } chan;'
     98     print '};'
     99     print
    100 
    101 
    102 def bswap_format(format):
    103     '''Generate a structure that describes the format.'''
    104 
    105     if format.is_bitmask() and not format.is_array() and format.block_size() > 8:
    106         print '#ifdef PIPE_ARCH_BIG_ENDIAN'
    107         print '   pixel.value = util_bswap%u(pixel.value);' % format.block_size()
    108         print '#endif'
    109 
    110 
    111 def is_format_supported(format):
    112     '''Determines whether we actually have the plumbing necessary to generate the 
    113     to read/write to/from this format.'''
    114 
    115     # FIXME: Ideally we would support any format combination here.
    116 
    117     if format.layout != PLAIN:
    118         return False
    119 
    120     for i in range(4):
    121         channel = format.channels[i]
    122         if channel.type not in (VOID, UNSIGNED, SIGNED, FLOAT, FIXED):
    123             return False
    124         if channel.type == FLOAT and channel.size not in (16, 32, 64):
    125             return False
    126 
    127     return True
    128 
    129 def is_format_pure_unsigned(format):
    130     for i in range(4):
    131         channel = format.channels[i]
    132         if channel.type not in (VOID, UNSIGNED):
    133             return False
    134         if channel.type == UNSIGNED and channel.pure == False:
    135             return False
    136 
    137     return True
    138 
    139 
    140 def is_format_pure_signed(format):
    141     for i in range(4):
    142         channel = format.channels[i]
    143         if channel.type not in (VOID, SIGNED):
    144             return False
    145         if channel.type == SIGNED and channel.pure == False:
    146             return False
    147 
    148     return True
    149 
    150 def native_type(format):
    151     '''Get the native appropriate for a format.'''
    152 
    153     if format.name == 'PIPE_FORMAT_R11G11B10_FLOAT':
    154         return 'uint32_t'
    155     if format.name == 'PIPE_FORMAT_R9G9B9E5_FLOAT':
    156         return 'uint32_t'
    157 
    158     if format.layout == PLAIN:
    159         if not format.is_array():
    160             # For arithmetic pixel formats return the integer type that matches the whole pixel
    161             return 'uint%u_t' % format.block_size()
    162         else:
    163             # For array pixel formats return the integer type that matches the color channel
    164             channel = format.channels[0]
    165             if channel.type in (UNSIGNED, VOID):
    166                 return 'uint%u_t' % channel.size
    167             elif channel.type in (SIGNED, FIXED):
    168                 return 'int%u_t' % channel.size
    169             elif channel.type == FLOAT:
    170                 if channel.size == 16:
    171                     return 'uint16_t'
    172                 elif channel.size == 32:
    173                     return 'float'
    174                 elif channel.size == 64:
    175                     return 'double'
    176                 else:
    177                     assert False
    178             else:
    179                 assert False
    180     else:
    181         assert False
    182 
    183 
    184 def intermediate_native_type(bits, sign):
    185     '''Find a native type adequate to hold intermediate results of the request bit size.'''
    186 
    187     bytes = 4 # don't use anything smaller than 32bits
    188     while bytes * 8 < bits:
    189         bytes *= 2
    190     bits = bytes*8
    191 
    192     if sign:
    193         return 'int%u_t' % bits
    194     else:
    195         return 'uint%u_t' % bits
    196 
    197 
    198 def get_one_shift(type):
    199     '''Get the number of the bit that matches unity for this type.'''
    200     if type.type == 'FLOAT':
    201         assert False
    202     if not type.norm:
    203         return 0
    204     if type.type == UNSIGNED:
    205         return type.size
    206     if type.type == SIGNED:
    207         return type.size - 1
    208     if type.type == FIXED:
    209         return type.size / 2
    210     assert False
    211 
    212 
    213 def value_to_native(type, value):
    214     '''Get the value of unity for this type.'''
    215     if type.type == FLOAT:
    216         return value
    217     if type.type == FIXED:
    218         return int(value * (1 << (type.size/2)))
    219     if not type.norm:
    220         return int(value)
    221     if type.type == UNSIGNED:
    222         return int(value * ((1 << type.size) - 1))
    223     if type.type == SIGNED:
    224         return int(value * ((1 << (type.size - 1)) - 1))
    225     assert False
    226 
    227 
    228 def native_to_constant(type, value):
    229     '''Get the value of unity for this type.'''
    230     if type.type == FLOAT:
    231         if type.size <= 32:
    232             return "%ff" % value 
    233         else:
    234             return "%ff" % value 
    235     else:
    236         return str(int(value))
    237 
    238 
    239 def get_one(type):
    240     '''Get the value of unity for this type.'''
    241     return value_to_native(type, 1)
    242 
    243 
    244 def clamp_expr(src_channel, dst_channel, dst_native_type, value):
    245     '''Generate the expression to clamp the value in the source type to the
    246     destination type range.'''
    247 
    248     if src_channel == dst_channel:
    249         return value
    250 
    251     src_min = src_channel.min()
    252     src_max = src_channel.max()
    253     dst_min = dst_channel.min()
    254     dst_max = dst_channel.max()
    255     
    256     # Translate the destination range to the src native value
    257     dst_min_native = value_to_native(src_channel, dst_min)
    258     dst_max_native = value_to_native(src_channel, dst_max)
    259 
    260     if src_min < dst_min and src_max > dst_max:
    261         return 'CLAMP(%s, %s, %s)' % (value, dst_min_native, dst_max_native)
    262 
    263     if src_max > dst_max:
    264         return 'MIN2(%s, %s)' % (value, dst_max_native)
    265         
    266     if src_min < dst_min:
    267         return 'MAX2(%s, %s)' % (value, dst_min_native)
    268 
    269     return value
    270 
    271 
    272 def conversion_expr(src_channel, 
    273                     dst_channel, dst_native_type, 
    274                     value, 
    275                     clamp=True, 
    276                     src_colorspace = RGB, 
    277                     dst_colorspace = RGB):
    278     '''Generate the expression to convert a value between two types.'''
    279 
    280     if src_colorspace != dst_colorspace:
    281         if src_colorspace == SRGB:
    282             assert src_channel.type == UNSIGNED
    283             assert src_channel.norm
    284             assert src_channel.size == 8
    285             assert dst_colorspace == RGB
    286             if dst_channel.type == FLOAT:
    287                 return 'util_format_srgb_8unorm_to_linear_float(%s)' % value
    288             else:
    289                 assert dst_channel.type == UNSIGNED
    290                 assert dst_channel.norm
    291                 assert dst_channel.size == 8
    292                 return 'util_format_srgb_to_linear_8unorm(%s)' % value
    293         elif dst_colorspace == SRGB:
    294             assert dst_channel.type == UNSIGNED
    295             assert dst_channel.norm
    296             assert dst_channel.size == 8
    297             assert src_colorspace == RGB
    298             if src_channel.type == FLOAT:
    299                 return 'util_format_linear_float_to_srgb_8unorm(%s)' % value
    300             else:
    301                 assert src_channel.type == UNSIGNED
    302                 assert src_channel.norm
    303                 assert src_channel.size == 8
    304                 return 'util_format_linear_to_srgb_8unorm(%s)' % value
    305         elif src_colorspace == ZS:
    306             pass
    307         elif dst_colorspace == ZS:
    308             pass
    309         else:
    310             assert 0
    311 
    312     if src_channel == dst_channel:
    313         return value
    314 
    315     src_type = src_channel.type
    316     src_size = src_channel.size
    317     src_norm = src_channel.norm
    318     src_pure = src_channel.pure
    319 
    320     # Promote half to float
    321     if src_type == FLOAT and src_size == 16:
    322         value = 'util_half_to_float(%s)' % value
    323         src_size = 32
    324 
    325     # Special case for float <-> ubytes for more accurate results
    326     # Done before clamping since these functions already take care of that
    327     if src_type == UNSIGNED and src_norm and src_size == 8 and dst_channel.type == FLOAT and dst_channel.size == 32:
    328         return 'ubyte_to_float(%s)' % value
    329     if src_type == FLOAT and src_size == 32 and dst_channel.type == UNSIGNED and dst_channel.norm and dst_channel.size == 8:
    330         return 'float_to_ubyte(%s)' % value
    331 
    332     if clamp:
    333         if dst_channel.type != FLOAT or src_type != FLOAT:
    334             value = clamp_expr(src_channel, dst_channel, dst_native_type, value)
    335 
    336     if src_type in (SIGNED, UNSIGNED) and dst_channel.type in (SIGNED, UNSIGNED):
    337         if not src_norm and not dst_channel.norm:
    338             # neither is normalized -- just cast
    339             return '(%s)%s' % (dst_native_type, value)
    340 
    341         src_one = get_one(src_channel)
    342         dst_one = get_one(dst_channel)
    343 
    344         if src_one > dst_one and src_norm and dst_channel.norm:
    345             # We can just bitshift
    346             src_shift = get_one_shift(src_channel)
    347             dst_shift = get_one_shift(dst_channel)
    348             value = '(%s >> %s)' % (value, src_shift - dst_shift)
    349         else:
    350             # We need to rescale using an intermediate type big enough to hold the multiplication of both
    351             tmp_native_type = intermediate_native_type(src_size + dst_channel.size, src_channel.sign and dst_channel.sign)
    352             value = '((%s)%s)' % (tmp_native_type, value)
    353             value = '(%s * 0x%x / 0x%x)' % (value, dst_one, src_one)
    354         value = '(%s)%s' % (dst_native_type, value)
    355         return value
    356 
    357     # Promote to either float or double
    358     if src_type != FLOAT:
    359         if src_norm or src_type == FIXED:
    360             one = get_one(src_channel)
    361             if src_size <= 23:
    362                 value = '(%s * (1.0f/0x%x))' % (value, one)
    363                 if dst_channel.size <= 32:
    364                     value = '(float)%s' % value
    365                 src_size = 32
    366             else:
    367                 # bigger than single precision mantissa, use double
    368                 value = '(%s * (1.0/0x%x))' % (value, one)
    369                 src_size = 64
    370             src_norm = False
    371         else:
    372             if src_size <= 23 or dst_channel.size <= 32:
    373                 value = '(float)%s' % value
    374                 src_size = 32
    375             else:
    376                 # bigger than single precision mantissa, use double
    377                 value = '(double)%s' % value
    378                 src_size = 64
    379         src_type = FLOAT
    380 
    381     # Convert double or float to non-float
    382     if dst_channel.type != FLOAT:
    383         if dst_channel.norm or dst_channel.type == FIXED:
    384             dst_one = get_one(dst_channel)
    385             if dst_channel.size <= 23:
    386                 value = '(%s * 0x%x)' % (value, dst_one)
    387             else:
    388                 # bigger than single precision mantissa, use double
    389                 value = '(%s * (double)0x%x)' % (value, dst_one)
    390         value = '(%s)%s' % (dst_native_type, value)
    391     else:
    392         # Cast double to float when converting to either half or float
    393         if dst_channel.size <= 32 and src_size > 32:
    394             value = '(float)%s' % value
    395             src_size = 32
    396 
    397         if dst_channel.size == 16:
    398             value = 'util_float_to_half(%s)' % value
    399         elif dst_channel.size == 64 and src_size < 64:
    400             value = '(double)%s' % value
    401 
    402     return value
    403 
    404 
    405 def generate_unpack_kernel(format, dst_channel, dst_native_type):
    406 
    407     if not is_format_supported(format):
    408         return
    409     
    410     assert format.layout == PLAIN
    411 
    412     src_native_type = native_type(format)
    413 
    414     if format.is_bitmask():
    415         depth = format.block_size()
    416         print '         uint%u_t value = *(const uint%u_t *)src;' % (depth, depth) 
    417 
    418         # Declare the intermediate variables
    419         for i in range(format.nr_channels()):
    420             src_channel = format.channels[i]
    421             if src_channel.type == UNSIGNED:
    422                 print '         uint%u_t %s;' % (depth, src_channel.name)
    423             elif src_channel.type == SIGNED:
    424                 print '         int%u_t %s;' % (depth, src_channel.name)
    425 
    426         if depth > 8:
    427             print '#ifdef PIPE_ARCH_BIG_ENDIAN'
    428             print '         value = util_bswap%u(value);' % depth
    429             print '#endif'
    430 
    431         # Compute the intermediate unshifted values 
    432         shift = 0
    433         for i in range(format.nr_channels()):
    434             src_channel = format.channels[i]
    435             value = 'value'
    436             if src_channel.type == UNSIGNED:
    437                 if shift:
    438                     value = '%s >> %u' % (value, shift)
    439                 if shift + src_channel.size < depth:
    440                     value = '(%s) & 0x%x' % (value, (1 << src_channel.size) - 1)
    441             elif src_channel.type == SIGNED:
    442                 if shift + src_channel.size < depth:
    443                     # Align the sign bit
    444                     lshift = depth - (shift + src_channel.size)
    445                     value = '%s << %u' % (value, lshift)
    446                 # Cast to signed
    447                 value = '(int%u_t)(%s) ' % (depth, value)
    448                 if src_channel.size < depth:
    449                     # Align the LSB bit
    450                     rshift = depth - src_channel.size
    451                     value = '(%s) >> %u' % (value, rshift)
    452             else:
    453                 value = None
    454                 
    455             if value is not None:
    456                 print '         %s = %s;' % (src_channel.name, value)
    457                 
    458             shift += src_channel.size
    459 
    460         # Convert, swizzle, and store final values
    461         for i in range(4):
    462             swizzle = format.swizzles[i]
    463             if swizzle < 4:
    464                 src_channel = format.channels[swizzle]
    465                 src_colorspace = format.colorspace
    466                 if src_colorspace == SRGB and i == 3:
    467                     # Alpha channel is linear
    468                     src_colorspace = RGB
    469                 value = src_channel.name 
    470                 value = conversion_expr(src_channel, 
    471                                         dst_channel, dst_native_type, 
    472                                         value,
    473                                         src_colorspace = src_colorspace)
    474             elif swizzle == SWIZZLE_0:
    475                 value = '0'
    476             elif swizzle == SWIZZLE_1:
    477                 value = get_one(dst_channel)
    478             elif swizzle == SWIZZLE_NONE:
    479                 value = '0'
    480             else:
    481                 assert False
    482             print '         dst[%u] = %s; /* %s */' % (i, value, 'rgba'[i])
    483         
    484     else:
    485         print '         union util_format_%s pixel;' % format.short_name()
    486         print '         memcpy(&pixel, src, sizeof pixel);'
    487         bswap_format(format)
    488     
    489         for i in range(4):
    490             swizzle = format.swizzles[i]
    491             if swizzle < 4:
    492                 src_channel = format.channels[swizzle]
    493                 src_colorspace = format.colorspace
    494                 if src_colorspace == SRGB and i == 3:
    495                     # Alpha channel is linear
    496                     src_colorspace = RGB
    497                 value = 'pixel.chan.%s' % src_channel.name 
    498                 value = conversion_expr(src_channel, 
    499                                         dst_channel, dst_native_type, 
    500                                         value,
    501                                         src_colorspace = src_colorspace)
    502             elif swizzle == SWIZZLE_0:
    503                 value = '0'
    504             elif swizzle == SWIZZLE_1:
    505                 value = get_one(dst_channel)
    506             elif swizzle == SWIZZLE_NONE:
    507                 value = '0'
    508             else:
    509                 assert False
    510             print '         dst[%u] = %s; /* %s */' % (i, value, 'rgba'[i])
    511     
    512 
    513 def generate_pack_kernel(format, src_channel, src_native_type):
    514 
    515     if not is_format_supported(format):
    516         return
    517     
    518     dst_native_type = native_type(format)
    519 
    520     assert format.layout == PLAIN
    521 
    522     inv_swizzle = format.inv_swizzles()
    523 
    524     if format.is_bitmask():
    525         depth = format.block_size()
    526         print '         uint%u_t value = 0;' % depth 
    527 
    528         shift = 0
    529         for i in range(4):
    530             dst_channel = format.channels[i]
    531             if inv_swizzle[i] is not None:
    532                 value ='src[%u]' % inv_swizzle[i]
    533                 dst_colorspace = format.colorspace
    534                 if dst_colorspace == SRGB and inv_swizzle[i] == 3:
    535                     # Alpha channel is linear
    536                     dst_colorspace = RGB
    537                 value = conversion_expr(src_channel, 
    538                                         dst_channel, dst_native_type, 
    539                                         value,
    540                                         dst_colorspace = dst_colorspace)
    541                 if dst_channel.type in (UNSIGNED, SIGNED):
    542                     if shift + dst_channel.size < depth:
    543                         value = '(%s) & 0x%x' % (value, (1 << dst_channel.size) - 1)
    544                     if shift:
    545                         value = '(%s) << %u' % (value, shift)
    546                     if dst_channel.type == SIGNED:
    547                         # Cast to unsigned
    548                         value = '(uint%u_t)(%s) ' % (depth, value)
    549                 else:
    550                     value = None
    551                 if value is not None:
    552                     print '         value |= %s;' % (value)
    553                 
    554             shift += dst_channel.size
    555 
    556         if depth > 8:
    557             print '#ifdef PIPE_ARCH_BIG_ENDIAN'
    558             print '         value = util_bswap%u(value);' % depth
    559             print '#endif'
    560         
    561         print '         *(uint%u_t *)dst = value;' % depth 
    562 
    563     else:
    564         print '         union util_format_%s pixel;' % format.short_name()
    565     
    566         for i in range(4):
    567             dst_channel = format.channels[i]
    568             width = dst_channel.size
    569             if inv_swizzle[i] is None:
    570                 continue
    571             dst_colorspace = format.colorspace
    572             if dst_colorspace == SRGB and inv_swizzle[i] == 3:
    573                 # Alpha channel is linear
    574                 dst_colorspace = RGB
    575             value ='src[%u]' % inv_swizzle[i]
    576             value = conversion_expr(src_channel, 
    577                                     dst_channel, dst_native_type, 
    578                                     value, 
    579                                     dst_colorspace = dst_colorspace)
    580             print '         pixel.chan.%s = %s;' % (dst_channel.name, value)
    581     
    582         bswap_format(format)
    583         print '         memcpy(dst, &pixel, sizeof pixel);'
    584     
    585 
    586 def generate_format_unpack(format, dst_channel, dst_native_type, dst_suffix):
    587     '''Generate the function to unpack pixels from a particular format'''
    588 
    589     name = format.short_name()
    590 
    591     print 'static INLINE void'
    592     print 'util_format_%s_unpack_%s(%s *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)' % (name, dst_suffix, dst_native_type)
    593     print '{'
    594 
    595     if is_format_supported(format):
    596         print '   unsigned x, y;'
    597         print '   for(y = 0; y < height; y += %u) {' % (format.block_height,)
    598         print '      %s *dst = dst_row;' % (dst_native_type)
    599         print '      const uint8_t *src = src_row;'
    600         print '      for(x = 0; x < width; x += %u) {' % (format.block_width,)
    601         
    602         generate_unpack_kernel(format, dst_channel, dst_native_type)
    603     
    604         print '         src += %u;' % (format.block_size() / 8,)
    605         print '         dst += 4;'
    606         print '      }'
    607         print '      src_row += src_stride;'
    608         print '      dst_row += dst_stride/sizeof(*dst_row);'
    609         print '   }'
    610 
    611     print '}'
    612     print
    613     
    614 
    615 def generate_format_pack(format, src_channel, src_native_type, src_suffix):
    616     '''Generate the function to pack pixels to a particular format'''
    617 
    618     name = format.short_name()
    619 
    620     print 'static INLINE void'
    621     print 'util_format_%s_pack_%s(uint8_t *dst_row, unsigned dst_stride, const %s *src_row, unsigned src_stride, unsigned width, unsigned height)' % (name, src_suffix, src_native_type)
    622     print '{'
    623     
    624     if is_format_supported(format):
    625         print '   unsigned x, y;'
    626         print '   for(y = 0; y < height; y += %u) {' % (format.block_height,)
    627         print '      const %s *src = src_row;' % (src_native_type)
    628         print '      uint8_t *dst = dst_row;'
    629         print '      for(x = 0; x < width; x += %u) {' % (format.block_width,)
    630     
    631         generate_pack_kernel(format, src_channel, src_native_type)
    632             
    633         print '         src += 4;'
    634         print '         dst += %u;' % (format.block_size() / 8,)
    635         print '      }'
    636         print '      dst_row += dst_stride;'
    637         print '      src_row += src_stride/sizeof(*src_row);'
    638         print '   }'
    639         
    640     print '}'
    641     print
    642     
    643 
    644 def generate_format_fetch(format, dst_channel, dst_native_type, dst_suffix):
    645     '''Generate the function to unpack pixels from a particular format'''
    646 
    647     name = format.short_name()
    648 
    649     print 'static INLINE void'
    650     print 'util_format_%s_fetch_%s(%s *dst, const uint8_t *src, unsigned i, unsigned j)' % (name, dst_suffix, dst_native_type)
    651     print '{'
    652 
    653     if is_format_supported(format):
    654         generate_unpack_kernel(format, dst_channel, dst_native_type)
    655 
    656     print '}'
    657     print
    658 
    659 
    660 def is_format_hand_written(format):
    661     return format.layout in ('s3tc', 'rgtc', 'etc', 'subsampled', 'other') or format.colorspace == ZS
    662 
    663 
    664 def generate(formats):
    665     print
    666     print '#include "pipe/p_compiler.h"'
    667     print '#include "u_math.h"'
    668     print '#include "u_half.h"'
    669     print '#include "u_format.h"'
    670     print '#include "u_format_other.h"'
    671     print '#include "u_format_srgb.h"'
    672     print '#include "u_format_yuv.h"'
    673     print '#include "u_format_zs.h"'
    674     print
    675 
    676     for format in formats:
    677         if not is_format_hand_written(format):
    678             
    679             if is_format_supported(format):
    680                 generate_format_type(format)
    681 
    682             if is_format_pure_unsigned(format):
    683                 native_type = 'unsigned'
    684                 suffix = 'unsigned'
    685                 channel = Channel(UNSIGNED, False, True, 32)
    686 
    687                 generate_format_unpack(format, channel, native_type, suffix)
    688                 generate_format_pack(format, channel, native_type, suffix)
    689                 generate_format_fetch(format, channel, native_type, suffix)
    690 
    691                 channel = Channel(SIGNED, False, True, 32)
    692                 native_type = 'int'
    693                 suffix = 'signed'
    694                 generate_format_unpack(format, channel, native_type, suffix)
    695                 generate_format_pack(format, channel, native_type, suffix)   
    696             elif is_format_pure_signed(format):
    697                 native_type = 'int'
    698                 suffix = 'signed'
    699                 channel = Channel(SIGNED, False, True, 32)
    700 
    701                 generate_format_unpack(format, channel, native_type, suffix)
    702                 generate_format_pack(format, channel, native_type, suffix)   
    703                 generate_format_fetch(format, channel, native_type, suffix)
    704 
    705                 native_type = 'unsigned'
    706                 suffix = 'unsigned'
    707                 channel = Channel(UNSIGNED, False, True, 32)
    708                 generate_format_unpack(format, channel, native_type, suffix)
    709                 generate_format_pack(format, channel, native_type, suffix)   
    710             else:
    711                 channel = Channel(FLOAT, False, False, 32)
    712                 native_type = 'float'
    713                 suffix = 'rgba_float'
    714 
    715                 generate_format_unpack(format, channel, native_type, suffix)
    716                 generate_format_pack(format, channel, native_type, suffix)
    717                 generate_format_fetch(format, channel, native_type, suffix)
    718 
    719                 channel = Channel(UNSIGNED, True, False, 8)
    720                 native_type = 'uint8_t'
    721                 suffix = 'rgba_8unorm'
    722 
    723                 generate_format_unpack(format, channel, native_type, suffix)
    724                 generate_format_pack(format, channel, native_type, suffix)
    725 
    726