5#ifndef FLUTTER_IMPELLER_COMPILER_REFLECTOR_H_
6#define FLUTTER_IMPELLER_COMPILER_REFLECTOR_H_
12#include "flutter/fml/mapping.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:
111 case spirv_cross::SPIRType::SampledImage:
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:
125 spirv_cross::SPIRType::BaseType p_base_type,
129 size_t p_byte_length,
130 std::optional<size_t> p_array_elements,
131 size_t p_element_padding,
154 const std::shared_ptr<const spirv_cross::ParsedIR>& ir,
155 const std::shared_ptr<fml::Mapping>& shader_data,
173 struct StructDefinition {
175 size_t byte_length = 0u;
176 std::vector<StructMember> members;
179 struct BindPrototypeArgument {
180 std::string type_name;
181 std::string argument_name;
184 struct BindPrototype {
186 std::string return_type;
187 std::string docstring;
188 std::string descriptor_type =
"";
189 std::vector<BindPrototypeArgument>
args;
192 const Options options_;
193 const std::shared_ptr<const spirv_cross::ParsedIR> ir_;
194 const std::shared_ptr<fml::Mapping> shader_data_;
195 const CompilerBackend compiler_;
196 std::unique_ptr<const nlohmann::json> template_arguments_;
197 std::shared_ptr<fml::Mapping> reflection_header_;
198 std::shared_ptr<fml::Mapping> reflection_cc_;
199 std::shared_ptr<RuntimeStageData::Shader> runtime_stage_shader_;
200 std::shared_ptr<ShaderBundleData> shader_bundle_data_;
201 bool is_valid_ =
false;
203 std::optional<nlohmann::json> GenerateTemplateArguments()
const;
205 std::shared_ptr<fml::Mapping> GenerateReflectionHeader()
const;
207 std::shared_ptr<fml::Mapping> GenerateReflectionCC()
const;
209 std::shared_ptr<RuntimeStageData::Shader> GenerateRuntimeStageData()
const;
211 std::shared_ptr<ShaderBundleData> GenerateShaderBundleData()
const;
213 std::shared_ptr<fml::Mapping> InflateTemplate(std::string_view tmpl)
const;
215 std::optional<nlohmann::json::object_t> ReflectResource(
216 const spirv_cross::Resource&
resource,
217 std::optional<size_t>
offset)
const;
219 std::optional<nlohmann::json::array_t> ReflectResources(
220 const spirv_cross::SmallVector<spirv_cross::Resource>& resources,
221 bool compute_offsets =
false)
const;
223 std::vector<size_t> ComputeOffsets(
224 const spirv_cross::SmallVector<spirv_cross::Resource>& resources)
const;
226 std::optional<size_t> GetOffset(spirv_cross::ID
id,
227 const std::vector<size_t>&
offsets)
const;
229 std::optional<nlohmann::json::object_t> ReflectType(
230 const spirv_cross::TypeID& type_id)
const;
232 nlohmann::json::object_t EmitStructDefinition(
233 std::optional<Reflector::StructDefinition> struc)
const;
235 std::optional<StructDefinition> ReflectStructDefinition(
236 const spirv_cross::TypeID& type_id)
const;
238 std::vector<BindPrototype> ReflectBindPrototypes(
239 const spirv_cross::ShaderResources& resources,
240 spv::ExecutionModel execution_model)
const;
242 nlohmann::json::array_t EmitBindPrototypes(
243 const spirv_cross::ShaderResources& resources,
244 spv::ExecutionModel execution_model)
const;
246 std::optional<StructDefinition> ReflectPerVertexStructDefinition(
247 const spirv_cross::SmallVector<spirv_cross::Resource>& stage_inputs)
250 std::optional<std::string> GetMemberNameAtIndexIfExists(
251 const spirv_cross::SPIRType& parent_type,
254 std::string GetMemberNameAtIndex(
const spirv_cross::SPIRType& parent_type,
256 std::string
suffix =
"")
const;
258 std::vector<StructMember> ReadStructMembers(
259 const spirv_cross::TypeID& type_id)
const;
261 std::optional<uint32_t> GetArrayElements(
262 const spirv_cross::SPIRType&
type)
const;
264 template <u
int32_t Size>
265 uint32_t GetArrayStride(
const spirv_cross::SPIRType& struct_type,
266 const spirv_cross::SPIRType& member_type,
267 uint32_t index)
const {
268 auto element_count = GetArrayElements(member_type).value_or(1);
269 if (element_count <= 1) {
272 return compiler_->type_struct_member_array_stride(struct_type, index);
static SkString resource(SkPDFResourceType type, int 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
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
#define FML_UNREACHABLE()
DEF_SWITCHES_START aot vmservice shared library name
std::function< ProfileSample(void)> Sampler
Sampler is run during SamplingProfiler::SampleRepeatedly. Each platform should implement its version ...
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)
static UnderlyingType DetermineUnderlyingType(spirv_cross::SPIRType::BaseType type)
spirv_cross::SPIRType::BaseType base_type