| Flutter Impeller |

Classes | Public Member Functions | List of all members
impeller::RuntimeEffectContents Class Referencefinal

`#include <runtime_effect_contents.h>`

Inheritance diagram for impeller::RuntimeEffectContents:

## Classes
struct | TextureInput

## Public Member Functions
void | SetRuntimeStage (std::shared_ptr< RuntimeStage > runtime_stage)
void | SetUniformData (std::shared_ptr< std::vector< uint8_t >> uniform_data)
void | SetTextureInputs (std::vector< TextureInput > texture_inputs)
bool | CanInheritOpacity (const Entity &entity) const override
| Whether or not this contents can accept the opacity peephole optimization. More...
bool | Render (const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Public Member Functions inherited from impeller::ColorSourceContents
| ColorSourceContents ()
| ~ColorSourceContents () override
void | SetGeometry (std::shared_ptr< Geometry > geometry)
| Set the geometry that this contents will use to render. More...
const std::shared_ptr< Geometry > & | GetGeometry () const
| Get the geometry that this contents will use to render. More...
void | SetEffectTransform (Matrix matrix)
| Set the effect transform for this color source. More...
const Matrix & | GetInverseEffectTransform () const
| Set the inverted effect transform for this color source. More...
void | SetOpacityFactor (Scalar opacity)
| Set the opacity factor for this color source. More...
Scalar | GetOpacityFactor () const
| Get the opacity factor for this color source. More...
virtual bool | IsSolidColor () const
std::optional< Rect > | GetCoverage (const Entity &entity) const override
| Get the area of the render pass that will be affected when this contents is rendered. More...
void | SetInheritedOpacity (Scalar opacity) override
| Inherit the provided opacity. More...
Public Member Functions inherited from impeller::Contents
| Contents ()
virtual | ~Contents ()
virtual void | PopulateGlyphAtlas (const std::shared_ptr< LazyGlyphAtlas > &lazy_glyph_atlas, Scalar scale)
| Add any text data to the specified lazy atlas. The scale parameter must be used again later when drawing the text. More...
void | SetCoverageHint (std::optional< Rect > coverage_hint)
| Hint that specifies the coverage area of this Contents that will actually be used during rendering. This is for optimization purposes only and can not be relied on as a clip. May optionally affect the result of `GetCoverage()`. More...
const std::optional< Rect > & | GetCoverageHint () const
virtual bool | IsOpaque () const
| Whether this Contents only emits opaque source colors from the fragment stage. This value does not account for any entity properties (e.g. the blend mode), clips/visibility culling, or inherited opacity. More...
virtual ClipCoverage | GetClipCoverage (const Entity &entity, const std::optional< Rect > &current_clip_coverage) const
| Given the current pass space bounding rectangle of the clip buffer, return the expected clip coverage after this draw call. This should only be implemented for contents that may write to the clip buffer. More...
virtual std::optional< Snapshot > | RenderToSnapshot (const ContentContext &renderer, const Entity &entity, std::optional< Rect > coverage_limit=std::nullopt, const std::optional< SamplerDescriptor > &sampler_descriptor=std::nullopt, bool msaa_enabled=true, int32_t mip_count=1, const std::string &label="Snapshot") const
| Render this contents to a snapshot, respecting the entity's transform, path, clip depth, and blend mode. The result texture size is always the size of `GetCoverage(entity)`. More...
virtual bool | ShouldRender (const Entity &entity, const std::optional< Rect > clip_coverage) const
std::optional< Size > | GetColorSourceSize () const
| Return the color source's intrinsic size, if available. More...
void | SetColorSourceSize (Size size)
virtual std::optional< Color > | AsBackgroundColor (const Entity &entity, ISize target_size) const
| Returns a color if this Contents will flood the given `target_size` with a color. This output color is the "Source" color that will be used for the Entity's blend operation. More...
virtual const FilterContents * | AsFilter () const
| Cast to a filter. Returns `nullptr` if this Contents is not a filter. More...
virtual bool | ApplyColorFilter (const ColorFilterProc &color_filter_proc)
| If possible, applies a color filter to this contents inputs on the CPU. More...

## Additional Inherited Members
Public Types inherited from impeller::Contents
using | ColorFilterProc = std::function< Color(Color)>
using | RenderProc = std::function< bool(const ContentContext &renderer, const Entity &entity, RenderPass &pass)>
using | CoverageProc = std::function< std::optional< Rect >(const Entity &entity)>
Static Public Member Functions inherited from impeller::Contents
static std::shared_ptr< Contents > | MakeAnonymous (RenderProc render_proc, CoverageProc coverage_proc)
Protected Types inherited from impeller::ColorSourceContents
using | BindFragmentCallback = std::function< bool(RenderPass &pass)>
using | PipelineBuilderMethod = std::shared_ptr< Pipeline< PipelineDescriptor > >(impeller::ContentContext::*)(ContentContextOptions) const
using | PipelineBuilderCallback = std::function< std::shared_ptr< Pipeline< PipelineDescriptor > >(ContentContextOptions)>
Protected Member Functions inherited from impeller::ColorSourceContents
template<typename VertexShaderT >
bool | DrawGeometry (const ContentContext &renderer, const Entity &entity, RenderPass &pass, const PipelineBuilderCallback &pipeline_callback, typename VertexShaderT::FrameInfo frame_info, const BindFragmentCallback &bind_fragment_callback, bool enable_uvs=false, Rect texture_coverage={}, const Matrix &effect_transform={}) const

## Detailed Description

Definition at line 17 of file runtime_effect_contents.h.

## Member Function Documentation

## ◆ CanInheritOpacity()

bool impeller::RuntimeEffectContents::CanInheritOpacity | ( | const Entity & | entity | ) | const
| override virtual

Whether or not this contents can accept the opacity peephole optimization.

By default all contents return false. Contents are responsible for determining whether or not their own geometries intersect in a way that makes accepting opacity impossible. It is always safe to return false, especially if computing overlap would be computationally expensive.

Reimplemented from impeller::ColorSourceContents.

Definition at line 40 of file runtime_effect_contents.cc.

40 {
41 return false;
42 }

## ◆ Render()

bool impeller::RuntimeEffectContents::Render | ( | const ContentContext & | renderer,
|  | const Entity & | entity,
|  | RenderPass & | pass
| ) |  | const
| override virtual

Get or register shader.

Resolve runtime stage function.

Set up the command. Defer setting up the pipeline until the descriptor set layouts are known from the uniforms.

Fragment stage uniforms.

Now that the descriptor set layouts are known, get the pipeline.

Implements impeller::Contents.

Definition at line 68 of file runtime_effect_contents.cc.

70 {
71 const std::shared_ptr<Context>& context = renderer.GetContext();
72 const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
73
74 //--------------------------------------------------------------------------
75 /// Get or register shader.
76 ///
77
78 // TODO(113719): Register the shader function earlier.
79
80 std::shared_ptr<const ShaderFunction> function = library->GetFunction(
81 runtime_stage_->GetEntrypoint(), ShaderStage::kFragment);
82
83 //--------------------------------------------------------------------------
84 /// Resolve runtime stage function.
85 ///
86
87 if (function && runtime_stage_->IsDirty()) {
88 renderer.ClearCachedRuntimeEffectPipeline(runtime_stage_->GetEntrypoint());
89 context->GetPipelineLibrary()->RemovePipelinesWithEntryPoint(function);
90 library->UnregisterFunction(runtime_stage_->GetEntrypoint(),
91 ShaderStage::kFragment);
92
93 function = nullptr;
94 }
95
96 if (!function) {
97 std::promise<bool> promise;
98 auto future = promise.get_future();
99
100 library->RegisterFunction(
101 runtime_stage_->GetEntrypoint(),
102 ToShaderStage(runtime_stage_->GetShaderStage()),
103 runtime_stage_->GetCodeMapping(),
104 fml::MakeCopyable([promise = std::move(promise)](bool result) mutable {
105 promise.set_value(result);
106 }));
107
108 if (!future.get()) {
109 VALIDATION_LOG << "Failed to build runtime effect (entry point: "
110 << runtime_stage_->GetEntrypoint() << ")";
111 return false;
112 }
113
114 function = library->GetFunction(runtime_stage_->GetEntrypoint(),
115 ShaderStage::kFragment);
116 if (!function) {
117 VALIDATION_LOG
118 << "Failed to fetch runtime effect function immediately after "
119 "registering it (entry point: "
120 << runtime_stage_->GetEntrypoint() << ")";
121 return false;
122 }
123
124 runtime_stage_->SetClean();
125 }
126
127 //--------------------------------------------------------------------------
128 /// Set up the command. Defer setting up the pipeline until the descriptor set
129 /// layouts are known from the uniforms.
130 ///
131
132 const std::shared_ptr<const Capabilities>& caps = context->GetCapabilities();
133 const auto color_attachment_format = caps->GetDefaultColorFormat();
134 const auto stencil_attachment_format = caps->GetDefaultDepthStencilFormat();
135
136 using VS = RuntimeEffectVertexShader;
137
138 //--------------------------------------------------------------------------
139 /// Fragment stage uniforms.
140 ///
141
142 std::vector<DescriptorSetLayout> descriptor_set_layouts;
143
144 BindFragmentCallback bind_callback = [this, &renderer, &context,
145 &descriptor_set_layouts](
146 RenderPass& pass) {
147 descriptor_set_layouts.clear();
148
149 size_t minimum_sampler_index = 100000000;
150 size_t buffer_index = 0;
151 size_t buffer_offset = 0;
152
153 for (const auto& uniform : runtime_stage_->GetUniforms()) {
154 std::shared_ptr<ShaderMetadata> metadata = MakeShaderMetadata(uniform);
155
156 switch (uniform.type) {
157 case kSampledImage: {
158 // Sampler uniforms are ordered in the IPLR according to their
159 // declaration and the uniform location reflects the correct offset to
160 // be mapped to - except that it may include all proceeding float
161 // uniforms. For example, a float sampler that comes after 4 float
162 // uniforms may have a location of 4. To convert to the actual offset
163 // we need to find the largest location assigned to a float uniform
164 // and then subtract this from all uniform locations. This is more or
165 // less the same operation we previously performed in the shader
166 // compiler.
167 minimum_sampler_index =
168 std::min(minimum_sampler_index, uniform.location);
169 break;
170 }
171 case kFloat: {
172 FML_DCHECK(renderer.GetContext()->GetBackendType() !=
173 Context::BackendType::kVulkan)
174 << "Uniform " << uniform.name
175 << " had unexpected type kFloat for Vulkan backend.";
176 size_t alignment =
177 std::max(uniform.bit_width / 8, DefaultUniformAlignment());
178 auto buffer_view = renderer.GetTransientsBuffer().Emplace(
179 uniform_data_->data() + buffer_offset, uniform.GetSize(),
180 alignment);
181
182 ShaderUniformSlot uniform_slot;
183 uniform_slot.name = uniform.name.c_str();
184 uniform_slot.ext_res_0 = uniform.location;
185 pass.BindResource(ShaderStage::kFragment,
186 DescriptorType::kUniformBuffer, uniform_slot,
187 metadata, buffer_view);
188 buffer_index++;
189 buffer_offset += uniform.GetSize();
190 break;
191 }
192 case kStruct: {
193 FML_DCHECK(renderer.GetContext()->GetBackendType() ==
194 Context::BackendType::kVulkan);
195 descriptor_set_layouts.emplace_back(DescriptorSetLayout{
196 static_cast<uint32_t>(uniform.location),
197 DescriptorType::kUniformBuffer,
198 ShaderStage::kFragment,
199 });
200 ShaderUniformSlot uniform_slot;
201 uniform_slot.name = uniform.name.c_str();
202 uniform_slot.binding = uniform.location;
203
204 std::vector<float> uniform_buffer;
205 uniform_buffer.reserve(uniform.struct_layout.size());
206 size_t uniform_byte_index = 0u;
207 for (const auto& byte_type : uniform.struct_layout) {
208 if (byte_type == 0) {
209 uniform_buffer.push_back(0.f);
210 } else if (byte_type == 1) {
211 uniform_buffer.push_back(reinterpret_cast<float*>(
212 uniform_data_->data())[uniform_byte_index++]);
213 } else {
214 FML_UNREACHABLE();
215 }
216 }
217
218 size_t alignment = std::max(sizeof(float) * uniform_buffer.size(),
219 DefaultUniformAlignment());
220
221 auto buffer_view = renderer.GetTransientsBuffer().Emplace(
222 reinterpret_cast<const void*>(uniform_buffer.data()),
223 sizeof(float) * uniform_buffer.size(), alignment);
224 pass.BindResource(ShaderStage::kFragment,
225 DescriptorType::kUniformBuffer, uniform_slot,
226 ShaderMetadata{}, buffer_view);
227 }
228 }
229 }
230
231 size_t sampler_index = 0;
232 for (const auto& uniform : runtime_stage_->GetUniforms()) {
233 std::shared_ptr<ShaderMetadata> metadata = MakeShaderMetadata(uniform);
234
235 switch (uniform.type) {
236 case kSampledImage: {
237 FML_DCHECK(sampler_index < texture_inputs_.size());
238 auto& input = texture_inputs_[sampler_index];
239
240 const std::unique_ptr<const Sampler>& sampler =
241 context->GetSamplerLibrary()->GetSampler(
242 input.sampler_descriptor);
243
244 SampledImageSlot image_slot;
245 image_slot.name = uniform.name.c_str();
246
247 uint32_t sampler_binding_location = 0u;
248 if (!descriptor_set_layouts.empty()) {
249 sampler_binding_location =
250 descriptor_set_layouts.back().binding + 1;
251 }
252
253 descriptor_set_layouts.emplace_back(DescriptorSetLayout{
254 sampler_binding_location,
255 DescriptorType::kSampledImage,
256 ShaderStage::kFragment,
257 });
258
259 image_slot.binding = sampler_binding_location;
260 image_slot.texture_index = uniform.location - minimum_sampler_index;
261 pass.BindResource(ShaderStage::kFragment,
262 DescriptorType::kSampledImage, image_slot,
263 *metadata, input.texture, sampler);
264
265 sampler_index++;
266 break;
267 }
268 default:
269 continue;
270 }
271 }
272 return true;
273 };
274
275 /// Now that the descriptor set layouts are known, get the pipeline.
276
277 PipelineBuilderCallback pipeline_callback = [&](ContentContextOptions
278 options) {
279 // Pipeline creation callback for the cache handler to call.
280 auto create_callback =
281 [&]() -> std::shared_ptr<Pipeline<PipelineDescriptor>> {
282 PipelineDescriptor desc;
283 desc.SetLabel("Runtime Stage");
284 desc.AddStageEntrypoint(
285 library->GetFunction(VS::kEntrypointName, ShaderStage::kVertex));
286 desc.AddStageEntrypoint(library->GetFunction(
287 runtime_stage_->GetEntrypoint(), ShaderStage::kFragment));
288 auto vertex_descriptor = std::make_shared<VertexDescriptor>();
289 vertex_descriptor->SetStageInputs(VS::kAllShaderStageInputs,
290 VS::kInterleavedBufferLayout);
291 vertex_descriptor->RegisterDescriptorSetLayouts(
292 VS::kDescriptorSetLayouts);
293 vertex_descriptor->RegisterDescriptorSetLayouts(
294 descriptor_set_layouts.data(), descriptor_set_layouts.size());
295 desc.SetVertexDescriptor(std::move(vertex_descriptor));
296 desc.SetColorAttachmentDescriptor(
297 0u, {.format = color_attachment_format, .blending_enabled = true});
298
299 desc.SetStencilAttachmentDescriptors(StencilAttachmentDescriptor{});
300 desc.SetStencilPixelFormat(stencil_attachment_format);
301
302 desc.SetDepthStencilAttachmentDescriptor(DepthAttachmentDescriptor{});
303 desc.SetDepthPixelFormat(stencil_attachment_format);
304
305 options.ApplyToPipelineDescriptor(desc);
306 auto pipeline = context->GetPipelineLibrary()->GetPipeline(desc).Get();
307 if (!pipeline) {
308 VALIDATION_LOG << "Failed to get or create runtime effect pipeline.";
309 return nullptr;
310 }
311
312 return pipeline;
313 };
314 return renderer.GetCachedRuntimeEffectPipeline(
315 runtime_stage_->GetEntrypoint(), options, create_callback);
316 };
317
318 return ColorSourceContents::DrawGeometry<VS>(renderer, entity, pass,
319 pipeline_callback,
320 VS::FrameInfo{}, bind_callback);
321 }

References impeller::PipelineDescriptor::AddStageEntrypoint(), impeller::ShaderUniformSlot::binding, impeller::SampledImageSlot::binding, impeller::RenderPass::BindResource(), impeller::ContentContext::ClearCachedRuntimeEffectPipeline(), impeller::DefaultUniformAlignment(), impeller::HostBuffer::Emplace(), impeller::ShaderUniformSlot::ext_res_0, impeller::ContentContext::GetCachedRuntimeEffectPipeline(), impeller::ContentContext::GetContext(), impeller::ContentContext::GetTransientsBuffer(), impeller::kFloat, impeller::kFragment, impeller::kSampledImage, impeller::kStruct, impeller::kUniformBuffer, impeller::kVertex, impeller::Context::kVulkan, impeller::MakeShaderMetadata(), impeller::ShaderUniformSlot::name, impeller::SampledImageSlot::name, impeller::PipelineDescriptor::SetColorAttachmentDescriptor(), impeller::PipelineDescriptor::SetDepthPixelFormat(), impeller::PipelineDescriptor::SetDepthStencilAttachmentDescriptor(), impeller::PipelineDescriptor::SetLabel(), impeller::PipelineDescriptor::SetStencilAttachmentDescriptors(), impeller::PipelineDescriptor::SetStencilPixelFormat(), impeller::PipelineDescriptor::SetVertexDescriptor(), impeller::SampledImageSlot::texture_index, impeller::ToShaderStage(), and VALIDATION_LOG.

## ◆ SetRuntimeStage()

void impeller::RuntimeEffectContents::SetRuntimeStage | ( | std::shared_ptr< RuntimeStage > | runtime_stage | ) |

Definition at line 25 of file runtime_effect_contents.cc.

26 {
27 runtime_stage_ = std::move(runtime_stage);
28 }

## ◆ SetTextureInputs()

void impeller::RuntimeEffectContents::SetTextureInputs | ( | std::vector< TextureInput > | texture_inputs | ) |

Definition at line 35 of file runtime_effect_contents.cc.

36 {
37 texture_inputs_ = std::move(texture_inputs);
38 }

## ◆ SetUniformData()

void impeller::RuntimeEffectContents::SetUniformData | ( | std::shared_ptr< std::vector< uint8_t >> | uniform_data | ) |

Definition at line 30 of file runtime_effect_contents.cc.

31 {
32 uniform_data_ = std::move(uniform_data);
33 }

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

- impeller/entity/contents/runtime_effect_contents.h
- impeller/entity/contents/runtime_effect_contents.cc

impeller::ColorSourceContents::PipelineBuilderCallback
std::function< std::shared_ptr< Pipeline< PipelineDescriptor > >(ContentContextOptions)> PipelineBuilderCallback
Definition: color_source_contents.h:113
impeller::kFloat
@ kFloat
Definition: runtime_types.h:23
impeller::MakeShaderMetadata
static std::shared_ptr< ShaderMetadata > MakeShaderMetadata(const RuntimeUniformDescription &uniform)
Definition: runtime_effect_contents.cc:55
impeller::DefaultUniformAlignment
constexpr size_t DefaultUniformAlignment()
Definition: platform.h:15
impeller::kSampledImage
@ kSampledImage
Definition: runtime_types.h:24
impeller::ToShaderStage
constexpr ShaderStage ToShaderStage(RuntimeShaderStage stage)
Definition: shader_types.h:29
impeller::VS
SolidFillVertexShader VS
Definition: stroke_path_geometry.cc:15
impeller::ShaderStage::kFragment
@ kFragment
impeller::kStruct
@ kStruct
Definition: runtime_types.h:25
impeller::DescriptorType::kSampledImage
@ kSampledImage
impeller::DescriptorType::kUniformBuffer
@ kUniformBuffer
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:73
impeller::Context::BackendType::kVulkan
@ kVulkan
impeller::ColorSourceContents::BindFragmentCallback
std::function< bool(RenderPass &pass)> BindFragmentCallback
Definition: color_source_contents.h:108
impeller::ShaderStage::kVertex
@ kVertex