Path: blob/21.2-virgl/src/gallium/auxiliary/indices/u_unfilled_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'3031INTYPES = (GENERATE, UBYTE, USHORT, UINT)32OUTTYPES = (USHORT, UINT)33PRIMS=('tris',34'trifan',35'tristrip',36'quads',37'quadstrip',38'polygon',39'trisadj',40'tristripadj')4142LONGPRIMS=('PIPE_PRIM_TRIANGLES',43'PIPE_PRIM_TRIANGLE_FAN',44'PIPE_PRIM_TRIANGLE_STRIP',45'PIPE_PRIM_QUADS',46'PIPE_PRIM_QUAD_STRIP',47'PIPE_PRIM_POLYGON',48'PIPE_PRIM_TRIANGLES_ADJACENCY',49'PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY')5051longprim = dict(zip(PRIMS, LONGPRIMS))52intype_idx = dict(ubyte='IN_UBYTE', ushort='IN_USHORT', uint='IN_UINT')53outtype_idx = dict(ushort='OUT_USHORT', uint='OUT_UINT')545556def prolog():57print('''/* File automatically generated by u_unfilled_gen.py */''')58print(copyright)59print(r'''6061/**62* @file63* Functions to translate and generate index lists64*/6566#include "indices/u_indices.h"67#include "indices/u_indices_priv.h"68#include "pipe/p_compiler.h"69#include "util/u_debug.h"70#include "pipe/p_defines.h"71#include "util/u_memory.h"727374static unsigned out_size_idx( unsigned index_size )75{76switch (index_size) {77case 4: return OUT_UINT;78case 2: return OUT_USHORT;79default: assert(0); return OUT_USHORT;80}81}8283static unsigned in_size_idx( unsigned index_size )84{85switch (index_size) {86case 4: return IN_UINT;87case 2: return IN_USHORT;88case 1: return IN_UBYTE;89default: assert(0); return IN_UBYTE;90}91}929394static u_generate_func generate_line[OUT_COUNT][PRIM_COUNT];95static u_translate_func translate_line[IN_COUNT][OUT_COUNT][PRIM_COUNT];9697''')9899def vert( intype, outtype, v0 ):100if intype == GENERATE:101return '(' + outtype + ')(' + v0 + ')'102else:103return '(' + outtype + ')in[' + v0 + ']'104105def line( intype, outtype, ptr, v0, v1 ):106print(' (' + ptr + ')[0] = ' + vert( intype, outtype, v0 ) + ';')107print(' (' + ptr + ')[1] = ' + vert( intype, outtype, v1 ) + ';')108109# XXX: have the opportunity here to avoid over-drawing shared lines in110# tristrips, fans, etc, by integrating this into the calling functions111# and only emitting each line at most once.112#113def do_tri( intype, outtype, ptr, v0, v1, v2 ):114line( intype, outtype, ptr, v0, v1 )115line( intype, outtype, ptr + '+2', v1, v2 )116line( intype, outtype, ptr + '+4', v2, v0 )117118def do_quad( intype, outtype, ptr, v0, v1, v2, v3 ):119line( intype, outtype, ptr, v0, v1 )120line( intype, outtype, ptr + '+2', v1, v2 )121line( intype, outtype, ptr + '+4', v2, v3 )122line( intype, outtype, ptr + '+6', v3, v0 )123124def name(intype, outtype, prim):125if intype == GENERATE:126return 'generate_' + prim + '_' + outtype127else:128return 'translate_' + prim + '_' + intype + '2' + outtype129130def preamble(intype, outtype, prim):131print('static void ' + name( intype, outtype, prim ) + '(')132if intype != GENERATE:133print(' const void * _in,')134print(' unsigned start,')135if intype != GENERATE:136print(' unsigned in_nr,')137print(' unsigned out_nr,')138if intype != GENERATE:139print(' unsigned restart_index,')140print(' void *_out )')141print('{')142if intype != GENERATE:143print(' const ' + intype + '*in = (const ' + intype + '*)_in;')144print(' ' + outtype + ' *out = (' + outtype + '*)_out;')145print(' unsigned i, j;')146print(' (void)j;')147148def postamble():149print('}')150151152def tris(intype, outtype):153preamble(intype, outtype, prim='tris')154print(' for (i = start, j = 0; j < out_nr; j+=6, i+=3) { ')155do_tri( intype, outtype, 'out+j', 'i', 'i+1', 'i+2' );156print(' }')157postamble()158159160def tristrip(intype, outtype):161preamble(intype, outtype, prim='tristrip')162print(' for (i = start, j = 0; j < out_nr; j+=6, i++) { ')163do_tri( intype, outtype, 'out+j', 'i', 'i+1/*+(i&1)*/', 'i+2/*-(i&1)*/' );164print(' }')165postamble()166167168def trifan(intype, outtype):169preamble(intype, outtype, prim='trifan')170print(' for (i = start, j = 0; j < out_nr; j+=6, i++) { ')171do_tri( intype, outtype, 'out+j', '0', 'i+1', 'i+2' );172print(' }')173postamble()174175176177def polygon(intype, outtype):178preamble(intype, outtype, prim='polygon')179print(' for (i = start, j = 0; j < out_nr; j+=2, i++) { ')180line( intype, outtype, 'out+j', 'i', '(i+1)%(out_nr/2)' )181print(' }')182postamble()183184185def quads(intype, outtype):186preamble(intype, outtype, prim='quads')187print(' for (i = start, j = 0; j < out_nr; j+=8, i+=4) { ')188do_quad( intype, outtype, 'out+j', 'i+0', 'i+1', 'i+2', 'i+3' );189print(' }')190postamble()191192193def quadstrip(intype, outtype):194preamble(intype, outtype, prim='quadstrip')195print(' for (i = start, j = 0; j < out_nr; j+=8, i+=2) { ')196do_quad( intype, outtype, 'out+j', 'i+2', 'i+0', 'i+1', 'i+3' );197print(' }')198postamble()199200201def trisadj(intype, outtype):202preamble(intype, outtype, prim='trisadj')203print(' for (i = start, j = 0; j < out_nr; j+=6, i+=6) { ')204do_tri( intype, outtype, 'out+j', 'i', 'i+2', 'i+4' );205print(' }')206postamble()207208209def tristripadj(intype, outtype):210preamble(intype, outtype, prim='tristripadj')211print(' for (i = start, j = 0; j < out_nr; j+=6, i+=2) { ')212do_tri( intype, outtype, 'out+j', 'i', 'i+2', 'i+4' );213print(' }')214postamble()215216217def emit_funcs():218for intype in INTYPES:219for outtype in OUTTYPES:220tris(intype, outtype)221tristrip(intype, outtype)222trifan(intype, outtype)223quads(intype, outtype)224quadstrip(intype, outtype)225polygon(intype, outtype)226trisadj(intype, outtype)227tristripadj(intype, outtype)228229def init(intype, outtype, prim):230if intype == GENERATE:231print(('generate_line[' +232outtype_idx[outtype] +233'][' + longprim[prim] +234'] = ' + name( intype, outtype, prim ) + ';'))235else:236print(('translate_line[' +237intype_idx[intype] +238'][' + outtype_idx[outtype] +239'][' + longprim[prim] +240'] = ' + name( intype, outtype, prim ) + ';'))241242243def emit_all_inits():244for intype in INTYPES:245for outtype in OUTTYPES:246for prim in PRIMS:247init(intype, outtype, prim)248249def emit_init():250print('void u_unfilled_init( void )')251print('{')252print(' static int firsttime = 1;')253print(' if (!firsttime) return;')254print(' firsttime = 0;')255emit_all_inits()256print('}')257258259260261def epilog():262print('#include "indices/u_unfilled_indices.c"')263264265def main():266prolog()267emit_funcs()268emit_init()269epilog()270271272if __name__ == '__main__':273main()274275276