Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[Impeller] Add ability to set the entry-point name for HLSL shaders #37608

Merged
merged 1 commit into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion impeller/compiler/compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ static CompilerBackend CreateCompiler(const spirv_cross::ParsedIR& ir,
return {};
}
auto* backend = compiler.GetCompiler();
if (!EntryPointMustBeNamedMain(source_options.target_platform)) {
if (!EntryPointMustBeNamedMain(source_options.target_platform) &&
source_options.source_language == SourceLanguage::kGLSL) {
backend->rename_entry_point("main", source_options.entry_point_name,
ToExecutionModel(source_options.type));
}
Expand Down
6 changes: 4 additions & 2 deletions impeller/compiler/compiler_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ std::unique_ptr<fml::FileMapping> CompilerTest::GetReflectionJson(

bool CompilerTest::CanCompileAndReflect(const char* fixture_name,
SourceType source_type,
SourceLanguage source_language) const {
SourceLanguage source_language,
const char* entry_point_name) const {
auto fixture = flutter::testing::OpenFixtureAsMapping(fixture_name);
if (!fixture || !fixture->GetMapping()) {
VALIDATION_LOG << "Could not find shader in fixtures: " << fixture_name;
Expand All @@ -80,7 +81,8 @@ bool CompilerTest::CanCompileAndReflect(const char* fixture_name,
source_options.working_directory = std::make_shared<fml::UniqueFD>(
flutter::testing::OpenFixturesDirectory());
source_options.entry_point_name = EntryPointFunctionNameFromSourceName(
fixture_name, SourceTypeFromFileName(fixture_name), source_language);
fixture_name, SourceTypeFromFileName(fixture_name), source_language,
entry_point_name);

Reflector::Options reflector_options;
reflector_options.header_file_name = ReflectionHeaderName(fixture_name);
Expand Down
3 changes: 2 additions & 1 deletion impeller/compiler/compiler_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class CompilerTest : public ::testing::TestWithParam<TargetPlatform> {
bool CanCompileAndReflect(
const char* fixture_name,
SourceType source_type = SourceType::kUnknown,
SourceLanguage source_language = SourceLanguage::kGLSL) const;
SourceLanguage source_language = SourceLanguage::kGLSL,
const char* entry_point_name = "main") const;

private:
fml::UniqueFD intermediates_directory_;
Expand Down
9 changes: 9 additions & 0 deletions impeller/compiler/compiler_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ TEST_P(CompilerTest, CanCompileHLSL) {
"simple.vert.hlsl", SourceType::kVertexShader, SourceLanguage::kHLSL));
}

TEST_P(CompilerTest, CanCompileHLSLWithMultipleStages) {
ASSERT_TRUE(CanCompileAndReflect("multiple_stages.hlsl",
SourceType::kVertexShader,
SourceLanguage::kHLSL, "VertexShader"));
ASSERT_TRUE(CanCompileAndReflect("multiple_stages.hlsl",
SourceType::kFragmentShader,
SourceLanguage::kHLSL, "FragmentShader"));
}

TEST_P(CompilerTest, CanCompileTessellationControlShader) {
ASSERT_TRUE(CanCompileAndReflect("sample.tesc"));
ASSERT_TRUE(CanCompileAndReflect("sample.tesc",
Expand Down
3 changes: 2 additions & 1 deletion impeller/compiler/impellerc_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ bool Main(const fml::CommandLine& command_line) {
options.include_dirs = switches.include_directories;
options.defines = switches.defines;
options.entry_point_name = EntryPointFunctionNameFromSourceName(
switches.source_file_name, options.type, options.source_language);
switches.source_file_name, options.type, options.source_language,
switches.entry_point);
options.json_format = switches.json_format;

Reflector::Options reflector_options;
Expand Down
7 changes: 6 additions & 1 deletion impeller/compiler/switches.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ void Switches::PrintHelp(std::ostream& stream) {
stream << "--spirv=<spirv_output_file>" << std::endl;
stream << "[optional] --source-language=glsl|hlsl (default: glsl)"
<< std::endl;
stream << "[optional] --entry-point=<entry_point_name> (default: main; "
"ignored for glsl)"
<< std::endl;
stream << "[optional] --iplr (causes --sl file to be emitted in iplr format)"
<< std::endl;
stream << "[optional] --reflection-json=<reflection_json_file>" << std::endl;
Expand Down Expand Up @@ -120,7 +123,9 @@ Switches::Switches(const fml::CommandLine& command_line)
reflection_cc_name(
command_line.GetOptionValueWithDefault("reflection-cc", "")),
depfile_path(command_line.GetOptionValueWithDefault("depfile", "")),
json_format(command_line.HasOption("json")) {
json_format(command_line.HasOption("json")),
entry_point(
command_line.GetOptionValueWithDefault("entry-point", "main")) {
if (!working_directory || !working_directory->is_valid()) {
return;
}
Expand Down
1 change: 1 addition & 0 deletions impeller/compiler/switches.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct Switches {
std::vector<std::string> defines;
bool json_format;
SourceLanguage source_language = SourceLanguage::kUnknown;
std::string entry_point;

Switches();

Expand Down
12 changes: 12 additions & 0 deletions impeller/compiler/switches_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ TEST(SwitchesTest, SourceLanguageCanBeSetToHLSL) {
ASSERT_EQ(switches.source_language, SourceLanguage::kHLSL);
}

TEST(SwitchesTest, DefaultEntryPointIsMain) {
Switches switches = MakeSwitchesDesktopGL({});
ASSERT_TRUE(switches.AreValid(std::cout));
ASSERT_EQ(switches.entry_point, "main");
}

TEST(SwitchesTest, EntryPointCanBeSetForHLSL) {
Switches switches = MakeSwitchesDesktopGL({"--entry-point=CustomEntryPoint"});
ASSERT_TRUE(switches.AreValid(std::cout));
ASSERT_EQ(switches.entry_point, "CustomEntryPoint");
}

} // namespace testing
} // namespace compiler
} // namespace impeller
5 changes: 3 additions & 2 deletions impeller/compiler/types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ std::string SourceLanguageToString(SourceLanguage source_language) {
std::string EntryPointFunctionNameFromSourceName(
const std::string& file_name,
SourceType type,
SourceLanguage source_language) {
SourceLanguage source_language,
const std::string& entry_point_name) {
if (source_language == SourceLanguage::kHLSL) {
return "main";
return entry_point_name;
}

std::stringstream stream;
Expand Down
3 changes: 2 additions & 1 deletion impeller/compiler/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ std::string TargetPlatformSLExtension(TargetPlatform platform);
std::string EntryPointFunctionNameFromSourceName(
const std::string& file_name,
SourceType type,
SourceLanguage source_language);
SourceLanguage source_language,
const std::string& entry_point_name);

bool TargetPlatformNeedsSL(TargetPlatform platform);

Expand Down
1 change: 1 addition & 0 deletions impeller/fixtures/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ test_fixtures("file_fixtures") {
"boston.jpg",
"embarcadero.jpg",
"kalimba.jpg",
"multiple_stages.hlsl",
"resources_limit.vert",
"sample.comp",
"sample.frag",
Expand Down
21 changes: 21 additions & 0 deletions impeller/fixtures/multiple_stages.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

struct VertexInput {
float3 position : POSITION;
};

struct VertexOutput {
float4 position : SV_POSITION;
};

VertexOutput VertexShader(VertexInput input) {
VertexOutput output;
output.position = float4(input.position, 1.0);
return output;
}

float4 FragmentShader(VertexOutput input) {
return input.position;
}
3 changes: 1 addition & 2 deletions impeller/fixtures/simple.vert.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ struct VertexOutput {
float4 position : SV_POSITION;
};

VertexOutput
main(VertexInput input) {
VertexOutput main(VertexInput input) {
VertexOutput output;
output.position = float4(input.position, 1.0);
return output;
Expand Down