Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 2c431d0

Browse files
committed
Add ShaderBundleData
1 parent e153419 commit 2c431d0

File tree

9 files changed

+394
-40
lines changed

9 files changed

+394
-40
lines changed

impeller/compiler/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ impeller_component("compiler_lib") {
4848
"runtime_stage_data.h",
4949
"shader_bundle.cc",
5050
"shader_bundle.h",
51+
"shader_bundle_data.cc",
52+
"shader_bundle_data.h",
5153
"source_options.cc",
5254
"source_options.h",
5355
"spirv_compiler.cc",

impeller/compiler/reflector.cc

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ Reflector::Reflector(Options options,
132132
return;
133133
}
134134

135+
shader_bundle_data_ = GenerateShaderBundleData();
136+
if (!shader_bundle_data_) {
137+
return;
138+
}
139+
135140
is_valid_ = true;
136141
}
137142

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

174+
std::shared_ptr<ShaderBundleData> Reflector::GetShaderBundleData() const {
175+
return shader_bundle_data_;
176+
}
177+
169178
std::optional<nlohmann::json> Reflector::GenerateTemplateArguments() const {
170179
nlohmann::json root;
171180

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

392+
std::shared_ptr<ShaderBundleData> Reflector::GenerateShaderBundleData() const {
393+
const auto& entrypoints = compiler_->get_entry_points_and_stages();
394+
if (entrypoints.size() != 1u) {
395+
VALIDATION_LOG << "Single entrypoint not found.";
396+
return nullptr;
397+
}
398+
auto data = std::make_shared<ShaderBundleData>(
399+
options_.entry_point_name, //
400+
entrypoints.front().execution_model, //
401+
options_.target_platform //
402+
);
403+
data->SetShaderData(shader_data_);
404+
405+
// Sort the IR so that the uniforms are in declaration order.
406+
std::vector<spirv_cross::ID> uniforms =
407+
SortUniforms(ir_.get(), compiler_.GetCompiler());
408+
409+
for (auto& sorted_id : uniforms) {
410+
auto var = ir_->ids[sorted_id].get<spirv_cross::SPIRVariable>();
411+
const auto spir_type = compiler_->get_type(var.basetype);
412+
UniformDescription uniform_description;
413+
uniform_description.name = compiler_->get_name(var.self);
414+
uniform_description.location = compiler_->get_decoration(
415+
var.self, spv::Decoration::DecorationLocation);
416+
uniform_description.type = spir_type.basetype;
417+
uniform_description.rows = spir_type.vecsize;
418+
uniform_description.columns = spir_type.columns;
419+
uniform_description.bit_width = spir_type.width;
420+
uniform_description.array_elements = GetArrayElements(spir_type);
421+
data->AddUniformDescription(std::move(uniform_description));
422+
}
423+
424+
// We only need to worry about storing vertex attributes.
425+
if (entrypoints.front().execution_model == spv::ExecutionModelVertex) {
426+
const auto inputs = compiler_->get_shader_resources().stage_inputs;
427+
auto input_offsets = ComputeOffsets(inputs);
428+
for (const auto& input : inputs) {
429+
auto location = compiler_->get_decoration(
430+
input.id, spv::Decoration::DecorationLocation);
431+
std::optional<size_t> offset = input_offsets[location];
432+
433+
const auto type = compiler_->get_type(input.type_id);
434+
435+
InputDescription input_description;
436+
input_description.name = input.name;
437+
input_description.location = compiler_->get_decoration(
438+
input.id, spv::Decoration::DecorationLocation);
439+
input_description.set = compiler_->get_decoration(
440+
input.id, spv::Decoration::DecorationDescriptorSet);
441+
input_description.binding = compiler_->get_decoration(
442+
input.id, spv::Decoration::DecorationBinding);
443+
input_description.type = type.basetype;
444+
input_description.bit_width = type.width;
445+
input_description.vec_size = type.vecsize;
446+
input_description.columns = type.columns;
447+
input_description.offset = offset.value_or(0u);
448+
data->AddInputDescription(std::move(input_description));
449+
}
450+
}
451+
452+
return data;
453+
}
454+
383455
std::optional<uint32_t> Reflector::GetArrayElements(
384456
const spirv_cross::SPIRType& type) const {
385457
if (type.array.empty()) {

impeller/compiler/reflector.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "flutter/fml/mapping.h"
1414
#include "impeller/compiler/compiler_backend.h"
1515
#include "impeller/compiler/runtime_stage_data.h"
16+
#include "impeller/compiler/shader_bundle_data.h"
1617
#include "inja/inja.hpp"
1718
#include "spirv_msl.hpp"
1819
#include "spirv_parser.hpp"
@@ -74,6 +75,8 @@ class Reflector {
7475

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

78+
std::shared_ptr<ShaderBundleData> GetShaderBundleData() const;
79+
7780
private:
7881
struct StructDefinition {
7982
std::string name;
@@ -101,6 +104,7 @@ class Reflector {
101104
std::shared_ptr<fml::Mapping> reflection_header_;
102105
std::shared_ptr<fml::Mapping> reflection_cc_;
103106
std::shared_ptr<RuntimeStageData> runtime_stage_data_;
107+
std::shared_ptr<ShaderBundleData> shader_bundle_data_;
104108
bool is_valid_ = false;
105109

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

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

118+
std::shared_ptr<ShaderBundleData> GenerateShaderBundleData() const;
119+
114120
std::shared_ptr<fml::Mapping> InflateTemplate(std::string_view tmpl) const;
115121

116122
std::optional<nlohmann::json::object_t> ReflectResource(

impeller/compiler/runtime_stage_data.h

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,6 @@
1717
namespace impeller {
1818
namespace compiler {
1919

20-
struct UniformDescription {
21-
std::string name;
22-
size_t location = 0u;
23-
spirv_cross::SPIRType::BaseType type = spirv_cross::SPIRType::BaseType::Float;
24-
size_t rows = 0u;
25-
size_t columns = 0u;
26-
size_t bit_width = 0u;
27-
std::optional<size_t> array_elements = std::nullopt;
28-
};
29-
30-
struct InputDescription {
31-
std::string name;
32-
size_t location;
33-
size_t set;
34-
size_t binding;
35-
spirv_cross::SPIRType::BaseType type =
36-
spirv_cross::SPIRType::BaseType::Unknown;
37-
size_t bit_width;
38-
size_t vec_size;
39-
size_t columns;
40-
size_t offset;
41-
};
42-
4320
class RuntimeStageData {
4421
public:
4522
RuntimeStageData(std::string entrypoint,

impeller/compiler/shader_bundle.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,14 @@ static std::unique_ptr<fb::RuntimeStageT> GenerateShaderBackendFB(
124124
return nullptr;
125125
}
126126

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

134-
result = stage_data->CreateFlatbuffer();
134+
result = bundle_data->CreateFlatbuffer();
135135
if (!result) {
136136
std::cerr << "Failed to create flatbuffer for bundled shader \""
137137
<< shader_name << "\"." << std::endl;

0 commit comments

Comments
 (0)