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 GeometryGetGeometry () 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 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 ()
 
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< SnapshotRenderToSnapshot (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< 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 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< ContentsMakeAnonymous (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 71 of file runtime_effect_contents.cc.

72  : 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 165 of file runtime_effect_contents.cc.

166  {
167  if (!RegisterShader(renderer)) {
168  return false;
169  }
170  ContentContextOptions options;
171  options.color_attachment_pixel_format =
172  renderer.GetContext()->GetCapabilities()->GetDefaultColorFormat();
173  CreatePipeline(renderer, options, /*async=*/true);
174  return true;
175 }

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

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

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

76  {
77  return geometry_;
78 }

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

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

References impeller::RenderPass::BindDynamicResource(), impeller::ShaderUniformSlot::binding, impeller::SampledImageSlot::binding, impeller::RenderPass::BindResource(), 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 80 of file runtime_effect_contents.cc.

81  {
82  runtime_stage_ = std::move(runtime_stage);
83 }

◆ SetTextureInputs()

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

Definition at line 90 of file runtime_effect_contents.cc.

91  {
92  texture_inputs_ = std::move(texture_inputs);
93 }

◆ SetUniformData()

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

Definition at line 85 of file runtime_effect_contents.cc.

86  {
87  uniform_data_ = std::move(uniform_data);
88 }

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