[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
This commit is contained in:
Dan Field
2024-01-02 22:55:29 -08:00
committed by GitHub
parent cd0ed89b1c
commit 125a9161bb
16 changed files with 353 additions and 224 deletions

View File

@@ -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<RuntimeStageData::Shader> CompileSkSL(
std::shared_ptr<fml::Mapping> 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<RuntimeStageData::Shader> 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<fml::Mapping> 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<fml::Mapping>& source_file_mapping) {
FML_DCHECK(switches.iplr);
RuntimeStageData stages;
std::shared_ptr<RuntimeStageData::Shader> 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<fml::FileMapping> 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;
}

View File

@@ -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<const spirv_cross::ParsedIR> ir,
std::shared_ptr<fml::Mapping> shader_data,
CompilerBackend compiler)
const std::shared_ptr<const spirv_cross::ParsedIR>& ir,
const std::shared_ptr<fml::Mapping>& 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<fml::Mapping> Reflector::GenerateReflectionCC() const {
return InflateTemplate(kReflectionCCTemplate);
}
static std::optional<RuntimeStageBackend> 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<RuntimeStageData::Shader> 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<RuntimeStageData::Shader> 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<spirv_cross::ID> uniforms =

View File

@@ -58,9 +58,9 @@ class Reflector {
};
Reflector(Options options,
std::shared_ptr<const spirv_cross::ParsedIR> ir,
std::shared_ptr<fml::Mapping> shader_data,
CompilerBackend compiler);
const std::shared_ptr<const spirv_cross::ParsedIR>& ir,
const std::shared_ptr<fml::Mapping>& shader_data,
const CompilerBackend& compiler);
~Reflector();

View File

@@ -9,6 +9,7 @@
#include <memory>
#include <optional>
#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<Shader>& data) {
FML_DCHECK(data_.find(backend) == data_.end());
data_[backend] = data;
void RuntimeStageData::AddShader(const std::shared_ptr<Shader>& data) {
FML_DCHECK(data);
FML_DCHECK(data_.find(data->backend) == data_.end());
data_[data->backend] = data;
}
static std::optional<fb::Stage> ToStage(spv::ExecutionModel stage) {

View File

@@ -51,6 +51,7 @@ class RuntimeStageData {
std::vector<UniformDescription> uniforms;
std::vector<InputDescription> inputs;
std::shared_ptr<fml::Mapping> 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<Shader>& data);
void AddShader(const std::shared_ptr<Shader>& data);
std::unique_ptr<fb::RuntimeStagesT> CreateFlatbuffer() const;

View File

@@ -82,7 +82,7 @@ std::optional<ShaderBundleConfig> ParseShaderBundleConfig(
}
static std::unique_ptr<fb::ShaderT> GenerateShaderFB(
SourceOptions& options,
SourceOptions options,
const std::string& shader_name,
const ShaderConfig& shader_config) {
auto result = std::make_unique<fb::ShaderT>();
@@ -130,27 +130,7 @@ static std::unique_ptr<fb::ShaderT> 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<fb::ShaderT> GenerateShaderFB(
std::optional<fb::ShaderBundleT> GenerateShaderBundleFlatbuffer(
const std::string& bundle_config_json,
SourceOptions& options) {
const SourceOptions& options) {
// --------------------------------------------------------------------------
/// 1. Parse the bundle configuration.
///
@@ -192,13 +172,13 @@ std::optional<fb::ShaderBundleT> 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.

View File

@@ -24,12 +24,12 @@ std::optional<ShaderBundleConfig> ParseShaderBundleConfig(
/// directly.
std::optional<fb::ShaderBundleT> 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

View File

@@ -10,6 +10,7 @@
#include <map>
#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<std::string, TargetPlatform> kKnownPlatforms = {
{"vulkan", TargetPlatform::kVulkan},
{"opengl-es", TargetPlatform::kOpenGLES},
{"opengl-desktop", TargetPlatform::kOpenGLDesktop},
};
static const std::map<std::string, TargetPlatform> kKnownRuntimeStages = {
{"sksl", TargetPlatform::kSkSL},
{"runtime-stage-metal", TargetPlatform::kRuntimeStageMetal},
{"runtime-stage-gles", TargetPlatform::kRuntimeStageGLES},
@@ -35,49 +39,77 @@ static const std::map<std::string, SourceType> 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 <One platform or multiple runtime stages> "
"--input=<source_file> --sl=<sl_output_file> <optional arguments>"
<< 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=<source_file>" << 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=<spirv_output_file> (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=<sl_output_file>" << std::endl;
stream << "--spirv=<spirv_output_file> (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=<entry_point_name> (default: main; "
stream << optional_prefix
<< "--entry-point=<entry_point_name> (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=<bundle_spec> (causes --sl file to be "
stream << optional_prefix
<< "--shader-bundle=<bundle_spec> (causes --sl "
"file to be "
"emitted in Flutter GPU's shader bundle format)"
<< std::endl;
stream << "[optional] --reflection-json=<reflection_json_file>" << std::endl;
stream << "[optional] --reflection-header=<reflection_header_file>"
stream << optional_prefix << "--reflection-json=<reflection_json_file>"
<< std::endl;
stream << "[optional] --reflection-cc=<reflection_cc_file>" << std::endl;
stream << "[optional,multiple] --include=<include_directory>" << std::endl;
stream << "[optional,multiple] --define=<define>" << std::endl;
stream << "[optional] --depfile=<depfile_path>" << std::endl;
stream << "[optional] --gles-language-version=<number>" << std::endl;
stream << "[optional] --json" << std::endl;
stream << "[optional] --use-half-textures (force openGL semantics when "
stream << optional_prefix << "--reflection-header=<reflection_header_file>"
<< std::endl;
stream << optional_prefix << "--reflection-cc=<reflection_cc_file>"
<< std::endl;
stream << optional_multiple_prefix << "--include=<include_directory>"
<< std::endl;
stream << optional_multiple_prefix << "--define=<define>" << std::endl;
stream << optional_prefix << "--depfile=<depfile_path>" << std::endl;
stream << optional_prefix << "--gles-language-version=<number>" << 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<TargetPlatform> RuntimeStagesFromCommandLine(
const fml::CommandLine& command_line) {
std::vector<TargetPlatform> 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::UniqueFD>(fml::OpenDirectory(
: working_directory(std::make_shared<fml::UniqueFD>(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<TargetPlatform> 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<TargetPlatform> 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

View File

@@ -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<fml::UniqueFD> working_directory = nullptr;
std::vector<IncludeDir> 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<TargetPlatform> PlatformsToCompile() const;
TargetPlatform SelectDefaultTargetPlatform() const;
// Creates source options from these switches for the specified
// TargetPlatform. Uses SelectDefaultTargetPlatform if not specified.
SourceOptions CreateSourceOptions(
std::optional<TargetPlatform> target_platform = std::nullopt) const;
static void PrintHelp(std::ostream& stream);
private:
// Use |SelectDefaultTargetPlatform|.
TargetPlatform target_platform_ = TargetPlatform::kUnknown;
// Use |PlatformsToCompile|.
std::vector<TargetPlatform> runtime_stages_ = {};
};
} // namespace compiler

View File

@@ -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"
}

View File

@@ -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

View File

@@ -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" ]
}

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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