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"
46 using Type = spirv_cross::SPIRType::BaseType;
49 return "ShaderType::kVoid";
51 return "ShaderType::kBoolean";
53 return "ShaderType::kSignedByte";
55 return "ShaderType::kUnsignedByte";
57 return "ShaderType::kSignedShort";
59 return "ShaderType::kUnsignedShort";
61 return "ShaderType::kSignedInt";
63 return "ShaderType::kUnsignedInt";
65 return "ShaderType::kSignedInt64";
67 return "ShaderType::kUnsignedInt64";
68 case Type::AtomicCounter:
69 return "ShaderType::kAtomicCounter";
71 return "ShaderType::kHalfFloat";
73 return "ShaderType::kFloat";
75 return "ShaderType::kDouble";
77 return "ShaderType::kStruct";
79 return "ShaderType::kImage";
80 case Type::SampledImage:
81 return "ShaderType::kSampledImage";
83 return "ShaderType::kSampler";
85 return "ShaderType::kUnknown";
91 spirv_cross::SPIRType::BaseType
type) {
93 case spirv_cross::SPIRType::Void:
95 case spirv_cross::SPIRType::Float:
97 case spirv_cross::SPIRType::Unknown:
98 case spirv_cross::SPIRType::Boolean:
99 case spirv_cross::SPIRType::SByte:
100 case spirv_cross::SPIRType::UByte:
101 case spirv_cross::SPIRType::Short:
102 case spirv_cross::SPIRType::UShort:
103 case spirv_cross::SPIRType::Int:
104 case spirv_cross::SPIRType::UInt:
105 case spirv_cross::SPIRType::Int64:
106 case spirv_cross::SPIRType::UInt64:
107 case spirv_cross::SPIRType::AtomicCounter:
108 case spirv_cross::SPIRType::Half:
109 case spirv_cross::SPIRType::Double:
110 case spirv_cross::SPIRType::Struct:
111 case spirv_cross::SPIRType::Image:
112 case spirv_cross::SPIRType::SampledImage:
113 case spirv_cross::SPIRType::Sampler:
114 case spirv_cross::SPIRType::AccelerationStructure:
115 case spirv_cross::SPIRType::RayQuery:
116 case spirv_cross::SPIRType::ControlPointArray:
117 case spirv_cross::SPIRType::Interpolant:
118 case spirv_cross::SPIRType::Char:
143 spirv_cross::SPIRType::BaseType p_base_type,
147 size_t p_byte_length,
148 std::optional<size_t> p_array_elements,
149 size_t p_element_padding,
150 std::optional<std::string> p_float_type = std::nullopt,
174 const std::shared_ptr<const spirv_cross::ParsedIR>& ir,
175 const std::shared_ptr<fml::Mapping>& shader_data,
193 struct StructDefinition {
195 size_t byte_length = 0u;
196 std::vector<StructMember> members;
199 struct BindPrototypeArgument {
201 std::string argument_name;
204 struct BindPrototype {
206 std::string return_type;
207 std::string docstring;
208 std::string descriptor_type =
"";
209 std::vector<BindPrototypeArgument> args;
212 const Options options_;
213 const std::shared_ptr<const spirv_cross::ParsedIR> ir_;
214 const std::shared_ptr<fml::Mapping> shader_data_;
215 const CompilerBackend compiler_;
216 std::unique_ptr<const nlohmann::json> template_arguments_;
217 std::shared_ptr<fml::Mapping> reflection_header_;
218 std::shared_ptr<fml::Mapping> reflection_cc_;
219 std::shared_ptr<RuntimeStageData::Shader> runtime_stage_shader_;
220 std::shared_ptr<ShaderBundleData> shader_bundle_data_;
221 bool is_valid_ =
false;
223 std::optional<nlohmann::json> GenerateTemplateArguments()
const;
225 std::shared_ptr<fml::Mapping> GenerateReflectionHeader()
const;
227 std::shared_ptr<fml::Mapping> GenerateReflectionCC()
const;
229 std::shared_ptr<RuntimeStageData::Shader> GenerateRuntimeStageData()
const;
231 std::shared_ptr<ShaderBundleData> GenerateShaderBundleData()
const;
233 std::shared_ptr<fml::Mapping> InflateTemplate(std::string_view tmpl)
const;
235 std::optional<nlohmann::json::object_t> ReflectResource(
236 const spirv_cross::Resource& resource,
237 std::optional<size_t> offset)
const;
239 std::optional<nlohmann::json::array_t> ReflectResources(
240 const spirv_cross::SmallVector<spirv_cross::Resource>& resources,
241 bool compute_offsets =
false)
const;
243 std::vector<size_t> ComputeOffsets(
244 const spirv_cross::SmallVector<spirv_cross::Resource>& resources)
const;
246 std::optional<size_t> GetOffset(spirv_cross::ID
id,
247 const std::vector<size_t>& offsets)
const;
249 std::optional<nlohmann::json::object_t> ReflectType(
250 const spirv_cross::TypeID& type_id)
const;
252 nlohmann::json::object_t EmitStructDefinition(
253 std::optional<Reflector::StructDefinition> struc)
const;
255 std::optional<StructDefinition> ReflectStructDefinition(
256 const spirv_cross::TypeID& type_id)
const;
258 std::vector<BindPrototype> ReflectBindPrototypes(
259 const spirv_cross::ShaderResources& resources,
260 spv::ExecutionModel execution_model)
const;
262 nlohmann::json::array_t EmitBindPrototypes(
263 const spirv_cross::ShaderResources& resources,
264 spv::ExecutionModel execution_model)
const;
266 std::optional<StructDefinition> ReflectPerVertexStructDefinition(
267 const spirv_cross::SmallVector<spirv_cross::Resource>& stage_inputs)
270 std::optional<std::string> GetMemberNameAtIndexIfExists(
271 const spirv_cross::SPIRType& parent_type,
274 std::string GetMemberNameAtIndex(
const spirv_cross::SPIRType& parent_type,
276 std::string suffix =
"")
const;
278 std::vector<StructMember> ReadStructMembers(
279 const spirv_cross::TypeID& type_id)
const;
281 std::optional<uint32_t> GetArrayElements(
282 const spirv_cross::SPIRType& type)
const;
284 template <u
int32_t Size>
285 uint32_t GetArrayStride(
const spirv_cross::SPIRType& struct_type,
286 const spirv_cross::SPIRType& member_type,
287 uint32_t index)
const {
288 auto element_count = GetArrayElements(member_type).value_or(1);
289 if (element_count <= 1) {
292 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, std::optional< std::string > p_float_type=std::nullopt, UnderlyingType p_underlying_type=UnderlyingType::kOther)
Constructs a new StructMember.
std::optional< std::string > float_type
static UnderlyingType DetermineUnderlyingType(spirv_cross::SPIRType::BaseType type)
spirv_cross::SPIRType::BaseType base_type