diff --git a/engine/src/flutter/impeller/compiler/compiler.cc b/engine/src/flutter/impeller/compiler/compiler.cc index 27778f43bf..f427aeba7b 100644 --- a/engine/src/flutter/impeller/compiler/compiler.cc +++ b/engine/src/flutter/impeller/compiler/compiler.cc @@ -198,6 +198,19 @@ static spv::ExecutionModel ToExecutionModel(Compiler::SourceType type) { ; } +static spirv_cross::CompilerMSL::Options::Platform +CompilerTargetPlatformToCompilerMSLTargetPlatform( + Compiler::TargetPlatform platform) { + switch (platform) { + case Compiler::TargetPlatform::kIPhoneOS: + return spirv_cross::CompilerMSL::Options::Platform::iOS; + case Compiler::TargetPlatform::kMacOS: + // Unknown should not happen due to prior validation. + case Compiler::TargetPlatform::kUnknown: + return spirv_cross::CompilerMSL::Options::Platform::macOS; + } +} + Compiler::Compiler(const fml::Mapping& source_mapping, SourceOptions source_options, Reflector::Options reflector_options) @@ -208,6 +221,11 @@ Compiler::Compiler(const fml::Mapping& source_mapping, return; } + if (source_options.target_platform == TargetPlatform::kUnknown) { + COMPILER_ERROR << "Target platform not specified."; + return; + } + auto shader_kind = ToShaderCShaderKind(source_options.type); if (shader_kind == shaderc_shader_kind::shaderc_glsl_infer_from_source) { @@ -297,7 +315,8 @@ Compiler::Compiler(const fml::Mapping& source_mapping, { spirv_cross::CompilerMSL::Options msl_options; - msl_options.platform = spirv_cross::CompilerMSL::Options::Platform::macOS; + msl_options.platform = CompilerTargetPlatformToCompilerMSLTargetPlatform( + options_.target_platform); // If this version specification changes, the GN rules that process the // Metal to AIR must be updated as well. msl_options.msl_version = diff --git a/engine/src/flutter/impeller/compiler/compiler.h b/engine/src/flutter/impeller/compiler/compiler.h index 7dec9d39ac..689ec08183 100644 --- a/engine/src/flutter/impeller/compiler/compiler.h +++ b/engine/src/flutter/impeller/compiler/compiler.h @@ -27,6 +27,12 @@ class Compiler { kFragmentShader, }; + enum class TargetPlatform { + kUnknown, + kMacOS, + kIPhoneOS, + }; + static SourceType SourceTypeFromFileName(const std::string& file_name); static std::string EntryPointFromSourceName(const std::string& file_name, @@ -34,6 +40,7 @@ class Compiler { struct SourceOptions { SourceType type = SourceType::kUnknown; + TargetPlatform target_platform = TargetPlatform::kUnknown; std::shared_ptr working_directory; std::vector include_dirs; std::string file_name = "main.glsl"; diff --git a/engine/src/flutter/impeller/compiler/impellerc_main.cc b/engine/src/flutter/impeller/compiler/impellerc_main.cc index 23132fd082..de9f762238 100644 --- a/engine/src/flutter/impeller/compiler/impellerc_main.cc +++ b/engine/src/flutter/impeller/compiler/impellerc_main.cc @@ -37,6 +37,7 @@ bool Main(const fml::CommandLine& command_line) { } Compiler::SourceOptions options; + options.target_platform = switches.target_platform; options.type = Compiler::SourceTypeFromFileName(switches.source_file_name); options.working_directory = switches.working_directory; options.file_name = switches.source_file_name; diff --git a/engine/src/flutter/impeller/compiler/switches.cc b/engine/src/flutter/impeller/compiler/switches.cc index 9b01363fd8..30f20a3006 100644 --- a/engine/src/flutter/impeller/compiler/switches.cc +++ b/engine/src/flutter/impeller/compiler/switches.cc @@ -5,14 +5,25 @@ #include "flutter/impeller/compiler/switches.h" #include +#include #include "flutter/fml/file.h" namespace impeller { namespace compiler { +static const std::map kKnownPlatforms = { + {"macos", Compiler::TargetPlatform::kMacOS}, + {"ios", Compiler::TargetPlatform::kIPhoneOS}, +}; + void Switches::PrintHelp(std::ostream& stream) { stream << std::endl << "Valid Argument are:" << std::endl; + stream << "One of ["; + for (const auto& platform : kKnownPlatforms) { + stream << " --" << platform.first; + } + stream << " ]" << std::endl; stream << "--input=" << std::endl; stream << "--metal=" << std::endl; stream << "--spirv=" << std::endl; @@ -28,8 +39,27 @@ Switches::Switches() = default; Switches::~Switches() = default; +static Compiler::TargetPlatform TargetPlatformFromCommandLine( + const fml::CommandLine& command_line) { + auto target = Compiler::TargetPlatform::kUnknown; + for (const auto& platform : kKnownPlatforms) { + if (command_line.HasOption(platform.first)) { + // If the platform has already been determined, the caller may have + // specified multiple platforms. This is an error and only one must be + // selected. + if (target != Compiler::TargetPlatform::kUnknown) { + return Compiler::TargetPlatform::kUnknown; + } + target = platform.second; + // Keep going to detect duplicates. + } + } + return target; +} + Switches::Switches(const fml::CommandLine& command_line) - : working_directory(std::make_shared( + : target_platform(TargetPlatformFromCommandLine(command_line)), + working_directory(std::make_shared( fml::OpenDirectory(std::filesystem::current_path().native().c_str(), false, // create if necessary, fml::FilePermission::kRead))), @@ -67,6 +97,11 @@ Switches::Switches(const fml::CommandLine& command_line) bool Switches::AreValid(std::ostream& explain) const { bool valid = true; + if (target_platform == Compiler::TargetPlatform::kUnknown) { + explain << "The target platform (only one) was not specified." << std::endl; + valid = false; + } + if (!working_directory || !working_directory->is_valid()) { explain << "Could not figure out working directory." << std::endl; valid = false; diff --git a/engine/src/flutter/impeller/compiler/switches.h b/engine/src/flutter/impeller/compiler/switches.h index 7028d4ab4e..2380ca2152 100644 --- a/engine/src/flutter/impeller/compiler/switches.h +++ b/engine/src/flutter/impeller/compiler/switches.h @@ -10,12 +10,14 @@ #include "flutter/fml/command_line.h" #include "flutter/fml/macros.h" #include "flutter/fml/unique_fd.h" +#include "flutter/impeller/compiler/compiler.h" #include "flutter/impeller/compiler/include_dir.h" namespace impeller { namespace compiler { struct Switches { + Compiler::TargetPlatform target_platform = Compiler::TargetPlatform::kUnknown; std::shared_ptr working_directory; std::vector include_directories; std::string source_file_name; diff --git a/engine/src/flutter/impeller/tools/impeller.gni b/engine/src/flutter/impeller/tools/impeller.gni index 84aab888b0..259107582a 100644 --- a/engine/src/flutter/impeller/tools/impeller.gni +++ b/engine/src/flutter/impeller/tools/impeller.gni @@ -142,6 +142,13 @@ template("impeller_shaders") { "--include={{source_dir}}", "--depfile=$depfile_intermediate_path", ] + + if (is_mac) { + args += [ "--macos" ] + } + if (is_ios) { + args += [ "--ios" ] + } } metal_library_target_name = "metal_library_$target_name"