5 #ifndef FLUTTER_IMPELLER_COMPILER_REFLECTOR_H_
6 #define FLUTTER_IMPELLER_COMPILER_REFLECTOR_H_
12 #include "flutter/fml/mapping.h"
13 #include "fml/logging.h"
17 #include "inja/inja.hpp"
18 #include "spirv_common.hpp"
19 #include "spirv_msl.hpp"
20 #include "spirv_parser.hpp"
53 using Type = spirv_cross::SPIRType::BaseType;
56 return "ShaderType::kVoid";
58 return "ShaderType::kBoolean";
60 return "ShaderType::kSignedByte";
62 return "ShaderType::kUnsignedByte";
64 return "ShaderType::kSignedShort";
66 return "ShaderType::kUnsignedShort";
68 return "ShaderType::kSignedInt";
70 return "ShaderType::kUnsignedInt";
72 return "ShaderType::kSignedInt64";
74 return "ShaderType::kUnsignedInt64";
75 case Type::AtomicCounter:
76 return "ShaderType::kAtomicCounter";
78 return "ShaderType::kHalfFloat";
80 return "ShaderType::kFloat";
82 return "ShaderType::kDouble";
84 return "ShaderType::kStruct";
86 return "ShaderType::kImage";
87 case Type::SampledImage:
88 return "ShaderType::kSampledImage";
90 return "ShaderType::kSampler";
92 return "ShaderType::kUnknown";
98 spirv_cross::SPIRType::BaseType
type) {
100 case spirv_cross::SPIRType::Void:
102 case spirv_cross::SPIRType::Float:
104 case spirv_cross::SPIRType::Unknown:
105 case spirv_cross::SPIRType::Boolean:
106 case spirv_cross::SPIRType::SByte:
107 case spirv_cross::SPIRType::UByte:
108 case spirv_cross::SPIRType::Short:
109 case spirv_cross::SPIRType::UShort:
110 case spirv_cross::SPIRType::Int:
111 case spirv_cross::SPIRType::UInt:
112 case spirv_cross::SPIRType::Int64:
113 case spirv_cross::SPIRType::UInt64:
114 case spirv_cross::SPIRType::AtomicCounter:
115 case spirv_cross::SPIRType::Half:
116 case spirv_cross::SPIRType::Double:
117 case spirv_cross::SPIRType::Struct:
118 case spirv_cross::SPIRType::Image:
119 case spirv_cross::SPIRType::SampledImage:
120 case spirv_cross::SPIRType::Sampler:
121 case spirv_cross::SPIRType::AccelerationStructure:
122 case spirv_cross::SPIRType::RayQuery:
123 case spirv_cross::SPIRType::ControlPointArray:
124 case spirv_cross::SPIRType::Interpolant:
125 case spirv_cross::SPIRType::Char:
155 spirv_cross::SPIRType::BaseType p_base_type,
159 size_t p_byte_length,
160 std::optional<size_t> p_array_elements,
161 size_t p_element_padding,
162 std::optional<std::string> p_float_type = std::nullopt,
163 size_t p_vec_size = 0u,
164 size_t p_columns = 0u,
190 const std::shared_ptr<const spirv_cross::ParsedIR>& ir,
191 const std::shared_ptr<fml::Mapping>& shader_data,
209 struct StructDefinition {
211 size_t byte_length = 0u;
212 std::vector<StructMember> members;
215 struct BindPrototypeArgument {
217 std::string argument_name;
220 struct BindPrototype {
222 std::string return_type;
223 std::string docstring;
224 std::string descriptor_type =
"";
225 std::vector<BindPrototypeArgument> args;
228 const Options options_;
229 const std::shared_ptr<const spirv_cross::ParsedIR> ir_;
230 const std::shared_ptr<fml::Mapping> shader_data_;
231 const CompilerBackend compiler_;
232 std::unique_ptr<const nlohmann::json> template_arguments_;
233 std::shared_ptr<fml::Mapping> reflection_header_;
234 std::shared_ptr<fml::Mapping> reflection_cc_;
235 std::shared_ptr<RuntimeStageData::Shader> runtime_stage_shader_;
236 std::shared_ptr<ShaderBundleData> shader_bundle_data_;
237 bool is_valid_ =
false;
239 std::optional<nlohmann::json> GenerateTemplateArguments()
const;
241 std::shared_ptr<fml::Mapping> GenerateReflectionHeader()
const;
243 std::shared_ptr<fml::Mapping> GenerateReflectionCC()
const;
245 std::shared_ptr<RuntimeStageData::Shader> GenerateRuntimeStageData()
const;
247 std::shared_ptr<ShaderBundleData> GenerateShaderBundleData()
const;
249 std::shared_ptr<fml::Mapping> InflateTemplate(std::string_view tmpl)
const;
251 std::optional<nlohmann::json::object_t> ReflectResource(
252 const spirv_cross::Resource& resource,
253 std::optional<size_t> offset)
const;
255 std::optional<nlohmann::json::array_t> ReflectResources(
256 const spirv_cross::SmallVector<spirv_cross::Resource>& resources,
257 bool compute_offsets =
false)
const;
259 std::vector<size_t> ComputeOffsets(
260 const spirv_cross::SmallVector<spirv_cross::Resource>& resources)
const;
262 std::optional<size_t> GetOffset(spirv_cross::ID
id,
263 const std::vector<size_t>& offsets)
const;
265 std::optional<nlohmann::json::object_t> ReflectType(
266 const spirv_cross::TypeID& type_id)
const;
268 nlohmann::json::object_t EmitStructDefinition(
269 std::optional<Reflector::StructDefinition> struc)
const;
271 std::optional<StructDefinition> ReflectStructDefinition(
272 const spirv_cross::TypeID& type_id)
const;
274 std::vector<BindPrototype> ReflectBindPrototypes(
275 const spirv_cross::ShaderResources& resources,
276 spv::ExecutionModel execution_model)
const;
278 nlohmann::json::array_t EmitBindPrototypes(
279 const spirv_cross::ShaderResources& resources,
280 spv::ExecutionModel execution_model)
const;
282 std::optional<StructDefinition> ReflectPerVertexStructDefinition(
283 const spirv_cross::SmallVector<spirv_cross::Resource>& stage_inputs)
286 std::optional<std::string> GetMemberNameAtIndexIfExists(
287 const spirv_cross::SPIRType& parent_type,
290 std::string GetMemberNameAtIndex(
const spirv_cross::SPIRType& parent_type,
292 std::string suffix =
"")
const;
294 std::vector<StructMember> ReadStructMembers(
295 const spirv_cross::TypeID& type_id)
const;
297 std::optional<uint32_t> GetArrayElements(
298 const spirv_cross::SPIRType& type)
const;
300 template <u
int32_t Size>
301 uint32_t GetArrayStride(
const spirv_cross::SPIRType& struct_type,
302 const spirv_cross::SPIRType& member_type,
303 uint32_t index)
const {
304 auto element_count = GetArrayElements(member_type).value_or(1);
305 if (element_count <= 1) {
308 return compiler_->type_struct_member_array_stride(struct_type, index);
Reflector(Options options, const std::shared_ptr< const spirv_cross::ParsedIR > &ir, const std::shared_ptr< fml::Mapping > &shader_data, const CompilerBackend &compiler)
std::shared_ptr< fml::Mapping > GetReflectionJSON() const
std::shared_ptr< fml::Mapping > GetReflectionCC() const
std::shared_ptr< RuntimeStageData::Shader > GetRuntimeStageShaderData() const
std::shared_ptr< ShaderBundleData > GetShaderBundleData() const
std::shared_ptr< fml::Mapping > GetReflectionHeader() const
TargetPlatform target_platform
std::string entry_point_name
std::string header_file_name
UnderlyingType underlying_type
static std::string BaseTypeToString(spirv_cross::SPIRType::BaseType type)
std::optional< size_t > array_elements
std::optional< std::string > float_type
StructMember(std::string p_type, spirv_cross::SPIRType::BaseType p_base_type, std::string p_name, size_t p_offset, size_t p_size, size_t p_byte_length, std::optional< size_t > p_array_elements, size_t p_element_padding, std::optional< std::string > p_float_type=std::nullopt, size_t p_vec_size=0u, size_t p_columns=0u, UnderlyingType p_underlying_type=UnderlyingType::kOther)
Constructs a new StructMember.
static UnderlyingType DetermineUnderlyingType(spirv_cross::SPIRType::BaseType type)
spirv_cross::SPIRType::BaseType base_type