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 
10 #include "impeller/runtime_stage/runtime_stage_flatbuffers.h"
11 
12 namespace impeller {
13 
14 static RuntimeUniformType ToType(fb::UniformDataType type) {
15  switch (type) {
42  }
43  FML_UNREACHABLE();
44 }
45 
46 static RuntimeShaderStage ToShaderStage(fb::Stage stage) {
47  switch (stage) {
48  case fb::Stage::kVertex:
50  case fb::Stage::kFragment:
52  case fb::Stage::kCompute:
54  case fb::Stage::kTessellationControl:
56  case fb::Stage::kTessellationEvaluation:
58  }
59  FML_UNREACHABLE();
60 }
61 
62 RuntimeStage::RuntimeStage(std::shared_ptr<fml::Mapping> payload)
63  : payload_(std::move(payload)) {
64  if (payload_ == nullptr || !payload_->GetMapping()) {
65  return;
66  }
67  if (!fb::RuntimeStageBufferHasIdentifier(payload_->GetMapping())) {
68  return;
69  }
70  auto runtime_stage = fb::GetRuntimeStage(payload_->GetMapping());
71  if (!runtime_stage) {
72  return;
73  }
74 
75  stage_ = ToShaderStage(runtime_stage->stage());
76  entrypoint_ = runtime_stage->entrypoint()->str();
77 
78  auto* uniforms = runtime_stage->uniforms();
79  if (uniforms) {
80  for (auto i = uniforms->begin(), end = uniforms->end(); i != end; i++) {
82  desc.name = i->name()->str();
83  desc.location = i->location();
84  desc.type = ToType(i->type());
86  static_cast<size_t>(i->rows()), static_cast<size_t>(i->columns())};
87  desc.bit_width = i->bit_width();
88  desc.array_elements = i->array_elements();
89  uniforms_.emplace_back(std::move(desc));
90  }
91  }
92 
93  code_mapping_ = std::make_shared<fml::NonOwnedMapping>(
94  runtime_stage->shader()->data(), //
95  runtime_stage->shader()->size(), //
96  [payload = payload_](auto, auto) {} //
97  );
98 
99  sksl_mapping_ = std::make_shared<fml::NonOwnedMapping>(
100  runtime_stage->sksl()->data(), //
101  runtime_stage->sksl()->size(), //
102  [payload = payload_](auto, auto) {} //
103  );
104 
105  is_valid_ = true;
106 }
107 
108 RuntimeStage::~RuntimeStage() = default;
111 
112 bool RuntimeStage::IsValid() const {
113  return is_valid_;
114 }
115 
116 const std::shared_ptr<fml::Mapping>& RuntimeStage::GetCodeMapping() const {
117  return code_mapping_;
118 }
119 
120 const std::shared_ptr<fml::Mapping>& RuntimeStage::GetSkSLMapping() const {
121  return sksl_mapping_;
122 }
123 
124 const std::vector<RuntimeUniformDescription>& RuntimeStage::GetUniforms()
125  const {
126  return uniforms_;
127 }
128 
130  const std::string& name) const {
131  for (const auto& uniform : uniforms_) {
132  if (uniform.name == name) {
133  return &uniform;
134  }
135  }
136  return nullptr;
137 }
138 
139 const std::string& RuntimeStage::GetEntrypoint() const {
140  return entrypoint_;
141 }
142 
144  return stage_;
145 }
146 
147 bool RuntimeStage::IsDirty() const {
148  return is_dirty_;
149 }
150 
152  is_dirty_ = false;
153 }
154 
155 } // namespace impeller
impeller::RuntimeStage::~RuntimeStage
~RuntimeStage()
impeller::RuntimeUniformDescription
Definition: runtime_types.h:42
impeller::kBoolean
@ kBoolean
Definition: runtime_types.h:14
impeller::kFloat
@ kFloat
Definition: runtime_types.h:24
impeller::RuntimeStage::GetShaderStage
RuntimeShaderStage GetShaderStage() const
Definition: runtime_stage.cc:143
impeller::ToType
static RuntimeUniformType ToType(fb::UniformDataType type)
Definition: runtime_stage.cc:14
impeller::RuntimeShaderStage
RuntimeShaderStage
Definition: runtime_types.h:29
impeller::kUnsignedShort
@ kUnsignedShort
Definition: runtime_types.h:18
impeller::RuntimeUniformDescription::dimensions
RuntimeUniformDimensions dimensions
Definition: runtime_types.h:46
impeller::kSignedByte
@ kSignedByte
Definition: runtime_types.h:15
impeller::kSampledImage
@ kSampledImage
Definition: runtime_types.h:26
validation.h
impeller::RuntimeStage
Definition: runtime_stage.h:17
impeller::ToShaderStage
constexpr ShaderStage ToShaderStage(RuntimeShaderStage stage)
Definition: shader_types.h:29
impeller::RuntimeUniformDimensions
Definition: runtime_types.h:37
impeller::RuntimeStage::GetUniforms
const std::vector< RuntimeUniformDescription > & GetUniforms() const
Definition: runtime_stage.cc:124
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:129
impeller::kSignedInt
@ kSignedInt
Definition: runtime_types.h:19
impeller::RuntimeStage::RuntimeStage
RuntimeStage(std::shared_ptr< fml::Mapping > payload)
Definition: runtime_stage.cc:62
impeller::RuntimeShaderStage::kFragment
@ kFragment
runtime_stage.h
impeller::RuntimeShaderStage::kTessellationEvaluation
@ kTessellationEvaluation
impeller::RuntimeUniformType
RuntimeUniformType
Definition: runtime_types.h:13
impeller::RuntimeUniformDescription::name
std::string name
Definition: runtime_types.h:43
impeller::RuntimeStage::SetClean
void SetClean()
Definition: runtime_stage.cc:151
impeller::kUnsignedByte
@ kUnsignedByte
Definition: runtime_types.h:16
impeller::kUnsignedInt64
@ kUnsignedInt64
Definition: runtime_types.h:22
impeller::kHalfFloat
@ kHalfFloat
Definition: runtime_types.h:23
impeller::kUnsignedInt
@ kUnsignedInt
Definition: runtime_types.h:20
impeller::RuntimeUniformDescription::type
RuntimeUniformType type
Definition: runtime_types.h:45
impeller::kSignedShort
@ kSignedShort
Definition: runtime_types.h:17
impeller::RuntimeUniformDescription::location
size_t location
Definition: runtime_types.h:44
impeller::RuntimeUniformDescription::bit_width
size_t bit_width
Definition: runtime_types.h:47
impeller::kDouble
@ kDouble
Definition: runtime_types.h:25
std
Definition: comparable.h:98
impeller::kSignedInt64
@ kSignedInt64
Definition: runtime_types.h:21
impeller::RuntimeStage::IsValid
bool IsValid() const
Definition: runtime_stage.cc:112
impeller::RuntimeStage::GetSkSLMapping
const std::shared_ptr< fml::Mapping > & GetSkSLMapping() const
Definition: runtime_stage.cc:120
impeller::RuntimeShaderStage::kCompute
@ kCompute
impeller::RuntimeStage::IsDirty
bool IsDirty() const
Definition: runtime_stage.cc:147
impeller::RuntimeUniformDescription::array_elements
std::optional< size_t > array_elements
Definition: runtime_types.h:48
impeller::RuntimeStage::GetEntrypoint
const std::string & GetEntrypoint() const
Definition: runtime_stage.cc:139
impeller::RuntimeShaderStage::kTessellationControl
@ kTessellationControl
impeller
Definition: aiks_context.cc:10
impeller::RuntimeStage::GetCodeMapping
const std::shared_ptr< fml::Mapping > & GetCodeMapping() const
Definition: runtime_stage.cc:116