Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -5016,6 +5016,8 @@ ORIGIN: ../../../flutter/impeller/compiler/runtime_stage_data.cc + ../../../flut
ORIGIN: ../../../flutter/impeller/compiler/runtime_stage_data.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/compiler/shader_bundle.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/compiler/shader_bundle.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/compiler/shader_bundle_data.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/compiler/shader_bundle_data.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/compiler/shader_lib/flutter/runtime_effect.glsl + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/compiler/shader_lib/impeller/blending.glsl + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/compiler/shader_lib/impeller/branching.glsl + ../../../flutter/LICENSE
Expand Down Expand Up @@ -7826,6 +7828,8 @@ FILE: ../../../flutter/impeller/compiler/runtime_stage_data.cc
FILE: ../../../flutter/impeller/compiler/runtime_stage_data.h
FILE: ../../../flutter/impeller/compiler/shader_bundle.cc
FILE: ../../../flutter/impeller/compiler/shader_bundle.h
FILE: ../../../flutter/impeller/compiler/shader_bundle_data.cc
FILE: ../../../flutter/impeller/compiler/shader_bundle_data.h
FILE: ../../../flutter/impeller/compiler/shader_lib/flutter/runtime_effect.glsl
FILE: ../../../flutter/impeller/compiler/shader_lib/impeller/blending.glsl
FILE: ../../../flutter/impeller/compiler/shader_lib/impeller/branching.glsl
Expand Down
2 changes: 2 additions & 0 deletions impeller/compiler/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ impeller_component("compiler_lib") {
"runtime_stage_data.h",
"shader_bundle.cc",
"shader_bundle.h",
"shader_bundle_data.cc",
"shader_bundle_data.h",
"source_options.cc",
"source_options.h",
"spirv_compiler.cc",
Expand Down
72 changes: 72 additions & 0 deletions impeller/compiler/reflector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ Reflector::Reflector(Options options,
return;
}

shader_bundle_data_ = GenerateShaderBundleData();
if (!shader_bundle_data_) {
return;
}

is_valid_ = true;
}

Expand Down Expand Up @@ -166,6 +171,10 @@ std::shared_ptr<RuntimeStageData> Reflector::GetRuntimeStageData() const {
return runtime_stage_data_;
}

std::shared_ptr<ShaderBundleData> Reflector::GetShaderBundleData() const {
return shader_bundle_data_;
}

std::optional<nlohmann::json> Reflector::GenerateTemplateArguments() const {
nlohmann::json root;

Expand Down Expand Up @@ -380,6 +389,69 @@ std::shared_ptr<RuntimeStageData> Reflector::GenerateRuntimeStageData() const {
return data;
}

std::shared_ptr<ShaderBundleData> Reflector::GenerateShaderBundleData() const {
const auto& entrypoints = compiler_->get_entry_points_and_stages();
if (entrypoints.size() != 1u) {
VALIDATION_LOG << "Single entrypoint not found.";
return nullptr;
}
auto data = std::make_shared<ShaderBundleData>(
options_.entry_point_name, //
entrypoints.front().execution_model, //
options_.target_platform //
);
data->SetShaderData(shader_data_);

// Sort the IR so that the uniforms are in declaration order.
std::vector<spirv_cross::ID> uniforms =
SortUniforms(ir_.get(), compiler_.GetCompiler());

for (auto& sorted_id : uniforms) {
auto var = ir_->ids[sorted_id].get<spirv_cross::SPIRVariable>();
const auto spir_type = compiler_->get_type(var.basetype);
UniformDescription uniform_description;
uniform_description.name = compiler_->get_name(var.self);
uniform_description.location = compiler_->get_decoration(
var.self, spv::Decoration::DecorationLocation);
uniform_description.type = spir_type.basetype;
uniform_description.rows = spir_type.vecsize;
uniform_description.columns = spir_type.columns;
uniform_description.bit_width = spir_type.width;
uniform_description.array_elements = GetArrayElements(spir_type);
data->AddUniformDescription(std::move(uniform_description));
}

// We only need to worry about storing vertex attributes.
if (entrypoints.front().execution_model == spv::ExecutionModelVertex) {
const auto inputs = compiler_->get_shader_resources().stage_inputs;
auto input_offsets = ComputeOffsets(inputs);
for (const auto& input : inputs) {
auto location = compiler_->get_decoration(
input.id, spv::Decoration::DecorationLocation);
std::optional<size_t> offset = input_offsets[location];

const auto type = compiler_->get_type(input.type_id);

InputDescription input_description;
input_description.name = input.name;
input_description.location = compiler_->get_decoration(
input.id, spv::Decoration::DecorationLocation);
input_description.set = compiler_->get_decoration(
input.id, spv::Decoration::DecorationDescriptorSet);
input_description.binding = compiler_->get_decoration(
input.id, spv::Decoration::DecorationBinding);
input_description.type = type.basetype;
input_description.bit_width = type.width;
input_description.vec_size = type.vecsize;
input_description.columns = type.columns;
input_description.offset = offset.value_or(0u);
data->AddInputDescription(std::move(input_description));
}
}

return data;
}

std::optional<uint32_t> Reflector::GetArrayElements(
const spirv_cross::SPIRType& type) const {
if (type.array.empty()) {
Expand Down
6 changes: 6 additions & 0 deletions impeller/compiler/reflector.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "flutter/fml/mapping.h"
#include "impeller/compiler/compiler_backend.h"
#include "impeller/compiler/runtime_stage_data.h"
#include "impeller/compiler/shader_bundle_data.h"
#include "inja/inja.hpp"
#include "spirv_msl.hpp"
#include "spirv_parser.hpp"
Expand Down Expand Up @@ -74,6 +75,8 @@ class Reflector {

std::shared_ptr<RuntimeStageData> GetRuntimeStageData() const;

std::shared_ptr<ShaderBundleData> GetShaderBundleData() const;

private:
struct StructDefinition {
std::string name;
Expand Down Expand Up @@ -101,6 +104,7 @@ class Reflector {
std::shared_ptr<fml::Mapping> reflection_header_;
std::shared_ptr<fml::Mapping> reflection_cc_;
std::shared_ptr<RuntimeStageData> runtime_stage_data_;
std::shared_ptr<ShaderBundleData> shader_bundle_data_;
bool is_valid_ = false;

std::optional<nlohmann::json> GenerateTemplateArguments() const;
Expand All @@ -111,6 +115,8 @@ class Reflector {

std::shared_ptr<RuntimeStageData> GenerateRuntimeStageData() const;

std::shared_ptr<ShaderBundleData> GenerateShaderBundleData() const;

std::shared_ptr<fml::Mapping> InflateTemplate(std::string_view tmpl) const;

std::optional<nlohmann::json::object_t> ReflectResource(
Expand Down
23 changes: 0 additions & 23 deletions impeller/compiler/runtime_stage_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,6 @@
namespace impeller {
namespace compiler {

struct UniformDescription {
std::string name;
size_t location = 0u;
spirv_cross::SPIRType::BaseType type = spirv_cross::SPIRType::BaseType::Float;
size_t rows = 0u;
size_t columns = 0u;
size_t bit_width = 0u;
std::optional<size_t> array_elements = std::nullopt;
};

struct InputDescription {
std::string name;
size_t location;
size_t set;
size_t binding;
spirv_cross::SPIRType::BaseType type =
spirv_cross::SPIRType::BaseType::Unknown;
size_t bit_width;
size_t vec_size;
size_t columns;
size_t offset;
};

class RuntimeStageData {
public:
RuntimeStageData(std::string entrypoint,
Expand Down
43 changes: 34 additions & 9 deletions impeller/compiler/shader_bundle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ std::optional<ShaderBundleConfig> ParseShaderBundleConfig(
return bundle;
}

static std::unique_ptr<fb::ShaderT> GenerateShaderFB(
static std::unique_ptr<fb::RuntimeStageT> GenerateShaderBackendFB(
TargetPlatform target_platform,
SourceOptions& options,
const std::string& shader_name,
const ShaderConfig& shader_config) {
auto result = std::make_unique<fb::ShaderT>();
result->name = shader_name;
auto result = std::make_unique<fb::RuntimeStageT>();

std::shared_ptr<fml::FileMapping> source_file_mapping =
fml::FileMapping::CreateReadOnly(shader_config.source_file_name);
Expand All @@ -96,6 +96,8 @@ static std::unique_ptr<fb::ShaderT> GenerateShaderFB(
}

/// Override options.
options.target_platform = target_platform;
options.file_name = shader_name; // This is just used for error messages.
options.type = shader_config.type;
options.source_language = shader_config.language;
options.entry_point_name = EntryPointFunctionNameFromSourceName(
Expand All @@ -122,15 +124,15 @@ static std::unique_ptr<fb::ShaderT> GenerateShaderFB(
return nullptr;
}

auto stage_data = reflector->GetRuntimeStageData();
if (!stage_data) {
std::cerr << "Runtime stage information was nil for bundled shader \""
<< shader_name << "\"." << std::endl;
auto bundle_data = reflector->GetShaderBundleData();
if (!bundle_data) {
std::cerr << "Bundled shader information was nil for \"" << shader_name
<< "\"." << std::endl;
return nullptr;
}

result->shader = stage_data->CreateFlatbuffer();
if (!result->shader) {
result = bundle_data->CreateFlatbuffer();
if (!result) {
std::cerr << "Failed to create flatbuffer for bundled shader \""
<< shader_name << "\"." << std::endl;
return nullptr;
Expand All @@ -139,6 +141,29 @@ static std::unique_ptr<fb::ShaderT> GenerateShaderFB(
return result;
}

static std::unique_ptr<fb::ShaderT> GenerateShaderFB(
SourceOptions& options,
const std::string& shader_name,
const ShaderConfig& shader_config) {
auto result = std::make_unique<fb::ShaderT>();
result->name = shader_name;
result->metal_ios = GenerateShaderBackendFB(
TargetPlatform::kMetalIOS, options, shader_name, shader_config);
result->metal_desktop = GenerateShaderBackendFB(
TargetPlatform::kMetalDesktop, options, shader_name, shader_config);
result->opengl_es = GenerateShaderBackendFB(
TargetPlatform::kOpenGLES, options, shader_name, shader_config);
result->opengl_desktop = GenerateShaderBackendFB(
TargetPlatform::kOpenGLDesktop, options, shader_name, shader_config);
result->vulkan = GenerateShaderBackendFB(TargetPlatform::kVulkan, options,
shader_name, shader_config);
if (!(result->metal_ios && result->metal_desktop && result->opengl_es &&
result->opengl_desktop && result->vulkan)) {
return nullptr;
}
return result;
}

std::optional<fb::ShaderBundleT> GenerateShaderBundleFlatbuffer(
const std::string& bundle_config_json,
SourceOptions& options) {
Expand Down
Loading