Flutter Engine
The Flutter Engine
shader_library.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 "flutter/lib/gpu/shader_library.h"
6
7#include <optional>
8#include <utility>
9
10#include "flutter/assets/asset_manager.h"
11#include "flutter/lib/gpu/fixtures.h"
12#include "flutter/lib/gpu/shader.h"
13#include "flutter/lib/ui/ui_dart_state.h"
14#include "flutter/lib/ui/window/platform_configuration.h"
15#include "fml/mapping.h"
16#include "fml/memory/ref_ptr.h"
20#include "impeller/shader_bundle/shader_bundle_flatbuffers.h"
21#include "lib/gpu/context.h"
22
23namespace flutter {
24namespace gpu {
25
27
28fml::RefPtr<ShaderLibrary> ShaderLibrary::override_shader_library_;
29
32 const std::string& name,
33 std::string& out_error) {
34 if (override_shader_library_) {
35 return override_shader_library_;
36 }
37
38 auto dart_state = UIDartState::Current();
39 std::shared_ptr<AssetManager> asset_manager =
40 dart_state->platform_configuration()->client()->GetAssetManager();
41
42 std::unique_ptr<fml::Mapping> data = asset_manager->GetAsMapping(name);
43 if (data == nullptr) {
44 out_error = std::string("Asset '") + name + std::string("' not found.");
45 return nullptr;
46 }
47
48 return MakeFromFlatbuffer(backend_type, std::move(data));
49}
50
52 return fml::MakeRefCounted<flutter::gpu::ShaderLibrary>(nullptr,
53 std::move(shaders));
54}
55
58 switch (stage) {
59 case impeller::fb::shaderbundle::ShaderStage::kVertex:
61 case impeller::fb::shaderbundle::ShaderStage::kFragment:
63 case impeller::fb::shaderbundle::ShaderStage::kCompute:
65 }
67}
68
70 impeller::fb::shaderbundle::InputDataType input_type) {
71 switch (input_type) {
72 case impeller::fb::shaderbundle::InputDataType::kBoolean:
74 case impeller::fb::shaderbundle::InputDataType::kSignedByte:
78 case impeller::fb::shaderbundle::InputDataType::kSignedShort:
80 case impeller::fb::shaderbundle::InputDataType::kUnsignedShort:
82 case impeller::fb::shaderbundle::InputDataType::kSignedInt:
84 case impeller::fb::shaderbundle::InputDataType::kUnsignedInt:
86 case impeller::fb::shaderbundle::InputDataType::kSignedInt64:
88 case impeller::fb::shaderbundle::InputDataType::kUnsignedInt64:
94 }
95}
96
98 impeller::fb::shaderbundle::UniformDataType uniform_type) {
99 switch (uniform_type) {
100 case impeller::fb::shaderbundle::UniformDataType::kBoolean:
102 case impeller::fb::shaderbundle::UniformDataType::kSignedByte:
106 case impeller::fb::shaderbundle::UniformDataType::kSignedShort:
108 case impeller::fb::shaderbundle::UniformDataType::kUnsignedShort:
110 case impeller::fb::shaderbundle::UniformDataType::kSignedInt:
112 case impeller::fb::shaderbundle::UniformDataType::kUnsignedInt:
114 case impeller::fb::shaderbundle::UniformDataType::kSignedInt64:
116 case impeller::fb::shaderbundle::UniformDataType::kUnsignedInt64:
122 case impeller::fb::shaderbundle::UniformDataType::kHalfFloat:
126 }
127}
128
129static size_t SizeOfInputType(
130 impeller::fb::shaderbundle::InputDataType input_type) {
131 switch (input_type) {
132 case impeller::fb::shaderbundle::InputDataType::kBoolean:
133 return 1;
134 case impeller::fb::shaderbundle::InputDataType::kSignedByte:
135 return 1;
137 return 1;
138 case impeller::fb::shaderbundle::InputDataType::kSignedShort:
139 return 2;
140 case impeller::fb::shaderbundle::InputDataType::kUnsignedShort:
141 return 2;
142 case impeller::fb::shaderbundle::InputDataType::kSignedInt:
143 return 4;
144 case impeller::fb::shaderbundle::InputDataType::kUnsignedInt:
145 return 4;
146 case impeller::fb::shaderbundle::InputDataType::kSignedInt64:
147 return 8;
148 case impeller::fb::shaderbundle::InputDataType::kUnsignedInt64:
149 return 8;
151 return 4;
153 return 8;
154 }
155}
156
157static const impeller::fb::shaderbundle::BackendShader* GetShaderBackend(
159 const impeller::fb::shaderbundle::Shader* shader) {
160 switch (backend_type) {
162#ifdef FML_OS_IOS
163 return shader->metal_ios();
164#else
165 return shader->metal_desktop();
166#endif
168#ifdef FML_OS_ANDROID
169 return shader->opengl_es();
170#else
171 return shader->opengl_desktop();
172#endif
174 return shader->vulkan();
175 }
176}
177
180 std::shared_ptr<fml::Mapping> payload) {
181 if (payload == nullptr || !payload->GetMapping()) {
182 return nullptr;
183 }
184 if (!impeller::fb::shaderbundle::ShaderBundleBufferHasIdentifier(
185 payload->GetMapping())) {
186 return nullptr;
187 }
188 auto* bundle =
189 impeller::fb::shaderbundle::GetShaderBundle(payload->GetMapping());
190 if (!bundle) {
191 return nullptr;
192 }
193
194 ShaderLibrary::ShaderMap shader_map;
195
196 for (const auto* bundled_shader : *bundle->shaders()) {
197 const impeller::fb::shaderbundle::BackendShader* backend_shader =
198 GetShaderBackend(backend_type, bundled_shader);
199 if (!backend_shader) {
200 VALIDATION_LOG << "Failed to unpack shader \""
201 << bundled_shader->name()->c_str() << "\" from bundle.";
202 continue;
203 }
204
205 auto code_mapping = std::make_shared<fml::NonOwnedMapping>(
206 backend_shader->shader()->data(), //
207 backend_shader->shader()->size(), //
208 [payload = payload](auto, auto) {} //
209 );
210
211 std::vector<impeller::DescriptorSetLayout> descriptor_set_layouts;
212
213 std::unordered_map<std::string, Shader::UniformBinding> uniform_structs;
214 if (backend_shader->uniform_structs() != nullptr) {
215 for (const auto& uniform : *backend_shader->uniform_structs()) {
216 std::vector<impeller::ShaderStructMemberMetadata> members;
217 if (uniform->fields() != nullptr) {
218 for (const auto& struct_member : *uniform->fields()) {
219 members.push_back(impeller::ShaderStructMemberMetadata{
220 .type = FromUniformType(struct_member->type()),
221 .name = struct_member->name()->c_str(),
222 .offset = static_cast<size_t>(struct_member->offset_in_bytes()),
223 .size =
224 static_cast<size_t>(struct_member->element_size_in_bytes()),
225 .byte_length =
226 static_cast<size_t>(struct_member->total_size_in_bytes()),
227 .array_elements =
228 struct_member->array_elements() != 0
229 ? std::optional<size_t>(std::nullopt)
230 : static_cast<size_t>(struct_member->array_elements()),
231 });
232 }
233 }
234
235 uniform_structs[uniform->name()->str()] = Shader::UniformBinding{
236 .slot =
238 .name = uniform->name()->c_str(),
239 .ext_res_0 = static_cast<size_t>(uniform->ext_res_0()),
240 .set = static_cast<size_t>(uniform->set()),
241 .binding = static_cast<size_t>(uniform->binding()),
242 },
243 .metadata =
245 .name = uniform->name()->c_str(),
246 .members = members,
247 },
248 .size_in_bytes = static_cast<size_t>(uniform->size_in_bytes()),
249 };
250
251 descriptor_set_layouts.push_back(impeller::DescriptorSetLayout{
252 static_cast<uint32_t>(uniform->binding()),
254 ToShaderStage(backend_shader->stage()),
255 });
256 }
257 }
258
259 std::unordered_map<std::string, impeller::SampledImageSlot>
260 uniform_textures;
261 if (backend_shader->uniform_textures() != nullptr) {
262 for (const auto& uniform : *backend_shader->uniform_textures()) {
263 uniform_textures[uniform->name()->str()] = impeller::SampledImageSlot{
264 .name = uniform->name()->c_str(),
265 .texture_index = static_cast<size_t>(uniform->ext_res_0()),
266 .set = static_cast<size_t>(uniform->set()),
267 .binding = static_cast<size_t>(uniform->binding()),
268 };
269
270 descriptor_set_layouts.push_back(impeller::DescriptorSetLayout{
271 static_cast<uint32_t>(uniform->binding()),
273 ToShaderStage(backend_shader->stage()),
274 });
275 }
276 }
277
278 std::vector<impeller::ShaderStageIOSlot> inputs;
279 std::vector<impeller::ShaderStageBufferLayout> layouts;
280 if (backend_shader->stage() ==
281 impeller::fb::shaderbundle::ShaderStage::kVertex) {
282 auto inputs_fb = backend_shader->inputs();
283
284 inputs.reserve(inputs_fb->size());
285 size_t default_stride = 0;
286 for (const auto& input : *inputs_fb) {
288 slot.name = input->name()->c_str();
289 slot.location = input->location();
290 slot.set = input->set();
291 slot.binding = input->binding();
292 slot.type = FromInputType(input->type());
293 slot.bit_width = input->bit_width();
294 slot.vec_size = input->vec_size();
295 slot.columns = input->columns();
296 slot.offset = input->offset();
297 inputs.emplace_back(slot);
298
299 default_stride +=
300 SizeOfInputType(input->type()) * slot.vec_size * slot.columns;
301 }
303 .stride = default_stride,
304 .binding = 0u,
305 }};
306 }
307
308 auto shader = flutter::gpu::Shader::Make(
309 backend_shader->entrypoint()->str(),
310 ToShaderStage(backend_shader->stage()), std::move(code_mapping),
311 std::move(inputs), std::move(layouts), std::move(uniform_structs),
312 std::move(uniform_textures), std::move(descriptor_set_layouts));
313 shader_map[bundled_shader->name()->str()] = std::move(shader);
314 }
315
316 return fml::MakeRefCounted<flutter::gpu::ShaderLibrary>(
317 std::move(payload), std::move(shader_map));
318}
319
321 fml::RefPtr<ShaderLibrary> override_shader_library) {
322 override_shader_library_ = std::move(override_shader_library);
323}
324
325fml::RefPtr<Shader> ShaderLibrary::GetShader(const std::string& shader_name,
326 Dart_Handle shader_wrapper) const {
327 auto it = shaders_.find(shader_name);
328 if (it == shaders_.end()) {
329 return nullptr; // No matching shaders.
330 }
331 auto shader = it->second;
332
333 if (shader->dart_wrapper() == nullptr) {
334 shader->AssociateWithDartWrapper(shader_wrapper);
335 }
336 return shader;
337}
338
339ShaderLibrary::ShaderLibrary(std::shared_ptr<fml::Mapping> payload,
340 ShaderMap shaders)
341 : payload_(std::move(payload)), shaders_(std::move(shaders)) {}
342
344
345} // namespace gpu
346} // namespace flutter
347
348//----------------------------------------------------------------------------
349/// Exports
350///
351
353 Dart_Handle wrapper,
354 Dart_Handle asset_name) {
355 if (!Dart_IsString(asset_name)) {
356 return tonic::ToDart("Asset name must be a string");
357 }
358
359 std::optional<std::string> out_error;
360 auto impeller_context = flutter::gpu::Context::GetDefaultContext(out_error);
361 if (out_error.has_value()) {
362 return tonic::ToDart(out_error.value());
363 }
364
365 std::string error;
367 impeller_context->GetBackendType(), tonic::StdStringFromDart(asset_name),
368 error);
369 if (!res) {
370 return tonic::ToDart(error);
371 }
372 res->AssociateWithDartWrapper(wrapper);
373 return Dart_Null();
374}
375
378 Dart_Handle shader_name,
379 Dart_Handle shader_wrapper) {
380 FML_DCHECK(Dart_IsString(shader_name));
381 auto shader =
382 wrapper->GetShader(tonic::StdStringFromDart(shader_name), shader_wrapper);
383 if (!shader) {
384 return Dart_Null();
385 }
386 return tonic::ToDart(shader.get());
387}
static UIDartState * Current()
static std::shared_ptr< impeller::Context > GetDefaultContext(std::optional< std::string > &out_error)
Definition: context.cc:29
An immutable collection of shaders loaded from a shader bundle asset.
static void SetOverride(fml::RefPtr< ShaderLibrary > override_shader_library)
Sets a return override for MakeFromAsset for testing purposes.
static fml::RefPtr< ShaderLibrary > MakeFromAsset(impeller::Context::BackendType backend_type, const std::string &name, std::string &out_error)
static fml::RefPtr< ShaderLibrary > MakeFromShaders(ShaderMap shaders)
static fml::RefPtr< ShaderLibrary > MakeFromFlatbuffer(impeller::Context::BackendType backend_type, std::shared_ptr< fml::Mapping > payload)
std::unordered_map< std::string, fml::RefPtr< Shader > > ShaderMap
fml::RefPtr< Shader > GetShader(const std::string &shader_name, Dart_Handle shader_wrapper) const
static fml::RefPtr< Shader > Make(std::string entrypoint, impeller::ShaderStage stage, std::shared_ptr< fml::Mapping > code_mapping, std::vector< impeller::ShaderStageIOSlot > inputs, std::vector< impeller::ShaderStageBufferLayout > layouts, std::unordered_map< std::string, UniformBinding > uniform_structs, std::unordered_map< std::string, impeller::SampledImageSlot > uniform_textures, std::vector< impeller::DescriptorSetLayout > descriptor_set_layouts)
Definition: shader.cc:38
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
DART_EXPORT Dart_Handle Dart_Null(void)
DART_EXPORT bool Dart_IsString(Dart_Handle object)
const uint8_t uint32_t uint32_t GError ** error
#define FML_UNREACHABLE()
Definition: logging.h:109
#define FML_DCHECK(condition)
Definition: logging.h:103
Dart_Handle InternalFlutterGpu_ShaderLibrary_InitializeWithAsset(Dart_Handle wrapper, Dart_Handle asset_name)
Dart_Handle InternalFlutterGpu_ShaderLibrary_GetShader(flutter::gpu::ShaderLibrary *wrapper, Dart_Handle shader_name, Dart_Handle shader_wrapper)
static const impeller::fb::shaderbundle::BackendShader * GetShaderBackend(impeller::Context::BackendType backend_type, const impeller::fb::shaderbundle::Shader *shader)
static impeller::ShaderType FromUniformType(impeller::fb::shaderbundle::UniformDataType uniform_type)
static impeller::ShaderType FromInputType(impeller::fb::shaderbundle::InputDataType input_type)
IMPLEMENT_WRAPPERTYPEINFO(flutter_gpu, CommandBuffer)
static impeller::ShaderStage ToShaderStage(impeller::fb::shaderbundle::ShaderStage stage)
static size_t SizeOfInputType(impeller::fb::shaderbundle::InputDataType input_type)
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition: switches.h:41
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not set
Definition: switches.h:76
Definition: ref_ptr.h:256
Dart_Handle ToDart(const T &object)
std::string StdStringFromDart(Dart_Handle handle)
impeller::ShaderUniformSlot slot
Definition: shader.h:29
Metadata required to bind a combined texture and sampler.
Definition: shader_types.h:98
const char * name
The name of the uniform slot.
Definition: shader_types.h:100
Metadata required to bind a buffer.
Definition: shader_types.h:81
const char * name
The name of the uniform slot.
Definition: shader_types.h:83
#define VALIDATION_LOG
Definition: validation.h:73