5 #ifndef FLUTTER_IMPELLER_COMPILER_REFLECTOR_H_
6 #define FLUTTER_IMPELLER_COMPILER_REFLECTOR_H_
12 #include "flutter/fml/macros.h"
13 #include "flutter/fml/mapping.h"
16 #include "inja/inja.hpp"
17 #include "spirv_msl.hpp"
18 #include "spirv_parser.hpp"
34 std::string p_base_type,
39 std::optional<size_t> p_array_elements,
40 size_t p_element_padding)
61 const std::shared_ptr<const spirv_cross::ParsedIR>& ir,
62 const std::shared_ptr<fml::Mapping>& shader_data,
78 struct StructDefinition {
80 size_t byte_length = 0u;
81 std::vector<StructMember> members;
84 struct BindPrototypeArgument {
85 std::string type_name;
86 std::string argument_name;
89 struct BindPrototype {
91 std::string return_type;
92 std::string docstring;
93 std::vector<BindPrototypeArgument> args;
96 const Options options_;
97 const std::shared_ptr<const spirv_cross::ParsedIR> ir_;
98 const std::shared_ptr<fml::Mapping> shader_data_;
99 const CompilerBackend compiler_;
100 std::unique_ptr<const nlohmann::json> template_arguments_;
101 std::shared_ptr<fml::Mapping> reflection_header_;
102 std::shared_ptr<fml::Mapping> reflection_cc_;
103 std::shared_ptr<RuntimeStageData::Shader> runtime_stage_shader_;
104 bool is_valid_ =
false;
106 std::optional<nlohmann::json> GenerateTemplateArguments()
const;
108 std::shared_ptr<fml::Mapping> GenerateReflectionHeader()
const;
110 std::shared_ptr<fml::Mapping> GenerateReflectionCC()
const;
112 std::shared_ptr<RuntimeStageData::Shader> GenerateRuntimeStageData()
const;
114 std::shared_ptr<fml::Mapping> InflateTemplate(std::string_view tmpl)
const;
116 std::optional<nlohmann::json::object_t> ReflectResource(
117 const spirv_cross::Resource& resource,
118 std::optional<size_t> offset)
const;
120 std::optional<nlohmann::json::array_t> ReflectResources(
121 const spirv_cross::SmallVector<spirv_cross::Resource>& resources,
122 bool compute_offsets =
false)
const;
124 std::vector<size_t> ComputeOffsets(
125 const spirv_cross::SmallVector<spirv_cross::Resource>& resources)
const;
127 std::optional<nlohmann::json::object_t> ReflectType(
128 const spirv_cross::TypeID& type_id)
const;
130 nlohmann::json::object_t EmitStructDefinition(
131 std::optional<Reflector::StructDefinition> struc)
const;
133 std::optional<StructDefinition> ReflectStructDefinition(
134 const spirv_cross::TypeID& type_id)
const;
136 std::vector<BindPrototype> ReflectBindPrototypes(
137 const spirv_cross::ShaderResources& resources,
138 spv::ExecutionModel execution_model)
const;
140 nlohmann::json::array_t EmitBindPrototypes(
141 const spirv_cross::ShaderResources& resources,
142 spv::ExecutionModel execution_model)
const;
144 std::optional<StructDefinition> ReflectPerVertexStructDefinition(
145 const spirv_cross::SmallVector<spirv_cross::Resource>& stage_inputs)
148 std::optional<std::string> GetMemberNameAtIndexIfExists(
149 const spirv_cross::SPIRType& parent_type,
152 std::string GetMemberNameAtIndex(
const spirv_cross::SPIRType& parent_type,
154 std::string suffix =
"")
const;
156 std::vector<StructMember> ReadStructMembers(
157 const spirv_cross::TypeID& type_id)
const;
159 std::optional<uint32_t> GetArrayElements(
160 const spirv_cross::SPIRType& type)
const;
162 template <u
int32_t Size>
163 uint32_t GetArrayStride(
const spirv_cross::SPIRType& struct_type,
164 const spirv_cross::SPIRType& member_type,
165 uint32_t index)
const {
166 auto element_count = GetArrayElements(member_type).value_or(1);
167 if (element_count <= 1) {
170 return compiler_->type_struct_member_array_stride(struct_type, index);
181 #endif // FLUTTER_IMPELLER_COMPILER_REFLECTOR_H_