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 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 (const Geometry *geometry)
 Set the geometry that this contents will use to render. More...
 
const GeometryGetGeometry () 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 ()
 
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.

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

122  {
123  if (!RegisterShader(renderer)) {
124  return false;
125  }
126  ContentContextOptions options;
127  options.color_attachment_pixel_format =
128  renderer.GetContext()->GetCapabilities()->GetDefaultColorFormat();
129  CreatePipeline(renderer, options, /*async=*/true);
130  return true;
131 }

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 impeller::testing::TEST_P().

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

242  {
243  const std::shared_ptr<Context>& context = renderer.GetContext();
244  const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
245 
246  //--------------------------------------------------------------------------
247  /// Get or register shader. Flutter will do this when the runtime effect
248  /// is first loaded, but this check is added to supporting testing of the
249  /// Aiks API and non-flutter usage of Impeller.
250  ///
251  if (!RegisterShader(renderer)) {
252  return false;
253  }
254 
255  //--------------------------------------------------------------------------
256  /// Fragment stage uniforms.
257  ///
258  BindFragmentCallback bind_callback = [this, &renderer,
259  &context](RenderPass& pass) {
260  size_t buffer_index = 0;
261  size_t buffer_offset = 0;
262  size_t sampler_location = 0;
263  size_t buffer_location = 0;
264 
265  // Uniforms are ordered in the IPLR according to their
266  // declaration and the uniform location reflects the correct offset to
267  // be mapped to - except that it may include all proceeding
268  // uniforms of a different type. For example, a texture sampler that comes
269  // after 4 float uniforms may have a location of 4. Since we know that
270  // the declarations are already ordered, we can track the uniform location
271  // ourselves.
272  auto& data_host_buffer = renderer.GetTransientsDataBuffer();
273  for (const auto& uniform : runtime_stage_->GetUniforms()) {
274  std::unique_ptr<ShaderMetadata> metadata = MakeShaderMetadata(uniform);
275  switch (uniform.type) {
276  case kSampledImage: {
277  FML_DCHECK(sampler_location < texture_inputs_.size());
278  auto& input = texture_inputs_[sampler_location];
279 
280  raw_ptr<const Sampler> sampler =
281  context->GetSamplerLibrary()->GetSampler(
282  input.sampler_descriptor);
283 
284  SampledImageSlot image_slot;
285  image_slot.name = uniform.name.c_str();
286  image_slot.binding = uniform.binding;
287  image_slot.texture_index = sampler_location;
288  pass.BindDynamicResource(ShaderStage::kFragment,
289  DescriptorType::kSampledImage, image_slot,
290  std::move(metadata), input.texture, sampler);
291  sampler_location++;
292  break;
293  }
294  case kFloat: {
295  FML_DCHECK(renderer.GetContext()->GetBackendType() !=
297  << "Uniform " << uniform.name
298  << " had unexpected type kFloat for Vulkan backend.";
299 
300  BufferView buffer_view = EmplaceUniform(
301  uniform_data_->data() + buffer_offset, data_host_buffer, uniform);
302 
303  ShaderUniformSlot uniform_slot;
304  uniform_slot.name = uniform.name.c_str();
305  uniform_slot.ext_res_0 = buffer_location;
306  pass.BindDynamicResource(ShaderStage::kFragment,
307  DescriptorType::kUniformBuffer, uniform_slot,
308  std::move(metadata), std::move(buffer_view));
309  buffer_index++;
310  buffer_offset += uniform.GetDartSize();
311  buffer_location++;
312  break;
313  }
314  case kStruct: {
315  FML_DCHECK(renderer.GetContext()->GetBackendType() ==
317  ShaderUniformSlot uniform_slot;
318  uniform_slot.binding = uniform.location;
319  uniform_slot.name = uniform.name.c_str();
320 
321  pass.BindResource(
323  uniform_slot, nullptr,
324  EmplaceUniform(uniform_data_->data(), data_host_buffer, uniform));
325  }
326  }
327  }
328 
329  return true;
330  };
331 
332  /// Now that the descriptor set layouts are known, get the pipeline.
333  using VS = RuntimeEffectVertexShader;
334 
335  PipelineBuilderCallback pipeline_callback =
336  [&](ContentContextOptions options) {
337  // Pipeline creation callback for the cache handler to call.
338  return renderer.GetCachedRuntimeEffectPipeline(
339  runtime_stage_->GetEntrypoint(), options, [&]() {
340  return CreatePipeline(renderer, options, /*async=*/false);
341  });
342  };
343 
344  return ColorSourceContents::DrawGeometry<VS>(renderer, entity, pass,
345  pipeline_callback,
346  VS::FrameInfo{}, bind_callback);
347 }
BufferView buffer_view
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(), buffer_view, impeller::ShaderUniformSlot::ext_res_0, impeller::ContentContext::GetCachedRuntimeEffectPipeline(), impeller::ContentContext::GetContext(), impeller::ContentContext::GetTransientsDataBuffer(), impeller::kFloat, impeller::kSampledImage, impeller::kStruct, 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 71 of file runtime_effect_contents.cc.

72  {
73  runtime_stage_ = std::move(runtime_stage);
74 }

◆ SetTextureInputs()

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

Definition at line 81 of file runtime_effect_contents.cc.

82  {
83  texture_inputs_ = std::move(texture_inputs);
84 }

◆ SetUniformData()

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

Definition at line 76 of file runtime_effect_contents.cc.

77  {
78  uniform_data_ = std::move(uniform_data);
79 }

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