Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  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. PRDISABLE, PRENABLE = 'prdisable', 'prenable'
  31.  
  32. INTYPES = (GENERATE, UBYTE, USHORT, UINT)
  33. OUTTYPES = (USHORT, UINT)
  34. PVS=(FIRST, LAST)
  35. PRS=(PRDISABLE, PRENABLE)
  36. PRIMS=('points',
  37.        'lines',
  38.        'linestrip',
  39.        'lineloop',
  40.        'tris',
  41.        'trifan',
  42.        'tristrip',
  43.        'quads',
  44.        'quadstrip',
  45.        'polygon')
  46.  
  47. LONGPRIMS=('PIPE_PRIM_POINTS',
  48.            'PIPE_PRIM_LINES',
  49.            'PIPE_PRIM_LINE_STRIP',
  50.            'PIPE_PRIM_LINE_LOOP',
  51.            'PIPE_PRIM_TRIANGLES',
  52.            'PIPE_PRIM_TRIANGLE_FAN',
  53.            'PIPE_PRIM_TRIANGLE_STRIP',
  54.            'PIPE_PRIM_QUADS',
  55.            'PIPE_PRIM_QUAD_STRIP',
  56.            'PIPE_PRIM_POLYGON')
  57.  
  58. longprim = dict(zip(PRIMS, LONGPRIMS))
  59. intype_idx = dict(ubyte='IN_UBYTE', ushort='IN_USHORT', uint='IN_UINT')
  60. outtype_idx = dict(ushort='OUT_USHORT', uint='OUT_UINT')
  61. pv_idx = dict(first='PV_FIRST', last='PV_LAST')
  62. pr_idx = dict(prdisable='PR_DISABLE', prenable='PR_ENABLE')
  63.  
  64. def prolog():
  65.     print '''/* File automatically generated by indices.py */'''
  66.     print copyright
  67.     print r'''
  68.  
  69. /**
  70. * @file
  71. * Functions to translate and generate index lists
  72. */
  73.  
  74. #include "indices/u_indices.h"
  75. #include "indices/u_indices_priv.h"
  76. #include "pipe/p_compiler.h"
  77. #include "util/u_debug.h"
  78. #include "pipe/p_defines.h"
  79. #include "util/u_memory.h"
  80.  
  81.  
  82. static unsigned out_size_idx( unsigned index_size )
  83. {
  84.   switch (index_size) {
  85.   case 4: return OUT_UINT;
  86.   case 2: return OUT_USHORT;
  87.   default: assert(0); return OUT_USHORT;
  88.   }
  89. }
  90.  
  91. static unsigned in_size_idx( unsigned index_size )
  92. {
  93.   switch (index_size) {
  94.   case 4: return IN_UINT;
  95.   case 2: return IN_USHORT;
  96.   case 1: return IN_UBYTE;
  97.   default: assert(0); return IN_UBYTE;
  98.   }
  99. }
  100.  
  101.  
  102. static u_translate_func translate[IN_COUNT][OUT_COUNT][PV_COUNT][PV_COUNT][PR_COUNT][PRIM_COUNT];
  103. static u_generate_func  generate[OUT_COUNT][PV_COUNT][PV_COUNT][PRIM_COUNT];
  104.  
  105.  
  106. '''
  107.  
  108. def vert( intype, outtype, v0 ):
  109.     if intype == GENERATE:
  110.         return '(' + outtype + ')(' + v0 + ')'
  111.     else:
  112.         return '(' + outtype + ')in[' + v0 + ']'
  113.  
  114. def point( intype, outtype, ptr, v0 ):
  115.     print '      (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';'
  116.  
  117. def line( intype, outtype, ptr, v0, v1 ):
  118.     print '      (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';'
  119.     print '      (' + ptr + ')[1] = ' + vert( intype, outtype, v1 ) + ';'
  120.  
  121. def tri( intype, outtype, ptr, v0, v1, v2 ):
  122.     print '      (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';'
  123.     print '      (' + ptr + ')[1] = ' + vert( intype, outtype, v1 ) + ';'
  124.     print '      (' + ptr + ')[2] = ' + vert( intype, outtype, v2 ) + ';'
  125.  
  126. def do_point( intype, outtype, ptr, v0 ):
  127.     point( intype, outtype, ptr, v0 )
  128.  
  129. def do_line( intype, outtype, ptr, v0, v1, inpv, outpv ):
  130.     if inpv == outpv:
  131.         line( intype, outtype, ptr, v0, v1 )
  132.     else:
  133.         line( intype, outtype, ptr, v1, v0 )
  134.  
  135. def do_tri( intype, outtype, ptr, v0, v1, v2, inpv, outpv ):
  136.     if inpv == outpv:
  137.         tri( intype, outtype, ptr, v0, v1, v2 )
  138.     else:
  139.         if inpv == FIRST:
  140.             tri( intype, outtype, ptr, v1, v2, v0 )
  141.         else:
  142.             tri( intype, outtype, ptr, v2, v0, v1 )
  143.  
  144. def do_quad( intype, outtype, ptr, v0, v1, v2, v3, inpv, outpv ):
  145.     if inpv == LAST:
  146.         do_tri( intype, outtype, ptr+'+0',  v0, v1, v3, inpv, outpv );
  147.         do_tri( intype, outtype, ptr+'+3',  v1, v2, v3, inpv, outpv );
  148.     else:
  149.         do_tri( intype, outtype, ptr+'+0',  v0, v1, v2, inpv, outpv );
  150.         do_tri( intype, outtype, ptr+'+3',  v0, v2, v3, inpv, outpv );
  151.  
  152. def name(intype, outtype, inpv, outpv, pr, prim):
  153.     if intype == GENERATE:
  154.         return 'generate_' + prim + '_' + outtype + '_' + inpv + '2' + outpv
  155.     else:
  156.         return 'translate_' + prim + '_' + intype + '2' + outtype + '_' + inpv + '2' + outpv + '_' + pr
  157.  
  158. def preamble(intype, outtype, inpv, outpv, pr, prim):
  159.     print 'static void ' + name( intype, outtype, inpv, outpv, pr, prim ) + '('
  160.     if intype != GENERATE:
  161.         print '    const void * _in,'
  162.     print '    unsigned start,'
  163.     if intype != GENERATE:
  164.         print '    unsigned in_nr,'
  165.     print '    unsigned out_nr,'
  166.     if intype != GENERATE:
  167.         print '    unsigned restart_index,'
  168.     print '    void *_out )'
  169.     print '{'
  170.     if intype != GENERATE:
  171.         print '  const ' + intype + '*in = (const ' + intype + '*)_in;'
  172.     print '  ' + outtype + ' *out = (' + outtype + '*)_out;'
  173.     print '  unsigned i, j;'
  174.     print '  (void)j;'
  175.  
  176. def postamble():
  177.     print '}'
  178.  
  179.  
  180. def points(intype, outtype, inpv, outpv, pr):
  181.     preamble(intype, outtype, inpv, outpv, pr, prim='points')
  182.     print '  for (i = start; i < (out_nr+start); i++) { '
  183.     do_point( intype, outtype, 'out+i',  'i' );
  184.     print '   }'
  185.     postamble()
  186.  
  187. def lines(intype, outtype, inpv, outpv, pr):
  188.     preamble(intype, outtype, inpv, outpv, pr, prim='lines')
  189.     print '  for (i = start; i < (out_nr+start); i+=2) { '
  190.     do_line( intype, outtype, 'out+i',  'i', 'i+1', inpv, outpv );
  191.     print '   }'
  192.     postamble()
  193.  
  194. def linestrip(intype, outtype, inpv, outpv, pr):
  195.     preamble(intype, outtype, inpv, outpv, pr, prim='linestrip')
  196.     print '  for (i = start, j = 0; j < out_nr; j+=2, i++) { '
  197.     do_line( intype, outtype, 'out+j',  'i', 'i+1', inpv, outpv );
  198.     print '   }'
  199.     postamble()
  200.  
  201. def lineloop(intype, outtype, inpv, outpv, pr):
  202.     preamble(intype, outtype, inpv, outpv, pr, prim='lineloop')
  203.     print '  for (i = start, j = 0; j < out_nr - 2; j+=2, i++) { '
  204.     do_line( intype, outtype, 'out+j',  'i', 'i+1', inpv, outpv );
  205.     print '   }'
  206.     do_line( intype, outtype, 'out+j',  'i', 'start', inpv, outpv );
  207.     postamble()
  208.  
  209. def tris(intype, outtype, inpv, outpv, pr):
  210.     preamble(intype, outtype, inpv, outpv, pr, prim='tris')
  211.     print '  for (i = start; i < (out_nr+start); i+=3) { '
  212.     do_tri( intype, outtype, 'out+i',  'i', 'i+1', 'i+2', inpv, outpv );
  213.     print '   }'
  214.     postamble()
  215.  
  216.  
  217. def tristrip(intype, outtype, inpv, outpv, pr):
  218.     preamble(intype, outtype, inpv, outpv, pr, prim='tristrip')
  219.     print '  for (i = start, j = 0; j < out_nr; j+=3, i++) { '
  220.     if inpv == FIRST:
  221.         do_tri( intype, outtype, 'out+j',  'i', 'i+1+(i&1)', 'i+2-(i&1)', inpv, outpv );
  222.     else:
  223.         do_tri( intype, outtype, 'out+j',  'i+(i&1)', 'i+1-(i&1)', 'i+2', inpv, outpv );
  224.     print '   }'
  225.     postamble()
  226.  
  227.  
  228. def trifan(intype, outtype, inpv, outpv, pr):
  229.     preamble(intype, outtype, inpv, outpv, pr, prim='trifan')
  230.     print '  for (i = start, j = 0; j < out_nr; j+=3, i++) { '
  231.     do_tri( intype, outtype, 'out+j',  'start', 'i+1', 'i+2', inpv, outpv );
  232.     print '   }'
  233.     postamble()
  234.  
  235.  
  236.  
  237. def polygon(intype, outtype, inpv, outpv, pr):
  238.     preamble(intype, outtype, inpv, outpv, pr, prim='polygon')
  239.     print '  for (i = start, j = 0; j < out_nr; j+=3, i++) { '
  240.     if pr == PRENABLE:
  241.         print 'restart:'
  242.         print '      if (i + 3 > in_nr) {'
  243.         print '         (out+j+0)[0] = restart_index;'
  244.         print '         (out+j+0)[1] = restart_index;'
  245.         print '         (out+j+0)[2] = restart_index;'
  246.         print '         continue;'
  247.         print '      }'
  248.         print '      if (in[i + 0] == restart_index) {'
  249.         print '         i += 1;'
  250.         print '         start = i;'
  251.         print '         goto restart;'
  252.         print '      }'
  253.         print '      if (in[i + 1] == restart_index) {'
  254.         print '         i += 2;'
  255.         print '         start = i;'
  256.         print '         goto restart;'
  257.         print '      }'
  258.         print '      if (in[i + 2] == restart_index) {'
  259.         print '         i += 3;'
  260.         print '         start = i;'
  261.         print '         goto restart;'
  262.         print '      }'
  263.  
  264.     if inpv == FIRST:
  265.         do_tri( intype, outtype, 'out+j',  'start', 'i+1', 'i+2', inpv, outpv );
  266.     else:
  267.         do_tri( intype, outtype, 'out+j',  'i+1', 'i+2', 'start', inpv, outpv );
  268.     print '   }'
  269.     postamble()
  270.  
  271.  
  272. def quads(intype, outtype, inpv, outpv, pr):
  273.     preamble(intype, outtype, inpv, outpv, pr, prim='quads')
  274.     print '  for (i = start, j = 0; j < out_nr; j+=6, i+=4) { '
  275.     if pr == PRENABLE:
  276.         print 'restart:'
  277.         print '      if (i + 4 > in_nr) {'
  278.         print '         (out+j+0)[0] = restart_index;'
  279.         print '         (out+j+0)[1] = restart_index;'
  280.         print '         (out+j+0)[2] = restart_index;'
  281.         print '         (out+j+3)[0] = restart_index;'
  282.         print '         (out+j+3)[1] = restart_index;'
  283.         print '         (out+j+3)[2] = restart_index;'
  284.         print '         continue;'
  285.         print '      }'
  286.         print '      if (in[i + 0] == restart_index) {'
  287.         print '         i += 1;'
  288.         print '         goto restart;'
  289.         print '      }'
  290.         print '      if (in[i + 1] == restart_index) {'
  291.         print '         i += 2;'
  292.         print '         goto restart;'
  293.         print '      }'
  294.         print '      if (in[i + 2] == restart_index) {'
  295.         print '         i += 3;'
  296.         print '         goto restart;'
  297.         print '      }'
  298.         print '      if (in[i + 3] == restart_index) {'
  299.         print '         i += 4;'
  300.         print '         goto restart;'
  301.         print '      }'
  302.  
  303.     do_quad( intype, outtype, 'out+j', 'i+0', 'i+1', 'i+2', 'i+3', inpv, outpv );
  304.     print '   }'
  305.     postamble()
  306.  
  307.  
  308. def quadstrip(intype, outtype, inpv, outpv, pr):
  309.     preamble(intype, outtype, inpv, outpv, pr, prim='quadstrip')
  310.     print '  for (i = start, j = 0; j < out_nr; j+=6, i+=2) { '
  311.     if pr == PRENABLE:
  312.         print 'restart:'
  313.         print '      if (i + 4 > in_nr) {'
  314.         print '         (out+j+0)[0] = restart_index;'
  315.         print '         (out+j+0)[1] = restart_index;'
  316.         print '         (out+j+0)[2] = restart_index;'
  317.         print '         (out+j+3)[0] = restart_index;'
  318.         print '         (out+j+3)[1] = restart_index;'
  319.         print '         (out+j+3)[2] = restart_index;'
  320.         print '         continue;'
  321.         print '      }'
  322.         print '      if (in[i + 0] == restart_index) {'
  323.         print '         i += 1;'
  324.         print '         goto restart;'
  325.         print '      }'
  326.         print '      if (in[i + 1] == restart_index) {'
  327.         print '         i += 2;'
  328.         print '         goto restart;'
  329.         print '      }'
  330.         print '      if (in[i + 2] == restart_index) {'
  331.         print '         i += 3;'
  332.         print '         goto restart;'
  333.         print '      }'
  334.         print '      if (in[i + 3] == restart_index) {'
  335.         print '         i += 4;'
  336.         print '         goto restart;'
  337.         print '      }'
  338.     if inpv == LAST:
  339.         do_quad( intype, outtype, 'out+j', 'i+2', 'i+0', 'i+1', 'i+3', inpv, outpv );
  340.     else:
  341.         do_quad( intype, outtype, 'out+j', 'i+0', 'i+1', 'i+3', 'i+2', inpv, outpv );
  342.     print '   }'
  343.     postamble()
  344.  
  345.  
  346. def emit_funcs():
  347.     for intype in INTYPES:
  348.         for outtype in OUTTYPES:
  349.             for inpv in (FIRST, LAST):
  350.                 for outpv in (FIRST, LAST):
  351.                     for pr in (PRDISABLE, PRENABLE):
  352.                         if pr == PRENABLE and intype == GENERATE:
  353.                             continue
  354.                         points(intype, outtype, inpv, outpv, pr)
  355.                         lines(intype, outtype, inpv, outpv, pr)
  356.                         linestrip(intype, outtype, inpv, outpv, pr)
  357.                         lineloop(intype, outtype, inpv, outpv, pr)
  358.                         tris(intype, outtype, inpv, outpv, pr)
  359.                         tristrip(intype, outtype, inpv, outpv, pr)
  360.                         trifan(intype, outtype, inpv, outpv, pr)
  361.                         quads(intype, outtype, inpv, outpv, pr)
  362.                         quadstrip(intype, outtype, inpv, outpv, pr)
  363.                         polygon(intype, outtype, inpv, outpv, pr)
  364.  
  365. def init(intype, outtype, inpv, outpv, pr, prim):
  366.     if intype == GENERATE:
  367.         print ('generate[' +
  368.                outtype_idx[outtype] +
  369.                '][' + pv_idx[inpv] +
  370.                '][' + pv_idx[outpv] +
  371.                '][' + longprim[prim] +
  372.                '] = ' + name( intype, outtype, inpv, outpv, pr, prim ) + ';')
  373.     else:
  374.         print ('translate[' +
  375.                intype_idx[intype] +
  376.                '][' + outtype_idx[outtype] +
  377.                '][' + pv_idx[inpv] +
  378.                '][' + pv_idx[outpv] +
  379.                '][' + pr_idx[pr] +
  380.                '][' + longprim[prim] +
  381.                '] = ' + name( intype, outtype, inpv, outpv, pr, prim ) + ';')
  382.  
  383.  
  384. def emit_all_inits():
  385.     for intype in INTYPES:
  386.         for outtype in OUTTYPES:
  387.             for inpv in PVS:
  388.                 for outpv in PVS:
  389.                     for pr in PRS:
  390.                         for prim in PRIMS:
  391.                             init(intype, outtype, inpv, outpv, pr, prim)
  392.  
  393. def emit_init():
  394.     print 'void u_index_init( void )'
  395.     print '{'
  396.     print '  static int firsttime = 1;'
  397.     print '  if (!firsttime) return;'
  398.     print '  firsttime = 0;'
  399.     emit_all_inits()
  400.     print '}'
  401.  
  402.  
  403.    
  404.  
  405. def epilog():
  406.     print '#include "indices/u_indices.c"'
  407.  
  408.  
  409. def main():
  410.     prolog()
  411.     emit_funcs()
  412.     emit_init()
  413.     epilog()
  414.  
  415.  
  416. if __name__ == '__main__':
  417.     main()
  418.