Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[impellerc] Adds an SkSL backend #34441

Merged
merged 8 commits into from
Jul 13, 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
6 changes: 3 additions & 3 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ group("unittests") {
"//flutter/flow:flow_unittests",
"//flutter/fml:fml_unittests",
"//flutter/lib/spirv/test/exception_shaders:spirv_compile_exception_shaders",
"//flutter/lib/spirv/test/general_shaders:spirv_compile_general_shaders",
"//flutter/lib/spirv/test/supported_glsl_op_shaders:spirv_compile_supported_glsl_shaders",
"//flutter/lib/spirv/test/supported_op_shaders:spirv_compile_supported_op_shaders",
"//flutter/lib/spirv/test/general_shaders",
"//flutter/lib/spirv/test/supported_glsl_op_shaders",
"//flutter/lib/spirv/test/supported_op_shaders",
"//flutter/lib/ui:ui_unittests",
"//flutter/runtime:dart_plugin_registrant_unittests",
"//flutter/runtime:no_dart_plugin_registrant_unittests",
Expand Down
2 changes: 2 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,8 @@ FILE: ../../../flutter/impeller/compiler/shader_lib/impeller/texture.glsl
FILE: ../../../flutter/impeller/compiler/shader_lib/impeller/types.glsl
FILE: ../../../flutter/impeller/compiler/source_options.cc
FILE: ../../../flutter/impeller/compiler/source_options.h
FILE: ../../../flutter/impeller/compiler/spirv_sksl.cc
FILE: ../../../flutter/impeller/compiler/spirv_sksl.h
FILE: ../../../flutter/impeller/compiler/switches.cc
FILE: ../../../flutter/impeller/compiler/switches.h
FILE: ../../../flutter/impeller/compiler/types.cc
Expand Down
2 changes: 2 additions & 0 deletions impeller/compiler/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ impeller_component("compiler_lib") {
"runtime_stage_data.h",
"source_options.cc",
"source_options.h",
"spirv_sksl.cc",
"spirv_sksl.h",
"switches.cc",
"switches.h",
"types.cc",
Expand Down
26 changes: 24 additions & 2 deletions impeller/compiler/compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static CompilerBackend CreateMSLCompiler(const spirv_cross::ParsedIR& ir,
sl_options.msl_version =
spirv_cross::CompilerMSL::Options::make_msl_version(1, 2);
sl_compiler->set_msl_options(sl_options);
return sl_compiler;
return CompilerBackend(sl_compiler);
}

static CompilerBackend CreateGLSLCompiler(const spirv_cross::ParsedIR& ir,
Expand All @@ -44,7 +44,13 @@ static CompilerBackend CreateGLSLCompiler(const spirv_cross::ParsedIR& ir,
sl_options.es = false;
}
gl_compiler->set_common_options(sl_options);
return gl_compiler;
return CompilerBackend(gl_compiler);
}

static CompilerBackend CreateSkSLCompiler(const spirv_cross::ParsedIR& ir,
const SourceOptions& source_options) {
auto sksl_compiler = std::make_shared<CompilerSkSL>(ir);
return CompilerBackend(sksl_compiler);
}

static bool EntryPointMustBeNamedMain(TargetPlatform platform) {
Expand All @@ -56,6 +62,7 @@ static bool EntryPointMustBeNamedMain(TargetPlatform platform) {
case TargetPlatform::kRuntimeStageMetal:
return false;
case TargetPlatform::kFlutterSPIRV:
case TargetPlatform::kSkSL:
case TargetPlatform::kOpenGLES:
case TargetPlatform::kOpenGLDesktop:
case TargetPlatform::kRuntimeStageGLES:
Expand All @@ -80,6 +87,8 @@ static CompilerBackend CreateCompiler(const spirv_cross::ParsedIR& ir,
case TargetPlatform::kOpenGLDesktop:
compiler = CreateGLSLCompiler(ir, source_options);
break;
case TargetPlatform::kSkSL:
compiler = CreateSkSLCompiler(ir, source_options);
}
if (!compiler) {
return {};
Expand Down Expand Up @@ -165,6 +174,19 @@ Compiler::Compiler(const fml::Mapping& source_mapping,
spirv_options.SetTargetSpirv(
shaderc_spirv_version::shaderc_spirv_version_1_0);
break;
case TargetPlatform::kSkSL:
// When any optimization level above 'zero' is enabled, the phi merges at
// loop continue blocks are rendered using syntax that is supported in
// GLSL, but not in SkSL.
// https://bugs.chromium.org/p/skia/issues/detail?id=13518.
spirv_options.SetOptimizationLevel(
shaderc_optimization_level::shaderc_optimization_level_zero);
spirv_options.SetTargetEnvironment(
shaderc_target_env::shaderc_target_env_opengl,
shaderc_env_version::shaderc_env_version_opengl_4_5);
spirv_options.SetTargetSpirv(
shaderc_spirv_version::shaderc_spirv_version_1_0);
break;
case TargetPlatform::kUnknown:
COMPILER_ERROR << "Target platform invalid.";
return;
Expand Down
17 changes: 17 additions & 0 deletions impeller/compiler/compiler_backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ CompilerBackend::CompilerBackend(MSLCompiler compiler)
CompilerBackend::CompilerBackend(GLSLCompiler compiler)
: CompilerBackend(Type::kGLSL, compiler) {}

CompilerBackend::CompilerBackend(SkSLCompiler compiler)
: CompilerBackend(Type::kSkSL, compiler) {}

CompilerBackend::CompilerBackend() = default;

CompilerBackend::CompilerBackend(Type type, Compiler compiler)
Expand Down Expand Up @@ -54,6 +57,10 @@ const spirv_cross::Compiler* CompilerBackend::GetCompiler() const {
return compiler;
}

if (auto compiler = GetSkSLCompiler()) {
return compiler;
}

return nullptr;
}

Expand All @@ -64,6 +71,9 @@ spirv_cross::Compiler* CompilerBackend::GetCompiler() {
if (auto* glsl = std::get_if<GLSLCompiler>(&compiler_)) {
return glsl->get();
}
if (auto* sksl = std::get_if<SkSLCompiler>(&compiler_)) {
return sksl->get();
}
return nullptr;
}

Expand All @@ -81,6 +91,13 @@ const spirv_cross::CompilerGLSL* CompilerBackend::GetGLSLCompiler() const {
return nullptr;
}

const CompilerSkSL* CompilerBackend::GetSkSLCompiler() const {
if (auto* sksl = std::get_if<SkSLCompiler>(&compiler_)) {
return sksl->get();
}
return nullptr;
}

CompilerBackend::operator bool() const {
return !!GetCompiler();
}
Expand Down
13 changes: 10 additions & 3 deletions impeller/compiler/compiler_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,28 @@
#include "flutter/fml/macros.h"
#include "spirv_glsl.hpp"
#include "spirv_msl.hpp"
#include "spirv_sksl.h"

namespace impeller {
namespace compiler {

struct CompilerBackend {
using MSLCompiler = std::shared_ptr<spirv_cross::CompilerMSL>;
using GLSLCompiler = std::shared_ptr<spirv_cross::CompilerGLSL>;
using Compiler = std::variant<MSLCompiler, GLSLCompiler>;
using SkSLCompiler = std::shared_ptr<CompilerSkSL>;
using Compiler = std::variant<MSLCompiler, GLSLCompiler, SkSLCompiler>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh, sorry about this. This is a cleanup that is tracked in flutter/flutter#106376.


enum class Type {
kMSL,
kGLSL,
kSkSL,
};

CompilerBackend(MSLCompiler compiler);
explicit CompilerBackend(MSLCompiler compiler);

CompilerBackend(GLSLCompiler compiler);
explicit CompilerBackend(GLSLCompiler compiler);

explicit CompilerBackend(SkSLCompiler compiler);

CompilerBackend(Type type, Compiler compiler);

Expand Down Expand Up @@ -59,6 +64,8 @@ struct CompilerBackend {
const spirv_cross::CompilerMSL* GetMSLCompiler() const;

const spirv_cross::CompilerGLSL* GetGLSLCompiler() const;

const CompilerSkSL* GetSkSLCompiler() const;
};

} // namespace compiler
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 @@ -172,6 +172,7 @@ bool Main(const fml::CommandLine& command_line) {
case TargetPlatform::kOpenGLDesktop:
case TargetPlatform::kRuntimeStageMetal:
case TargetPlatform::kRuntimeStageGLES:
case TargetPlatform::kSkSL:
result_file = switches.sl_file_name;
break;
case TargetPlatform::kFlutterSPIRV:
Expand Down
2 changes: 2 additions & 0 deletions impeller/compiler/reflector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ static std::string ToString(CompilerBackend::Type type) {
return "Metal Shading Language";
case CompilerBackend::Type::kGLSL:
return "OpenGL Shading Language";
case CompilerBackend::Type::kSkSL:
return "SkSL Shading Language";
}
FML_UNREACHABLE();
}
Expand Down
1 change: 1 addition & 0 deletions impeller/compiler/runtime_stage_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ static std::optional<fb::TargetPlatform> ToTargetPlatform(
case TargetPlatform::kMetalDesktop:
case TargetPlatform::kMetalIOS:
case TargetPlatform::kFlutterSPIRV:
case TargetPlatform::kSkSL:
case TargetPlatform::kOpenGLES:
case TargetPlatform::kOpenGLDesktop:
return std::nullopt;
Expand Down
Loading