Home | History | Annotate | Download | only in indices
      1 #!/usr/bin/env python
      2 copyright = '''
      3 /*
      4  * Copyright 2009 VMware, Inc.
      5  * All Rights Reserved.
      6  *
      7  * Permission is hereby granted, free of charge, to any person obtaining a
      8  * copy of this software and associated documentation files (the "Software"),
      9  * to deal in the Software without restriction, including without limitation
     10  * on the rights to use, copy, modify, merge, publish, distribute, sub
     11  * license, and/or sell copies of the Software, and to permit persons to whom
     12  * the Software is furnished to do so, subject to the following conditions:
     13  *
     14  * The above copyright notice and this permission notice (including the next
     15  * paragraph) shall be included in all copies or substantial portions of the
     16  * Software.
     17  *
     18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
     21  * VMWARE AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
     22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
     23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
     24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
     25  */
     26 '''
     27 
     28 GENERATE, UBYTE, USHORT, UINT = 'generate', 'ubyte', 'ushort', 'uint'
     29 FIRST, LAST = 'first', 'last'
     30 
     31 INTYPES = (GENERATE, UBYTE, USHORT, UINT)
     32 OUTTYPES = (USHORT, UINT)
     33 PVS=(FIRST, LAST)
     34 PRIMS=('points', 
     35        'lines', 
     36        'linestrip', 
     37        'lineloop', 
     38        'tris', 
     39        'trifan', 
     40        'tristrip', 
     41        'quads', 
     42        'quadstrip', 
     43        'polygon')
     44 
     45 LONGPRIMS=('PIPE_PRIM_POINTS', 
     46            'PIPE_PRIM_LINES', 
     47            'PIPE_PRIM_LINE_STRIP', 
     48            'PIPE_PRIM_LINE_LOOP', 
     49            'PIPE_PRIM_TRIANGLES', 
     50            'PIPE_PRIM_TRIANGLE_FAN', 
     51            'PIPE_PRIM_TRIANGLE_STRIP', 
     52            'PIPE_PRIM_QUADS', 
     53            'PIPE_PRIM_QUAD_STRIP', 
     54            'PIPE_PRIM_POLYGON')
     55 
     56 longprim = dict(zip(PRIMS, LONGPRIMS))
     57 intype_idx = dict(ubyte='IN_UBYTE', ushort='IN_USHORT', uint='IN_UINT')
     58 outtype_idx = dict(ushort='OUT_USHORT', uint='OUT_UINT')
     59 pv_idx = dict(first='PV_FIRST', last='PV_LAST')
     60 
     61 
     62 def prolog():
     63     print '''/* File automatically generated by indices.py */'''
     64     print copyright
     65     print r'''
     66 
     67 /**
     68  * @file
     69  * Functions to translate and generate index lists
     70  */
     71 
     72 #include "indices/u_indices.h"
     73 #include "indices/u_indices_priv.h"
     74 #include "pipe/p_compiler.h"
     75 #include "util/u_debug.h"
     76 #include "pipe/p_defines.h"
     77 #include "util/u_memory.h"
     78 
     79 
     80 static unsigned out_size_idx( unsigned index_size )
     81 {
     82    switch (index_size) {
     83    case 4: return OUT_UINT;
     84    case 2: return OUT_USHORT;
     85    default: assert(0); return OUT_USHORT;
     86    }
     87 }
     88 
     89 static unsigned in_size_idx( unsigned index_size )
     90 {
     91    switch (index_size) {
     92    case 4: return IN_UINT;
     93    case 2: return IN_USHORT;
     94    case 1: return IN_UBYTE;
     95    default: assert(0); return IN_UBYTE;
     96    }
     97 }
     98 
     99 
    100 static u_translate_func translate[IN_COUNT][OUT_COUNT][PV_COUNT][PV_COUNT][PRIM_COUNT];
    101 static u_generate_func  generate[OUT_COUNT][PV_COUNT][PV_COUNT][PRIM_COUNT];
    102 
    103 
    104 '''
    105 
    106 def vert( intype, outtype, v0 ):
    107     if intype == GENERATE:
    108         return '(' + outtype + ')(' + v0 + ')'
    109     else:
    110         return '(' + outtype + ')in[' + v0 + ']'
    111 
    112 def point( intype, outtype, ptr, v0 ):
    113     print '      (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';'
    114 
    115 def line( intype, outtype, ptr, v0, v1 ):
    116     print '      (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';'
    117     print '      (' + ptr + ')[1] = ' + vert( intype, outtype, v1 ) + ';'
    118 
    119 def tri( intype, outtype, ptr, v0, v1, v2 ):
    120     print '      (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';'
    121     print '      (' + ptr + ')[1] = ' + vert( intype, outtype, v1 ) + ';'
    122     print '      (' + ptr + ')[2] = ' + vert( intype, outtype, v2 ) + ';'
    123 
    124 def do_point( intype, outtype, ptr, v0 ):
    125     point( intype, outtype, ptr, v0 )
    126 
    127 def do_line( intype, outtype, ptr, v0, v1, inpv, outpv ):
    128     if inpv == outpv:
    129         line( intype, outtype, ptr, v0, v1 )
    130     else:
    131         line( intype, outtype, ptr, v1, v0 )
    132 
    133 def do_tri( intype, outtype, ptr, v0, v1, v2, inpv, outpv ):
    134     if inpv == outpv:
    135         tri( intype, outtype, ptr, v0, v1, v2 )
    136     else: 
    137         if inpv == FIRST:
    138             tri( intype, outtype, ptr, v1, v2, v0 )
    139         else:
    140             tri( intype, outtype, ptr, v2, v0, v1 )
    141 
    142 def do_quad( intype, outtype, ptr, v0, v1, v2, v3, inpv, outpv ):
    143     do_tri( intype, outtype, ptr+'+0',  v0, v1, v3, inpv, outpv );
    144     do_tri( intype, outtype, ptr+'+3',  v1, v2, v3, inpv, outpv );
    145 
    146 def name(intype, outtype, inpv, outpv, prim):
    147     if intype == GENERATE:
    148         return 'generate_' + prim + '_' + outtype + '_' + inpv + '2' + outpv
    149     else:
    150         return 'translate_' + prim + '_' + intype + '2' + outtype + '_' + inpv + '2' + outpv
    151 
    152 def preamble(intype, outtype, inpv, outpv, prim):
    153     print 'static void ' + name( intype, outtype, inpv, outpv, prim ) + '('
    154     if intype != GENERATE:
    155         print '    const void * _in,'
    156     print '    unsigned nr,'
    157     print '    void *_out )'
    158     print '{'
    159     if intype != GENERATE:
    160         print '  const ' + intype + '*in = (const ' + intype + '*)_in;'
    161     print '  ' + outtype + ' *out = (' + outtype + '*)_out;'
    162     print '  unsigned i, j;'
    163     print '  (void)j;'
    164 
    165 def postamble():
    166     print '}'
    167 
    168 
    169 def points(intype, outtype, inpv, outpv):
    170     preamble(intype, outtype, inpv, outpv, prim='points')
    171     print '  for (i = 0; i < nr; i++) { '
    172     do_point( intype, outtype, 'out+i',  'i' );
    173     print '   }'
    174     postamble()
    175 
    176 def lines(intype, outtype, inpv, outpv):
    177     preamble(intype, outtype, inpv, outpv, prim='lines')
    178     print '  for (i = 0; i < nr; i+=2) { '
    179     do_line( intype, outtype, 'out+i',  'i', 'i+1', inpv, outpv );
    180     print '   }'
    181     postamble()
    182 
    183 def linestrip(intype, outtype, inpv, outpv):
    184     preamble(intype, outtype, inpv, outpv, prim='linestrip')
    185     print '  for (j = i = 0; j < nr; j+=2, i++) { '
    186     do_line( intype, outtype, 'out+j',  'i', 'i+1', inpv, outpv );
    187     print '   }'
    188     postamble()
    189 
    190 def lineloop(intype, outtype, inpv, outpv):
    191     preamble(intype, outtype, inpv, outpv, prim='lineloop')
    192     print '  for (j = i = 0; j < nr - 2; j+=2, i++) { '
    193     do_line( intype, outtype, 'out+j',  'i', 'i+1', inpv, outpv );
    194     print '   }'
    195     do_line( intype, outtype, 'out+j',  'i', '0', inpv, outpv );
    196     postamble()
    197 
    198 def tris(intype, outtype, inpv, outpv):
    199     preamble(intype, outtype, inpv, outpv, prim='tris')
    200     print '  for (i = 0; i < nr; i+=3) { '
    201     do_tri( intype, outtype, 'out+i',  'i', 'i+1', 'i+2', inpv, outpv );
    202     print '   }'
    203     postamble()
    204 
    205 
    206 def tristrip(intype, outtype, inpv, outpv):
    207     preamble(intype, outtype, inpv, outpv, prim='tristrip')
    208     print '  for (j = i = 0; j < nr; j+=3, i++) { '
    209     if inpv == FIRST:
    210         do_tri( intype, outtype, 'out+j',  'i', 'i+1+(i&1)', 'i+2-(i&1)', inpv, outpv );
    211     else:
    212         do_tri( intype, outtype, 'out+j',  'i+(i&1)', 'i+1-(i&1)', 'i+2', inpv, outpv );
    213     print '   }'
    214     postamble()
    215 
    216 
    217 def trifan(intype, outtype, inpv, outpv):
    218     preamble(intype, outtype, inpv, outpv, prim='trifan')
    219     print '  for (j = i = 0; j < nr; j+=3, i++) { '
    220     do_tri( intype, outtype, 'out+j',  '0', 'i+1', 'i+2', inpv, outpv );
    221     print '   }'
    222     postamble()
    223 
    224 
    225 
    226 def polygon(intype, outtype, inpv, outpv):
    227     preamble(intype, outtype, inpv, outpv, prim='polygon')
    228     print '  for (j = i = 0; j < nr; j+=3, i++) { '
    229     if inpv == FIRST:
    230         do_tri( intype, outtype, 'out+j',  '0', 'i+1', 'i+2', inpv, outpv );
    231     else:
    232         do_tri( intype, outtype, 'out+j',  'i+1', 'i+2', '0', inpv, outpv );
    233     print '   }'
    234     postamble()
    235 
    236 
    237 def quads(intype, outtype, inpv, outpv):
    238     preamble(intype, outtype, inpv, outpv, prim='quads')
    239     print '  for (j = i = 0; j < nr; j+=6, i+=4) { '
    240     do_quad( intype, outtype, 'out+j', 'i+0', 'i+1', 'i+2', 'i+3', inpv, outpv );
    241     print '   }'
    242     postamble()
    243 
    244 
    245 def quadstrip(intype, outtype, inpv, outpv):
    246     preamble(intype, outtype, inpv, outpv, prim='quadstrip')
    247     print '  for (j = i = 0; j < nr; j+=6, i+=2) { '
    248     do_quad( intype, outtype, 'out+j', 'i+2', 'i+0', 'i+1', 'i+3', inpv, outpv );
    249     print '   }'
    250     postamble()
    251 
    252 
    253 def emit_funcs():
    254     for intype in INTYPES:
    255         for outtype in OUTTYPES:
    256             for inpv in (FIRST, LAST):
    257                 for outpv in (FIRST, LAST):
    258                     points(intype, outtype, inpv, outpv)
    259                     lines(intype, outtype, inpv, outpv)
    260                     linestrip(intype, outtype, inpv, outpv)
    261                     lineloop(intype, outtype, inpv, outpv)
    262                     tris(intype, outtype, inpv, outpv)
    263                     tristrip(intype, outtype, inpv, outpv)
    264                     trifan(intype, outtype, inpv, outpv)
    265                     quads(intype, outtype, inpv, outpv)
    266                     quadstrip(intype, outtype, inpv, outpv)
    267                     polygon(intype, outtype, inpv, outpv)
    268 
    269 def init(intype, outtype, inpv, outpv, prim):
    270     if intype == GENERATE:
    271         print ('generate[' + 
    272                outtype_idx[outtype] + 
    273                '][' + pv_idx[inpv] + 
    274                '][' + pv_idx[outpv] + 
    275                '][' + longprim[prim] + 
    276                '] = ' + name( intype, outtype, inpv, outpv, prim ) + ';')
    277     else:
    278         print ('translate[' + 
    279                intype_idx[intype] + 
    280                '][' + outtype_idx[outtype] + 
    281                '][' + pv_idx[inpv] + 
    282                '][' + pv_idx[outpv] + 
    283                '][' + longprim[prim] + 
    284                '] = ' + name( intype, outtype, inpv, outpv, prim ) + ';')
    285 
    286 
    287 def emit_all_inits():
    288     for intype in INTYPES:
    289         for outtype in OUTTYPES:
    290             for inpv in PVS:
    291                 for outpv in PVS:
    292                     for prim in PRIMS:
    293                         init(intype, outtype, inpv, outpv, prim)
    294 
    295 def emit_init():
    296     print 'void u_index_init( void )'
    297     print '{'
    298     print '  static int firsttime = 1;'
    299     print '  if (!firsttime) return;'
    300     print '  firsttime = 0;'
    301     emit_all_inits()
    302     print '}'
    303 
    304 
    305     
    306 
    307 def epilog():
    308     print '#include "indices/u_indices.c"'
    309 
    310 
    311 def main():
    312     prolog()
    313     emit_funcs()
    314     emit_init()
    315     epilog()
    316 
    317 
    318 if __name__ == '__main__':
    319     main()
    320