Hash :
34a99db4
Author :
Date :
2023-08-03T16:46:36
Metal: Check the full generated default shader in Add the default MSL shader text in the repo. Fix an error with include path setup to make the script runnable from an arbitrary directory, for WebKit purposes. WebKit would compile that to a compile-specific metallib and then attach that metallib to the compilation as .h. Current upstream behavior is to compile the metallib once few platforms. Bug: angleproject:8284 Change-Id: Icbf7c0055718496cb380fad0b73d5b9b0d0abb0e Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4748400 Commit-Queue: Kenneth Russell <kbr@chromium.org> Auto-Submit: Kimmo Kinnunen <kkinnunen@apple.com> Reviewed-by: Geoff Lang <geofflang@chromium.org> Reviewed-by: Kenneth Russell <kbr@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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
#!/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(os.path.join(os.path.dirname(__file__), '../..'))
import angle_format
import gen_angle_format_table
configs = [{
"sdk": "macosx",
"compile_flags": ["--std=macos-metal2.1", "-mmacosx-version-min=10.14"],
"variable_name": "gDefaultMetallib",
"header": "mtl_internal_shaders_macos_autogen.h"
}, {
"sdk": "iphoneos",
"compile_flags": ["--std=ios-metal2.1", "-mios-version-min=12"],
"variable_name": "gDefaultMetallib",
"header": "mtl_internal_shaders_ios_autogen.h"
}]
metal_source_output_header = "mtl_internal_shaders_src_autogen.h"
metal_shader_output_file = "mtl_internal_shaders_autogen.metal"
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 metal_to_air(metal_src, sdk, compile_flags, dest_air_file):
temp_fname = 'temp_metal_src.metal'
with open(temp_fname, 'wt') as out_file:
out_file.write(metal_src.decode("utf-8"))
result = subprocess.run(
["xcrun", "-sdk", sdk, "metal"] + compile_flags + ["-c", temp_fname, "-o", dest_air_file],
stdout=subprocess.PIPE,
text=True)
os.remove(temp_fname)
if result.returncode != 0:
raise Exception('Failed to compile metal to air: ' + result.stdout.strip())
def air_to_mtllib(src_air_file, sdk, dest_mtllib_file):
result = subprocess.run(
["xcrun", "-sdk", sdk, "metallib", src_air_file, "-o", dest_mtllib_file],
stdout=subprocess.PIPE,
text=True)
if result.returncode != 0:
raise Exception('Failed to compile metal to air: ' + result.stdout.strip())
def metal_to_metallib(metal_src, sdk, compile_flags):
intermediate_air_file = "temp_metal.air"
metal_to_air(metal_src, sdk, compile_flags, intermediate_air_file)
intermediate_metallib_file = "temp_metal.metallib"
air_to_mtllib(intermediate_air_file, sdk, intermediate_metallib_file)
os.remove(intermediate_air_file)
with open(intermediate_metallib_file, 'rb') as f:
mtllib = f.read()
os.remove(intermediate_metallib_file)
return mtllib
def generate_metallib_header(metal_src, sdk, compile_flags, variable_name, dest_header_file):
boilerplate_code = template_header_boilerplate.format(
script_name=os.path.basename(sys.argv[0]))
metallib_data = metal_to_metallib(metal_src, sdk, compile_flags)
with open(dest_header_file, 'wt') as out_file:
out_file.write(boilerplate_code)
out_file.write('\n')
out_file.write('// C++ string version of default shaders mtllib.\n\n')
out_file.write('\n\nstatic constexpr uint8_t ' + variable_name + '[] = {\n')
for byte in metallib_data:
out_file.write(f"{byte}, ")
out_file.write('\n')
out_file.write('};\n')
out_file.close()
def generate_metal_autogen_header(dest_metal_header):
angle_to_gl = angle_format.load_inverse_table('../../angle_format_map.json')
shader_autogen_header = gen_shader_enums_code(angle_to_gl.keys())
with open(dest_metal_header, 'wt') as out_file:
out_file.write(shader_autogen_header)
out_file.close()
def generate_combined_metal_src(metal_src_files):
autogen_header_file = "format_autogen.h"
generate_metal_autogen_header(autogen_header_file)
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 metal_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)
os.remove(temp_fname)
os.remove(autogen_header_file)
# Remove '@@' tokens
final_combined_src_string = combined_source.replace('@@'.encode('utf-8'), ''.encode('utf-8'))
return final_combined_src_string
def generate_combined_metal_src_header(combined_metal_src, dest_header):
boilerplate_code = template_header_boilerplate.format(
script_name=os.path.basename(sys.argv[0]))
with open(dest_header, '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(combined_metal_src.decode("utf-8"))
out_file.write('\n')
out_file.write(')";\n')
out_file.close()
def generate_combined_metal_shader_file(combined_metal_src, dest_file):
boilerplate_code = template_header_boilerplate.format(
script_name=os.path.basename(sys.argv[0]))
with open(dest_file, 'wt') as out_file:
out_file.write(boilerplate_code)
out_file.write('\n')
out_file.write('// Combined Metal default shaders.\n\n')
out_file.write(combined_metal_src.decode("utf-8"))
out_file.write('\n')
out_file.close()
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 = [config["header"] for config in configs
] + [metal_source_output_header, metal_shader_output_file]
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])
combined_metal_src = generate_combined_metal_src(src_files)
generate_combined_metal_src_header(combined_metal_src, metal_source_output_header)
# Write also the shader text. At the time of writing WebKit compilation would use this.
# The build system should take `metal_shader_output_file` and produce
# libANGLE/renderer/metal/shaders/mtl_internal_shaders_metallib.h in some part
# of the include path before the real libANGLE file.
generate_combined_metal_shader_file(combined_metal_src, metal_shader_output_file)
for config in configs:
generate_metallib_header(combined_metal_src, config["sdk"], config["compile_flags"],
config["variable_name"], config["header"])
if __name__ == '__main__':
sys.exit(main())