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'
31 INTYPES
= (GENERATE
, UBYTE
, USHORT
, UINT
)
32 OUTTYPES
= (USHORT
, UINT
)
45 LONGPRIMS
=('PIPE_PRIM_POINTS',
47 'PIPE_PRIM_LINE_STRIP',
48 'PIPE_PRIM_LINE_LOOP',
49 'PIPE_PRIM_TRIANGLES',
50 'PIPE_PRIM_TRIANGLE_FAN',
51 'PIPE_PRIM_TRIANGLE_STRIP',
53 'PIPE_PRIM_QUAD_STRIP',
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')
63 print '''/* File automatically generated by indices.py */'''
69 * Functions to translate and generate index lists
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"
80 static unsigned out_size_idx( unsigned index_size )
83 case 4: return OUT_UINT;
84 case 2: return OUT_USHORT;
85 default: assert(0); return OUT_USHORT;
89 static unsigned in_size_idx( unsigned 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;
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];
106 def vert( intype
, outtype
, v0
):
107 if intype
== GENERATE
:
108 return '(' + outtype
+ ')(' + v0
+ ')'
110 return '(' + outtype
+ ')in[' + v0
+ ']'
112 def point( intype
, outtype
, ptr
, v0
):
113 print ' (' + ptr
+ ')[0] = ' + vert( intype
, outtype
, v0
) + ';'
115 def line( intype
, outtype
, ptr
, v0
, v1
):
116 print ' (' + ptr
+ ')[0] = ' + vert( intype
, outtype
, v0
) + ';'
117 print ' (' + ptr
+ ')[1] = ' + vert( intype
, outtype
, v1
) + ';'
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
) + ';'
124 def do_point( intype
, outtype
, ptr
, v0
):
125 point( intype
, outtype
, ptr
, v0
)
127 def do_line( intype
, outtype
, ptr
, v0
, v1
, inpv
, outpv
):
129 line( intype
, outtype
, ptr
, v0
, v1
)
131 line( intype
, outtype
, ptr
, v1
, v0
)
133 def do_tri( intype
, outtype
, ptr
, v0
, v1
, v2
, inpv
, outpv
):
135 tri( intype
, outtype
, ptr
, v0
, v1
, v2
)
138 tri( intype
, outtype
, ptr
, v1
, v2
, v0
)
140 tri( intype
, outtype
, ptr
, v2
, v0
, v1
)
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
);
146 def name(intype
, outtype
, inpv
, outpv
, prim
):
147 if intype
== GENERATE
:
148 return 'generate_' + prim
+ '_' + outtype
+ '_' + inpv
+ '2' + outpv
150 return 'translate_' + prim
+ '_' + intype
+ '2' + outtype
+ '_' + inpv
+ '2' + outpv
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 nr,'
157 print ' void *_out )'
159 if intype
!= GENERATE
:
160 print ' const ' + intype
+ '*in = (const ' + intype
+ '*)_in;'
161 print ' ' + outtype
+ ' *out = (' + outtype
+ '*)_out;'
162 print ' unsigned i, j;'
169 def points(intype
, outtype
, inpv
, outpv
):
170 preamble(intype
, outtype
, inpv
, outpv
, prim
='points')
171 print ' for (i = 0; i < nr; i++) { '
172 do_point( intype
, outtype
, 'out+i', 'i' );
176 def lines(intype
, outtype
, inpv
, outpv
):
177 preamble(intype
, outtype
, inpv
, outpv
, prim
='lines')
178 print ' for (i = 0; i < nr; i+=2) { '
179 do_line( intype
, outtype
, 'out+i', 'i', 'i+1', inpv
, outpv
);
183 def linestrip(intype
, outtype
, inpv
, outpv
):
184 preamble(intype
, outtype
, inpv
, outpv
, prim
='linestrip')
185 print ' for (j = i = 0; j < nr; j+=2, i++) { '
186 do_line( intype
, outtype
, 'out+j', 'i', 'i+1', inpv
, outpv
);
190 def lineloop(intype
, outtype
, inpv
, outpv
):
191 preamble(intype
, outtype
, inpv
, outpv
, prim
='lineloop')
192 print ' for (j = i = 0; j < nr - 2; j+=2, i++) { '
193 do_line( intype
, outtype
, 'out+j', 'i', 'i+1', inpv
, outpv
);
195 do_line( intype
, outtype
, 'out+j', 'i', '0', inpv
, outpv
);
198 def tris(intype
, outtype
, inpv
, outpv
):
199 preamble(intype
, outtype
, inpv
, outpv
, prim
='tris')
200 print ' for (i = 0; i < nr; i+=3) { '
201 do_tri( intype
, outtype
, 'out+i', 'i', 'i+1', 'i+2', inpv
, outpv
);
206 def tristrip(intype
, outtype
, inpv
, outpv
):
207 preamble(intype
, outtype
, inpv
, outpv
, prim
='tristrip')
208 print ' for (j = i = 0; j < nr; j+=3, i++) { '
210 do_tri( intype
, outtype
, 'out+j', 'i', 'i+1+(i&1)', 'i+2-(i&1)', inpv
, outpv
);
212 do_tri( intype
, outtype
, 'out+j', 'i+(i&1)', 'i+1-(i&1)', 'i+2', inpv
, outpv
);
217 def trifan(intype
, outtype
, inpv
, outpv
):
218 preamble(intype
, outtype
, inpv
, outpv
, prim
='trifan')
219 print ' for (j = i = 0; j < nr; j+=3, i++) { '
220 do_tri( intype
, outtype
, 'out+j', '0', 'i+1', 'i+2', inpv
, outpv
);
226 def polygon(intype
, outtype
, inpv
, outpv
):
227 preamble(intype
, outtype
, inpv
, outpv
, prim
='polygon')
228 print ' for (j = i = 0; j < nr; j+=3, i++) { '
230 do_tri( intype
, outtype
, 'out+j', '0', 'i+1', 'i+2', inpv
, outpv
);
232 do_tri( intype
, outtype
, 'out+j', 'i+1', 'i+2', '0', inpv
, outpv
);
237 def quads(intype
, outtype
, inpv
, outpv
):
238 preamble(intype
, outtype
, inpv
, outpv
, prim
='quads')
239 print ' for (j = i = 0; j < nr; j+=6, i+=4) { '
240 do_quad( intype
, outtype
, 'out+j', 'i+0', 'i+1', 'i+2', 'i+3', inpv
, outpv
);
245 def quadstrip(intype
, outtype
, inpv
, outpv
):
246 preamble(intype
, outtype
, inpv
, outpv
, prim
='quadstrip')
247 print ' for (j = i = 0; j < nr; j+=6, i+=2) { '
248 do_quad( intype
, outtype
, 'out+j', 'i+2', 'i+0', 'i+1', 'i+3', inpv
, outpv
);
254 for intype
in INTYPES
:
255 for outtype
in OUTTYPES
:
256 for inpv
in (FIRST
, LAST
):
257 for outpv
in (FIRST
, LAST
):
258 points(intype
, outtype
, inpv
, outpv
)
259 lines(intype
, outtype
, inpv
, outpv
)
260 linestrip(intype
, outtype
, inpv
, outpv
)
261 lineloop(intype
, outtype
, inpv
, outpv
)
262 tris(intype
, outtype
, inpv
, outpv
)
263 tristrip(intype
, outtype
, inpv
, outpv
)
264 trifan(intype
, outtype
, inpv
, outpv
)
265 quads(intype
, outtype
, inpv
, outpv
)
266 quadstrip(intype
, outtype
, inpv
, outpv
)
267 polygon(intype
, outtype
, inpv
, outpv
)
269 def init(intype
, outtype
, inpv
, outpv
, prim
):
270 if intype
== GENERATE
:
272 outtype_idx
[outtype
] +
273 '][' + pv_idx
[inpv
] +
274 '][' + pv_idx
[outpv
] +
275 '][' + longprim
[prim
] +
276 '] = ' + name( intype
, outtype
, inpv
, outpv
, prim
) + ';')
278 print ('translate[' +
280 '][' + outtype_idx
[outtype
] +
281 '][' + pv_idx
[inpv
] +
282 '][' + pv_idx
[outpv
] +
283 '][' + longprim
[prim
] +
284 '] = ' + name( intype
, outtype
, inpv
, outpv
, prim
) + ';')
287 def emit_all_inits():
288 for intype
in INTYPES
:
289 for outtype
in OUTTYPES
:
293 init(intype
, outtype
, inpv
, outpv
, prim
)
296 print 'void u_index_init( void )'
298 print ' static int firsttime = 1;'
299 print ' if (!firsttime) return;'
300 print ' firsttime = 0;'
308 print '#include "indices/u_indices.c"'
318 if __name__
== '__main__':