1from __future__ import print_function 2 3copyright = ''' 4/* 5 * Copyright 2009 VMware, Inc. 6 * All Rights Reserved. 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining a 9 * copy of this software and associated documentation files (the "Software"), 10 * to deal in the Software without restriction, including without limitation 11 * on the rights to use, copy, modify, merge, publish, distribute, sub 12 * license, and/or sell copies of the Software, and to permit persons to whom 13 * the Software is furnished to do so, subject to the following conditions: 14 * 15 * The above copyright notice and this permission notice (including the next 16 * paragraph) shall be included in all copies or substantial portions of the 17 * Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 22 * VMWARE AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, 23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 25 * USE OR OTHER DEALINGS IN THE SOFTWARE. 26 */ 27''' 28 29GENERATE, UBYTE, USHORT, UINT = 'generate', 'ubyte', 'ushort', 'uint' 30FIRST, LAST = 'first', 'last' 31PRDISABLE, PRENABLE = 'prdisable', 'prenable' 32 33INTYPES = (GENERATE, UBYTE, USHORT, UINT) 34OUTTYPES = (USHORT, UINT) 35PVS=(FIRST, LAST) 36PRS=(PRDISABLE, PRENABLE) 37PRIMS=('points', 38 'lines', 39 'linestrip', 40 'lineloop', 41 'tris', 42 'trifan', 43 'tristrip', 44 'quads', 45 'quadstrip', 46 'polygon', 47 'linesadj', 48 'linestripadj', 49 'trisadj', 50 'tristripadj') 51 52LONGPRIMS=('PIPE_PRIM_POINTS', 53 'PIPE_PRIM_LINES', 54 'PIPE_PRIM_LINE_STRIP', 55 'PIPE_PRIM_LINE_LOOP', 56 'PIPE_PRIM_TRIANGLES', 57 'PIPE_PRIM_TRIANGLE_FAN', 58 'PIPE_PRIM_TRIANGLE_STRIP', 59 'PIPE_PRIM_QUADS', 60 'PIPE_PRIM_QUAD_STRIP', 61 'PIPE_PRIM_POLYGON', 62 'PIPE_PRIM_LINES_ADJACENCY', 63 'PIPE_PRIM_LINE_STRIP_ADJACENCY', 64 'PIPE_PRIM_TRIANGLES_ADJACENCY', 65 'PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY') 66 67longprim = dict(zip(PRIMS, LONGPRIMS)) 68intype_idx = dict(ubyte='IN_UBYTE', ushort='IN_USHORT', uint='IN_UINT') 69outtype_idx = dict(ushort='OUT_USHORT', uint='OUT_UINT') 70pv_idx = dict(first='PV_FIRST', last='PV_LAST') 71pr_idx = dict(prdisable='PR_DISABLE', prenable='PR_ENABLE') 72 73def prolog(): 74 print('''/* File automatically generated by u_indices_gen.py */''') 75 print(copyright) 76 print(r''' 77 78/** 79 * @file 80 * Functions to translate and generate index lists 81 */ 82 83#include "indices/u_indices_priv.h" 84#include "util/u_debug.h" 85#include "util/u_memory.h" 86 87 88static unsigned out_size_idx( unsigned index_size ) 89{ 90 switch (index_size) { 91 case 4: return OUT_UINT; 92 case 2: return OUT_USHORT; 93 default: assert(0); return OUT_USHORT; 94 } 95} 96 97static unsigned in_size_idx( unsigned index_size ) 98{ 99 switch (index_size) { 100 case 4: return IN_UINT; 101 case 2: return IN_USHORT; 102 case 1: return IN_UBYTE; 103 default: assert(0); return IN_UBYTE; 104 } 105} 106 107 108static u_translate_func translate[IN_COUNT][OUT_COUNT][PV_COUNT][PV_COUNT][PR_COUNT][PRIM_COUNT]; 109static u_generate_func generate[OUT_COUNT][PV_COUNT][PV_COUNT][PRIM_COUNT]; 110 111 112''') 113 114def vert( intype, outtype, v0 ): 115 if intype == GENERATE: 116 return '(' + outtype + ')(' + v0 + ')' 117 else: 118 return '(' + outtype + ')in[' + v0 + ']' 119 120def point( intype, outtype, ptr, v0 ): 121 print(' (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';') 122 123def line( intype, outtype, ptr, v0, v1 ): 124 print(' (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';') 125 print(' (' + ptr + ')[1] = ' + vert( intype, outtype, v1 ) + ';') 126 127def tri( intype, outtype, ptr, v0, v1, v2 ): 128 print(' (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';') 129 print(' (' + ptr + ')[1] = ' + vert( intype, outtype, v1 ) + ';') 130 print(' (' + ptr + ')[2] = ' + vert( intype, outtype, v2 ) + ';') 131 132def lineadj( intype, outtype, ptr, v0, v1, v2, v3 ): 133 print(' (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';') 134 print(' (' + ptr + ')[1] = ' + vert( intype, outtype, v1 ) + ';') 135 print(' (' + ptr + ')[2] = ' + vert( intype, outtype, v2 ) + ';') 136 print(' (' + ptr + ')[3] = ' + vert( intype, outtype, v3 ) + ';') 137 138def triadj( intype, outtype, ptr, v0, v1, v2, v3, v4, v5 ): 139 print(' (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';') 140 print(' (' + ptr + ')[1] = ' + vert( intype, outtype, v1 ) + ';') 141 print(' (' + ptr + ')[2] = ' + vert( intype, outtype, v2 ) + ';') 142 print(' (' + ptr + ')[3] = ' + vert( intype, outtype, v3 ) + ';') 143 print(' (' + ptr + ')[4] = ' + vert( intype, outtype, v4 ) + ';') 144 print(' (' + ptr + ')[5] = ' + vert( intype, outtype, v5 ) + ';') 145 146def do_point( intype, outtype, ptr, v0 ): 147 point( intype, outtype, ptr, v0 ) 148 149def do_line( intype, outtype, ptr, v0, v1, inpv, outpv ): 150 if inpv == outpv: 151 line( intype, outtype, ptr, v0, v1 ) 152 else: 153 line( intype, outtype, ptr, v1, v0 ) 154 155def do_tri( intype, outtype, ptr, v0, v1, v2, inpv, outpv ): 156 if inpv == outpv: 157 tri( intype, outtype, ptr, v0, v1, v2 ) 158 else: 159 if inpv == FIRST: 160 tri( intype, outtype, ptr, v1, v2, v0 ) 161 else: 162 tri( intype, outtype, ptr, v2, v0, v1 ) 163 164def do_quad( intype, outtype, ptr, v0, v1, v2, v3, inpv, outpv ): 165 if inpv == LAST: 166 do_tri( intype, outtype, ptr+'+0', v0, v1, v3, inpv, outpv ); 167 do_tri( intype, outtype, ptr+'+3', v1, v2, v3, inpv, outpv ); 168 else: 169 do_tri( intype, outtype, ptr+'+0', v0, v1, v2, inpv, outpv ); 170 do_tri( intype, outtype, ptr+'+3', v0, v2, v3, inpv, outpv ); 171 172def do_lineadj( intype, outtype, ptr, v0, v1, v2, v3, inpv, outpv ): 173 if inpv == outpv: 174 lineadj( intype, outtype, ptr, v0, v1, v2, v3 ) 175 else: 176 lineadj( intype, outtype, ptr, v3, v2, v1, v0 ) 177 178def do_triadj( intype, outtype, ptr, v0, v1, v2, v3, v4, v5, inpv, outpv ): 179 if inpv == outpv: 180 triadj( intype, outtype, ptr, v0, v1, v2, v3, v4, v5 ) 181 else: 182 triadj( intype, outtype, ptr, v4, v5, v0, v1, v2, v3 ) 183 184def name(intype, outtype, inpv, outpv, pr, prim): 185 if intype == GENERATE: 186 return 'generate_' + prim + '_' + outtype + '_' + inpv + '2' + outpv 187 else: 188 return 'translate_' + prim + '_' + intype + '2' + outtype + '_' + inpv + '2' + outpv + '_' + pr 189 190def preamble(intype, outtype, inpv, outpv, pr, prim): 191 print('static void ' + name( intype, outtype, inpv, outpv, pr, prim ) + '(') 192 if intype != GENERATE: 193 print(' const void * _in,') 194 print(' unsigned start,') 195 if intype != GENERATE: 196 print(' unsigned in_nr,') 197 print(' unsigned out_nr,') 198 if intype != GENERATE: 199 print(' unsigned restart_index,') 200 print(' void *_out )') 201 print('{') 202 if intype != GENERATE: 203 print(' const ' + intype + '*in = (const ' + intype + '*)_in;') 204 print(' ' + outtype + ' *out = (' + outtype + '*)_out;') 205 print(' unsigned i, j;') 206 print(' (void)j;') 207 208def postamble(): 209 print('}') 210 211def prim_restart(in_verts, out_verts, out_prims, close_func = None): 212 print('restart:') 213 print(' if (i + ' + str(in_verts) + ' > in_nr) {') 214 for i in range(out_prims): 215 for j in range(out_verts): 216 print(' (out+j+' + str(out_verts * i) + ')[' + str(j) + '] = restart_index;') 217 print(' continue;') 218 print(' }') 219 for i in range(in_verts): 220 print(' if (in[i + ' + str(i) + '] == restart_index) {') 221 print(' i += ' + str(i + 1) + ';') 222 223 if close_func is not None: 224 close_func(i) 225 226 print(' goto restart;') 227 print(' }') 228 229def points(intype, outtype, inpv, outpv, pr): 230 preamble(intype, outtype, inpv, outpv, pr, prim='points') 231 print(' for (i = start, j = 0; j < out_nr; j++, i++) { ') 232 do_point( intype, outtype, 'out+j', 'i' ); 233 print(' }') 234 postamble() 235 236def lines(intype, outtype, inpv, outpv, pr): 237 preamble(intype, outtype, inpv, outpv, pr, prim='lines') 238 print(' for (i = start, j = 0; j < out_nr; j+=2, i+=2) { ') 239 do_line( intype, outtype, 'out+j', 'i', 'i+1', inpv, outpv ); 240 print(' }') 241 postamble() 242 243def linestrip(intype, outtype, inpv, outpv, pr): 244 preamble(intype, outtype, inpv, outpv, pr, prim='linestrip') 245 print(' for (i = start, j = 0; j < out_nr; j+=2, i++) { ') 246 do_line( intype, outtype, 'out+j', 'i', 'i+1', inpv, outpv ); 247 print(' }') 248 postamble() 249 250def lineloop(intype, outtype, inpv, outpv, pr): 251 preamble(intype, outtype, inpv, outpv, pr, prim='lineloop') 252 print(' unsigned end = start;') 253 print(' for (i = start, j = 0; j < out_nr - 2; j+=2, i++) { ') 254 if pr == PRENABLE: 255 def close_func(index): 256 do_line( intype, outtype, 'out+j', 'end', 'start', inpv, outpv ) 257 print(' start = i;') 258 print(' end = start;') 259 print(' j += 2;') 260 261 prim_restart(2, 2, 1, close_func) 262 263 do_line( intype, outtype, 'out+j', 'i', 'i+1', inpv, outpv ); 264 print(' end = i+1;') 265 print(' }') 266 do_line( intype, outtype, 'out+j', 'end', 'start', inpv, outpv ); 267 postamble() 268 269def tris(intype, outtype, inpv, outpv, pr): 270 preamble(intype, outtype, inpv, outpv, pr, prim='tris') 271 print(' for (i = start, j = 0; j < out_nr; j+=3, i+=3) { ') 272 do_tri( intype, outtype, 'out+j', 'i', 'i+1', 'i+2', inpv, outpv ); 273 print(' }') 274 postamble() 275 276 277def tristrip(intype, outtype, inpv, outpv, pr): 278 preamble(intype, outtype, inpv, outpv, pr, prim='tristrip') 279 print(' for (i = start, j = 0; j < out_nr; j+=3, i++) { ') 280 if inpv == FIRST: 281 do_tri( intype, outtype, 'out+j', 'i', 'i+1+(i&1)', 'i+2-(i&1)', inpv, outpv ); 282 else: 283 do_tri( intype, outtype, 'out+j', 'i+(i&1)', 'i+1-(i&1)', 'i+2', inpv, outpv ); 284 print(' }') 285 postamble() 286 287 288def trifan(intype, outtype, inpv, outpv, pr): 289 preamble(intype, outtype, inpv, outpv, pr, prim='trifan') 290 print(' for (i = start, j = 0; j < out_nr; j+=3, i++) { ') 291 292 if pr == PRENABLE: 293 def close_func(index): 294 print(' start = i;') 295 prim_restart(3, 3, 1, close_func) 296 297 if inpv == FIRST: 298 do_tri( intype, outtype, 'out+j', 'i+1', 'i+2', 'start', inpv, outpv ); 299 else: 300 do_tri( intype, outtype, 'out+j', 'start', 'i+1', 'i+2', inpv, outpv ); 301 302 print(' }') 303 postamble() 304 305 306 307def polygon(intype, outtype, inpv, outpv, pr): 308 preamble(intype, outtype, inpv, outpv, pr, prim='polygon') 309 print(' for (i = start, j = 0; j < out_nr; j+=3, i++) { ') 310 if pr == PRENABLE: 311 def close_func(index): 312 print(' start = i;') 313 prim_restart(3, 3, 1, close_func) 314 315 if inpv == FIRST: 316 do_tri( intype, outtype, 'out+j', 'start', 'i+1', 'i+2', inpv, outpv ); 317 else: 318 do_tri( intype, outtype, 'out+j', 'i+1', 'i+2', 'start', inpv, outpv ); 319 print(' }') 320 postamble() 321 322 323def quads(intype, outtype, inpv, outpv, pr): 324 preamble(intype, outtype, inpv, outpv, pr, prim='quads') 325 print(' for (i = start, j = 0; j < out_nr; j+=6, i+=4) { ') 326 if pr == PRENABLE: 327 prim_restart(4, 3, 2) 328 329 do_quad( intype, outtype, 'out+j', 'i+0', 'i+1', 'i+2', 'i+3', inpv, outpv ); 330 print(' }') 331 postamble() 332 333 334def quadstrip(intype, outtype, inpv, outpv, pr): 335 preamble(intype, outtype, inpv, outpv, pr, prim='quadstrip') 336 print(' for (i = start, j = 0; j < out_nr; j+=6, i+=2) { ') 337 if pr == PRENABLE: 338 prim_restart(4, 3, 2) 339 340 if inpv == LAST: 341 do_quad( intype, outtype, 'out+j', 'i+2', 'i+0', 'i+1', 'i+3', inpv, outpv ); 342 else: 343 do_quad( intype, outtype, 'out+j', 'i+0', 'i+1', 'i+3', 'i+2', inpv, outpv ); 344 print(' }') 345 postamble() 346 347 348def linesadj(intype, outtype, inpv, outpv, pr): 349 preamble(intype, outtype, inpv, outpv, pr, prim='linesadj') 350 print(' for (i = start, j = 0; j < out_nr; j+=4, i+=4) { ') 351 do_lineadj( intype, outtype, 'out+j', 'i+0', 'i+1', 'i+2', 'i+3', inpv, outpv ) 352 print(' }') 353 postamble() 354 355 356def linestripadj(intype, outtype, inpv, outpv, pr): 357 preamble(intype, outtype, inpv, outpv, pr, prim='linestripadj') 358 print(' for (i = start, j = 0; j < out_nr; j+=4, i++) {') 359 do_lineadj( intype, outtype, 'out+j', 'i+0', 'i+1', 'i+2', 'i+3', inpv, outpv ) 360 print(' }') 361 postamble() 362 363 364def trisadj(intype, outtype, inpv, outpv, pr): 365 preamble(intype, outtype, inpv, outpv, pr, prim='trisadj') 366 print(' for (i = start, j = 0; j < out_nr; j+=6, i+=6) { ') 367 do_triadj( intype, outtype, 'out+j', 'i+0', 'i+1', 'i+2', 'i+3', 368 'i+4', 'i+5', inpv, outpv ) 369 print(' }') 370 postamble() 371 372 373def tristripadj(intype, outtype, inpv, outpv, pr): 374 preamble(intype, outtype, inpv, outpv, pr, prim='tristripadj') 375 print(' for (i = start, j = 0; j < out_nr; i+=2, j+=6) { ') 376 print(' if (i % 4 == 0) {') 377 print(' /* even triangle */') 378 do_triadj( intype, outtype, 'out+j', 379 'i+0', 'i+1', 'i+2', 'i+3', 'i+4', 'i+5', inpv, outpv ) 380 print(' } else {') 381 print(' /* odd triangle */') 382 do_triadj( intype, outtype, 'out+j', 383 'i+2', 'i-2', 'i+0', 'i+3', 'i+4', 'i+6', inpv, outpv ) 384 print(' }') 385 print(' }') 386 postamble() 387 388 389def emit_funcs(): 390 for intype in INTYPES: 391 for outtype in OUTTYPES: 392 for inpv in (FIRST, LAST): 393 for outpv in (FIRST, LAST): 394 for pr in (PRDISABLE, PRENABLE): 395 if pr == PRENABLE and intype == GENERATE: 396 continue 397 points(intype, outtype, inpv, outpv, pr) 398 lines(intype, outtype, inpv, outpv, pr) 399 linestrip(intype, outtype, inpv, outpv, pr) 400 lineloop(intype, outtype, inpv, outpv, pr) 401 tris(intype, outtype, inpv, outpv, pr) 402 tristrip(intype, outtype, inpv, outpv, pr) 403 trifan(intype, outtype, inpv, outpv, pr) 404 quads(intype, outtype, inpv, outpv, pr) 405 quadstrip(intype, outtype, inpv, outpv, pr) 406 polygon(intype, outtype, inpv, outpv, pr) 407 linesadj(intype, outtype, inpv, outpv, pr) 408 linestripadj(intype, outtype, inpv, outpv, pr) 409 trisadj(intype, outtype, inpv, outpv, pr) 410 tristripadj(intype, outtype, inpv, outpv, pr) 411 412def init(intype, outtype, inpv, outpv, pr, prim): 413 if intype == GENERATE: 414 print ('generate[' + 415 outtype_idx[outtype] + 416 '][' + pv_idx[inpv] + 417 '][' + pv_idx[outpv] + 418 '][' + longprim[prim] + 419 '] = ' + name( intype, outtype, inpv, outpv, pr, prim ) + ';') 420 else: 421 print ('translate[' + 422 intype_idx[intype] + 423 '][' + outtype_idx[outtype] + 424 '][' + pv_idx[inpv] + 425 '][' + pv_idx[outpv] + 426 '][' + pr_idx[pr] + 427 '][' + longprim[prim] + 428 '] = ' + name( intype, outtype, inpv, outpv, pr, prim ) + ';') 429 430 431def emit_all_inits(): 432 for intype in INTYPES: 433 for outtype in OUTTYPES: 434 for inpv in PVS: 435 for outpv in PVS: 436 for pr in PRS: 437 for prim in PRIMS: 438 init(intype, outtype, inpv, outpv, pr, prim) 439 440def emit_init(): 441 print('void u_index_init( void )') 442 print('{') 443 print(' static int firsttime = 1;') 444 print(' if (!firsttime) return;') 445 print(' firsttime = 0;') 446 emit_all_inits() 447 print('}') 448 449 450 451 452def epilog(): 453 print('#include "indices/u_indices.c"') 454 455 456def main(): 457 prolog() 458 emit_funcs() 459 emit_init() 460 epilog() 461 462 463if __name__ == '__main__': 464 main() 465