From 125a9161bbc2dfaae488103747d70ef41c541f82 Mon Sep 17 00:00:00 2001 From: Dan Field Date: Tue, 2 Jan 2024 22:55:29 -0800 Subject: [PATCH] [Impeller] Allow multiple --runtime-stage-* parameters to impellerc (flutter/engine#49475) This patch will be important for Android/Vulkan, where we'll need to bundle GLES, Vulkan, and SkSL (at least for now). I've refactored impellerc_main to break up some of the larger methods into more granular ones. I've also changed the way that the `IPLR` output works so that we create as many compilers as runtime stages requested. We still are in a weird situation where we create one "Default" compiler for the reflection data and depfile. I don't think that needs to be addressed in this patch, but we should do something about this. I've talked a little with @bdero and @jonahwilliams - it will probably involve something like making the generated reflection data a bit more flexible than it currently is so that it can more ergonomically capture different platform anomalies - we could do this either by having a compiler type that is explicitly for generating reflection data rather than using one arbitrary one and making the reflector try to be platform agnostic. I've filed a couple TODOs related to this and linked them in the patch. Fixes https://github.com/flutter/flutter/issues/140817 --- .../impeller/compiler/impellerc_main.cc | 242 +++++++++--------- .../flutter/impeller/compiler/reflector.cc | 47 +++- .../src/flutter/impeller/compiler/reflector.h | 6 +- .../impeller/compiler/runtime_stage_data.cc | 9 +- .../impeller/compiler/runtime_stage_data.h | 4 +- .../impeller/compiler/shader_bundle.cc | 32 +-- .../flutter/impeller/compiler/shader_bundle.h | 4 +- .../src/flutter/impeller/compiler/switches.cc | 137 ++++++++-- .../src/flutter/impeller/compiler/switches.h | 20 +- engine/src/flutter/impeller/fixtures/BUILD.gn | 14 +- .../runtime_stage/runtime_stage_unittests.cc | 16 ++ .../src/flutter/impeller/tools/impeller.gni | 31 +-- .../flutter/lib/ui/fixtures/shaders/BUILD.gn | 9 +- .../fixtures/shaders/general_shaders/BUILD.gn | 2 +- .../supported_glsl_op_shaders/BUILD.gn | 2 +- .../shaders/supported_op_shaders/BUILD.gn | 2 +- 16 files changed, 353 insertions(+), 224 deletions(-) diff --git a/engine/src/flutter/impeller/compiler/impellerc_main.cc b/engine/src/flutter/impeller/compiler/impellerc_main.cc index 9f3266681a..f9ecc73d6c 100644 --- a/engine/src/flutter/impeller/compiler/impellerc_main.cc +++ b/engine/src/flutter/impeller/compiler/impellerc_main.cc @@ -20,20 +20,31 @@ namespace impeller { namespace compiler { +static Reflector::Options CreateReflectorOptions(const SourceOptions& options, + const Switches& switches) { + Reflector::Options reflector_options; + reflector_options.target_platform = options.target_platform; + reflector_options.entry_point_name = options.entry_point_name; + reflector_options.shader_name = + InferShaderNameFromPath(switches.source_file_name); + reflector_options.header_file_name = Utf8FromPath( + std::filesystem::path{switches.reflection_header_name}.filename()); + return reflector_options; +} + /// Run the shader compiler to geneate SkSL reflection data. /// If there is an error, prints error text and returns `nullptr`. static std::shared_ptr CompileSkSL( std::shared_ptr source_file_mapping, - SourceOptions& options, - Reflector::Options& reflector_options) { - SourceOptions sksl_options = options; - sksl_options.target_platform = TargetPlatform::kSkSL; + const Switches& switches) { + auto options = switches.CreateSourceOptions(TargetPlatform::kSkSL); - Reflector::Options sksl_reflector_options = reflector_options; + Reflector::Options sksl_reflector_options = + CreateReflectorOptions(options, switches); sksl_reflector_options.target_platform = TargetPlatform::kSkSL; - Compiler sksl_compiler = Compiler(std::move(source_file_mapping), - sksl_options, sksl_reflector_options); + Compiler sksl_compiler = + Compiler(std::move(source_file_mapping), options, sksl_reflector_options); if (!sksl_compiler.IsValid()) { std::cerr << "Compilation to SkSL failed." << std::endl; std::cerr << sksl_compiler.GetErrorMessages() << std::endl; @@ -42,26 +53,77 @@ static std::shared_ptr CompileSkSL( return sksl_compiler.GetReflector()->GetRuntimeStageShaderData(); } -/// Outputs artifacts for a single compiler invocation and option configuration. -/// If there is an error, prints error text and returns `false`. -static bool OutputArtifacts(Compiler& compiler, - Switches& switches, - std::shared_ptr source_file_mapping, - SourceOptions& options, - Reflector::Options& reflector_options) { - // -------------------------------------------------------------------------- - /// 1. Invoke the compiler to generate SkSL if needed. - /// +static bool OutputIPLR( + const Switches& switches, + const std::shared_ptr& source_file_mapping) { + FML_DCHECK(switches.iplr); + RuntimeStageData stages; std::shared_ptr sksl_shader; - if (switches.iplr && TargetPlatformBundlesSkSL(switches.target_platform)) { - sksl_shader = - CompileSkSL(std::move(source_file_mapping), options, reflector_options); + if (TargetPlatformBundlesSkSL(switches.SelectDefaultTargetPlatform())) { + sksl_shader = CompileSkSL(source_file_mapping, switches); if (!sksl_shader) { return false; } + stages.AddShader(sksl_shader); } + for (const auto& platform : switches.PlatformsToCompile()) { + if (platform == TargetPlatform::kSkSL) { + // Already handled above. + continue; + } + SourceOptions options = switches.CreateSourceOptions(platform); + + // Invoke the compiler and generate reflection data for a single shader. + + Reflector::Options reflector_options = + CreateReflectorOptions(options, switches); + Compiler compiler(source_file_mapping, options, reflector_options); + if (!compiler.IsValid()) { + std::cerr << "Compilation failed." << std::endl; + std::cerr << compiler.GetErrorMessages() << std::endl; + return false; + } + + auto reflector = compiler.GetReflector(); + if (reflector == nullptr) { + std::cerr << "Could not create reflector." << std::endl; + return false; + } + + auto stage_data = reflector->GetRuntimeStageShaderData(); + if (!stage_data) { + std::cerr << "Runtime stage information was nil." << std::endl; + return false; + } + + stages.AddShader(stage_data); + } + + auto stage_data_mapping = switches.json_format ? stages.CreateJsonMapping() + : stages.CreateMapping(); + if (!stage_data_mapping) { + std::cerr << "Runtime stage data could not be created." << std::endl; + return false; + } + if (!fml::WriteAtomically(*switches.working_directory, // + Utf8FromPath(switches.sl_file_name).c_str(), // + *stage_data_mapping // + )) { + std::cerr << "Could not write file to " << switches.sl_file_name + << std::endl; + return false; + } + // Tools that consume the runtime stage data expect the access mode to + // be 0644. + if (!SetPermissiveAccess(switches.sl_file_name)) { + return false; + } + return true; +} + +static bool OutputSLFile(const Compiler& compiler, const Switches& switches) { // -------------------------------------------------------------------------- /// 2. Output the source file. When in IPLR/RuntimeStage mode, output the /// serialized IPLR flatbuffer. @@ -69,75 +131,19 @@ static bool OutputArtifacts(Compiler& compiler, auto sl_file_name = std::filesystem::absolute( std::filesystem::current_path() / switches.sl_file_name); - if (switches.iplr) { - auto reflector = compiler.GetReflector(); - if (reflector == nullptr) { - std::cerr << "Could not create reflector." << std::endl; - return false; - } - auto stage_data = reflector->GetRuntimeStageShaderData(); - if (!stage_data) { - std::cerr << "Runtime stage information was nil." << std::endl; - return false; - } - RuntimeStageData stages; - if (sksl_shader) { - stages.AddShader(RuntimeStageBackend::kSkSL, sksl_shader); - } - switch (switches.target_platform) { - case TargetPlatform::kUnknown: - case TargetPlatform::kMetalDesktop: - case TargetPlatform::kMetalIOS: - case TargetPlatform::kOpenGLES: - case TargetPlatform::kOpenGLDesktop: - case TargetPlatform::kVulkan: - std::cerr << "TargetPlatform " - << TargetPlatformToString(switches.target_platform) - << " not supported for IPLR."; - return false; - case TargetPlatform::kRuntimeStageMetal: - stages.AddShader(RuntimeStageBackend::kMetal, stage_data); - break; - case TargetPlatform::kRuntimeStageGLES: - stages.AddShader(RuntimeStageBackend::kOpenGLES, stage_data); - break; - case TargetPlatform::kRuntimeStageVulkan: - stages.AddShader(RuntimeStageBackend::kVulkan, stage_data); - break; - case TargetPlatform::kSkSL: - // Already handled above. - break; - } - - auto stage_data_mapping = options.json_format ? stages.CreateJsonMapping() - : stages.CreateMapping(); - if (!stage_data_mapping) { - std::cerr << "Runtime stage data could not be created." << std::endl; - return false; - } - if (!fml::WriteAtomically(*switches.working_directory, // - Utf8FromPath(sl_file_name).c_str(), // - *stage_data_mapping // - )) { - std::cerr << "Could not write file to " << switches.sl_file_name - << std::endl; - return false; - } - // Tools that consume the runtime stage data expect the access mode to - // be 0644. - if (!SetPermissiveAccess(sl_file_name)) { - return false; - } - } else { - if (!fml::WriteAtomically(*switches.working_directory, - Utf8FromPath(sl_file_name).c_str(), - *compiler.GetSLShaderSource())) { - std::cerr << "Could not write file to " << switches.sl_file_name - << std::endl; - return false; - } + if (!fml::WriteAtomically(*switches.working_directory, + Utf8FromPath(sl_file_name).c_str(), + *compiler.GetSLShaderSource())) { + std::cerr << "Could not write file to " << switches.sl_file_name + << std::endl; + return false; } + return true; +} +static bool OutputReflectionData(const Compiler& compiler, + const Switches& switches, + const SourceOptions& options) { // -------------------------------------------------------------------------- /// 3. Output shader reflection data. /// May include a JSON file, a C++ header, and/or a C++ TU. @@ -184,14 +190,17 @@ static bool OutputArtifacts(Compiler& compiler, } } } + return true; +} +static bool OutputDepfile(const Compiler& compiler, const Switches& switches) { // -------------------------------------------------------------------------- /// 4. Output a depfile. /// if (!switches.depfile_path.empty()) { std::string result_file; - switch (switches.target_platform) { + switch (switches.SelectDefaultTargetPlatform()) { case TargetPlatform::kMetalDesktop: case TargetPlatform::kMetalIOS: case TargetPlatform::kOpenGLES: @@ -234,31 +243,11 @@ bool Main(const fml::CommandLine& command_line) { Switches::PrintHelp(std::cerr); return false; } - SourceOptions options; - options.target_platform = switches.target_platform; - options.source_language = switches.source_language; - if (switches.input_type == SourceType::kUnknown) { - options.type = SourceTypeFromFileName(switches.source_file_name); - } else { - options.type = switches.input_type; - } - options.working_directory = switches.working_directory; - options.file_name = switches.source_file_name; - 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.entry_point); - options.json_format = switches.json_format; - options.gles_language_version = switches.gles_language_version; - options.metal_version = switches.metal_version; - options.use_half_textures = switches.use_half_textures; - options.require_framebuffer_fetch = switches.require_framebuffer_fetch; if (!switches.shader_bundle.empty()) { // Invoke the compiler multiple times to build a shader bundle with the // given shader_bundle spec. - return GenerateShaderBundle(switches, options); + return GenerateShaderBundle(switches); } std::shared_ptr source_file_mapping = @@ -268,16 +257,22 @@ bool Main(const fml::CommandLine& command_line) { return false; } - // Invoke the compiler and generate reflection data for a single shader or - // runtime stage IPLR. + if (switches.iplr && !OutputIPLR(switches, source_file_mapping)) { + return false; + } - Reflector::Options reflector_options; - reflector_options.target_platform = switches.target_platform; - reflector_options.entry_point_name = options.entry_point_name; - reflector_options.shader_name = - InferShaderNameFromPath(switches.source_file_name); - reflector_options.header_file_name = Utf8FromPath( - std::filesystem::path{switches.reflection_header_name}.filename()); + // Create at least one compiler to output the SL file, reflection data, and a + // depfile. + // TODO(dnfield): This seems off. We should more explicitly handle how we + // generate reflection and depfile data for the runtime stage case. + // https://github.com/flutter/flutter/issues/140841 + + SourceOptions options = switches.CreateSourceOptions(); + + // Invoke the compiler and generate reflection data for a single shader. + + Reflector::Options reflector_options = + CreateReflectorOptions(options, switches); Compiler compiler(source_file_mapping, options, reflector_options); if (!compiler.IsValid()) { @@ -296,8 +291,15 @@ bool Main(const fml::CommandLine& command_line) { return false; } - if (!OutputArtifacts(compiler, switches, std::move(source_file_mapping), - options, reflector_options)) { + if (!switches.iplr && !OutputSLFile(compiler, switches)) { + return false; + } + + if (!OutputReflectionData(compiler, switches, options)) { + return false; + } + + if (!OutputDepfile(compiler, switches)) { return false; } diff --git a/engine/src/flutter/impeller/compiler/reflector.cc b/engine/src/flutter/impeller/compiler/reflector.cc index efb0ec4fde..4f0af09d56 100644 --- a/engine/src/flutter/impeller/compiler/reflector.cc +++ b/engine/src/flutter/impeller/compiler/reflector.cc @@ -15,8 +15,10 @@ #include "impeller/base/strings.h" #include "impeller/base/validation.h" #include "impeller/compiler/code_gen_template.h" +#include "impeller/compiler/types.h" #include "impeller/compiler/uniform_sorter.h" #include "impeller/compiler/utilities.h" +#include "impeller/core/runtime_types.h" #include "impeller/geometry/half.h" #include "impeller/geometry/matrix.h" #include "impeller/geometry/scalar.h" @@ -81,7 +83,7 @@ static std::string ExecutionModelToString(spv::ExecutionModel model) { } } -static std::string StringToShaderStage(std::string str) { +static std::string StringToShaderStage(const std::string& str) { if (str == "vertex") { return "ShaderStage::kVertex"; } @@ -98,13 +100,13 @@ static std::string StringToShaderStage(std::string str) { } Reflector::Reflector(Options options, - std::shared_ptr ir, - std::shared_ptr shader_data, - CompilerBackend compiler) + const std::shared_ptr& ir, + const std::shared_ptr& shader_data, + const CompilerBackend& compiler) : options_(std::move(options)), - ir_(std::move(ir)), - shader_data_(std::move(shader_data)), - compiler_(std::move(compiler)) { + ir_(ir), + shader_data_(shader_data), + compiler_(compiler) { if (!ir_ || !compiler_) { return; } @@ -128,9 +130,6 @@ Reflector::Reflector(Options options, } runtime_stage_shader_ = GenerateRuntimeStageData(); - if (!runtime_stage_shader_) { - return; - } is_valid_ = true; } @@ -318,8 +317,35 @@ std::shared_ptr Reflector::GenerateReflectionCC() const { return InflateTemplate(kReflectionCCTemplate); } +static std::optional GetRuntimeStageBackend( + TargetPlatform target_platform) { + switch (target_platform) { + case TargetPlatform::kUnknown: + case TargetPlatform::kMetalDesktop: + case TargetPlatform::kMetalIOS: + case TargetPlatform::kOpenGLES: + case TargetPlatform::kOpenGLDesktop: + case TargetPlatform::kVulkan: + return std::nullopt; + case TargetPlatform::kRuntimeStageMetal: + return RuntimeStageBackend::kMetal; + case TargetPlatform::kRuntimeStageGLES: + return RuntimeStageBackend::kOpenGLES; + case TargetPlatform::kRuntimeStageVulkan: + return RuntimeStageBackend::kVulkan; + case TargetPlatform::kSkSL: + return RuntimeStageBackend::kSkSL; + } + FML_UNREACHABLE(); +} + std::shared_ptr Reflector::GenerateRuntimeStageData() const { + auto backend = GetRuntimeStageBackend(options_.target_platform); + if (!backend.has_value()) { + return nullptr; + } + const auto& entrypoints = compiler_->get_entry_points_and_stages(); if (entrypoints.size() != 1u) { VALIDATION_LOG << "Single entrypoint not found."; @@ -329,6 +355,7 @@ std::shared_ptr Reflector::GenerateRuntimeStageData() data->entrypoint = options_.entry_point_name; data->stage = entrypoints.front().execution_model; data->shader = shader_data_; + data->backend = backend.value(); // Sort the IR so that the uniforms are in declaration order. std::vector uniforms = diff --git a/engine/src/flutter/impeller/compiler/reflector.h b/engine/src/flutter/impeller/compiler/reflector.h index 2ebfc6fbde..37141e4c38 100644 --- a/engine/src/flutter/impeller/compiler/reflector.h +++ b/engine/src/flutter/impeller/compiler/reflector.h @@ -58,9 +58,9 @@ class Reflector { }; Reflector(Options options, - std::shared_ptr ir, - std::shared_ptr shader_data, - CompilerBackend compiler); + const std::shared_ptr& ir, + const std::shared_ptr& shader_data, + const CompilerBackend& compiler); ~Reflector(); diff --git a/engine/src/flutter/impeller/compiler/runtime_stage_data.cc b/engine/src/flutter/impeller/compiler/runtime_stage_data.cc index 3e9497664a..e6d8cdeead 100644 --- a/engine/src/flutter/impeller/compiler/runtime_stage_data.cc +++ b/engine/src/flutter/impeller/compiler/runtime_stage_data.cc @@ -9,6 +9,7 @@ #include #include +#include "fml/backtrace.h" #include "impeller/core/runtime_types.h" #include "inja/inja.hpp" @@ -23,10 +24,10 @@ RuntimeStageData::RuntimeStageData() = default; RuntimeStageData::~RuntimeStageData() = default; -void RuntimeStageData::AddShader(RuntimeStageBackend backend, - const std::shared_ptr& data) { - FML_DCHECK(data_.find(backend) == data_.end()); - data_[backend] = data; +void RuntimeStageData::AddShader(const std::shared_ptr& data) { + FML_DCHECK(data); + FML_DCHECK(data_.find(data->backend) == data_.end()); + data_[data->backend] = data; } static std::optional ToStage(spv::ExecutionModel stage) { diff --git a/engine/src/flutter/impeller/compiler/runtime_stage_data.h b/engine/src/flutter/impeller/compiler/runtime_stage_data.h index c54efc8079..f09e3eeca3 100644 --- a/engine/src/flutter/impeller/compiler/runtime_stage_data.h +++ b/engine/src/flutter/impeller/compiler/runtime_stage_data.h @@ -51,6 +51,7 @@ class RuntimeStageData { std::vector uniforms; std::vector inputs; std::shared_ptr shader; + RuntimeStageBackend backend; Shader(const Shader&) = delete; Shader& operator=(const Shader&) = delete; @@ -60,8 +61,7 @@ class RuntimeStageData { ~RuntimeStageData(); - void AddShader(RuntimeStageBackend backend, - const std::shared_ptr& data); + void AddShader(const std::shared_ptr& data); std::unique_ptr CreateFlatbuffer() const; diff --git a/engine/src/flutter/impeller/compiler/shader_bundle.cc b/engine/src/flutter/impeller/compiler/shader_bundle.cc index d32f225169..dc63e4f62a 100644 --- a/engine/src/flutter/impeller/compiler/shader_bundle.cc +++ b/engine/src/flutter/impeller/compiler/shader_bundle.cc @@ -82,7 +82,7 @@ std::optional ParseShaderBundleConfig( } static std::unique_ptr GenerateShaderFB( - SourceOptions& options, + SourceOptions options, const std::string& shader_name, const ShaderConfig& shader_config) { auto result = std::make_unique(); @@ -130,27 +130,7 @@ static std::unique_ptr GenerateShaderFB( return nullptr; } RuntimeStageData stages; - switch (options.target_platform) { - case TargetPlatform::kUnknown: - case TargetPlatform::kMetalDesktop: - case TargetPlatform::kMetalIOS: - case TargetPlatform::kOpenGLES: - case TargetPlatform::kOpenGLDesktop: - case TargetPlatform::kVulkan: - case TargetPlatform::kSkSL: - std::cerr << "Invalid target platform " - << TargetPlatformToString(options.target_platform); - return nullptr; - case TargetPlatform::kRuntimeStageMetal: - stages.AddShader(RuntimeStageBackend::kMetal, stage_data); - break; - case TargetPlatform::kRuntimeStageGLES: - stages.AddShader(RuntimeStageBackend::kOpenGLES, stage_data); - break; - case TargetPlatform::kRuntimeStageVulkan: - stages.AddShader(RuntimeStageBackend::kVulkan, stage_data); - break; - } + stages.AddShader(stage_data); result->shader = stages.CreateFlatbuffer(); if (!result->shader) { std::cerr << "Failed to create flatbuffer for bundled shader \"" @@ -163,7 +143,7 @@ static std::unique_ptr GenerateShaderFB( std::optional GenerateShaderBundleFlatbuffer( const std::string& bundle_config_json, - SourceOptions& options) { + const SourceOptions& options) { // -------------------------------------------------------------------------- /// 1. Parse the bundle configuration. /// @@ -192,13 +172,13 @@ std::optional GenerateShaderBundleFlatbuffer( return shader_bundle; } -bool GenerateShaderBundle(Switches& switches, SourceOptions& options) { +bool GenerateShaderBundle(Switches& switches) { // -------------------------------------------------------------------------- /// 1. Parse the shader bundle and generate the flatbuffer result. /// - auto shader_bundle = - GenerateShaderBundleFlatbuffer(switches.shader_bundle, options); + auto shader_bundle = GenerateShaderBundleFlatbuffer( + switches.shader_bundle, switches.CreateSourceOptions()); if (!shader_bundle.has_value()) { // Specific error messages are already handled by // GenerateShaderBundleFlatbuffer. diff --git a/engine/src/flutter/impeller/compiler/shader_bundle.h b/engine/src/flutter/impeller/compiler/shader_bundle.h index 438e5a56d7..3af4b28fae 100644 --- a/engine/src/flutter/impeller/compiler/shader_bundle.h +++ b/engine/src/flutter/impeller/compiler/shader_bundle.h @@ -24,12 +24,12 @@ std::optional ParseShaderBundleConfig( /// directly. std::optional GenerateShaderBundleFlatbuffer( const std::string& bundle_config_json, - SourceOptions& options); + const SourceOptions& options); /// @brief Parses the JSON shader bundle configuration and invokes the /// compiler multiple times to produce a shader bundle flatbuffer, which /// is then output to the `sl` file. -bool GenerateShaderBundle(Switches& switches, SourceOptions& options); +bool GenerateShaderBundle(Switches& switches); } // namespace compiler } // namespace impeller diff --git a/engine/src/flutter/impeller/compiler/switches.cc b/engine/src/flutter/impeller/compiler/switches.cc index 46a65c246f..c1f8ac6d6a 100644 --- a/engine/src/flutter/impeller/compiler/switches.cc +++ b/engine/src/flutter/impeller/compiler/switches.cc @@ -10,6 +10,7 @@ #include #include "flutter/fml/file.h" +#include "fml/command_line.h" #include "impeller/compiler/types.h" #include "impeller/compiler/utilities.h" @@ -22,6 +23,9 @@ static const std::map kKnownPlatforms = { {"vulkan", TargetPlatform::kVulkan}, {"opengl-es", TargetPlatform::kOpenGLES}, {"opengl-desktop", TargetPlatform::kOpenGLDesktop}, +}; + +static const std::map kKnownRuntimeStages = { {"sksl", TargetPlatform::kSkSL}, {"runtime-stage-metal", TargetPlatform::kRuntimeStageMetal}, {"runtime-stage-gles", TargetPlatform::kRuntimeStageGLES}, @@ -35,49 +39,77 @@ static const std::map kKnownSourceTypes = { }; void Switches::PrintHelp(std::ostream& stream) { + // clang-format off + const std::string optional_prefix = "[optional] "; + const std::string optional_multiple_prefix = "[optional,multiple] "; + // clang-format on + stream << std::endl; stream << "ImpellerC is an offline shader processor and reflection engine." << std::endl; stream << "---------------------------------------------------------------" << std::endl; - stream << "Valid Argument are:" << std::endl; + stream << "Expected invocation is:" << std::endl << std::endl; + stream << "./impellerc " + "--input= --sl= " + << std::endl + << std::endl; + + stream << "Valid platforms are:" << std::endl << std::endl; stream << "One of ["; for (const auto& platform : kKnownPlatforms) { stream << " --" << platform.first; } - stream << " ]" << std::endl; - stream << "--input=" << std::endl; - stream << "[optional] --input-type={"; + stream << " ]" << std::endl << std::endl; + + stream << "Valid runtime stages are:" << std::endl << std::endl; + stream << "At least one of ["; + for (const auto& platform : kKnownRuntimeStages) { + stream << " --" << platform.first; + } + stream << " ]" << std::endl << std::endl; + + stream << "Optional arguments:" << std::endl << std::endl; + stream << optional_prefix + << "--spirv= (ignored for --shader-bundle)" + << std::endl; + stream << optional_prefix << "--input-type={"; for (const auto& source_type : kKnownSourceTypes) { stream << source_type.first << ", "; } stream << "}" << std::endl; - stream << "--sl=" << std::endl; - stream << "--spirv= (ignored for --shader-bundle)" + stream << optional_prefix << "--source-language=glsl|hlsl (default: glsl)" << std::endl; - stream << "[optional] --source-language=glsl|hlsl (default: glsl)" - << std::endl; - stream << "[optional] --entry-point= (default: main; " + stream << optional_prefix + << "--entry-point= (default: main; " "ignored for glsl)" << std::endl; - stream << "[optional] --iplr (causes --sl file to be emitted in iplr format)" + stream << optional_prefix + << "--iplr (causes --sl file to be emitted in " + "iplr format)" << std::endl; - stream << "[optional] --shader-bundle= (causes --sl file to be " + stream << optional_prefix + << "--shader-bundle= (causes --sl " + "file to be " "emitted in Flutter GPU's shader bundle format)" << std::endl; - stream << "[optional] --reflection-json=" << std::endl; - stream << "[optional] --reflection-header=" + stream << optional_prefix << "--reflection-json=" << std::endl; - stream << "[optional] --reflection-cc=" << std::endl; - stream << "[optional,multiple] --include=" << std::endl; - stream << "[optional,multiple] --define=" << std::endl; - stream << "[optional] --depfile=" << std::endl; - stream << "[optional] --gles-language-version=" << std::endl; - stream << "[optional] --json" << std::endl; - stream << "[optional] --use-half-textures (force openGL semantics when " + stream << optional_prefix << "--reflection-header=" + << std::endl; + stream << optional_prefix << "--reflection-cc=" + << std::endl; + stream << optional_multiple_prefix << "--include=" + << std::endl; + stream << optional_multiple_prefix << "--define=" << std::endl; + stream << optional_prefix << "--depfile=" << std::endl; + stream << optional_prefix << "--gles-language-version=" << std::endl; + stream << optional_prefix << "--json" << std::endl; + stream << optional_prefix + << "--use-half-textures (force openGL semantics when " "targeting metal)" << std::endl; - stream << "[optional] --require-framebuffer-fetch" << std::endl; + stream << optional_prefix << "--require-framebuffer-fetch" << std::endl; } Switches::Switches() = default; @@ -102,6 +134,17 @@ static TargetPlatform TargetPlatformFromCommandLine( return target; } +static std::vector RuntimeStagesFromCommandLine( + const fml::CommandLine& command_line) { + std::vector stages; + for (const auto& platform : kKnownRuntimeStages) { + if (command_line.HasOption(platform.first)) { + stages.push_back(platform.second); + } + } + return stages; +} + static SourceType SourceTypeFromCommandLine( const fml::CommandLine& command_line) { auto source_type_option = @@ -114,8 +157,7 @@ static SourceType SourceTypeFromCommandLine( } Switches::Switches(const fml::CommandLine& command_line) - : target_platform(TargetPlatformFromCommandLine(command_line)), - working_directory(std::make_shared(fml::OpenDirectory( + : working_directory(std::make_shared(fml::OpenDirectory( Utf8FromPath(std::filesystem::current_path()).c_str(), false, // create if necessary, fml::FilePermission::kRead))), @@ -143,7 +185,9 @@ Switches::Switches(const fml::CommandLine& command_line) command_line.GetOptionValueWithDefault("entry-point", "main")), use_half_textures(command_line.HasOption("use-half-textures")), require_framebuffer_fetch( - command_line.HasOption("require-framebuffer-fetch")) { + command_line.HasOption("require-framebuffer-fetch")), + target_platform_(TargetPlatformFromCommandLine(command_line)), + runtime_stages_(RuntimeStagesFromCommandLine(command_line)) { auto language = ToLowerCase( command_line.GetOptionValueWithDefault("source-language", "glsl")); @@ -200,8 +244,10 @@ bool Switches::AreValid(std::ostream& explain) const { const bool shader_bundle_mode = !shader_bundle.empty(); bool valid = true; - if (target_platform == TargetPlatform::kUnknown) { - explain << "The target platform (only one) was not specified." << std::endl; + if (target_platform_ == TargetPlatform::kUnknown && runtime_stages_.empty()) { + explain << "Either a target platform was not specified, or no runtime " + "stages were specified." + << std::endl; valid = false; } @@ -242,5 +288,44 @@ bool Switches::AreValid(std::ostream& explain) const { return valid; } +std::vector Switches::PlatformsToCompile() const { + if (target_platform_ == TargetPlatform::kUnknown) { + return runtime_stages_; + } + return {target_platform_}; +} + +TargetPlatform Switches::SelectDefaultTargetPlatform() const { + if (target_platform_ == TargetPlatform::kUnknown) { + return runtime_stages_.front(); + } + return target_platform_; +} + +SourceOptions Switches::CreateSourceOptions( + std::optional target_platform) const { + SourceOptions options; + options.target_platform = + target_platform.value_or(SelectDefaultTargetPlatform()); + options.source_language = source_language; + if (input_type == SourceType::kUnknown) { + options.type = SourceTypeFromFileName(source_file_name); + } else { + options.type = input_type; + } + options.working_directory = working_directory; + options.file_name = source_file_name; + options.include_dirs = include_directories; + options.defines = defines; + options.entry_point_name = EntryPointFunctionNameFromSourceName( + source_file_name, options.type, options.source_language, entry_point); + options.json_format = json_format; + options.gles_language_version = gles_language_version; + options.metal_version = metal_version; + options.use_half_textures = use_half_textures; + options.require_framebuffer_fetch = require_framebuffer_fetch; + return options; +} + } // namespace compiler } // namespace impeller diff --git a/engine/src/flutter/impeller/compiler/switches.h b/engine/src/flutter/impeller/compiler/switches.h index ea894606ac..f6ea55649c 100644 --- a/engine/src/flutter/impeller/compiler/switches.h +++ b/engine/src/flutter/impeller/compiler/switches.h @@ -12,13 +12,14 @@ #include "flutter/fml/command_line.h" #include "flutter/fml/unique_fd.h" #include "impeller/compiler/include_dir.h" +#include "impeller/compiler/source_options.h" #include "impeller/compiler/types.h" namespace impeller { namespace compiler { -struct Switches { - TargetPlatform target_platform = TargetPlatform::kUnknown; +class Switches { + public: std::shared_ptr working_directory = nullptr; std::vector include_directories = {}; std::string source_file_name = ""; @@ -51,7 +52,22 @@ struct Switches { bool AreValid(std::ostream& explain) const; + /// A vector containing at least one valid platform. + std::vector PlatformsToCompile() const; + TargetPlatform SelectDefaultTargetPlatform() const; + + // Creates source options from these switches for the specified + // TargetPlatform. Uses SelectDefaultTargetPlatform if not specified. + SourceOptions CreateSourceOptions( + std::optional target_platform = std::nullopt) const; + static void PrintHelp(std::ostream& stream); + + private: + // Use |SelectDefaultTargetPlatform|. + TargetPlatform target_platform_ = TargetPlatform::kUnknown; + // Use |PlatformsToCompile|. + std::vector runtime_stages_ = {}; }; } // namespace compiler diff --git a/engine/src/flutter/impeller/fixtures/BUILD.gn b/engine/src/flutter/impeller/fixtures/BUILD.gn index bce07ac400..473109e2f8 100644 --- a/engine/src/flutter/impeller/fixtures/BUILD.gn +++ b/engine/src/flutter/impeller/fixtures/BUILD.gn @@ -61,13 +61,11 @@ impellerc("runtime_stages") { ] sl_file_extension = "iplr" - # TODO(dnfield): Make impellerc able to bundle multiple non-SkSL shaders. - # https://github.com/flutter/flutter/issues/140817 - if (is_ios || is_mac) { - shader_target_flag = "--runtime-stage-metal" - } else { - shader_target_flag = "--runtime-stage-gles" - } + shader_target_flags = [ + "--runtime-stage-metal", + "--runtime-stage-gles", + ] + iplr = true } @@ -132,7 +130,7 @@ impellerc("flutter_gpu_shaders") { "flutter_gpu_texture.frag", "flutter_gpu_texture.vert", ] - shader_target_flag = "--runtime-stage-metal" + shader_target_flags = [ "--runtime-stage-metal" ] shader_bundle = "{\"UnlitFragment\": {\"type\": \"fragment\", \"file\": \"../../flutter/impeller/fixtures/flutter_gpu_unlit.frag\"}, \"UnlitVertex\": {\"type\": \"vertex\", \"file\": \"../../flutter/impeller/fixtures/flutter_gpu_unlit.vert\"}, \"TextureFragment\": {\"type\": \"fragment\", \"file\": \"../../flutter/impeller/fixtures/flutter_gpu_texture.frag\"}, \"TextureVertex\": {\"type\": \"vertex\", \"file\": \"../../flutter/impeller/fixtures/flutter_gpu_texture.vert\"}}" shader_bundle_output = "playground.shaderbundle" } diff --git a/engine/src/flutter/impeller/runtime_stage/runtime_stage_unittests.cc b/engine/src/flutter/impeller/runtime_stage/runtime_stage_unittests.cc index 082b4a44a5..1d88146010 100644 --- a/engine/src/flutter/impeller/runtime_stage/runtime_stage_unittests.cc +++ b/engine/src/flutter/impeller/runtime_stage/runtime_stage_unittests.cc @@ -9,6 +9,7 @@ #include "flutter/testing/testing.h" #include "impeller/base/allocation.h" #include "impeller/base/validation.h" +#include "impeller/core/runtime_types.h" #include "impeller/core/shader_types.h" #include "impeller/playground/playground.h" #include "impeller/renderer/pipeline_descriptor.h" @@ -277,5 +278,20 @@ TEST_P(RuntimeStageTest, CanCreatePipelineFromRuntimeStage) { ASSERT_NE(pipeline, nullptr); } +TEST_P(RuntimeStageTest, ContainsExpectedShaderTypes) { + auto stages = OpenAssetAsRuntimeStage("ink_sparkle.frag.iplr"); + // Right now, SkSL gets implicitly bundled regardless of what the build rule + // for this test requested. After + // https://github.com/flutter/flutter/issues/138919, this may require a build + // rule change or a new test. + EXPECT_TRUE(stages[RuntimeStageBackend::kSkSL]); + + EXPECT_TRUE(stages[RuntimeStageBackend::kOpenGLES]); + EXPECT_TRUE(stages[RuntimeStageBackend::kMetal]); + // TODO(dnfield): Flip this when + // https://github.com/flutter/flutter/issues/122823 is fixed. + EXPECT_FALSE(stages[RuntimeStageBackend::kVulkan]); +} + } // namespace testing } // namespace impeller diff --git a/engine/src/flutter/impeller/tools/impeller.gni b/engine/src/flutter/impeller/tools/impeller.gni index db66f60a6d..0fdcf260dc 100644 --- a/engine/src/flutter/impeller/tools/impeller.gni +++ b/engine/src/flutter/impeller/tools/impeller.gni @@ -280,7 +280,7 @@ template("_impellerc") { } # Required: shaders The list of shaders inputs to compile. -# Required: shader_target_flag The target flag to append. Valid options: +# Required: shader_target_flags The target flag(s) to append. Valid options: # --sksl # --metal-ios # --metal-desktop @@ -288,6 +288,7 @@ template("_impellerc") { # --opengl-desktop # --vulkan # --runtime-stage-metal +# --runtime-stage-gles # Required: sl_file_extension The file extension to use for output files. # Not required for --shader_bundle mode. # Optional: iplr Causes --sl output to be in iplr/runtime @@ -305,7 +306,7 @@ template("_impellerc") { # flatbuffer. template("impellerc") { assert(defined(invoker.shaders), "Impeller shaders must be specified.") - assert(defined(invoker.shader_target_flag), + assert(defined(invoker.shader_target_flags), "The flag to impellerc for target selection must be specified.") assert(defined(invoker.sl_file_extension) || defined(invoker.shader_bundle), "The extension of the SL file must be specified (metal, glsl, etc..).") @@ -315,7 +316,10 @@ template("impellerc") { "When shader_bundle is specified, shader_output_bundle must also be specified.") } - sksl = invoker.shader_target_flag == "--sksl" + sksl = false + foreach(shader_target_flag, invoker.shader_target_flags) { + sksl = shader_target_flag == "--sksl" + } iplr = false if (defined(invoker.iplr) && invoker.iplr) { iplr = invoker.iplr @@ -347,13 +351,10 @@ template("impellerc") { generated_dir = "$target_gen_dir" } - shader_target_flag = invoker.shader_target_flag + shader_target_flags = invoker.shader_target_flags shader_lib_dir = rebase_path("//flutter/impeller/compiler/shader_lib") - args = [ - "--include=$shader_lib_dir", - "$shader_target_flag", - ] + args = [ "--include=$shader_lib_dir" ] + shader_target_flags # When we're in single invocation mode, we can't use source enumeration. if (!single_invocation) { @@ -414,7 +415,7 @@ template("impellerc") { outputs = [ sl_output ] } else if (sksl) { - # When SkSL is selected as the `shader_target_flag`, don't generate + # When SkSL is selected as a `shader_target_flags`, don't generate # C++ reflection state. Nothing needs to use it and it's likely invalid # given the special cases when generating SkSL. # Note that this configuration is orthogonal to the "--iplr" flag @@ -536,13 +537,13 @@ template("_impeller_shaders_metal") { metal_version = metal_version sl_file_extension = "metal" use_half_textures = use_half_textures - shader_target_flag = "" + shader_target_flags = [] defines = [ "IMPELLER_TARGET_METAL" ] if (is_ios) { - shader_target_flag = "--metal-ios" + shader_target_flags += [ "--metal-ios" ] defines += [ "IMPELLER_TARGET_METAL_IOS" ] } else if (is_mac) { - shader_target_flag = "--metal-desktop" + shader_target_flags = [ "--metal-desktop" ] defines += [ "IMPELLER_TARGET_METAL_DESKTOP" ] } else { assert(false, "Metal not supported on this platform.") @@ -631,9 +632,9 @@ template("_impeller_shaders_gles") { intermediates_subdir = "gles" } if (is_mac) { - shader_target_flag = "--opengl-desktop" + shader_target_flags = [ "--opengl-desktop" ] } else { - shader_target_flag = "--opengl-es" + shader_target_flags = [ "--opengl-es" ] } defines = [ "IMPELLER_TARGET_OPENGLES" ] @@ -710,7 +711,7 @@ template("_impeller_shaders_vk") { intermediates_subdir = "vk" } - shader_target_flag = "--vulkan" + shader_target_flags = [ "--vulkan" ] defines = [ "IMPELLER_TARGET_VULKAN" ] } diff --git a/engine/src/flutter/lib/ui/fixtures/shaders/BUILD.gn b/engine/src/flutter/lib/ui/fixtures/shaders/BUILD.gn index 7d3e2169e3..1ea359df4c 100644 --- a/engine/src/flutter/lib/ui/fixtures/shaders/BUILD.gn +++ b/engine/src/flutter/lib/ui/fixtures/shaders/BUILD.gn @@ -18,7 +18,7 @@ if (enable_unittests) { impellerc("ink_sparkle") { shaders = [ "//flutter/impeller/fixtures/ink_sparkle.frag" ] - shader_target_flag = "--sksl" + shader_target_flags = [ "--sksl" ] intermediates_subdir = "iplr" sl_file_extension = "iplr" iplr = true @@ -26,7 +26,7 @@ if (enable_unittests) { impellerc("ink_sparkle_web") { shaders = [ "//flutter/impeller/fixtures/ink_sparkle.frag" ] - shader_target_flag = "--sksl" + shader_target_flags = [ "--sksl" ] intermediates_subdir = "iplr-json" sl_file_extension = "iplr" iplr = true @@ -38,7 +38,10 @@ if (enable_unittests) { "//flutter/impeller/fixtures/ordering/shader_with_samplers.frag", "//flutter/third_party/test_shaders/selman/glow_shader.frag", ] - shader_target_flag = "--runtime-stage-metal" + shader_target_flags = [ + "--runtime-stage-metal", + "--runtime-stage-gles", + ] intermediates_subdir = "iplr-remap" sl_file_extension = "iplr" iplr = true diff --git a/engine/src/flutter/lib/ui/fixtures/shaders/general_shaders/BUILD.gn b/engine/src/flutter/lib/ui/fixtures/shaders/general_shaders/BUILD.gn index 640823715e..9e9eb0be00 100644 --- a/engine/src/flutter/lib/ui/fixtures/shaders/general_shaders/BUILD.gn +++ b/engine/src/flutter/lib/ui/fixtures/shaders/general_shaders/BUILD.gn @@ -26,7 +26,7 @@ if (enable_unittests) { impellerc("compile_general_shaders") { shaders = test_shaders - shader_target_flag = "--sksl" + shader_target_flags = [ "--sksl" ] intermediates_subdir = "iplr" sl_file_extension = "iplr" iplr = true diff --git a/engine/src/flutter/lib/ui/fixtures/shaders/supported_glsl_op_shaders/BUILD.gn b/engine/src/flutter/lib/ui/fixtures/shaders/supported_glsl_op_shaders/BUILD.gn index e3480feb75..825e46e1ba 100644 --- a/engine/src/flutter/lib/ui/fixtures/shaders/supported_glsl_op_shaders/BUILD.gn +++ b/engine/src/flutter/lib/ui/fixtures/shaders/supported_glsl_op_shaders/BUILD.gn @@ -50,7 +50,7 @@ if (enable_unittests) { impellerc("compile_supported_glsl_shaders") { shaders = test_shaders - shader_target_flag = "--sksl" + shader_target_flags = [ "--sksl" ] intermediates_subdir = "iplr" sl_file_extension = "iplr" iplr = true diff --git a/engine/src/flutter/lib/ui/fixtures/shaders/supported_op_shaders/BUILD.gn b/engine/src/flutter/lib/ui/fixtures/shaders/supported_op_shaders/BUILD.gn index 880500f9c4..b0c3c8a79a 100644 --- a/engine/src/flutter/lib/ui/fixtures/shaders/supported_op_shaders/BUILD.gn +++ b/engine/src/flutter/lib/ui/fixtures/shaders/supported_op_shaders/BUILD.gn @@ -46,7 +46,7 @@ if (enable_unittests) { impellerc("compile_supported_op_shaders") { shaders = test_shaders - shader_target_flag = "--sksl" + shader_target_flags = [ "--sksl" ] intermediates_subdir = "iplr" sl_file_extension = "iplr" iplr = true