swr/rast: minimize codegen redundant work
[mesa.git] / src / gallium / drivers / swr / rasterizer / codegen / gen_backends.py
1 # Copyright (C) 2017-2018 Intel Corporation. All Rights Reserved.
2 #
3 # Permission is hereby granted, free of charge, to any person obtaining a
4 # copy of this software and associated documentation files (the 'Software'),
5 # to deal in the Software without restriction, including without limitation
6 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 # and/or sell copies of the Software, and to permit persons to whom the
8 # Software is furnished to do so, subject to the following conditions:
9 #
10 # The above copyright notice and this permission notice (including the next
11 # paragraph) shall be included in all copies or substantial portions of the
12 # Software.
13 #
14 # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 # IN THE SOFTWARE.
21
22 # Python source
23 # Compatible with Python2.X and Python3.X
24
25 from __future__ import print_function
26 import itertools
27 import os
28 import sys
29 from gen_common import *
30
31
32 def main(args=sys.argv[1:]):
33 thisDir = os.path.dirname(os.path.realpath(__file__))
34 parser = ArgumentParser('Generate files and initialization functions for all permutuations of BackendPixelRate.')
35 parser.add_argument('--dim', help='gBackendPixelRateTable array dimensions', nargs='+', type=int, required=True)
36 parser.add_argument('--outdir', help='output directory', nargs='?', type=str, default=thisDir)
37 parser.add_argument('--split', help='how many lines of initialization per file [0=no split]', nargs='?', type=int, default='512')
38 parser.add_argument('--numfiles', help='how many output files to generate', nargs='?', type=int, default='0')
39 parser.add_argument('--cpp', help='Generate cpp file(s)', action='store_true', default=False)
40 parser.add_argument('--hpp', help='Generate hpp file', action='store_true', default=False)
41 parser.add_argument('--cmake', help='Generate cmake file', action='store_true', default=False)
42 parser.add_argument('--rast', help='Generate rasterizer functions instead of normal backend', action='store_true', default=False)
43
44 args = parser.parse_args(args)
45
46
47 class backendStrs :
48 def __init__(self) :
49 self.outFileName = 'gen_BackendPixelRate%s.cpp'
50 self.outHeaderName = 'gen_BackendPixelRate.hpp'
51 self.functionTableName = 'gBackendPixelRateTable'
52 self.funcInstanceHeader = ' = BackendPixelRate<SwrBackendTraits<'
53 self.template = 'gen_backend.cpp'
54 self.hpp_template = 'gen_header_init.hpp'
55 self.cmakeFileName = 'gen_backends.cmake'
56 self.cmakeSrcVar = 'GEN_BACKEND_SOURCES'
57 self.tableName = 'BackendPixelRate'
58
59 if args.rast:
60 self.outFileName = 'gen_rasterizer%s.cpp'
61 self.outHeaderName = 'gen_rasterizer.hpp'
62 self.functionTableName = 'gRasterizerFuncs'
63 self.funcInstanceHeader = ' = RasterizeTriangle<RasterizerTraits<'
64 self.template = 'gen_rasterizer.cpp'
65 self.cmakeFileName = 'gen_rasterizer.cmake'
66 self.cmakeSrcVar = 'GEN_RASTERIZER_SOURCES'
67 self.tableName = 'RasterizerFuncs'
68
69
70 backend = backendStrs()
71
72 output_list = []
73 for x in args.dim:
74 output_list.append(list(range(x)))
75
76 # generate all permutations possible for template parameter inputs
77 output_combinations = list(itertools.product(*output_list))
78 output_list = []
79
80 # for each permutation
81 for x in range(len(output_combinations)):
82 # separate each template peram into its own list member
83 new_list = [output_combinations[x][i] for i in range(len(output_combinations[x]))]
84 tempStr = backend.functionTableName
85 #print each list member as an index in the multidimensional array
86 for i in new_list:
87 tempStr += '[' + str(i) + ']'
88 #map each entry in the permuation as its own string member, store as the template instantiation string
89 tempStr += backend.funcInstanceHeader + ','.join(map(str, output_combinations[x])) + '>>;'
90 #append the line of c++ code in the list of output lines
91 output_list.append(tempStr)
92
93 # how many files should we split the global template initialization into?
94 if (args.split == 0):
95 numFiles = 1
96 else:
97 numFiles = (len(output_list) + args.split - 1) // args.split
98 if (args.numfiles != 0):
99 numFiles = args.numfiles
100 linesPerFile = (len(output_list) + numFiles - 1) // numFiles
101 chunkedList = [output_list[x:x+linesPerFile] for x in range(0, len(output_list), linesPerFile)]
102
103 tmp_output_dir = MakeTmpDir('_codegen')
104
105 if not os.path.exists(args.outdir):
106 try:
107 os.makedirs(args.outdir)
108 except OSError as err:
109 if err.errno != errno.EEXIST:
110 print('ERROR: Could not create directory:', args.outdir, file=sys.stderr)
111 return 1
112
113 rval = 0
114
115 # generate .cpp files
116 try:
117 if args.cpp:
118 baseCppName = os.path.join(tmp_output_dir, backend.outFileName)
119 templateCpp = os.path.join(thisDir, 'templates', backend.template)
120
121 for fileNum in range(numFiles):
122 filename = baseCppName % str(fileNum)
123 MakoTemplateWriter.to_file(
124 templateCpp,
125 baseCppName % str(fileNum),
126 cmdline=sys.argv,
127 fileNum=fileNum,
128 funcList=chunkedList[fileNum])
129
130 if args.hpp:
131 baseHppName = os.path.join(tmp_output_dir, backend.outHeaderName)
132 templateHpp = os.path.join(thisDir, 'templates', backend.hpp_template)
133
134 MakoTemplateWriter.to_file(
135 templateHpp,
136 baseHppName,
137 cmdline=sys.argv,
138 numFiles=numFiles,
139 filename=backend.outHeaderName,
140 tableName=backend.tableName)
141
142 # generate gen_backend.cmake file
143 if args.cmake:
144 templateCmake = os.path.join(thisDir, 'templates', 'gen_backend.cmake')
145 cmakeFile = os.path.join(tmp_output_dir, backend.cmakeFileName)
146
147 MakoTemplateWriter.to_file(
148 templateCmake,
149 cmakeFile,
150 cmdline=sys.argv,
151 srcVar=backend.cmakeSrcVar,
152 numFiles=numFiles,
153 baseCppName='${RASTY_GEN_SRC_DIR}/backends/' + os.path.basename(baseCppName))
154
155 rval = CopyDirFilesIfDifferent(tmp_output_dir, args.outdir)
156
157 except:
158 rval = 1
159
160 finally:
161 DeleteDirTree(tmp_output_dir)
162
163 return rval
164
165 if __name__ == '__main__':
166 sys.exit(main())