4 * Copyright 2009 VMware, Inc.
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:
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
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.
28 GENERATE
, UBYTE
, USHORT
, UINT
= 'generate', 'ubyte', 'ushort', 'uint'
29 FIRST
, LAST
= 'first', 'last'
30 PRDISABLE
, PRENABLE
= 'prdisable', 'prenable'
32 INTYPES
= (GENERATE
, UBYTE
, USHORT
, UINT
)
33 OUTTYPES
= (USHORT
, UINT
)
35 PRS
=(PRDISABLE
, PRENABLE
)
51 LONGPRIMS
=('PIPE_PRIM_POINTS',
53 'PIPE_PRIM_LINE_STRIP',
54 'PIPE_PRIM_LINE_LOOP',
55 'PIPE_PRIM_TRIANGLES',
56 'PIPE_PRIM_TRIANGLE_FAN',
57 'PIPE_PRIM_TRIANGLE_STRIP',
59 'PIPE_PRIM_QUAD_STRIP',
61 'PIPE_PRIM_LINES_ADJACENCY',
62 'PIPE_PRIM_LINE_STRIP_ADJACENCY',
63 'PIPE_PRIM_TRIANGLES_ADJACENCY',
64 'PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY')
66 longprim
= dict(zip(PRIMS
, LONGPRIMS
))
67 intype_idx
= dict(ubyte
='IN_UBYTE', ushort
='IN_USHORT', uint
='IN_UINT')
68 outtype_idx
= dict(ushort
='OUT_USHORT', uint
='OUT_UINT')
69 pv_idx
= dict(first
='PV_FIRST', last
='PV_LAST')
70 pr_idx
= dict(prdisable
='PR_DISABLE', prenable
='PR_ENABLE')
73 print '''/* File automatically generated by indices.py */'''
79 * Functions to translate and generate index lists
82 #include "indices/u_indices.h"
83 #include "indices/u_indices_priv.h"
84 #include "pipe/p_compiler.h"
85 #include "util/u_debug.h"
86 #include "pipe/p_defines.h"
87 #include "util/u_memory.h"
90 static unsigned out_size_idx( unsigned index_size )
93 case 4: return OUT_UINT;
94 case 2: return OUT_USHORT;
95 default: assert(0); return OUT_USHORT;
99 static unsigned in_size_idx( unsigned index_size )
101 switch (index_size) {
102 case 4: return IN_UINT;
103 case 2: return IN_USHORT;
104 case 1: return IN_UBYTE;
105 default: assert(0); return IN_UBYTE;
110 static u_translate_func translate[IN_COUNT][OUT_COUNT][PV_COUNT][PV_COUNT][PR_COUNT][PRIM_COUNT];
111 static u_generate_func generate[OUT_COUNT][PV_COUNT][PV_COUNT][PRIM_COUNT];
116 def vert( intype
, outtype
, v0
):
117 if intype
== GENERATE
:
118 return '(' + outtype
+ ')(' + v0
+ ')'
120 return '(' + outtype
+ ')in[' + v0
+ ']'
122 def point( intype
, outtype
, ptr
, v0
):
123 print ' (' + ptr
+ ')[0] = ' + vert( intype
, outtype
, v0
) + ';'
125 def line( intype
, outtype
, ptr
, v0
, v1
):
126 print ' (' + ptr
+ ')[0] = ' + vert( intype
, outtype
, v0
) + ';'
127 print ' (' + ptr
+ ')[1] = ' + vert( intype
, outtype
, v1
) + ';'
129 def tri( intype
, outtype
, ptr
, v0
, v1
, v2
):
130 print ' (' + ptr
+ ')[0] = ' + vert( intype
, outtype
, v0
) + ';'
131 print ' (' + ptr
+ ')[1] = ' + vert( intype
, outtype
, v1
) + ';'
132 print ' (' + ptr
+ ')[2] = ' + vert( intype
, outtype
, v2
) + ';'
134 def lineadj( intype
, outtype
, ptr
, v0
, v1
, v2
, v3
):
135 print ' (' + ptr
+ ')[0] = ' + vert( intype
, outtype
, v0
) + ';'
136 print ' (' + ptr
+ ')[1] = ' + vert( intype
, outtype
, v1
) + ';'
137 print ' (' + ptr
+ ')[2] = ' + vert( intype
, outtype
, v2
) + ';'
138 print ' (' + ptr
+ ')[3] = ' + vert( intype
, outtype
, v3
) + ';'
140 def triadj( intype
, outtype
, ptr
, v0
, v1
, v2
, v3
, v4
, v5
):
141 print ' (' + ptr
+ ')[0] = ' + vert( intype
, outtype
, v0
) + ';'
142 print ' (' + ptr
+ ')[1] = ' + vert( intype
, outtype
, v1
) + ';'
143 print ' (' + ptr
+ ')[2] = ' + vert( intype
, outtype
, v2
) + ';'
144 print ' (' + ptr
+ ')[3] = ' + vert( intype
, outtype
, v3
) + ';'
145 print ' (' + ptr
+ ')[4] = ' + vert( intype
, outtype
, v4
) + ';'
146 print ' (' + ptr
+ ')[5] = ' + vert( intype
, outtype
, v5
) + ';'
148 def do_point( intype
, outtype
, ptr
, v0
):
149 point( intype
, outtype
, ptr
, v0
)
151 def do_line( intype
, outtype
, ptr
, v0
, v1
, inpv
, outpv
):
153 line( intype
, outtype
, ptr
, v0
, v1
)
155 line( intype
, outtype
, ptr
, v1
, v0
)
157 def do_tri( intype
, outtype
, ptr
, v0
, v1
, v2
, inpv
, outpv
):
159 tri( intype
, outtype
, ptr
, v0
, v1
, v2
)
162 tri( intype
, outtype
, ptr
, v1
, v2
, v0
)
164 tri( intype
, outtype
, ptr
, v2
, v0
, v1
)
166 def do_quad( intype
, outtype
, ptr
, v0
, v1
, v2
, v3
, inpv
, outpv
):
168 do_tri( intype
, outtype
, ptr
+'+0', v0
, v1
, v3
, inpv
, outpv
);
169 do_tri( intype
, outtype
, ptr
+'+3', v1
, v2
, v3
, inpv
, outpv
);
171 do_tri( intype
, outtype
, ptr
+'+0', v0
, v1
, v2
, inpv
, outpv
);
172 do_tri( intype
, outtype
, ptr
+'+3', v0
, v2
, v3
, inpv
, outpv
);
174 def do_lineadj( intype
, outtype
, ptr
, v0
, v1
, v2
, v3
, inpv
, outpv
):
176 lineadj( intype
, outtype
, ptr
, v0
, v1
, v2
, v3
)
178 lineadj( intype
, outtype
, ptr
, v3
, v2
, v1
, v0
)
180 def do_triadj( intype
, outtype
, ptr
, v0
, v1
, v2
, v3
, v4
, v5
, inpv
, outpv
):
182 triadj( intype
, outtype
, ptr
, v0
, v1
, v2
, v3
, v4
, v5
)
184 triadj( intype
, outtype
, ptr
, v4
, v5
, v0
, v1
, v2
, v3
)
186 def name(intype
, outtype
, inpv
, outpv
, pr
, prim
):
187 if intype
== GENERATE
:
188 return 'generate_' + prim
+ '_' + outtype
+ '_' + inpv
+ '2' + outpv
190 return 'translate_' + prim
+ '_' + intype
+ '2' + outtype
+ '_' + inpv
+ '2' + outpv
+ '_' + pr
192 def preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
):
193 print 'static void ' + name( intype
, outtype
, inpv
, outpv
, pr
, prim
) + '('
194 if intype
!= GENERATE
:
195 print ' const void * _in,'
196 print ' unsigned start,'
197 if intype
!= GENERATE
:
198 print ' unsigned in_nr,'
199 print ' unsigned out_nr,'
200 if intype
!= GENERATE
:
201 print ' unsigned restart_index,'
202 print ' void *_out )'
204 if intype
!= GENERATE
:
205 print ' const ' + intype
+ '*in = (const ' + intype
+ '*)_in;'
206 print ' ' + outtype
+ ' *out = (' + outtype
+ '*)_out;'
207 print ' unsigned i, j;'
214 def points(intype
, outtype
, inpv
, outpv
, pr
):
215 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='points')
216 print ' for (i = start; i < (out_nr+start); i++) { '
217 do_point( intype
, outtype
, 'out+i', 'i' );
221 def lines(intype
, outtype
, inpv
, outpv
, pr
):
222 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='lines')
223 print ' for (i = start; i < (out_nr+start); i+=2) { '
224 do_line( intype
, outtype
, 'out+i', 'i', 'i+1', inpv
, outpv
);
228 def linestrip(intype
, outtype
, inpv
, outpv
, pr
):
229 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='linestrip')
230 print ' for (i = start, j = 0; j < out_nr; j+=2, i++) { '
231 do_line( intype
, outtype
, 'out+j', 'i', 'i+1', inpv
, outpv
);
235 def lineloop(intype
, outtype
, inpv
, outpv
, pr
):
236 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='lineloop')
237 print ' for (i = start, j = 0; j < out_nr - 2; j+=2, i++) { '
238 do_line( intype
, outtype
, 'out+j', 'i', 'i+1', inpv
, outpv
);
240 do_line( intype
, outtype
, 'out+j', 'i', 'start', inpv
, outpv
);
243 def tris(intype
, outtype
, inpv
, outpv
, pr
):
244 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='tris')
245 print ' for (i = start; i < (out_nr+start); i+=3) { '
246 do_tri( intype
, outtype
, 'out+i', 'i', 'i+1', 'i+2', inpv
, outpv
);
251 def tristrip(intype
, outtype
, inpv
, outpv
, pr
):
252 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='tristrip')
253 print ' for (i = start, j = 0; j < out_nr; j+=3, i++) { '
255 do_tri( intype
, outtype
, 'out+j', 'i', 'i+1+(i&1)', 'i+2-(i&1)', inpv
, outpv
);
257 do_tri( intype
, outtype
, 'out+j', 'i+(i&1)', 'i+1-(i&1)', 'i+2', inpv
, outpv
);
262 def trifan(intype
, outtype
, inpv
, outpv
, pr
):
263 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='trifan')
264 print ' for (i = start, j = 0; j < out_nr; j+=3, i++) { '
265 do_tri( intype
, outtype
, 'out+j', 'start', 'i+1', 'i+2', inpv
, outpv
);
271 def polygon(intype
, outtype
, inpv
, outpv
, pr
):
272 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='polygon')
273 print ' for (i = start, j = 0; j < out_nr; j+=3, i++) { '
276 print ' if (i + 3 > in_nr) {'
277 print ' (out+j+0)[0] = restart_index;'
278 print ' (out+j+0)[1] = restart_index;'
279 print ' (out+j+0)[2] = restart_index;'
282 print ' if (in[i + 0] == restart_index) {'
285 print ' goto restart;'
287 print ' if (in[i + 1] == restart_index) {'
290 print ' goto restart;'
292 print ' if (in[i + 2] == restart_index) {'
295 print ' goto restart;'
299 do_tri( intype
, outtype
, 'out+j', 'start', 'i+1', 'i+2', inpv
, outpv
);
301 do_tri( intype
, outtype
, 'out+j', 'i+1', 'i+2', 'start', inpv
, outpv
);
306 def quads(intype
, outtype
, inpv
, outpv
, pr
):
307 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='quads')
308 print ' for (i = start, j = 0; j < out_nr; j+=6, i+=4) { '
311 print ' if (i + 4 > in_nr) {'
312 print ' (out+j+0)[0] = restart_index;'
313 print ' (out+j+0)[1] = restart_index;'
314 print ' (out+j+0)[2] = restart_index;'
315 print ' (out+j+3)[0] = restart_index;'
316 print ' (out+j+3)[1] = restart_index;'
317 print ' (out+j+3)[2] = restart_index;'
320 print ' if (in[i + 0] == restart_index) {'
322 print ' goto restart;'
324 print ' if (in[i + 1] == restart_index) {'
326 print ' goto restart;'
328 print ' if (in[i + 2] == restart_index) {'
330 print ' goto restart;'
332 print ' if (in[i + 3] == restart_index) {'
334 print ' goto restart;'
337 do_quad( intype
, outtype
, 'out+j', 'i+0', 'i+1', 'i+2', 'i+3', inpv
, outpv
);
342 def quadstrip(intype
, outtype
, inpv
, outpv
, pr
):
343 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='quadstrip')
344 print ' for (i = start, j = 0; j < out_nr; j+=6, i+=2) { '
347 print ' if (i + 4 > in_nr) {'
348 print ' (out+j+0)[0] = restart_index;'
349 print ' (out+j+0)[1] = restart_index;'
350 print ' (out+j+0)[2] = restart_index;'
351 print ' (out+j+3)[0] = restart_index;'
352 print ' (out+j+3)[1] = restart_index;'
353 print ' (out+j+3)[2] = restart_index;'
356 print ' if (in[i + 0] == restart_index) {'
358 print ' goto restart;'
360 print ' if (in[i + 1] == restart_index) {'
362 print ' goto restart;'
364 print ' if (in[i + 2] == restart_index) {'
366 print ' goto restart;'
368 print ' if (in[i + 3] == restart_index) {'
370 print ' goto restart;'
373 do_quad( intype
, outtype
, 'out+j', 'i+2', 'i+0', 'i+1', 'i+3', inpv
, outpv
);
375 do_quad( intype
, outtype
, 'out+j', 'i+0', 'i+1', 'i+3', 'i+2', inpv
, outpv
);
380 def linesadj(intype
, outtype
, inpv
, outpv
, pr
):
381 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='linesadj')
382 print ' for (i = start; i < (out_nr+start); i+=4) { '
383 do_lineadj( intype
, outtype
, 'out+i', 'i+0', 'i+1', 'i+2', 'i+3', inpv
, outpv
)
388 def linestripadj(intype
, outtype
, inpv
, outpv
, pr
):
389 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='linestripadj')
390 print ' for (i = start, j = 0; j < out_nr; j+=4, i++) {'
391 do_lineadj( intype
, outtype
, 'out+j', 'i+0', 'i+1', 'i+2', 'i+3', inpv
, outpv
)
396 def trisadj(intype
, outtype
, inpv
, outpv
, pr
):
397 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='trisadj')
398 print ' for (i = start; i < (out_nr+start); i+=6) { '
399 do_triadj( intype
, outtype
, 'out+i', 'i+0', 'i+1', 'i+2', 'i+3',
400 'i+4', 'i+5', inpv
, outpv
)
405 def tristripadj(intype
, outtype
, inpv
, outpv
, pr
):
406 preamble(intype
, outtype
, inpv
, outpv
, pr
, prim
='tristripadj')
407 print ' for (i = start, j = 0; j < out_nr; i+=2, j+=6) { '
408 print ' if (i % 4 == 0) {'
409 print ' /* even triangle */'
410 do_triadj( intype
, outtype
, 'out+j',
411 'i+0', 'i+1', 'i+2', 'i+3', 'i+4', 'i+5', inpv
, outpv
)
413 print ' /* odd triangle */'
414 do_triadj( intype
, outtype
, 'out+j',
415 'i+2', 'i-2', 'i+0', 'i+3', 'i+4', 'i+6', inpv
, outpv
)
422 for intype
in INTYPES
:
423 for outtype
in OUTTYPES
:
424 for inpv
in (FIRST
, LAST
):
425 for outpv
in (FIRST
, LAST
):
426 for pr
in (PRDISABLE
, PRENABLE
):
427 if pr
== PRENABLE
and intype
== GENERATE
:
429 points(intype
, outtype
, inpv
, outpv
, pr
)
430 lines(intype
, outtype
, inpv
, outpv
, pr
)
431 linestrip(intype
, outtype
, inpv
, outpv
, pr
)
432 lineloop(intype
, outtype
, inpv
, outpv
, pr
)
433 tris(intype
, outtype
, inpv
, outpv
, pr
)
434 tristrip(intype
, outtype
, inpv
, outpv
, pr
)
435 trifan(intype
, outtype
, inpv
, outpv
, pr
)
436 quads(intype
, outtype
, inpv
, outpv
, pr
)
437 quadstrip(intype
, outtype
, inpv
, outpv
, pr
)
438 polygon(intype
, outtype
, inpv
, outpv
, pr
)
439 linesadj(intype
, outtype
, inpv
, outpv
, pr
)
440 linestripadj(intype
, outtype
, inpv
, outpv
, pr
)
441 trisadj(intype
, outtype
, inpv
, outpv
, pr
)
442 tristripadj(intype
, outtype
, inpv
, outpv
, pr
)
444 def init(intype
, outtype
, inpv
, outpv
, pr
, prim
):
445 if intype
== GENERATE
:
447 outtype_idx
[outtype
] +
448 '][' + pv_idx
[inpv
] +
449 '][' + pv_idx
[outpv
] +
450 '][' + longprim
[prim
] +
451 '] = ' + name( intype
, outtype
, inpv
, outpv
, pr
, prim
) + ';')
453 print ('translate[' +
455 '][' + outtype_idx
[outtype
] +
456 '][' + pv_idx
[inpv
] +
457 '][' + pv_idx
[outpv
] +
459 '][' + longprim
[prim
] +
460 '] = ' + name( intype
, outtype
, inpv
, outpv
, pr
, prim
) + ';')
463 def emit_all_inits():
464 for intype
in INTYPES
:
465 for outtype
in OUTTYPES
:
470 init(intype
, outtype
, inpv
, outpv
, pr
, prim
)
473 print 'void u_index_init( void )'
475 print ' static int firsttime = 1;'
476 print ' if (!firsttime) return;'
477 print ' firsttime = 0;'
485 print '#include "indices/u_indices.c"'
495 if __name__
== '__main__':