1 from __future__
import print_function
5 * Copyright 2009 VMware, Inc.
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:
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
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.
29 GENERATE
, UBYTE
, USHORT
, UINT
= 'generate', 'ubyte', 'ushort', 'uint'
30 FIRST
, LAST
= 'first', 'last'
31 PRDISABLE
, PRENABLE
= 'prdisable', 'prenable'
33 INTYPES
= (GENERATE
, UBYTE
, USHORT
, UINT
)
34 OUTTYPES
= (USHORT
, UINT
)
36 PRS
=(PRDISABLE
, PRENABLE
)
52 LONGPRIMS
=('PIPE_PRIM_POINTS',
54 'PIPE_PRIM_LINE_STRIP',
55 'PIPE_PRIM_LINE_LOOP',
56 'PIPE_PRIM_TRIANGLES',
57 'PIPE_PRIM_TRIANGLE_FAN',
58 'PIPE_PRIM_TRIANGLE_STRIP',
60 'PIPE_PRIM_QUAD_STRIP',
62 'PIPE_PRIM_LINES_ADJACENCY',
63 'PIPE_PRIM_LINE_STRIP_ADJACENCY',
64 'PIPE_PRIM_TRIANGLES_ADJACENCY',
65 'PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY')
67 longprim
= dict(zip(PRIMS
, LONGPRIMS
))
68 intype_idx
= dict(ubyte
='IN_UBYTE', ushort
='IN_USHORT', uint
='IN_UINT')
69 outtype_idx
= dict(ushort
='OUT_USHORT', uint
='OUT_UINT')
70 pv_idx
= dict(first
='PV_FIRST', last
='PV_LAST')
71 pr_idx
= dict(prdisable
='PR_DISABLE', prenable
='PR_ENABLE')
74 print('''/* File automatically generated by u_indices_gen.py */''')
80 * Functions to translate and generate index lists
83 #include "indices/u_indices_priv.h"
84 #include "util/u_debug.h"
85 #include "util/u_memory.h"
88 static unsigned out_size_idx( unsigned index_size )
91 case 4: return OUT_UINT;
92 case 2: return OUT_USHORT;
93 default: assert(0); return OUT_USHORT;
97 static unsigned in_size_idx( unsigned index_size )
100 case 4: return IN_UINT;
101 case 2: return IN_USHORT;
102 case 1: return IN_UBYTE;
103 default: assert(0); return IN_UBYTE;
108 static u_translate_func translate[IN_COUNT][OUT_COUNT][PV_COUNT][PV_COUNT][PR_COUNT][PRIM_COUNT];
109 static u_generate_func generate[OUT_COUNT][PV_COUNT][PV_COUNT][PRIM_COUNT];
114 def vert( intype
, outtype
, v0
):
115 if intype
== GENERATE
:
116 return '(' + outtype
+ ')(' + v0
+ ')'
118 return '(' + outtype
+ ')in[' + v0
+ ']'
120 def point( intype
, outtype
, ptr
, v0
):
121 print(' (' + ptr
+ ')[0] = ' + vert( intype
, outtype
, v0
) + ';')
123 def line( intype
, outtype
, ptr
, v0
, v1
):
124 print(' (' + ptr
+ ')[0] = ' + vert( intype
, outtype
, v0
) + ';')
125 print(' (' + ptr
+ ')[1] = ' + vert( intype
, outtype
, v1
) + ';')
127 def tri( intype
, outtype
, ptr
, v0
, v1
, v2
):
128 print(' (' + ptr
+ ')[0] = ' + vert( intype
, outtype
, v0
) + ';')
129 print(' (' + ptr
+ ')[1] = ' + vert( intype
, outtype
, v1
) + ';')
130 print(' (' + ptr
+ ')[2] = ' + vert( intype
, outtype
, v2
) + ';')
132 def lineadj( intype
, outtype
, ptr
, v0
, v1
, v2
, v3
):
133 print(' (' + ptr
+ ')[0] = ' + vert( intype
, outtype
, v0
) + ';')
134 print(' (' + ptr
+ ')[1] = ' + vert( intype
, outtype
, v1
) + ';')
135 print(' (' + ptr
+ ')[2] = ' + vert( intype
, outtype
, v2
) + ';')
136 print(' (' + ptr
+ ')[3] = ' + vert( intype
, outtype
, v3
) + ';')
138 def triadj( intype
, outtype
, ptr
, v0
, v1
, v2
, v3
, v4
, v5
):
139 print(' (' + ptr
+ ')[0] = ' + vert( intype
, outtype
, v0
) + ';')
140 print(' (' + ptr
+ ')[1] = ' + vert( intype
, outtype
, v1
) + ';')
141 print(' (' + ptr
+ ')[2] = ' + vert( intype
, outtype
, v2
) + ';')
142 print(' (' + ptr
+ ')[3] = ' + vert( intype
, outtype
, v3
) + ';')
143 print(' (' + ptr
+ ')[4] = ' + vert( intype
, outtype
, v4
) + ';')
144 print(' (' + ptr
+ ')[5] = ' + vert( intype
, outtype
, v5
) + ';')
146 def do_point( intype
, outtype
, ptr
, v0
):
147 point( intype
, outtype
, ptr
, v0
)
149 def do_line( intype
, outtype
, ptr
, v0
, v1
, inpv
, outpv
):
151 line( intype
, outtype
, ptr
, v0
, v1
)
153 line( intype
, outtype
, ptr
, v1
, v0
)
155 def do_tri( intype
, outtype
, ptr
, v0
, v1
, v2
, inpv
, outpv
):
157 tri( intype
, outtype
, ptr
, v0
, v1
, v2
)
160 tri( intype
, outtype
, ptr
, v1
, v2
, v0
)
162 tri( intype
, outtype
, ptr
, v2
, v0
, v1
)
164 def do_quad( intype
, outtype
, ptr
, v0
, v1
, v2
, v3
, inpv
, outpv
):
166 do_tri( intype
, outtype
, ptr
+'+0', v0
, v1
, v3
, inpv
, outpv
);
167 do_tri( intype
, outtype
, ptr
+'+3', v1
, v2
, v3
, inpv
, outpv
);
169 do_tri( intype
, outtype
, ptr
+'+0', v0
, v1
, v2
, inpv
, outpv
);
170 do_tri( intype
, outtype
, ptr
+'+3', v0
, v2
, v3
, inpv
, outpv
);
172 def do_lineadj( intype
, outtype
, ptr
, v0
, v1
, v2
, v3
, inpv
, outpv
):
174 lineadj( intype
, outtype
, ptr
, v0
, v1
, v2
, v3
)
176 lineadj( intype
, outtype
, ptr
, v3
, v2
, v1
, v0
)
178 def do_triadj( intype
, outtype
, ptr
, v0
, v1
, v2
, v3
, v4
, v5
, inpv
, outpv
):
180 triadj( intype
, outtype
, ptr
, v0
, v1
, v2
, v3
, v4
, v5
)
182 triadj( intype
, outtype
, ptr
, v4
, v5
, v0
, v1
, v2
, v3
)
184 def name(intype
, outtype
, inpv
, outpv
, pr
, prim
):
185 if intype
== GENERATE
:
186 return 'generate_' + prim
+ '_' + outtype
+ '_' + inpv
+ '2' + outpv
188 return 'translate_' + prim
+ '_' + intype
+ '2' + outtype
+ '_' + inpv
+ '2' + outpv
+ '_' + pr
190 def preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
):
191 print('static void ' + name( intype
, outtype
, inpv
, outpv
, pr
, prim
) + '(')
192 if intype
!= GENERATE
:
193 print(' const void * _in,')
194 print(' unsigned start,')
195 if intype
!= GENERATE
:
196 print(' unsigned in_nr,')
197 print(' unsigned out_nr,')
198 if intype
!= GENERATE
:
199 print(' unsigned restart_index,')
200 print(' void *_out )')
202 if intype
!= GENERATE
:
203 print(' const ' + intype
+ '*in = (const ' + intype
+ '*)_in;')
204 print(' ' + outtype
+ ' *out = (' + outtype
+ '*)_out;')
205 print(' unsigned i, j;')
212 def points(intype
, outtype
, inpv
, outpv
, pr
):
213 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='points')
214 print(' for (i = start; i < (out_nr+start); i++) { ')
215 do_point( intype
, outtype
, 'out+i', 'i' );
219 def lines(intype
, outtype
, inpv
, outpv
, pr
):
220 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='lines')
221 print(' for (i = start; i < (out_nr+start); i+=2) { ')
222 do_line( intype
, outtype
, 'out+i', 'i', 'i+1', inpv
, outpv
);
226 def linestrip(intype
, outtype
, inpv
, outpv
, pr
):
227 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='linestrip')
228 print(' for (i = start, j = 0; j < out_nr; j+=2, i++) { ')
229 do_line( intype
, outtype
, 'out+j', 'i', 'i+1', inpv
, outpv
);
233 def lineloop(intype
, outtype
, inpv
, outpv
, pr
):
234 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='lineloop')
235 print(' for (i = start, j = 0; j < out_nr - 2; j+=2, i++) { ')
236 do_line( intype
, outtype
, 'out+j', 'i', 'i+1', inpv
, outpv
);
238 do_line( intype
, outtype
, 'out+j', 'i', 'start', inpv
, outpv
);
241 def tris(intype
, outtype
, inpv
, outpv
, pr
):
242 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='tris')
243 print(' for (i = start; i < (out_nr+start); i+=3) { ')
244 do_tri( intype
, outtype
, 'out+i', 'i', 'i+1', 'i+2', inpv
, outpv
);
249 def tristrip(intype
, outtype
, inpv
, outpv
, pr
):
250 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='tristrip')
251 print(' for (i = start, j = 0; j < out_nr; j+=3, i++) { ')
253 do_tri( intype
, outtype
, 'out+j', 'i', 'i+1+(i&1)', 'i+2-(i&1)', inpv
, outpv
);
255 do_tri( intype
, outtype
, 'out+j', 'i+(i&1)', 'i+1-(i&1)', 'i+2', inpv
, outpv
);
260 def trifan(intype
, outtype
, inpv
, outpv
, pr
):
261 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='trifan')
262 print(' for (i = start, j = 0; j < out_nr; j+=3, i++) { ')
263 do_tri( intype
, outtype
, 'out+j', 'start', 'i+1', 'i+2', inpv
, outpv
);
269 def polygon(intype
, outtype
, inpv
, outpv
, pr
):
270 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='polygon')
271 print(' for (i = start, j = 0; j < out_nr; j+=3, i++) { ')
274 print(' if (i + 3 > in_nr) {')
275 print(' (out+j+0)[0] = restart_index;')
276 print(' (out+j+0)[1] = restart_index;')
277 print(' (out+j+0)[2] = restart_index;')
280 print(' if (in[i + 0] == restart_index) {')
283 print(' goto restart;')
285 print(' if (in[i + 1] == restart_index) {')
288 print(' goto restart;')
290 print(' if (in[i + 2] == restart_index) {')
293 print(' goto restart;')
297 do_tri( intype
, outtype
, 'out+j', 'start', 'i+1', 'i+2', inpv
, outpv
);
299 do_tri( intype
, outtype
, 'out+j', 'i+1', 'i+2', 'start', inpv
, outpv
);
304 def quads(intype
, outtype
, inpv
, outpv
, pr
):
305 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='quads')
306 print(' for (i = start, j = 0; j < out_nr; j+=6, i+=4) { ')
309 print(' if (i + 4 > in_nr) {')
310 print(' (out+j+0)[0] = restart_index;')
311 print(' (out+j+0)[1] = restart_index;')
312 print(' (out+j+0)[2] = restart_index;')
313 print(' (out+j+3)[0] = restart_index;')
314 print(' (out+j+3)[1] = restart_index;')
315 print(' (out+j+3)[2] = restart_index;')
318 print(' if (in[i + 0] == restart_index) {')
320 print(' goto restart;')
322 print(' if (in[i + 1] == restart_index) {')
324 print(' goto restart;')
326 print(' if (in[i + 2] == restart_index) {')
328 print(' goto restart;')
330 print(' if (in[i + 3] == restart_index) {')
332 print(' goto restart;')
335 do_quad( intype
, outtype
, 'out+j', 'i+0', 'i+1', 'i+2', 'i+3', inpv
, outpv
);
340 def quadstrip(intype
, outtype
, inpv
, outpv
, pr
):
341 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='quadstrip')
342 print(' for (i = start, j = 0; j < out_nr; j+=6, i+=2) { ')
345 print(' if (i + 4 > in_nr) {')
346 print(' (out+j+0)[0] = restart_index;')
347 print(' (out+j+0)[1] = restart_index;')
348 print(' (out+j+0)[2] = restart_index;')
349 print(' (out+j+3)[0] = restart_index;')
350 print(' (out+j+3)[1] = restart_index;')
351 print(' (out+j+3)[2] = restart_index;')
354 print(' if (in[i + 0] == restart_index) {')
356 print(' goto restart;')
358 print(' if (in[i + 1] == restart_index) {')
360 print(' goto restart;')
362 print(' if (in[i + 2] == restart_index) {')
364 print(' goto restart;')
366 print(' if (in[i + 3] == restart_index) {')
368 print(' goto restart;')
371 do_quad( intype
, outtype
, 'out+j', 'i+2', 'i+0', 'i+1', 'i+3', inpv
, outpv
);
373 do_quad( intype
, outtype
, 'out+j', 'i+0', 'i+1', 'i+3', 'i+2', inpv
, outpv
);
378 def linesadj(intype
, outtype
, inpv
, outpv
, pr
):
379 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='linesadj')
380 print(' for (i = start; i < (out_nr+start); i+=4) { ')
381 do_lineadj( intype
, outtype
, 'out+i', 'i+0', 'i+1', 'i+2', 'i+3', inpv
, outpv
)
386 def linestripadj(intype
, outtype
, inpv
, outpv
, pr
):
387 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='linestripadj')
388 print(' for (i = start, j = 0; j < out_nr; j+=4, i++) {')
389 do_lineadj( intype
, outtype
, 'out+j', 'i+0', 'i+1', 'i+2', 'i+3', inpv
, outpv
)
394 def trisadj(intype
, outtype
, inpv
, outpv
, pr
):
395 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='trisadj')
396 print(' for (i = start; i < (out_nr+start); i+=6) { ')
397 do_triadj( intype
, outtype
, 'out+i', 'i+0', 'i+1', 'i+2', 'i+3',
398 'i+4', 'i+5', inpv
, outpv
)
403 def tristripadj(intype
, outtype
, inpv
, outpv
, pr
):
404 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='tristripadj')
405 print(' for (i = start, j = 0; j < out_nr; i+=2, j+=6) { ')
406 print(' if (i % 4 == 0) {')
407 print(' /* even triangle */')
408 do_triadj( intype
, outtype
, 'out+j',
409 'i+0', 'i+1', 'i+2', 'i+3', 'i+4', 'i+5', inpv
, outpv
)
411 print(' /* odd triangle */')
412 do_triadj( intype
, outtype
, 'out+j',
413 'i+2', 'i-2', 'i+0', 'i+3', 'i+4', 'i+6', inpv
, outpv
)
420 for intype
in INTYPES
:
421 for outtype
in OUTTYPES
:
422 for inpv
in (FIRST
, LAST
):
423 for outpv
in (FIRST
, LAST
):
424 for pr
in (PRDISABLE
, PRENABLE
):
425 if pr
== PRENABLE
and intype
== GENERATE
:
427 points(intype
, outtype
, inpv
, outpv
, pr
)
428 lines(intype
, outtype
, inpv
, outpv
, pr
)
429 linestrip(intype
, outtype
, inpv
, outpv
, pr
)
430 lineloop(intype
, outtype
, inpv
, outpv
, pr
)
431 tris(intype
, outtype
, inpv
, outpv
, pr
)
432 tristrip(intype
, outtype
, inpv
, outpv
, pr
)
433 trifan(intype
, outtype
, inpv
, outpv
, pr
)
434 quads(intype
, outtype
, inpv
, outpv
, pr
)
435 quadstrip(intype
, outtype
, inpv
, outpv
, pr
)
436 polygon(intype
, outtype
, inpv
, outpv
, pr
)
437 linesadj(intype
, outtype
, inpv
, outpv
, pr
)
438 linestripadj(intype
, outtype
, inpv
, outpv
, pr
)
439 trisadj(intype
, outtype
, inpv
, outpv
, pr
)
440 tristripadj(intype
, outtype
, inpv
, outpv
, pr
)
442 def init(intype
, outtype
, inpv
, outpv
, pr
, prim
):
443 if intype
== GENERATE
:
445 outtype_idx
[outtype
] +
446 '][' + pv_idx
[inpv
] +
447 '][' + pv_idx
[outpv
] +
448 '][' + longprim
[prim
] +
449 '] = ' + name( intype
, outtype
, inpv
, outpv
, pr
, prim
) + ';')
451 print ('translate[' +
453 '][' + outtype_idx
[outtype
] +
454 '][' + pv_idx
[inpv
] +
455 '][' + pv_idx
[outpv
] +
457 '][' + longprim
[prim
] +
458 '] = ' + name( intype
, outtype
, inpv
, outpv
, pr
, prim
) + ';')
461 def emit_all_inits():
462 for intype
in INTYPES
:
463 for outtype
in OUTTYPES
:
468 init(intype
, outtype
, inpv
, outpv
, pr
, prim
)
471 print('void u_index_init( void )')
473 print(' static int firsttime = 1;')
474 print(' if (!firsttime) return;')
475 print(' firsttime = 0;')
483 print('#include "indices/u_indices.c"')
493 if __name__
== '__main__':