Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 24811cf

Browse files
committed
simplify vertex accumulation
1 parent 92b97fd commit 24811cf

File tree

4 files changed

+71
-92
lines changed

4 files changed

+71
-92
lines changed

impeller/entity/geometry/ellipse_geometry.cc

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,19 @@ GeometryResult EllipseGeometry::GetPositionBuffer(
2121
RenderPass& pass) {
2222
auto& host_buffer = pass.GetTransientsBuffer();
2323

24+
VertexBufferBuilder<SolidFillVertexShader::PerVertexData> vtx_builder;
25+
2426
CircleTessellator tessellator(entity.GetTransformation(), radius_);
25-
auto vertices = std::vector<Point>(tessellator.GetCircleVertexCount());
27+
vtx_builder.Reserve(tessellator.GetCircleVertexCount());
2628
tessellator.GenerateCircleTriangleStrip(
27-
[&vertices](const Point& p) { vertices.push_back(p); }, center_, radius_);
29+
[&vtx_builder](const Point& p) { //
30+
vtx_builder.AppendVertex({.position = p});
31+
},
32+
center_, radius_);
2833

2934
return GeometryResult{
3035
.type = PrimitiveType::kTriangleStrip,
31-
.vertex_buffer =
32-
{
33-
.vertex_buffer = host_buffer.Emplace(
34-
vertices.data(), vertices.size() * sizeof(Point),
35-
alignof(float)),
36-
.vertex_count = vertices.size(),
37-
.index_type = IndexType::kNone,
38-
},
36+
.vertex_buffer = vtx_builder.CreateVertexBuffer(host_buffer),
3937
.transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
4038
entity.GetTransformation(),
4139
.prevent_overdraw = false,
@@ -53,26 +51,22 @@ GeometryResult EllipseGeometry::GetPositionUVBuffer(
5351

5452
auto uv_transform =
5553
texture_coverage.GetNormalizingTransform() * effect_transform;
54+
VertexBufferBuilder<TextureFillVertexShader::PerVertexData> vtx_builder;
5655

5756
CircleTessellator tessellator(entity.GetTransformation(), radius_);
58-
auto vertices = std::vector<Point>(tessellator.GetCircleVertexCount());
57+
vtx_builder.Reserve(tessellator.GetCircleVertexCount());
5958
tessellator.GenerateCircleTriangleStrip(
60-
[&vertices, &uv_transform](const Point& p) {
61-
vertices.push_back(p);
62-
vertices.push_back(uv_transform * p);
59+
[&vtx_builder, &uv_transform](const Point& p) {
60+
vtx_builder.AppendVertex({
61+
.position = p,
62+
.texture_coords = uv_transform * p,
63+
});
6364
},
6465
center_, radius_);
6566

6667
return GeometryResult{
6768
.type = PrimitiveType::kTriangleStrip,
68-
.vertex_buffer =
69-
{
70-
.vertex_buffer = host_buffer.Emplace(
71-
vertices.data(), vertices.size() * sizeof(Point),
72-
alignof(float)),
73-
.vertex_count = vertices.size() / 2,
74-
.index_type = IndexType::kNone,
75-
},
69+
.vertex_buffer = vtx_builder.CreateVertexBuffer(host_buffer),
7670
.transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
7771
entity.GetTransformation(),
7872
.prevent_overdraw = false,

impeller/entity/geometry/line_geometry.cc

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -70,44 +70,37 @@ bool LineGeometry::ComputeCorners(Point corners[4],
7070
GeometryResult LineGeometry::GetPositionBuffer(const ContentContext& renderer,
7171
const Entity& entity,
7272
RenderPass& pass) {
73-
std::vector<Point> vertices;
7473
auto& transform = entity.GetTransformation();
7574
auto radius = ComputeHalfWidth(transform);
7675

76+
VertexBufferBuilder<SolidFillVertexShader::PerVertexData> vtx_builder;
77+
7778
if (cap_ == Cap::kRound) {
7879
CircleTessellator tessellator(transform, radius);
79-
vertices.reserve(tessellator.GetCircleVertexCount());
80+
vtx_builder.Reserve(tessellator.GetCircleVertexCount());
8081
tessellator.GenerateRoundCapLineTriangleStrip(
81-
[&vertices](const Point& p) { //
82-
vertices.push_back(p);
82+
[&vtx_builder](const Point& p) { //
83+
vtx_builder.AppendVertex({.position = p});
8384
},
8485
p0_, p1_, radius);
8586
} else {
8687
Point corners[4];
8788
if (ComputeCorners(corners, transform, cap_ == Cap::kSquare)) {
88-
vertices.reserve(4);
89-
vertices.push_back(corners[0]);
90-
vertices.push_back(corners[1]);
91-
vertices.push_back(corners[2]);
92-
vertices.push_back(corners[3]);
89+
vtx_builder.Reserve(4);
90+
vtx_builder.AppendVertex({.position = corners[0]});
91+
vtx_builder.AppendVertex({.position = corners[1]});
92+
vtx_builder.AppendVertex({.position = corners[2]});
93+
vtx_builder.AppendVertex({.position = corners[3]});
9394
}
9495
}
95-
if (vertices.empty()) {
96+
if (!vtx_builder.HasVertices()) {
9697
return {};
9798
}
9899

99-
static_assert(sizeof(Point) == 2 * sizeof(float));
100-
static_assert(alignof(Point) == alignof(float));
101100
return GeometryResult{
102101
.type = PrimitiveType::kTriangleStrip,
103102
.vertex_buffer =
104-
{
105-
.vertex_buffer = pass.GetTransientsBuffer().Emplace(
106-
vertices.data(), vertices.size() * sizeof(Point),
107-
alignof(Point)),
108-
.vertex_count = vertices.size(),
109-
.index_type = IndexType::kNone,
110-
},
103+
vtx_builder.CreateVertexBuffer(pass.GetTransientsBuffer()),
111104
.transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
112105
entity.GetTransformation(),
113106
.prevent_overdraw = false,
@@ -120,52 +113,44 @@ GeometryResult LineGeometry::GetPositionUVBuffer(Rect texture_coverage,
120113
const ContentContext& renderer,
121114
const Entity& entity,
122115
RenderPass& pass) {
123-
std::vector<Point> vertices;
124116
auto& transform = entity.GetTransformation();
125117
auto radius = ComputeHalfWidth(transform);
126118

127119
auto uv_transform =
128120
texture_coverage.GetNormalizingTransform() * effect_transform;
121+
VertexBufferBuilder<TextureFillVertexShader::PerVertexData> vtx_builder;
129122

130123
if (cap_ == Cap::kRound) {
131124
CircleTessellator tessellator(transform, radius);
132-
vertices.reserve(tessellator.GetCircleVertexCount() * 2);
125+
vtx_builder.Reserve(tessellator.GetCircleVertexCount());
133126
tessellator.GenerateRoundCapLineTriangleStrip(
134-
[&vertices, &uv_transform](const Point& p) {
135-
vertices.push_back(p);
136-
vertices.push_back(uv_transform * p);
127+
[&vtx_builder, &uv_transform](const Point& p) {
128+
vtx_builder.AppendVertex({
129+
.position = p,
130+
.texture_coords = uv_transform * p,
131+
});
137132
},
138133
p0_, p1_, radius);
139134
} else {
140135
Point corners[4];
141136
if (ComputeCorners(corners, transform, cap_ == Cap::kSquare)) {
142-
vertices.reserve(8);
143-
vertices.push_back(corners[0]);
144-
vertices.push_back(uv_transform * corners[0]);
145-
vertices.push_back(corners[1]);
146-
vertices.push_back(uv_transform * corners[1]);
147-
vertices.push_back(corners[2]);
148-
vertices.push_back(uv_transform * corners[2]);
149-
vertices.push_back(corners[3]);
150-
vertices.push_back(uv_transform * corners[3]);
137+
vtx_builder.Reserve(4);
138+
for (auto& corner : corners) {
139+
vtx_builder.AppendVertex({
140+
.position = corner,
141+
.texture_coords = uv_transform * corner,
142+
});
143+
}
151144
}
152145
}
153-
if (vertices.empty()) {
146+
if (!vtx_builder.HasVertices()) {
154147
return {};
155148
}
156149

157-
static_assert(sizeof(Point) == 2 * sizeof(float));
158-
static_assert(alignof(Point) == alignof(float));
159150
return GeometryResult{
160151
.type = PrimitiveType::kTriangleStrip,
161152
.vertex_buffer =
162-
{
163-
.vertex_buffer = pass.GetTransientsBuffer().Emplace(
164-
vertices.data(), vertices.size() * sizeof(Point),
165-
alignof(Point)),
166-
.vertex_count = vertices.size() / 2,
167-
.index_type = IndexType::kNone,
168-
},
153+
vtx_builder.CreateVertexBuffer(pass.GetTransientsBuffer()),
169154
.transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
170155
entity.GetTransformation(),
171156
.prevent_overdraw = false,

impeller/entity/geometry/point_field_geometry.cc

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -89,48 +89,43 @@ PointFieldGeometry::GetPositionBufferCPU(const ContentContext& renderer,
8989
if (round_) {
9090
CircleTessellator tessellator(entity.GetTransformation(), radius);
9191

92-
// Get polygon relative to {0, 0} so we can translate it to each point
93-
// in turn.
94-
std::vector<Point> vertices;
92+
// Get triangulation relative to {0, 0} so we can translate it to each
93+
// point in turn.
94+
std::vector<Point> circle_vertices;
95+
circle_vertices.reserve(tessellator.GetCircleVertexCount());
9596
tessellator.GenerateCircleTriangleStrip(
96-
[&vertices](const Point& p) { //
97-
vertices.push_back(p);
97+
[&circle_vertices](const Point& p) { //
98+
circle_vertices.push_back(p);
9899
},
99100
{}, radius);
100-
FML_DCHECK(vertices.size() == tessellator.GetCircleVertexCount());
101-
vtx_builder.Reserve(vertices.size() * points_.size());
101+
FML_DCHECK(circle_vertices.size() == tessellator.GetCircleVertexCount());
102102

103-
bool first = true;
104-
Point prev;
103+
vtx_builder.Reserve((circle_vertices.size() + 2) * points_.size() - 2);
105104
for (auto& center : points_) {
106-
if (first) {
107-
first = false;
108-
} else {
109-
vtx_builder.AppendVertex({prev});
110-
vtx_builder.AppendVertex({center + vertices[0]});
105+
if (vtx_builder.HasVertices()) {
106+
vtx_builder.AppendVertex(vtx_builder.Last());
107+
vtx_builder.AppendVertex({center + circle_vertices[0]});
111108
}
112-
for (auto& vertex : vertices) {
113-
prev = center + vertex;
114-
vtx_builder.AppendVertex({prev});
109+
110+
for (auto& vertex : circle_vertices) {
111+
vtx_builder.AppendVertex({center + vertex});
115112
}
116113
}
117114
} else {
118-
vtx_builder.Reserve(6 * points_.size());
119-
bool first = true;
120-
Point prev;
115+
vtx_builder.Reserve(6 * points_.size() - 2);
121116
for (auto& point : points_) {
122-
if (first) {
123-
first = false;
124-
} else {
125-
vtx_builder.AppendVertex({prev});
126-
vtx_builder.AppendVertex({{point.x - radius, point.y - radius}});
117+
auto first = Point(point.x - radius, point.y - radius);
118+
119+
if (vtx_builder.HasVertices()) {
120+
vtx_builder.AppendVertex(vtx_builder.Last());
121+
vtx_builder.AppendVertex({first});
127122
}
128123

129124
// Z pattern from UL -> UR -> LL -> LR
130-
vtx_builder.AppendVertex({{point.x - radius, point.y - radius}});
125+
vtx_builder.AppendVertex({first});
131126
vtx_builder.AppendVertex({{point.x + radius, point.y - radius}});
132127
vtx_builder.AppendVertex({{point.x - radius, point.y + radius}});
133-
vtx_builder.AppendVertex({prev = {point.x + radius, point.y + radius}});
128+
vtx_builder.AppendVertex({{point.x + radius, point.y + radius}});
134129
}
135130
}
136131

impeller/renderer/vertex_buffer_builder.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ class VertexBufferBuilder {
5656
return indices_.size() > 0 ? indices_.size() : vertices_.size();
5757
}
5858

59+
const VertexType& Last() const {
60+
FML_DCHECK(!vertices_.empty());
61+
return vertices_.back();
62+
}
63+
5964
VertexBufferBuilder& AppendVertex(VertexType_ vertex) {
6065
vertices_.emplace_back(std::move(vertex));
6166
return *this;

0 commit comments

Comments
 (0)