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