| Flutter Impeller |

vertices_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 "vertices_contents.h"
6
7 #include "impeller/entity/contents/content_context.h"
8 #include "impeller/entity/contents/filters/color_filter_contents.h"
9 #include "impeller/entity/position_color.vert.h"
10 #include "impeller/entity/vertices.frag.h"
11 #include "impeller/geometry/color.h"
12 #include "impeller/renderer/render_pass.h"
13
14 namespace impeller {
15
16 VerticesContents::VerticesContents() = default;
17
18 VerticesContents::~VerticesContents() = default;
19
20 std::optional<Rect> VerticesContents::GetCoverage(const Entity& entity) const {
21 return geometry_->GetCoverage(entity.GetTransform());
22 };
23
24 void VerticesContents::SetGeometry(std::shared_ptr<VerticesGeometry> geometry) {
25 geometry_ = std::move(geometry);
26 }
27
28 void VerticesContents::SetSourceContents(std::shared_ptr<Contents> contents) {
29 src_contents_ = std::move(contents);
30 }
31
32 std::shared_ptr<VerticesGeometry> VerticesContents::GetGeometry() const {
33 return geometry_;
34 }
35
36 void VerticesContents::SetAlpha(Scalar alpha) {
37 alpha_ = alpha;
38 }
39
40 void VerticesContents::SetBlendMode(BlendMode blend_mode) {
41 blend_mode_ = blend_mode;
42 }
43
44 const std::shared_ptr<Contents>& VerticesContents::GetSourceContents() const {
45 return src_contents_;
46 }
47
48 bool VerticesContents::Render(const ContentContext& renderer,
49 const Entity& entity,
50 RenderPass& pass) const {
51 if (blend_mode_ == BlendMode::kClear) {
52 return true;
53 }
54 std::shared_ptr<Contents> src_contents = src_contents_;
55 src_contents->SetCoverageHint(GetCoverageHint());
56 if (geometry_->HasTextureCoordinates()) {
57 auto contents = std::make_shared<VerticesUVContents>(*this);
58 contents->SetCoverageHint(GetCoverageHint());
59 if (!geometry_->HasVertexColors()) {
60 contents->SetAlpha(alpha_);
61 return contents->Render(renderer, entity, pass);
62 }
63 src_contents = contents;
64 }
65
66 auto dst_contents = std::make_shared<VerticesColorContents>(*this);
67 dst_contents->SetCoverageHint(GetCoverageHint());
68
69 std::shared_ptr<Contents> contents;
70 if (blend_mode_ == BlendMode::kDestination) {
71 dst_contents->SetAlpha(alpha_);
72 contents = dst_contents;
73 } else {
74 auto color_filter_contents = ColorFilterContents::MakeBlend(
75 blend_mode_, {FilterInput::Make(dst_contents, false),
76 FilterInput::Make(src_contents, false)});
77 color_filter_contents->SetAlpha(alpha_);
78 color_filter_contents->SetCoverageHint(GetCoverageHint());
79 contents = color_filter_contents;
80 }
81
82 FML_DCHECK(contents->GetCoverageHint() == GetCoverageHint());
83 return contents->Render(renderer, entity, pass);
84 }
85
86 //------------------------------------------------------
87 // VerticesUVContents
88
89 VerticesUVContents::VerticesUVContents(const VerticesContents& parent)
90 : parent_(parent) {}
91
92 VerticesUVContents::~VerticesUVContents() {}
93
94 std::optional<Rect> VerticesUVContents::GetCoverage(
95 const Entity& entity) const {
96 return parent_.GetCoverage(entity);
97 }
98
99 void VerticesUVContents::SetAlpha(Scalar alpha) {
100 alpha_ = alpha;
101 }
102
103 bool VerticesUVContents::Render(const ContentContext& renderer,
104 const Entity& entity,
105 RenderPass& pass) const {
106 using VS = TexturePipeline::VertexShader;
107 using FS = TexturePipeline::FragmentShader;
108
109 auto src_contents = parent_.GetSourceContents();
110
111 auto snapshot =
112 src_contents->RenderToSnapshot(renderer, // renderer
113 entity, // entity
114 GetCoverageHint(), // coverage_limit
115 std::nullopt, // sampler_descriptor
116 true, // msaa_enabled
117 "VerticesUVContents Snapshot"); // label
118 if (!snapshot.has_value()) {
119 return false;
120 }
121
122 Command cmd;
123 DEBUG_COMMAND_INFO(cmd, "VerticesUV");
124 auto& host_buffer = pass.GetTransientsBuffer();
125 auto geometry = parent_.GetGeometry();
126
127 auto coverage = src_contents->GetCoverage(Entity{});
128 if (!coverage.has_value()) {
129 return false;
130 }
131 auto geometry_result = geometry->GetPositionUVBuffer(
132 coverage.value(), Matrix(), renderer, entity, pass);
133 auto opts = OptionsFromPassAndEntity(pass, entity);
134 opts.primitive_type = geometry_result.type;
135 cmd.pipeline = renderer.GetTexturePipeline(opts);
136 cmd.stencil_reference = entity.GetClipDepth();
137 cmd.BindVertices(std::move(geometry_result.vertex_buffer));
138
139 VS::FrameInfo frame_info;
140 frame_info.mvp = geometry_result.transform;
141 frame_info.texture_sampler_y_coord_scale =
142 snapshot->texture->GetYCoordScale();
143 frame_info.alpha = alpha_ * snapshot->opacity;
144 VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info));
145
146 FS::BindTextureSampler(cmd, snapshot->texture,
147 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
148 snapshot->sampler_descriptor));
149
150 return pass.AddCommand(std::move(cmd));
151 }
152
153 //------------------------------------------------------
154 // VerticesColorContents
155
156 VerticesColorContents::VerticesColorContents(const VerticesContents& parent)
157 : parent_(parent) {}
158
159 VerticesColorContents::~VerticesColorContents() {}
160
161 std::optional<Rect> VerticesColorContents::GetCoverage(
162 const Entity& entity) const {
163 return parent_.GetCoverage(entity);
164 }
165
166 void VerticesColorContents::SetAlpha(Scalar alpha) {
167 alpha_ = alpha;
168 }
169
170 bool VerticesColorContents::Render(const ContentContext& renderer,
171 const Entity& entity,
172 RenderPass& pass) const {
173 using VS = GeometryColorPipeline::VertexShader;
174 using FS = GeometryColorPipeline::FragmentShader;
175
176 Command cmd;
177 DEBUG_COMMAND_INFO(cmd, "VerticesColors");
178 auto& host_buffer = pass.GetTransientsBuffer();
179 auto geometry = parent_.GetGeometry();
180
181 auto geometry_result =
182 geometry->GetPositionColorBuffer(renderer, entity, pass);
183 auto opts = OptionsFromPassAndEntity(pass, entity);
184 opts.primitive_type = geometry_result.type;
185 cmd.pipeline = renderer.GetGeometryColorPipeline(opts);
186 cmd.stencil_reference = entity.GetClipDepth();
187 cmd.BindVertices(std::move(geometry_result.vertex_buffer));
188
189 VS::FrameInfo frame_info;
190 frame_info.mvp = geometry_result.transform;
191 VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info));
192
193 FS::FragInfo frag_info;
194 frag_info.alpha = alpha_;
195 FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info));
196
197 return pass.AddCommand(std::move(cmd));
198 }
199
200 } // namespace impeller
impeller::ContentContext::GetTexturePipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetTexturePipeline(ContentContextOptions opts) const
Definition: content_context.h:415
impeller::VerticesContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: vertices_contents.cc:48
impeller::VerticesColorContents::~VerticesColorContents
~VerticesColorContents() override
Definition: vertices_contents.cc:159
impeller::VerticesContents::SetBlendMode
void SetBlendMode(BlendMode blend_mode)
Definition: vertices_contents.cc:40
impeller::VerticesUVContents::SetAlpha
void SetAlpha(Scalar alpha)
Definition: vertices_contents.cc:99
impeller::Command
An object used to specify work to the GPU along with references to resources the GPU will used when d...
Definition: command.h:92
impeller::VerticesColorContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: vertices_contents.cc:170
DEBUG_COMMAND_INFO
#define DEBUG_COMMAND_INFO(obj, arg)
Definition: command.h:28
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:49
impeller::BlendMode
BlendMode
Definition: color.h:59
impeller::VerticesUVContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: vertices_contents.cc:103
impeller::FilterInput::Make
static FilterInput::Ref Make(Variant input, bool msaa_enabled=true)
Definition: filter_input.cc:19
impeller::VerticesColorContents::VerticesColorContents
VerticesColorContents(const VerticesContents &parent)
Definition: vertices_contents.cc:156
impeller::BlendMode::kDestination
@ kDestination
impeller::RenderPipelineT::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:93
impeller::VerticesContents::SetGeometry
void SetGeometry(std::shared_ptr< VerticesGeometry > geometry)
Definition: vertices_contents.cc:24
vertices_contents.h
impeller::VerticesContents::SetAlpha
void SetAlpha(Scalar alpha)
Definition: vertices_contents.cc:36
impeller::VerticesContents
Definition: vertices_contents.h:24
impeller::Entity
Definition: entity.h:21
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:28
impeller::Contents::GetCoverageHint
const std::optional< Rect > & GetCoverageHint() const
Definition: contents.cc:152
impeller::VerticesContents::~VerticesContents
~VerticesContents() override
render_pass.h
impeller::VerticesUVContents::VerticesUVContents
VerticesUVContents(const VerticesContents &parent)
Definition: vertices_contents.cc:89
impeller::BlendMode::kClear
@ kClear
impeller::ContentContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: content_context.cc:479
impeller::VerticesContents::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: vertices_contents.cc:20
impeller::Command::BindVertices
bool BindVertices(VertexBuffer buffer)
Specify the vertex and index buffer to use for this command.
Definition: command.cc:15
impeller::VerticesContents::SetSourceContents
void SetSourceContents(std::shared_ptr< Contents > contents)
Definition: vertices_contents.cc:28
color_filter_contents.h
impeller::VerticesColorContents::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: vertices_contents.cc:161
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:29
impeller::Entity::GetClipDepth
uint32_t GetClipDepth() const
Definition: entity.cc:93
impeller::Command::stencil_reference
uint32_t stencil_reference
Definition: command.h:122
content_context.h
impeller::VerticesContents::VerticesContents
VerticesContents()
impeller::VerticesUVContents::~VerticesUVContents
~VerticesUVContents() override
Definition: vertices_contents.cc:92
color.h
impeller::Command::pipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > pipeline
Definition: command.h:96
impeller::RenderPipelineT::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:92
impeller::ColorFilterContents::MakeBlend
static std::shared_ptr< ColorFilterContents > MakeBlend(BlendMode blend_mode, FilterInput::Vector inputs, std::optional< Color > foreground_color=std::nullopt)
the [inputs] are expected to be in the order of dst, src.
Definition: color_filter_contents.cc:17
impeller::RenderPass::AddCommand
bool AddCommand(Command &&command)
Record a command for subsequent encoding to the underlying command buffer. No work is encoded into th...
Definition: render_pass.cc:67
impeller
Definition: aiks_context.cc:10
impeller::VerticesContents::GetSourceContents
const std::shared_ptr< Contents > & GetSourceContents() const
Definition: vertices_contents.cc:44
impeller::VerticesContents::GetGeometry
std::shared_ptr< VerticesGeometry > GetGeometry() const
Definition: vertices_contents.cc:32
impeller::ContentContext
Definition: content_context.h:332
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::ContentContext::GetGeometryColorPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetGeometryColorPipeline(ContentContextOptions opts) const
Definition: content_context.h:506
impeller::VerticesColorContents::SetAlpha
void SetAlpha(Scalar alpha)
Definition: vertices_contents.cc:166
impeller::RenderPass::GetTransientsBuffer
HostBuffer & GetTransientsBuffer()
Definition: render_pass.cc:55
impeller::VerticesUVContents::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: vertices_contents.cc:94