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

Commit 6ad93e3

Browse files
authored
[Impeller] Clean up fill shaders (#34739)
1 parent 7d173c5 commit 6ad93e3

File tree

11 files changed

+114
-94
lines changed

11 files changed

+114
-94
lines changed

impeller/entity/contents/clip_contents.cc

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,17 @@ bool ClipContents::Render(const ContentContext& renderer,
4545
const Entity& entity,
4646
RenderPass& pass) const {
4747
using VS = ClipPipeline::VertexShader;
48+
using FS = ClipPipeline::FragmentShader;
4849

49-
VS::FrameInfo info;
50-
// The color really doesn't matter.
51-
info.color = Color::SkyBlue();
50+
VS::VertInfo info;
5251

5352
Command cmd;
53+
54+
FS::FragInfo frag_info;
55+
// The color really doesn't matter.
56+
frag_info.color = Color::SkyBlue();
57+
FS::BindFragInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frag_info));
58+
5459
auto options = OptionsFromPassAndEntity(pass, entity);
5560
cmd.stencil_reference = entity.GetStencilDepth();
5661
options.stencil_compare = CompareFunction::kEqual;
@@ -69,7 +74,7 @@ bool ClipContents::Render(const ContentContext& renderer,
6974
cmd.BindVertices(std::move(vertices));
7075

7176
info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize());
72-
VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(info));
77+
VS::BindVertInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(info));
7378

7479
cmd.pipeline = renderer.GetClipPipeline(options);
7580
pass.AddCommand(cmd);
@@ -95,7 +100,7 @@ bool ClipContents::Render(const ContentContext& renderer,
95100

96101
info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
97102
entity.GetTransformation();
98-
VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(info));
103+
VS::BindVertInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(info));
99104

100105
pass.AddCommand(std::move(cmd));
101106
return true;
@@ -123,6 +128,7 @@ bool ClipRestoreContents::Render(const ContentContext& renderer,
123128
const Entity& entity,
124129
RenderPass& pass) const {
125130
using VS = ClipPipeline::VertexShader;
131+
using FS = ClipPipeline::FragmentShader;
126132

127133
Command cmd;
128134
cmd.label = "Restore Clip";
@@ -145,12 +151,14 @@ bool ClipRestoreContents::Render(const ContentContext& renderer,
145151
});
146152
cmd.BindVertices(vtx_builder.CreateVertexBuffer(pass.GetTransientsBuffer()));
147153

148-
VS::FrameInfo info;
149-
// The color really doesn't matter.
150-
info.color = Color::SkyBlue();
154+
VS::VertInfo info;
151155
info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize());
156+
VS::BindVertInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(info));
152157

153-
VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(info));
158+
FS::FragInfo frag_info;
159+
// The color really doesn't matter.
160+
frag_info.color = Color::SkyBlue();
161+
FS::BindFragInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frag_info));
154162

155163
pass.AddCommand(std::move(cmd));
156164
return true;

impeller/entity/contents/solid_color_contents.cc

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,8 @@ VertexBuffer SolidColorContents::CreateSolidFillVertices(const Path& path,
4848
VertexBufferBuilder<VS::PerVertexData> vtx_builder;
4949

5050
auto tesselation_result = Tessellator{}.Tessellate(
51-
path.GetFillType(), path.CreatePolyline(), [&vtx_builder](auto point) {
52-
VS::PerVertexData vtx;
53-
vtx.vertices = point;
54-
vtx_builder.AppendVertex(vtx);
55-
});
51+
path.GetFillType(), path.CreatePolyline(),
52+
[&vtx_builder](auto point) { vtx_builder.AppendVertex({point}); });
5653
if (tesselation_result != Tessellator::Result::kSuccess) {
5754
return {};
5855
}
@@ -64,6 +61,7 @@ bool SolidColorContents::Render(const ContentContext& renderer,
6461
const Entity& entity,
6562
RenderPass& pass) const {
6663
using VS = SolidFillPipeline::VertexShader;
64+
using FS = SolidFillPipeline::FragmentShader;
6765

6866
Command cmd;
6967
cmd.label = "Solid Fill";
@@ -77,11 +75,14 @@ bool SolidColorContents::Render(const ContentContext& renderer,
7775
: path_,
7876
pass.GetTransientsBuffer()));
7977

80-
VS::FrameInfo frame_info;
81-
frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
82-
entity.GetTransformation();
83-
frame_info.color = color_.Premultiply();
84-
VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
78+
VS::VertInfo vert_info;
79+
vert_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
80+
entity.GetTransformation();
81+
VS::BindVertInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(vert_info));
82+
83+
FS::FragInfo frag_info;
84+
frag_info.color = color_.Premultiply();
85+
FS::BindFragInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frag_info));
8586

8687
cmd.primitive_type = PrimitiveType::kTriangle;
8788

impeller/entity/contents/solid_stroke_contents.cc

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ static VertexBuffer CreateSolidStrokeVertices(
109109
// We're drawing a triangle strip, so we need to "pick up the pen" by
110110
// appending transparent vertices between the end of the previous contour
111111
// and the beginning of the new contour.
112-
vtx.vertex_position = polyline.points[contour_start_point_i - 1];
113-
vtx.vertex_normal = {};
112+
vtx.position = polyline.points[contour_start_point_i - 1];
113+
vtx.normal = {};
114114
vtx.pen_down = 0.0;
115115
// Append two transparent vertices when "picking up" the pen so that the
116116
// triangle drawn when moving to the beginning of the new contour will
@@ -119,7 +119,7 @@ static VertexBuffer CreateSolidStrokeVertices(
119119
vtx_builder.AppendVertex(vtx);
120120
vtx_builder.AppendVertex(vtx);
121121

122-
vtx.vertex_position = polyline.points[contour_start_point_i];
122+
vtx.position = polyline.points[contour_start_point_i];
123123
// Append two vertices at the beginning of the new contour
124124
// so that the next appended vertex will create a triangle with zero
125125
// volume.
@@ -139,16 +139,16 @@ static VertexBuffer CreateSolidStrokeVertices(
139139
point_i++) {
140140
if (point_i > contour_start_point_i) {
141141
// Generate line rect.
142-
vtx.vertex_position = polyline.points[point_i - 1];
142+
vtx.position = polyline.points[point_i - 1];
143143
vtx.pen_down = 1.0;
144-
vtx.vertex_normal = normal;
144+
vtx.normal = normal;
145145
vtx_builder.AppendVertex(vtx);
146-
vtx.vertex_normal = -normal;
146+
vtx.normal = -normal;
147147
vtx_builder.AppendVertex(vtx);
148-
vtx.vertex_position = polyline.points[point_i];
149-
vtx.vertex_normal = normal;
148+
vtx.position = polyline.points[point_i];
149+
vtx.normal = normal;
150150
vtx_builder.AppendVertex(vtx);
151-
vtx.vertex_normal = -normal;
151+
vtx.normal = -normal;
152152
vtx_builder.AppendVertex(vtx);
153153

154154
if (point_i < contour_end_point_i - 1) {
@@ -181,13 +181,16 @@ bool SolidStrokeContents::Render(const ContentContext& renderer,
181181
return true;
182182
}
183183

184-
using VS = SolidStrokeVertexShader;
184+
using VS = SolidStrokePipeline::VertexShader;
185+
using FS = SolidStrokePipeline::FragmentShader;
186+
187+
VS::VertInfo vert_info;
188+
vert_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
189+
entity.GetTransformation();
190+
vert_info.size = stroke_size_;
185191

186-
VS::FrameInfo frame_info;
187-
frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
188-
entity.GetTransformation();
189-
frame_info.color = color_.Premultiply();
190-
frame_info.size = stroke_size_;
192+
FS::FragInfo frag_info;
193+
frag_info.color = color_.Premultiply();
191194

192195
Command cmd;
193196
cmd.primitive_type = PrimitiveType::kTriangleStrip;
@@ -206,7 +209,8 @@ bool SolidStrokeContents::Render(const ContentContext& renderer,
206209
cmd.BindVertices(CreateSolidStrokeVertices(path_, pass.GetTransientsBuffer(),
207210
cap_proc_, join_proc_,
208211
miter_limit_, smoothing));
209-
VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
212+
VS::BindVertInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(vert_info));
213+
FS::BindFragInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frag_info));
210214

211215
pass.AddCommand(cmd);
212216

@@ -251,7 +255,7 @@ void SolidStrokeContents::SetStrokeCap(Cap cap) {
251255
const Point& position, const Point& normal,
252256
const SmoothingApproximation& smoothing) {
253257
SolidStrokeVertexShader::PerVertexData vtx;
254-
vtx.vertex_position = position;
258+
vtx.position = position;
255259
vtx.pen_down = 1.0;
256260

257261
Point forward(normal.y, -normal.x);
@@ -262,14 +266,14 @@ void SolidStrokeContents::SetStrokeCap(Cap cap) {
262266
forward + normal * PathBuilder::kArcApproximationMagic, forward)
263267
.CreatePolyline(smoothing);
264268

265-
vtx.vertex_normal = normal;
269+
vtx.normal = normal;
266270
vtx_builder.AppendVertex(vtx);
267-
vtx.vertex_normal = -normal;
271+
vtx.normal = -normal;
268272
vtx_builder.AppendVertex(vtx);
269273
for (const auto& point : arc_points) {
270-
vtx.vertex_normal = point;
274+
vtx.normal = point;
271275
vtx_builder.AppendVertex(vtx);
272-
vtx.vertex_normal = (-point).Reflect(forward);
276+
vtx.normal = (-point).Reflect(forward);
273277
vtx_builder.AppendVertex(vtx);
274278
}
275279
};
@@ -279,18 +283,18 @@ void SolidStrokeContents::SetStrokeCap(Cap cap) {
279283
const Point& position, const Point& normal,
280284
const SmoothingApproximation& smoothing) {
281285
SolidStrokeVertexShader::PerVertexData vtx;
282-
vtx.vertex_position = position;
286+
vtx.position = position;
283287
vtx.pen_down = 1.0;
284288

285289
Point forward(normal.y, -normal.x);
286290

287-
vtx.vertex_normal = normal;
291+
vtx.normal = normal;
288292
vtx_builder.AppendVertex(vtx);
289-
vtx.vertex_normal = -normal;
293+
vtx.normal = -normal;
290294
vtx_builder.AppendVertex(vtx);
291-
vtx.vertex_normal = normal + forward;
295+
vtx.normal = normal + forward;
292296
vtx_builder.AppendVertex(vtx);
293-
vtx.vertex_normal = -normal + forward;
297+
vtx.normal = -normal + forward;
294298
vtx_builder.AppendVertex(vtx);
295299
};
296300
break;
@@ -307,15 +311,15 @@ static Scalar CreateBevelAndGetDirection(
307311
const Point& start_normal,
308312
const Point& end_normal) {
309313
SolidStrokeVertexShader::PerVertexData vtx;
310-
vtx.vertex_position = position;
314+
vtx.position = position;
311315
vtx.pen_down = 1.0;
312-
vtx.vertex_normal = {};
316+
vtx.normal = {};
313317
vtx_builder.AppendVertex(vtx);
314318

315319
Scalar dir = start_normal.Cross(end_normal) > 0 ? -1 : 1;
316-
vtx.vertex_normal = start_normal * dir;
320+
vtx.normal = start_normal * dir;
317321
vtx_builder.AppendVertex(vtx);
318-
vtx.vertex_normal = end_normal * dir;
322+
vtx.normal = end_normal * dir;
319323
vtx_builder.AppendVertex(vtx);
320324

321325
return dir;
@@ -357,9 +361,9 @@ void SolidStrokeContents::SetStrokeJoin(Join join) {
357361

358362
// Outer miter point.
359363
SolidStrokeVertexShader::PerVertexData vtx;
360-
vtx.vertex_position = position;
364+
vtx.position = position;
361365
vtx.pen_down = 1.0;
362-
vtx.vertex_normal = miter_point * dir;
366+
vtx.normal = miter_point * dir;
363367
vtx_builder.AppendVertex(vtx);
364368
};
365369
break;
@@ -391,12 +395,12 @@ void SolidStrokeContents::SetStrokeJoin(Join join) {
391395
.CreatePolyline(smoothing);
392396

393397
SolidStrokeVertexShader::PerVertexData vtx;
394-
vtx.vertex_position = position;
398+
vtx.position = position;
395399
vtx.pen_down = 1.0;
396400
for (const auto& point : arc_points) {
397-
vtx.vertex_normal = point * dir;
401+
vtx.normal = point * dir;
398402
vtx_builder.AppendVertex(vtx);
399-
vtx.vertex_normal = (-point * dir).Reflect(middle);
403+
vtx.normal = (-point * dir).Reflect(middle);
400404
vtx_builder.AppendVertex(vtx);
401405
}
402406
};

impeller/entity/contents/texture_contents.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ bool TextureContents::Render(const ContentContext& renderer,
7878
path_.GetFillType(), path_.CreatePolyline(),
7979
[this, &vertex_builder, &coverage_rect, &texture_size](Point vtx) {
8080
VS::PerVertexData data;
81-
data.vertices = vtx;
81+
data.position = vtx;
8282
auto coverage_coords =
8383
(vtx - coverage_rect->origin) / coverage_rect->size;
8484
data.texture_coords =
@@ -101,21 +101,21 @@ bool TextureContents::Render(const ContentContext& renderer,
101101

102102
auto& host_buffer = pass.GetTransientsBuffer();
103103

104-
VS::FrameInfo frame_info;
105-
frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
106-
entity.GetTransformation();
107-
frame_info.alpha = opacity_;
104+
VS::VertInfo vert_info;
105+
vert_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
106+
entity.GetTransformation();
108107

109108
FS::FragInfo frag_info;
110109
frag_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
110+
frag_info.alpha = opacity_;
111111

112112
Command cmd;
113113
cmd.label = "TextureFill";
114114
cmd.pipeline =
115115
renderer.GetTexturePipeline(OptionsFromPassAndEntity(pass, entity));
116116
cmd.stencil_reference = entity.GetStencilDepth();
117117
cmd.BindVertices(vertex_builder.CreateVertexBuffer(host_buffer));
118-
VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info));
118+
VS::BindVertInfo(cmd, host_buffer.EmplaceUniform(vert_info));
119119
FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info));
120120
FS::BindTextureSampler(cmd, texture_,
121121
renderer.GetContext()->GetSamplerLibrary()->GetSampler(

impeller/entity/entity_unittests.cc

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,8 @@ TEST_P(EntityTest, BlendingModeOptions) {
708708
Rect rect, Color color,
709709
Entity::BlendMode blend_mode) -> bool {
710710
using VS = SolidFillPipeline::VertexShader;
711+
using FS = SolidFillPipeline::FragmentShader;
712+
711713
VertexBufferBuilder<VS::PerVertexData> vtx_builder;
712714
{
713715
auto r = rect.GetLTRB();
@@ -729,12 +731,16 @@ TEST_P(EntityTest, BlendingModeOptions) {
729731
cmd.BindVertices(
730732
vtx_builder.CreateVertexBuffer(pass.GetTransientsBuffer()));
731733

732-
VS::FrameInfo frame_info;
734+
VS::VertInfo frame_info;
733735
frame_info.mvp =
734736
Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * world_matrix;
735-
frame_info.color = color.Premultiply();
736-
VS::BindFrameInfo(cmd,
737-
pass.GetTransientsBuffer().EmplaceUniform(frame_info));
737+
VS::BindVertInfo(cmd,
738+
pass.GetTransientsBuffer().EmplaceUniform(frame_info));
739+
740+
FS::FragInfo frag_info;
741+
frag_info.color = color.Premultiply();
742+
FS::BindFragInfo(cmd,
743+
pass.GetTransientsBuffer().EmplaceUniform(frag_info));
738744

739745
cmd.primitive_type = PrimitiveType::kTriangle;
740746

impeller/entity/shaders/solid_fill.frag

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
in vec4 color;
5+
uniform FragInfo {
6+
vec4 color;
7+
}
8+
frag_info;
69

710
out vec4 frag_color;
811

912
void main() {
10-
frag_color = color;
13+
frag_color = frag_info.color;
1114
}

impeller/entity/shaders/solid_fill.vert

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
uniform FrameInfo {
5+
uniform VertInfo {
66
mat4 mvp;
7-
vec4 color;
8-
} frame_info;
9-
10-
in vec2 vertices;
7+
}
8+
vert_info;
119

12-
out vec4 color;
10+
in vec2 position;
1311

1412
void main() {
15-
gl_Position = frame_info.mvp * vec4(vertices, 0.0, 1.0);
16-
color = frame_info.color;
13+
gl_Position = vert_info.mvp * vec4(position, 0.0, 1.0);
1714
}

0 commit comments

Comments
 (0)