9#include "impeller/shader_bundle/shader_bundle_flatbuffers.h"
17 spv::ExecutionModel stage,
19 : entrypoint_(
std::move(entrypoint)),
21 target_platform_(target_platform) {}
26 uniform_structs_.emplace_back(std::move(uniform_struct));
30 uniform_textures_.emplace_back(std::move(uniform_texture));
34 inputs_.emplace_back(std::move(input));
38 shader_ = std::move(shader);
41static std::optional<fb::shaderbundle::ShaderStage>
ToStage(
42 spv::ExecutionModel stage) {
44 case spv::ExecutionModel::ExecutionModelVertex:
45 return fb::shaderbundle::ShaderStage::kVertex;
46 case spv::ExecutionModel::ExecutionModelFragment:
47 return fb::shaderbundle::ShaderStage::kFragment;
48 case spv::ExecutionModel::ExecutionModelGLCompute:
49 return fb::shaderbundle::ShaderStage::kCompute;
57 spirv_cross::SPIRType::BaseType
type) {
59 case spirv_cross::SPIRType::Boolean:
60 return fb::shaderbundle::UniformDataType::kBoolean;
61 case spirv_cross::SPIRType::SByte:
62 return fb::shaderbundle::UniformDataType::kSignedByte;
63 case spirv_cross::SPIRType::UByte:
65 case spirv_cross::SPIRType::Short:
66 return fb::shaderbundle::UniformDataType::kSignedShort;
67 case spirv_cross::SPIRType::UShort:
68 return fb::shaderbundle::UniformDataType::kUnsignedShort;
69 case spirv_cross::SPIRType::Int:
70 return fb::shaderbundle::UniformDataType::kSignedInt;
71 case spirv_cross::SPIRType::UInt:
72 return fb::shaderbundle::UniformDataType::kUnsignedInt;
73 case spirv_cross::SPIRType::Int64:
74 return fb::shaderbundle::UniformDataType::kSignedInt64;
75 case spirv_cross::SPIRType::UInt64:
76 return fb::shaderbundle::UniformDataType::kUnsignedInt64;
77 case spirv_cross::SPIRType::Half:
78 return fb::shaderbundle::UniformDataType::kHalfFloat;
79 case spirv_cross::SPIRType::Float:
81 case spirv_cross::SPIRType::Double:
83 case spirv_cross::SPIRType::SampledImage:
85 case spirv_cross::SPIRType::AccelerationStructure:
86 case spirv_cross::SPIRType::AtomicCounter:
87 case spirv_cross::SPIRType::Char:
88 case spirv_cross::SPIRType::ControlPointArray:
90 case spirv_cross::SPIRType::Interpolant:
91 case spirv_cross::SPIRType::RayQuery:
93 case spirv_cross::SPIRType::Struct:
94 case spirv_cross::SPIRType::Unknown:
95 case spirv_cross::SPIRType::Void:
101 spirv_cross::SPIRType::BaseType
type) {
103 case spirv_cross::SPIRType::Boolean:
104 return fb::shaderbundle::InputDataType::kBoolean;
105 case spirv_cross::SPIRType::SByte:
106 return fb::shaderbundle::InputDataType::kSignedByte;
107 case spirv_cross::SPIRType::UByte:
109 case spirv_cross::SPIRType::Short:
110 return fb::shaderbundle::InputDataType::kSignedShort;
111 case spirv_cross::SPIRType::UShort:
112 return fb::shaderbundle::InputDataType::kUnsignedShort;
113 case spirv_cross::SPIRType::Int:
114 return fb::shaderbundle::InputDataType::kSignedInt;
115 case spirv_cross::SPIRType::UInt:
116 return fb::shaderbundle::InputDataType::kUnsignedInt;
117 case spirv_cross::SPIRType::Int64:
118 return fb::shaderbundle::InputDataType::kSignedInt64;
119 case spirv_cross::SPIRType::UInt64:
120 return fb::shaderbundle::InputDataType::kUnsignedInt64;
121 case spirv_cross::SPIRType::Float:
123 case spirv_cross::SPIRType::Double:
125 case spirv_cross::SPIRType::Unknown:
126 case spirv_cross::SPIRType::Void:
127 case spirv_cross::SPIRType::Half:
128 case spirv_cross::SPIRType::AtomicCounter:
129 case spirv_cross::SPIRType::Struct:
131 case spirv_cross::SPIRType::SampledImage:
133 case spirv_cross::SPIRType::AccelerationStructure:
134 case spirv_cross::SPIRType::RayQuery:
135 case spirv_cross::SPIRType::ControlPointArray:
136 case spirv_cross::SPIRType::Interpolant:
137 case spirv_cross::SPIRType::Char:
143std::unique_ptr<fb::shaderbundle::BackendShaderT>
145 auto shader_bundle = std::make_unique<fb::shaderbundle::BackendShaderT>();
149 shader_bundle->entrypoint = entrypoint_;
150 const auto stage =
ToStage(stage_);
151 if (!stage.has_value()) {
155 shader_bundle->stage = stage.value();
161 if (shader_->GetSize() > 0u) {
162 shader_bundle->shader = {shader_->GetMapping(),
163 shader_->GetMapping() + shader_->GetSize()};
165 for (
const auto& uniform : uniform_structs_) {
166 auto desc = std::make_unique<fb::shaderbundle::ShaderUniformStructT>();
168 desc->name = uniform.name;
169 if (
desc->name.empty()) {
173 desc->ext_res_0 = uniform.ext_res_0;
174 desc->set = uniform.set;
175 desc->binding = uniform.binding;
176 desc->size_in_bytes = uniform.size_in_bytes;
178 for (
const auto& field : uniform.fields) {
180 std::make_unique<fb::shaderbundle::ShaderUniformStructFieldT>();
181 field_desc->name = field.name;
183 if (!
type.has_value()) {
187 field_desc->type =
type.value();
188 field_desc->offset_in_bytes = field.offset_in_bytes;
189 field_desc->element_size_in_bytes = field.element_size_in_bytes;
190 field_desc->total_size_in_bytes = field.total_size_in_bytes;
191 field_desc->array_elements = field.array_elements.value_or(0);
192 desc->fields.push_back(std::move(field_desc));
195 shader_bundle->uniform_structs.emplace_back(std::move(
desc));
198 for (
const auto&
texture : uniform_textures_) {
199 auto desc = std::make_unique<fb::shaderbundle::ShaderUniformTextureT>();
201 if (
desc->name.empty()) {
208 shader_bundle->uniform_textures.emplace_back(std::move(
desc));
211 for (
const auto& input : inputs_) {
212 auto desc = std::make_unique<fb::shaderbundle::ShaderInputT>();
214 desc->name = input.name;
216 if (
desc->name.empty()) {
220 desc->location = input.location;
221 desc->set = input.set;
222 desc->binding = input.binding;
224 if (!input_type.has_value()) {
228 desc->type = input_type.value();
229 desc->bit_width = input.bit_width;
230 desc->vec_size = input.vec_size;
231 desc->columns = input.columns;
232 desc->offset = input.offset;
234 shader_bundle->inputs.emplace_back(std::move(
desc));
237 return shader_bundle;
void AddInputDescription(InputDescription input)
ShaderBundleData(std::string entrypoint, spv::ExecutionModel stage, TargetPlatform target_platform)
std::unique_ptr< fb::shaderbundle::BackendShaderT > CreateFlatbuffer() const
void AddUniformStruct(ShaderUniformStruct uniform_struct)
void SetShaderData(std::shared_ptr< fml::Mapping > shader)
void AddUniformTexture(ShaderUniformTexture uniform_texture)
#define FML_UNREACHABLE()
std::function< ProfileSample(void)> Sampler
Sampler is run during SamplingProfiler::SampleRepeatedly. Each platform should implement its version ...
static std::optional< fb::InputDataType > ToInputType(spirv_cross::SPIRType::BaseType type)
static std::optional< fb::UniformDataType > ToUniformType(spirv_cross::SPIRType::BaseType type)
static std::optional< fb::Stage > ToStage(spv::ExecutionModel stage)