gallium,util: Pull u_indices and u_primconvert back into gallium
[platform/upstream/mesa.git] / src / gallium / auxiliary / indices / u_unfilled_gen.py
1 copyright = '''
2 /*
3  * Copyright 2009 VMware, Inc.
4  * All Rights Reserved.
5  *
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:
12  *
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
15  * Software.
16  *
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.
24  */
25 '''
26
27 import argparse
28 import itertools
29 import typing as T
30
31 GENERATE, UBYTE, USHORT, UINT = 'generate', 'ubyte', 'ushort', 'uint'
32 FIRST, LAST = 'first', 'last'
33
34 INTYPES = (GENERATE, UBYTE, USHORT, UINT)
35 OUTTYPES = (USHORT, UINT)
36 PRIMS=('tris',
37        'trifan',
38        'tristrip',
39        'quads',
40        'quadstrip',
41        'polygon',
42        'trisadj',
43        'tristripadj')
44
45 LONGPRIMS=('PIPE_PRIM_TRIANGLES',
46            'PIPE_PRIM_TRIANGLE_FAN',
47            'PIPE_PRIM_TRIANGLE_STRIP',
48            'PIPE_PRIM_QUADS',
49            'PIPE_PRIM_QUAD_STRIP',
50            'PIPE_PRIM_POLYGON',
51            'PIPE_PRIM_TRIANGLES_ADJACENCY',
52            'PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY')
53
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')
57
58
59 def prolog(f: 'T.TextIO'):
60     f.write('/* File automatically generated by u_unfilled_gen.py */\n')
61     f.write(copyright)
62     f.write(r'''
63
64 /**
65  * @file
66  * Functions to translate and generate index lists
67  */
68
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"
75
76 static u_generate_func generate_line[OUT_COUNT][PRIM_COUNT];
77 static u_translate_func translate_line[IN_COUNT][OUT_COUNT][PRIM_COUNT];
78
79 ''')
80
81 def vert( intype, outtype, v0 ):
82     if intype == GENERATE:
83         return '(' + outtype + ')(' + v0 + ')'
84     else:
85         return '(' + outtype + ')in[' + v0 + ']'
86
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')
90
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.
94 #
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 )
99
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 )
105
106 def name(intype, outtype, prim):
107     if intype == GENERATE:
108         return 'generate_' + prim + '_' + outtype
109     else:
110         return 'translate_' + prim + '_' + intype + '2' + outtype
111
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')
123     f.write('{\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')
129
130 def postamble(f: 'T.TextIO'):
131     f.write('}\n')
132
133
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' );
138     f.write('   }\n')
139     postamble(f)
140
141
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)*/' );
146     f.write('   }\n')
147     postamble(f)
148
149
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' );
154     f.write('   }\n')
155     postamble(f)
156
157
158
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' )
163     f.write('   }\n')
164     postamble(f)
165
166
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' );
171     f.write('   }\n')
172     postamble(f)
173
174
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' );
179     f.write('   }\n')
180     postamble(f)
181
182
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' );
187     f.write('   }\n')
188     postamble(f)
189
190
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' );
195     f.write('   }\n')
196     postamble(f)
197
198
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)
209
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')
216     else:
217         f.write('translate_line[' +
218                 intype_idx[intype] +
219                 '][' + outtype_idx[outtype] +
220                 '][' + longprim[prim] +
221                 '] = ' + name( intype, outtype, prim ) + ';\n')
222
223
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)
227
228 def emit_init(f: 'T.TextIO'):
229     f.write('void u_unfilled_init( void )\n')
230     f.write('{\n')
231     f.write('  static int firsttime = 1;\n')
232     f.write('  if (!firsttime) return;\n')
233     f.write('  firsttime = 0;\n')
234     emit_all_inits(f)
235     f.write('}\n')
236
237
238
239
240 def epilog(f: 'T.TextIO'):
241     f.write('#include "indices/u_unfilled_indices.c"\n')
242
243
244 def main():
245     parser = argparse.ArgumentParser()
246     parser.add_argument('output')
247     args = parser.parse_args()
248
249     with open(args.output, 'w') as f:
250         prolog(f)
251         emit_funcs(f)
252         emit_init(f)
253         epilog(f)
254
255
256 if __name__ == '__main__':
257     main()