376348d5f8e3efb8799e84c2d5fa90361b3207ac
3 * Copyright 2009 VMware, Inc.
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:
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
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.
27 GENERATE
, UBYTE
, USHORT
, UINT
= 'generate', 'ubyte', 'ushort', 'uint'
28 FIRST
, LAST
= 'first', 'last'
29 PRDISABLE
, PRENABLE
= 'prdisable', 'prenable'
31 INTYPES
= (GENERATE
, UBYTE
, USHORT
, UINT
)
32 OUTTYPES
= (USHORT
, UINT
)
34 PRS
=(PRDISABLE
, PRENABLE
)
50 LONGPRIMS
=('PIPE_PRIM_POINTS',
52 'PIPE_PRIM_LINE_STRIP',
53 'PIPE_PRIM_LINE_LOOP',
54 'PIPE_PRIM_TRIANGLES',
55 'PIPE_PRIM_TRIANGLE_FAN',
56 'PIPE_PRIM_TRIANGLE_STRIP',
58 'PIPE_PRIM_QUAD_STRIP',
60 'PIPE_PRIM_LINES_ADJACENCY',
61 'PIPE_PRIM_LINE_STRIP_ADJACENCY',
62 'PIPE_PRIM_TRIANGLES_ADJACENCY',
63 'PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY')
65 longprim
= dict(zip(PRIMS
, LONGPRIMS
))
66 intype_idx
= dict(ubyte
='IN_UBYTE', ushort
='IN_USHORT', uint
='IN_UINT')
67 outtype_idx
= dict(ushort
='OUT_USHORT', uint
='OUT_UINT')
68 pv_idx
= dict(first
='PV_FIRST', last
='PV_LAST')
69 pr_idx
= dict(prdisable
='PR_DISABLE', prenable
='PR_ENABLE')
72 print '''/* File automatically generated by u_indices_gen.py */'''
78 * Functions to translate and generate index lists
81 #include "indices/u_indices_priv.h"
82 #include "util/u_debug.h"
83 #include "util/u_memory.h"
86 static unsigned out_size_idx( unsigned index_size )
89 case 4: return OUT_UINT;
90 case 2: return OUT_USHORT;
91 default: assert(0); return OUT_USHORT;
95 static unsigned in_size_idx( unsigned index_size )
98 case 4: return IN_UINT;
99 case 2: return IN_USHORT;
100 case 1: return IN_UBYTE;
101 default: assert(0); return IN_UBYTE;
106 static u_translate_func translate[IN_COUNT][OUT_COUNT][PV_COUNT][PV_COUNT][PR_COUNT][PRIM_COUNT];
107 static u_generate_func generate[OUT_COUNT][PV_COUNT][PV_COUNT][PRIM_COUNT];
112 def vert( intype
, outtype
, v0
):
113 if intype
== GENERATE
:
114 return '(' + outtype
+ ')(' + v0
+ ')'
116 return '(' + outtype
+ ')in[' + v0
+ ']'
118 def point( intype
, outtype
, ptr
, v0
):
119 print ' (' + ptr
+ ')[0] = ' + vert( intype
, outtype
, v0
) + ';'
121 def line( intype
, outtype
, ptr
, v0
, v1
):
122 print ' (' + ptr
+ ')[0] = ' + vert( intype
, outtype
, v0
) + ';'
123 print ' (' + ptr
+ ')[1] = ' + vert( intype
, outtype
, v1
) + ';'
125 def tri( intype
, outtype
, ptr
, v0
, v1
, v2
):
126 print ' (' + ptr
+ ')[0] = ' + vert( intype
, outtype
, v0
) + ';'
127 print ' (' + ptr
+ ')[1] = ' + vert( intype
, outtype
, v1
) + ';'
128 print ' (' + ptr
+ ')[2] = ' + vert( intype
, outtype
, v2
) + ';'
130 def lineadj( intype
, outtype
, ptr
, v0
, v1
, v2
, v3
):
131 print ' (' + ptr
+ ')[0] = ' + vert( intype
, outtype
, v0
) + ';'
132 print ' (' + ptr
+ ')[1] = ' + vert( intype
, outtype
, v1
) + ';'
133 print ' (' + ptr
+ ')[2] = ' + vert( intype
, outtype
, v2
) + ';'
134 print ' (' + ptr
+ ')[3] = ' + vert( intype
, outtype
, v3
) + ';'
136 def triadj( intype
, outtype
, ptr
, v0
, v1
, v2
, v3
, v4
, v5
):
137 print ' (' + ptr
+ ')[0] = ' + vert( intype
, outtype
, v0
) + ';'
138 print ' (' + ptr
+ ')[1] = ' + vert( intype
, outtype
, v1
) + ';'
139 print ' (' + ptr
+ ')[2] = ' + vert( intype
, outtype
, v2
) + ';'
140 print ' (' + ptr
+ ')[3] = ' + vert( intype
, outtype
, v3
) + ';'
141 print ' (' + ptr
+ ')[4] = ' + vert( intype
, outtype
, v4
) + ';'
142 print ' (' + ptr
+ ')[5] = ' + vert( intype
, outtype
, v5
) + ';'
144 def do_point( intype
, outtype
, ptr
, v0
):
145 point( intype
, outtype
, ptr
, v0
)
147 def do_line( intype
, outtype
, ptr
, v0
, v1
, inpv
, outpv
):
149 line( intype
, outtype
, ptr
, v0
, v1
)
151 line( intype
, outtype
, ptr
, v1
, v0
)
153 def do_tri( intype
, outtype
, ptr
, v0
, v1
, v2
, inpv
, outpv
):
155 tri( intype
, outtype
, ptr
, v0
, v1
, v2
)
158 tri( intype
, outtype
, ptr
, v1
, v2
, v0
)
160 tri( intype
, outtype
, ptr
, v2
, v0
, v1
)
162 def do_quad( intype
, outtype
, ptr
, v0
, v1
, v2
, v3
, inpv
, outpv
):
164 do_tri( intype
, outtype
, ptr
+'+0', v0
, v1
, v3
, inpv
, outpv
);
165 do_tri( intype
, outtype
, ptr
+'+3', v1
, v2
, v3
, inpv
, outpv
);
167 do_tri( intype
, outtype
, ptr
+'+0', v0
, v1
, v2
, inpv
, outpv
);
168 do_tri( intype
, outtype
, ptr
+'+3', v0
, v2
, v3
, inpv
, outpv
);
170 def do_lineadj( intype
, outtype
, ptr
, v0
, v1
, v2
, v3
, inpv
, outpv
):
172 lineadj( intype
, outtype
, ptr
, v0
, v1
, v2
, v3
)
174 lineadj( intype
, outtype
, ptr
, v3
, v2
, v1
, v0
)
176 def do_triadj( intype
, outtype
, ptr
, v0
, v1
, v2
, v3
, v4
, v5
, inpv
, outpv
):
178 triadj( intype
, outtype
, ptr
, v0
, v1
, v2
, v3
, v4
, v5
)
180 triadj( intype
, outtype
, ptr
, v4
, v5
, v0
, v1
, v2
, v3
)
182 def name(intype
, outtype
, inpv
, outpv
, pr
, prim
):
183 if intype
== GENERATE
:
184 return 'generate_' + prim
+ '_' + outtype
+ '_' + inpv
+ '2' + outpv
186 return 'translate_' + prim
+ '_' + intype
+ '2' + outtype
+ '_' + inpv
+ '2' + outpv
+ '_' + pr
188 def preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
):
189 print 'static void ' + name( intype
, outtype
, inpv
, outpv
, pr
, prim
) + '('
190 if intype
!= GENERATE
:
191 print ' const void * _in,'
192 print ' unsigned start,'
193 if intype
!= GENERATE
:
194 print ' unsigned in_nr,'
195 print ' unsigned out_nr,'
196 if intype
!= GENERATE
:
197 print ' unsigned restart_index,'
198 print ' void *_out )'
200 if intype
!= GENERATE
:
201 print ' const ' + intype
+ '*in = (const ' + intype
+ '*)_in;'
202 print ' ' + outtype
+ ' *out = (' + outtype
+ '*)_out;'
203 print ' unsigned i, j;'
210 def points(intype
, outtype
, inpv
, outpv
, pr
):
211 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='points')
212 print ' for (i = start; i < (out_nr+start); i++) { '
213 do_point( intype
, outtype
, 'out+i', 'i' );
217 def lines(intype
, outtype
, inpv
, outpv
, pr
):
218 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='lines')
219 print ' for (i = start; i < (out_nr+start); i+=2) { '
220 do_line( intype
, outtype
, 'out+i', 'i', 'i+1', inpv
, outpv
);
224 def linestrip(intype
, outtype
, inpv
, outpv
, pr
):
225 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='linestrip')
226 print ' for (i = start, j = 0; j < out_nr; j+=2, i++) { '
227 do_line( intype
, outtype
, 'out+j', 'i', 'i+1', inpv
, outpv
);
231 def lineloop(intype
, outtype
, inpv
, outpv
, pr
):
232 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='lineloop')
233 print ' for (i = start, j = 0; j < out_nr - 2; j+=2, i++) { '
234 do_line( intype
, outtype
, 'out+j', 'i', 'i+1', inpv
, outpv
);
236 do_line( intype
, outtype
, 'out+j', 'i', 'start', inpv
, outpv
);
239 def tris(intype
, outtype
, inpv
, outpv
, pr
):
240 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='tris')
241 print ' for (i = start; i < (out_nr+start); i+=3) { '
242 do_tri( intype
, outtype
, 'out+i', 'i', 'i+1', 'i+2', inpv
, outpv
);
247 def tristrip(intype
, outtype
, inpv
, outpv
, pr
):
248 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='tristrip')
249 print ' for (i = start, j = 0; j < out_nr; j+=3, i++) { '
251 do_tri( intype
, outtype
, 'out+j', 'i', 'i+1+(i&1)', 'i+2-(i&1)', inpv
, outpv
);
253 do_tri( intype
, outtype
, 'out+j', 'i+(i&1)', 'i+1-(i&1)', 'i+2', inpv
, outpv
);
258 def trifan(intype
, outtype
, inpv
, outpv
, pr
):
259 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='trifan')
260 print ' for (i = start, j = 0; j < out_nr; j+=3, i++) { '
261 do_tri( intype
, outtype
, 'out+j', 'start', 'i+1', 'i+2', inpv
, outpv
);
267 def polygon(intype
, outtype
, inpv
, outpv
, pr
):
268 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='polygon')
269 print ' for (i = start, j = 0; j < out_nr; j+=3, i++) { '
272 print ' if (i + 3 > in_nr) {'
273 print ' (out+j+0)[0] = restart_index;'
274 print ' (out+j+0)[1] = restart_index;'
275 print ' (out+j+0)[2] = restart_index;'
278 print ' if (in[i + 0] == restart_index) {'
281 print ' goto restart;'
283 print ' if (in[i + 1] == restart_index) {'
286 print ' goto restart;'
288 print ' if (in[i + 2] == restart_index) {'
291 print ' goto restart;'
295 do_tri( intype
, outtype
, 'out+j', 'start', 'i+1', 'i+2', inpv
, outpv
);
297 do_tri( intype
, outtype
, 'out+j', 'i+1', 'i+2', 'start', inpv
, outpv
);
302 def quads(intype
, outtype
, inpv
, outpv
, pr
):
303 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='quads')
304 print ' for (i = start, j = 0; j < out_nr; j+=6, i+=4) { '
307 print ' if (i + 4 > in_nr) {'
308 print ' (out+j+0)[0] = restart_index;'
309 print ' (out+j+0)[1] = restart_index;'
310 print ' (out+j+0)[2] = restart_index;'
311 print ' (out+j+3)[0] = restart_index;'
312 print ' (out+j+3)[1] = restart_index;'
313 print ' (out+j+3)[2] = restart_index;'
316 print ' if (in[i + 0] == restart_index) {'
318 print ' goto restart;'
320 print ' if (in[i + 1] == restart_index) {'
322 print ' goto restart;'
324 print ' if (in[i + 2] == restart_index) {'
326 print ' goto restart;'
328 print ' if (in[i + 3] == restart_index) {'
330 print ' goto restart;'
333 do_quad( intype
, outtype
, 'out+j', 'i+0', 'i+1', 'i+2', 'i+3', inpv
, outpv
);
338 def quadstrip(intype
, outtype
, inpv
, outpv
, pr
):
339 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='quadstrip')
340 print ' for (i = start, j = 0; j < out_nr; j+=6, i+=2) { '
343 print ' if (i + 4 > in_nr) {'
344 print ' (out+j+0)[0] = restart_index;'
345 print ' (out+j+0)[1] = restart_index;'
346 print ' (out+j+0)[2] = restart_index;'
347 print ' (out+j+3)[0] = restart_index;'
348 print ' (out+j+3)[1] = restart_index;'
349 print ' (out+j+3)[2] = restart_index;'
352 print ' if (in[i + 0] == restart_index) {'
354 print ' goto restart;'
356 print ' if (in[i + 1] == restart_index) {'
358 print ' goto restart;'
360 print ' if (in[i + 2] == restart_index) {'
362 print ' goto restart;'
364 print ' if (in[i + 3] == restart_index) {'
366 print ' goto restart;'
369 do_quad( intype
, outtype
, 'out+j', 'i+2', 'i+0', 'i+1', 'i+3', inpv
, outpv
);
371 do_quad( intype
, outtype
, 'out+j', 'i+0', 'i+1', 'i+3', 'i+2', inpv
, outpv
);
376 def linesadj(intype
, outtype
, inpv
, outpv
, pr
):
377 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='linesadj')
378 print ' for (i = start; i < (out_nr+start); i+=4) { '
379 do_lineadj( intype
, outtype
, 'out+i', 'i+0', 'i+1', 'i+2', 'i+3', inpv
, outpv
)
384 def linestripadj(intype
, outtype
, inpv
, outpv
, pr
):
385 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='linestripadj')
386 print ' for (i = start, j = 0; j < out_nr; j+=4, i++) {'
387 do_lineadj( intype
, outtype
, 'out+j', 'i+0', 'i+1', 'i+2', 'i+3', inpv
, outpv
)
392 def trisadj(intype
, outtype
, inpv
, outpv
, pr
):
393 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='trisadj')
394 print ' for (i = start; i < (out_nr+start); i+=6) { '
395 do_triadj( intype
, outtype
, 'out+i', 'i+0', 'i+1', 'i+2', 'i+3',
396 'i+4', 'i+5', inpv
, outpv
)
401 def tristripadj(intype
, outtype
, inpv
, outpv
, pr
):
402 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='tristripadj')
403 print ' for (i = start, j = 0; j < out_nr; i+=2, j+=6) { '
404 print ' if (i % 4 == 0) {'
405 print ' /* even triangle */'
406 do_triadj( intype
, outtype
, 'out+j',
407 'i+0', 'i+1', 'i+2', 'i+3', 'i+4', 'i+5', inpv
, outpv
)
409 print ' /* odd triangle */'
410 do_triadj( intype
, outtype
, 'out+j',
411 'i+2', 'i-2', 'i+0', 'i+3', 'i+4', 'i+6', inpv
, outpv
)
418 for intype
in INTYPES
:
419 for outtype
in OUTTYPES
:
420 for inpv
in (FIRST
, LAST
):
421 for outpv
in (FIRST
, LAST
):
422 for pr
in (PRDISABLE
, PRENABLE
):
423 if pr
== PRENABLE
and intype
== GENERATE
:
425 points(intype
, outtype
, inpv
, outpv
, pr
)
426 lines(intype
, outtype
, inpv
, outpv
, pr
)
427 linestrip(intype
, outtype
, inpv
, outpv
, pr
)
428 lineloop(intype
, outtype
, inpv
, outpv
, pr
)
429 tris(intype
, outtype
, inpv
, outpv
, pr
)
430 tristrip(intype
, outtype
, inpv
, outpv
, pr
)
431 trifan(intype
, outtype
, inpv
, outpv
, pr
)
432 quads(intype
, outtype
, inpv
, outpv
, pr
)
433 quadstrip(intype
, outtype
, inpv
, outpv
, pr
)
434 polygon(intype
, outtype
, inpv
, outpv
, pr
)
435 linesadj(intype
, outtype
, inpv
, outpv
, pr
)
436 linestripadj(intype
, outtype
, inpv
, outpv
, pr
)
437 trisadj(intype
, outtype
, inpv
, outpv
, pr
)
438 tristripadj(intype
, outtype
, inpv
, outpv
, pr
)
440 def init(intype
, outtype
, inpv
, outpv
, pr
, prim
):
441 if intype
== GENERATE
:
443 outtype_idx
[outtype
] +
444 '][' + pv_idx
[inpv
] +
445 '][' + pv_idx
[outpv
] +
446 '][' + longprim
[prim
] +
447 '] = ' + name( intype
, outtype
, inpv
, outpv
, pr
, prim
) + ';')
449 print ('translate[' +
451 '][' + outtype_idx
[outtype
] +
452 '][' + pv_idx
[inpv
] +
453 '][' + pv_idx
[outpv
] +
455 '][' + longprim
[prim
] +
456 '] = ' + name( intype
, outtype
, inpv
, outpv
, pr
, prim
) + ';')
459 def emit_all_inits():
460 for intype
in INTYPES
:
461 for outtype
in OUTTYPES
:
466 init(intype
, outtype
, inpv
, outpv
, pr
, prim
)
469 print 'void u_index_init( void )'
471 print ' static int firsttime = 1;'
472 print ' if (!firsttime) return;'
473 print ' firsttime = 0;'
481 print '#include "indices/u_indices.c"'
491 if __name__
== '__main__':