Path: blob/21.2-virgl/src/gallium/auxiliary/indices/u_indices_gen.py
4565 views
from __future__ import print_function12copyright = '''3/*4* Copyright 2009 VMware, Inc.5* All Rights Reserved.6*7* Permission is hereby granted, free of charge, to any person obtaining a8* copy of this software and associated documentation files (the "Software"),9* to deal in the Software without restriction, including without limitation10* on the rights to use, copy, modify, merge, publish, distribute, sub11* license, and/or sell copies of the Software, and to permit persons to whom12* the Software is furnished to do so, subject to the following conditions:13*14* The above copyright notice and this permission notice (including the next15* paragraph) shall be included in all copies or substantial portions of the16* Software.17*18* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR19* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,20* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL21* VMWARE AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,22* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR23* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE24* USE OR OTHER DEALINGS IN THE SOFTWARE.25*/26'''2728GENERATE, UBYTE, USHORT, UINT = 'generate', 'ubyte', 'ushort', 'uint'29FIRST, LAST = 'first', 'last'30PRDISABLE, PRENABLE = 'prdisable', 'prenable'3132INTYPES = (GENERATE, UBYTE, USHORT, UINT)33OUTTYPES = (USHORT, UINT)34PVS=(FIRST, LAST)35PRS=(PRDISABLE, PRENABLE)36PRIMS=('points',37'lines',38'linestrip',39'lineloop',40'tris',41'trifan',42'tristrip',43'quads',44'quadstrip',45'polygon',46'linesadj',47'linestripadj',48'trisadj',49'tristripadj')5051LONGPRIMS=('PIPE_PRIM_POINTS',52'PIPE_PRIM_LINES',53'PIPE_PRIM_LINE_STRIP',54'PIPE_PRIM_LINE_LOOP',55'PIPE_PRIM_TRIANGLES',56'PIPE_PRIM_TRIANGLE_FAN',57'PIPE_PRIM_TRIANGLE_STRIP',58'PIPE_PRIM_QUADS',59'PIPE_PRIM_QUAD_STRIP',60'PIPE_PRIM_POLYGON',61'PIPE_PRIM_LINES_ADJACENCY',62'PIPE_PRIM_LINE_STRIP_ADJACENCY',63'PIPE_PRIM_TRIANGLES_ADJACENCY',64'PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY')6566longprim = dict(zip(PRIMS, LONGPRIMS))67intype_idx = dict(ubyte='IN_UBYTE', ushort='IN_USHORT', uint='IN_UINT')68outtype_idx = dict(ushort='OUT_USHORT', uint='OUT_UINT')69pv_idx = dict(first='PV_FIRST', last='PV_LAST')70pr_idx = dict(prdisable='PR_DISABLE', prenable='PR_ENABLE')7172def prolog():73print('''/* File automatically generated by u_indices_gen.py */''')74print(copyright)75print(r'''7677/**78* @file79* Functions to translate and generate index lists80*/8182#include "indices/u_indices_priv.h"83#include "util/u_debug.h"84#include "util/u_memory.h"858687static unsigned out_size_idx( unsigned index_size )88{89switch (index_size) {90case 4: return OUT_UINT;91case 2: return OUT_USHORT;92default: assert(0); return OUT_USHORT;93}94}9596static unsigned in_size_idx( unsigned index_size )97{98switch (index_size) {99case 4: return IN_UINT;100case 2: return IN_USHORT;101case 1: return IN_UBYTE;102default: assert(0); return IN_UBYTE;103}104}105106107static u_translate_func translate[IN_COUNT][OUT_COUNT][PV_COUNT][PV_COUNT][PR_COUNT][PRIM_COUNT];108static u_generate_func generate[OUT_COUNT][PV_COUNT][PV_COUNT][PRIM_COUNT];109110111''')112113def vert( intype, outtype, v0 ):114if intype == GENERATE:115return '(' + outtype + ')(' + v0 + ')'116else:117return '(' + outtype + ')in[' + v0 + ']'118119def point( intype, outtype, ptr, v0 ):120print(' (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';')121122def line( intype, outtype, ptr, v0, v1 ):123print(' (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';')124print(' (' + ptr + ')[1] = ' + vert( intype, outtype, v1 ) + ';')125126def tri( intype, outtype, ptr, v0, v1, v2 ):127print(' (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';')128print(' (' + ptr + ')[1] = ' + vert( intype, outtype, v1 ) + ';')129print(' (' + ptr + ')[2] = ' + vert( intype, outtype, v2 ) + ';')130131def lineadj( intype, outtype, ptr, v0, v1, v2, v3 ):132print(' (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';')133print(' (' + ptr + ')[1] = ' + vert( intype, outtype, v1 ) + ';')134print(' (' + ptr + ')[2] = ' + vert( intype, outtype, v2 ) + ';')135print(' (' + ptr + ')[3] = ' + vert( intype, outtype, v3 ) + ';')136137def triadj( intype, outtype, ptr, v0, v1, v2, v3, v4, v5 ):138print(' (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';')139print(' (' + ptr + ')[1] = ' + vert( intype, outtype, v1 ) + ';')140print(' (' + ptr + ')[2] = ' + vert( intype, outtype, v2 ) + ';')141print(' (' + ptr + ')[3] = ' + vert( intype, outtype, v3 ) + ';')142print(' (' + ptr + ')[4] = ' + vert( intype, outtype, v4 ) + ';')143print(' (' + ptr + ')[5] = ' + vert( intype, outtype, v5 ) + ';')144145def do_point( intype, outtype, ptr, v0 ):146point( intype, outtype, ptr, v0 )147148def do_line( intype, outtype, ptr, v0, v1, inpv, outpv ):149if inpv == outpv:150line( intype, outtype, ptr, v0, v1 )151else:152line( intype, outtype, ptr, v1, v0 )153154def do_tri( intype, outtype, ptr, v0, v1, v2, inpv, outpv ):155if inpv == outpv:156tri( intype, outtype, ptr, v0, v1, v2 )157else:158if inpv == FIRST:159tri( intype, outtype, ptr, v1, v2, v0 )160else:161tri( intype, outtype, ptr, v2, v0, v1 )162163def do_quad( intype, outtype, ptr, v0, v1, v2, v3, inpv, outpv ):164if inpv == LAST:165do_tri( intype, outtype, ptr+'+0', v0, v1, v3, inpv, outpv );166do_tri( intype, outtype, ptr+'+3', v1, v2, v3, inpv, outpv );167else:168do_tri( intype, outtype, ptr+'+0', v0, v1, v2, inpv, outpv );169do_tri( intype, outtype, ptr+'+3', v0, v2, v3, inpv, outpv );170171def do_lineadj( intype, outtype, ptr, v0, v1, v2, v3, inpv, outpv ):172if inpv == outpv:173lineadj( intype, outtype, ptr, v0, v1, v2, v3 )174else:175lineadj( intype, outtype, ptr, v3, v2, v1, v0 )176177def do_triadj( intype, outtype, ptr, v0, v1, v2, v3, v4, v5, inpv, outpv ):178if inpv == outpv:179triadj( intype, outtype, ptr, v0, v1, v2, v3, v4, v5 )180else:181triadj( intype, outtype, ptr, v4, v5, v0, v1, v2, v3 )182183def name(intype, outtype, inpv, outpv, pr, prim):184if intype == GENERATE:185return 'generate_' + prim + '_' + outtype + '_' + inpv + '2' + outpv186else:187return 'translate_' + prim + '_' + intype + '2' + outtype + '_' + inpv + '2' + outpv + '_' + pr188189def preamble(intype, outtype, inpv, outpv, pr, prim):190print('static void ' + name( intype, outtype, inpv, outpv, pr, prim ) + '(')191if intype != GENERATE:192print(' const void * restrict _in,')193print(' unsigned start,')194if intype != GENERATE:195print(' unsigned in_nr,')196print(' unsigned out_nr,')197if intype != GENERATE:198print(' unsigned restart_index,')199print(' void * restrict _out )')200print('{')201if intype != GENERATE:202print(' const ' + intype + '* restrict in = (const ' + intype + '* restrict)_in;')203print(' ' + outtype + ' * restrict out = (' + outtype + '* restrict)_out;')204print(' unsigned i, j;')205print(' (void)j;')206207def postamble():208print('}')209210def prim_restart(in_verts, out_verts, out_prims, close_func = None):211print('restart:')212print(' if (i + ' + str(in_verts) + ' > in_nr) {')213for i in range(out_prims):214for j in range(out_verts):215print(' (out+j+' + str(out_verts * i) + ')[' + str(j) + '] = restart_index;')216print(' continue;')217print(' }')218for i in range(in_verts):219print(' if (in[i + ' + str(i) + '] == restart_index) {')220print(' i += ' + str(i + 1) + ';')221222if close_func is not None:223close_func(i)224225print(' goto restart;')226print(' }')227228def points(intype, outtype, inpv, outpv, pr):229preamble(intype, outtype, inpv, outpv, pr, prim='points')230print(' for (i = start, j = 0; j < out_nr; j++, i++) { ')231do_point( intype, outtype, 'out+j', 'i' );232print(' }')233postamble()234235def lines(intype, outtype, inpv, outpv, pr):236preamble(intype, outtype, inpv, outpv, pr, prim='lines')237print(' for (i = start, j = 0; j < out_nr; j+=2, i+=2) { ')238do_line( intype, outtype, 'out+j', 'i', 'i+1', inpv, outpv );239print(' }')240postamble()241242def linestrip(intype, outtype, inpv, outpv, pr):243preamble(intype, outtype, inpv, outpv, pr, prim='linestrip')244print(' for (i = start, j = 0; j < out_nr; j+=2, i++) { ')245do_line( intype, outtype, 'out+j', 'i', 'i+1', inpv, outpv );246print(' }')247postamble()248249def lineloop(intype, outtype, inpv, outpv, pr):250preamble(intype, outtype, inpv, outpv, pr, prim='lineloop')251print(' unsigned end = start;')252print(' for (i = start, j = 0; j < out_nr - 2; j+=2, i++) { ')253if pr == PRENABLE:254def close_func(index):255do_line( intype, outtype, 'out+j', 'end', 'start', inpv, outpv )256print(' start = i;')257print(' end = start;')258print(' j += 2;')259260prim_restart(2, 2, 1, close_func)261262do_line( intype, outtype, 'out+j', 'i', 'i+1', inpv, outpv );263print(' end = i+1;')264print(' }')265do_line( intype, outtype, 'out+j', 'end', 'start', inpv, outpv );266postamble()267268def tris(intype, outtype, inpv, outpv, pr):269preamble(intype, outtype, inpv, outpv, pr, prim='tris')270print(' for (i = start, j = 0; j < out_nr; j+=3, i+=3) { ')271do_tri( intype, outtype, 'out+j', 'i', 'i+1', 'i+2', inpv, outpv );272print(' }')273postamble()274275276def tristrip(intype, outtype, inpv, outpv, pr):277preamble(intype, outtype, inpv, outpv, pr, prim='tristrip')278print(' for (i = start, j = 0; j < out_nr; j+=3, i++) { ')279if inpv == FIRST:280do_tri( intype, outtype, 'out+j', 'i', 'i+1+(i&1)', 'i+2-(i&1)', inpv, outpv );281else:282do_tri( intype, outtype, 'out+j', 'i+(i&1)', 'i+1-(i&1)', 'i+2', inpv, outpv );283print(' }')284postamble()285286287def trifan(intype, outtype, inpv, outpv, pr):288preamble(intype, outtype, inpv, outpv, pr, prim='trifan')289print(' for (i = start, j = 0; j < out_nr; j+=3, i++) { ')290291if pr == PRENABLE:292def close_func(index):293print(' start = i;')294prim_restart(3, 3, 1, close_func)295296if inpv == FIRST:297do_tri( intype, outtype, 'out+j', 'i+1', 'i+2', 'start', inpv, outpv );298else:299do_tri( intype, outtype, 'out+j', 'start', 'i+1', 'i+2', inpv, outpv );300301print(' }')302postamble()303304305306def polygon(intype, outtype, inpv, outpv, pr):307preamble(intype, outtype, inpv, outpv, pr, prim='polygon')308print(' for (i = start, j = 0; j < out_nr; j+=3, i++) { ')309if pr == PRENABLE:310def close_func(index):311print(' start = i;')312prim_restart(3, 3, 1, close_func)313314if inpv == FIRST:315do_tri( intype, outtype, 'out+j', 'start', 'i+1', 'i+2', inpv, outpv );316else:317do_tri( intype, outtype, 'out+j', 'i+1', 'i+2', 'start', inpv, outpv );318print(' }')319postamble()320321322def quads(intype, outtype, inpv, outpv, pr):323preamble(intype, outtype, inpv, outpv, pr, prim='quads')324print(' for (i = start, j = 0; j < out_nr; j+=6, i+=4) { ')325if pr == PRENABLE:326prim_restart(4, 3, 2)327328do_quad( intype, outtype, 'out+j', 'i+0', 'i+1', 'i+2', 'i+3', inpv, outpv );329print(' }')330postamble()331332333def quadstrip(intype, outtype, inpv, outpv, pr):334preamble(intype, outtype, inpv, outpv, pr, prim='quadstrip')335print(' for (i = start, j = 0; j < out_nr; j+=6, i+=2) { ')336if pr == PRENABLE:337prim_restart(4, 3, 2)338339if inpv == LAST:340do_quad( intype, outtype, 'out+j', 'i+2', 'i+0', 'i+1', 'i+3', inpv, outpv );341else:342do_quad( intype, outtype, 'out+j', 'i+0', 'i+1', 'i+3', 'i+2', inpv, outpv );343print(' }')344postamble()345346347def linesadj(intype, outtype, inpv, outpv, pr):348preamble(intype, outtype, inpv, outpv, pr, prim='linesadj')349print(' for (i = start, j = 0; j < out_nr; j+=4, i+=4) { ')350do_lineadj( intype, outtype, 'out+j', 'i+0', 'i+1', 'i+2', 'i+3', inpv, outpv )351print(' }')352postamble()353354355def linestripadj(intype, outtype, inpv, outpv, pr):356preamble(intype, outtype, inpv, outpv, pr, prim='linestripadj')357print(' for (i = start, j = 0; j < out_nr; j+=4, i++) {')358do_lineadj( intype, outtype, 'out+j', 'i+0', 'i+1', 'i+2', 'i+3', inpv, outpv )359print(' }')360postamble()361362363def trisadj(intype, outtype, inpv, outpv, pr):364preamble(intype, outtype, inpv, outpv, pr, prim='trisadj')365print(' for (i = start, j = 0; j < out_nr; j+=6, i+=6) { ')366do_triadj( intype, outtype, 'out+j', 'i+0', 'i+1', 'i+2', 'i+3',367'i+4', 'i+5', inpv, outpv )368print(' }')369postamble()370371372def tristripadj(intype, outtype, inpv, outpv, pr):373preamble(intype, outtype, inpv, outpv, pr, prim='tristripadj')374print(' for (i = start, j = 0; j < out_nr; i+=2, j+=6) { ')375print(' if (i % 4 == 0) {')376print(' /* even triangle */')377do_triadj( intype, outtype, 'out+j',378'i+0', 'i+1', 'i+2', 'i+3', 'i+4', 'i+5', inpv, outpv )379print(' } else {')380print(' /* odd triangle */')381do_triadj( intype, outtype, 'out+j',382'i+2', 'i-2', 'i+0', 'i+3', 'i+4', 'i+6', inpv, outpv )383print(' }')384print(' }')385postamble()386387388def emit_funcs():389for intype in INTYPES:390for outtype in OUTTYPES:391for inpv in (FIRST, LAST):392for outpv in (FIRST, LAST):393for pr in (PRDISABLE, PRENABLE):394if pr == PRENABLE and intype == GENERATE:395continue396points(intype, outtype, inpv, outpv, pr)397lines(intype, outtype, inpv, outpv, pr)398linestrip(intype, outtype, inpv, outpv, pr)399lineloop(intype, outtype, inpv, outpv, pr)400tris(intype, outtype, inpv, outpv, pr)401tristrip(intype, outtype, inpv, outpv, pr)402trifan(intype, outtype, inpv, outpv, pr)403quads(intype, outtype, inpv, outpv, pr)404quadstrip(intype, outtype, inpv, outpv, pr)405polygon(intype, outtype, inpv, outpv, pr)406linesadj(intype, outtype, inpv, outpv, pr)407linestripadj(intype, outtype, inpv, outpv, pr)408trisadj(intype, outtype, inpv, outpv, pr)409tristripadj(intype, outtype, inpv, outpv, pr)410411def init(intype, outtype, inpv, outpv, pr, prim):412if intype == GENERATE:413print ('generate[' +414outtype_idx[outtype] +415'][' + pv_idx[inpv] +416'][' + pv_idx[outpv] +417'][' + longprim[prim] +418'] = ' + name( intype, outtype, inpv, outpv, pr, prim ) + ';')419else:420print ('translate[' +421intype_idx[intype] +422'][' + outtype_idx[outtype] +423'][' + pv_idx[inpv] +424'][' + pv_idx[outpv] +425'][' + pr_idx[pr] +426'][' + longprim[prim] +427'] = ' + name( intype, outtype, inpv, outpv, pr, prim ) + ';')428429430def emit_all_inits():431for intype in INTYPES:432for outtype in OUTTYPES:433for inpv in PVS:434for outpv in PVS:435for pr in PRS:436for prim in PRIMS:437init(intype, outtype, inpv, outpv, pr, prim)438439def emit_init():440print('void u_index_init( void )')441print('{')442print(' static int firsttime = 1;')443print(' if (!firsttime) return;')444print(' firsttime = 0;')445emit_all_inits()446print('}')447448449450451def epilog():452print('#include "indices/u_indices.c"')453454455def main():456prolog()457emit_funcs()458emit_init()459epilog()460461462if __name__ == '__main__':463main()464465466