xref: /aosp_15_r20/external/mesa3d/src/gallium/auxiliary/indices/u_unfilled_gen.py (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1copyright = '''
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
27import argparse
28import itertools
29import typing as T
30
31GENERATE, UINT8, UINT16, UINT32 = 'generate', 'uint8', 'uint16', 'uint32'
32FIRST, LAST = 'first', 'last'
33
34INTYPES = (GENERATE, UINT8, UINT16, UINT32)
35OUTTYPES = (UINT16, UINT32)
36PRIMS=('tris',
37       'trifan',
38       'tristrip',
39       'quads',
40       'quadstrip',
41       'polygon',
42       'trisadj',
43       'tristripadj')
44
45LONGPRIMS=('MESA_PRIM_TRIANGLES',
46           'MESA_PRIM_TRIANGLE_FAN',
47           'MESA_PRIM_TRIANGLE_STRIP',
48           'MESA_PRIM_QUADS',
49           'MESA_PRIM_QUAD_STRIP',
50           'MESA_PRIM_POLYGON',
51           'MESA_PRIM_TRIANGLES_ADJACENCY',
52           'MESA_PRIM_TRIANGLE_STRIP_ADJACENCY')
53
54longprim = dict(zip(PRIMS, LONGPRIMS))
55intype_idx = dict(uint8='IN_UINT8', uint16='IN_UINT16', uint32='IN_UINT32')
56outtype_idx = dict(uint16='OUT_UINT16', uint32='OUT_UINT32')
57
58def prolog(f: 'T.TextIO'):
59    f.write('/* File automatically generated by u_unfilled_gen.py */\n')
60    f.write(copyright)
61    f.write(r'''
62
63/**
64 * @file
65 * Functions to translate and generate index lists
66 */
67
68#include "indices/u_indices.h"
69#include "indices/u_indices_priv.h"
70#include "util/compiler.h"
71#include "util/u_debug.h"
72#include "pipe/p_defines.h"
73#include "util/u_memory.h"
74
75static u_generate_func generate_line[OUT_COUNT][PRIM_COUNT];
76static u_translate_func translate_line[IN_COUNT][OUT_COUNT][PRIM_COUNT];
77
78''')
79
80def vert( intype, outtype, v0 ):
81    if intype == GENERATE:
82        return '(' + outtype + '_t)(' + v0 + ')'
83    else:
84        return '(' + outtype + '_t)in[' + v0 + ']'
85
86def line(f: 'T.TextIO', intype, outtype, ptr, v0, v1 ):
87    f.write('      (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';\n')
88    f.write('      (' + ptr + ')[1] = ' + vert( intype, outtype, v1 ) + ';\n')
89
90# XXX: have the opportunity here to avoid over-drawing shared lines in
91# tristrips, fans, etc, by integrating this into the calling functions
92# and only emitting each line at most once.
93#
94def do_tri(f: 'T.TextIO', intype, outtype, ptr, v0, v1, v2 ):
95    line(f, intype, outtype, ptr, v0, v1 )
96    line(f, intype, outtype, ptr + '+2', v1, v2 )
97    line(f, intype, outtype, ptr + '+4', v2, v0 )
98
99def do_quad(f: 'T.TextIO', intype, outtype, ptr, v0, v1, v2, v3 ):
100    line(f, intype, outtype, ptr, v0, v1 )
101    line(f, intype, outtype, ptr + '+2', v1, v2 )
102    line(f, intype, outtype, ptr + '+4', v2, v3 )
103    line(f, intype, outtype, ptr + '+6', v3, v0 )
104
105def name(intype, outtype, prim):
106    if intype == GENERATE:
107        return 'generate_' + prim + '_' + outtype
108    else:
109        return 'translate_' + prim + '_' + intype + '2' + outtype
110
111def preamble(f: 'T.TextIO', intype, outtype, prim):
112    f.write('static void ' + name( intype, outtype, prim ) + '(\n')
113    if intype != GENERATE:
114        f.write('    const void * _in,\n')
115    f.write('    unsigned start,\n')
116    if intype != GENERATE:
117        f.write('    unsigned in_nr,\n')
118    f.write('    unsigned out_nr,\n')
119    if intype != GENERATE:
120        f.write('    unsigned restart_index,\n')
121    f.write('    void *_out )\n')
122    f.write('{\n')
123    if intype != GENERATE:
124        f.write('  const ' + intype + '_t *in = (const ' + intype + '_t*)_in;\n')
125    f.write('  ' + outtype + '_t *out = (' + outtype + '_t*)_out;\n')
126    f.write('  unsigned i, j;\n')
127    f.write('  (void)j;\n')
128
129def postamble(f: 'T.TextIO'):
130    f.write('}\n')
131
132
133def tris(f: 'T.TextIO', intype, outtype):
134    preamble(f, intype, outtype, prim='tris')
135    f.write('  for (i = start, j = 0; j < out_nr; j+=6, i+=3) {\n')
136    do_tri(f, intype, outtype, 'out+j',  'i', 'i+1', 'i+2' );
137    f.write('   }\n')
138    postamble(f)
139
140
141def tristrip(f: 'T.TextIO', intype, outtype):
142    preamble(f, intype, outtype, prim='tristrip')
143    f.write('  for (i = start, j = 0; j < out_nr; j+=6, i++) {\n')
144    do_tri(f, intype, outtype, 'out+j',  'i', 'i+1/*+(i&1)*/', 'i+2/*-(i&1)*/' );
145    f.write('   }\n')
146    postamble(f)
147
148
149def trifan(f: 'T.TextIO', intype, outtype):
150    preamble(f, intype, outtype, prim='trifan')
151    f.write('  for (i = start, j = 0; j < out_nr; j+=6, i++) {\n')
152    do_tri(f, intype, outtype, 'out+j',  '0', 'i+1', 'i+2' );
153    f.write('   }\n')
154    postamble(f)
155
156
157
158def polygon(f: 'T.TextIO', intype, outtype):
159    preamble(f, intype, outtype, prim='polygon')
160    f.write('  for (i = start, j = 0; j < out_nr; j+=2, i++) {\n')
161    line(f, intype, outtype, 'out+j', 'i', '(i+1)%(out_nr/2)\n' )
162    f.write('   }\n')
163    postamble(f)
164
165
166def quads(f: 'T.TextIO', intype, outtype):
167    preamble(f, intype, outtype, prim='quads')
168    f.write('  for (i = start, j = 0; j < out_nr; j+=8, i+=4) {\n')
169    do_quad(f, intype, outtype, 'out+j', 'i+0', 'i+1', 'i+2', 'i+3' );
170    f.write('   }\n')
171    postamble(f)
172
173
174def quadstrip(f: 'T.TextIO', intype, outtype):
175    preamble(f, intype, outtype, prim='quadstrip')
176    f.write('  for (i = start, j = 0; j < out_nr; j+=8, i+=2) {\n')
177    do_quad(f, intype, outtype, 'out+j', 'i+2', 'i+0', 'i+1', 'i+3' );
178    f.write('   }\n')
179    postamble(f)
180
181
182def trisadj(f: 'T.TextIO', intype, outtype):
183    preamble(f, intype, outtype, prim='trisadj')
184    f.write('  for (i = start, j = 0; j < out_nr; j+=6, i+=6) {\n')
185    do_tri(f, intype, outtype, 'out+j',  'i', 'i+2', 'i+4' );
186    f.write('   }\n')
187    postamble(f)
188
189
190def tristripadj(f: 'T.TextIO', intype, outtype):
191    preamble(f, intype, outtype, prim='tristripadj')
192    f.write('  for (i = start, j = 0; j < out_nr; j+=6, i+=2) {\n')
193    do_tri(f, intype, outtype, 'out+j',  'i', 'i+2', 'i+4' );
194    f.write('   }\n')
195    postamble(f)
196
197
198def emit_funcs(f: 'T.TextIO'):
199    for intype, outtype in itertools.product(INTYPES, OUTTYPES):
200        tris(f, intype, outtype)
201        tristrip(f, intype, outtype)
202        trifan(f, intype, outtype)
203        quads(f, intype, outtype)
204        quadstrip(f, intype, outtype)
205        polygon(f, intype, outtype)
206        trisadj(f, intype, outtype)
207        tristripadj(f, intype, outtype)
208
209def init(f: 'T.TextIO', intype, outtype, prim):
210    if intype == GENERATE:
211        f.write('generate_line[' +
212                outtype_idx[outtype] +
213                '][' + longprim[prim] +
214                '] = ' + name( intype, outtype, prim ) + ';\n')
215    else:
216        f.write('translate_line[' +
217                intype_idx[intype] +
218                '][' + outtype_idx[outtype] +
219                '][' + longprim[prim] +
220                '] = ' + name( intype, outtype, prim ) + ';\n')
221
222
223def emit_all_inits(f: 'T.TextIO'):
224    for intype, outtype, prim in itertools.product(INTYPES, OUTTYPES, PRIMS):
225        init(f, intype, outtype, prim)
226
227def emit_init(f: 'T.TextIO'):
228    f.write('void u_unfilled_init( void )\n')
229    f.write('{\n')
230    f.write('  static int firsttime = 1;\n')
231    f.write('  if (!firsttime) return;\n')
232    f.write('  firsttime = 0;\n')
233    emit_all_inits(f)
234    f.write('}\n')
235
236
237
238
239def epilog(f: 'T.TextIO'):
240    f.write('#include "indices/u_unfilled_indices.c"\n')
241
242
243def main():
244    parser = argparse.ArgumentParser()
245    parser.add_argument('output')
246    args = parser.parse_args()
247
248    with open(args.output, 'w') as f:
249        prolog(f)
250        emit_funcs(f)
251        emit_init(f)
252        epilog(f)
253
254
255if __name__ == '__main__':
256    main()
257