Always include SkSL in .iplr files (flutter/engine#37131)

This commit is contained in:
Zachary Anderson
2022-10-28 17:51:06 -07:00
committed by GitHub
parent 715b1af8cc
commit ff9927e3e8
11 changed files with 72 additions and 1 deletions

View File

@@ -81,6 +81,25 @@ bool Main(const fml::CommandLine& command_line) {
.filename()
.native());
// Generate SkSL if needed.
std::shared_ptr<fml::Mapping> 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;

View File

@@ -356,6 +356,9 @@ std::shared_ptr<RuntimeStageData> Reflector::GenerateRuntimeStageData() const {
options_.target_platform //
);
data->SetShaderData(shader_data_);
if (sksl_data_) {
data->SetSkSLData(sksl_data_);
}
ir_->for_each_typed_id<spirv_cross::SPIRVariable>(
[&](uint32_t, const spirv_cross::SPIRVariable& var) {
if (var.storage != spv::StorageClassUniformConstant) {

View File

@@ -94,6 +94,7 @@ class Reflector {
const Options options_;
const std::shared_ptr<const spirv_cross::ParsedIR> ir_;
const std::shared_ptr<fml::Mapping> shader_data_;
const std::shared_ptr<fml::Mapping> sksl_data_;
const CompilerBackend compiler_;
std::unique_ptr<const nlohmann::json> template_arguments_;
std::shared_ptr<fml::Mapping> reflection_header_;

View File

@@ -30,6 +30,10 @@ void RuntimeStageData::SetShaderData(std::shared_ptr<fml::Mapping> shader) {
shader_ = std::move(shader);
}
void RuntimeStageData::SetSkSLData(std::shared_ptr<fml::Mapping> sksl) {
sksl_ = std::move(sksl);
}
static std::optional<fb::Stage> ToStage(spv::ExecutionModel stage) {
switch (stage) {
case spv::ExecutionModel::ExecutionModelVertex:
@@ -138,6 +142,11 @@ std::shared_ptr<fml::Mapping> 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<fb::UniformDescriptionT>();

View File

@@ -37,6 +37,8 @@ class RuntimeStageData {
void SetShaderData(std::shared_ptr<fml::Mapping> shader);
void SetSkSLData(std::shared_ptr<fml::Mapping> sksl);
std::shared_ptr<fml::Mapping> CreateMapping() const;
private:
@@ -45,6 +47,7 @@ class RuntimeStageData {
const TargetPlatform target_platform_;
std::vector<UniformDescription> uniforms_;
std::shared_ptr<fml::Mapping> shader_;
std::shared_ptr<fml::Mapping> sksl_;
FML_DISALLOW_COPY_AND_ASSIGN(RuntimeStageData);
};

View File

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

View File

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

View File

@@ -96,6 +96,12 @@ RuntimeStage::RuntimeStage(std::shared_ptr<fml::Mapping> payload)
[payload = payload_](auto, auto) {} //
);
sksl_mapping_ = std::make_shared<fml::NonOwnedMapping>(
runtime_stage->sksl()->data(), //
runtime_stage->sksl()->size(), //
[payload = payload_](auto, auto) {} //
);
is_valid_ = true;
}
@@ -111,6 +117,10 @@ const std::shared_ptr<fml::Mapping>& RuntimeStage::GetCodeMapping() const {
return code_mapping_;
}
const std::shared_ptr<fml::Mapping>& RuntimeStage::GetSkSLMapping() const {
return sksl_mapping_;
}
const std::vector<RuntimeUniformDescription>& RuntimeStage::GetUniforms()
const {
return uniforms_;

View File

@@ -50,6 +50,7 @@ table RuntimeStage {
entrypoint: string;
uniforms: [UniformDescription];
shader: [ubyte];
sksl: [ubyte];
}
root_type RuntimeStage;

View File

@@ -34,11 +34,14 @@ class RuntimeStage {
const std::shared_ptr<fml::Mapping>& GetCodeMapping() const;
const std::shared_ptr<fml::Mapping>& GetSkSLMapping() const;
private:
RuntimeShaderStage stage_ = RuntimeShaderStage::kVertex;
std::shared_ptr<fml::Mapping> payload_;
std::string entrypoint_;
std::shared_ptr<fml::Mapping> code_mapping_;
std::shared_ptr<fml::Mapping> sksl_mapping_;
std::vector<RuntimeUniformDescription> uniforms_;
bool is_valid_ = false;

View File

@@ -60,7 +60,7 @@ std::string FragmentProgram::initFromAsset(const std::string& asset_name) {
runtime_effect_ = DlRuntimeEffect::MakeImpeller(
std::make_unique<impeller::RuntimeStage>(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<const char*>(code_mapping->GetMapping());