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