summaryrefslogtreecommitdiffstats
path: root/src/gallium/auxiliary/indices/u_indices_gen.py
blob: 23d8bd782eb46aecb6c6b792e8ca04c2ac219c8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#!/usr/bin/env python
copyright = '''
/*
 * Copyright 2009 VMware, Inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
 * VMWARE AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
'''

GENERATE, UBYTE, USHORT, UINT = 'generate', 'ubyte', 'ushort', 'uint'
FIRST, LAST = 'first', 'last'
PRDISABLE, PRENABLE = 'prdisable', 'prenable'

INTYPES = (GENERATE, UBYTE, USHORT, UINT)
OUTTYPES = (USHORT, UINT)
PVS=(FIRST, LAST)
PRS=(PRDISABLE, PRENABLE)
PRIMS=('points', 
       'lines', 
       'linestrip', 
       'lineloop', 
       'tris', 
       'trifan', 
       'tristrip', 
       'quads', 
       'quadstrip', 
       'polygon',
       'linesadj',
       'linestripadj',
       'trisadj',
       'tristripadj')

LONGPRIMS=('PIPE_PRIM_POINTS', 
           'PIPE_PRIM_LINES', 
           'PIPE_PRIM_LINE_STRIP', 
           'PIPE_PRIM_LINE_LOOP', 
           'PIPE_PRIM_TRIANGLES', 
           'PIPE_PRIM_TRIANGLE_FAN', 
           'PIPE_PRIM_TRIANGLE_STRIP', 
           'PIPE_PRIM_QUADS', 
           'PIPE_PRIM_QUAD_STRIP', 
           'PIPE_PRIM_POLYGON',
           'PIPE_PRIM_LINES_ADJACENCY',
           'PIPE_PRIM_LINE_STRIP_ADJACENCY',
           'PIPE_PRIM_TRIANGLES_ADJACENCY',
           'PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY')

longprim = dict(zip(PRIMS, LONGPRIMS))
intype_idx = dict(ubyte='IN_UBYTE', ushort='IN_USHORT', uint='IN_UINT')
outtype_idx = dict(ushort='OUT_USHORT', uint='OUT_UINT')
pv_idx = dict(first='PV_FIRST', last='PV_LAST')
pr_idx = dict(prdisable='PR_DISABLE', prenable='PR_ENABLE')

def prolog():
    print '''/* File automatically generated by u_indices_gen.py */'''
    print copyright
    print r'''

/**
 * @file
 * Functions to translate and generate index lists
 */

#include "indices/u_indices_priv.h"
#include "util/u_debug.h"
#include "util/u_memory.h"


static unsigned out_size_idx( unsigned index_size )
{
   switch (index_size) {
   case 4: return OUT_UINT;
   case 2: return OUT_USHORT;
   default: assert(0); return OUT_USHORT;
   }
}

static unsigned in_size_idx( unsigned index_size )
{
   switch (index_size) {
   case 4: return IN_UINT;
   case 2: return IN_USHORT;
   case 1: return IN_UBYTE;
   default: assert(0); return IN_UBYTE;
   }
}


static u_translate_func translate[IN_COUNT][OUT_COUNT][PV_COUNT][PV_COUNT][PR_COUNT][PRIM_COUNT];
static u_generate_func  generate[OUT_COUNT][PV_COUNT][PV_COUNT][PRIM_COUNT];


'''

def vert( intype, outtype, v0 ):
    if intype == GENERATE:
        return '(' + outtype + ')(' + v0 + ')'
    else:
        return '(' + outtype + ')in[' + v0 + ']'

def point( intype, outtype, ptr, v0 ):
    print '      (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';'

def line( intype, outtype, ptr, v0, v1 ):
    print '      (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';'
    print '      (' + ptr + ')[1] = ' + vert( intype, outtype, v1 ) + ';'

def tri( intype, outtype, ptr, v0, v1, v2 ):
    print '      (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';'
    print '      (' + ptr + ')[1] = ' + vert( intype, outtype, v1 ) + ';'
    print '      (' + ptr + ')[2] = ' + vert( intype, outtype, v2 ) + ';'

def lineadj( intype, outtype, ptr, v0, v1, v2, v3 ):
    print '      (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';'
    print '      (' + ptr + ')[1] = ' + vert( intype, outtype, v1 ) + ';'
    print '      (' + ptr + ')[2] = ' + vert( intype, outtype, v2 ) + ';'
    print '      (' + ptr + ')[3] = ' + vert( intype, outtype, v3 ) + ';'

def triadj( intype, outtype, ptr, v0, v1, v2, v3, v4, v5 ):
    print '      (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';'
    print '      (' + ptr + ')[1] = ' + vert( intype, outtype, v1 ) + ';'
    print '      (' + ptr + ')[2] = ' + vert( intype, outtype, v2 ) + ';'
    print '      (' + ptr + ')[3] = ' + vert( intype, outtype, v3 ) + ';'
    print '      (' + ptr + ')[4] = ' + vert( intype, outtype, v4 ) + ';'
    print '      (' + ptr + ')[5] = ' + vert( intype, outtype, v5 ) + ';'

def do_point( intype, outtype, ptr, v0 ):
    point( intype, outtype, ptr, v0 )

def do_line( intype, outtype, ptr, v0, v1, inpv, outpv ):
    if inpv == outpv:
        line( intype, outtype, ptr, v0, v1 )
    else:
        line( intype, outtype, ptr, v1, v0 )

def do_tri( intype, outtype, ptr, v0, v1, v2, inpv, outpv ):
    if inpv == outpv:
        tri( intype, outtype, ptr, v0, v1, v2 )
    else: 
        if inpv == FIRST:
            tri( intype, outtype, ptr, v1, v2, v0 )
        else:
            tri( intype, outtype, ptr, v2, v0, v1 )

def do_quad( intype, outtype, ptr, v0, v1, v2, v3, inpv, outpv ):
    if inpv == LAST:
        do_tri( intype, outtype, ptr+'+0',  v0, v1, v3, inpv, outpv );
        do_tri( intype, outtype, ptr+'+3',  v1, v2, v3, inpv, outpv );
    else:
        do_tri( intype, outtype, ptr+'+0',  v0, v1, v2, inpv, outpv );
        do_tri( intype, outtype, ptr+'+3',  v0, v2, v3, inpv, outpv );

def do_lineadj( intype, outtype, ptr, v0, v1, v2, v3, inpv, outpv ):
    if inpv == outpv:
        lineadj( intype, outtype, ptr, v0, v1, v2, v3 )
    else:
        lineadj( intype, outtype, ptr, v3, v2, v1, v0 )

def do_triadj( intype, outtype, ptr, v0, v1, v2, v3, v4, v5, inpv, outpv ):
    if inpv == outpv:
        triadj( intype, outtype, ptr, v0, v1, v2, v3, v4, v5 )
    else:
        triadj( intype, outtype, ptr, v4, v5, v0, v1, v2, v3 )

def name(intype, outtype, inpv, outpv, pr, prim):
    if intype == GENERATE:
        return 'generate_' + prim + '_' + outtype + '_' + inpv + '2' + outpv
    else:
        return 'translate_' + prim + '_' + intype + '2' + outtype + '_' + inpv + '2' + outpv + '_' + pr

def preamble(intype, outtype, inpv, outpv, pr, prim):
    print 'static void ' + name( intype, outtype, inpv, outpv, pr, prim ) + '('
    if intype != GENERATE:
        print '    const void * _in,'
    print '    unsigned start,'
    if intype != GENERATE:
        print '    unsigned in_nr,'
    print '    unsigned out_nr,'
    if intype != GENERATE:
        print '    unsigned restart_index,'
    print '    void *_out )'
    print '{'
    if intype != GENERATE:
        print '  const ' + intype + '*in = (const ' + intype + '*)_in;'
    print '  ' + outtype + ' *out = (' + outtype + '*)_out;'
    print '  unsigned i, j;'
    print '  (void)j;'

def postamble():
    print '}'


def points(intype, outtype, inpv, outpv, pr):
    preamble(intype, outtype, inpv, outpv, pr, prim='points')
    print '  for (i = start; i < (out_nr+start); i++) { '
    do_point( intype, outtype, 'out+i',  'i' );
    print '   }'
    postamble()

def lines(intype, outtype, inpv, outpv, pr):
    preamble(intype, outtype, inpv, outpv, pr, prim='lines')
    print '  for (i = start; i < (out_nr+start); i+=2) { '
    do_line( intype, outtype, 'out+i',  'i', 'i+1', inpv, outpv );
    print '   }'
    postamble()

def linestrip(intype, outtype, inpv, outpv, pr):
    preamble(intype, outtype, inpv, outpv, pr, prim='linestrip')
    print '  for (i = start, j = 0; j < out_nr; j+=2, i++) { '
    do_line( intype, outtype, 'out+j',  'i', 'i+1', inpv, outpv );
    print '   }'
    postamble()

def lineloop(intype, outtype, inpv, outpv, pr):
    preamble(intype, outtype, inpv, outpv, pr, prim='lineloop')
    print '  for (i = start, j = 0; j < out_nr - 2; j+=2, i++) { '
    do_line( intype, outtype, 'out+j',  'i', 'i+1', inpv, outpv );
    print '   }'
    do_line( intype, outtype, 'out+j',  'i', 'start', inpv, outpv );
    postamble()

def tris(intype, outtype, inpv, outpv, pr):
    preamble(intype, outtype, inpv, outpv, pr, prim='tris')
    print '  for (i = start; i < (out_nr+start); i+=3) { '
    do_tri( intype, outtype, 'out+i',  'i', 'i+1', 'i+2', inpv, outpv );
    print '   }'
    postamble()


def tristrip(intype, outtype, inpv, outpv, pr):
    preamble(intype, outtype, inpv, outpv, pr, prim='tristrip')
    print '  for (i = start, j = 0; j < out_nr; j+=3, i++) { '
    if inpv == FIRST:
        do_tri( intype, outtype, 'out+j',  'i', 'i+1+(i&1)', 'i+2-(i&1)', inpv, outpv );
    else:
        do_tri( intype, outtype, 'out+j',  'i+(i&1)', 'i+1-(i&1)', 'i+2', inpv, outpv );
    print '   }'
    postamble()


def trifan(intype, outtype, inpv, outpv, pr):
    preamble(intype, outtype, inpv, outpv, pr, prim='trifan')
    print '  for (i = start, j = 0; j < out_nr; j+=3, i++) { '
    do_tri( intype, outtype, 'out+j',  'start', 'i+1', 'i+2', inpv, outpv );
    print '   }'
    postamble()



def polygon(intype, outtype, inpv, outpv, pr):
    preamble(intype, outtype, inpv, outpv, pr, prim='polygon')
    print '  for (i = start, j = 0; j < out_nr; j+=3, i++) { '
    if pr == PRENABLE:
        print 'restart:'
        print '      if (i + 3 > in_nr) {'
        print '         (out+j+0)[0] = restart_index;'
        print '         (out+j+0)[1] = restart_index;'
        print '         (out+j+0)[2] = restart_index;'
        print '         continue;'
        print '      }'
        print '      if (in[i + 0] == restart_index) {'
        print '         i += 1;'
        print '         start = i;'
        print '         goto restart;'
        print '      }'
        print '      if (in[i + 1] == restart_index) {'
        print '         i += 2;'
        print '         start = i;'
        print '         goto restart;'
        print '      }'
        print '      if (in[i + 2] == restart_index) {'
        print '         i += 3;'
        print '         start = i;'
        print '         goto restart;'
        print '      }'

    if inpv == FIRST:
        do_tri( intype, outtype, 'out+j',  'start', 'i+1', 'i+2', inpv, outpv );
    else:
        do_tri( intype, outtype, 'out+j',  'i+1', 'i+2', 'start', inpv, outpv );
    print '   }'
    postamble()


def quads(intype, outtype, inpv, outpv, pr):
    preamble(intype, outtype, inpv, outpv, pr, prim='quads')
    print '  for (i = start, j = 0; j < out_nr; j+=6, i+=4) { '
    if pr == PRENABLE:
        print 'restart:'
        print '      if (i + 4 > in_nr) {'
        print '         (out+j+0)[0] = restart_index;'
        print '         (out+j+0)[1] = restart_index;'
        print '         (out+j+0)[2] = restart_index;'
        print '         (out+j+3)[0] = restart_index;'
        print '         (out+j+3)[1] = restart_index;'
        print '         (out+j+3)[2] = restart_index;'
        print '         continue;'
        print '      }'
        print '      if (in[i + 0] == restart_index) {'
        print '         i += 1;'
        print '         goto restart;'
        print '      }'
        print '      if (in[i + 1] == restart_index) {'
        print '         i += 2;'
        print '         goto restart;'
        print '      }'
        print '      if (in[i + 2] == restart_index) {'
        print '         i += 3;'
        print '         goto restart;'
        print '      }'
        print '      if (in[i + 3] == restart_index) {'
        print '         i += 4;'
        print '         goto restart;'
        print '      }'

    do_quad( intype, outtype, 'out+j', 'i+0', 'i+1', 'i+2', 'i+3', inpv, outpv );
    print '   }'
    postamble()


def quadstrip(intype, outtype, inpv, outpv, pr):
    preamble(intype, outtype, inpv, outpv, pr, prim='quadstrip')
    print '  for (i = start, j = 0; j < out_nr; j+=6, i+=2) { '
    if pr == PRENABLE:
        print 'restart:'
        print '      if (i + 4 > in_nr) {'
        print '         (out+j+0)[0] = restart_index;'
        print '         (out+j+0)[1] = restart_index;'
        print '         (out+j+0)[2] = restart_index;'
        print '         (out+j+3)[0] = restart_index;'
        print '         (out+j+3)[1] = restart_index;'
        print '         (out+j+3)[2] = restart_index;'
        print '         continue;'
        print '      }'
        print '      if (in[i + 0] == restart_index) {'
        print '         i += 1;'
        print '         goto restart;'
        print '      }'
        print '      if (in[i + 1] == restart_index) {'
        print '         i += 2;'
        print '         goto restart;'
        print '      }'
        print '      if (in[i + 2] == restart_index) {'
        print '         i += 3;'
        print '         goto restart;'
        print '      }'
        print '      if (in[i + 3] == restart_index) {'
        print '         i += 4;'
        print '         goto restart;'
        print '      }'
    if inpv == LAST:
        do_quad( intype, outtype, 'out+j', 'i+2', 'i+0', 'i+1', 'i+3', inpv, outpv );
    else:
        do_quad( intype, outtype, 'out+j', 'i+0', 'i+1', 'i+3', 'i+2', inpv, outpv );
    print '   }'
    postamble()


def linesadj(intype, outtype, inpv, outpv, pr):
    preamble(intype, outtype, inpv, outpv, pr, prim='linesadj')
    print '  for (i = start; i < (out_nr+start); i+=4) { '
    do_lineadj( intype, outtype, 'out+i',  'i+0', 'i+1', 'i+2', 'i+3', inpv, outpv )
    print '  }'
    postamble()


def linestripadj(intype, outtype, inpv, outpv, pr):
    preamble(intype, outtype, inpv, outpv, pr, prim='linestripadj')
    print '  for (i = start, j = 0; j < out_nr; j+=4, i++) {'
    do_lineadj( intype, outtype, 'out+j',  'i+0', 'i+1', 'i+2', 'i+3', inpv, outpv )
    print '  }'
    postamble()


def trisadj(intype, outtype, inpv, outpv, pr):
    preamble(intype, outtype, inpv, outpv, pr, prim='trisadj')
    print '  for (i = start; i < (out_nr+start); i+=6) { '
    do_triadj( intype, outtype, 'out+i',  'i+0', 'i+1', 'i+2', 'i+3',
               'i+4', 'i+5', inpv, outpv )
    print '  }'
    postamble()


def tristripadj(intype, outtype, inpv, outpv, pr):
    preamble(intype, outtype, inpv, outpv, pr, prim='tristripadj')
    print '  for (i = start, j = 0; j < out_nr; i+=2, j+=6) { '
    print '    if (i % 4 == 0) {'
    print '      /* even triangle */'
    do_triadj( intype, outtype, 'out+j',
               'i+0', 'i+1', 'i+2', 'i+3', 'i+4', 'i+5', inpv, outpv )
    print '    } else {'
    print '      /* odd triangle */'
    do_triadj( intype, outtype, 'out+j',
               'i+2', 'i-2', 'i+0', 'i+3', 'i+4', 'i+6', inpv, outpv )
    print '    }'
    print '  }'
    postamble()


def emit_funcs():
    for intype in INTYPES:
        for outtype in OUTTYPES:
            for inpv in (FIRST, LAST):
                for outpv in (FIRST, LAST):
                    for pr in (PRDISABLE, PRENABLE):
                        if pr == PRENABLE and intype == GENERATE:
                            continue
                        points(intype, outtype, inpv, outpv, pr)
                        lines(intype, outtype, inpv, outpv, pr)
                        linestrip(intype, outtype, inpv, outpv, pr)
                        lineloop(intype, outtype, inpv, outpv, pr)
                        tris(intype, outtype, inpv, outpv, pr)
                        tristrip(intype, outtype, inpv, outpv, pr)
                        trifan(intype, outtype, inpv, outpv, pr)
                        quads(intype, outtype, inpv, outpv, pr)
                        quadstrip(intype, outtype, inpv, outpv, pr)
                        polygon(intype, outtype, inpv, outpv, pr)
                        linesadj(intype, outtype, inpv, outpv, pr)
                        linestripadj(intype, outtype, inpv, outpv, pr)
                        trisadj(intype, outtype, inpv, outpv, pr)
                        tristripadj(intype, outtype, inpv, outpv, pr)

def init(intype, outtype, inpv, outpv, pr, prim):
    if intype == GENERATE:
        print ('generate[' + 
               outtype_idx[outtype] + 
               '][' + pv_idx[inpv] + 
               '][' + pv_idx[outpv] + 
               '][' + longprim[prim] + 
               '] = ' + name( intype, outtype, inpv, outpv, pr, prim ) + ';')
    else:
        print ('translate[' + 
               intype_idx[intype] + 
               '][' + outtype_idx[outtype] + 
               '][' + pv_idx[inpv] + 
               '][' + pv_idx[outpv] +
               '][' + pr_idx[pr] + 
               '][' + longprim[prim] + 
               '] = ' + name( intype, outtype, inpv, outpv, pr, prim ) + ';')


def emit_all_inits():
    for intype in INTYPES:
        for outtype in OUTTYPES:
            for inpv in PVS:
                for outpv in PVS:
                    for pr in PRS:
                        for prim in PRIMS:
                            init(intype, outtype, inpv, outpv, pr, prim)

def emit_init():
    print 'void u_index_init( void )'
    print '{'
    print '  static int firsttime = 1;'
    print '  if (!firsttime) return;'
    print '  firsttime = 0;'
    emit_all_inits()
    print '}'


    

def epilog():
    print '#include "indices/u_indices.c"'


def main():
    prolog()
    emit_funcs()
    emit_init()
    epilog()


if __name__ == '__main__':
    main()