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"
45 using Type = spirv_cross::SPIRType::BaseType;
48 return "ShaderType::kVoid";
50 return "ShaderType::kBoolean";
52 return "ShaderType::kSignedByte";
54 return "ShaderType::kUnsignedByte";
56 return "ShaderType::kSignedShort";
58 return "ShaderType::kUnsignedShort";
60 return "ShaderType::kSignedInt";
62 return "ShaderType::kUnsignedInt";
64 return "ShaderType::kSignedInt64";
66 return "ShaderType::kUnsignedInt64";
67 case Type::AtomicCounter:
68 return "ShaderType::kAtomicCounter";
70 return "ShaderType::kHalfFloat";
72 return "ShaderType::kFloat";
74 return "ShaderType::kDouble";
76 return "ShaderType::kStruct";
78 return "ShaderType::kImage";
79 case Type::SampledImage:
80 return "ShaderType::kSampledImage";
82 return "ShaderType::kSampler";
84 return "ShaderType::kUnknown";
90 spirv_cross::SPIRType::BaseType
type) {
92 case spirv_cross::SPIRType::Void:
94 case spirv_cross::SPIRType::Float:
96 case spirv_cross::SPIRType::Unknown:
97 case spirv_cross::SPIRType::Boolean:
98 case spirv_cross::SPIRType::SByte:
99 case spirv_cross::SPIRType::UByte:
100 case spirv_cross::SPIRType::Short:
101 case spirv_cross::SPIRType::UShort:
102 case spirv_cross::SPIRType::Int:
103 case spirv_cross::SPIRType::UInt:
104 case spirv_cross::SPIRType::Int64:
105 case spirv_cross::SPIRType::UInt64:
106 case spirv_cross::SPIRType::AtomicCounter:
107 case spirv_cross::SPIRType::Half:
108 case spirv_cross::SPIRType::Double:
109 case spirv_cross::SPIRType::Struct:
110 case spirv_cross::SPIRType::Image:
111 case spirv_cross::SPIRType::SampledImage:
112 case spirv_cross::SPIRType::Sampler:
113 case spirv_cross::SPIRType::AccelerationStructure:
114 case spirv_cross::SPIRType::RayQuery:
115 case spirv_cross::SPIRType::ControlPointArray:
116 case spirv_cross::SPIRType::Interpolant:
117 case spirv_cross::SPIRType::Char:
141 spirv_cross::SPIRType::BaseType p_base_type,
145 size_t p_byte_length,
146 std::optional<size_t> p_array_elements,
147 size_t p_element_padding,
170 const std::shared_ptr<const spirv_cross::ParsedIR>& ir,
171 const std::shared_ptr<fml::Mapping>& shader_data,
189 struct StructDefinition {
191 size_t byte_length = 0u;
192 std::vector<StructMember> members;
195 struct BindPrototypeArgument {
197 std::string argument_name;
200 struct BindPrototype {
202 std::string return_type;
203 std::string docstring;
204 std::string descriptor_type =
"";
205 std::vector<BindPrototypeArgument> args;
208 const Options options_;
209 const std::shared_ptr<const spirv_cross::ParsedIR> ir_;
210 const std::shared_ptr<fml::Mapping> shader_data_;
211 const CompilerBackend compiler_;
212 std::unique_ptr<const nlohmann::json> template_arguments_;
213 std::shared_ptr<fml::Mapping> reflection_header_;
214 std::shared_ptr<fml::Mapping> reflection_cc_;
215 std::shared_ptr<RuntimeStageData::Shader> runtime_stage_shader_;
216 std::shared_ptr<ShaderBundleData> shader_bundle_data_;
217 bool is_valid_ =
false;
219 std::optional<nlohmann::json> GenerateTemplateArguments()
const;
221 std::shared_ptr<fml::Mapping> GenerateReflectionHeader()
const;
223 std::shared_ptr<fml::Mapping> GenerateReflectionCC()
const;
225 std::shared_ptr<RuntimeStageData::Shader> GenerateRuntimeStageData()
const;
227 std::shared_ptr<ShaderBundleData> GenerateShaderBundleData()
const;
229 std::shared_ptr<fml::Mapping> InflateTemplate(std::string_view tmpl)
const;
231 std::optional<nlohmann::json::object_t> ReflectResource(
232 const spirv_cross::Resource& resource,
233 std::optional<size_t> offset)
const;
235 std::optional<nlohmann::json::array_t> ReflectResources(
236 const spirv_cross::SmallVector<spirv_cross::Resource>& resources,
237 bool compute_offsets =
false)
const;
239 std::vector<size_t> ComputeOffsets(
240 const spirv_cross::SmallVector<spirv_cross::Resource>& resources)
const;
242 std::optional<size_t> GetOffset(spirv_cross::ID
id,
243 const std::vector<size_t>& offsets)
const;
245 std::optional<nlohmann::json::object_t> ReflectType(
246 const spirv_cross::TypeID& type_id)
const;
248 nlohmann::json::object_t EmitStructDefinition(
249 std::optional<Reflector::StructDefinition> struc)
const;
251 std::optional<StructDefinition> ReflectStructDefinition(
252 const spirv_cross::TypeID& type_id)
const;
254 std::vector<BindPrototype> ReflectBindPrototypes(
255 const spirv_cross::ShaderResources& resources,
256 spv::ExecutionModel execution_model)
const;
258 nlohmann::json::array_t EmitBindPrototypes(
259 const spirv_cross::ShaderResources& resources,
260 spv::ExecutionModel execution_model)
const;
262 std::optional<StructDefinition> ReflectPerVertexStructDefinition(
263 const spirv_cross::SmallVector<spirv_cross::Resource>& stage_inputs)
266 std::optional<std::string> GetMemberNameAtIndexIfExists(
267 const spirv_cross::SPIRType& parent_type,
270 std::string GetMemberNameAtIndex(
const spirv_cross::SPIRType& parent_type,
272 std::string suffix =
"")
const;
274 std::vector<StructMember> ReadStructMembers(
275 const spirv_cross::TypeID& type_id)
const;
277 std::optional<uint32_t> GetArrayElements(
278 const spirv_cross::SPIRType&
type)
const;
280 template <u
int32_t Size>
281 uint32_t GetArrayStride(
const spirv_cross::SPIRType& struct_type,
282 const spirv_cross::SPIRType& member_type,
283 uint32_t index)
const {
284 auto element_count = GetArrayElements(member_type).value_or(1);
285 if (element_count <= 1) {
288 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
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, UnderlyingType p_underlying_type=UnderlyingType::kOther)
Constructs a new StructMember.
static UnderlyingType DetermineUnderlyingType(spirv_cross::SPIRType::BaseType type)
spirv_cross::SPIRType::BaseType base_type