Skip to content

Commit 7e5f105

Browse files
authored
[Impeller] Fix perspective clips with a large perspective bias (#181434)
Fixes: #173104 We were precomputing the perspective adjustment inside the clipping vertex shader, which prevented the GPU from performing correct clipping against the camera plane.
1 parent 8eb2fac commit 7e5f105

4 files changed

Lines changed: 77 additions & 27 deletions

File tree

‎engine/src/flutter/impeller/display_list/aiks_dl_basic_unittests.cc‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2237,5 +2237,56 @@ TEST_P(AiksTest, BackdropFilterOverUnclosedClip) {
22372237
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
22382238
}
22392239

2240+
TEST_P(AiksTest, PerspectiveRectangle) {
2241+
int perspective = 58;
2242+
bool use_clip = true;
2243+
bool diff_clip = false;
2244+
2245+
auto callback = [&]() -> sk_sp<DisplayList> {
2246+
if (AiksTest::ImGuiBegin("Controls", nullptr,
2247+
ImGuiWindowFlags_AlwaysAutoResize)) {
2248+
ImGui::SliderInt("perspective%", &perspective, 0, 100);
2249+
ImGui::Checkbox("use clip", &use_clip);
2250+
if (use_clip) {
2251+
ImGui::Checkbox("diff clip", &diff_clip);
2252+
}
2253+
ImGui::SetWindowPos("Controls", ImVec2(500, 100));
2254+
ImGui::End();
2255+
}
2256+
2257+
DisplayListBuilder builder;
2258+
2259+
Scalar val = perspective * -0.00005f;
2260+
builder.TransformFullPerspective(
2261+
// clang-format off
2262+
1.0f, 0.0f, 0.0f, 400.0f,
2263+
0.0f, 1.0f, 0.0f, 400.0f,
2264+
0.0f, 0.0f, 1.0f, 0.0f,
2265+
0.0f, val, 0.0f, 2.2f
2266+
// clang-format on
2267+
);
2268+
2269+
if (use_clip) {
2270+
Rect clip = DlRect::MakeLTRB(0, 0, 400, 800);
2271+
DlClipOp clip_op = DlClipOp::kIntersect;
2272+
if (diff_clip) {
2273+
clip = clip.Expand(-20);
2274+
clip_op = DlClipOp::kDifference;
2275+
}
2276+
builder.ClipRect(clip, clip_op);
2277+
}
2278+
2279+
DlPaint paint;
2280+
paint.setColor(DlColor::kBlue());
2281+
builder.DrawRect(DlRect::MakeLTRB(0, 0, 400, 800), paint);
2282+
2283+
builder.DrawColor(DlColor::kWhite().withAlphaF(0.5f),
2284+
DlBlendMode::kSrcOver);
2285+
2286+
return builder.Build();
2287+
};
2288+
ASSERT_TRUE(OpenPlaygroundHere(callback));
2289+
}
2290+
22402291
} // namespace testing
22412292
} // namespace impeller

‎engine/src/flutter/impeller/entity/geometry/rect_geometry.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ GeometryResult FillRectGeometry::GetPositionBuffer(
3131

3232
std::optional<Rect> FillRectGeometry::GetCoverage(
3333
const Matrix& transform) const {
34-
return rect_.TransformBounds(transform);
34+
return rect_.TransformAndClipBounds(transform);
3535
}
3636

3737
bool FillRectGeometry::CoversArea(const Matrix& transform,

‎engine/src/flutter/impeller/entity/shaders/clip.vert‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ in vec2 position;
1414

1515
void main() {
1616
gl_Position = frame_info.mvp * vec4(position, 0.0, 1.0);
17-
// We can just absorb W and override the depth value here because we don't
18-
// need to worry about perspective correcting any vertex attributes when
19-
// drawing clips.
20-
gl_Position /= gl_Position.w;
21-
gl_Position.z = frame_info.depth;
17+
// We can just override the depth value (relative to W) here because we
18+
// don't need to worry about perspective correcting any vertex attributes
19+
// when drawing clips.
20+
gl_Position.z = frame_info.depth * gl_Position.w;
2221
}

‎engine/src/flutter/impeller/tools/malioc.json‎

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -530,10 +530,10 @@
530530
"load_store"
531531
],
532532
"longest_path_cycles": [
533-
0.140625,
534-
0.140625,
535-
0.015625,
536-
0.0625,
533+
0.109375,
534+
0.109375,
535+
0.0,
536+
0.0,
537537
2.0,
538538
0.0
539539
],
@@ -549,21 +549,21 @@
549549
"load_store"
550550
],
551551
"shortest_path_cycles": [
552-
0.140625,
553-
0.140625,
554-
0.015625,
555-
0.0625,
552+
0.109375,
553+
0.109375,
554+
0.0,
555+
0.0,
556556
2.0,
557557
0.0
558558
],
559559
"total_bound_pipelines": [
560560
"load_store"
561561
],
562562
"total_cycles": [
563-
0.140625,
564-
0.140625,
565-
0.015625,
566-
0.0625,
563+
0.109375,
564+
0.109375,
565+
0.0,
566+
0.0,
567567
2.0,
568568
0.0
569569
]
@@ -2577,10 +2577,10 @@
25772577
"load_store"
25782578
],
25792579
"longest_path_cycles": [
2580-
0.15625,
2581-
0.15625,
2580+
0.109375,
2581+
0.109375,
2582+
0.0,
25822583
0.0,
2583-
0.0625,
25842584
2.0,
25852585
0.0
25862586
],
@@ -2596,21 +2596,21 @@
25962596
"load_store"
25972597
],
25982598
"shortest_path_cycles": [
2599-
0.15625,
2600-
0.15625,
2599+
0.109375,
2600+
0.109375,
2601+
0.0,
26012602
0.0,
2602-
0.0625,
26032603
2.0,
26042604
0.0
26052605
],
26062606
"total_bound_pipelines": [
26072607
"load_store"
26082608
],
26092609
"total_cycles": [
2610-
0.15625,
2611-
0.15625,
2610+
0.109375,
2611+
0.109375,
2612+
0.0,
26122613
0.0,
2613-
0.0625,
26142614
2.0,
26152615
0.0
26162616
]

0 commit comments

Comments
 (0)