Skip to content

Commit

Permalink
[Impeller] Allow multiple --runtime-stage-* parameters to impellerc (f…
Browse files Browse the repository at this point in the history
…lutter#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 flutter/flutter#140817
  • Loading branch information
dnfield authored Jan 3, 2024
1 parent 9371184 commit 7572468
Show file tree
Hide file tree
Showing 16 changed files with 348 additions and 219 deletions.
232 changes: 117 additions & 115 deletions impeller/compiler/impellerc_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,102 +53,97 @@ 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);
}

// --------------------------------------------------------------------------
/// 2. Output the source file. When in IPLR/RuntimeStage mode, output the
/// serialized IPLR flatbuffer.
///
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 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;
}
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.
///

auto sl_file_name = std::filesystem::absolute(
std::filesystem::current_path() / switches.sl_file_name);
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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 =
Expand All @@ -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()) {
Expand All @@ -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;
}

Expand Down
Loading

0 comments on commit 7572468

Please sign in to comment.