Path: blob/21.2-virgl/src/gallium/drivers/swr/rasterizer/codegen/gen_backends.py
4574 views
# Copyright (C) 2017-2018 Intel Corporation. All Rights Reserved.1#2# Permission is hereby granted, free of charge, to any person obtaining a3# copy of this software and associated documentation files (the 'Software'),4# to deal in the Software without restriction, including without limitation5# the rights to use, copy, modify, merge, publish, distribute, sublicense,6# and/or sell copies of the Software, and to permit persons to whom the7# Software is furnished to do so, subject to the following conditions:8#9# The above copyright notice and this permission notice (including the next10# paragraph) shall be included in all copies or substantial portions of the11# Software.12#13# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER17# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING18# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS19# IN THE SOFTWARE.2021# Python source22# Compatible with Python2.X and Python3.X2324from __future__ import print_function25import itertools26import os27import sys28from gen_common import *293031def main(args=sys.argv[1:]):32thisDir = os.path.dirname(os.path.realpath(__file__))33parser = ArgumentParser('Generate files and initialization functions for all permutations of BackendPixelRate.')34parser.add_argument('--dim', help='gBackendPixelRateTable array dimensions', nargs='+', type=int, required=True)35parser.add_argument('--outdir', help='output directory', nargs='?', type=str, default=thisDir)36parser.add_argument('--split', help='how many lines of initialization per file [0=no split]', nargs='?', type=int, default='512')37parser.add_argument('--numfiles', help='how many output files to generate', nargs='?', type=int, default='0')38parser.add_argument('--cpp', help='Generate cpp file(s)', action='store_true', default=False)39parser.add_argument('--hpp', help='Generate hpp file', action='store_true', default=False)40parser.add_argument('--cmake', help='Generate cmake file', action='store_true', default=False)41parser.add_argument('--rast', help='Generate rasterizer functions instead of normal backend', action='store_true', default=False)4243args = parser.parse_args(args)444546class backendStrs :47def __init__(self) :48self.outFileName = 'gen_BackendPixelRate%s.cpp'49self.outHeaderName = 'gen_BackendPixelRate.hpp'50self.functionTableName = 'gBackendPixelRateTable'51self.funcInstanceHeader = ' = BackendPixelRate<SwrBackendTraits<'52self.template = 'gen_backend.cpp'53self.hpp_template = 'gen_header_init.hpp'54self.cmakeFileName = 'gen_backends.cmake'55self.cmakeSrcVar = 'GEN_BACKEND_SOURCES'56self.tableName = 'BackendPixelRate'5758if args.rast:59self.outFileName = 'gen_rasterizer%s.cpp'60self.outHeaderName = 'gen_rasterizer.hpp'61self.functionTableName = 'gRasterizerFuncs'62self.funcInstanceHeader = ' = RasterizeTriangle<RasterizerTraits<'63self.template = 'gen_rasterizer.cpp'64self.cmakeFileName = 'gen_rasterizer.cmake'65self.cmakeSrcVar = 'GEN_RASTERIZER_SOURCES'66self.tableName = 'RasterizerFuncs'676869backend = backendStrs()7071output_list = []72for x in args.dim:73output_list.append(list(range(x)))7475# generate all permutations possible for template parameter inputs76output_combinations = list(itertools.product(*output_list))77output_list = []7879# for each permutation80for x in range(len(output_combinations)):81# separate each template peram into its own list member82new_list = [output_combinations[x][i] for i in range(len(output_combinations[x]))]83tempStr = backend.functionTableName84#print each list member as an index in the multidimensional array85for i in new_list:86tempStr += '[' + str(i) + ']'87#map each entry in the permutation as its own string member, store as the template instantiation string88tempStr += backend.funcInstanceHeader + ','.join(map(str, output_combinations[x])) + '>>;'89#append the line of c++ code in the list of output lines90output_list.append(tempStr)9192# how many files should we split the global template initialization into?93if (args.split == 0):94numFiles = 195else:96numFiles = (len(output_list) + args.split - 1) // args.split97if (args.numfiles != 0):98numFiles = args.numfiles99linesPerFile = (len(output_list) + numFiles - 1) // numFiles100chunkedList = [output_list[x:x+linesPerFile] for x in range(0, len(output_list), linesPerFile)]101102tmp_output_dir = MakeTmpDir('_codegen')103104if not os.path.exists(args.outdir):105try:106os.makedirs(args.outdir)107except OSError as err:108if err.errno != errno.EEXIST:109print('ERROR: Could not create directory:', args.outdir, file=sys.stderr)110return 1111112rval = 0113114# generate .cpp files115try:116if args.cpp:117baseCppName = os.path.join(tmp_output_dir, backend.outFileName)118templateCpp = os.path.join(thisDir, 'templates', backend.template)119120for fileNum in range(numFiles):121filename = baseCppName % str(fileNum)122MakoTemplateWriter.to_file(123templateCpp,124baseCppName % str(fileNum),125cmdline=sys.argv,126fileNum=fileNum,127funcList=chunkedList[fileNum])128129if args.hpp:130baseHppName = os.path.join(tmp_output_dir, backend.outHeaderName)131templateHpp = os.path.join(thisDir, 'templates', backend.hpp_template)132133MakoTemplateWriter.to_file(134templateHpp,135baseHppName,136cmdline=sys.argv,137numFiles=numFiles,138filename=backend.outHeaderName,139tableName=backend.tableName)140141# generate gen_backend.cmake file142if args.cmake:143templateCmake = os.path.join(thisDir, 'templates', 'gen_backend.cmake')144cmakeFile = os.path.join(tmp_output_dir, backend.cmakeFileName)145146MakoTemplateWriter.to_file(147templateCmake,148cmakeFile,149cmdline=sys.argv,150srcVar=backend.cmakeSrcVar,151numFiles=numFiles,152baseCppName='${RASTY_GEN_SRC_DIR}/backends/' + os.path.basename(baseCppName))153154rval = CopyDirFilesIfDifferent(tmp_output_dir, args.outdir)155156except:157rval = 1158159finally:160DeleteDirTree(tmp_output_dir)161162return rval163164if __name__ == '__main__':165sys.exit(main())166167168