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