• 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'
30
31INTYPES = (GENERATE, UBYTE, USHORT, UINT)
32OUTTYPES = (USHORT, UINT)
33PRIMS=('tris',
34       'trifan',
35       'tristrip',
36       'quads',
37       'quadstrip',
38       'polygon',
39       'trisadj',
40       'tristripadj')
41
42LONGPRIMS=('PIPE_PRIM_TRIANGLES',
43           'PIPE_PRIM_TRIANGLE_FAN',
44           'PIPE_PRIM_TRIANGLE_STRIP',
45           'PIPE_PRIM_QUADS',
46           'PIPE_PRIM_QUAD_STRIP',
47           'PIPE_PRIM_POLYGON',
48           'PIPE_PRIM_TRIANGLES_ADJACENCY',
49           'PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY')
50
51longprim = dict(zip(PRIMS, LONGPRIMS))
52intype_idx = dict(ubyte='IN_UBYTE', ushort='IN_USHORT', uint='IN_UINT')
53outtype_idx = dict(ushort='OUT_USHORT', uint='OUT_UINT')
54
55
56def prolog():
57    print '''/* File automatically generated by u_unfilled_gen.py */'''
58    print copyright
59    print r'''
60
61/**
62 * @file
63 * Functions to translate and generate index lists
64 */
65
66#include "indices/u_indices.h"
67#include "indices/u_indices_priv.h"
68#include "pipe/p_compiler.h"
69#include "util/u_debug.h"
70#include "pipe/p_defines.h"
71#include "util/u_memory.h"
72
73
74static unsigned out_size_idx( unsigned index_size )
75{
76   switch (index_size) {
77   case 4: return OUT_UINT;
78   case 2: return OUT_USHORT;
79   default: assert(0); return OUT_USHORT;
80   }
81}
82
83static unsigned in_size_idx( unsigned index_size )
84{
85   switch (index_size) {
86   case 4: return IN_UINT;
87   case 2: return IN_USHORT;
88   case 1: return IN_UBYTE;
89   default: assert(0); return IN_UBYTE;
90   }
91}
92
93
94static u_generate_func generate_line[OUT_COUNT][PRIM_COUNT];
95static u_translate_func translate_line[IN_COUNT][OUT_COUNT][PRIM_COUNT];
96
97'''
98
99def vert( intype, outtype, v0 ):
100    if intype == GENERATE:
101        return '(' + outtype + ')(' + v0 + ')'
102    else:
103        return '(' + outtype + ')in[' + v0 + ']'
104
105def line( intype, outtype, ptr, v0, v1 ):
106    print '      (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';'
107    print '      (' + ptr + ')[1] = ' + vert( intype, outtype, v1 ) + ';'
108
109# XXX: have the opportunity here to avoid over-drawing shared lines in
110# tristrips, fans, etc, by integrating this into the calling functions
111# and only emitting each line at most once.
112#
113def do_tri( intype, outtype, ptr, v0, v1, v2 ):
114    line( intype, outtype, ptr, v0, v1 )
115    line( intype, outtype, ptr + '+2', v1, v2 )
116    line( intype, outtype, ptr + '+4', v2, v0 )
117
118def do_quad( intype, outtype, ptr, v0, v1, v2, v3 ):
119    line( intype, outtype, ptr, v0, v1 )
120    line( intype, outtype, ptr + '+2', v1, v2 )
121    line( intype, outtype, ptr + '+4', v2, v3 )
122    line( intype, outtype, ptr + '+6', v3, v0 )
123
124def name(intype, outtype, prim):
125    if intype == GENERATE:
126        return 'generate_' + prim + '_' + outtype
127    else:
128        return 'translate_' + prim + '_' + intype + '2' + outtype
129
130def preamble(intype, outtype, prim):
131    print 'static void ' + name( intype, outtype, prim ) + '('
132    if intype != GENERATE:
133        print '    const void * _in,'
134    print '    unsigned start,'
135    if intype != GENERATE:
136        print '    unsigned in_nr,'
137    print '    unsigned out_nr,'
138    if intype != GENERATE:
139        print '    unsigned restart_index,'
140    print '    void *_out )'
141    print '{'
142    if intype != GENERATE:
143        print '  const ' + intype + '*in = (const ' + intype + '*)_in;'
144    print '  ' + outtype + ' *out = (' + outtype + '*)_out;'
145    print '  unsigned i, j;'
146    print '  (void)j;'
147
148def postamble():
149    print '}'
150
151
152def tris(intype, outtype):
153    preamble(intype, outtype, prim='tris')
154    print '  for (i = start, j = 0; j < out_nr; j+=6, i+=3) { '
155    do_tri( intype, outtype, 'out+j',  'i', 'i+1', 'i+2' );
156    print '   }'
157    postamble()
158
159
160def tristrip(intype, outtype):
161    preamble(intype, outtype, prim='tristrip')
162    print '  for (i = start, j = 0; j < out_nr; j+=6, i++) { '
163    do_tri( intype, outtype, 'out+j',  'i', 'i+1/*+(i&1)*/', 'i+2/*-(i&1)*/' );
164    print '   }'
165    postamble()
166
167
168def trifan(intype, outtype):
169    preamble(intype, outtype, prim='trifan')
170    print '  for (i = start, j = 0; j < out_nr; j+=6, i++) { '
171    do_tri( intype, outtype, 'out+j',  '0', 'i+1', 'i+2' );
172    print '   }'
173    postamble()
174
175
176
177def polygon(intype, outtype):
178    preamble(intype, outtype, prim='polygon')
179    print '  for (i = start, j = 0; j < out_nr; j+=2, i++) { '
180    line( intype, outtype, 'out+j', 'i', '(i+1)%(out_nr/2)' )
181    print '   }'
182    postamble()
183
184
185def quads(intype, outtype):
186    preamble(intype, outtype, prim='quads')
187    print '  for (i = start, j = 0; j < out_nr; j+=8, i+=4) { '
188    do_quad( intype, outtype, 'out+j', 'i+0', 'i+1', 'i+2', 'i+3' );
189    print '   }'
190    postamble()
191
192
193def quadstrip(intype, outtype):
194    preamble(intype, outtype, prim='quadstrip')
195    print '  for (i = start, j = 0; j < out_nr; j+=8, i+=2) { '
196    do_quad( intype, outtype, 'out+j', 'i+2', 'i+0', 'i+1', 'i+3' );
197    print '   }'
198    postamble()
199
200
201def trisadj(intype, outtype):
202    preamble(intype, outtype, prim='trisadj')
203    print '  for (i = start, j = 0; j < out_nr; j+=6, i+=6) { '
204    do_tri( intype, outtype, 'out+j',  'i', 'i+2', 'i+4' );
205    print '   }'
206    postamble()
207
208
209def tristripadj(intype, outtype):
210    preamble(intype, outtype, prim='tristripadj')
211    print '  for (i = start, j = 0; j < out_nr; j+=6, i+=2) { '
212    do_tri( intype, outtype, 'out+j',  'i', 'i+2', 'i+4' );
213    print '   }'
214    postamble()
215
216
217def emit_funcs():
218    for intype in INTYPES:
219        for outtype in OUTTYPES:
220            tris(intype, outtype)
221            tristrip(intype, outtype)
222            trifan(intype, outtype)
223            quads(intype, outtype)
224            quadstrip(intype, outtype)
225            polygon(intype, outtype)
226            trisadj(intype, outtype)
227            tristripadj(intype, outtype)
228
229def init(intype, outtype, prim):
230    if intype == GENERATE:
231        print ('generate_line[' +
232               outtype_idx[outtype] +
233               '][' + longprim[prim] +
234               '] = ' + name( intype, outtype, prim ) + ';')
235    else:
236        print ('translate_line[' +
237               intype_idx[intype] +
238               '][' + outtype_idx[outtype] +
239               '][' + longprim[prim] +
240               '] = ' + name( intype, outtype, prim ) + ';')
241
242
243def emit_all_inits():
244    for intype in INTYPES:
245        for outtype in OUTTYPES:
246            for prim in PRIMS:
247                init(intype, outtype, prim)
248
249def emit_init():
250    print 'void u_unfilled_init( void )'
251    print '{'
252    print '  static int firsttime = 1;'
253    print '  if (!firsttime) return;'
254    print '  firsttime = 0;'
255    emit_all_inits()
256    print '}'
257
258
259
260
261def epilog():
262    print '#include "indices/u_unfilled_indices.c"'
263
264
265def main():
266    prolog()
267    emit_funcs()
268    emit_init()
269    epilog()
270
271
272if __name__ == '__main__':
273    main()
274