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
 
- 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 screen space bounding rectangle that this contents affects. 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 StencilCoverage GetStencilCoverage (const Entity &entity, const std::optional< Rect > &current_stencil_coverage) const
 Given the current screen space bounding rectangle of the stencil, return the expected stencil coverage after this draw call. This should only be implemented for contents that may write to the stencil 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, const std::string &label="Snapshot") const
 Render this contents to a snapshot, respecting the entity's transform, path, stencil 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 > &stencil_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)
 

Detailed Description

Definition at line 17 of file runtime_effect_contents.h.

Member Function Documentation

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

40  {
41  return false;
42 }

◆ Render()

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

Get or register shader.

Resolve geometry.

Get or create runtime stage pipeline.

Vertex stage uniforms.

Fragment stage uniforms.

Implements impeller::Contents.

Definition at line 44 of file runtime_effect_contents.cc.

46  {
47 // TODO(jonahwilliams): FragmentProgram API is not fully wired up on Android.
48 // Disable until this is complete so that integration tests and benchmarks can
49 // run m3 applications.
50 #ifdef FML_OS_ANDROID
51  return true;
52 #else
53 
54  auto context = renderer.GetContext();
55  auto library = context->GetShaderLibrary();
56 
57  //--------------------------------------------------------------------------
58  /// Get or register shader.
59  ///
60 
61  // TODO(113719): Register the shader function earlier.
62 
63  std::shared_ptr<const ShaderFunction> function = library->GetFunction(
64  runtime_stage_->GetEntrypoint(), ShaderStage::kFragment);
65 
66  if (function && runtime_stage_->IsDirty()) {
67  context->GetPipelineLibrary()->RemovePipelinesWithEntryPoint(function);
68  library->UnregisterFunction(runtime_stage_->GetEntrypoint(),
70 
71  function = nullptr;
72  }
73 
74  if (!function) {
75  std::promise<bool> promise;
76  auto future = promise.get_future();
77 
78  library->RegisterFunction(
79  runtime_stage_->GetEntrypoint(),
80  ToShaderStage(runtime_stage_->GetShaderStage()),
81  runtime_stage_->GetCodeMapping(),
82  fml::MakeCopyable([promise = std::move(promise)](bool result) mutable {
83  promise.set_value(result);
84  }));
85 
86  if (!future.get()) {
87  VALIDATION_LOG << "Failed to build runtime effect (entry point: "
88  << runtime_stage_->GetEntrypoint() << ")";
89  return false;
90  }
91 
92  function = library->GetFunction(runtime_stage_->GetEntrypoint(),
94  if (!function) {
96  << "Failed to fetch runtime effect function immediately after "
97  "registering it (entry point: "
98  << runtime_stage_->GetEntrypoint() << ")";
99  return false;
100  }
101 
102  runtime_stage_->SetClean();
103  }
104 
105  //--------------------------------------------------------------------------
106  /// Resolve geometry.
107  ///
108 
109  auto geometry_result =
110  GetGeometry()->GetPositionBuffer(renderer, entity, pass);
111 
112  //--------------------------------------------------------------------------
113  /// Get or create runtime stage pipeline.
114  ///
115 
116  const auto& caps = context->GetCapabilities();
117  const auto color_attachment_format = caps->GetDefaultColorFormat();
118  const auto stencil_attachment_format = caps->GetDefaultStencilFormat();
119 
120  using VS = RuntimeEffectVertexShader;
121  PipelineDescriptor desc;
122  desc.SetLabel("Runtime Stage");
123  desc.AddStageEntrypoint(
124  library->GetFunction(VS::kEntrypointName, ShaderStage::kVertex));
125  desc.AddStageEntrypoint(library->GetFunction(runtime_stage_->GetEntrypoint(),
127  auto vertex_descriptor = std::make_shared<VertexDescriptor>();
128  vertex_descriptor->SetStageInputs(VS::kAllShaderStageInputs,
129  VS::kInterleavedBufferLayout);
130  desc.SetVertexDescriptor(std::move(vertex_descriptor));
131  desc.SetColorAttachmentDescriptor(
132  0u, {.format = color_attachment_format, .blending_enabled = true});
133 
134  StencilAttachmentDescriptor stencil0;
135  stencil0.stencil_compare = CompareFunction::kEqual;
136  desc.SetStencilAttachmentDescriptors(stencil0);
137  desc.SetStencilPixelFormat(stencil_attachment_format);
138 
139  auto options = OptionsFromPassAndEntity(pass, entity);
140  if (geometry_result.prevent_overdraw) {
141  options.stencil_compare = CompareFunction::kEqual;
142  options.stencil_operation = StencilOperation::kIncrementClamp;
143  }
144  options.primitive_type = geometry_result.type;
145  options.ApplyToPipelineDescriptor(desc);
146 
147  auto pipeline = context->GetPipelineLibrary()->GetPipeline(desc).Get();
148  if (!pipeline) {
149  VALIDATION_LOG << "Failed to get or create runtime effect pipeline.";
150  return false;
151  }
152 
153  Command cmd;
154  DEBUG_COMMAND_INFO(cmd, "RuntimeEffectContents");
155  cmd.pipeline = pipeline;
156  cmd.stencil_reference = entity.GetStencilDepth();
157  cmd.BindVertices(geometry_result.vertex_buffer);
158 
159  //--------------------------------------------------------------------------
160  /// Vertex stage uniforms.
161  ///
162 
163  VS::FrameInfo frame_info;
164  frame_info.mvp = geometry_result.transform;
165  VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
166 
167  //--------------------------------------------------------------------------
168  /// Fragment stage uniforms.
169  ///
170 
171  size_t minimum_sampler_index = 100000000;
172  size_t buffer_index = 0;
173  size_t buffer_offset = 0;
174  for (auto uniform : runtime_stage_->GetUniforms()) {
175  // TODO(113715): Populate this metadata once GLES is able to handle
176  // non-struct uniform names.
177  std::shared_ptr<ShaderMetadata> metadata =
178  std::make_shared<ShaderMetadata>();
179 
180  switch (uniform.type) {
181  case kSampledImage: {
182  // Sampler uniforms are ordered in the IPLR according to their
183  // declaration and the uniform location reflects the correct offset to
184  // be mapped to - except that it may include all proceeding float
185  // uniforms. For example, a float sampler that comes after 4 float
186  // uniforms may have a location of 4. To convert to the actual offset we
187  // need to find the largest location assigned to a float uniform and
188  // then subtract this from all uniform locations. This is more or less
189  // the same operation we previously performed in the shader compiler.
190  minimum_sampler_index =
191  std::min(minimum_sampler_index, uniform.location);
192  break;
193  }
194  case kFloat: {
195  size_t alignment =
196  std::max(uniform.bit_width / 8, DefaultUniformAlignment());
197  auto buffer_view = pass.GetTransientsBuffer().Emplace(
198  uniform_data_->data() + buffer_offset, uniform.GetSize(),
199  alignment);
200 
201  ShaderUniformSlot uniform_slot;
202  uniform_slot.name = uniform.name.c_str();
203  uniform_slot.ext_res_0 = uniform.location;
204  cmd.BindResource(ShaderStage::kFragment, uniform_slot, metadata,
205  buffer_view);
206  buffer_index++;
207  buffer_offset += uniform.GetSize();
208  break;
209  }
210  case kBoolean:
211  case kSignedByte:
212  case kUnsignedByte:
213  case kSignedShort:
214  case kUnsignedShort:
215  case kSignedInt:
216  case kUnsignedInt:
217  case kSignedInt64:
218  case kUnsignedInt64:
219  case kHalfFloat:
220  case kDouble:
221  VALIDATION_LOG << "Unsupported uniform type for " << uniform.name
222  << ".";
223  return true;
224  }
225  }
226 
227  size_t sampler_index = 0;
228  for (auto uniform : runtime_stage_->GetUniforms()) {
229  // TODO(113715): Populate this metadata once GLES is able to handle
230  // non-struct uniform names.
231  ShaderMetadata metadata;
232 
233  switch (uniform.type) {
234  case kSampledImage: {
235  FML_DCHECK(sampler_index < texture_inputs_.size());
236  auto& input = texture_inputs_[sampler_index];
237 
238  auto sampler =
239  context->GetSamplerLibrary()->GetSampler(input.sampler_descriptor);
240 
241  SampledImageSlot image_slot;
242  image_slot.name = uniform.name.c_str();
243  image_slot.texture_index = uniform.location - minimum_sampler_index;
244  image_slot.sampler_index = uniform.location - minimum_sampler_index;
245  cmd.BindResource(ShaderStage::kFragment, image_slot, metadata,
246  input.texture, sampler);
247 
248  sampler_index++;
249  break;
250  }
251  default:
252  continue;
253  }
254  }
255 
256  pass.AddCommand(std::move(cmd));
257 
258  if (geometry_result.prevent_overdraw) {
259  auto restore = ClipRestoreContents();
260  restore.SetRestoreCoverage(GetCoverage(entity));
261  return restore.Render(renderer, entity, pass);
262  }
263  return true;
264 #endif // FML_OS_ANDROID
265 }

References impeller::RenderPass::AddCommand(), impeller::PipelineDescriptor::AddStageEntrypoint(), impeller::Command::BindResource(), impeller::Command::BindVertices(), DEBUG_COMMAND_INFO, impeller::DefaultUniformAlignment(), impeller::HostBuffer::Emplace(), impeller::HostBuffer::EmplaceUniform(), impeller::ShaderUniformSlot::ext_res_0, impeller::ContentContext::GetContext(), impeller::ColorSourceContents::GetCoverage(), impeller::ColorSourceContents::GetGeometry(), impeller::Entity::GetStencilDepth(), impeller::RenderPass::GetTransientsBuffer(), impeller::kBoolean, impeller::kDouble, impeller::kEqual, impeller::kFloat, impeller::kFragment, impeller::kHalfFloat, impeller::kIncrementClamp, impeller::kSampledImage, impeller::kSignedByte, impeller::kSignedInt, impeller::kSignedInt64, impeller::kSignedShort, impeller::kUnsignedByte, impeller::kUnsignedInt, impeller::kUnsignedInt64, impeller::kUnsignedShort, impeller::kVertex, impeller::ShaderUniformSlot::name, impeller::SampledImageSlot::name, impeller::OptionsFromPassAndEntity(), impeller::Command::pipeline, impeller::SampledImageSlot::sampler_index, impeller::PipelineDescriptor::SetColorAttachmentDescriptor(), impeller::PipelineDescriptor::SetLabel(), impeller::PipelineDescriptor::SetStencilAttachmentDescriptors(), impeller::PipelineDescriptor::SetStencilPixelFormat(), impeller::PipelineDescriptor::SetVertexDescriptor(), impeller::StencilAttachmentDescriptor::stencil_compare, impeller::Command::stencil_reference, impeller::SampledImageSlot::texture_index, impeller::ToShaderStage(), and VALIDATION_LOG.

◆ SetRuntimeStage()

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

Definition at line 25 of file runtime_effect_contents.cc.

26  {
27  runtime_stage_ = std::move(runtime_stage);
28 }

◆ SetTextureInputs()

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

Definition at line 35 of file runtime_effect_contents.cc.

36  {
37  texture_inputs_ = std::move(texture_inputs);
38 }

◆ SetUniformData()

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

Definition at line 30 of file runtime_effect_contents.cc.

31  {
32  uniform_data_ = std::move(uniform_data);
33 }

The documentation for this class was generated from the following files:
impeller::kBoolean
@ kBoolean
Definition: runtime_types.h:14
impeller::kFloat
@ kFloat
Definition: runtime_types.h:24
DEBUG_COMMAND_INFO
#define DEBUG_COMMAND_INFO(obj, arg)
Definition: command.h:31
impeller::DefaultUniformAlignment
constexpr size_t DefaultUniformAlignment()
Definition: platform.h:14
impeller::kUnsignedShort
@ kUnsignedShort
Definition: runtime_types.h:18
impeller::kSignedByte
@ kSignedByte
Definition: runtime_types.h:15
impeller::ColorSourceContents::GetGeometry
const std::shared_ptr< Geometry > & GetGeometry() const
Get the geometry that this contents will use to render.
Definition: color_source_contents.cc:21
impeller::kSampledImage
@ kSampledImage
Definition: runtime_types.h:26
impeller::ToShaderStage
constexpr ShaderStage ToShaderStage(RuntimeShaderStage stage)
Definition: shader_types.h:29
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:30
impeller::kSignedInt
@ kSignedInt
Definition: runtime_types.h:19
impeller::kUnsignedByte
@ kUnsignedByte
Definition: runtime_types.h:16
impeller::kUnsignedInt64
@ kUnsignedInt64
Definition: runtime_types.h:22
impeller::ShaderStage::kFragment
@ kFragment
impeller::kHalfFloat
@ kHalfFloat
Definition: runtime_types.h:23
impeller::kUnsignedInt
@ kUnsignedInt
Definition: runtime_types.h:20
impeller::StencilOperation::kIncrementClamp
@ kIncrementClamp
Increment the current stencil value by 1. Clamp it to the maximum.
impeller::kSignedShort
@ kSignedShort
Definition: runtime_types.h:17
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:60
impeller::ColorSourceContents::GetCoverage
std::optional< Rect > GetCoverage(const Entity &entity) const override
Get the screen space bounding rectangle that this contents affects.
Definition: color_source_contents.cc:45
impeller::kDouble
@ kDouble
Definition: runtime_types.h:25
impeller::ShaderStage::kVertex
@ kVertex
impeller::kSignedInt64
@ kSignedInt64
Definition: runtime_types.h:21
impeller::CompareFunction::kEqual
@ kEqual
Comparison test passes if new_value == current_value.