Flutter Impeller
impeller::RuntimeEffectContents Class Referencefinal

#include <runtime_effect_contents.h>

Inheritance diagram for impeller::RuntimeEffectContents:
impeller::ColorSourceContents impeller::Contents

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
 
bool BootstrapShader (const ContentContext &renderer) const
 Load the runtime effect and ensure a default PSO is initialized. More...
 
- 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 MatrixGetInverseEffectTransform () 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< RectGetCoverage (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< SnapshotRenderToSnapshot (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< SizeGetColorSourceSize () const
 Return the color source's intrinsic size, if available. More...
 
void SetColorSourceSize (Size size)
 
virtual std::optional< ColorAsBackgroundColor (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 FilterContentsAsFilter () 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< ContentsMakeAnonymous (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)>
 
using CreateGeometryCallback = std::function< GeometryResult(const ContentContext &renderer, const Entity &entity, RenderPass &pass, const Geometry &geom)>
 
- 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 force_stencil=false, const CreateGeometryCallback &create_geom_callback=DefaultCreateGeometryCallback) const
 
- Static Protected Member Functions inherited from impeller::ColorSourceContents
static GeometryResult DefaultCreateGeometryCallback (const ContentContext &renderer, const Entity &entity, RenderPass &pass, const Geometry &geom)
 

Detailed Description

Definition at line 17 of file runtime_effect_contents.h.

Member Function Documentation

◆ BootstrapShader()

bool impeller::RuntimeEffectContents::BootstrapShader ( const ContentContext renderer) const

Load the runtime effect and ensure a default PSO is initialized.

Definition at line 70 of file runtime_effect_contents.cc.

71  {
72  if (!RegisterShader(renderer)) {
73  return false;
74  }
75  ContentContextOptions options;
76  options.color_attachment_pixel_format =
77  renderer.GetContext()->GetCapabilities()->GetDefaultColorFormat();
78  CreatePipeline(renderer, options, /*async=*/true);
79  return true;
80 }

References impeller::ContentContextOptions::color_attachment_pixel_format, and impeller::ContentContext::GetContext().

◆ CanInheritOpacity()

bool impeller::RuntimeEffectContents::CanInheritOpacity ( const Entity entity) const
overridevirtual

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 42 of file runtime_effect_contents.cc.

42  {
43  return false;
44 }

◆ Render()

bool impeller::RuntimeEffectContents::Render ( const ContentContext renderer,
const Entity entity,
RenderPass pass 
) const
overridevirtual

Get or register shader. Flutter will do this when the runtime effect is first loaded, but this check is added to supporting testing of the Aiks API and non-flutter usage of Impeller.

Fragment stage uniforms.

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

Implements impeller::Contents.

Definition at line 188 of file runtime_effect_contents.cc.

190  {
191  const std::shared_ptr<Context>& context = renderer.GetContext();
192  const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
193 
194  //--------------------------------------------------------------------------
195  /// Get or register shader. Flutter will do this when the runtime effect
196  /// is first loaded, but this check is added to supporting testing of the
197  /// Aiks API and non-flutter usage of Impeller.
198  ///
199  if (!RegisterShader(renderer)) {
200  return false;
201  }
202 
203  //--------------------------------------------------------------------------
204  /// Fragment stage uniforms.
205  ///
206  BindFragmentCallback bind_callback = [this, &renderer,
207  &context](RenderPass& pass) {
208  size_t minimum_sampler_index = 100000000;
209  size_t buffer_index = 0;
210  size_t buffer_offset = 0;
211 
212  for (const auto& uniform : runtime_stage_->GetUniforms()) {
213  std::shared_ptr<ShaderMetadata> metadata = MakeShaderMetadata(uniform);
214 
215  switch (uniform.type) {
216  case kSampledImage: {
217  // Sampler uniforms are ordered in the IPLR according to their
218  // declaration and the uniform location reflects the correct offset to
219  // be mapped to - except that it may include all proceeding float
220  // uniforms. For example, a float sampler that comes after 4 float
221  // uniforms may have a location of 4. To convert to the actual offset
222  // we need to find the largest location assigned to a float uniform
223  // and then subtract this from all uniform locations. This is more or
224  // less the same operation we previously performed in the shader
225  // compiler.
226  minimum_sampler_index =
227  std::min(minimum_sampler_index, uniform.location);
228  break;
229  }
230  case kFloat: {
231  FML_DCHECK(renderer.GetContext()->GetBackendType() !=
233  << "Uniform " << uniform.name
234  << " had unexpected type kFloat for Vulkan backend.";
235 
236  size_t alignment =
237  std::max(uniform.bit_width / 8, DefaultUniformAlignment());
238  BufferView buffer_view = renderer.GetTransientsBuffer().Emplace(
239  uniform_data_->data() + buffer_offset, uniform.GetSize(),
240  alignment);
241 
242  ShaderUniformSlot uniform_slot;
243  uniform_slot.name = uniform.name.c_str();
244  uniform_slot.ext_res_0 = uniform.location;
245  pass.BindResource(ShaderStage::kFragment,
246  DescriptorType::kUniformBuffer, uniform_slot,
247  metadata, std::move(buffer_view));
248  buffer_index++;
249  buffer_offset += uniform.GetSize();
250  break;
251  }
252  case kStruct: {
253  FML_DCHECK(renderer.GetContext()->GetBackendType() ==
255  ShaderUniformSlot uniform_slot;
256  uniform_slot.name = uniform.name.c_str();
257  uniform_slot.binding = uniform.location;
258 
259  // TODO(jonahwilliams): rewrite this to emplace directly into
260  // HostBuffer.
261  std::vector<float> uniform_buffer;
262  uniform_buffer.reserve(uniform.struct_layout.size());
263  size_t uniform_byte_index = 0u;
264  for (const auto& byte_type : uniform.struct_layout) {
265  if (byte_type == 0) {
266  uniform_buffer.push_back(0.f);
267  } else if (byte_type == 1) {
268  uniform_buffer.push_back(reinterpret_cast<float*>(
269  uniform_data_->data())[uniform_byte_index++]);
270  } else {
271  FML_UNREACHABLE();
272  }
273  }
274  size_t alignment = std::max(sizeof(float) * uniform_buffer.size(),
276 
277  BufferView buffer_view = renderer.GetTransientsBuffer().Emplace(
278  reinterpret_cast<const void*>(uniform_buffer.data()),
279  sizeof(float) * uniform_buffer.size(), alignment);
280  pass.BindResource(ShaderStage::kFragment,
281  DescriptorType::kUniformBuffer, uniform_slot,
282  ShaderMetadata{}, std::move(buffer_view));
283  }
284  }
285  }
286 
287  size_t sampler_index = 0;
288  for (const auto& uniform : runtime_stage_->GetUniforms()) {
289  std::shared_ptr<ShaderMetadata> metadata = MakeShaderMetadata(uniform);
290 
291  switch (uniform.type) {
292  case kSampledImage: {
293  FML_DCHECK(sampler_index < texture_inputs_.size());
294  auto& input = texture_inputs_[sampler_index];
295 
296  const std::unique_ptr<const Sampler>& sampler =
297  context->GetSamplerLibrary()->GetSampler(
298  input.sampler_descriptor);
299 
300  SampledImageSlot image_slot;
301  image_slot.name = uniform.name.c_str();
302  image_slot.binding = uniform.binding;
303  image_slot.texture_index = uniform.location - minimum_sampler_index;
304  pass.BindResource(ShaderStage::kFragment,
305  DescriptorType::kSampledImage, image_slot,
306  *metadata, input.texture, sampler);
307 
308  sampler_index++;
309  break;
310  }
311  default:
312  continue;
313  }
314  }
315  return true;
316  };
317 
318  /// Now that the descriptor set layouts are known, get the pipeline.
319  using VS = RuntimeEffectVertexShader;
320 
321  PipelineBuilderCallback pipeline_callback =
322  [&](ContentContextOptions options) {
323  // Pipeline creation callback for the cache handler to call.
324  return renderer.GetCachedRuntimeEffectPipeline(
325  runtime_stage_->GetEntrypoint(), options, [&]() {
326  return CreatePipeline(renderer, options, /*async=*/false);
327  });
328  };
329 
330  return ColorSourceContents::DrawGeometry<VS>(renderer, entity, pass,
331  pipeline_callback,
332  VS::FrameInfo{}, bind_callback);
333 }

References impeller::ShaderUniformSlot::binding, impeller::SampledImageSlot::binding, impeller::RenderPass::BindResource(), buffer_view, 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::Context::kVulkan, impeller::MakeShaderMetadata(), impeller::ShaderUniformSlot::name, impeller::SampledImageSlot::name, and impeller::SampledImageSlot::texture_index.

◆ SetRuntimeStage()

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

Definition at line 27 of file runtime_effect_contents.cc.

28  {
29  runtime_stage_ = std::move(runtime_stage);
30 }

◆ SetTextureInputs()

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

Definition at line 37 of file runtime_effect_contents.cc.

38  {
39  texture_inputs_ = std::move(texture_inputs);
40 }

◆ SetUniformData()

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

Definition at line 32 of file runtime_effect_contents.cc.

33  {
34  uniform_data_ = std::move(uniform_data);
35 }

The documentation for this class was generated from the following files:
impeller::ColorSourceContents::PipelineBuilderCallback
std::function< std::shared_ptr< Pipeline< PipelineDescriptor > >(ContentContextOptions)> PipelineBuilderCallback
Definition: color_source_contents.h:114
impeller::kFloat
@ kFloat
Definition: runtime_types.h:23
impeller::MakeShaderMetadata
static std::shared_ptr< ShaderMetadata > MakeShaderMetadata(const RuntimeUniformDescription &uniform)
Definition: runtime_effect_contents.cc:57
impeller::DefaultUniformAlignment
constexpr size_t DefaultUniformAlignment()
Definition: platform.h:14
impeller::kSampledImage
@ kSampledImage
Definition: runtime_types.h:24
buffer_view
BufferView buffer_view
Definition: blit_command_gles.cc:127
impeller::VS
SolidFillVertexShader VS
Definition: stroke_path_geometry.cc:16
impeller::ShaderStage::kFragment
@ kFragment
impeller::kStruct
@ kStruct
Definition: runtime_types.h:25
impeller::DescriptorType::kSampledImage
@ kSampledImage
impeller::DescriptorType::kUniformBuffer
@ kUniformBuffer
impeller::Context::BackendType::kVulkan
@ kVulkan
impeller::ColorSourceContents::BindFragmentCallback
std::function< bool(RenderPass &pass)> BindFragmentCallback
Definition: color_source_contents.h:109