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, std::optional< Rect > coverage_limit=std::nullopt, const std::optional< SamplerDescriptor > &sampler_descriptor=std::nullopt, bool msaa_enabled=true, int32_t mip_count=1, std::string_view 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 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 EmplaceVulkanUniform (const std::shared_ptr< const std::vector< uint8_t >> &input_data, HostBuffer &host_buffer, const RuntimeUniformDescription &uniform)
 
- 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::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 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)>
 
- 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 98 of file runtime_effect_contents.cc.

99  {
100  if (!RegisterShader(renderer)) {
101  return false;
102  }
103  ContentContextOptions options;
104  options.color_attachment_pixel_format =
105  renderer.GetContext()->GetCapabilities()->GetDefaultColorFormat();
106  CreatePipeline(renderer, options, /*async=*/true);
107  return true;
108 }

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

◆ EmplaceVulkanUniform()

BufferView impeller::RuntimeEffectContents::EmplaceVulkanUniform ( const std::shared_ptr< const std::vector< uint8_t >> &  input_data,
HostBuffer host_buffer,
const RuntimeUniformDescription uniform 
)
static

Definition at line 33 of file runtime_effect_contents.cc.

36  {
37  // TODO(jonahwilliams): rewrite this to emplace directly into
38  // HostBuffer.
39  std::vector<float> uniform_buffer;
40  uniform_buffer.reserve(uniform.struct_layout.size());
41  size_t uniform_byte_index = 0u;
42  for (char byte_type : uniform.struct_layout) {
43  if (byte_type == kPaddingType) {
44  uniform_buffer.push_back(0.f);
45  } else {
46  FML_DCHECK(byte_type == kFloatType);
47  uniform_buffer.push_back(reinterpret_cast<const float*>(
48  input_data->data())[uniform_byte_index++]);
49  }
50  }
51  size_t alignment = std::max(sizeof(float) * uniform_buffer.size(),
53 
54  return host_buffer.Emplace(
55  reinterpret_cast<const void*>(uniform_buffer.data()),
56  sizeof(float) * uniform_buffer.size(), alignment);
57 }
constexpr size_t DefaultUniformAlignment()
Definition: platform.h:14

References impeller::DefaultUniformAlignment(), impeller::HostBuffer::Emplace(), and impeller::RuntimeUniformDescription::struct_layout.

Referenced by Render(), and 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 216 of file runtime_effect_contents.cc.

218  {
219  const std::shared_ptr<Context>& context = renderer.GetContext();
220  const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
221 
222  //--------------------------------------------------------------------------
223  /// Get or register shader. Flutter will do this when the runtime effect
224  /// is first loaded, but this check is added to supporting testing of the
225  /// Aiks API and non-flutter usage of Impeller.
226  ///
227  if (!RegisterShader(renderer)) {
228  return false;
229  }
230 
231  //--------------------------------------------------------------------------
232  /// Fragment stage uniforms.
233  ///
234  BindFragmentCallback bind_callback = [this, &renderer,
235  &context](RenderPass& pass) {
236  size_t minimum_sampler_index = 100000000;
237  size_t buffer_index = 0;
238  size_t buffer_offset = 0;
239 
240  for (const auto& uniform : runtime_stage_->GetUniforms()) {
241  std::unique_ptr<ShaderMetadata> metadata = MakeShaderMetadata(uniform);
242  switch (uniform.type) {
243  case kSampledImage: {
244  // Sampler uniforms are ordered in the IPLR according to their
245  // declaration and the uniform location reflects the correct offset to
246  // be mapped to - except that it may include all proceeding float
247  // uniforms. For example, a float sampler that comes after 4 float
248  // uniforms may have a location of 4. To convert to the actual offset
249  // we need to find the largest location assigned to a float uniform
250  // and then subtract this from all uniform locations. This is more or
251  // less the same operation we previously performed in the shader
252  // compiler.
253  minimum_sampler_index =
254  std::min(minimum_sampler_index, uniform.location);
255  break;
256  }
257  case kFloat: {
258  FML_DCHECK(renderer.GetContext()->GetBackendType() !=
260  << "Uniform " << uniform.name
261  << " had unexpected type kFloat for Vulkan backend.";
262 
263  size_t alignment =
264  std::max(uniform.bit_width / 8, DefaultUniformAlignment());
265  BufferView buffer_view = renderer.GetTransientsBuffer().Emplace(
266  uniform_data_->data() + buffer_offset, uniform.GetSize(),
267  alignment);
268 
269  ShaderUniformSlot uniform_slot;
270  uniform_slot.name = uniform.name.c_str();
271  uniform_slot.ext_res_0 = uniform.location;
272  pass.BindDynamicResource(ShaderStage::kFragment,
273  DescriptorType::kUniformBuffer, uniform_slot,
274  std::move(metadata), std::move(buffer_view));
275  buffer_index++;
276  buffer_offset += uniform.GetSize();
277  break;
278  }
279  case kStruct: {
280  FML_DCHECK(renderer.GetContext()->GetBackendType() ==
282  ShaderUniformSlot uniform_slot;
283  uniform_slot.binding = uniform.location;
284  uniform_slot.name = uniform.name.c_str();
285 
286  pass.BindResource(
288  uniform_slot, nullptr,
289  EmplaceVulkanUniform(uniform_data_,
290  renderer.GetTransientsBuffer(), uniform));
291  }
292  }
293  }
294 
295  size_t sampler_index = 0;
296  for (const auto& uniform : runtime_stage_->GetUniforms()) {
297  std::unique_ptr<ShaderMetadata> metadata = MakeShaderMetadata(uniform);
298 
299  switch (uniform.type) {
300  case kSampledImage: {
301  FML_DCHECK(sampler_index < texture_inputs_.size());
302  auto& input = texture_inputs_[sampler_index];
303 
304  raw_ptr<const Sampler> sampler =
305  context->GetSamplerLibrary()->GetSampler(
306  input.sampler_descriptor);
307 
308  SampledImageSlot image_slot;
309  image_slot.name = uniform.name.c_str();
310  image_slot.binding = uniform.binding;
311  image_slot.texture_index = uniform.location - minimum_sampler_index;
312  pass.BindDynamicResource(ShaderStage::kFragment,
313  DescriptorType::kSampledImage, image_slot,
314  std::move(metadata), input.texture, sampler);
315 
316  sampler_index++;
317  break;
318  }
319  default:
320  continue;
321  }
322  }
323  return true;
324  };
325 
326  /// Now that the descriptor set layouts are known, get the pipeline.
327  using VS = RuntimeEffectVertexShader;
328 
329  PipelineBuilderCallback pipeline_callback =
330  [&](ContentContextOptions options) {
331  // Pipeline creation callback for the cache handler to call.
332  return renderer.GetCachedRuntimeEffectPipeline(
333  runtime_stage_->GetEntrypoint(), options, [&]() {
334  return CreatePipeline(renderer, options, /*async=*/false);
335  });
336  };
337 
338  return ColorSourceContents::DrawGeometry<VS>(renderer, entity, pass,
339  pipeline_callback,
340  VS::FrameInfo{}, bind_callback);
341 }
BufferView buffer_view
std::function< PipelineRef(ContentContextOptions)> PipelineBuilderCallback
std::function< bool(RenderPass &pass)> BindFragmentCallback
static BufferView EmplaceVulkanUniform(const std::shared_ptr< const std::vector< uint8_t >> &input_data, HostBuffer &host_buffer, const RuntimeUniformDescription &uniform)
GlyphAtlasPipeline::VertexShader VS
static std::unique_ptr< ShaderMetadata > MakeShaderMetadata(const RuntimeUniformDescription &uniform)

References impeller::RenderPass::BindDynamicResource(), impeller::ShaderUniformSlot::binding, impeller::SampledImageSlot::binding, impeller::RenderPass::BindResource(), buffer_view, impeller::DefaultUniformAlignment(), impeller::HostBuffer::Emplace(), EmplaceVulkanUniform(), 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 59 of file runtime_effect_contents.cc.

60  {
61  runtime_stage_ = std::move(runtime_stage);
62 }

◆ SetTextureInputs()

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

Definition at line 69 of file runtime_effect_contents.cc.

70  {
71  texture_inputs_ = std::move(texture_inputs);
72 }

◆ SetUniformData()

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

Definition at line 64 of file runtime_effect_contents.cc.

65  {
66  uniform_data_ = std::move(uniform_data);
67 }

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