Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
flutter::gpu::Shader Class Reference

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

#include <shader.h>

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

Classes

struct  TextureBinding
 
struct  UniformBinding
 

Public Member Functions

 ~Shader () override
 
std::shared_ptr< const impeller::ShaderFunctionGetFunctionFromLibrary (impeller::ShaderLibrary &library)
 
bool IsRegistered (Context &context)
 
bool RegisterSync (Context &context)
 
bool IsDirty () const
 
void SetClean ()
 
void ResetFrom (Shader &other)
 
std::shared_ptr< impeller::VertexDescriptorCreateVertexDescriptor () const
 
const std::vector< impeller::ShaderStageIOSlot > & GetStageInputs () const
 
const std::vector< impeller::ShaderStageBufferLayout > & GetStageBufferLayouts () const
 
const std::vector< impeller::DescriptorSetLayout > & GetDescriptorSetLayouts () const
 
impeller::ShaderStage GetShaderStage () const
 
const Shader::UniformBindingGetUniformStruct (const std::string &name) const
 
const Shader::TextureBindingGetUniformTexture (const std::string &name) const
 
- Public Member Functions inherited from flutter::RefCountedDartWrappable< Shader >
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< ShaderMake (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)
 

Additional Inherited Members

- Public Types inherited from tonic::DartWrappable
enum  DartNativeFields {
  kPeerIndex ,
  kNumberOfNativeFields
}
 
- 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 23 of file shader.h.

Constructor & Destructor Documentation

◆ ~Shader()

flutter::gpu::Shader::~Shader ( )
overridedefault

Member Function Documentation

◆ CreateVertexDescriptor()

std::shared_ptr< impeller::VertexDescriptor > flutter::gpu::Shader::CreateVertexDescriptor ( ) const

Definition at line 151 of file shader.cc.

152 {
153 auto vertex_descriptor = std::make_shared<impeller::VertexDescriptor>();
154 vertex_descriptor->SetStageInputs(inputs_, layouts_);
155 return vertex_descriptor;
156}

Referenced by InternalFlutterGpu_RenderPipeline_Initialize().

◆ GetDescriptorSetLayouts()

const std::vector< impeller::DescriptorSetLayout > & flutter::gpu::Shader::GetDescriptorSetLayouts ( ) const

Definition at line 172 of file shader.cc.

172 {
173 return descriptor_set_layouts_;
174}

Referenced by flutter::gpu::RenderPipeline::RenderPipeline().

◆ GetFunctionFromLibrary()

std::shared_ptr< const impeller::ShaderFunction > flutter::gpu::Shader::GetFunctionFromLibrary ( impeller::ShaderLibrary library)

Definition at line 69 of file shader.cc.

70 {
71 return library.GetFunction(GetScopedName(), stage_);
72}
virtual std::shared_ptr< const ShaderFunction > GetFunction(std::string_view name, ShaderStage stage)=0

References impeller::ShaderLibrary::GetFunction().

Referenced by flutter::gpu::RenderPipeline::BindToPipelineDescriptor(), and IsRegistered().

◆ GetShaderStage()

impeller::ShaderStage flutter::gpu::Shader::GetShaderStage ( ) const

Definition at line 167 of file shader.cc.

167 {
168 return stage_;
169}

Referenced by BindUniform(), and InternalFlutterGpu_RenderPass_BindTexture().

◆ GetStageBufferLayouts()

const std::vector< impeller::ShaderStageBufferLayout > & flutter::gpu::Shader::GetStageBufferLayouts ( ) const

Definition at line 163 of file shader.cc.

163 {
164 return layouts_;
165}

◆ GetStageInputs()

const std::vector< impeller::ShaderStageIOSlot > & flutter::gpu::Shader::GetStageInputs ( ) const

Definition at line 158 of file shader.cc.

158 {
159 return inputs_;
160}

◆ GetUniformStruct()

const Shader::UniformBinding * flutter::gpu::Shader::GetUniformStruct ( const std::string &  name) const

Definition at line 176 of file shader.cc.

177 {
178 auto uniform = uniform_structs_.find(name);
179 if (uniform == uniform_structs_.end()) {
180 return nullptr;
181 }
182 return &uniform->second;
183}
DEF_SWITCHES_START aot vmservice shared library name
Definition switch_defs.h:27

References flutter::name.

Referenced by BindUniform(), InternalFlutterGpu_Shader_GetUniformMemberOffset(), and InternalFlutterGpu_Shader_GetUniformStructSize().

◆ GetUniformTexture()

const Shader::TextureBinding * flutter::gpu::Shader::GetUniformTexture ( const std::string &  name) const

Definition at line 185 of file shader.cc.

186 {
187 auto uniform = uniform_textures_.find(name);
188 if (uniform == uniform_textures_.end()) {
189 return nullptr;
190 }
191 return &uniform->second;
192}

References flutter::name.

Referenced by InternalFlutterGpu_RenderPass_BindTexture().

◆ IsDirty()

bool flutter::gpu::Shader::IsDirty ( ) const

Whether this shader needs to be re-registered with the impeller shader library on next use. Fresh shaders start dirty. Set back to false by RegisterSync after registration completes, and back to true by ResetFrom when the underlying asset is reloaded.

Definition at line 79 of file shader.cc.

79 {
80 return is_dirty_;
81}

Referenced by InternalFlutterGpu_Shader_DebugIsDirty().

◆ IsRegistered()

bool flutter::gpu::Shader::IsRegistered ( Context context)

Definition at line 74 of file shader.cc.

74 {
75 auto& lib = *context.GetContext().GetShaderLibrary();
76 return GetFunctionFromLibrary(lib) != nullptr;
77}
std::shared_ptr< const impeller::ShaderFunction > GetFunctionFromLibrary(impeller::ShaderLibrary &library)
Definition shader.cc:69
std::shared_ptr< ContextGLES > context

References context, and GetFunctionFromLibrary().

◆ Make()

fml::RefPtr< Shader > flutter::gpu::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 
)
static

Definition at line 41 of file shader.cc.

50 {
51 auto shader = fml::MakeRefCounted<Shader>();
52 shader->library_id_ = std::move(library_id);
53 shader->entrypoint_ = std::move(entrypoint);
54 shader->stage_ = stage;
55 shader->code_mapping_ = std::move(code_mapping);
56 shader->inputs_ = std::move(inputs);
57 shader->layouts_ = std::move(layouts);
58 shader->uniform_structs_ = std::move(uniform_structs);
59 shader->uniform_textures_ = std::move(uniform_textures);
60 shader->descriptor_set_layouts_ = std::move(descriptor_set_layouts);
61 return shader;
62}

Referenced by flutter::gpu::ParseShaderBundle().

◆ RegisterSync()

bool flutter::gpu::Shader::RegisterSync ( Context context)

Definition at line 118 of file shader.cc.

118 {
119 auto& lib = *context.GetContext().GetShaderLibrary();
120 const std::string scoped_name = GetScopedName();
121
122 std::shared_ptr<const impeller::ShaderFunction> existing =
123 lib.GetFunction(scoped_name, stage_);
124 if (existing && !is_dirty_) {
125 return true; // Already registered and current.
126 }
127
128 // Dirty path: an earlier asset version still occupies the scoped slot.
129 // Evict it (and any pipelines that referenced it) before registering the
130 // new code mapping. Mirrors `RuntimeEffectContents::RegisterShader`.
131 if (existing && is_dirty_) {
132 context.GetContext().GetPipelineLibrary()->RemovePipelinesWithEntryPoint(
133 existing);
134 lib.UnregisterFunction(scoped_name, stage_);
135 }
136
137 std::promise<bool> promise;
138 auto future = promise.get_future();
139 lib.RegisterFunction(
140 scoped_name, stage_, code_mapping_,
141 fml::MakeCopyable([promise = std::move(promise)](bool result) mutable {
142 promise.set_value(result);
143 }));
144 if (!future.get()) {
145 return false; // Registration failed.
146 }
147 is_dirty_ = false;
148 return true;
149}
internal::CopyableLambda< T > MakeCopyable(T lambda)

References context, and fml::MakeCopyable().

Referenced by InternalFlutterGpu_RenderPipeline_Initialize().

◆ ResetFrom()

void flutter::gpu::Shader::ResetFrom ( Shader other)

Replaces this shader's payload (code, layouts, uniforms) with the data from other, preserving the library_id / entrypoint registry key and marking this shader dirty. Used by ShaderLibrary to reload a shader bundle in place without breaking existing Dart wrappers.

Definition at line 87 of file shader.cc.

87 {
88 // Compare the compiled bytes against the freshly-parsed mapping before
89 // moving anything. A shader bundle is the unit of asset distribution, so
90 // editing one shader recompiles the whole bundle; without this dedupe
91 // every unchanged shader in the bundle would still be evicted and
92 // re-registered on reload. Code-byte equality is sufficient because
93 // impellerc derives reflection metadata (uniforms, inputs, layouts)
94 // deterministically from the compiled output.
95 const bool code_changed =
96 code_mapping_ == nullptr || other.code_mapping_ == nullptr ||
97 code_mapping_->GetSize() != other.code_mapping_->GetSize() ||
98 std::memcmp(code_mapping_->GetMapping(),
99 other.code_mapping_->GetMapping(),
100 code_mapping_->GetSize()) != 0;
101
102 // library_id_ is intentionally preserved: the scoped registry key
103 // (library_id + entrypoint) must remain stable across reloads so the
104 // eviction triple-call in `RegisterSync` lands at the same slot.
105 entrypoint_ = std::move(other.entrypoint_);
106 stage_ = other.stage_;
107 code_mapping_ = std::move(other.code_mapping_);
108 inputs_ = std::move(other.inputs_);
109 layouts_ = std::move(other.layouts_);
110 uniform_structs_ = std::move(other.uniform_structs_);
111 uniform_textures_ = std::move(other.uniform_textures_);
112 descriptor_set_layouts_ = std::move(other.descriptor_set_layouts_);
113 if (code_changed) {
114 is_dirty_ = true;
115 }
116}

◆ SetClean()

void flutter::gpu::Shader::SetClean ( )

Definition at line 83 of file shader.cc.

83 {
84 is_dirty_ = false;
85}

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