Skip to content

Commit 84cb1f8

Browse files
authored
Add ability to set entry-point for HLSL shaders (flutter#37608)
1 parent 6dc4a6a commit 84cb1f8

File tree

13 files changed

+66
-11
lines changed

13 files changed

+66
-11
lines changed

impeller/compiler/compiler.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ static CompilerBackend CreateCompiler(const spirv_cross::ParsedIR& ir,
102102
return {};
103103
}
104104
auto* backend = compiler.GetCompiler();
105-
if (!EntryPointMustBeNamedMain(source_options.target_platform)) {
105+
if (!EntryPointMustBeNamedMain(source_options.target_platform) &&
106+
source_options.source_language == SourceLanguage::kGLSL) {
106107
backend->rename_entry_point("main", source_options.entry_point_name,
107108
ToExecutionModel(source_options.type));
108109
}

impeller/compiler/compiler_test.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ std::unique_ptr<fml::FileMapping> CompilerTest::GetReflectionJson(
6767

6868
bool CompilerTest::CanCompileAndReflect(const char* fixture_name,
6969
SourceType source_type,
70-
SourceLanguage source_language) const {
70+
SourceLanguage source_language,
71+
const char* entry_point_name) const {
7172
auto fixture = flutter::testing::OpenFixtureAsMapping(fixture_name);
7273
if (!fixture || !fixture->GetMapping()) {
7374
VALIDATION_LOG << "Could not find shader in fixtures: " << fixture_name;
@@ -80,7 +81,8 @@ bool CompilerTest::CanCompileAndReflect(const char* fixture_name,
8081
source_options.working_directory = std::make_shared<fml::UniqueFD>(
8182
flutter::testing::OpenFixturesDirectory());
8283
source_options.entry_point_name = EntryPointFunctionNameFromSourceName(
83-
fixture_name, SourceTypeFromFileName(fixture_name), source_language);
84+
fixture_name, SourceTypeFromFileName(fixture_name), source_language,
85+
entry_point_name);
8486

8587
Reflector::Options reflector_options;
8688
reflector_options.header_file_name = ReflectionHeaderName(fixture_name);

impeller/compiler/compiler_test.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class CompilerTest : public ::testing::TestWithParam<TargetPlatform> {
2727
bool CanCompileAndReflect(
2828
const char* fixture_name,
2929
SourceType source_type = SourceType::kUnknown,
30-
SourceLanguage source_language = SourceLanguage::kGLSL) const;
30+
SourceLanguage source_language = SourceLanguage::kGLSL,
31+
const char* entry_point_name = "main") const;
3132

3233
private:
3334
fml::UniqueFD intermediates_directory_;

impeller/compiler/compiler_unittests.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ TEST_P(CompilerTest, CanCompileHLSL) {
3737
"simple.vert.hlsl", SourceType::kVertexShader, SourceLanguage::kHLSL));
3838
}
3939

40+
TEST_P(CompilerTest, CanCompileHLSLWithMultipleStages) {
41+
ASSERT_TRUE(CanCompileAndReflect("multiple_stages.hlsl",
42+
SourceType::kVertexShader,
43+
SourceLanguage::kHLSL, "VertexShader"));
44+
ASSERT_TRUE(CanCompileAndReflect("multiple_stages.hlsl",
45+
SourceType::kFragmentShader,
46+
SourceLanguage::kHLSL, "FragmentShader"));
47+
}
48+
4049
TEST_P(CompilerTest, CanCompileTessellationControlShader) {
4150
ASSERT_TRUE(CanCompileAndReflect("sample.tesc"));
4251
ASSERT_TRUE(CanCompileAndReflect("sample.tesc",

impeller/compiler/impellerc_main.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ bool Main(const fml::CommandLine& command_line) {
7070
options.include_dirs = switches.include_directories;
7171
options.defines = switches.defines;
7272
options.entry_point_name = EntryPointFunctionNameFromSourceName(
73-
switches.source_file_name, options.type, options.source_language);
73+
switches.source_file_name, options.type, options.source_language,
74+
switches.entry_point);
7475
options.json_format = switches.json_format;
7576

7677
Reflector::Options reflector_options;

impeller/compiler/switches.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ void Switches::PrintHelp(std::ostream& stream) {
5757
stream << "--spirv=<spirv_output_file>" << std::endl;
5858
stream << "[optional] --source-language=glsl|hlsl (default: glsl)"
5959
<< std::endl;
60+
stream << "[optional] --entry-point=<entry_point_name> (default: main; "
61+
"ignored for glsl)"
62+
<< std::endl;
6063
stream << "[optional] --iplr (causes --sl file to be emitted in iplr format)"
6164
<< std::endl;
6265
stream << "[optional] --reflection-json=<reflection_json_file>" << std::endl;
@@ -120,7 +123,9 @@ Switches::Switches(const fml::CommandLine& command_line)
120123
reflection_cc_name(
121124
command_line.GetOptionValueWithDefault("reflection-cc", "")),
122125
depfile_path(command_line.GetOptionValueWithDefault("depfile", "")),
123-
json_format(command_line.HasOption("json")) {
126+
json_format(command_line.HasOption("json")),
127+
entry_point(
128+
command_line.GetOptionValueWithDefault("entry-point", "main")) {
124129
if (!working_directory || !working_directory->is_valid()) {
125130
return;
126131
}

impeller/compiler/switches.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ struct Switches {
3333
std::vector<std::string> defines;
3434
bool json_format;
3535
SourceLanguage source_language = SourceLanguage::kUnknown;
36+
std::string entry_point;
3637

3738
Switches();
3839

impeller/compiler/switches_unittests.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ TEST(SwitchesTest, SourceLanguageCanBeSetToHLSL) {
5757
ASSERT_EQ(switches.source_language, SourceLanguage::kHLSL);
5858
}
5959

60+
TEST(SwitchesTest, DefaultEntryPointIsMain) {
61+
Switches switches = MakeSwitchesDesktopGL({});
62+
ASSERT_TRUE(switches.AreValid(std::cout));
63+
ASSERT_EQ(switches.entry_point, "main");
64+
}
65+
66+
TEST(SwitchesTest, EntryPointCanBeSetForHLSL) {
67+
Switches switches = MakeSwitchesDesktopGL({"--entry-point=CustomEntryPoint"});
68+
ASSERT_TRUE(switches.AreValid(std::cout));
69+
ASSERT_EQ(switches.entry_point, "CustomEntryPoint");
70+
}
71+
6072
} // namespace testing
6173
} // namespace compiler
6274
} // namespace impeller

impeller/compiler/types.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,10 @@ std::string SourceLanguageToString(SourceLanguage source_language) {
8888
std::string EntryPointFunctionNameFromSourceName(
8989
const std::string& file_name,
9090
SourceType type,
91-
SourceLanguage source_language) {
91+
SourceLanguage source_language,
92+
const std::string& entry_point_name) {
9293
if (source_language == SourceLanguage::kHLSL) {
93-
return "main";
94+
return entry_point_name;
9495
}
9596

9697
std::stringstream stream;

impeller/compiler/types.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ std::string TargetPlatformSLExtension(TargetPlatform platform);
6060
std::string EntryPointFunctionNameFromSourceName(
6161
const std::string& file_name,
6262
SourceType type,
63-
SourceLanguage source_language);
63+
SourceLanguage source_language,
64+
const std::string& entry_point_name);
6465

6566
bool TargetPlatformNeedsSL(TargetPlatform platform);
6667

0 commit comments

Comments
 (0)