• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2
3# Copyright (c) 2020 Google Inc.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import glob
18import sys
19import os
20
21def generate_main(glsl_files, output_header_file):
22    # Write commit ID to output header file
23    with open(output_header_file, "w") as header_file:
24        # Copyright Notice
25        header_string =  '/***************************************************************************\n'
26        header_string += ' *\n'
27        header_string += ' * Copyright (c) 2015-2021 The Khronos Group Inc.\n'
28        header_string += ' * Copyright (c) 2015-2021 Valve Corporation\n'
29        header_string += ' * Copyright (c) 2015-2021 LunarG, Inc.\n'
30        header_string += ' * Copyright (c) 2015-2021 Google Inc.\n'
31        header_string += ' * Copyright (c) 2021 Advanced Micro Devices, Inc.All rights reserved.\n'
32        header_string += ' *\n'
33        header_string += ' ****************************************************************************/\n'
34        header_string += '#pragma once\n\n'
35        header_string += '#ifndef _INTRINSIC_EXTENSION_HEADER_H_\n'
36        header_string += '#define _INTRINSIC_EXTENSION_HEADER_H_\n\n'
37        header_file.write(header_string)
38
39        symbol_name_list = []
40
41        for i in glsl_files:
42            glsl_contents = open(i,"r").read()
43
44            filename = os.path.basename(i)
45            symbol_name = filename.split(".")[0]
46            symbol_name_list.append(symbol_name)
47            header_name = symbol_name + ".h"
48            header_str = 'std::string %s_GLSL = R"(\n%s\n)";\n' % (symbol_name, glsl_contents)
49            header_str += '\n'
50            header_file.write(header_str)
51
52        contents = ''
53        contents += '\n'
54        contents += 'std::string getIntrinsic(const char* const* shaders, int n) {\n'
55        contents += '\tstd::string shaderString = "";\n';
56
57        contents += '\tfor (int i = 0; i < n; i++) {\n'
58
59        for symbol_name in symbol_name_list:
60            contents += '\t\tif (strstr(shaders[i], "%s") != nullptr) {\n'   % (symbol_name)
61            contents += '\t\t    shaderString.append(%s_GLSL);\n' % (symbol_name)
62            contents += '\t\t}\n'
63
64        contents += '\t}\n'
65        contents += '\treturn shaderString;\n';
66        contents += '}\n'
67
68        contents += '\n#endif\n'
69        header_file.write(contents)
70
71def main():
72    if len(sys.argv) < 2:
73        raise Exception("Invalid number of arguments")
74
75    i = 0
76    while i < len(sys.argv):
77        opt = sys.argv[i]
78        i = i + 1
79
80        if opt == "-i" or opt == "-o":
81            if i == len(sys.argv):
82                raise Exception("Expected path after {}".format(opt))
83            val = sys.argv[i]
84            i = i + 1
85            if (opt == "-i"):
86                input_dir = val
87            elif (opt == "-o"):
88                output_file = val
89            else:
90                raise Exception("Unknown flag {}".format(opt))
91
92    glsl_files = glob.glob(input_dir + '/*.glsl')
93
94    # Generate main header
95    generate_main(glsl_files, output_file)
96
97if __name__ == '__main__':
98    main()
99