Flutter Engine
 
Loading...
Searching...
No Matches
flutter::gpu::ShaderLibrary Class Reference

An immutable collection of shaders loaded from a shader bundle asset. More...

#include <shader_library.h>

Inheritance diagram for flutter::gpu::ShaderLibrary:
flutter::RefCountedDartWrappable< ShaderLibrary > fml::RefCountedThreadSafe< T > tonic::DartWrappable fml::internal::RefCountedThreadSafeBase

Public Types

using ShaderMap = std::unordered_map< std::string, fml::RefPtr< Shader > >
 
- Public Types inherited from tonic::DartWrappable
enum  DartNativeFields {
  kPeerIndex ,
  kNumberOfNativeFields
}
 

Public Member Functions

fml::RefPtr< ShaderGetShader (const std::string &shader_name, Dart_Handle shader_wrapper) const
 
 ~ShaderLibrary () override
 
- Public Member Functions inherited from flutter::RefCountedDartWrappable< ShaderLibrary >
virtual void RetainDartWrappableReference () const override
 
virtual void ReleaseDartWrappableReference () const override
 
- Public Member Functions inherited from fml::RefCountedThreadSafe< T >
void Release () const
 
- Public Member Functions inherited from fml::internal::RefCountedThreadSafeBase
void AddRef () const
 
bool HasOneRef () const
 
void AssertHasOneRef () const
 
- Public Member Functions inherited from tonic::DartWrappable
 DartWrappable ()
 
virtual const DartWrapperInfoGetDartWrapperInfo () const =0
 
Dart_Handle CreateDartWrapper (DartState *dart_state)
 
void AssociateWithDartWrapper (Dart_Handle wrappable)
 
void ClearDartWrapper ()
 
Dart_WeakPersistentHandle dart_wrapper () const
 

Static Public Member Functions

static fml::RefPtr< ShaderLibraryMakeFromAsset (impeller::Context::BackendType backend_type, const std::string &name, std::string &out_error)
 
static fml::RefPtr< ShaderLibraryMakeFromShaders (ShaderMap shaders)
 
static fml::RefPtr< ShaderLibraryMakeFromFlatbuffer (impeller::Context::BackendType backend_type, std::shared_ptr< fml::Mapping > payload)
 
static void SetOverride (fml::RefPtr< ShaderLibrary > override_shader_library)
 Sets a return override for MakeFromAsset for testing purposes.
 

Additional Inherited Members

- Protected Member Functions inherited from fml::RefCountedThreadSafe< T >
 RefCountedThreadSafe ()
 
 ~RefCountedThreadSafe ()
 
- Protected Member Functions inherited from fml::internal::RefCountedThreadSafeBase
 RefCountedThreadSafeBase ()
 
 ~RefCountedThreadSafeBase ()
 
bool Release () const
 
void Adopt ()
 
- Protected Member Functions inherited from tonic::DartWrappable
virtual ~DartWrappable ()
 
- Static Protected Member Functions inherited from tonic::DartWrappable
static Dart_PersistentHandle GetTypeForWrapper (tonic::DartState *dart_state, const tonic::DartWrapperInfo &wrapper_info)
 

Detailed Description

An immutable collection of shaders loaded from a shader bundle asset.

Definition at line 21 of file shader_library.h.

Member Typedef Documentation

◆ ShaderMap

using flutter::gpu::ShaderLibrary::ShaderMap = std::unordered_map<std::string, fml::RefPtr<Shader> >

Definition at line 26 of file shader_library.h.

Constructor & Destructor Documentation

◆ ~ShaderLibrary()

flutter::gpu::ShaderLibrary::~ShaderLibrary ( )
overridedefault

Member Function Documentation

◆ GetShader()

fml::RefPtr< Shader > flutter::gpu::ShaderLibrary::GetShader ( const std::string &  shader_name,
Dart_Handle  shader_wrapper 
) const

Definition at line 325 of file shader_library.cc.

326 {
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}

Referenced by InternalFlutterGpu_ShaderLibrary_GetShader().

◆ MakeFromAsset()

fml::RefPtr< ShaderLibrary > flutter::gpu::ShaderLibrary::MakeFromAsset ( impeller::Context::BackendType  backend_type,
const std::string &  name,
std::string &  out_error 
)
static

Definition at line 28 of file shader_library.cc.

31 {
32 if (override_shader_library_) {
33 return override_shader_library_;
34 }
35
36 auto dart_state = UIDartState::Current();
37 std::shared_ptr<AssetManager> asset_manager =
38 dart_state->platform_configuration()->client()->GetAssetManager();
39
40 std::unique_ptr<fml::Mapping> data = asset_manager->GetAsMapping(name);
41 if (data == nullptr) {
42 out_error = std::string("Asset '") + name + std::string("' not found.");
43 return nullptr;
44 }
45
46 return MakeFromFlatbuffer(backend_type, std::move(data));
47}
static UIDartState * Current()
static fml::RefPtr< ShaderLibrary > MakeFromFlatbuffer(impeller::Context::BackendType backend_type, std::shared_ptr< fml::Mapping > payload)
DEF_SWITCHES_START aot vmservice shared library name
Definition switch_defs.h:27
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 switch_defs.h:36

References flutter::UIDartState::Current(), flutter::data, MakeFromFlatbuffer(), and flutter::name.

Referenced by InternalFlutterGpu_ShaderLibrary_InitializeWithAsset().

◆ MakeFromFlatbuffer()

fml::RefPtr< ShaderLibrary > flutter::gpu::ShaderLibrary::MakeFromFlatbuffer ( impeller::Context::BackendType  backend_type,
std::shared_ptr< fml::Mapping payload 
)
static

Definition at line 172 of file shader_library.cc.

174 {
175 if (payload == nullptr || !payload->GetMapping()) {
176 return nullptr;
177 }
178 if (!impeller::fb::shaderbundle::ShaderBundleBufferHasIdentifier(
179 payload->GetMapping())) {
180 return nullptr;
181 }
182 auto* bundle =
183 impeller::fb::shaderbundle::GetShaderBundle(payload->GetMapping());
184 if (!bundle) {
185 return nullptr;
186 }
187
188 ShaderLibrary::ShaderMap shader_map;
189
190 for (const auto* bundled_shader : *bundle->shaders()) {
191 const impeller::fb::shaderbundle::BackendShader* backend_shader =
192 GetShaderBackend(backend_type, bundled_shader);
193 if (!backend_shader) {
194 VALIDATION_LOG << "Failed to unpack shader \""
195 << bundled_shader->name()->c_str() << "\" from bundle.";
196 continue;
197 }
198
199 auto code_mapping = std::make_shared<fml::NonOwnedMapping>(
200 backend_shader->shader()->data(), //
201 backend_shader->shader()->size(), //
202 [payload = payload](auto, auto) {} //
203 );
204
205 std::vector<impeller::DescriptorSetLayout> descriptor_set_layouts;
206
207 std::unordered_map<std::string, Shader::UniformBinding> uniform_structs;
208 if (backend_shader->uniform_structs() != nullptr) {
209 for (const auto& uniform : *backend_shader->uniform_structs()) {
210 std::vector<impeller::ShaderStructMemberMetadata> members;
211 if (uniform->fields() != nullptr) {
212 for (const auto& struct_member : *uniform->fields()) {
213 members.push_back(impeller::ShaderStructMemberMetadata{
214 .type = FromUniformType(struct_member->type()),
215 .name = struct_member->name()->c_str(),
216 .offset = static_cast<size_t>(struct_member->offset_in_bytes()),
217 .size =
218 static_cast<size_t>(struct_member->element_size_in_bytes()),
219 .byte_length =
220 static_cast<size_t>(struct_member->total_size_in_bytes()),
221 .array_elements =
222 struct_member->array_elements() == 0
223 ? std::optional<size_t>(std::nullopt)
224 : static_cast<size_t>(struct_member->array_elements()),
225 });
226 }
227 }
228
229 uniform_structs[uniform->name()->str()] = Shader::UniformBinding{
230 .slot =
232 .name = uniform->name()->c_str(),
233 .ext_res_0 = static_cast<size_t>(uniform->ext_res_0()),
234 .set = static_cast<size_t>(uniform->set()),
235 .binding = static_cast<size_t>(uniform->binding()),
236 },
237 .metadata =
239 .name = uniform->name()->c_str(),
240 .members = members,
241 },
242 .size_in_bytes = static_cast<size_t>(uniform->size_in_bytes()),
243 };
244
245 descriptor_set_layouts.push_back(impeller::DescriptorSetLayout{
246 static_cast<uint32_t>(uniform->binding()),
248 ToShaderStage(backend_shader->stage()),
249 });
250 }
251 }
252
253 std::unordered_map<std::string, Shader::TextureBinding> uniform_textures;
254 if (backend_shader->uniform_textures() != nullptr) {
255 for (const auto& uniform : *backend_shader->uniform_textures()) {
256 Shader::TextureBinding texture_binding;
257 texture_binding.slot = impeller::SampledImageSlot{
258 .name = uniform->name()->c_str(),
259 .texture_index = static_cast<size_t>(uniform->ext_res_0()),
260 .set = static_cast<size_t>(uniform->set()),
261 .binding = static_cast<size_t>(uniform->binding()),
262 };
263 texture_binding.metadata = impeller::ShaderMetadata{
264 .name = uniform->name()->c_str(),
265 .members = {},
266 };
267
268 uniform_textures[uniform->name()->str()] = texture_binding;
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}
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, TextureBinding > uniform_textures, std::vector< impeller::DescriptorSetLayout > descriptor_set_layouts)
Definition shader.cc:38
std::unordered_map< std::string, fml::RefPtr< Shader > > ShaderMap
static int input(yyscan_t yyscanner)
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)
static impeller::ShaderStage ToShaderStage(impeller::fb::shaderbundle::ShaderStage stage)
static size_t SizeOfInputType(impeller::fb::shaderbundle::InputDataType input_type)
it will be possible to load the file into Perfetto s trace viewer use test Running tests that layout and measure text will not yield consistent results across various platforms Enabling this option will make font resolution default to the Ahem test font on all 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
Metadata required to bind a combined texture and sampler.
const char * name
The name of the uniform slot.
Metadata required to bind a buffer.
const char * name
The name of the uniform slot.
#define VALIDATION_LOG
Definition validation.h:91

References impeller::ShaderStageIOSlot::binding, impeller::ShaderStageIOSlot::bit_width, impeller::ShaderStageIOSlot::columns, flutter::gpu::FromInputType(), flutter::gpu::FromUniformType(), flutter::gpu::GetShaderBackend(), input(), impeller::kSampledImage, impeller::kUniformBuffer, impeller::ShaderStageIOSlot::location, flutter::gpu::Shader::Make(), flutter::gpu::Shader::TextureBinding::metadata, impeller::ShaderMetadata::name, impeller::ShaderUniformSlot::name, impeller::SampledImageSlot::name, impeller::ShaderStageIOSlot::name, impeller::ShaderStageIOSlot::offset, impeller::ShaderStageIOSlot::set, flutter::set, flutter::size, flutter::gpu::SizeOfInputType(), flutter::gpu::Shader::UniformBinding::slot, flutter::gpu::Shader::TextureBinding::slot, impeller::ShaderStageBufferLayout::stride, flutter::gpu::ToShaderStage(), impeller::ShaderStructMemberMetadata::type, impeller::ShaderStageIOSlot::type, VALIDATION_LOG, and impeller::ShaderStageIOSlot::vec_size.

Referenced by impeller::testing::InstantiateTestShaderLibrary(), and MakeFromAsset().

◆ MakeFromShaders()

fml::RefPtr< ShaderLibrary > flutter::gpu::ShaderLibrary::MakeFromShaders ( ShaderMap  shaders)
static

Definition at line 49 of file shader_library.cc.

49 {
50 return fml::MakeRefCounted<flutter::gpu::ShaderLibrary>(nullptr,
51 std::move(shaders));
52}

◆ SetOverride()

void flutter::gpu::ShaderLibrary::SetOverride ( fml::RefPtr< ShaderLibrary override_shader_library)
static

Sets a return override for MakeFromAsset for testing purposes.

Definition at line 320 of file shader_library.cc.

321 {
322 override_shader_library_ = std::move(override_shader_library);
323}

Referenced by impeller::testing::InstantiateTestShaderLibrary().


The documentation for this class was generated from the following files: