| Flutter Impeller |

Classes | Public Member Functions | Static Public Member Functions | List of all members
impeller::RuntimeEffectContents Class Referencefinal

`#include <runtime_effect_contents.h>`

Inheritance diagram for impeller::RuntimeEffectContents:

## 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 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 Matrix & | GetInverseEffectTransform () 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< Rect > | GetCoverage (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< Snapshot > | RenderToSnapshot (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< Size > | GetColorSourceSize () const
| Return the color source's intrinsic size, if available. More...
void | SetColorSourceSize (Size size)
virtual std::optional< Color > | AsBackgroundColor (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, size_t minimum_uniform_alignment)
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< Contents > | MakeAnonymous (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 102 of file runtime_effect_contents.cc.

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

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,
|  | size_t | minimum_uniform_alignment
| ) |  |
| static

Definition at line 33 of file runtime_effect_contents.cc.

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

References 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
| override virtual

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

223 {
224 const std::shared_ptr<Context>& context = renderer.GetContext();
225 const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
226
227 //--------------------------------------------------------------------------
228 /// Get or register shader. Flutter will do this when the runtime effect
229 /// is first loaded, but this check is added to supporting testing of the
230 /// Aiks API and non-flutter usage of Impeller.
231 ///
232 if (!RegisterShader(renderer)) {
233 return false;
234 }
235
236 //--------------------------------------------------------------------------
237 /// Fragment stage uniforms.
238 ///
239 BindFragmentCallback bind_callback = [this, &renderer,
240 &context](RenderPass& pass) {
241 size_t buffer_index = 0;
242 size_t buffer_offset = 0;
243 size_t sampler_location = 0;
244 size_t buffer_location = 0;
245
246 // Uniforms are ordered in the IPLR according to their
247 // declaration and the uniform location reflects the correct offset to
248 // be mapped to - except that it may include all proceeding
249 // uniforms of a different type. For example, a texture sampler that comes
250 // after 4 float uniforms may have a location of 4. Since we know that
251 // the declarations are already ordered, we can track the uniform location
252 // ourselves.
253 auto& data_host_buffer = renderer.GetTransientsDataBuffer();
254 for (const auto& uniform : runtime_stage_->GetUniforms()) {
255 std::unique_ptr<ShaderMetadata> metadata = MakeShaderMetadata(uniform);
256 switch (uniform.type) {
257 case kSampledImage: {
258 FML_DCHECK(sampler_location < texture_inputs_.size());
259 auto& input = texture_inputs_[sampler_location];
260
261 raw_ptr<const Sampler> sampler =
262 context->GetSamplerLibrary()->GetSampler(
263 input.sampler_descriptor);
264
265 SampledImageSlot image_slot;
266 image_slot.name = uniform.name.c_str();
267 image_slot.binding = uniform.binding;
268 image_slot.texture_index = sampler_location;
269 pass.BindDynamicResource(ShaderStage::kFragment,
270 DescriptorType::kSampledImage, image_slot,
271 std::move(metadata), input.texture, sampler);
272 sampler_location++;
273 break;
274 }
275 case kFloat: {
276 FML_DCHECK(renderer.GetContext()->GetBackendType() !=
277 Context::BackendType::kVulkan)
278 << "Uniform " << uniform.name
279 << " had unexpected type kFloat for Vulkan backend.";
280
281 size_t alignment =
282 std::max(uniform.bit_width / 8,
283 data_host_buffer.GetMinimumUniformAlignment());
284 BufferView buffer_view =
285 data_host_buffer.Emplace(uniform_data_->data() + buffer_offset,
286 uniform.GetSize(), alignment);
287
288 ShaderUniformSlot uniform_slot;
289 uniform_slot.name = uniform.name.c_str();
290 uniform_slot.ext_res_0 = buffer_location;
291 pass.BindDynamicResource(ShaderStage::kFragment,
292 DescriptorType::kUniformBuffer, uniform_slot,
293 std::move(metadata), std::move(buffer_view));
294 buffer_index++;
295 buffer_offset += uniform.GetSize();
296 buffer_location++;
297 break;
298 }
299 case kStruct: {
300 FML_DCHECK(renderer.GetContext()->GetBackendType() ==
301 Context::BackendType::kVulkan);
302 ShaderUniformSlot uniform_slot;
303 uniform_slot.binding = uniform.location;
304 uniform_slot.name = uniform.name.c_str();
305
306 pass.BindResource(ShaderStage::kFragment,
307 DescriptorType::kUniformBuffer, uniform_slot,
308 nullptr,
309 EmplaceVulkanUniform(
310 uniform_data_, data_host_buffer, uniform,
311 data_host_buffer.GetMinimumUniformAlignment()));
312 }
313 }
314 }
315
316 return true;
317 };
318
319 /// Now that the descriptor set layouts are known, get the pipeline.
320 using VS = RuntimeEffectVertexShader;
321
322 PipelineBuilderCallback pipeline_callback =
323 [&](ContentContextOptions options) {
324 // Pipeline creation callback for the cache handler to call.
325 return renderer.GetCachedRuntimeEffectPipeline(
326 runtime_stage_->GetEntrypoint(), options, [&]() {
327 return CreatePipeline(renderer, options, /*async=*/false);
328 });
329 };
330
331 return ColorSourceContents::DrawGeometry<VS>(renderer, entity, pass,
332 pipeline_callback,
333 VS::FrameInfo{}, bind_callback);
334 }
buffer_view
BufferView buffer_view
Definition: blit_command_gles.cc:154
impeller::ColorSourceContents::PipelineBuilderCallback
std::function< PipelineRef(ContentContextOptions)> PipelineBuilderCallback
Definition: color_source_contents.h:42
impeller::ColorSourceContents::BindFragmentCallback
std::function< bool(RenderPass &pass)> BindFragmentCallback
Definition: color_source_contents.h:40
impeller::Context::BackendType::kVulkan
@ kVulkan
impeller::RuntimeEffectContents::EmplaceVulkanUniform
static BufferView EmplaceVulkanUniform(const std::shared_ptr< const std::vector< uint8_t >> &input_data, HostBuffer &host_buffer, const RuntimeUniformDescription &uniform, size_t minimum_uniform_alignment)
Definition: runtime_effect_contents.cc:33
impeller::MakeShaderMetadata
static std::unique_ptr< ShaderMetadata > MakeShaderMetadata(const RuntimeUniformDescription &uniform)
Definition: runtime_effect_contents.cc:86
impeller::ShaderStage::kFragment
@ kFragment
impeller::VS
LinePipeline::VertexShader VS
Definition: line_contents.cc:13
impeller::DescriptorType::kUniformBuffer
@ kUniformBuffer
impeller::DescriptorType::kSampledImage
@ kSampledImage
impeller::kSampledImage
@ kSampledImage
Definition: runtime_types.h:26
impeller::kFloat
@ kFloat
Definition: runtime_types.h:25
impeller::kStruct
@ kStruct
Definition: runtime_types.h:27

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

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

## ◆ SetTextureInputs()

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

Definition at line 70 of file runtime_effect_contents.cc.

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

## ◆ SetUniformData()

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

Definition at line 65 of file runtime_effect_contents.cc.

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

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

- impeller/entity/contents/runtime_effect_contents.h
- impeller/entity/contents/runtime_effect_contents.cc