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"
13 #include "impeller/runtime_stage/runtime_stage_flatbuffers.h"
14 #include "runtime_stage_types_flatbuffers.h"
15 
16 namespace impeller {
17 
18 static RuntimeUniformType ToType(fb::UniformDataType type) {
19  switch (type) {
26  }
27  FML_UNREACHABLE();
28 }
29 
30 static RuntimeShaderStage ToShaderStage(fb::Stage stage) {
31  switch (stage) {
32  case fb::Stage::kVertex:
34  case fb::Stage::kFragment:
36  case fb::Stage::kCompute:
38  }
39  FML_UNREACHABLE();
40 }
41 
42 /// The generated name from GLSLang/shaderc for the UBO containing non-opaque
43 /// uniforms specified in the user-written runtime effect shader.
44 ///
45 /// Vulkan does not allow non-opaque uniforms outside of a UBO.
46 const char* RuntimeStage::kVulkanUBOName =
47  "_RESERVED_IDENTIFIER_FIXUP_gl_DefaultUniformBlock";
48 
49 std::unique_ptr<RuntimeStage> RuntimeStage::RuntimeStageIfPresent(
50  const fb::RuntimeStage* runtime_stage,
51  const std::shared_ptr<fml::Mapping>& payload) {
52  if (!runtime_stage) {
53  return nullptr;
54  }
55 
56  return std::unique_ptr<RuntimeStage>(
57  new RuntimeStage(runtime_stage, payload));
58 }
59 
61  const std::shared_ptr<fml::Mapping>& payload) {
62  if (payload == nullptr || !payload->GetMapping()) {
63  return {};
64  }
65  if (!fb::RuntimeStagesBufferHasIdentifier(payload->GetMapping())) {
66  return {};
67  }
68 
69  auto raw_stages = fb::GetRuntimeStages(payload->GetMapping());
70  return {
72  RuntimeStageIfPresent(raw_stages->sksl(), payload)},
74  RuntimeStageIfPresent(raw_stages->metal(), payload)},
76  RuntimeStageIfPresent(raw_stages->opengles(), payload)},
78  RuntimeStageIfPresent(raw_stages->vulkan(), payload)},
79  };
80 }
81 
82 RuntimeStage::RuntimeStage(const fb::RuntimeStage* runtime_stage,
83  const std::shared_ptr<fml::Mapping>& payload)
84  : payload_(payload) {
85  FML_DCHECK(runtime_stage);
86 
87  stage_ = ToShaderStage(runtime_stage->stage());
88  entrypoint_ = runtime_stage->entrypoint()->str();
89 
90  auto* uniforms = runtime_stage->uniforms();
91  if (uniforms) {
92  for (auto i = uniforms->begin(), end = uniforms->end(); i != end; i++) {
94  desc.name = i->name()->str();
95  desc.location = i->location();
96  desc.type = ToType(i->type());
98  static_cast<size_t>(i->rows()), static_cast<size_t>(i->columns())};
99  desc.bit_width = i->bit_width();
100  desc.array_elements = i->array_elements();
101  if (i->struct_layout()) {
102  for (const auto& byte_type : *i->struct_layout()) {
103  desc.struct_layout.push_back(static_cast<uint8_t>(byte_type));
104  }
105  }
106  desc.struct_float_count = i->struct_float_count();
107  uniforms_.emplace_back(std::move(desc));
108  }
109  }
110 
111  code_mapping_ = std::make_shared<fml::NonOwnedMapping>(
112  runtime_stage->shader()->data(), //
113  runtime_stage->shader()->size(), //
114  [payload = payload_](auto, auto) {} //
115  );
116 
117  is_valid_ = true;
118 }
119 
120 RuntimeStage::~RuntimeStage() = default;
123 
124 bool RuntimeStage::IsValid() const {
125  return is_valid_;
126 }
127 
128 const std::shared_ptr<fml::Mapping>& RuntimeStage::GetCodeMapping() const {
129  return code_mapping_;
130 }
131 
132 const std::vector<RuntimeUniformDescription>& RuntimeStage::GetUniforms()
133  const {
134  return uniforms_;
135 }
136 
138  const std::string& name) const {
139  for (const auto& uniform : uniforms_) {
140  if (uniform.name == name) {
141  return &uniform;
142  }
143  }
144  return nullptr;
145 }
146 
147 const std::string& RuntimeStage::GetEntrypoint() const {
148  return entrypoint_;
149 }
150 
152  return stage_;
153 }
154 
155 bool RuntimeStage::IsDirty() const {
156  return is_dirty_;
157 }
158 
160  is_dirty_ = false;
161 }
162 
163 } // namespace impeller
impeller::RuntimeStage::~RuntimeStage
~RuntimeStage()
impeller::RuntimeUniformDescription
Definition: runtime_types.h:39
impeller::kFloat
@ kFloat
Definition: runtime_types.h:23
impeller::RuntimeStage::GetShaderStage
RuntimeShaderStage GetShaderStage() const
Definition: runtime_stage.cc:151
impeller::ToType
static RuntimeUniformType ToType(fb::UniformDataType type)
Definition: runtime_stage.cc:18
impeller::RuntimeShaderStage
RuntimeShaderStage
Definition: runtime_types.h:28
impeller::RuntimeStageBackend::kVulkan
@ kVulkan
impeller::RuntimeStage::RuntimeStage
RuntimeStage(const fb::RuntimeStage *runtime_stage, const std::shared_ptr< fml::Mapping > &payload)
Definition: runtime_stage.cc:82
impeller::RuntimeUniformDescription::dimensions
RuntimeUniformDimensions dimensions
Definition: runtime_types.h:43
impeller::RuntimeStage::DecodeRuntimeStages
static Map DecodeRuntimeStages(const std::shared_ptr< fml::Mapping > &payload)
Definition: runtime_stage.cc:60
impeller::RuntimeStage::kVulkanUBOName
static const char * kVulkanUBOName
Definition: runtime_stage.h:21
impeller::kSampledImage
@ kSampledImage
Definition: runtime_types.h:24
validation.h
impeller::RuntimeStage
Definition: runtime_stage.h:19
impeller::ToShaderStage
constexpr ShaderStage ToShaderStage(RuntimeShaderStage stage)
Definition: shader_types.h:29
runtime_types.h
impeller::RuntimeStageBackend::kOpenGLES
@ kOpenGLES
impeller::RuntimeUniformDimensions
Definition: runtime_types.h:34
impeller::RuntimeStage::GetUniforms
const std::vector< RuntimeUniformDescription > & GetUniforms() const
Definition: runtime_stage.cc:132
impeller::RuntimeShaderStage::kVertex
@ kVertex
impeller::RuntimeStage::operator=
RuntimeStage & operator=(RuntimeStage &&)
impeller::RuntimeStage::GetUniform
const RuntimeUniformDescription * GetUniform(const std::string &name) const
Definition: runtime_stage.cc:137
impeller::RuntimeShaderStage::kFragment
@ kFragment
runtime_stage.h
impeller::RuntimeUniformType
RuntimeUniformType
Definition: runtime_types.h:22
impeller::RuntimeUniformDescription::name
std::string name
Definition: runtime_types.h:40
impeller::RuntimeStage::SetClean
void SetClean()
Definition: runtime_stage.cc:159
impeller::RuntimeUniformDescription::struct_float_count
size_t struct_float_count
Definition: runtime_types.h:47
impeller::RuntimeUniformDescription::struct_layout
std::vector< uint8_t > struct_layout
Definition: runtime_types.h:46
impeller::RuntimeUniformDescription::type
RuntimeUniformType type
Definition: runtime_types.h:42
impeller::kStruct
@ kStruct
Definition: runtime_types.h:25
impeller::RuntimeUniformDescription::location
size_t location
Definition: runtime_types.h:41
impeller::RuntimeUniformDescription::bit_width
size_t bit_width
Definition: runtime_types.h:44
impeller::RuntimeStageBackend::kSkSL
@ kSkSL
impeller::RuntimeStage::IsValid
bool IsValid() const
Definition: runtime_stage.cc:124
impeller::RuntimeShaderStage::kCompute
@ kCompute
impeller::RuntimeStage::IsDirty
bool IsDirty() const
Definition: runtime_stage.cc:155
impeller::RuntimeUniformDescription::array_elements
std::optional< size_t > array_elements
Definition: runtime_types.h:45
impeller::RuntimeStage::GetEntrypoint
const std::string & GetEntrypoint() const
Definition: runtime_stage.cc:147
impeller::RuntimeStageBackend::kMetal
@ kMetal
impeller
Definition: aiks_blur_unittests.cc:20
impeller::RuntimeStage::Map
std::map< RuntimeStageBackend, std::shared_ptr< RuntimeStage > > Map
Definition: runtime_stage.h:23
impeller::RuntimeStage::GetCodeMapping
const std::shared_ptr< fml::Mapping > & GetCodeMapping() const
Definition: runtime_stage.cc:128