diff --git a/engine/src/flutter/impeller/compiler/impellerc_main.cc b/engine/src/flutter/impeller/compiler/impellerc_main.cc index 0c6df17fcf..f2cc0f4a17 100644 --- a/engine/src/flutter/impeller/compiler/impellerc_main.cc +++ b/engine/src/flutter/impeller/compiler/impellerc_main.cc @@ -81,6 +81,25 @@ bool Main(const fml::CommandLine& command_line) { .filename() .native()); + // Generate SkSL if needed. + std::shared_ptr sksl_mapping; + if (switches.iplr && TargetPlatformBundlesSkSL(switches.target_platform)) { + SourceOptions sksl_options = options; + sksl_options.target_platform = TargetPlatform::kSkSL; + + Reflector::Options sksl_reflector_options = reflector_options; + sksl_reflector_options.target_platform = TargetPlatform::kSkSL; + + Compiler sksl_compiler = + Compiler(*source_file_mapping, sksl_options, sksl_reflector_options); + if (!sksl_compiler.IsValid()) { + std::cerr << "Compilation to SkSL failed." << std::endl; + std::cerr << sksl_compiler.GetErrorMessages() << std::endl; + return false; + } + sksl_mapping = sksl_compiler.GetSLShaderSource(); + } + Compiler compiler(*source_file_mapping, options, reflector_options); if (!compiler.IsValid()) { std::cerr << "Compilation failed." << std::endl; @@ -114,6 +133,9 @@ bool Main(const fml::CommandLine& command_line) { std::cerr << "Runtime stage information was nil." << std::endl; return false; } + if (sksl_mapping) { + stage_data->SetSkSLData(sksl_mapping); + } auto stage_data_mapping = stage_data->CreateMapping(); if (!stage_data_mapping) { std::cerr << "Runtime stage data could not be created." << std::endl; diff --git a/engine/src/flutter/impeller/compiler/reflector.cc b/engine/src/flutter/impeller/compiler/reflector.cc index 181fc3ecd5..9b911dfef4 100644 --- a/engine/src/flutter/impeller/compiler/reflector.cc +++ b/engine/src/flutter/impeller/compiler/reflector.cc @@ -356,6 +356,9 @@ std::shared_ptr Reflector::GenerateRuntimeStageData() const { options_.target_platform // ); data->SetShaderData(shader_data_); + if (sksl_data_) { + data->SetSkSLData(sksl_data_); + } ir_->for_each_typed_id( [&](uint32_t, const spirv_cross::SPIRVariable& var) { if (var.storage != spv::StorageClassUniformConstant) { diff --git a/engine/src/flutter/impeller/compiler/reflector.h b/engine/src/flutter/impeller/compiler/reflector.h index a17993a06f..69b550c7b9 100644 --- a/engine/src/flutter/impeller/compiler/reflector.h +++ b/engine/src/flutter/impeller/compiler/reflector.h @@ -94,6 +94,7 @@ class Reflector { const Options options_; const std::shared_ptr ir_; const std::shared_ptr shader_data_; + const std::shared_ptr sksl_data_; const CompilerBackend compiler_; std::unique_ptr template_arguments_; std::shared_ptr reflection_header_; diff --git a/engine/src/flutter/impeller/compiler/runtime_stage_data.cc b/engine/src/flutter/impeller/compiler/runtime_stage_data.cc index 267b73be7a..596d52d08b 100644 --- a/engine/src/flutter/impeller/compiler/runtime_stage_data.cc +++ b/engine/src/flutter/impeller/compiler/runtime_stage_data.cc @@ -30,6 +30,10 @@ void RuntimeStageData::SetShaderData(std::shared_ptr shader) { shader_ = std::move(shader); } +void RuntimeStageData::SetSkSLData(std::shared_ptr sksl) { + sksl_ = std::move(sksl); +} + static std::optional ToStage(spv::ExecutionModel stage) { switch (stage) { case spv::ExecutionModel::ExecutionModelVertex: @@ -138,6 +142,11 @@ std::shared_ptr RuntimeStageData::CreateMapping() const { runtime_stage.shader = {shader_->GetMapping(), shader_->GetMapping() + shader_->GetSize()}; } + // It is not an error for the SkSL to be ommitted. + if (sksl_->GetSize() > 0u) { + runtime_stage.sksl = {sksl_->GetMapping(), + sksl_->GetMapping() + sksl_->GetSize()}; + } for (const auto& uniform : uniforms_) { auto desc = std::make_unique(); diff --git a/engine/src/flutter/impeller/compiler/runtime_stage_data.h b/engine/src/flutter/impeller/compiler/runtime_stage_data.h index 310259db0a..0e7acbd678 100644 --- a/engine/src/flutter/impeller/compiler/runtime_stage_data.h +++ b/engine/src/flutter/impeller/compiler/runtime_stage_data.h @@ -37,6 +37,8 @@ class RuntimeStageData { void SetShaderData(std::shared_ptr shader); + void SetSkSLData(std::shared_ptr sksl); + std::shared_ptr CreateMapping() const; private: @@ -45,6 +47,7 @@ class RuntimeStageData { const TargetPlatform target_platform_; std::vector uniforms_; std::shared_ptr shader_; + std::shared_ptr sksl_; FML_DISALLOW_COPY_AND_ASSIGN(RuntimeStageData); }; diff --git a/engine/src/flutter/impeller/compiler/types.cc b/engine/src/flutter/impeller/compiler/types.cc index ffae9eb201..b7e13e5aba 100644 --- a/engine/src/flutter/impeller/compiler/types.cc +++ b/engine/src/flutter/impeller/compiler/types.cc @@ -296,5 +296,22 @@ bool TargetPlatformIsMetal(TargetPlatform platform) { FML_UNREACHABLE(); } +bool TargetPlatformBundlesSkSL(TargetPlatform platform) { + switch (platform) { + case TargetPlatform::kSkSL: + case TargetPlatform::kRuntimeStageMetal: + case TargetPlatform::kRuntimeStageGLES: + return true; + case TargetPlatform::kMetalDesktop: + case TargetPlatform::kMetalIOS: + case TargetPlatform::kUnknown: + case TargetPlatform::kOpenGLES: + case TargetPlatform::kOpenGLDesktop: + case TargetPlatform::kVulkan: + return false; + } + FML_UNREACHABLE(); +} + } // namespace compiler } // namespace impeller diff --git a/engine/src/flutter/impeller/compiler/types.h b/engine/src/flutter/impeller/compiler/types.h index 7733621bc2..dd274cf6d6 100644 --- a/engine/src/flutter/impeller/compiler/types.h +++ b/engine/src/flutter/impeller/compiler/types.h @@ -56,6 +56,8 @@ bool TargetPlatformNeedsSL(TargetPlatform platform); bool TargetPlatformNeedsReflection(TargetPlatform platform); +bool TargetPlatformBundlesSkSL(TargetPlatform platform); + std::string ShaderCErrorToString(shaderc_compilation_status status); shaderc_shader_kind ToShaderCShaderKind(SourceType type); diff --git a/engine/src/flutter/impeller/runtime_stage/runtime_stage.cc b/engine/src/flutter/impeller/runtime_stage/runtime_stage.cc index 88423a8b31..c783ecdad1 100644 --- a/engine/src/flutter/impeller/runtime_stage/runtime_stage.cc +++ b/engine/src/flutter/impeller/runtime_stage/runtime_stage.cc @@ -96,6 +96,12 @@ RuntimeStage::RuntimeStage(std::shared_ptr payload) [payload = payload_](auto, auto) {} // ); + sksl_mapping_ = std::make_shared( + runtime_stage->sksl()->data(), // + runtime_stage->sksl()->size(), // + [payload = payload_](auto, auto) {} // + ); + is_valid_ = true; } @@ -111,6 +117,10 @@ const std::shared_ptr& RuntimeStage::GetCodeMapping() const { return code_mapping_; } +const std::shared_ptr& RuntimeStage::GetSkSLMapping() const { + return sksl_mapping_; +} + const std::vector& RuntimeStage::GetUniforms() const { return uniforms_; diff --git a/engine/src/flutter/impeller/runtime_stage/runtime_stage.fbs b/engine/src/flutter/impeller/runtime_stage/runtime_stage.fbs index bb3d04e00e..6e294a0eae 100644 --- a/engine/src/flutter/impeller/runtime_stage/runtime_stage.fbs +++ b/engine/src/flutter/impeller/runtime_stage/runtime_stage.fbs @@ -50,6 +50,7 @@ table RuntimeStage { entrypoint: string; uniforms: [UniformDescription]; shader: [ubyte]; + sksl: [ubyte]; } root_type RuntimeStage; diff --git a/engine/src/flutter/impeller/runtime_stage/runtime_stage.h b/engine/src/flutter/impeller/runtime_stage/runtime_stage.h index 0fde9eb26c..5bbdaa86a7 100644 --- a/engine/src/flutter/impeller/runtime_stage/runtime_stage.h +++ b/engine/src/flutter/impeller/runtime_stage/runtime_stage.h @@ -34,11 +34,14 @@ class RuntimeStage { const std::shared_ptr& GetCodeMapping() const; + const std::shared_ptr& GetSkSLMapping() const; + private: RuntimeShaderStage stage_ = RuntimeShaderStage::kVertex; std::shared_ptr payload_; std::string entrypoint_; std::shared_ptr code_mapping_; + std::shared_ptr sksl_mapping_; std::vector uniforms_; bool is_valid_ = false; diff --git a/engine/src/flutter/lib/ui/painting/fragment_program.cc b/engine/src/flutter/lib/ui/painting/fragment_program.cc index 145dbb6b14..3e814e1753 100644 --- a/engine/src/flutter/lib/ui/painting/fragment_program.cc +++ b/engine/src/flutter/lib/ui/painting/fragment_program.cc @@ -60,7 +60,7 @@ std::string FragmentProgram::initFromAsset(const std::string& asset_name) { runtime_effect_ = DlRuntimeEffect::MakeImpeller( std::make_unique(std::move(runtime_stage))); } else { - auto code_mapping = runtime_stage.GetCodeMapping(); + auto code_mapping = runtime_stage.GetSkSLMapping(); auto code_size = code_mapping->GetSize(); const char* sksl = reinterpret_cast(code_mapping->GetMapping());