| 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
5 #include "impeller/runtime_stage/runtime_stage.h"
6
7 #include <array>
8
9 #include "impeller/base/validation.h"
10 #include "impeller/runtime_stage/runtime_stage_flatbuffers.h"
11
12 namespace impeller {
13
14 static RuntimeUniformType ToType(fb::UniformDataType type) {
15 switch (type) {
16 case fb::UniformDataType::kBoolean:
17 return RuntimeUniformType::kBoolean;
18 case fb::UniformDataType::kSignedByte:
19 return RuntimeUniformType::kSignedByte;
20 case fb::UniformDataType::kUnsignedByte:
21 return RuntimeUniformType::kUnsignedByte;
22 case fb::UniformDataType::kSignedShort:
23 return RuntimeUniformType::kSignedShort;
24 case fb::UniformDataType::kUnsignedShort:
25 return RuntimeUniformType::kUnsignedShort;
26 case fb::UniformDataType::kSignedInt:
27 return RuntimeUniformType::kSignedInt;
28 case fb::UniformDataType::kUnsignedInt:
29 return RuntimeUniformType::kUnsignedInt;
30 case fb::UniformDataType::kSignedInt64:
31 return RuntimeUniformType::kSignedInt64;
32 case fb::UniformDataType::kUnsignedInt64:
33 return RuntimeUniformType::kUnsignedInt64;
34 case fb::UniformDataType::kHalfFloat:
35 return RuntimeUniformType::kHalfFloat;
36 case fb::UniformDataType::kFloat:
37 return RuntimeUniformType::kFloat;
38 case fb::UniformDataType::kDouble:
39 return RuntimeUniformType::kDouble;
40 case fb::UniformDataType::kSampledImage:
41 return RuntimeUniformType::kSampledImage;
42 }
43 FML_UNREACHABLE();
44 }
45
46 static RuntimeShaderStage ToShaderStage(fb::Stage stage) {
47 switch (stage) {
48 case fb::Stage::kVertex:
49 return RuntimeShaderStage::kVertex;
50 case fb::Stage::kFragment:
51 return RuntimeShaderStage::kFragment;
52 case fb::Stage::kCompute:
53 return RuntimeShaderStage::kCompute;
54 case fb::Stage::kTessellationControl:
55 return RuntimeShaderStage::kTessellationControl;
56 case fb::Stage::kTessellationEvaluation:
57 return RuntimeShaderStage::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++) {
81 RuntimeUniformDescription desc;
82 desc.name = i->name()->str();
83 desc.location = i->location();
84 desc.type = ToType(i->type());
85 desc.dimensions = RuntimeUniformDimensions{
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;
109 RuntimeStage::RuntimeStage(RuntimeStage&&) = default;
110 RuntimeStage& RuntimeStage::operator=(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
129 const RuntimeUniformDescription* RuntimeStage::GetUniform(
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
143 RuntimeShaderStage RuntimeStage::GetShaderStage() const {
144 return stage_;
145 }
146
147 bool RuntimeStage::IsDirty() const {
148 return is_dirty_;
149 }
150
151 void RuntimeStage::SetClean() {
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