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