Skip to content

Commit 3ade737

Browse files
committed
Revert "Revert "[Impeller] Fix MatrixFilter multiplication ordering for subpasses." (flutter#43978)"
This reverts commit 176457d.
1 parent 176457d commit 3ade737

File tree

3 files changed

+73
-30
lines changed

3 files changed

+73
-30
lines changed

impeller/aiks/aiks_unittests.cc

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2908,15 +2908,22 @@ TEST_P(AiksTest, DrawPictureWithText) {
29082908

29092909
TEST_P(AiksTest, MatrixBackdropFilter) {
29102910
Canvas canvas;
2911-
canvas.SaveLayer({}, std::nullopt,
2912-
[](const FilterInput::Ref& input,
2913-
const Matrix& effect_transform, bool is_subpass) {
2914-
return FilterContents::MakeMatrixFilter(
2915-
input, Matrix::MakeTranslation(Vector2(100, 100)), {},
2916-
Matrix(), true);
2917-
});
2918-
canvas.DrawCircle(Point(100, 100), 100,
2919-
{.color = Color::Green(), .blend_mode = BlendMode::kPlus});
2911+
canvas.DrawPaint({.color = Color::Black()});
2912+
canvas.SaveLayer({}, std::nullopt);
2913+
{
2914+
canvas.DrawCircle(
2915+
Point(200, 200), 100,
2916+
{.color = Color::Green(), .blend_mode = BlendMode::kPlus});
2917+
// Should render a second intersecting circle, offset by 100, 100.
2918+
canvas.SaveLayer({}, std::nullopt,
2919+
[](const FilterInput::Ref& input,
2920+
const Matrix& effect_transform, bool is_subpass) {
2921+
return FilterContents::MakeMatrixFilter(
2922+
input, Matrix::MakeTranslation(Vector2(100, 100)),
2923+
{}, Matrix(), true);
2924+
});
2925+
canvas.Restore();
2926+
}
29202927
canvas.Restore();
29212928

29222929
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));

impeller/entity/contents/filters/matrix_filter_contents.cc

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,45 @@ std::optional<Entity> MatrixFilterContents::RenderFilter(
3434
return std::nullopt;
3535
}
3636

37-
auto& transform = is_subpass_ ? effect_transform : entity.GetTransformation();
38-
snapshot->transform = transform * //
39-
matrix_ * //
40-
transform.Invert() * //
41-
snapshot->transform;
37+
if (is_subpass_) {
38+
// There are two special quirks with how Matrix filters behave when used as
39+
// subpass backdrop filters:
40+
//
41+
// 1. For subpass backdrop filters, the snapshot transform is always just a
42+
// translation that positions the parent pass texture correctly relative
43+
// to the subpass texture. However, this translation always needs to be
44+
// applied in screen space.
45+
//
46+
// Since we know the snapshot transform will always have an identity
47+
// basis in this case, we safely reverse the order and apply the filter's
48+
// matrix within the snapshot transform space.
49+
//
50+
// 2. The filter's matrix needs to be applied within the space defined by
51+
// the scene's current transformation matrix (CTM). For example: If the
52+
// CTM is scaled up, then translations applied by the matrix should be
53+
// magnified accordingly.
54+
//
55+
// To accomplish this, we sandwitch the filter's matrix within the CTM in
56+
// both cases. But notice that for the subpass backdrop filter case, we
57+
// use the "effect transform" instead of the Entity's transform!
58+
//
59+
// That's because in the subpass backdrop filter case, the Entity's
60+
// transform isn't actually the captured CTM of the scene like it usually
61+
// is; instead, it's just a screen space translation that offsets the
62+
// backdrop texture (as mentioned above). And so we sneak the subpass's
63+
// captured CTM in through the effect transform.
64+
//
65+
66+
snapshot->transform = snapshot->transform * //
67+
effect_transform * //
68+
matrix_ * //
69+
effect_transform.Invert();
70+
} else {
71+
snapshot->transform = entity.GetTransformation() * //
72+
matrix_ * //
73+
entity.GetTransformation().Invert() * //
74+
snapshot->transform;
75+
}
4276
snapshot->sampler_descriptor = sampler_descriptor_;
4377
return Entity::FromSnapshot(snapshot, entity.GetBlendMode(),
4478
entity.GetStencilDepth());

impeller/entity/entity_pass.cc

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -465,12 +465,12 @@ EntityPass::EntityResult EntityPass::GetEntityForElement(
465465
return EntityPass::EntityResult::Skip();
466466
}
467467

468-
std::shared_ptr<Contents> backdrop_filter_contents = nullptr;
468+
std::shared_ptr<Contents> subpass_backdrop_filter_contents = nullptr;
469469
if (subpass->backdrop_filter_proc_) {
470470
auto texture = pass_context.GetTexture();
471471
// Render the backdrop texture before any of the pass elements.
472472
const auto& proc = subpass->backdrop_filter_proc_;
473-
backdrop_filter_contents =
473+
subpass_backdrop_filter_contents =
474474
proc(FilterInput::Make(std::move(texture)), subpass->xformation_,
475475
/*is_subpass*/ true);
476476

@@ -507,9 +507,10 @@ EntityPass::EntityResult EntityPass::GetEntityForElement(
507507
return EntityPass::EntityResult::Skip();
508508
}
509509

510-
auto subpass_coverage = (subpass->flood_clip_ || backdrop_filter_contents)
511-
? coverage_limit
512-
: GetSubpassCoverage(*subpass, coverage_limit);
510+
auto subpass_coverage =
511+
(subpass->flood_clip_ || subpass_backdrop_filter_contents)
512+
? coverage_limit
513+
: GetSubpassCoverage(*subpass, coverage_limit);
513514
if (!subpass_coverage.has_value()) {
514515
return EntityPass::EntityResult::Skip();
515516
}
@@ -532,17 +533,18 @@ EntityPass::EntityResult EntityPass::GetEntityForElement(
532533

533534
// Stencil textures aren't shared between EntityPasses (as much of the
534535
// time they are transient).
535-
if (!subpass->OnRender(renderer, // renderer
536-
root_pass_size, // root_pass_size
537-
subpass_target, // pass_target
538-
subpass_coverage->origin, // global_pass_position
539-
subpass_coverage->origin -
540-
global_pass_position, // local_pass_position
541-
++pass_depth, // pass_depth
542-
stencil_coverage_stack, // stencil_coverage_stack
543-
subpass->stencil_depth_, // stencil_depth_floor
544-
backdrop_filter_contents // backdrop_filter_contents
545-
)) {
536+
if (!subpass->OnRender(
537+
renderer, // renderer
538+
root_pass_size, // root_pass_size
539+
subpass_target, // pass_target
540+
subpass_coverage->origin, // global_pass_position
541+
subpass_coverage->origin -
542+
global_pass_position, // local_pass_position
543+
++pass_depth, // pass_depth
544+
stencil_coverage_stack, // stencil_coverage_stack
545+
subpass->stencil_depth_, // stencil_depth_floor
546+
subpass_backdrop_filter_contents // backdrop_filter_contents
547+
)) {
546548
// Validation error messages are triggered for all `OnRender()` failure
547549
// cases.
548550
return EntityPass::EntityResult::Failure();

0 commit comments

Comments
 (0)