| 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 #include <sstream>
10
11 #include "fml/mapping.h"
12 #include "impeller/base/validation.h"
13 #include "impeller/core/runtime_types.h"
14 #include "impeller/core/shader_types.h"
15 #include "impeller/runtime_stage/runtime_stage_flatbuffers.h"
16 #include "runtime_stage_types_flatbuffers.h"
17
18 namespace impeller {
19
20 static RuntimeUniformType ToType(fb::UniformDataType type) {
21 switch (type) {
22 case fb::UniformDataType::kFloat:
23 return RuntimeUniformType::kFloat;
24 case fb::UniformDataType::kSampledImage:
25 return RuntimeUniformType::kSampledImage;
26 case fb::UniformDataType::kStruct:
27 return RuntimeUniformType::kStruct;
28 }
29 FML_UNREACHABLE();
30 }
31
32 static RuntimeShaderStage ToShaderStage(fb::Stage stage) {
33 switch (stage) {
34 case fb::Stage::kVertex:
35 return RuntimeShaderStage::kVertex;
36 case fb::Stage::kFragment:
37 return RuntimeShaderStage::kFragment;
38 case fb::Stage::kCompute:
39 return RuntimeShaderStage::kCompute;
40 }
41 FML_UNREACHABLE();
42 }
43
44 /// The generated name from GLSLang/shaderc for the UBO containing non-opaque
45 /// uniforms specified in the user-written runtime effect shader.
46 ///
47 /// Vulkan does not allow non-opaque uniforms outside of a UBO.
48 const char* RuntimeStage::kVulkanUBOName =
49 "_RESERVED_IDENTIFIER_FIXUP_gl_DefaultUniformBlock";
50
51 std::unique_ptr<RuntimeStage> RuntimeStage::RuntimeStageIfPresent(
52 const fb::RuntimeStage* runtime_stage,
53 const std::shared_ptr<fml::Mapping>& payload) {
54 if (!runtime_stage) {
55 return nullptr;
56 }
57
58 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
59 return std::unique_ptr<RuntimeStage>(
60 new RuntimeStage(runtime_stage, payload));
61 }
62
63 absl::StatusOr<RuntimeStage::Map> RuntimeStage::DecodeRuntimeStages(
64 const std::shared_ptr<fml::Mapping>& payload) {
65 if (payload == nullptr || !payload->GetMapping()) {
66 return absl::InvalidArgumentError("Payload is null or empty.");
67 }
68 if (!fb::RuntimeStagesBufferHasIdentifier(payload->GetMapping())) {
69 return absl::InvalidArgumentError(
70 "Payload does not have valid identifier.");
71 }
72
73 auto raw_stages = fb::GetRuntimeStages(payload->GetMapping());
74 if (!raw_stages) {
75 return absl::InvalidArgumentError("Failed to get runtime stages.");
76 }
77
78 const uint32_t version = raw_stages->format_version();
79 const auto expected =
80 static_cast<uint32_t>(fb::RuntimeStagesFormatVersion::kVersion);
81 if (version != expected) {
82 std::stringstream stream;
83 stream << "Unsupported runtime stages format version. Expected " << expected
84 << ", got " << version << ".";
85 return absl::InvalidArgumentError(stream.str());
86 }
87
88 return Map{
89 {RuntimeStageBackend::kSkSL,
90 RuntimeStageIfPresent(raw_stages->sksl(), payload)},
91 {RuntimeStageBackend::kMetal,
92 RuntimeStageIfPresent(raw_stages->metal(), payload)},
93 {RuntimeStageBackend::kOpenGLES,
94 RuntimeStageIfPresent(raw_stages->opengles(), payload)},
95 {RuntimeStageBackend::kOpenGLES3,
96 RuntimeStageIfPresent(raw_stages->opengles3(), payload)},
97 {RuntimeStageBackend::kVulkan,
98 RuntimeStageIfPresent(raw_stages->vulkan(), payload)},
99 };
100 }
101
102 RuntimeStage::RuntimeStage(const fb::RuntimeStage* runtime_stage,
103 const std::shared_ptr<fml::Mapping>& payload)
104 : payload_(payload) {
105 FML_DCHECK(runtime_stage);
106
107 stage_ = ToShaderStage(runtime_stage->stage());
108 entrypoint_ = runtime_stage->entrypoint()->str();
109
110 auto* uniforms = runtime_stage->uniforms();
111
112 // Note: image bindings are screwy and will always have the same offset.
113 // track the binding of the UBO to determine where the image bindings go.
114 // This is only guaranteed to give us the correct bindings if there is a
115 // single sampler2D.
116 std::optional<size_t> ubo_id;
117 if (uniforms) {
118 for (auto i = uniforms->begin(), end = uniforms->end(); i != end; i++) {
119 RuntimeUniformDescription desc;
120 desc.name = i->name()->str();
121 desc.location = i->location();
122 desc.binding = i->binding();
123 desc.type = ToType(i->type());
124 if (desc.type == kStruct) {
125 ubo_id = desc.location;
126 desc.binding = desc.location;
127 }
128 desc.dimensions = RuntimeUniformDimensions{
129 static_cast<size_t>(i->rows()), static_cast<size_t>(i->columns())};
130 desc.bit_width = i->bit_width();
131 desc.array_elements = i->array_elements();
132 if (i->struct_layout()) {
133 for (const auto& byte_type : *i->struct_layout()) {
134 desc.struct_layout.push_back(static_cast<uint8_t>(byte_type));
135 }
136 }
137 desc.struct_float_count = i->struct_float_count();
138 uniforms_.push_back(std::move(desc));
139 }
140 }
141
142 code_mapping_ = std::make_shared<fml::NonOwnedMapping>(
143 runtime_stage->shader()->data(), //
144 runtime_stage->shader()->size(), //
145 [payload = payload_](auto, auto) {} //
146 );
147
148 size_t binding = 64;
149 if (ubo_id.has_value() && ubo_id.value() == binding) {
150 binding++;
151 }
152 for (auto& uniform : uniforms_) {
153 if (uniform.type == kSampledImage) {
154 uniform.binding = binding;
155 binding++;
156 if (ubo_id.has_value() && ubo_id.value() == binding) {
157 binding++;
158 }
159 }
160 }
161
162 for (const auto& uniform : GetUniforms()) {
163 if (uniform.type == kStruct) {
164 descriptor_set_layouts_.push_back(DescriptorSetLayout{
165 static_cast<uint32_t>(uniform.location),
166 DescriptorType::kUniformBuffer,
167 ShaderStage::kFragment,
168 });
169 } else if (uniform.type == kSampledImage) {
170 descriptor_set_layouts_.push_back(DescriptorSetLayout{
171 static_cast<uint32_t>(uniform.binding),
172 DescriptorType::kSampledImage,
173 ShaderStage::kFragment,
174 });
175 }
176 }
177 is_valid_ = true;
178 }
179
180 RuntimeStage::~RuntimeStage() = default;
181 RuntimeStage::RuntimeStage(RuntimeStage&&) = default;
182 RuntimeStage& RuntimeStage::operator=(RuntimeStage&&) = default;
183
184 bool RuntimeStage::IsValid() const {
185 return is_valid_;
186 }
187
188 const std::shared_ptr<fml::Mapping>& RuntimeStage::GetCodeMapping() const {
189 return code_mapping_;
190 }
191
192 const std::vector<RuntimeUniformDescription>& RuntimeStage::GetUniforms()
193 const {
194 return uniforms_;
195 }
196
197 const RuntimeUniformDescription* RuntimeStage::GetUniform(
198 const std::string& name) const {
199 for (const auto& uniform : uniforms_) {
200 if (uniform.name == name) {
201 return &uniform;
202 }
203 }
204 return nullptr;
205 }
206
207 const std::string& RuntimeStage::GetEntrypoint() const {
208 return entrypoint_;
209 }
210
211 RuntimeShaderStage RuntimeStage::GetShaderStage() const {
212 return stage_;
213 }
214
215 bool RuntimeStage::IsDirty() const {
216 return is_dirty_;
217 }
218
219 void RuntimeStage::SetClean() {
220 is_dirty_ = false;
221 }
222
223 const std::vector<DescriptorSetLayout>& RuntimeStage::GetDescriptorSetLayouts()
224 const {
225 return descriptor_set_layouts_;
226 }
227
228 } // namespace impeller
type
GLenum type
Definition: blit_command_gles.cc:153
impeller::RuntimeStage
Definition: runtime_stage.h:21
impeller::RuntimeStage::~RuntimeStage
~RuntimeStage()
impeller::RuntimeStage::GetEntrypoint
const std::string & GetEntrypoint() const
Definition: runtime_stage.cc:207
impeller::RuntimeStage::operator=
RuntimeStage & operator=(RuntimeStage &&)
impeller::RuntimeStage::GetUniforms
const std::vector< RuntimeUniformDescription > & GetUniforms() const
Definition: runtime_stage.cc:192
impeller::RuntimeStage::SetClean
void SetClean()
Definition: runtime_stage.cc:219
impeller::RuntimeStage::IsValid
bool IsValid() const
Definition: runtime_stage.cc:184
impeller::RuntimeStage::GetUniform
const RuntimeUniformDescription * GetUniform(const std::string &name) const
Definition: runtime_stage.cc:197
impeller::RuntimeStage::Map
std::map< RuntimeStageBackend, std::shared_ptr< RuntimeStage > > Map
Definition: runtime_stage.h:25
impeller::RuntimeStage::kVulkanUBOName
static const char * kVulkanUBOName
Definition: runtime_stage.h:23
impeller::RuntimeStage::GetCodeMapping
const std::shared_ptr< fml::Mapping > & GetCodeMapping() const
Definition: runtime_stage.cc:188
impeller::RuntimeStage::IsDirty
bool IsDirty() const
Definition: runtime_stage.cc:215
impeller::RuntimeStage::GetDescriptorSetLayouts
const std::vector< DescriptorSetLayout > & GetDescriptorSetLayouts() const
Definition: runtime_stage.cc:223
impeller::RuntimeStage::DecodeRuntimeStages
static absl::StatusOr< Map > DecodeRuntimeStages(const std::shared_ptr< fml::Mapping > &payload)
Definition: runtime_stage.cc:63
impeller::RuntimeStage::GetShaderStage
RuntimeShaderStage GetShaderStage() const
Definition: runtime_stage.cc:211
impeller::RuntimeStage::RuntimeStage
RuntimeStage(const fb::RuntimeStage *runtime_stage, const std::shared_ptr< fml::Mapping > &payload)
Definition: runtime_stage.cc:102
impeller
Definition: allocation.cc:12
impeller::RuntimeShaderStage
RuntimeShaderStage
Definition: runtime_types.h:30
impeller::RuntimeShaderStage::kCompute
@ kCompute
impeller::RuntimeShaderStage::kFragment
@ kFragment
impeller::RuntimeShaderStage::kVertex
@ kVertex
impeller::RuntimeStageBackend::kOpenGLES
@ kOpenGLES
impeller::RuntimeStageBackend::kMetal
@ kMetal
impeller::RuntimeStageBackend::kVulkan
@ kVulkan
impeller::RuntimeStageBackend::kSkSL
@ kSkSL
impeller::RuntimeStageBackend::kOpenGLES3
@ kOpenGLES3
impeller::ToShaderStage
constexpr ShaderStage ToShaderStage(RuntimeShaderStage stage)
Definition: shader_types.h:29
impeller::ShaderStage::kFragment
@ kFragment
impeller::ToType
static RuntimeUniformType ToType(fb::UniformDataType type)
Definition: runtime_stage.cc:20
impeller::DescriptorType::kUniformBuffer
@ kUniformBuffer
impeller::DescriptorType::kSampledImage
@ kSampledImage
impeller::RuntimeUniformType
RuntimeUniformType
Definition: runtime_types.h:24
impeller::kSampledImage
@ kSampledImage
Definition: runtime_types.h:26
impeller::kFloat
@ kFloat
Definition: runtime_types.h:25
impeller::kStruct
@ kStruct
Definition: runtime_types.h:27
runtime_stage.h
runtime_types.h
shader_types.h
impeller::DescriptorSetLayout
Definition: shader_types.h:167
impeller::RuntimeUniformDescription
Definition: runtime_types.h:41
impeller::RuntimeUniformDescription::dimensions
RuntimeUniformDimensions dimensions
Definition: runtime_types.h:47
impeller::RuntimeUniformDescription::location
size_t location
Definition: runtime_types.h:43
impeller::RuntimeUniformDescription::struct_layout
std::vector< uint8_t > struct_layout
Definition: runtime_types.h:50
impeller::RuntimeUniformDescription::name
std::string name
Definition: runtime_types.h:42
impeller::RuntimeUniformDescription::type
RuntimeUniformType type
Definition: runtime_types.h:46
impeller::RuntimeUniformDescription::struct_float_count
size_t struct_float_count
Definition: runtime_types.h:51
impeller::RuntimeUniformDescription::array_elements
std::optional< size_t > array_elements
Definition: runtime_types.h:49
impeller::RuntimeUniformDescription::bit_width
size_t bit_width
Definition: runtime_types.h:48
impeller::RuntimeUniformDescription::binding
size_t binding
Location, but for Vulkan.
Definition: runtime_types.h:45
impeller::RuntimeUniformDimensions
Definition: runtime_types.h:36
end
const size_t end
Definition: tessellator_libtess.cc:63
validation.h