Skip to content

Commit

Permalink
Forward targets specification to the code generator in impellerc.
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaygarde authored and dnfield committed Apr 27, 2022
1 parent 64e3b29 commit 3ac38d8
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 2 deletions.
21 changes: 20 additions & 1 deletion impeller/compiler/compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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 =
Expand Down
7 changes: 7 additions & 0 deletions impeller/compiler/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,20 @@ 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,
SourceType type);

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";
Expand Down
1 change: 1 addition & 0 deletions impeller/compiler/impellerc_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
37 changes: 36 additions & 1 deletion impeller/compiler/switches.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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))),
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions impeller/compiler/switches.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions impeller/tools/impeller.gni
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 3ac38d8

Please sign in to comment.