Skip to content

Commit

Permalink
Holy size savings Batman!
Browse files Browse the repository at this point in the history
Optimize and compress shaders for size in release modes.
  • Loading branch information
chinmaygarde authored and dnfield committed Apr 27, 2022
1 parent 0016e48 commit 64e3b29
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
4 changes: 4 additions & 0 deletions impeller/compiler/compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ Compiler::Compiler(const fml::Mapping& source_mapping,
{
spirv_cross::CompilerMSL::Options msl_options;
msl_options.platform = spirv_cross::CompilerMSL::Options::Platform::macOS;
// If this version specification changes, the GN rules that process the
// Metal to AIR must be updated as well.
msl_options.msl_version =
spirv_cross::CompilerMSL::Options::make_msl_version(1, 2);
msl_compiler->set_msl_options(msl_options);
}

Expand Down
53 changes: 48 additions & 5 deletions impeller/tools/build_metal_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,32 @@ def Main():
parser.add_argument("--source",
type=str, action="append", required=True,
help="The source file to compile. Can be specified multiple times.")
parser.add_argument("--optimize", action="store_true", default=False,
help="If available optimizations must be applied to the compiled Metal sources.")
parser.add_argument("--platform", required=True, choices=["mac", "ios"],
help="Select the platform.")

args = parser.parse_args()

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

command = [
"xcrun",
]

if args.platform == "mac":
command += [
"-sdk",
"macosx",
]
elif args.platform == "ios":
command += [
"-sdk",
"iphoneos",
]

command += [
"metal",
# TODO: Embeds both sources and driver options in the output. This aids in
# debugging but should be removed from release builds.
"-MO",
"-gline-tables-only",
# These warnings are from generated code and would make no sense to the GLSL
# author.
"-Wno-unused-variable",
Expand All @@ -49,9 +63,38 @@ def Main():
"-MF",
args.depfile,
"-o",
args.output
args.output,
]

# The Metal standard must match the specification in impellerc.
if args.platform == "mac":
command += [
"--std=macos-metal1.2",
]
elif args.platform == "ios":
command += [
"--std=ios-metal1.2",
]

if args.optimize:
command += [
# Like -Os (and thus -O2), but reduces code size further.
"-Oz",
# Allow aggressive, lossy floating-point optimizations.
"-ffast-math",
]
else:
command += [
# Embeds both sources and driver options in the output. This aids in
# debugging but should be removed from release builds.
"-frecord-sources",
# Assist the sampling profiler.
"-gline-tables-only",
"-g",
# Optimize for debuggability.
"-Og",
]

command += args.source

subprocess.check_call(command)
Expand Down
12 changes: 12 additions & 0 deletions impeller/tools/impeller.gni
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ template("metal_library") {
rebase_path(depfile),
]

if (!is_debug) {
args += [ "--optimize" ]
}

if (is_ios) {
args += [ "--platform=ios" ]
} else if (is_mac) {
args += [ "--platform=mac" ]
} else {
assert(false, "Unsupported platform for generating Metal shaders.")
}

foreach(source, invoker.sources) {
args += [
"--source",
Expand Down

0 comments on commit 64e3b29

Please sign in to comment.