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

 RuntimeEffectContents (const Geometry *geometry)
 
 ~RuntimeEffectContents () override
 
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)
 
const Geometry * GetGeometry () const override
 Get the geometry that this contents will use to render. 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 SetEffectTransform (Matrix matrix)
 Set the effect transform for this color source. More...
 
const Matrix & GetInverseEffectTransform () const
 Get 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 ()
 
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 Matrix &transform) 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 std::optional< Snapshot > RenderToSnapshot (const ContentContext &renderer, const Entity &entity, const SnapshotOptions &options) 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...
 
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 bool ApplyColorFilter (const ColorFilterProc &color_filter_proc)
 If possible, applies a color filter to this contents inputs on the CPU. More...
 

Static Public Member Functions

static BufferView EmplaceUniform (const uint8_t *source_data, HostBuffer &host_buffer, const RuntimeUniformDescription &uniform)
 
- Static Public Member Functions inherited from impeller::ColorSourceContents
template<typename VertexShaderT >
static bool DrawGeometry (const Contents *contents, const Geometry *geometry, 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)
 
- Static Public Member Functions inherited from impeller::Contents
static std::shared_ptr< Contents > MakeAnonymous (RenderProc render_proc, CoverageProc coverage_proc)
 

Additional Inherited Members

- Public Types inherited from impeller::ColorSourceContents
using BindFragmentCallback = std::function< bool(RenderPass &pass)>
 
using PipelineBuilderCallback = std::function< PipelineRef(ContentContextOptions)>
 
using CreateGeometryCallback = std::function< GeometryResult(const ContentContext &renderer, const Entity &entity, RenderPass &pass, const Geometry *geom)>
 
- 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)>
 
- Protected Member Functions inherited from impeller::ColorSourceContents
bool AppliesAlphaForStrokeCoverage (const Matrix &transform) const
 Whether the entity should be treated as non-opaque due to stroke geometry requiring alpha for coverage. More...
 
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 18 of file runtime_effect_contents.h.

Constructor & Destructor Documentation

◆ RuntimeEffectContents()

impeller::RuntimeEffectContents::RuntimeEffectContents ( const Geometry *  geometry)
explicit

Definition at line 72 of file runtime_effect_contents.cc.

73  : geometry_(geometry) {}

◆ ~RuntimeEffectContents()

impeller::RuntimeEffectContents::~RuntimeEffectContents ( )
overridedefault

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

150  {
151  if (!RegisterShader(renderer)) {
152  return false;
153  }
154  ContentContextOptions options;
155  options.color_attachment_pixel_format =
156  renderer.GetContext()->GetCapabilities()->GetDefaultColorFormat();
157  CreatePipeline(renderer, options, /*async=*/true);
158  return true;
159 }

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

◆ EmplaceUniform()

BufferView impeller::RuntimeEffectContents::EmplaceUniform ( const uint8_t *  source_data,
HostBuffer &  host_buffer,
const RuntimeUniformDescription &  uniform 
)
static

Copies the uniform data into the host buffer.

If the uniform has a padding_layout, it is used to repack the data.

Parameters
source_dataThe pointer to the start of the uniform data in the source.
host_bufferThe host buffer to emplace the uniform data into.
uniformThe description of the uniform being emplaced.

Definition at line 29 of file runtime_effect_contents.cc.

32  {
33  size_t minimum_uniform_alignment =
34  data_host_buffer.GetMinimumUniformAlignment();
35  size_t alignment = std::max(uniform.bit_width / 8, minimum_uniform_alignment);
36 
37  if (uniform.padding_layout.empty()) {
38  return data_host_buffer.Emplace(source_data, uniform.GetGPUSize(),
39  alignment);
40  }
41 
42  // If the uniform has a padding layout, we need to repack the data.
43  // We can do this by using the EmplaceProc to write directly to the
44  // HostBuffer.
45  return data_host_buffer.Emplace(
46  uniform.GetGPUSize(), alignment,
47  [&uniform, source_data](uint8_t* destination) {
48  size_t count = uniform.array_elements.value_or(1);
49  if (count == 0) {
50  // Make sure to run at least once.
51  count = 1;
52  }
53  size_t uniform_byte_index = 0u;
54  size_t struct_float_index = 0u;
55  auto* float_destination = reinterpret_cast<float*>(destination);
56  auto* float_source = reinterpret_cast<const float*>(source_data);
57 
58  for (size_t i = 0; i < count; i++) {
59  for (RuntimePaddingType byte_type : uniform.padding_layout) {
60  if (byte_type == RuntimePaddingType::kPadding) {
61  float_destination[struct_float_index++] = 0.f;
62  } else {
63  FML_DCHECK(byte_type == RuntimePaddingType::kFloat);
64  float_destination[struct_float_index++] =
65  float_source[uniform_byte_index++];
66  }
67  }
68  }
69  });
70 }

References impeller::RuntimeUniformDescription::bit_width, impeller::HostBuffer::Emplace(), impeller::RuntimeUniformDescription::GetGPUSize(), impeller::HostBuffer::GetMinimumUniformAlignment(), and impeller::RuntimeUniformDescription::padding_layout.

Referenced by Render(), and impeller::testing::TEST_P().

◆ GetGeometry()

const Geometry * impeller::RuntimeEffectContents::GetGeometry ( ) const
overridevirtual

Get the geometry that this contents will use to render.

Implements impeller::ColorSourceContents.

Definition at line 77 of file runtime_effect_contents.cc.

77  {
78  return geometry_;
79 }

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

268  {
269  const std::shared_ptr<Context>& context = renderer.GetContext();
270  const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
271 
272  //--------------------------------------------------------------------------
273  /// Get or register shader. Flutter will do this when the runtime effect
274  /// is first loaded, but this check is added to supporting testing of the
275  /// Aiks API and non-flutter usage of Impeller.
276  ///
277  if (!RegisterShader(renderer)) {
278  return false;
279  }
280 
281  //--------------------------------------------------------------------------
282  /// Fragment stage uniforms.
283  ///
284  BindFragmentCallback bind_callback = [this, &renderer,
285  &context](RenderPass& pass) {
286  size_t buffer_index = 0;
287  size_t buffer_offset = 0;
288  size_t sampler_location = 0;
289  size_t buffer_location = 0;
290 
291  // Uniforms are ordered in the IPLR according to their
292  // declaration and the uniform location reflects the correct offset to
293  // be mapped to - except that it may include all proceeding
294  // uniforms of a different type. For example, a texture sampler that comes
295  // after 4 float uniforms may have a location of 4. Since we know that
296  // the declarations are already ordered, we can track the uniform location
297  // ourselves.
298  auto& data_host_buffer = renderer.GetTransientsDataBuffer();
299  for (const auto& uniform : runtime_stage_->GetUniforms()) {
300  std::unique_ptr<ShaderMetadata> metadata = MakeShaderMetadata(uniform);
301  switch (uniform.type) {
302  case kSampledImage: {
303  FML_DCHECK(sampler_location < texture_inputs_.size());
304  auto& input = texture_inputs_[sampler_location];
305 
306  raw_ptr<const Sampler> sampler =
307  context->GetSamplerLibrary()->GetSampler(
308  input.sampler_descriptor);
309 
310  SampledImageSlot image_slot;
311  image_slot.name = uniform.name.c_str();
312  image_slot.binding = uniform.binding;
313  image_slot.texture_index = sampler_location;
314  pass.BindDynamicResource(ShaderStage::kFragment,
315  DescriptorType::kSampledImage, image_slot,
316  std::move(metadata), input.texture, sampler);
317  sampler_location++;
318  break;
319  }
320  case kFloat: {
321  FML_DCHECK(renderer.GetContext()->GetBackendType() !=
323  << "Uniform " << uniform.name
324  << " had unexpected type kFloat for Vulkan backend.";
325 
326  BufferView buffer_view = EmplaceUniform(
327  uniform_data_->data() + buffer_offset, data_host_buffer, uniform);
328 
329  ShaderUniformSlot uniform_slot;
330  uniform_slot.name = uniform.name.c_str();
331  uniform_slot.ext_res_0 = buffer_location;
332  pass.BindDynamicResource(ShaderStage::kFragment,
333  DescriptorType::kUniformBuffer, uniform_slot,
334  std::move(metadata), std::move(buffer_view));
335  buffer_index++;
336  buffer_offset += uniform.GetDartSize();
337  buffer_location++;
338  break;
339  }
340  case kStruct: {
341  FML_DCHECK(renderer.GetContext()->GetBackendType() ==
343  ShaderUniformSlot uniform_slot;
344  uniform_slot.binding = uniform.location;
345  uniform_slot.name = uniform.name.c_str();
346 
347  pass.BindResource(
349  uniform_slot, nullptr,
350  EmplaceUniform(uniform_data_->data(), data_host_buffer, uniform));
351  }
352  }
353  }
354 
355  return true;
356  };
357 
358  /// Now that the descriptor set layouts are known, get the pipeline.
359  using VS = RuntimeEffectVertexShader;
360 
361  PipelineBuilderCallback pipeline_callback =
362  [&](ContentContextOptions options) {
363  // Pipeline creation callback for the cache handler to call.
364  return renderer.GetCachedRuntimeEffectPipeline(
365  scoped_fragment_name_, options, [&]() {
366  return CreatePipeline(renderer, options, /*async=*/false);
367  });
368  };
369 
370  return ColorSourceContents::DrawGeometry<VS>(renderer, entity, pass,
371  pipeline_callback,
372  VS::FrameInfo{}, bind_callback);
373 }
std::function< PipelineRef(ContentContextOptions)> PipelineBuilderCallback
std::function< bool(RenderPass &pass)> BindFragmentCallback
static BufferView EmplaceUniform(const uint8_t *source_data, HostBuffer &host_buffer, const RuntimeUniformDescription &uniform)
static std::unique_ptr< ShaderMetadata > MakeShaderMetadata(const RuntimeUniformDescription &uniform)
LinePipeline::VertexShader VS
std::shared_ptr< ContextGLES > context

References impeller::RenderPass::BindDynamicResource(), impeller::ShaderUniformSlot::binding, impeller::SampledImageSlot::binding, impeller::RenderPass::BindResource(), context, EmplaceUniform(), impeller::ShaderUniformSlot::ext_res_0, impeller::ContentContext::GetCachedRuntimeEffectPipeline(), impeller::ContentContext::GetContext(), impeller::ContentContext::GetTransientsDataBuffer(), 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 81 of file runtime_effect_contents.cc.

82  {
83  runtime_stage_ = std::move(runtime_stage);
84  // Precompute the scoped registry name now so that the hot per-frame
85  // `Render` path does not re-allocate it on every call. The name is keyed
86  // on the stage's library id and entrypoint, both of which are stable for
87  // a given `runtime_stage_` value, so this stays valid until the next
88  // `SetRuntimeStage`.
89  if (runtime_stage_) {
90  scoped_fragment_name_ = ShaderKey::MakeUserScopedName(
91  ShaderKey::kScopeRuntimeEffect, runtime_stage_->GetLibraryId(),
92  runtime_stage_->GetEntrypoint());
93  } else {
94  scoped_fragment_name_.clear();
95  }
96 }
static std::string MakeUserScopedName(std::string_view scope, std::string_view library_id, std::string_view entrypoint)
Definition: shader_key.cc:19
static constexpr std::string_view kScopeRuntimeEffect
Definition: shader_key.h:56

References impeller::ShaderKey::kScopeRuntimeEffect, and impeller::ShaderKey::MakeUserScopedName().

◆ SetTextureInputs()

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

Definition at line 103 of file runtime_effect_contents.cc.

104  {
105  texture_inputs_ = std::move(texture_inputs);
106 }

◆ SetUniformData()

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

Definition at line 98 of file runtime_effect_contents.cc.

99  {
100  uniform_data_ = std::move(uniform_data);
101 }

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