gen_extension_headers.py 3.6 KB

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