Skip to content

Commit 64e3b29

Browse files
chinmaygardednfield
authored andcommitted
Holy size savings Batman!
Optimize and compress shaders for size in release modes.
1 parent 0016e48 commit 64e3b29

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

impeller/compiler/compiler.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,10 @@ Compiler::Compiler(const fml::Mapping& source_mapping,
298298
{
299299
spirv_cross::CompilerMSL::Options msl_options;
300300
msl_options.platform = spirv_cross::CompilerMSL::Options::Platform::macOS;
301+
// If this version specification changes, the GN rules that process the
302+
// Metal to AIR must be updated as well.
303+
msl_options.msl_version =
304+
spirv_cross::CompilerMSL::Options::make_msl_version(1, 2);
301305
msl_compiler->set_msl_options(msl_options);
302306
}
303307

impeller/tools/build_metal_library.py

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,32 @@ def Main():
2929
parser.add_argument("--source",
3030
type=str, action="append", required=True,
3131
help="The source file to compile. Can be specified multiple times.")
32+
parser.add_argument("--optimize", action="store_true", default=False,
33+
help="If available optimizations must be applied to the compiled Metal sources.")
34+
parser.add_argument("--platform", required=True, choices=["mac", "ios"],
35+
help="Select the platform.")
3236

3337
args = parser.parse_args()
3438

3539
MakeDirectories(os.path.dirname(args.depfile))
3640

3741
command = [
3842
"xcrun",
43+
]
44+
45+
if args.platform == "mac":
46+
command += [
47+
"-sdk",
48+
"macosx",
49+
]
50+
elif args.platform == "ios":
51+
command += [
52+
"-sdk",
53+
"iphoneos",
54+
]
55+
56+
command += [
3957
"metal",
40-
# TODO: Embeds both sources and driver options in the output. This aids in
41-
# debugging but should be removed from release builds.
42-
"-MO",
43-
"-gline-tables-only",
4458
# These warnings are from generated code and would make no sense to the GLSL
4559
# author.
4660
"-Wno-unused-variable",
@@ -49,9 +63,38 @@ def Main():
4963
"-MF",
5064
args.depfile,
5165
"-o",
52-
args.output
66+
args.output,
5367
]
5468

69+
# The Metal standard must match the specification in impellerc.
70+
if args.platform == "mac":
71+
command += [
72+
"--std=macos-metal1.2",
73+
]
74+
elif args.platform == "ios":
75+
command += [
76+
"--std=ios-metal1.2",
77+
]
78+
79+
if args.optimize:
80+
command += [
81+
# Like -Os (and thus -O2), but reduces code size further.
82+
"-Oz",
83+
# Allow aggressive, lossy floating-point optimizations.
84+
"-ffast-math",
85+
]
86+
else:
87+
command += [
88+
# Embeds both sources and driver options in the output. This aids in
89+
# debugging but should be removed from release builds.
90+
"-frecord-sources",
91+
# Assist the sampling profiler.
92+
"-gline-tables-only",
93+
"-g",
94+
# Optimize for debuggability.
95+
"-Og",
96+
]
97+
5598
command += args.source
5699

57100
subprocess.check_call(command)

impeller/tools/impeller.gni

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ template("metal_library") {
7575
rebase_path(depfile),
7676
]
7777

78+
if (!is_debug) {
79+
args += [ "--optimize" ]
80+
}
81+
82+
if (is_ios) {
83+
args += [ "--platform=ios" ]
84+
} else if (is_mac) {
85+
args += [ "--platform=mac" ]
86+
} else {
87+
assert(false, "Unsupported platform for generating Metal shaders.")
88+
}
89+
7890
foreach(source, invoker.sources) {
7991
args += [
8092
"--source",

0 commit comments

Comments
 (0)