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

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

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

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

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

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:108
impeller::kFloat
@ kFloat
Definition: runtime_types.h:23
impeller::MakeShaderMetadata
static std::shared_ptr< ShaderMetadata > MakeShaderMetadata(const RuntimeUniformDescription &uniform)
Definition: runtime_effect_contents.cc:53
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:128
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:103