Skip to content

Commit 3ac38d8

Browse files
chinmaygardednfield
authored andcommitted
Forward targets specification to the code generator in impellerc.
1 parent 64e3b29 commit 3ac38d8

File tree

6 files changed

+73
-2
lines changed

6 files changed

+73
-2
lines changed

impeller/compiler/compiler.cc

+20-1
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,19 @@ static spv::ExecutionModel ToExecutionModel(Compiler::SourceType type) {
198198
;
199199
}
200200

201+
static spirv_cross::CompilerMSL::Options::Platform
202+
CompilerTargetPlatformToCompilerMSLTargetPlatform(
203+
Compiler::TargetPlatform platform) {
204+
switch (platform) {
205+
case Compiler::TargetPlatform::kIPhoneOS:
206+
return spirv_cross::CompilerMSL::Options::Platform::iOS;
207+
case Compiler::TargetPlatform::kMacOS:
208+
// Unknown should not happen due to prior validation.
209+
case Compiler::TargetPlatform::kUnknown:
210+
return spirv_cross::CompilerMSL::Options::Platform::macOS;
211+
}
212+
}
213+
201214
Compiler::Compiler(const fml::Mapping& source_mapping,
202215
SourceOptions source_options,
203216
Reflector::Options reflector_options)
@@ -208,6 +221,11 @@ Compiler::Compiler(const fml::Mapping& source_mapping,
208221
return;
209222
}
210223

224+
if (source_options.target_platform == TargetPlatform::kUnknown) {
225+
COMPILER_ERROR << "Target platform not specified.";
226+
return;
227+
}
228+
211229
auto shader_kind = ToShaderCShaderKind(source_options.type);
212230

213231
if (shader_kind == shaderc_shader_kind::shaderc_glsl_infer_from_source) {
@@ -297,7 +315,8 @@ Compiler::Compiler(const fml::Mapping& source_mapping,
297315

298316
{
299317
spirv_cross::CompilerMSL::Options msl_options;
300-
msl_options.platform = spirv_cross::CompilerMSL::Options::Platform::macOS;
318+
msl_options.platform = CompilerTargetPlatformToCompilerMSLTargetPlatform(
319+
options_.target_platform);
301320
// If this version specification changes, the GN rules that process the
302321
// Metal to AIR must be updated as well.
303322
msl_options.msl_version =

impeller/compiler/compiler.h

+7
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,20 @@ class Compiler {
2727
kFragmentShader,
2828
};
2929

30+
enum class TargetPlatform {
31+
kUnknown,
32+
kMacOS,
33+
kIPhoneOS,
34+
};
35+
3036
static SourceType SourceTypeFromFileName(const std::string& file_name);
3137

3238
static std::string EntryPointFromSourceName(const std::string& file_name,
3339
SourceType type);
3440

3541
struct SourceOptions {
3642
SourceType type = SourceType::kUnknown;
43+
TargetPlatform target_platform = TargetPlatform::kUnknown;
3744
std::shared_ptr<fml::UniqueFD> working_directory;
3845
std::vector<IncludeDir> include_dirs;
3946
std::string file_name = "main.glsl";

impeller/compiler/impellerc_main.cc

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ bool Main(const fml::CommandLine& command_line) {
3737
}
3838

3939
Compiler::SourceOptions options;
40+
options.target_platform = switches.target_platform;
4041
options.type = Compiler::SourceTypeFromFileName(switches.source_file_name);
4142
options.working_directory = switches.working_directory;
4243
options.file_name = switches.source_file_name;

impeller/compiler/switches.cc

+36-1
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,25 @@
55
#include "flutter/impeller/compiler/switches.h"
66

77
#include <filesystem>
8+
#include <map>
89

910
#include "flutter/fml/file.h"
1011

1112
namespace impeller {
1213
namespace compiler {
1314

15+
static const std::map<std::string, Compiler::TargetPlatform> kKnownPlatforms = {
16+
{"macos", Compiler::TargetPlatform::kMacOS},
17+
{"ios", Compiler::TargetPlatform::kIPhoneOS},
18+
};
19+
1420
void Switches::PrintHelp(std::ostream& stream) {
1521
stream << std::endl << "Valid Argument are:" << std::endl;
22+
stream << "One of [";
23+
for (const auto& platform : kKnownPlatforms) {
24+
stream << " --" << platform.first;
25+
}
26+
stream << " ]" << std::endl;
1627
stream << "--input=<glsl_file>" << std::endl;
1728
stream << "--metal=<metal_output_file>" << std::endl;
1829
stream << "--spirv=<spirv_output_file>" << std::endl;
@@ -28,8 +39,27 @@ Switches::Switches() = default;
2839

2940
Switches::~Switches() = default;
3041

42+
static Compiler::TargetPlatform TargetPlatformFromCommandLine(
43+
const fml::CommandLine& command_line) {
44+
auto target = Compiler::TargetPlatform::kUnknown;
45+
for (const auto& platform : kKnownPlatforms) {
46+
if (command_line.HasOption(platform.first)) {
47+
// If the platform has already been determined, the caller may have
48+
// specified multiple platforms. This is an error and only one must be
49+
// selected.
50+
if (target != Compiler::TargetPlatform::kUnknown) {
51+
return Compiler::TargetPlatform::kUnknown;
52+
}
53+
target = platform.second;
54+
// Keep going to detect duplicates.
55+
}
56+
}
57+
return target;
58+
}
59+
3160
Switches::Switches(const fml::CommandLine& command_line)
32-
: working_directory(std::make_shared<fml::UniqueFD>(
61+
: target_platform(TargetPlatformFromCommandLine(command_line)),
62+
working_directory(std::make_shared<fml::UniqueFD>(
3363
fml::OpenDirectory(std::filesystem::current_path().native().c_str(),
3464
false, // create if necessary,
3565
fml::FilePermission::kRead))),
@@ -67,6 +97,11 @@ Switches::Switches(const fml::CommandLine& command_line)
6797

6898
bool Switches::AreValid(std::ostream& explain) const {
6999
bool valid = true;
100+
if (target_platform == Compiler::TargetPlatform::kUnknown) {
101+
explain << "The target platform (only one) was not specified." << std::endl;
102+
valid = false;
103+
}
104+
70105
if (!working_directory || !working_directory->is_valid()) {
71106
explain << "Could not figure out working directory." << std::endl;
72107
valid = false;

impeller/compiler/switches.h

+2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
#include "flutter/fml/command_line.h"
1111
#include "flutter/fml/macros.h"
1212
#include "flutter/fml/unique_fd.h"
13+
#include "flutter/impeller/compiler/compiler.h"
1314
#include "flutter/impeller/compiler/include_dir.h"
1415

1516
namespace impeller {
1617
namespace compiler {
1718

1819
struct Switches {
20+
Compiler::TargetPlatform target_platform = Compiler::TargetPlatform::kUnknown;
1921
std::shared_ptr<fml::UniqueFD> working_directory;
2022
std::vector<IncludeDir> include_directories;
2123
std::string source_file_name;

impeller/tools/impeller.gni

+7
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ template("impeller_shaders") {
142142
"--include={{source_dir}}",
143143
"--depfile=$depfile_intermediate_path",
144144
]
145+
146+
if (is_mac) {
147+
args += [ "--macos" ]
148+
}
149+
if (is_ios) {
150+
args += [ "--ios" ]
151+
}
145152
}
146153

147154
metal_library_target_name = "metal_library_$target_name"

0 commit comments

Comments
 (0)