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

Commit afa093d

Browse files
authored
[Impeller] Map UVs correctly for TiledTextureContents (#38894)
1 parent c33c11c commit afa093d

File tree

8 files changed

+48
-30
lines changed

8 files changed

+48
-30
lines changed

impeller/aiks/aiks_unittests.cc

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ TEST_P(AiksTest, CanRenderTiledTexture) {
163163
contents->SetTexture(texture);
164164
contents->SetTileModes(x_tile_mode, y_tile_mode);
165165
contents->SetSamplerDescriptor(descriptor);
166-
contents->SetMatrix(matrix);
166+
contents->SetEffectTransform(matrix);
167167
return contents;
168168
};
169169
paint.color = Color(1, 1, 1, alpha);
@@ -347,7 +347,7 @@ TEST_P(AiksTest, CanRenderLinearGradient) {
347347
contents->SetColors(std::move(colors));
348348
contents->SetStops(std::move(stops));
349349
contents->SetTileMode(tile_mode);
350-
contents->SetMatrix(matrix);
350+
contents->SetEffectTransform(matrix);
351351
return contents;
352352
};
353353
paint.color = Color(1.0, 1.0, 1.0, alpha);
@@ -412,7 +412,7 @@ TEST_P(AiksTest, CanRenderLinearGradientManyColors) {
412412
contents->SetColors(std::move(colors));
413413
contents->SetStops(std::move(stops));
414414
contents->SetTileMode(tile_mode);
415-
contents->SetMatrix(matrix);
415+
contents->SetEffectTransform(matrix);
416416
return contents;
417417
};
418418
paint.color = Color(1.0, 1.0, 1.0, alpha);
@@ -468,7 +468,7 @@ TEST_P(AiksTest, CanRenderLinearGradientWayManyColors) {
468468
contents->SetColors(colors);
469469
contents->SetStops(stops);
470470
contents->SetTileMode(tile_mode);
471-
contents->SetMatrix(matrix);
471+
contents->SetEffectTransform(matrix);
472472
return contents;
473473
};
474474
canvas.DrawRect({0, 0, 600, 600}, paint);
@@ -525,7 +525,7 @@ TEST_P(AiksTest, CanRenderLinearGradientManyColorsUnevenStops) {
525525
contents->SetColors(std::move(colors));
526526
contents->SetStops(std::move(stops));
527527
contents->SetTileMode(tile_mode);
528-
contents->SetMatrix(matrix);
528+
contents->SetEffectTransform(matrix);
529529
return contents;
530530
};
531531
canvas.DrawRect({0, 0, 600, 600}, paint);
@@ -573,7 +573,7 @@ TEST_P(AiksTest, CanRenderRadialGradient) {
573573
contents->SetColors(std::move(colors));
574574
contents->SetStops(std::move(stops));
575575
contents->SetTileMode(tile_mode);
576-
contents->SetMatrix(matrix);
576+
contents->SetEffectTransform(matrix);
577577
return contents;
578578
};
579579
canvas.DrawRect({0, 0, 600, 600}, paint);
@@ -635,7 +635,7 @@ TEST_P(AiksTest, CanRenderRadialGradientManyColors) {
635635
contents->SetColors(std::move(colors));
636636
contents->SetStops(std::move(stops));
637637
contents->SetTileMode(tile_mode);
638-
contents->SetMatrix(matrix);
638+
contents->SetEffectTransform(matrix);
639639
return contents;
640640
};
641641
canvas.DrawRect({0, 0, 600, 600}, paint);
@@ -682,7 +682,7 @@ TEST_P(AiksTest, CanRenderSweepGradient) {
682682
contents->SetColors(std::move(colors));
683683
contents->SetStops(std::move(stops));
684684
contents->SetTileMode(tile_mode);
685-
contents->SetMatrix(matrix);
685+
contents->SetEffectTransform(matrix);
686686
return contents;
687687
};
688688
canvas.DrawRect({0, 0, 600, 600}, paint);
@@ -744,7 +744,7 @@ TEST_P(AiksTest, CanRenderSweepGradientManyColors) {
744744
contents->SetStops(std::move(stops));
745745
contents->SetColors(std::move(colors));
746746
contents->SetTileMode(tile_mode);
747-
contents->SetMatrix(matrix);
747+
contents->SetEffectTransform(matrix);
748748
return contents;
749749
};
750750
canvas.DrawRect({0, 0, 600, 600}, paint);
@@ -1509,7 +1509,7 @@ TEST_P(AiksTest, GradientStrokesRenderCorrectly) {
15091509
contents->SetColors(std::move(colors));
15101510
contents->SetStops(std::move(stops));
15111511
contents->SetTileMode(tile_mode);
1512-
contents->SetMatrix(matrix);
1512+
contents->SetEffectTransform(matrix);
15131513
return contents;
15141514
};
15151515

@@ -1718,21 +1718,30 @@ TEST_P(AiksTest, SceneColorSource) {
17181718
auto callback = [&](AiksContext& renderer, RenderTarget& render_target) {
17191719
Paint paint;
17201720

1721+
ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
1722+
static Scalar distance = 2;
1723+
ImGui::SliderFloat("Distance", &distance, 0, 4);
1724+
static Scalar y_pos = 0;
1725+
ImGui::SliderFloat("Y", &y_pos, -3, 3);
1726+
static Scalar fov = 45;
1727+
ImGui::SliderFloat("FOV", &fov, 1, 180);
1728+
ImGui::End();
1729+
17211730
paint.color_source_type = Paint::ColorSourceType::kScene;
1722-
paint.color_source = [this, gltf_scene]() {
1731+
paint.color_source = [&]() {
17231732
Scalar angle = GetSecondsElapsed();
1724-
Scalar distance = 2;
1725-
auto camera_position =
1726-
Vector3(distance * std::sin(angle), 2, -distance * std::cos(angle));
1733+
auto camera_position = Vector3(distance * std::sin(angle), y_pos,
1734+
-distance * std::cos(angle));
17271735
auto contents = std::make_shared<SceneContents>();
17281736
contents->SetNode(gltf_scene);
17291737
contents->SetCameraTransform(
1730-
Matrix::MakePerspective(Degrees(45), GetWindowSize(), 0.1, 1000) *
1738+
Matrix::MakePerspective(Degrees(fov), GetWindowSize(), 0.1, 1000) *
17311739
Matrix::MakeLookAt(camera_position, {0, 0, 0}, {0, 1, 0}));
17321740
return contents;
17331741
};
17341742

17351743
Canvas canvas;
1744+
canvas.DrawPaint(Paint{.color = Color::MakeRGBA8(0xf9, 0xf9, 0xf9, 0xff)});
17361745
canvas.Scale(GetContentScale());
17371746
canvas.DrawPaint(paint);
17381747
return renderer.Render(canvas.EndRecordingAsPicture(), render_target);

impeller/display_list/display_list_dispatcher.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ void DisplayListDispatcher::setColorSource(
394394
contents->SetStops(stops);
395395
contents->SetEndPoints(start_point, end_point);
396396
contents->SetTileMode(tile_mode);
397-
contents->SetMatrix(matrix);
397+
contents->SetEffectTransform(matrix);
398398
return contents;
399399
};
400400
return;
@@ -418,7 +418,7 @@ void DisplayListDispatcher::setColorSource(
418418
contents->SetStops(stops);
419419
contents->SetCenterAndRadius(center, radius);
420420
contents->SetTileMode(tile_mode);
421-
contents->SetMatrix(matrix);
421+
contents->SetEffectTransform(matrix);
422422
return contents;
423423
};
424424
return;
@@ -445,7 +445,7 @@ void DisplayListDispatcher::setColorSource(
445445
contents->SetColors(colors);
446446
contents->SetStops(stops);
447447
contents->SetTileMode(tile_mode);
448-
contents->SetMatrix(matrix);
448+
contents->SetEffectTransform(matrix);
449449
return contents;
450450
};
451451
return;
@@ -465,7 +465,7 @@ void DisplayListDispatcher::setColorSource(
465465
contents->SetTexture(texture);
466466
contents->SetTileModes(x_tile_mode, y_tile_mode);
467467
contents->SetSamplerDescriptor(desc);
468-
contents->SetMatrix(matrix);
468+
contents->SetEffectTransform(matrix);
469469
return contents;
470470
};
471471
return;

impeller/entity/contents/color_source_contents.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Scalar ColorSourceContents::GetAlpha() const {
2929
return alpha_;
3030
}
3131

32-
void ColorSourceContents::SetMatrix(Matrix matrix) {
32+
void ColorSourceContents::SetEffectTransform(Matrix matrix) {
3333
inverse_matrix_ = matrix.Invert();
3434
}
3535

impeller/entity/contents/color_source_contents.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ColorSourceContents : public Contents {
2020

2121
void SetGeometry(std::shared_ptr<Geometry> geometry);
2222

23-
void SetMatrix(Matrix matrix);
23+
void SetEffectTransform(Matrix matrix);
2424

2525
void SetAlpha(Scalar alpha);
2626

impeller/entity/contents/scene_contents.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ bool SceneContents::Render(const ContentContext& renderer,
7171
TiledTextureContents contents;
7272
contents.SetGeometry(GetGeometry());
7373
contents.SetTexture(subpass_target.GetRenderTargetTexture());
74-
contents.SetMatrix(
74+
contents.SetEffectTransform(
7575
Matrix::MakeScale(1 / entity.GetTransformation().GetScale()));
7676
return contents.Render(renderer, entity, pass);
7777
}

impeller/entity/contents/tiled_texture_contents.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,21 @@ bool TiledTextureContents::Render(const ContentContext& renderer,
5050

5151
auto& host_buffer = pass.GetTransientsBuffer();
5252

53+
auto geometry = GetGeometry();
5354
auto geometry_result =
5455
GetGeometry()->GetPositionBuffer(renderer, entity, pass);
5556

57+
// TODO(bdero): The geometry should be fetched from GetPositionUVBuffer and
58+
// contain coverage-mapped UVs, and this should use
59+
// position_uv.vert.
60+
// https://github.com/flutter/flutter/issues/118553
61+
5662
VS::VertInfo vert_info;
5763
vert_info.mvp = geometry_result.transform;
58-
vert_info.matrix = GetInverseMatrix();
59-
vert_info.texture_size = Vector2{static_cast<Scalar>(texture_size.width),
60-
static_cast<Scalar>(texture_size.height)};
64+
vert_info.effect_transform = GetInverseMatrix();
65+
vert_info.bounds_origin = geometry->GetCoverage(Matrix())->origin;
66+
vert_info.texture_size = Vector2(static_cast<Scalar>(texture_size.width),
67+
static_cast<Scalar>(texture_size.height));
6168

6269
FS::FragInfo frag_info;
6370
frag_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();

impeller/entity/contents/tiled_texture_contents.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ class TiledTextureContents final : public ColorSourceContents {
3636
private:
3737
std::shared_ptr<Texture> texture_;
3838
SamplerDescriptor sampler_descriptor_ = {};
39-
Entity::TileMode x_tile_mode_;
40-
Entity::TileMode y_tile_mode_;
39+
Entity::TileMode x_tile_mode_ = Entity::TileMode::kClamp;
40+
Entity::TileMode y_tile_mode_ = Entity::TileMode::kClamp;
4141

4242
FML_DISALLOW_COPY_AND_ASSIGN(TiledTextureContents);
4343
};

impeller/entity/shaders/tiled_texture_fill.vert

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
uniform VertInfo {
99
mat4 mvp;
10-
mat4 matrix;
10+
mat4 effect_transform;
11+
vec2 bounds_origin;
1112
vec2 texture_size;
1213
}
1314
vert_info;
@@ -18,6 +19,7 @@ out vec2 v_texture_coords;
1819

1920
void main() {
2021
gl_Position = vert_info.mvp * vec4(position, 0.0, 1.0);
21-
vec2 transformed = IPVec2TransformPosition(vert_info.matrix, position);
22-
v_texture_coords = transformed / vert_info.texture_size;
22+
v_texture_coords = IPVec2TransformPosition(
23+
vert_info.effect_transform,
24+
(position - vert_info.bounds_origin) / vert_info.texture_size);
2325
}

0 commit comments

Comments
 (0)