Hash :
6937ea98
Author :
Date :
2022-09-08T14:15:10
Use base name of the generator script in various generated files Increases compiler cache hits especially in cases where the file is run during build. Bug: angleproject:7642 Change-Id: I769dae2d7cca2cf1e238531f4cb356bad41b06dd Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3880323 Reviewed-by: Kenneth Russell <kbr@chromium.org> Commit-Queue: Kenneth Russell <kbr@chromium.org> Auto-Submit: Kimmo Kinnunen <kkinnunen@apple.com> Reviewed-by: Jamie Madill <jmadill@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
#!/usr/bin/python3
# Copyright 2019 The ANGLE Project Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# gen_mtl_internal_shaders.py:
# Code generation for Metal backend's default shaders.
# NOTE: don't run this script directly. Run scripts/run_code_generation.py.
import json
import os
import subprocess
import sys
sys.path.append('../..')
import angle_format
import gen_angle_format_table
template_header_boilerplate = """// GENERATED FILE - DO NOT EDIT.
// Generated by {script_name}
//
// Copyright 2020 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
"""
def gen_shader_enums_code(angle_formats):
code = """// This file is similar to src/libANGLE/renderer/FormatID_autogen.h but is used by Metal default
// shaders instead of C++ code.
//
"""
code += "namespace rx\n"
code += "{\n"
code += "namespace mtl_shader\n"
code += "{\n"
code += "\n"
code += "namespace FormatID\n"
code += "{\n"
code += "enum\n"
code += "{\n"
code += gen_angle_format_table.gen_enum_string(angle_formats) + '\n'
code += "};\n\n"
code += "}\n"
code += "\n"
code += "}\n"
code += "}\n"
return code
def find_clang():
if os.name == 'nt':
binary = 'clang-cl.exe'
else:
binary = 'clang++'
clang = os.path.join('..', '..', '..', '..', '..', 'third_party', 'llvm-build',
'Release+Asserts', 'bin', binary)
if not os.path.isfile(clang):
xcrun_clang = subprocess.run(["xcrun", "-f", binary], stdout=subprocess.PIPE, text=True)
if xcrun_clang.returncode == 0:
clang = xcrun_clang.stdout.strip()
if (not os.path.isfile(clang)):
raise Exception('Cannot find clang')
return clang
def main():
angle_format_script_files = [
'../../angle_format_map.json', '../../angle_format.py', '../../gen_angle_format_table.py'
]
src_files = [
'blit.metal', 'clear.metal', 'gen_indices.metal', 'gen_mipmap.metal', 'copy_buffer.metal',
'visibility.metal', 'rewrite_indices.metal'
]
# auto_script parameters.
if len(sys.argv) > 1:
inputs = angle_format_script_files + src_files + ['common.h', 'constants.h']
outputs = ['format_autogen.h', 'mtl_default_shaders_src_autogen.inc']
if sys.argv[1] == 'inputs':
print(','.join(inputs))
elif sys.argv[1] == 'outputs':
print(','.join(outputs))
else:
print('Invalid script parameters')
return 1
return 0
os.chdir(sys.path[0])
boilerplate_code = template_header_boilerplate.format(
script_name=os.path.basename(sys.argv[0]))
# -------- Generate shader constants -----------
angle_to_gl = angle_format.load_inverse_table('../../angle_format_map.json')
shader_formats_autogen = gen_shader_enums_code(angle_to_gl.keys())
shader_autogen_header = boilerplate_code + shader_formats_autogen
with open('format_autogen.h', 'wt') as out_file:
out_file.write(shader_autogen_header)
out_file.close()
# -------- Combine and create shader source string -----------
# Generate combined source
clang = find_clang()
# Use clang to preprocess the combination source. "@@" token is used to prevent clang from
# expanding the preprocessor directive
temp_fname = 'temp_master_source.metal'
with open(temp_fname, 'wb') as temp_file:
for src_file in src_files:
include_str = '#include "' + src_file + '" \n'
temp_file.write(include_str.encode('utf-8'))
args = [clang]
if not os.name == 'nt':
args += ['-xc++']
args += ['-E', temp_fname]
combined_source = subprocess.check_output(args)
# Remove '@@' tokens
final_combined_src_string = combined_source.replace('@@'.encode('utf-8'), ''.encode('utf-8'))
# Generate final file:
with open('mtl_default_shaders_src_autogen.inc', 'wt') as out_file:
out_file.write(boilerplate_code)
out_file.write('\n')
out_file.write('// C++ string version of combined Metal default shaders.\n\n')
out_file.write('\n\nstatic char gDefaultMetallibSrc[] = R"(\n')
out_file.write(final_combined_src_string.decode("utf-8"))
out_file.write('\n')
out_file.write(')";\n')
out_file.close()
with open('mtl_default_shaders_src_autogen.metal', 'wt') as out_file:
out_file.write(boilerplate_code)
out_file.write('\n')
out_file.write('// Metal version of combined Metal default shaders.\n\n')
out_file.write(final_combined_src_string.decode("utf-8"))
out_file.close()
os.remove(temp_fname)
if __name__ == '__main__':
sys.exit(main())