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 #pragma once
6 
7 #include <memory>
8 #include <optional>
9 
10 #include "flutter/fml/macros.h"
11 #include "flutter/fml/mapping.h"
14 #include "inja/inja.hpp"
15 #include "spirv_msl.hpp"
16 #include "spirv_parser.hpp"
17 
18 namespace impeller {
19 namespace compiler {
20 
21 struct StructMember {
22  std::string type;
23  std::string base_type;
24  std::string name;
25  size_t offset = 0u;
26  size_t size = 0u;
27  size_t byte_length = 0u;
28  std::optional<size_t> array_elements = std::nullopt;
29  size_t element_padding = 0u;
30 
31  StructMember(std::string p_type,
32  std::string p_base_type,
33  std::string p_name,
34  size_t p_offset,
35  size_t p_size,
36  size_t p_byte_length,
37  std::optional<size_t> p_array_elements,
38  size_t p_element_padding)
39  : type(std::move(p_type)),
40  base_type(std::move(p_base_type)),
41  name(std::move(p_name)),
42  offset(p_offset),
43  size(p_size),
44  byte_length(p_byte_length),
45  array_elements(p_array_elements),
46  element_padding(p_element_padding) {}
47 };
48 
49 class Reflector {
50  public:
51  struct Options {
53  std::string entry_point_name;
54  std::string shader_name;
55  std::string header_file_name;
56  };
57 
58  Reflector(Options options,
59  std::shared_ptr<const spirv_cross::ParsedIR> ir,
60  std::shared_ptr<fml::Mapping> shader_data,
61  CompilerBackend compiler);
62 
63  ~Reflector();
64 
65  bool IsValid() const;
66 
67  std::shared_ptr<fml::Mapping> GetReflectionJSON() const;
68 
69  std::shared_ptr<fml::Mapping> GetReflectionHeader() const;
70 
71  std::shared_ptr<fml::Mapping> GetReflectionCC() const;
72 
73  std::shared_ptr<RuntimeStageData> GetRuntimeStageData() const;
74 
75  private:
76  struct StructDefinition {
77  std::string name;
78  size_t byte_length = 0u;
79  std::vector<StructMember> members;
80  };
81 
82  struct BindPrototypeArgument {
83  std::string type_name;
84  std::string argument_name;
85  };
86 
87  struct BindPrototype {
88  std::string name;
89  std::string return_type;
90  std::string docstring;
91  std::vector<BindPrototypeArgument> args;
92  };
93 
94  const Options options_;
95  const std::shared_ptr<const spirv_cross::ParsedIR> ir_;
96  const std::shared_ptr<fml::Mapping> shader_data_;
97  const std::shared_ptr<fml::Mapping> sksl_data_;
98  const CompilerBackend compiler_;
99  std::unique_ptr<const nlohmann::json> template_arguments_;
100  std::shared_ptr<fml::Mapping> reflection_header_;
101  std::shared_ptr<fml::Mapping> reflection_cc_;
102  std::shared_ptr<RuntimeStageData> runtime_stage_data_;
103  bool is_valid_ = false;
104 
105  std::optional<nlohmann::json> GenerateTemplateArguments() const;
106 
107  std::shared_ptr<fml::Mapping> GenerateReflectionHeader() const;
108 
109  std::shared_ptr<fml::Mapping> GenerateReflectionCC() const;
110 
111  std::shared_ptr<RuntimeStageData> GenerateRuntimeStageData() const;
112 
113  std::shared_ptr<fml::Mapping> InflateTemplate(std::string_view tmpl) const;
114 
115  std::optional<nlohmann::json::object_t> ReflectResource(
116  const spirv_cross::Resource& resource,
117  std::optional<size_t> offset) const;
118 
119  std::optional<nlohmann::json::array_t> ReflectResources(
120  const spirv_cross::SmallVector<spirv_cross::Resource>& resources,
121  bool compute_offsets = false) const;
122 
123  std::vector<size_t> ComputeOffsets(
124  const spirv_cross::SmallVector<spirv_cross::Resource>& resources) const;
125 
126  std::optional<nlohmann::json::object_t> ReflectType(
127  const spirv_cross::TypeID& type_id) const;
128 
129  nlohmann::json::object_t EmitStructDefinition(
130  std::optional<Reflector::StructDefinition> struc) const;
131 
132  std::optional<StructDefinition> ReflectStructDefinition(
133  const spirv_cross::TypeID& type_id) const;
134 
135  std::vector<BindPrototype> ReflectBindPrototypes(
136  const spirv_cross::ShaderResources& resources,
137  spv::ExecutionModel execution_model) const;
138 
139  nlohmann::json::array_t EmitBindPrototypes(
140  const spirv_cross::ShaderResources& resources,
141  spv::ExecutionModel execution_model) const;
142 
143  std::optional<StructDefinition> ReflectPerVertexStructDefinition(
144  const spirv_cross::SmallVector<spirv_cross::Resource>& stage_inputs)
145  const;
146 
147  std::optional<std::string> GetMemberNameAtIndexIfExists(
148  const spirv_cross::SPIRType& parent_type,
149  size_t index) const;
150 
151  std::string GetMemberNameAtIndex(const spirv_cross::SPIRType& parent_type,
152  size_t index,
153  std::string suffix = "") const;
154 
155  std::vector<StructMember> ReadStructMembers(
156  const spirv_cross::TypeID& type_id) const;
157 
158  std::optional<uint32_t> GetArrayElements(
159  const spirv_cross::SPIRType& type) const;
160 
161  template <uint32_t Size>
162  uint32_t GetArrayStride(const spirv_cross::SPIRType& struct_type,
163  const spirv_cross::SPIRType& member_type,
164  uint32_t index) const {
165  auto element_count = GetArrayElements(member_type).value_or(1);
166  if (element_count <= 1) {
167  return Size;
168  }
169  return compiler_->type_struct_member_array_stride(struct_type, index);
170  };
171 
172  FML_DISALLOW_COPY_AND_ASSIGN(Reflector);
173 };
174 
175 } // namespace compiler
176 } // namespace impeller
runtime_stage_data.h
impeller::compiler::StructMember::offset
size_t offset
Definition: reflector.h:25
impeller::compiler::StructMember::type
std::string type
Definition: reflector.h:22
impeller::compiler::StructMember::array_elements
std::optional< size_t > array_elements
Definition: reflector.h:28
impeller::compiler::CompilerBackend
Definition: compiler_backend.h:19
impeller::compiler::StructMember
Definition: reflector.h:21
impeller::compiler::Reflector::Reflector
Reflector(Options options, std::shared_ptr< const spirv_cross::ParsedIR > ir, std::shared_ptr< fml::Mapping > shader_data, CompilerBackend compiler)
Definition: reflector.cc:114
impeller::compiler::StructMember::byte_length
size_t byte_length
Definition: reflector.h:27
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:31
impeller::Size
TSize< Scalar > Size
Definition: size.h:135
impeller::compiler::Reflector::GetReflectionCC
std::shared_ptr< fml::Mapping > GetReflectionCC() const
Definition: reflector.cc:175
impeller::compiler::Reflector::Options::header_file_name
std::string header_file_name
Definition: reflector.h:55
impeller::compiler::TargetPlatform
TargetPlatform
Definition: types.h:28
impeller::compiler::Reflector::GetReflectionHeader
std::shared_ptr< fml::Mapping > GetReflectionHeader() const
Definition: reflector.cc:171
impeller::compiler::Reflector::Options::target_platform
TargetPlatform target_platform
Definition: reflector.h:52
impeller::compiler::Reflector::Options::shader_name
std::string shader_name
Definition: reflector.h:54
impeller::compiler::Reflector
Definition: reflector.h:49
impeller::compiler::Reflector::~Reflector
~Reflector()
impeller::compiler::Reflector::Options
Definition: reflector.h:51
compiler_backend.h
impeller::compiler::Reflector::GetRuntimeStageData
std::shared_ptr< RuntimeStageData > GetRuntimeStageData() const
Definition: reflector.cc:179
impeller::compiler::StructMember::element_padding
size_t element_padding
Definition: reflector.h:29
std
Definition: comparable.h:98
impeller::compiler::StructMember::base_type
std::string base_type
Definition: reflector.h:23
impeller::compiler::TargetPlatform::kUnknown
@ kUnknown
impeller::compiler::StructMember::size
size_t size
Definition: reflector.h:26
impeller::compiler::Reflector::GetReflectionJSON
std::shared_ptr< fml::Mapping > GetReflectionJSON() const
Definition: reflector.cc:158
impeller::compiler::StructMember::name
std::string name
Definition: reflector.h:24
impeller::compiler::Reflector::Options::entry_point_name
std::string entry_point_name
Definition: reflector.h:53
impeller::compiler::Reflector::IsValid
bool IsValid() const
Definition: reflector.cc:154
impeller
Definition: aiks_context.cc:10