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