| Flutter Impeller |

atlas_contents.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <optional>
6
7 #include "impeller/core/formats.h"
8 #include "impeller/entity/contents/atlas_contents.h"
9 #include "impeller/entity/contents/content_context.h"
10 #include "impeller/entity/contents/filters/blend_filter_contents.h"
11 #include "impeller/entity/entity.h"
12 #include "impeller/entity/texture_fill.frag.h"
13 #include "impeller/entity/texture_fill.vert.h"
14 #include "impeller/geometry/color.h"
15 #include "impeller/renderer/render_pass.h"
16
17 namespace impeller {
18
19 AtlasContents::AtlasContents() = default;
20
21 AtlasContents::~AtlasContents() = default;
22
23 std::optional<Rect> AtlasContents::GetCoverage(const Entity& entity) const {
24 if (!geometry_) {
25 return std::nullopt;
26 }
27 return geometry_->ComputeBoundingBox().TransformBounds(entity.GetTransform());
28 }
29
30 void AtlasContents::SetGeometry(AtlasGeometry* geometry) {
31 geometry_ = geometry;
32 }
33
34 void AtlasContents::SetAlpha(Scalar alpha) {
35 alpha_ = alpha;
36 }
37
38 bool AtlasContents::Render(const ContentContext& renderer,
39 const Entity& entity,
40 RenderPass& pass) const {
41 if (geometry_->ShouldSkip() || alpha_ <= 0.0) {
42 return true;
43 }
44
45 auto dst_sampler_descriptor = geometry_->GetSamplerDescriptor();
46 if (renderer.GetDeviceCapabilities().SupportsDecalSamplerAddressMode()) {
47 dst_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
48 dst_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
49 }
50 raw_ptr<const Sampler> dst_sampler =
51 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
52 dst_sampler_descriptor);
53
54 auto& host_buffer = renderer.GetTransientsBuffer();
55 if (!geometry_->ShouldUseBlend()) {
56 using VS = TextureFillVertexShader;
57 using FS = TextureFillFragmentShader;
58
59 auto dst_sampler_descriptor = geometry_->GetSamplerDescriptor();
60
61 raw_ptr<const Sampler> dst_sampler =
62 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
63 dst_sampler_descriptor);
64
65 auto pipeline_options = OptionsFromPassAndEntity(pass, entity);
66 pipeline_options.primitive_type = PrimitiveType::kTriangle;
67 pipeline_options.depth_write_enabled =
68 pipeline_options.blend_mode == BlendMode::kSource;
69
70 pass.SetPipeline(renderer.GetTexturePipeline(pipeline_options));
71 pass.SetVertexBuffer(geometry_->CreateSimpleVertexBuffer(host_buffer));
72 #ifdef IMPELLER_DEBUG
73 pass.SetCommandLabel("DrawAtlas");
74 #endif // IMPELLER_DEBUG
75
76 VS::FrameInfo frame_info;
77 frame_info.mvp = entity.GetShaderTransform(pass);
78 frame_info.texture_sampler_y_coord_scale =
79 geometry_->GetAtlas()->GetYCoordScale();
80
81 VS::BindFrameInfo(pass, host_buffer.EmplaceUniform(frame_info));
82
83 FS::FragInfo frag_info;
84 frag_info.alpha = alpha_;
85 FS::BindFragInfo(pass, host_buffer.EmplaceUniform((frag_info)));
86 FS::BindTextureSampler(pass, geometry_->GetAtlas(), dst_sampler);
87 return pass.Draw().ok();
88 }
89
90 BlendMode blend_mode = geometry_->GetBlendMode();
91
92 if (blend_mode <= BlendMode::kModulate) {
93 using VS = PorterDuffBlendPipeline::VertexShader;
94 using FS = PorterDuffBlendPipeline::FragmentShader;
95
96 #ifdef IMPELLER_DEBUG
97 pass.SetCommandLabel("DrawAtlas Blend");
98 #endif // IMPELLER_DEBUG
99 pass.SetVertexBuffer(geometry_->CreateBlendVertexBuffer(host_buffer));
100 auto inverted_blend_mode =
101 InvertPorterDuffBlend(blend_mode).value_or(BlendMode::kSource);
102 pass.SetPipeline(renderer.GetPorterDuffPipeline(inverted_blend_mode,
103 OptionsFromPass(pass)));
104
105 FS::FragInfo frag_info;
106 VS::FrameInfo frame_info;
107
108 FS::BindTextureSamplerDst(pass, geometry_->GetAtlas(), dst_sampler);
109 frame_info.texture_sampler_y_coord_scale =
110 geometry_->GetAtlas()->GetYCoordScale();
111
112 frag_info.output_alpha = alpha_;
113 frag_info.input_alpha = 1.0;
114
115 // These values are ignored on platforms that natively support decal.
116 frag_info.tmx = static_cast<int>(Entity::TileMode::kDecal);
117 frag_info.tmy = static_cast<int>(Entity::TileMode::kDecal);
118
119 FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
120
121 frame_info.mvp = entity.GetShaderTransform(pass);
122
123 auto uniform_view = host_buffer.EmplaceUniform(frame_info);
124 VS::BindFrameInfo(pass, uniform_view);
125
126 return pass.Draw().ok();
127 }
128
129 using VUS = VerticesUberShader::VertexShader;
130 using FS = VerticesUberShader::FragmentShader;
131
132 #ifdef IMPELLER_DEBUG
133 pass.SetCommandLabel("DrawAtlas Advanced Blend");
134 #endif // IMPELLER_DEBUG
135 pass.SetVertexBuffer(geometry_->CreateBlendVertexBuffer(host_buffer));
136
137 pass.SetPipeline(renderer.GetDrawVerticesUberShader(OptionsFromPass(pass)));
138 FS::BindTextureSampler(pass, geometry_->GetAtlas(), dst_sampler);
139
140 VUS::FrameInfo frame_info;
141 FS::FragInfo frag_info;
142
143 frame_info.texture_sampler_y_coord_scale =
144 geometry_->GetAtlas()->GetYCoordScale();
145 frame_info.mvp = entity.GetShaderTransform(pass);
146
147 frag_info.alpha = alpha_;
148 frag_info.blend_mode = static_cast<int>(blend_mode);
149
150 // These values are ignored on platforms that natively support decal.
151 frag_info.tmx = static_cast<int>(Entity::TileMode::kDecal);
152 frag_info.tmy = static_cast<int>(Entity::TileMode::kDecal);
153
154 FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
155 VUS::BindFrameInfo(pass, host_buffer.EmplaceUniform(frame_info));
156
157 return pass.Draw().ok();
158 }
159
160 } // namespace impeller
atlas_contents.h
blend_filter_contents.h
impeller::AtlasContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: atlas_contents.cc:38
impeller::AtlasContents::SetGeometry
void SetGeometry(AtlasGeometry *geometry)
Definition: atlas_contents.cc:30
impeller::AtlasContents::GetCoverage
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.
Definition: atlas_contents.cc:23
impeller::AtlasContents::SetAlpha
void SetAlpha(Scalar alpha)
Definition: atlas_contents.cc:34
impeller::AtlasContents::~AtlasContents
~AtlasContents() override
impeller::AtlasContents::AtlasContents
AtlasContents()
impeller::AtlasGeometry
Definition: atlas_contents.h:19
impeller::AtlasGeometry::CreateSimpleVertexBuffer
virtual VertexBuffer CreateSimpleVertexBuffer(HostBuffer &host_buffer) const =0
impeller::AtlasGeometry::ComputeBoundingBox
virtual Rect ComputeBoundingBox() const =0
impeller::AtlasGeometry::ShouldUseBlend
virtual bool ShouldUseBlend() const =0
impeller::AtlasGeometry::GetAtlas
virtual std::shared_ptr< Texture > GetAtlas() const =0
impeller::AtlasGeometry::GetSamplerDescriptor
virtual const SamplerDescriptor & GetSamplerDescriptor() const =0
impeller::AtlasGeometry::ShouldSkip
virtual bool ShouldSkip() const =0
impeller::AtlasGeometry::CreateBlendVertexBuffer
virtual VertexBuffer CreateBlendVertexBuffer(HostBuffer &host_buffer) const =0
impeller::AtlasGeometry::GetBlendMode
virtual BlendMode GetBlendMode() const =0
impeller::Capabilities::SupportsDecalSamplerAddressMode
virtual bool SupportsDecalSamplerAddressMode() const =0
Whether the context backend supports SamplerAddressMode::Decal.
impeller::ContentContext
Definition: content_context.h:371
impeller::ContentContext::GetTransientsBuffer
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
Definition: content_context.h:866
impeller::ContentContext::GetPorterDuffPipeline
PipelineRef GetPorterDuffPipeline(BlendMode mode, ContentContextOptions opts) const
Definition: content_context.h:527
impeller::ContentContext::GetDeviceCapabilities
const Capabilities & GetDeviceCapabilities() const
Definition: content_context.cc:617
impeller::ContentContext::GetDrawVerticesUberShader
PipelineRef GetDrawVerticesUberShader(ContentContextOptions opts) const
Definition: content_context.h:799
impeller::ContentContext::GetTexturePipeline
PipelineRef GetTexturePipeline(ContentContextOptions opts) const
Definition: content_context.h:456
impeller::ContentContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: content_context.cc:613
impeller::Entity
Definition: entity.h:20
impeller::Entity::TileMode::kDecal
@ kDecal
impeller::Entity::GetShaderTransform
Matrix GetShaderTransform(const RenderPass &pass) const
Get the vertex shader transform used for drawing this Entity.
Definition: entity.cc:48
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:44
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
impeller::RenderPass::SetVertexBuffer
virtual bool SetVertexBuffer(VertexBuffer buffer)
Specify the vertex and index buffer to use for this command.
Definition: render_pass.cc:127
impeller::RenderPass::SetPipeline
virtual void SetPipeline(PipelineRef pipeline)
The pipeline to use for this command.
Definition: render_pass.cc:86
impeller::RenderPass::Draw
virtual fml::Status Draw()
Record the currently pending command.
Definition: render_pass.cc:208
impeller::RenderPass::SetCommandLabel
virtual void SetCommandLabel(std::string_view label)
The debugging label to use for the command.
Definition: render_pass.cc:97
impeller::RenderPipelineHandle::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:113
impeller::RenderPipelineHandle::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:114
impeller::raw_ptr
A wrapper around a raw ptr that adds additional unopt mode only checks.
Definition: raw_ptr.h:15
color.h
content_context.h
formats.h
entity.h
impeller
Definition: allocation.cc:12
impeller::PrimitiveType::kTriangle
@ kTriangle
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::SamplerAddressMode::kDecal
@ kDecal
decal sampling mode is only supported on devices that pass the Capabilities.SupportsDecalSamplerAddre...
impeller::VS
GlyphAtlasPipeline::VertexShader VS
Definition: text_contents.cc:25
impeller::InvertPorterDuffBlend
std::optional< BlendMode > InvertPorterDuffBlend(BlendMode blend_mode)
Definition: blend_filter_contents.cc:51
impeller::BlendMode
BlendMode
Definition: color.h:58
impeller::BlendMode::kModulate
@ kModulate
impeller::BlendMode::kSource
@ kSource
impeller::FS
GlyphAtlasPipeline::FragmentShader FS
Definition: text_contents.cc:26
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:34
impeller::OptionsFromPass
ContentContextOptions OptionsFromPass(const RenderPass &pass)
Definition: contents.cc:19
render_pass.h
impeller::SamplerDescriptor::width_address_mode
SamplerAddressMode width_address_mode
Definition: sampler_descriptor.h:19
impeller::TRect::TransformBounds
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:476