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

[Impeller] Map UVs correctly for TiledTextureContents #38894

Merged
merged 3 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions impeller/aiks/aiks_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ TEST_P(AiksTest, CanRenderTiledTexture) {
contents->SetTexture(texture);
contents->SetTileModes(x_tile_mode, y_tile_mode);
contents->SetSamplerDescriptor(descriptor);
contents->SetMatrix(matrix);
contents->SetEffectTransform(matrix);
return contents;
};
paint.color = Color(1, 1, 1, alpha);
Expand Down Expand Up @@ -347,7 +347,7 @@ TEST_P(AiksTest, CanRenderLinearGradient) {
contents->SetColors(std::move(colors));
contents->SetStops(std::move(stops));
contents->SetTileMode(tile_mode);
contents->SetMatrix(matrix);
contents->SetEffectTransform(matrix);
return contents;
};
paint.color = Color(1.0, 1.0, 1.0, alpha);
Expand Down Expand Up @@ -412,7 +412,7 @@ TEST_P(AiksTest, CanRenderLinearGradientManyColors) {
contents->SetColors(std::move(colors));
contents->SetStops(std::move(stops));
contents->SetTileMode(tile_mode);
contents->SetMatrix(matrix);
contents->SetEffectTransform(matrix);
return contents;
};
paint.color = Color(1.0, 1.0, 1.0, alpha);
Expand Down Expand Up @@ -468,7 +468,7 @@ TEST_P(AiksTest, CanRenderLinearGradientWayManyColors) {
contents->SetColors(colors);
contents->SetStops(stops);
contents->SetTileMode(tile_mode);
contents->SetMatrix(matrix);
contents->SetEffectTransform(matrix);
return contents;
};
canvas.DrawRect({0, 0, 600, 600}, paint);
Expand Down Expand Up @@ -525,7 +525,7 @@ TEST_P(AiksTest, CanRenderLinearGradientManyColorsUnevenStops) {
contents->SetColors(std::move(colors));
contents->SetStops(std::move(stops));
contents->SetTileMode(tile_mode);
contents->SetMatrix(matrix);
contents->SetEffectTransform(matrix);
return contents;
};
canvas.DrawRect({0, 0, 600, 600}, paint);
Expand Down Expand Up @@ -573,7 +573,7 @@ TEST_P(AiksTest, CanRenderRadialGradient) {
contents->SetColors(std::move(colors));
contents->SetStops(std::move(stops));
contents->SetTileMode(tile_mode);
contents->SetMatrix(matrix);
contents->SetEffectTransform(matrix);
return contents;
};
canvas.DrawRect({0, 0, 600, 600}, paint);
Expand Down Expand Up @@ -635,7 +635,7 @@ TEST_P(AiksTest, CanRenderRadialGradientManyColors) {
contents->SetColors(std::move(colors));
contents->SetStops(std::move(stops));
contents->SetTileMode(tile_mode);
contents->SetMatrix(matrix);
contents->SetEffectTransform(matrix);
return contents;
};
canvas.DrawRect({0, 0, 600, 600}, paint);
Expand Down Expand Up @@ -682,7 +682,7 @@ TEST_P(AiksTest, CanRenderSweepGradient) {
contents->SetColors(std::move(colors));
contents->SetStops(std::move(stops));
contents->SetTileMode(tile_mode);
contents->SetMatrix(matrix);
contents->SetEffectTransform(matrix);
return contents;
};
canvas.DrawRect({0, 0, 600, 600}, paint);
Expand Down Expand Up @@ -744,7 +744,7 @@ TEST_P(AiksTest, CanRenderSweepGradientManyColors) {
contents->SetStops(std::move(stops));
contents->SetColors(std::move(colors));
contents->SetTileMode(tile_mode);
contents->SetMatrix(matrix);
contents->SetEffectTransform(matrix);
return contents;
};
canvas.DrawRect({0, 0, 600, 600}, paint);
Expand Down Expand Up @@ -1509,7 +1509,7 @@ TEST_P(AiksTest, GradientStrokesRenderCorrectly) {
contents->SetColors(std::move(colors));
contents->SetStops(std::move(stops));
contents->SetTileMode(tile_mode);
contents->SetMatrix(matrix);
contents->SetEffectTransform(matrix);
return contents;
};

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

ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
static Scalar distance = 2;
ImGui::SliderFloat("Distance", &distance, 0, 4);
static Scalar y_pos = 0;
ImGui::SliderFloat("Y", &y_pos, -3, 3);
static Scalar fov = 45;
ImGui::SliderFloat("FOV", &fov, 1, 180);
ImGui::End();

paint.color_source_type = Paint::ColorSourceType::kScene;
paint.color_source = [this, gltf_scene]() {
paint.color_source = [&]() {
Scalar angle = GetSecondsElapsed();
Scalar distance = 2;
auto camera_position =
Vector3(distance * std::sin(angle), 2, -distance * std::cos(angle));
auto camera_position = Vector3(distance * std::sin(angle), y_pos,
-distance * std::cos(angle));
auto contents = std::make_shared<SceneContents>();
contents->SetNode(gltf_scene);
contents->SetCameraTransform(
Matrix::MakePerspective(Degrees(45), GetWindowSize(), 0.1, 1000) *
Matrix::MakePerspective(Degrees(fov), GetWindowSize(), 0.1, 1000) *
Matrix::MakeLookAt(camera_position, {0, 0, 0}, {0, 1, 0}));
return contents;
};

Canvas canvas;
canvas.DrawPaint(Paint{.color = Color::MakeRGBA8(0xf9, 0xf9, 0xf9, 0xff)});
canvas.Scale(GetContentScale());
canvas.DrawPaint(paint);
return renderer.Render(canvas.EndRecordingAsPicture(), render_target);
Expand Down
8 changes: 4 additions & 4 deletions impeller/display_list/display_list_dispatcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ void DisplayListDispatcher::setColorSource(
contents->SetStops(stops);
contents->SetEndPoints(start_point, end_point);
contents->SetTileMode(tile_mode);
contents->SetMatrix(matrix);
contents->SetEffectTransform(matrix);
return contents;
};
return;
Expand All @@ -418,7 +418,7 @@ void DisplayListDispatcher::setColorSource(
contents->SetStops(stops);
contents->SetCenterAndRadius(center, radius);
contents->SetTileMode(tile_mode);
contents->SetMatrix(matrix);
contents->SetEffectTransform(matrix);
return contents;
};
return;
Expand All @@ -445,7 +445,7 @@ void DisplayListDispatcher::setColorSource(
contents->SetColors(colors);
contents->SetStops(stops);
contents->SetTileMode(tile_mode);
contents->SetMatrix(matrix);
contents->SetEffectTransform(matrix);
return contents;
};
return;
Expand All @@ -465,7 +465,7 @@ void DisplayListDispatcher::setColorSource(
contents->SetTexture(texture);
contents->SetTileModes(x_tile_mode, y_tile_mode);
contents->SetSamplerDescriptor(desc);
contents->SetMatrix(matrix);
contents->SetEffectTransform(matrix);
return contents;
};
return;
Expand Down
2 changes: 1 addition & 1 deletion impeller/entity/contents/color_source_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Scalar ColorSourceContents::GetAlpha() const {
return alpha_;
}

void ColorSourceContents::SetMatrix(Matrix matrix) {
void ColorSourceContents::SetEffectTransform(Matrix matrix) {
inverse_matrix_ = matrix.Invert();
}

Expand Down
2 changes: 1 addition & 1 deletion impeller/entity/contents/color_source_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ColorSourceContents : public Contents {

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

void SetMatrix(Matrix matrix);
void SetEffectTransform(Matrix matrix);

void SetAlpha(Scalar alpha);

Expand Down
2 changes: 1 addition & 1 deletion impeller/entity/contents/scene_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ bool SceneContents::Render(const ContentContext& renderer,
TiledTextureContents contents;
contents.SetGeometry(GetGeometry());
contents.SetTexture(subpass_target.GetRenderTargetTexture());
contents.SetMatrix(
contents.SetEffectTransform(
Matrix::MakeScale(1 / entity.GetTransformation().GetScale()));
return contents.Render(renderer, entity, pass);
}
Expand Down
13 changes: 10 additions & 3 deletions impeller/entity/contents/tiled_texture_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,21 @@ bool TiledTextureContents::Render(const ContentContext& renderer,

auto& host_buffer = pass.GetTransientsBuffer();

auto geometry = GetGeometry();
auto geometry_result =
GetGeometry()->GetPositionBuffer(renderer, entity, pass);

// TODO(bdero): The geometry should be fetched from GetPositionUVBuffer and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a link to an issue here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

// contain coverage-mapped UVs, and this should use
// position_uv.vert.
// https://github.com/flutter/flutter/issues/118553

VS::VertInfo vert_info;
vert_info.mvp = geometry_result.transform;
vert_info.matrix = GetInverseMatrix();
vert_info.texture_size = Vector2{static_cast<Scalar>(texture_size.width),
static_cast<Scalar>(texture_size.height)};
vert_info.effect_transform = GetInverseMatrix();
vert_info.bounds_origin = geometry->GetCoverage(Matrix())->origin;
vert_info.texture_size = Vector2(static_cast<Scalar>(texture_size.width),
static_cast<Scalar>(texture_size.height));

FS::FragInfo frag_info;
frag_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
Expand Down
4 changes: 2 additions & 2 deletions impeller/entity/contents/tiled_texture_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class TiledTextureContents final : public ColorSourceContents {
private:
std::shared_ptr<Texture> texture_;
SamplerDescriptor sampler_descriptor_ = {};
Entity::TileMode x_tile_mode_;
Entity::TileMode y_tile_mode_;
Entity::TileMode x_tile_mode_ = Entity::TileMode::kClamp;
Entity::TileMode y_tile_mode_ = Entity::TileMode::kClamp;

FML_DISALLOW_COPY_AND_ASSIGN(TiledTextureContents);
};
Expand Down
8 changes: 5 additions & 3 deletions impeller/entity/shaders/tiled_texture_fill.vert
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

uniform VertInfo {
mat4 mvp;
mat4 matrix;
mat4 effect_transform;
vec2 bounds_origin;
vec2 texture_size;
}
vert_info;
Expand All @@ -18,6 +19,7 @@ out vec2 v_texture_coords;

void main() {
gl_Position = vert_info.mvp * vec4(position, 0.0, 1.0);
vec2 transformed = IPVec2TransformPosition(vert_info.matrix, position);
v_texture_coords = transformed / vert_info.texture_size;
v_texture_coords = IPVec2TransformPosition(
vert_info.effect_transform,
(position - vert_info.bounds_origin) / vert_info.texture_size);
}