gbm: use the loader util lib
[mesa.git] / src / gallium / auxiliary / indices / u_indices_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 PVS=(FIRST, LAST)
34 PRIMS=('points',
35 'lines',
36 'linestrip',
37 'lineloop',
38 'tris',
39 'trifan',
40 'tristrip',
41 'quads',
42 'quadstrip',
43 'polygon')
44
45 LONGPRIMS=('PIPE_PRIM_POINTS',
46 'PIPE_PRIM_LINES',
47 'PIPE_PRIM_LINE_STRIP',
48 'PIPE_PRIM_LINE_LOOP',
49 'PIPE_PRIM_TRIANGLES',
50 'PIPE_PRIM_TRIANGLE_FAN',
51 'PIPE_PRIM_TRIANGLE_STRIP',
52 'PIPE_PRIM_QUADS',
53 'PIPE_PRIM_QUAD_STRIP',
54 'PIPE_PRIM_POLYGON')
55
56 longprim = dict(zip(PRIMS, LONGPRIMS))
57 intype_idx = dict(ubyte='IN_UBYTE', ushort='IN_USHORT', uint='IN_UINT')
58 outtype_idx = dict(ushort='OUT_USHORT', uint='OUT_UINT')
59 pv_idx = dict(first='PV_FIRST', last='PV_LAST')
60
61
62 def prolog():
63 print '''/* File automatically generated by indices.py */'''
64 print copyright
65 print r'''
66
67 /**
68 * @file
69 * Functions to translate and generate index lists
70 */
71
72 #include "indices/u_indices.h"
73 #include "indices/u_indices_priv.h"
74 #include "pipe/p_compiler.h"
75 #include "util/u_debug.h"
76 #include "pipe/p_defines.h"
77 #include "util/u_memory.h"
78
79
80 static unsigned out_size_idx( unsigned index_size )
81 {
82 switch (index_size) {
83 case 4: return OUT_UINT;
84 case 2: return OUT_USHORT;
85 default: assert(0); return OUT_USHORT;
86 }
87 }
88
89 static unsigned in_size_idx( unsigned index_size )
90 {
91 switch (index_size) {
92 case 4: return IN_UINT;
93 case 2: return IN_USHORT;
94 case 1: return IN_UBYTE;
95 default: assert(0); return IN_UBYTE;
96 }
97 }
98
99
100 static u_translate_func translate[IN_COUNT][OUT_COUNT][PV_COUNT][PV_COUNT][PRIM_COUNT];
101 static u_generate_func generate[OUT_COUNT][PV_COUNT][PV_COUNT][PRIM_COUNT];
102
103
104 '''
105
106 def vert( intype, outtype, v0 ):
107 if intype == GENERATE:
108 return '(' + outtype + ')(' + v0 + ')'
109 else:
110 return '(' + outtype + ')in[' + v0 + ']'
111
112 def point( intype, outtype, ptr, v0 ):
113 print ' (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';'
114
115 def line( intype, outtype, ptr, v0, v1 ):
116 print ' (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';'
117 print ' (' + ptr + ')[1] = ' + vert( intype, outtype, v1 ) + ';'
118
119 def tri( intype, outtype, ptr, v0, v1, v2 ):
120 print ' (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';'
121 print ' (' + ptr + ')[1] = ' + vert( intype, outtype, v1 ) + ';'
122 print ' (' + ptr + ')[2] = ' + vert( intype, outtype, v2 ) + ';'
123
124 def do_point( intype, outtype, ptr, v0 ):
125 point( intype, outtype, ptr, v0 )
126
127 def do_line( intype, outtype, ptr, v0, v1, inpv, outpv ):
128 if inpv == outpv:
129 line( intype, outtype, ptr, v0, v1 )
130 else:
131 line( intype, outtype, ptr, v1, v0 )
132
133 def do_tri( intype, outtype, ptr, v0, v1, v2, inpv, outpv ):
134 if inpv == outpv:
135 tri( intype, outtype, ptr, v0, v1, v2 )
136 else:
137 if inpv == FIRST:
138 tri( intype, outtype, ptr, v1, v2, v0 )
139 else:
140 tri( intype, outtype, ptr, v2, v0, v1 )
141
142 def do_quad( intype, outtype, ptr, v0, v1, v2, v3, inpv, outpv ):
143 do_tri( intype, outtype, ptr+'+0', v0, v1, v3, inpv, outpv );
144 do_tri( intype, outtype, ptr+'+3', v1, v2, v3, inpv, outpv );
145
146 def name(intype, outtype, inpv, outpv, prim):
147 if intype == GENERATE:
148 return 'generate_' + prim + '_' + outtype + '_' + inpv + '2' + outpv
149 else:
150 return 'translate_' + prim + '_' + intype + '2' + outtype + '_' + inpv + '2' + outpv
151
152 def preamble(intype, outtype, inpv, outpv, prim):
153 print 'static void ' + name( intype, outtype, inpv, outpv, prim ) + '('
154 if intype != GENERATE:
155 print ' const void * _in,'
156 print ' unsigned start,'
157 print ' unsigned nr,'
158 print ' void *_out )'
159 print '{'
160 if intype != GENERATE:
161 print ' const ' + intype + '*in = (const ' + intype + '*)_in;'
162 print ' ' + outtype + ' *out = (' + outtype + '*)_out;'
163 print ' unsigned i, j;'
164 print ' (void)j;'
165
166 def postamble():
167 print '}'
168
169
170 def points(intype, outtype, inpv, outpv):
171 preamble(intype, outtype, inpv, outpv, prim='points')
172 print ' for (i = start; i < (nr+start); i++) { '
173 do_point( intype, outtype, 'out+i', 'i' );
174 print ' }'
175 postamble()
176
177 def lines(intype, outtype, inpv, outpv):
178 preamble(intype, outtype, inpv, outpv, prim='lines')
179 print ' for (i = start; i < (nr+start); i+=2) { '
180 do_line( intype, outtype, 'out+i', 'i', 'i+1', inpv, outpv );
181 print ' }'
182 postamble()
183
184 def linestrip(intype, outtype, inpv, outpv):
185 preamble(intype, outtype, inpv, outpv, prim='linestrip')
186 print ' for (i = start, j = 0; j < nr; j+=2, i++) { '
187 do_line( intype, outtype, 'out+j', 'i', 'i+1', inpv, outpv );
188 print ' }'
189 postamble()
190
191 def lineloop(intype, outtype, inpv, outpv):
192 preamble(intype, outtype, inpv, outpv, prim='lineloop')
193 print ' for (i = start, j = 0; j < nr - 2; j+=2, i++) { '
194 do_line( intype, outtype, 'out+j', 'i', 'i+1', inpv, outpv );
195 print ' }'
196 do_line( intype, outtype, 'out+j', 'i', '0', inpv, outpv );
197 postamble()
198
199 def tris(intype, outtype, inpv, outpv):
200 preamble(intype, outtype, inpv, outpv, prim='tris')
201 print ' for (i = start; i < (nr+start); i+=3) { '
202 do_tri( intype, outtype, 'out+i', 'i', 'i+1', 'i+2', inpv, outpv );
203 print ' }'
204 postamble()
205
206
207 def tristrip(intype, outtype, inpv, outpv):
208 preamble(intype, outtype, inpv, outpv, prim='tristrip')
209 print ' for (i = start, j = 0; j < nr; j+=3, i++) { '
210 if inpv == FIRST:
211 do_tri( intype, outtype, 'out+j', 'i', 'i+1+(i&1)', 'i+2-(i&1)', inpv, outpv );
212 else:
213 do_tri( intype, outtype, 'out+j', 'i+(i&1)', 'i+1-(i&1)', 'i+2', inpv, outpv );
214 print ' }'
215 postamble()
216
217
218 def trifan(intype, outtype, inpv, outpv):
219 preamble(intype, outtype, inpv, outpv, prim='trifan')
220 print ' for (i = start, j = 0; j < nr; j+=3, i++) { '
221 do_tri( intype, outtype, 'out+j', '0', 'i+1', 'i+2', inpv, outpv );
222 print ' }'
223 postamble()
224
225
226
227 def polygon(intype, outtype, inpv, outpv):
228 preamble(intype, outtype, inpv, outpv, prim='polygon')
229 print ' for (i = start, j = 0; j < nr; j+=3, i++) { '
230 if inpv == FIRST:
231 do_tri( intype, outtype, 'out+j', '0', 'i+1', 'i+2', inpv, outpv );
232 else:
233 do_tri( intype, outtype, 'out+j', 'i+1', 'i+2', '0', inpv, outpv );
234 print ' }'
235 postamble()
236
237
238 def quads(intype, outtype, inpv, outpv):
239 preamble(intype, outtype, inpv, outpv, prim='quads')
240 print ' for (i = start, j = 0; j < nr; j+=6, i+=4) { '
241 do_quad( intype, outtype, 'out+j', 'i+0', 'i+1', 'i+2', 'i+3', inpv, outpv );
242 print ' }'
243 postamble()
244
245
246 def quadstrip(intype, outtype, inpv, outpv):
247 preamble(intype, outtype, inpv, outpv, prim='quadstrip')
248 print ' for (i = start, j = 0; j < nr; j+=6, i+=2) { '
249 do_quad( intype, outtype, 'out+j', 'i+2', 'i+0', 'i+1', 'i+3', inpv, outpv );
250 print ' }'
251 postamble()
252
253
254 def emit_funcs():
255 for intype in INTYPES:
256 for outtype in OUTTYPES:
257 for inpv in (FIRST, LAST):
258 for outpv in (FIRST, LAST):
259 points(intype, outtype, inpv, outpv)
260 lines(intype, outtype, inpv, outpv)
261 linestrip(intype, outtype, inpv, outpv)
262 lineloop(intype, outtype, inpv, outpv)
263 tris(intype, outtype, inpv, outpv)
264 tristrip(intype, outtype, inpv, outpv)
265 trifan(intype, outtype, inpv, outpv)
266 quads(intype, outtype, inpv, outpv)
267 quadstrip(intype, outtype, inpv, outpv)
268 polygon(intype, outtype, inpv, outpv)
269
270 def init(intype, outtype, inpv, outpv, prim):
271 if intype == GENERATE:
272 print ('generate[' +
273 outtype_idx[outtype] +
274 '][' + pv_idx[inpv] +
275 '][' + pv_idx[outpv] +
276 '][' + longprim[prim] +
277 '] = ' + name( intype, outtype, inpv, outpv, prim ) + ';')
278 else:
279 print ('translate[' +
280 intype_idx[intype] +
281 '][' + outtype_idx[outtype] +
282 '][' + pv_idx[inpv] +
283 '][' + pv_idx[outpv] +
284 '][' + longprim[prim] +
285 '] = ' + name( intype, outtype, inpv, outpv, prim ) + ';')
286
287
288 def emit_all_inits():
289 for intype in INTYPES:
290 for outtype in OUTTYPES:
291 for inpv in PVS:
292 for outpv in PVS:
293 for prim in PRIMS:
294 init(intype, outtype, inpv, outpv, prim)
295
296 def emit_init():
297 print 'void u_index_init( void )'
298 print '{'
299 print ' static int firsttime = 1;'
300 print ' if (!firsttime) return;'
301 print ' firsttime = 0;'
302 emit_all_inits()
303 print '}'
304
305
306
307
308 def epilog():
309 print '#include "indices/u_indices.c"'
310
311
312 def main():
313 prolog()
314 emit_funcs()
315 emit_init()
316 epilog()
317
318
319 if __name__ == '__main__':
320 main()