Skip to content

Commit f1c1b68

Browse files
zandersodnfield
authored andcommitted
Adds --sksl target to impellerc (flutter#131)
1 parent ca3cd7f commit f1c1b68

File tree

6 files changed

+109
-42
lines changed

6 files changed

+109
-42
lines changed

impeller/compiler/compiler.cc

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ CompilerTargetPlatformToCompilerMSLTargetPlatform(
206206
return spirv_cross::CompilerMSL::Options::Platform::iOS;
207207
case Compiler::TargetPlatform::kMacOS:
208208
// Unknown should not happen due to prior validation.
209+
case Compiler::TargetPlatform::kFlutterSPIRV:
209210
case Compiler::TargetPlatform::kUnknown:
210211
return spirv_cross::CompilerMSL::Options::Platform::macOS;
211212
}
@@ -239,18 +240,29 @@ Compiler::Compiler(const fml::Mapping& source_mapping,
239240
// will be processed later by backend specific compilers. So optimizations
240241
// here are irrelevant and get in the way of generating reflection code.
241242
options.SetGenerateDebugInfo();
242-
options.SetOptimizationLevel(
243-
shaderc_optimization_level::shaderc_optimization_level_zero);
244243

245244
// Expects GLSL 4.60 (Core Profile).
246245
// https://www.khronos.org/registry/OpenGL/specs/gl/GLSLangSpec.4.60.pdf
247246
options.SetSourceLanguage(
248247
shaderc_source_language::shaderc_source_language_glsl);
249248
options.SetForcedVersionProfile(460, shaderc_profile::shaderc_profile_core);
250-
options.SetTargetEnvironment(
251-
shaderc_target_env::shaderc_target_env_vulkan,
252-
shaderc_env_version::shaderc_env_version_vulkan_1_1);
253-
options.SetTargetSpirv(shaderc_spirv_version::shaderc_spirv_version_1_3);
249+
if (source_options.target_platform == TargetPlatform::kFlutterSPIRV) {
250+
options.SetOptimizationLevel(
251+
shaderc_optimization_level::shaderc_optimization_level_size);
252+
options.SetTargetEnvironment(
253+
shaderc_target_env::shaderc_target_env_opengl,
254+
shaderc_env_version::shaderc_env_version_opengl_4_5
255+
);
256+
options.SetTargetSpirv(shaderc_spirv_version::shaderc_spirv_version_1_0);
257+
} else {
258+
options.SetOptimizationLevel(
259+
shaderc_optimization_level::shaderc_optimization_level_zero);
260+
options.SetTargetEnvironment(
261+
shaderc_target_env::shaderc_target_env_vulkan,
262+
shaderc_env_version::shaderc_env_version_vulkan_1_1
263+
);
264+
options.SetTargetSpirv(shaderc_spirv_version::shaderc_spirv_version_1_3);
265+
}
254266

255267
options.SetAutoBindUniforms(true);
256268
options.SetAutoMapLocations(true);
@@ -295,6 +307,11 @@ Compiler::Compiler(const fml::Mapping& source_mapping,
295307
included_file_names_ = std::move(included_file_names);
296308
}
297309

310+
if (!TargetPlatformNeedsMSL(source_options.target_platform)) {
311+
is_valid_ = true;
312+
return;
313+
}
314+
298315
// MSL Generation.
299316
spirv_cross::Parser parser(spv_result_->cbegin(),
300317
spv_result_->cend() - spv_result_->cbegin());
@@ -418,6 +435,26 @@ std::string Compiler::EntryPointFromSourceName(const std::string& file_name,
418435
return stream.str();
419436
}
420437

438+
bool Compiler::TargetPlatformNeedsMSL(TargetPlatform platform) {
439+
switch (platform) {
440+
case TargetPlatform::kIPhoneOS:
441+
case TargetPlatform::kMacOS:
442+
return true;
443+
default:
444+
return false;
445+
}
446+
}
447+
448+
bool Compiler::TargetPlatformNeedsReflection(TargetPlatform platform) {
449+
switch (platform) {
450+
case TargetPlatform::kIPhoneOS:
451+
case TargetPlatform::kMacOS:
452+
return true;
453+
default:
454+
return false;
455+
}
456+
}
457+
421458
std::string Compiler::GetSourcePrefix() const {
422459
std::stringstream stream;
423460
stream << options_.file_name << ": ";

impeller/compiler/compiler.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@ class Compiler {
3131
kUnknown,
3232
kMacOS,
3333
kIPhoneOS,
34+
kFlutterSPIRV,
3435
};
3536

3637
static SourceType SourceTypeFromFileName(const std::string& file_name);
3738

3839
static std::string EntryPointFromSourceName(const std::string& file_name,
3940
SourceType type);
4041

42+
static bool TargetPlatformNeedsMSL(TargetPlatform platform);
43+
44+
static bool TargetPlatformNeedsReflection(TargetPlatform platform);
45+
4146
struct SourceOptions {
4247
SourceType type = SourceType::kUnknown;
4348
TargetPlatform target_platform = TargetPlatform::kUnknown;

impeller/compiler/compiler_unittests.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ class CompilerTest : public ::testing::Test {
2323

2424
~CompilerTest() = default;
2525

26-
bool CanCompileFixture(const char* fixture_name) const {
26+
bool CanCompileFixture(const char* fixture_name,
27+
Compiler::TargetPlatform target_platform) const {
2728
auto fixture = flutter::testing::OpenFixtureAsMapping(fixture_name);
2829
if (!fixture->GetMapping()) {
2930
VALIDATION_LOG << "Could not find shader in fixtures: " << fixture_name;
3031
return false;
3132
}
3233
Compiler::SourceOptions compiler_options(fixture_name);
33-
compiler_options.target_platform = Compiler::TargetPlatform::kMacOS;
34+
compiler_options.target_platform = target_platform;
3435
compiler_options.working_directory = std::make_shared<fml::UniqueFD>(
3536
flutter::testing::OpenFixturesDirectory());
3637
Reflector::Options reflector_options;
@@ -60,7 +61,13 @@ TEST_F(CompilerTest, ShaderKindMatchingIsSuccessful) {
6061
}
6162

6263
TEST_F(CompilerTest, CanCompileSample) {
63-
ASSERT_TRUE(CanCompileFixture("sample.vert"));
64+
ASSERT_TRUE(CanCompileFixture(
65+
"sample.vert", Compiler::TargetPlatform::kMacOS));
66+
}
67+
68+
TEST_F(CompilerTest, CanTargetFlutterSPIRV) {
69+
ASSERT_TRUE(CanCompileFixture(
70+
"test_texture.frag", Compiler::TargetPlatform::kFlutterSPIRV));
6471
}
6572

6673
} // namespace testing

impeller/compiler/impellerc_main.cc

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -69,49 +69,65 @@ bool Main(const fml::CommandLine& command_line) {
6969
return false;
7070
}
7171

72-
if (!fml::WriteAtomically(*switches.working_directory,
73-
switches.metal_file_name.c_str(),
74-
*compiler.GetMSLShaderSource())) {
75-
std::cerr << "Could not write file to " << switches.spirv_file_name
76-
<< std::endl;
77-
return false;
78-
}
79-
80-
if (!switches.reflection_json_name.empty()) {
72+
if (Compiler::TargetPlatformNeedsMSL(options.target_platform)) {
8173
if (!fml::WriteAtomically(*switches.working_directory,
82-
switches.reflection_json_name.c_str(),
83-
*compiler.GetReflector()->GetReflectionJSON())) {
84-
std::cerr << "Could not write reflection json to "
85-
<< switches.reflection_json_name << std::endl;
74+
switches.metal_file_name.c_str(),
75+
*compiler.GetMSLShaderSource())) {
76+
std::cerr << "Could not write file to " << switches.spirv_file_name
77+
<< std::endl;
8678
return false;
8779
}
8880
}
8981

90-
if (!switches.reflection_header_name.empty()) {
91-
if (!fml::WriteAtomically(
92-
*switches.working_directory,
93-
switches.reflection_header_name.c_str(),
94-
*compiler.GetReflector()->GetReflectionHeader())) {
95-
std::cerr << "Could not write reflection header to "
96-
<< switches.reflection_header_name << std::endl;
97-
return false;
82+
83+
if (Compiler::TargetPlatformNeedsReflection(options.target_platform)) {
84+
if (!switches.reflection_json_name.empty()) {
85+
if (!fml::WriteAtomically(*switches.working_directory,
86+
switches.reflection_json_name.c_str(),
87+
*compiler.GetReflector()->GetReflectionJSON())) {
88+
std::cerr << "Could not write reflection json to "
89+
<< switches.reflection_json_name << std::endl;
90+
return false;
91+
}
9892
}
99-
}
10093

101-
if (!switches.reflection_cc_name.empty()) {
102-
if (!fml::WriteAtomically(*switches.working_directory,
103-
switches.reflection_cc_name.c_str(),
104-
*compiler.GetReflector()->GetReflectionCC())) {
105-
std::cerr << "Could not write reflection CC to "
106-
<< switches.reflection_cc_name << std::endl;
107-
return false;
94+
if (!switches.reflection_header_name.empty()) {
95+
if (!fml::WriteAtomically(
96+
*switches.working_directory,
97+
switches.reflection_header_name.c_str(),
98+
*compiler.GetReflector()->GetReflectionHeader())) {
99+
std::cerr << "Could not write reflection header to "
100+
<< switches.reflection_header_name << std::endl;
101+
return false;
102+
}
103+
}
104+
105+
if (!switches.reflection_cc_name.empty()) {
106+
if (!fml::WriteAtomically(*switches.working_directory,
107+
switches.reflection_cc_name.c_str(),
108+
*compiler.GetReflector()->GetReflectionCC())) {
109+
std::cerr << "Could not write reflection CC to "
110+
<< switches.reflection_cc_name << std::endl;
111+
return false;
112+
}
108113
}
109114
}
110115

111116
if (!switches.depfile_path.empty()) {
117+
std::string result_file;
118+
switch (switches.target_platform) {
119+
case Compiler::TargetPlatform::kMacOS:
120+
case Compiler::TargetPlatform::kIPhoneOS:
121+
result_file = switches.metal_file_name;
122+
break;
123+
case Compiler::TargetPlatform::kFlutterSPIRV:
124+
case Compiler::TargetPlatform::kUnknown:
125+
result_file = switches.spirv_file_name;
126+
break;
127+
}
112128
if (!fml::WriteAtomically(
113129
*switches.working_directory, switches.depfile_path.c_str(),
114-
*compiler.CreateDepfileContents({switches.metal_file_name}))) {
130+
*compiler.CreateDepfileContents({result_file}))) {
115131
std::cerr << "Could not write depfile to " << switches.depfile_path
116132
<< std::endl;
117133
return false;

impeller/compiler/switches.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace compiler {
1515
static const std::map<std::string, Compiler::TargetPlatform> kKnownPlatforms = {
1616
{"macos", Compiler::TargetPlatform::kMacOS},
1717
{"ios", Compiler::TargetPlatform::kIPhoneOS},
18+
{"flutter-spirv", Compiler::TargetPlatform::kFlutterSPIRV},
1819
};
1920

2021
void Switches::PrintHelp(std::ostream& stream) {
@@ -112,7 +113,7 @@ bool Switches::AreValid(std::ostream& explain) const {
112113
valid = false;
113114
}
114115

115-
if (metal_file_name.empty()) {
116+
if (metal_file_name.empty() && Compiler::TargetPlatformNeedsMSL(target_platform)) {
116117
explain << "Metal file name was empty." << std::endl;
117118
valid = false;
118119
}

impeller/fixtures/BUILD.gn

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ impeller_shaders("shader_fixtures") {
2121

2222
test_fixtures("file_fixtures") {
2323
fixtures = [
24-
"sample.vert",
25-
"types.h",
2624
"airplane.jpg",
2725
"bay_bridge.jpg",
2826
"boston.jpg",
2927
"embarcadero.jpg",
3028
"kalimba.jpg",
29+
"sample.vert",
30+
"types.h",
31+
"test_texture.frag",
3132
"//flutter/third_party/txt/third_party/fonts/Roboto-Regular.ttf",
3233
"//flutter/third_party/txt/third_party/fonts/NotoColorEmoji.ttf",
3334
"//flutter/third_party/txt/third_party/fonts/HomemadeApple.ttf",

0 commit comments

Comments
 (0)