OSDN Git Service

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