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.
31 GENERATE, UBYTE, USHORT, UINT = 'generate', 'ubyte', 'ushort', 'uint'
32 FIRST, LAST = 'first', 'last'
34 INTYPES = (GENERATE, UBYTE, USHORT, UINT)
35 OUTTYPES = (USHORT, UINT)
45 LONGPRIMS=('PIPE_PRIM_TRIANGLES',
46 'PIPE_PRIM_TRIANGLE_FAN',
47 'PIPE_PRIM_TRIANGLE_STRIP',
49 'PIPE_PRIM_QUAD_STRIP',
51 'PIPE_PRIM_TRIANGLES_ADJACENCY',
52 'PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY')
54 longprim = dict(zip(PRIMS, LONGPRIMS))
55 intype_idx = dict(ubyte='IN_UBYTE', ushort='IN_USHORT', uint='IN_UINT')
56 outtype_idx = dict(ushort='OUT_USHORT', uint='OUT_UINT')
59 def prolog(f: 'T.TextIO'):
60 f.write('/* File automatically generated by u_unfilled_gen.py */\n')
66 * Functions to translate and generate index lists
69 #include "indices/u_indices.h"
70 #include "indices/u_indices_priv.h"
71 #include "pipe/p_compiler.h"
72 #include "util/u_debug.h"
73 #include "pipe/p_defines.h"
74 #include "util/u_memory.h"
76 static u_generate_func generate_line[OUT_COUNT][PRIM_COUNT];
77 static u_translate_func translate_line[IN_COUNT][OUT_COUNT][PRIM_COUNT];
81 def vert( intype, outtype, v0 ):
82 if intype == GENERATE:
83 return '(' + outtype + ')(' + v0 + ')'
85 return '(' + outtype + ')in[' + v0 + ']'
87 def line(f: 'T.TextIO', intype, outtype, ptr, v0, v1 ):
88 f.write(' (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';\n')
89 f.write(' (' + ptr + ')[1] = ' + vert( intype, outtype, v1 ) + ';\n')
91 # XXX: have the opportunity here to avoid over-drawing shared lines in
92 # tristrips, fans, etc, by integrating this into the calling functions
93 # and only emitting each line at most once.
95 def do_tri(f: 'T.TextIO', intype, outtype, ptr, v0, v1, v2 ):
96 line(f, intype, outtype, ptr, v0, v1 )
97 line(f, intype, outtype, ptr + '+2', v1, v2 )
98 line(f, intype, outtype, ptr + '+4', v2, v0 )
100 def do_quad(f: 'T.TextIO', intype, outtype, ptr, v0, v1, v2, v3 ):
101 line(f, intype, outtype, ptr, v0, v1 )
102 line(f, intype, outtype, ptr + '+2', v1, v2 )
103 line(f, intype, outtype, ptr + '+4', v2, v3 )
104 line(f, intype, outtype, ptr + '+6', v3, v0 )
106 def name(intype, outtype, prim):
107 if intype == GENERATE:
108 return 'generate_' + prim + '_' + outtype
110 return 'translate_' + prim + '_' + intype + '2' + outtype
112 def preamble(f: 'T.TextIO', intype, outtype, prim):
113 f.write('static void ' + name( intype, outtype, prim ) + '(\n')
114 if intype != GENERATE:
115 f.write(' const void * _in,\n')
116 f.write(' unsigned start,\n')
117 if intype != GENERATE:
118 f.write(' unsigned in_nr,\n')
119 f.write(' unsigned out_nr,\n')
120 if intype != GENERATE:
121 f.write(' unsigned restart_index,\n')
122 f.write(' void *_out )\n')
124 if intype != GENERATE:
125 f.write(' const ' + intype + '*in = (const ' + intype + '*)_in;\n')
126 f.write(' ' + outtype + ' *out = (' + outtype + '*)_out;\n')
127 f.write(' unsigned i, j;\n')
128 f.write(' (void)j;\n')
130 def postamble(f: 'T.TextIO'):
134 def tris(f: 'T.TextIO', intype, outtype):
135 preamble(f, intype, outtype, prim='tris')
136 f.write(' for (i = start, j = 0; j < out_nr; j+=6, i+=3) {\n')
137 do_tri(f, intype, outtype, 'out+j', 'i', 'i+1', 'i+2' );
142 def tristrip(f: 'T.TextIO', intype, outtype):
143 preamble(f, intype, outtype, prim='tristrip')
144 f.write(' for (i = start, j = 0; j < out_nr; j+=6, i++) {\n')
145 do_tri(f, intype, outtype, 'out+j', 'i', 'i+1/*+(i&1)*/', 'i+2/*-(i&1)*/' );
150 def trifan(f: 'T.TextIO', intype, outtype):
151 preamble(f, intype, outtype, prim='trifan')
152 f.write(' for (i = start, j = 0; j < out_nr; j+=6, i++) {\n')
153 do_tri(f, intype, outtype, 'out+j', '0', 'i+1', 'i+2' );
159 def polygon(f: 'T.TextIO', intype, outtype):
160 preamble(f, intype, outtype, prim='polygon')
161 f.write(' for (i = start, j = 0; j < out_nr; j+=2, i++) {\n')
162 line(f, intype, outtype, 'out+j', 'i', '(i+1)%(out_nr/2)\n' )
167 def quads(f: 'T.TextIO', intype, outtype):
168 preamble(f, intype, outtype, prim='quads')
169 f.write(' for (i = start, j = 0; j < out_nr; j+=8, i+=4) {\n')
170 do_quad(f, intype, outtype, 'out+j', 'i+0', 'i+1', 'i+2', 'i+3' );
175 def quadstrip(f: 'T.TextIO', intype, outtype):
176 preamble(f, intype, outtype, prim='quadstrip')
177 f.write(' for (i = start, j = 0; j < out_nr; j+=8, i+=2) {\n')
178 do_quad(f, intype, outtype, 'out+j', 'i+2', 'i+0', 'i+1', 'i+3' );
183 def trisadj(f: 'T.TextIO', intype, outtype):
184 preamble(f, intype, outtype, prim='trisadj')
185 f.write(' for (i = start, j = 0; j < out_nr; j+=6, i+=6) {\n')
186 do_tri(f, intype, outtype, 'out+j', 'i', 'i+2', 'i+4' );
191 def tristripadj(f: 'T.TextIO', intype, outtype):
192 preamble(f, intype, outtype, prim='tristripadj')
193 f.write(' for (i = start, j = 0; j < out_nr; j+=6, i+=2) {\n')
194 do_tri(f, intype, outtype, 'out+j', 'i', 'i+2', 'i+4' );
199 def emit_funcs(f: 'T.TextIO'):
200 for intype, outtype in itertools.product(INTYPES, OUTTYPES):
201 tris(f, intype, outtype)
202 tristrip(f, intype, outtype)
203 trifan(f, intype, outtype)
204 quads(f, intype, outtype)
205 quadstrip(f, intype, outtype)
206 polygon(f, intype, outtype)
207 trisadj(f, intype, outtype)
208 tristripadj(f, intype, outtype)
210 def init(f: 'T.TextIO', intype, outtype, prim):
211 if intype == GENERATE:
212 f.write('generate_line[' +
213 outtype_idx[outtype] +
214 '][' + longprim[prim] +
215 '] = ' + name( intype, outtype, prim ) + ';\n')
217 f.write('translate_line[' +
219 '][' + outtype_idx[outtype] +
220 '][' + longprim[prim] +
221 '] = ' + name( intype, outtype, prim ) + ';\n')
224 def emit_all_inits(f: 'T.TextIO'):
225 for intype, outtype, prim in itertools.product(INTYPES, OUTTYPES, PRIMS):
226 init(f, intype, outtype, prim)
228 def emit_init(f: 'T.TextIO'):
229 f.write('void u_unfilled_init( void )\n')
231 f.write(' static int firsttime = 1;\n')
232 f.write(' if (!firsttime) return;\n')
233 f.write(' firsttime = 0;\n')
240 def epilog(f: 'T.TextIO'):
241 f.write('#include "indices/u_unfilled_indices.c"\n')
245 parser = argparse.ArgumentParser()
246 parser.add_argument('output')
247 args = parser.parse_args()
249 with open(args.output, 'w') as f:
256 if __name__ == '__main__':