Forward targets specification to the code generator in impellerc.

This commit is contained in:
Chinmay Garde
2021-12-30 15:28:01 -08:00
committed by Dan Field
parent 3c5bfd5901
commit 35432f7578
6 changed files with 73 additions and 2 deletions

View File

@@ -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 =

View File

@@ -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<fml::UniqueFD> working_directory;
std::vector<IncludeDir> include_dirs;
std::string file_name = "main.glsl";

View File

@@ -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;

View File

@@ -5,14 +5,25 @@
#include "flutter/impeller/compiler/switches.h"
#include <filesystem>
#include <map>
#include "flutter/fml/file.h"
namespace impeller {
namespace compiler {
static const std::map<std::string, Compiler::TargetPlatform> 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=<glsl_file>" << std::endl;
stream << "--metal=<metal_output_file>" << std::endl;
stream << "--spirv=<spirv_output_file>" << 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<fml::UniqueFD>(
: target_platform(TargetPlatformFromCommandLine(command_line)),
working_directory(std::make_shared<fml::UniqueFD>(
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;

View File

@@ -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<fml::UniqueFD> working_directory;
std::vector<IncludeDir> include_directories;
std::string source_file_name;

View File

@@ -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"