| Flutter Impeller |

reflector.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef FLUTTER_IMPELLER_COMPILER_REFLECTOR_H_
6 #define FLUTTER_IMPELLER_COMPILER_REFLECTOR_H_
7
8 #include <cstdint>
9 #include <memory>
10 #include <optional>
11
12 #include "flutter/fml/macros.h"
13 #include "flutter/fml/mapping.h"
14 #include "impeller/compiler/compiler_backend.h"
15 #include "impeller/compiler/runtime_stage_data.h"
16 #include "inja/inja.hpp"
17 #include "spirv_msl.hpp"
18 #include "spirv_parser.hpp"
19
20 namespace impeller {
21 namespace compiler {
22
23 struct StructMember {
24 std::string type;
25 std::string base_type;
26 std::string name;
27 size_t offset = 0u;
28 size_t size = 0u;
29 size_t byte_length = 0u;
30 std::optional<size_t> array_elements = std::nullopt;
31 size_t element_padding = 0u;
32
33 StructMember(std::string p_type,
34 std::string p_base_type,
35 std::string p_name,
36 size_t p_offset,
37 size_t p_size,
38 size_t p_byte_length,
39 std::optional<size_t> p_array_elements,
40 size_t p_element_padding)
41 : type(std::move(p_type)),
42 base_type(std::move(p_base_type)),
43 name(std::move(p_name)),
44 offset(p_offset),
45 size(p_size),
46 byte_length(p_byte_length),
47 array_elements(p_array_elements),
48 element_padding(p_element_padding) {}
49 };
50
51 class Reflector {
52 public:
53 struct Options {
54 TargetPlatform target_platform = TargetPlatform::kUnknown;
55 std::string entry_point_name;
56 std::string shader_name;
57 std::string header_file_name;
58 };
59
60 Reflector(Options options,
61 const std::shared_ptr<const spirv_cross::ParsedIR>& ir,
62 const std::shared_ptr<fml::Mapping>& shader_data,
63 const CompilerBackend& compiler);
64
65 ~Reflector();
66
67 bool IsValid() const;
68
69 std::shared_ptr<fml::Mapping> GetReflectionJSON() const;
70
71 std::shared_ptr<fml::Mapping> GetReflectionHeader() const;
72
73 std::shared_ptr<fml::Mapping> GetReflectionCC() const;
74
75 std::shared_ptr<RuntimeStageData::Shader> GetRuntimeStageShaderData() const;
76
77 private:
78 struct StructDefinition {
79 std::string name;
80 size_t byte_length = 0u;
81 std::vector<StructMember> members;
82 };
83
84 struct BindPrototypeArgument {
85 std::string type_name;
86 std::string argument_name;
87 };
88
89 struct BindPrototype {
90 std::string name;
91 std::string return_type;
92 std::string docstring;
93 std::vector<BindPrototypeArgument> args;
94 };
95
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;
105
106 std::optional<nlohmann::json> GenerateTemplateArguments() const;
107
108 std::shared_ptr<fml::Mapping> GenerateReflectionHeader() const;
109
110 std::shared_ptr<fml::Mapping> GenerateReflectionCC() const;
111
112 std::shared_ptr<RuntimeStageData::Shader> GenerateRuntimeStageData() const;
113
114 std::shared_ptr<fml::Mapping> InflateTemplate(std::string_view tmpl) const;
115
116 std::optional<nlohmann::json::object_t> ReflectResource(
117 const spirv_cross::Resource& resource,
118 std::optional<size_t> offset) const;
119
120 std::optional<nlohmann::json::array_t> ReflectResources(
121 const spirv_cross::SmallVector<spirv_cross::Resource>& resources,
122 bool compute_offsets = false) const;
123
124 std::vector<size_t> ComputeOffsets(
125 const spirv_cross::SmallVector<spirv_cross::Resource>& resources) const;
126
127 std::optional<nlohmann::json::object_t> ReflectType(
128 const spirv_cross::TypeID& type_id) const;
129
130 nlohmann::json::object_t EmitStructDefinition(
131 std::optional<Reflector::StructDefinition> struc) const;
132
133 std::optional<StructDefinition> ReflectStructDefinition(
134 const spirv_cross::TypeID& type_id) const;
135
136 std::vector<BindPrototype> ReflectBindPrototypes(
137 const spirv_cross::ShaderResources& resources,
138 spv::ExecutionModel execution_model) const;
139
140 nlohmann::json::array_t EmitBindPrototypes(
141 const spirv_cross::ShaderResources& resources,
142 spv::ExecutionModel execution_model) const;
143
144 std::optional<StructDefinition> ReflectPerVertexStructDefinition(
145 const spirv_cross::SmallVector<spirv_cross::Resource>& stage_inputs)
146 const;
147
148 std::optional<std::string> GetMemberNameAtIndexIfExists(
149 const spirv_cross::SPIRType& parent_type,
150 size_t index) const;
151
152 std::string GetMemberNameAtIndex(const spirv_cross::SPIRType& parent_type,
153 size_t index,
154 std::string suffix = "") const;
155
156 std::vector<StructMember> ReadStructMembers(
157 const spirv_cross::TypeID& type_id) const;
158
159 std::optional<uint32_t> GetArrayElements(
160 const spirv_cross::SPIRType& type) const;
161
162 template <uint32_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) {
168 return Size;
169 }
170 return compiler_->type_struct_member_array_stride(struct_type, index);
171 };
172
173 Reflector(const Reflector&) = delete;
174
175 Reflector& operator=(const Reflector&) = delete;
176 };
177
178 } // namespace compiler
179 } // namespace impeller
180
181 #endif // FLUTTER_IMPELLER_COMPILER_REFLECTOR_H_
runtime_stage_data.h
impeller::compiler::StructMember::offset
size_t offset
Definition: reflector.h:27
impeller::compiler::StructMember::type
std::string type
Definition: reflector.h:24
impeller::compiler::StructMember::array_elements
std::optional< size_t > array_elements
Definition: reflector.h:30
impeller::compiler::CompilerBackend
Definition: compiler_backend.h:21
impeller::compiler::StructMember
Definition: reflector.h:23
impeller::compiler::StructMember::byte_length
size_t byte_length
Definition: reflector.h:29
impeller::compiler::StructMember::StructMember
StructMember(std::string p_type, std::string 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)
Definition: reflector.h:33
impeller::Size
TSize< Scalar > Size
Definition: size.h:137
impeller::compiler::Reflector::GetReflectionCC
std::shared_ptr< fml::Mapping > GetReflectionCC() const
Definition: reflector.cc:160
impeller::compiler::Reflector::Options::header_file_name
std::string header_file_name
Definition: reflector.h:57
impeller::compiler::TargetPlatform
TargetPlatform
Definition: types.h:28
impeller::compiler::Reflector::GetReflectionHeader
std::shared_ptr< fml::Mapping > GetReflectionHeader() const
Definition: reflector.cc:156
impeller::compiler::Reflector::Options::target_platform
TargetPlatform target_platform
Definition: reflector.h:54
impeller::compiler::Reflector::GetRuntimeStageShaderData
std::shared_ptr< RuntimeStageData::Shader > GetRuntimeStageShaderData() const
Definition: reflector.cc:164
impeller::compiler::Reflector::Options::shader_name
std::string shader_name
Definition: reflector.h:56
impeller::compiler::Reflector
Definition: reflector.h:51
impeller::compiler::Reflector::~Reflector
~Reflector()
impeller::compiler::Reflector::Options
Definition: reflector.h:53
compiler_backend.h
impeller::compiler::StructMember::element_padding
size_t element_padding
Definition: reflector.h:31
std
Definition: comparable.h:95
impeller::compiler::StructMember::base_type
std::string base_type
Definition: reflector.h:25
impeller::compiler::TargetPlatform::kUnknown
@ kUnknown
impeller::compiler::StructMember::size
size_t size
Definition: reflector.h:28
impeller::compiler::Reflector::GetReflectionJSON
std::shared_ptr< fml::Mapping > GetReflectionJSON() const
Definition: reflector.cc:143
impeller::compiler::StructMember::name
std::string name
Definition: reflector.h:26
impeller::compiler::Reflector::Options::entry_point_name
std::string entry_point_name
Definition: reflector.h:55
impeller::compiler::Reflector::IsValid
bool IsValid() const
Definition: reflector.cc:139
impeller
Definition: aiks_context.cc:10
impeller::compiler::Reflector::Reflector
Reflector(Options options, const std::shared_ptr< const spirv_cross::ParsedIR > &ir, const std::shared_ptr< fml::Mapping > &shader_data, const CompilerBackend &compiler)
Definition: reflector.cc:102