Flutter Impeller
runtime_stage.cc
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 
6 
7 #include <array>
8 #include <memory>
9 
10 #include "fml/mapping.h"
14 #include "impeller/runtime_stage/runtime_stage_flatbuffers.h"
15 #include "runtime_stage_types_flatbuffers.h"
16 
17 namespace impeller {
18 
19 static RuntimeUniformType ToType(fb::UniformDataType type) {
20  switch (type) {
27  }
28  FML_UNREACHABLE();
29 }
30 
31 static RuntimeShaderStage ToShaderStage(fb::Stage stage) {
32  switch (stage) {
33  case fb::Stage::kVertex:
35  case fb::Stage::kFragment:
37  case fb::Stage::kCompute:
39  }
40  FML_UNREACHABLE();
41 }
42 
43 /// The generated name from GLSLang/shaderc for the UBO containing non-opaque
44 /// uniforms specified in the user-written runtime effect shader.
45 ///
46 /// Vulkan does not allow non-opaque uniforms outside of a UBO.
47 const char* RuntimeStage::kVulkanUBOName =
48  "_RESERVED_IDENTIFIER_FIXUP_gl_DefaultUniformBlock";
49 
50 std::unique_ptr<RuntimeStage> RuntimeStage::RuntimeStageIfPresent(
51  const fb::RuntimeStage* runtime_stage,
52  const std::shared_ptr<fml::Mapping>& payload) {
53  if (!runtime_stage) {
54  return nullptr;
55  }
56 
57  return std::unique_ptr<RuntimeStage>(
58  new RuntimeStage(runtime_stage, payload));
59 }
60 
62  const std::shared_ptr<fml::Mapping>& payload) {
63  if (payload == nullptr || !payload->GetMapping()) {
64  return {};
65  }
66  if (!fb::RuntimeStagesBufferHasIdentifier(payload->GetMapping())) {
67  return {};
68  }
69 
70  auto raw_stages = fb::GetRuntimeStages(payload->GetMapping());
71  return {
73  RuntimeStageIfPresent(raw_stages->sksl(), payload)},
75  RuntimeStageIfPresent(raw_stages->metal(), payload)},
77  RuntimeStageIfPresent(raw_stages->opengles(), payload)},
79  RuntimeStageIfPresent(raw_stages->opengles3(), payload)},
81  RuntimeStageIfPresent(raw_stages->vulkan(), payload)},
82  };
83 }
84 
85 RuntimeStage::RuntimeStage(const fb::RuntimeStage* runtime_stage,
86  const std::shared_ptr<fml::Mapping>& payload)
87  : payload_(payload) {
88  FML_DCHECK(runtime_stage);
89 
90  stage_ = ToShaderStage(runtime_stage->stage());
91  entrypoint_ = runtime_stage->entrypoint()->str();
92 
93  auto* uniforms = runtime_stage->uniforms();
94 
95  // Note: image bindings are screwy and will always have the same offset.
96  // track the binding of the UBO to determine where the image bindings go.
97  // This is only guaranteed to give us the correct bindings if there is a
98  // single sampler2D.
99  std::optional<size_t> ubo_id;
100  if (uniforms) {
101  for (auto i = uniforms->begin(), end = uniforms->end(); i != end; i++) {
103  desc.name = i->name()->str();
104  desc.location = i->location();
105  desc.binding = i->binding();
106  desc.type = ToType(i->type());
107  if (desc.type == kStruct) {
108  ubo_id = desc.location;
109  desc.binding = desc.location;
110  }
112  static_cast<size_t>(i->rows()), static_cast<size_t>(i->columns())};
113  desc.bit_width = i->bit_width();
114  desc.array_elements = i->array_elements();
115  if (i->struct_layout()) {
116  for (const auto& byte_type : *i->struct_layout()) {
117  desc.struct_layout.push_back(static_cast<uint8_t>(byte_type));
118  }
119  }
120  desc.struct_float_count = i->struct_float_count();
121  uniforms_.push_back(std::move(desc));
122  }
123  }
124 
125  code_mapping_ = std::make_shared<fml::NonOwnedMapping>(
126  runtime_stage->shader()->data(), //
127  runtime_stage->shader()->size(), //
128  [payload = payload_](auto, auto) {} //
129  );
130 
131  size_t binding = 64;
132  if (ubo_id.has_value() && ubo_id.value() == binding) {
133  binding++;
134  }
135  for (auto& uniform : uniforms_) {
136  if (uniform.type == kSampledImage) {
137  uniform.binding = binding;
138  binding++;
139  if (ubo_id.has_value() && ubo_id.value() == binding) {
140  binding++;
141  }
142  }
143  }
144 
145  for (const auto& uniform : GetUniforms()) {
146  if (uniform.type == kStruct) {
147  descriptor_set_layouts_.push_back(DescriptorSetLayout{
148  static_cast<uint32_t>(uniform.location),
151  });
152  } else if (uniform.type == kSampledImage) {
153  descriptor_set_layouts_.push_back(DescriptorSetLayout{
154  static_cast<uint32_t>(uniform.binding),
157  });
158  }
159  }
160  is_valid_ = true;
161 }
162 
163 RuntimeStage::~RuntimeStage() = default;
166 
167 bool RuntimeStage::IsValid() const {
168  return is_valid_;
169 }
170 
171 const std::shared_ptr<fml::Mapping>& RuntimeStage::GetCodeMapping() const {
172  return code_mapping_;
173 }
174 
175 const std::vector<RuntimeUniformDescription>& RuntimeStage::GetUniforms()
176  const {
177  return uniforms_;
178 }
179 
181  const std::string& name) const {
182  for (const auto& uniform : uniforms_) {
183  if (uniform.name == name) {
184  return &uniform;
185  }
186  }
187  return nullptr;
188 }
189 
190 const std::string& RuntimeStage::GetEntrypoint() const {
191  return entrypoint_;
192 }
193 
195  return stage_;
196 }
197 
198 bool RuntimeStage::IsDirty() const {
199  return is_dirty_;
200 }
201 
203  is_dirty_ = false;
204 }
205 
206 const std::vector<DescriptorSetLayout>& RuntimeStage::GetDescriptorSetLayouts()
207  const {
208  return descriptor_set_layouts_;
209 }
210 
211 } // namespace impeller
GLenum type
const std::string & GetEntrypoint() const
RuntimeStage & operator=(RuntimeStage &&)
const std::vector< RuntimeUniformDescription > & GetUniforms() const
const RuntimeUniformDescription * GetUniform(const std::string &name) const
std::map< RuntimeStageBackend, std::shared_ptr< RuntimeStage > > Map
Definition: runtime_stage.h:24
static const char * kVulkanUBOName
Definition: runtime_stage.h:22
static Map DecodeRuntimeStages(const std::shared_ptr< fml::Mapping > &payload)
const std::shared_ptr< fml::Mapping > & GetCodeMapping() const
const std::vector< DescriptorSetLayout > & GetDescriptorSetLayouts() const
RuntimeShaderStage GetShaderStage() const
RuntimeStage(const fb::RuntimeStage *runtime_stage, const std::shared_ptr< fml::Mapping > &payload)
constexpr ShaderStage ToShaderStage(RuntimeShaderStage stage)
Definition: shader_types.h:29
static RuntimeUniformType ToType(fb::UniformDataType type)
RuntimeUniformDimensions dimensions
Definition: runtime_types.h:47
std::vector< uint8_t > struct_layout
Definition: runtime_types.h:50
std::optional< size_t > array_elements
Definition: runtime_types.h:49
size_t binding
Location, but for Vulkan.
Definition: runtime_types.h:45