Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
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, std::string library_id="")
 
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 348 of file shader_library.cc.

349 {
350 auto it = shaders_.find(shader_name);
351 if (it == shaders_.end()) {
352 return nullptr; // No matching shaders.
353 }
354 auto shader = it->second;
355
356 if (shader->dart_wrapper() == nullptr) {
357 shader->AssociateWithDartWrapper(shader_wrapper);
358 }
359 return shader;
360}

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 29 of file shader_library.cc.

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

library_id is a stable identifier (typically the asset path the bundle was loaded from) used to namespace the shaders' entrypoints in the shared shader registry, so they cannot collide with engine-internal shaders or with shaders from a different bundle. If empty, a process-unique fallback is generated.

Definition at line 173 of file shader_library.cc.

176 {
177 if (payload == nullptr || !payload->GetMapping()) {
178 return nullptr;
179 }
180 if (library_id.empty()) {
182 }
183 if (!impeller::fb::shaderbundle::ShaderBundleBufferHasIdentifier(
184 payload->GetMapping())) {
185 return nullptr;
186 }
187 auto* bundle =
188 impeller::fb::shaderbundle::GetShaderBundle(payload->GetMapping());
189 if (!bundle) {
190 return nullptr;
191 }
192
193 const auto version = bundle->format_version();
194 const auto expected = static_cast<uint32_t>(
195 impeller::fb::shaderbundle::ShaderBundleFormatVersion::kVersion);
196 if (version != expected) {
197 VALIDATION_LOG << "Unsupported shader bundle format version: " << version
198 << ", expected: " << expected
199 << ". This shader bundle was compiled with an incompatible "
200 "version of impellerc. Please rebuild the shader bundle "
201 "with the version of impellerc that ships with the "
202 "current Flutter SDK.";
203 return nullptr;
204 }
205
206 ShaderLibrary::ShaderMap shader_map;
207
208 for (const auto* bundled_shader : *bundle->shaders()) {
209 const impeller::fb::shaderbundle::BackendShader* backend_shader =
210 GetShaderBackend(backend_type, bundled_shader);
211 if (!backend_shader) {
212 VALIDATION_LOG << "Failed to unpack shader \""
213 << bundled_shader->name()->c_str() << "\" from bundle.";
214 continue;
215 }
216
217 auto code_mapping = std::make_shared<fml::NonOwnedMapping>(
218 backend_shader->shader()->data(), //
219 backend_shader->shader()->size(), //
220 [payload = payload](auto, auto) {} //
221 );
222
223 std::vector<impeller::DescriptorSetLayout> descriptor_set_layouts;
224
225 std::unordered_map<std::string, Shader::UniformBinding> uniform_structs;
226 if (backend_shader->uniform_structs() != nullptr) {
227 for (const auto& uniform : *backend_shader->uniform_structs()) {
228 std::vector<impeller::ShaderStructMemberMetadata> members;
229 if (uniform->fields() != nullptr) {
230 for (const auto& struct_member : *uniform->fields()) {
232 FromUniformType(struct_member->type());
233 members.push_back(impeller::ShaderStructMemberMetadata{
234 .type = type,
235 .name = struct_member->name()->c_str(),
236 .offset = static_cast<size_t>(struct_member->offset_in_bytes()),
237 .size =
238 static_cast<size_t>(struct_member->element_size_in_bytes()),
239 .byte_length =
240 static_cast<size_t>(struct_member->total_size_in_bytes()),
241 .array_elements =
242 struct_member->array_elements() == 0
243 ? std::optional<size_t>(std::nullopt)
244 : static_cast<size_t>(struct_member->array_elements()),
245 .float_type = impeller::DeriveShaderFloatType(
246 type, static_cast<size_t>(struct_member->vec_size()),
247 static_cast<size_t>(struct_member->columns())),
248 });
249 }
250 }
251
252 uniform_structs[uniform->name()->str()] = Shader::UniformBinding{
253 .slot =
255 .name = uniform->name()->c_str(),
256 .ext_res_0 = static_cast<size_t>(uniform->ext_res_0()),
257 .set = static_cast<size_t>(uniform->set()),
258 .binding = static_cast<size_t>(uniform->binding()),
259 },
260 .metadata =
262 .name = uniform->name()->c_str(),
263 .members = members,
264 },
265 .size_in_bytes = static_cast<size_t>(uniform->size_in_bytes()),
266 };
267
268 descriptor_set_layouts.push_back(impeller::DescriptorSetLayout{
269 static_cast<uint32_t>(uniform->binding()),
271 ToShaderStage(backend_shader->stage()),
272 });
273 }
274 }
275
276 std::unordered_map<std::string, Shader::TextureBinding> uniform_textures;
277 if (backend_shader->uniform_textures() != nullptr) {
278 for (const auto& uniform : *backend_shader->uniform_textures()) {
279 Shader::TextureBinding texture_binding;
280 texture_binding.slot = impeller::SampledImageSlot{
281 .name = uniform->name()->c_str(),
282 .texture_index = static_cast<size_t>(uniform->ext_res_0()),
283 .set = static_cast<size_t>(uniform->set()),
284 .binding = static_cast<size_t>(uniform->binding()),
285 };
286 texture_binding.metadata = impeller::ShaderMetadata{
287 .name = uniform->name()->c_str(),
288 .members = {},
289 };
290
291 uniform_textures[uniform->name()->str()] = texture_binding;
292
293 descriptor_set_layouts.push_back(impeller::DescriptorSetLayout{
294 static_cast<uint32_t>(uniform->binding()),
296 ToShaderStage(backend_shader->stage()),
297 });
298 }
299 }
300
301 std::vector<impeller::ShaderStageIOSlot> inputs;
302 std::vector<impeller::ShaderStageBufferLayout> layouts;
303 if (backend_shader->stage() ==
304 impeller::fb::shaderbundle::ShaderStage::kVertex) {
305 auto inputs_fb = backend_shader->inputs();
306
307 inputs.reserve(inputs_fb->size());
308 size_t default_stride = 0;
309 for (const auto& input : *inputs_fb) {
311 slot.name = input->name()->c_str();
312 slot.location = input->location();
313 slot.set = input->set();
314 slot.binding = input->binding();
315 slot.type = FromInputType(input->type());
316 slot.bit_width = input->bit_width();
317 slot.vec_size = input->vec_size();
318 slot.columns = input->columns();
319 slot.offset = input->offset();
320 inputs.emplace_back(slot);
321
322 default_stride +=
323 SizeOfInputType(input->type()) * slot.vec_size * slot.columns;
324 }
326 .stride = default_stride,
327 .binding = 0u,
328 }};
329 }
330
331 auto shader = flutter::gpu::Shader::Make(
332 library_id, backend_shader->entrypoint()->str(),
333 ToShaderStage(backend_shader->stage()), std::move(code_mapping),
334 std::move(inputs), std::move(layouts), std::move(uniform_structs),
335 std::move(uniform_textures), std::move(descriptor_set_layouts));
336 shader_map[bundled_shader->name()->str()] = std::move(shader);
337 }
338
339 return fml::MakeRefCounted<flutter::gpu::ShaderLibrary>(
340 std::move(payload), std::move(shader_map));
341}
static fml::RefPtr< Shader > Make(std::string library_id, 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:39
std::unordered_map< std::string, fml::RefPtr< Shader > > ShaderMap
static int input(yyscan_t yyscanner)
uint32_t vec_size
uint32_t columns
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)
constexpr std::optional< ShaderFloatType > DeriveShaderFloatType(ShaderType type, size_t vec_size, size_t columns)
Derive the ShaderFloatType from the base ShaderType and the (vec_size, columns) dimensions reported b...
impeller::ShaderType type
Metadata required to bind a combined texture and sampler.
const char * name
The name of the uniform slot.
static std::string MakeFallbackLibraryId()
Definition shader_key.cc:14
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, impeller::DeriveShaderFloatType(), flutter::gpu::FromInputType(), flutter::gpu::FromUniformType(), flutter::gpu::GetShaderBackend(), input(), impeller::kSampledImage, impeller::kUniformBuffer, impeller::ShaderStageIOSlot::location, flutter::gpu::Shader::Make(), impeller::ShaderKey::MakeFallbackLibraryId(), 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::gpu::SizeOfInputType(), flutter::gpu::Shader::UniformBinding::slot, flutter::gpu::Shader::TextureBinding::slot, impeller::ShaderStageBufferLayout::stride, flutter::gpu::ToShaderStage(), impeller::ShaderStructMemberMetadata::type, impeller::ShaderStageIOSlot::type, 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 50 of file shader_library.cc.

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

◆ 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 343 of file shader_library.cc.

344 {
345 override_shader_library_ = std::move(override_shader_library);
346}

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


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