From d0ec73496f3adf4b34fbe7e739d0b707b78bab1d Mon Sep 17 00:00:00 2001 From: Aaron Clarke Date: Thu, 30 May 2024 13:26:23 -0700 Subject: [PATCH 01/13] [Impeller] match rrect_blur math to the gaussian math --- impeller/display_list/dl_golden_unittests.cc | 37 +++++++++++++++++++ .../contents/solid_rrect_blur_contents.cc | 15 +++++--- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/impeller/display_list/dl_golden_unittests.cc b/impeller/display_list/dl_golden_unittests.cc index 89f04aa4b55ea..524894938a5b6 100644 --- a/impeller/display_list/dl_golden_unittests.cc +++ b/impeller/display_list/dl_golden_unittests.cc @@ -14,6 +14,7 @@ namespace testing { using impeller::PlaygroundBackend; using impeller::PlaygroundTest; using impeller::Point; +using impeller::Scalar; INSTANTIATE_PLAYGROUND_SUITE(DlGoldenTest); @@ -100,5 +101,41 @@ TEST_P(DlGoldenTest, Bug147807) { ASSERT_TRUE(OpenPlaygroundHere(builder.Build())); } +TEST_P(DlGoldenTest, GaussianVsRRectBlur) { + Point content_scale = GetContentScale(); + auto draw = [content_scale](DlCanvas* canvas, + const std::vector>& images) { + canvas->Transform2DAffine(content_scale.x, 0, 0, 0, content_scale.y, 0); + canvas->DrawPaint(DlPaint().setColor(DlColor(0xff112233))); + DlPaint paint; + paint.setColor(DlColor(0xfffef7ff)); + Scalar width = 150; + Scalar height = 150; + Scalar gap = 80; + std::vector blur_radii = {10, 30, 50}; + for (size_t i = 0; i < blur_radii.size(); ++i) { + Scalar blur_radius = blur_radii[i]; + auto blur_filter = std::make_shared( + flutter::DlBlurStyle::kNormal, blur_radius); + paint.setMaskFilter(blur_filter); + SkRRect rrect; + Scalar yval = gap + i * (gap + height); + rrect.setNinePatch(SkRect::MakeXYWH(gap, yval, width, height), 10, 10, 10, + 10); + canvas->DrawRRect(rrect, paint); + rrect.setNinePatch( + SkRect::MakeXYWH(2.0 * gap + width, yval, width, height), 9, 10, 10, + 10); + canvas->DrawRRect(rrect, paint); + } + }; + + DisplayListBuilder builder; + std::vector> images; + draw(&builder, images); + + ASSERT_TRUE(OpenPlaygroundHere(builder.Build())); +} + } // namespace testing } // namespace flutter diff --git a/impeller/entity/contents/solid_rrect_blur_contents.cc b/impeller/entity/contents/solid_rrect_blur_contents.cc index b6bdffd2b7d81..6dc27bad3a719 100644 --- a/impeller/entity/contents/solid_rrect_blur_contents.cc +++ b/impeller/entity/contents/solid_rrect_blur_contents.cc @@ -6,6 +6,7 @@ #include #include "impeller/entity/contents/content_context.h" +#include "impeller/entity/contents/filters/gaussian_blur_filter_contents.h" #include "impeller/entity/entity.h" #include "impeller/geometry/color.h" #include "impeller/geometry/constants.h" @@ -48,13 +49,20 @@ Color SolidRRectBlurContents::GetColor() const { return color_; } +namespace { +Scalar ScaleSigma(Scalar sigma) { + // 0.5 comes from the math in `blur_rrect.frag`. + return 0.5 * GaussianBlurFilterContents::ScaleSigma(sigma); +} +} // namespace + std::optional SolidRRectBlurContents::GetCoverage( const Entity& entity) const { if (!rect_.has_value()) { return std::nullopt; } - Scalar radius = PadForSigma(sigma_.sigma); + Scalar radius = PadForSigma(ScaleSigma(sigma_.sigma)); return rect_->Expand(radius).TransformBounds(entity.GetTransform()); } @@ -71,10 +79,7 @@ bool SolidRRectBlurContents::Render(const ContentContext& renderer, VertexBufferBuilder vtx_builder; - // Clamp the max kernel width/height to 1000 to limit the extent - // of the blur and to kEhCloseEnough to prevent NaN calculations - // trying to evaluate a Guassian distribution with a sigma of 0. - auto blur_sigma = std::clamp(sigma_.sigma, kEhCloseEnough, 250.0f); + auto blur_sigma = ScaleSigma(sigma_.sigma); // Increase quality by making the radius a bit bigger than the typical // sigma->radius conversion we use for slower blurs. auto blur_radius = PadForSigma(blur_sigma); From ee4dd2bf5b906cb2bc7d0cc504dfff9cd9f5aa35 Mon Sep 17 00:00:00 2001 From: Aaron Clarke Date: Thu, 30 May 2024 14:00:40 -0700 Subject: [PATCH 02/13] update golden --- testing/impeller_golden_tests_output.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/testing/impeller_golden_tests_output.txt b/testing/impeller_golden_tests_output.txt index fb2b34e52ff03..8c7631b0c2999 100644 --- a/testing/impeller_golden_tests_output.txt +++ b/testing/impeller_golden_tests_output.txt @@ -798,3 +798,6 @@ impeller_Play_DlGoldenTest_CanDrawPaint_Vulkan.png impeller_Play_DlGoldenTest_CanRenderImage_Metal.png impeller_Play_DlGoldenTest_CanRenderImage_OpenGLES.png impeller_Play_DlGoldenTest_CanRenderImage_Vulkan.png +impeller_Play_DlGoldenTest_GaussianVsRRectBlur_Metal.png +impeller_Play_DlGoldenTest_GaussianVsRRectBlur_OpenGLES.png +impeller_Play_DlGoldenTest_GaussianVsRRectBlur_Vulkan.png From d63d3111a74e0fc01d025ab972301aaf0b2a41d9 Mon Sep 17 00:00:00 2001 From: Aaron Clarke Date: Fri, 31 May 2024 09:54:28 -0700 Subject: [PATCH 03/13] added scaled golden to show differences there --- impeller/display_list/dl_golden_unittests.cc | 64 +++++++++++++------- 1 file changed, 43 insertions(+), 21 deletions(-) diff --git a/impeller/display_list/dl_golden_unittests.cc b/impeller/display_list/dl_golden_unittests.cc index 524894938a5b6..b736b96e528c0 100644 --- a/impeller/display_list/dl_golden_unittests.cc +++ b/impeller/display_list/dl_golden_unittests.cc @@ -101,33 +101,55 @@ TEST_P(DlGoldenTest, Bug147807) { ASSERT_TRUE(OpenPlaygroundHere(builder.Build())); } +namespace { +void DrawBlurGrid(DlCanvas* canvas) { + DlPaint paint; + paint.setColor(DlColor(0xfffef7ff)); + Scalar width = 150; + Scalar height = 150; + Scalar gap = 80; + std::vector blur_radii = {10, 30, 50}; + for (size_t i = 0; i < blur_radii.size(); ++i) { + Scalar blur_radius = blur_radii[i]; + auto blur_filter = std::make_shared( + flutter::DlBlurStyle::kNormal, blur_radius); + paint.setMaskFilter(blur_filter); + SkRRect rrect; + Scalar yval = gap + i * (gap + height); + rrect.setNinePatch(SkRect::MakeXYWH(gap, yval, width, height), 10, 10, 10, + 10); + canvas->DrawRRect(rrect, paint); + rrect.setNinePatch(SkRect::MakeXYWH(2.0 * gap + width, yval, width, height), + 9, 10, 10, 10); + canvas->DrawRRect(rrect, paint); + } +} +} // namespace + TEST_P(DlGoldenTest, GaussianVsRRectBlur) { Point content_scale = GetContentScale(); auto draw = [content_scale](DlCanvas* canvas, const std::vector>& images) { canvas->Transform2DAffine(content_scale.x, 0, 0, 0, content_scale.y, 0); canvas->DrawPaint(DlPaint().setColor(DlColor(0xff112233))); - DlPaint paint; - paint.setColor(DlColor(0xfffef7ff)); - Scalar width = 150; - Scalar height = 150; - Scalar gap = 80; - std::vector blur_radii = {10, 30, 50}; - for (size_t i = 0; i < blur_radii.size(); ++i) { - Scalar blur_radius = blur_radii[i]; - auto blur_filter = std::make_shared( - flutter::DlBlurStyle::kNormal, blur_radius); - paint.setMaskFilter(blur_filter); - SkRRect rrect; - Scalar yval = gap + i * (gap + height); - rrect.setNinePatch(SkRect::MakeXYWH(gap, yval, width, height), 10, 10, 10, - 10); - canvas->DrawRRect(rrect, paint); - rrect.setNinePatch( - SkRect::MakeXYWH(2.0 * gap + width, yval, width, height), 9, 10, 10, - 10); - canvas->DrawRRect(rrect, paint); - } + DrawBlurGrid(canvas); + }; + + DisplayListBuilder builder; + std::vector> images; + draw(&builder, images); + + ASSERT_TRUE(OpenPlaygroundHere(builder.Build())); +} + +TEST_P(DlGoldenTest, GaussianVsRRectBlurScaled) { + Point content_scale = GetContentScale(); + auto draw = [content_scale](DlCanvas* canvas, + const std::vector>& images) { + canvas->Transform2DAffine(content_scale.x, 0, 0, 0, content_scale.y, 0); + canvas->DrawPaint(DlPaint().setColor(DlColor(0xff112233))); + canvas->Transform2DAffine(0.5, 0, 0, 0, 0.5, 0); + DrawBlurGrid(canvas); }; DisplayListBuilder builder; From 8a0e91fe34a4bca0f526465f6a067acf4f2f2ca1 Mon Sep 17 00:00:00 2001 From: Aaron Clarke Date: Fri, 31 May 2024 10:09:49 -0700 Subject: [PATCH 04/13] fixed gaussian blur sigma scaling --- impeller/display_list/dl_golden_unittests.cc | 2 +- .../contents/filters/gaussian_blur_filter_contents.cc | 7 ++++--- impeller/entity/contents/solid_rrect_blur_contents.cc | 3 +-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/impeller/display_list/dl_golden_unittests.cc b/impeller/display_list/dl_golden_unittests.cc index b736b96e528c0..f83f5bb2efc0c 100644 --- a/impeller/display_list/dl_golden_unittests.cc +++ b/impeller/display_list/dl_golden_unittests.cc @@ -148,7 +148,7 @@ TEST_P(DlGoldenTest, GaussianVsRRectBlurScaled) { const std::vector>& images) { canvas->Transform2DAffine(content_scale.x, 0, 0, 0, content_scale.y, 0); canvas->DrawPaint(DlPaint().setColor(DlColor(0xff112233))); - canvas->Transform2DAffine(0.5, 0, 0, 0, 0.5, 0); + canvas->Transform2DAffine(0.33, 0, 0, 0, 0.33, 0); DrawBlurGrid(canvas); }; diff --git a/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc b/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc index c6d5e54a8ba14..ad5a73b05cb66 100644 --- a/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc +++ b/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc @@ -384,9 +384,10 @@ std::optional GaussianBlurFilterContents::RenderFilter( return std::nullopt; } - Vector2 scaled_sigma = (effect_transform.Basis() * - Vector2(ScaleSigma(sigma_x_), ScaleSigma(sigma_y_))) - .Abs(); + Vector2 scaled_sigma = + (effect_transform.Basis() * entity.GetTransform().Basis() * + Vector2(ScaleSigma(sigma_x_), ScaleSigma(sigma_y_))) + .Abs(); Vector2 blur_radius = Vector2(CalculateBlurRadius(scaled_sigma.x), CalculateBlurRadius(scaled_sigma.y)); Vector2 padding(ceil(blur_radius.x), ceil(blur_radius.y)); diff --git a/impeller/entity/contents/solid_rrect_blur_contents.cc b/impeller/entity/contents/solid_rrect_blur_contents.cc index 6dc27bad3a719..0709861fe940a 100644 --- a/impeller/entity/contents/solid_rrect_blur_contents.cc +++ b/impeller/entity/contents/solid_rrect_blur_contents.cc @@ -51,8 +51,7 @@ Color SolidRRectBlurContents::GetColor() const { namespace { Scalar ScaleSigma(Scalar sigma) { - // 0.5 comes from the math in `blur_rrect.frag`. - return 0.5 * GaussianBlurFilterContents::ScaleSigma(sigma); + return GaussianBlurFilterContents::ScaleSigma(sigma); } } // namespace From 05b43d8778567194ab3b7a0dd91f15514fd473d0 Mon Sep 17 00:00:00 2001 From: Aaron Clarke Date: Fri, 31 May 2024 10:45:51 -0700 Subject: [PATCH 05/13] updated coverage calculation --- .../contents/filters/gaussian_blur_filter_contents.cc | 7 ++++--- .../filters/gaussian_blur_filter_contents_unittests.cc | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc b/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc index ad5a73b05cb66..04039c108df54 100644 --- a/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc +++ b/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc @@ -363,9 +363,10 @@ std::optional GaussianBlurFilterContents::GetFilterCoverage( return {}; } - Vector2 scaled_sigma = (effect_transform.Basis() * - Vector2(ScaleSigma(sigma_x_), ScaleSigma(sigma_y_))) - .Abs(); + Vector2 scaled_sigma = + (effect_transform.Basis() * entity.GetTransform().Basis() * + Vector2(ScaleSigma(sigma_x_), ScaleSigma(sigma_y_))) + .Abs(); Vector2 blur_radius = Vector2(CalculateBlurRadius(scaled_sigma.x), CalculateBlurRadius(scaled_sigma.y)); Vector2 padding(ceil(blur_radius.x), ceil(blur_radius.y)); diff --git a/impeller/entity/contents/filters/gaussian_blur_filter_contents_unittests.cc b/impeller/entity/contents/filters/gaussian_blur_filter_contents_unittests.cc index ec64d46cf1450..dad6c5bc0ba6d 100644 --- a/impeller/entity/contents/filters/gaussian_blur_filter_contents_unittests.cc +++ b/impeller/entity/contents/filters/gaussian_blur_filter_contents_unittests.cc @@ -400,7 +400,7 @@ TEST_P(GaussianBlurFilterContentsTest, if (result_coverage.has_value() && contents_coverage.has_value()) { EXPECT_TRUE(RectNear(result_coverage.value(), contents_coverage.value())); EXPECT_TRUE(RectNear(contents_coverage.value(), - Rect::MakeLTRB(98.f, 78.f, 302.f, 282.f))); + Rect::MakeXYWH(94.f, 74.f, 212.f, 212.f))); } } } From 761bd4dc019c85bd5f96d1c88e82eee8fc842dda Mon Sep 17 00:00:00 2001 From: Aaron Clarke Date: Fri, 31 May 2024 11:10:30 -0700 Subject: [PATCH 06/13] updated golden --- testing/impeller_golden_tests_output.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/testing/impeller_golden_tests_output.txt b/testing/impeller_golden_tests_output.txt index 8c7631b0c2999..ea940dd65e102 100644 --- a/testing/impeller_golden_tests_output.txt +++ b/testing/impeller_golden_tests_output.txt @@ -798,6 +798,9 @@ impeller_Play_DlGoldenTest_CanDrawPaint_Vulkan.png impeller_Play_DlGoldenTest_CanRenderImage_Metal.png impeller_Play_DlGoldenTest_CanRenderImage_OpenGLES.png impeller_Play_DlGoldenTest_CanRenderImage_Vulkan.png +impeller_Play_DlGoldenTest_GaussianVsRRectBlurScaled_Metal.png +impeller_Play_DlGoldenTest_GaussianVsRRectBlurScaled_OpenGLES.png +impeller_Play_DlGoldenTest_GaussianVsRRectBlurScaled_Vulkan.png impeller_Play_DlGoldenTest_GaussianVsRRectBlur_Metal.png impeller_Play_DlGoldenTest_GaussianVsRRectBlur_OpenGLES.png impeller_Play_DlGoldenTest_GaussianVsRRectBlur_Vulkan.png From f23f8518782a3780dca951a93408bfd84bb34844 Mon Sep 17 00:00:00 2001 From: Aaron Clarke Date: Fri, 31 May 2024 13:06:57 -0700 Subject: [PATCH 07/13] jim feedback --- impeller/display_list/dl_golden_unittests.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/impeller/display_list/dl_golden_unittests.cc b/impeller/display_list/dl_golden_unittests.cc index f83f5bb2efc0c..5d63721fa9582 100644 --- a/impeller/display_list/dl_golden_unittests.cc +++ b/impeller/display_list/dl_golden_unittests.cc @@ -56,7 +56,7 @@ TEST_P(DlGoldenTest, Bug147807) { Point content_scale = GetContentScale(); auto draw = [content_scale](DlCanvas* canvas, const std::vector>& images) { - canvas->Transform2DAffine(content_scale.x, 0, 0, 0, content_scale.y, 0); + canvas->Scale(content_scale.x, content_scale.y); DlPaint paint; paint.setColor(DlColor(0xfffef7ff)); canvas->DrawRect(SkRect::MakeLTRB(0, 0, 375, 667), paint); @@ -130,7 +130,7 @@ TEST_P(DlGoldenTest, GaussianVsRRectBlur) { Point content_scale = GetContentScale(); auto draw = [content_scale](DlCanvas* canvas, const std::vector>& images) { - canvas->Transform2DAffine(content_scale.x, 0, 0, 0, content_scale.y, 0); + canvas->Scale(content_scale.x, content_scale.y); canvas->DrawPaint(DlPaint().setColor(DlColor(0xff112233))); DrawBlurGrid(canvas); }; @@ -146,9 +146,9 @@ TEST_P(DlGoldenTest, GaussianVsRRectBlurScaled) { Point content_scale = GetContentScale(); auto draw = [content_scale](DlCanvas* canvas, const std::vector>& images) { - canvas->Transform2DAffine(content_scale.x, 0, 0, 0, content_scale.y, 0); + canvas->Scale(content_scale.x, content_scale.y); canvas->DrawPaint(DlPaint().setColor(DlColor(0xff112233))); - canvas->Transform2DAffine(0.33, 0, 0, 0, 0.33, 0); + canvas->Scale(0.33, 0.33); DrawBlurGrid(canvas); }; From b511584b5fcb4a0145017600c5d5624abd887e59 Mon Sep 17 00:00:00 2001 From: Aaron Clarke Date: Fri, 31 May 2024 14:24:40 -0700 Subject: [PATCH 08/13] jim feedback 2 --- impeller/display_list/dl_golden_unittests.cc | 21 +++++++++++++++++++ .../filters/gaussian_blur_filter_contents.cc | 10 +++++---- testing/impeller_golden_tests_output.txt | 3 +++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/impeller/display_list/dl_golden_unittests.cc b/impeller/display_list/dl_golden_unittests.cc index 5d63721fa9582..a0c3af88c04e9 100644 --- a/impeller/display_list/dl_golden_unittests.cc +++ b/impeller/display_list/dl_golden_unittests.cc @@ -159,5 +159,26 @@ TEST_P(DlGoldenTest, GaussianVsRRectBlurScaled) { ASSERT_TRUE(OpenPlaygroundHere(builder.Build())); } +TEST_P(DlGoldenTest, GaussianVsRRectBlurScaledRotated) { + Point content_scale = GetContentScale(); + auto draw = [content_scale](DlCanvas* canvas, + const std::vector>& images) { + canvas->Scale(content_scale.x, content_scale.y); + canvas->Translate(200, 200); + canvas->DrawPaint(DlPaint().setColor(DlColor(0xff112233))); + canvas->Scale(0.33, 0.33); + canvas->Translate(300, 300); + canvas->Rotate(45); + canvas->Translate(-300, -300); + DrawBlurGrid(canvas); + }; + + DisplayListBuilder builder; + std::vector> images; + draw(&builder, images); + + ASSERT_TRUE(OpenPlaygroundHere(builder.Build())); +} + } // namespace testing } // namespace flutter diff --git a/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc b/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc index 04039c108df54..3780c57a98916 100644 --- a/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc +++ b/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc @@ -385,10 +385,12 @@ std::optional GaussianBlurFilterContents::RenderFilter( return std::nullopt; } - Vector2 scaled_sigma = - (effect_transform.Basis() * entity.GetTransform().Basis() * - Vector2(ScaleSigma(sigma_x_), ScaleSigma(sigma_y_))) - .Abs(); + Vector2 entity_scale_x = entity.GetTransform().Basis() * Vector2(1.0, 0.0); + Vector2 entity_scale_y = entity.GetTransform().Basis() * Vector2(0.0, 1.0); + Vector2 scaled_sigma = (Matrix::MakeScale({entity_scale_x.GetLength(), + entity_scale_y.GetLength(), 1.0}) * + Vector2(ScaleSigma(sigma_x_), ScaleSigma(sigma_y_))) + .Abs(); Vector2 blur_radius = Vector2(CalculateBlurRadius(scaled_sigma.x), CalculateBlurRadius(scaled_sigma.y)); Vector2 padding(ceil(blur_radius.x), ceil(blur_radius.y)); diff --git a/testing/impeller_golden_tests_output.txt b/testing/impeller_golden_tests_output.txt index ea940dd65e102..0fd9d0534e3c4 100644 --- a/testing/impeller_golden_tests_output.txt +++ b/testing/impeller_golden_tests_output.txt @@ -801,6 +801,9 @@ impeller_Play_DlGoldenTest_CanRenderImage_Vulkan.png impeller_Play_DlGoldenTest_GaussianVsRRectBlurScaled_Metal.png impeller_Play_DlGoldenTest_GaussianVsRRectBlurScaled_OpenGLES.png impeller_Play_DlGoldenTest_GaussianVsRRectBlurScaled_Vulkan.png +impeller_Play_DlGoldenTest_GaussianVsRRectBlurScaledRotated_Metal.png +impeller_Play_DlGoldenTest_GaussianVsRRectBlurScaledRotated_OpenGLES.png +impeller_Play_DlGoldenTest_GaussianVsRRectBlurScaledRotated_Vulkan.png impeller_Play_DlGoldenTest_GaussianVsRRectBlur_Metal.png impeller_Play_DlGoldenTest_GaussianVsRRectBlur_OpenGLES.png impeller_Play_DlGoldenTest_GaussianVsRRectBlur_Vulkan.png From ace336eace444d9e3ec2a04e883282571b9c28a1 Mon Sep 17 00:00:00 2001 From: Aaron Clarke Date: Fri, 31 May 2024 14:30:30 -0700 Subject: [PATCH 09/13] updated coverage math --- .../contents/filters/gaussian_blur_filter_contents.cc | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc b/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc index 3780c57a98916..8b762efca6703 100644 --- a/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc +++ b/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc @@ -363,10 +363,12 @@ std::optional GaussianBlurFilterContents::GetFilterCoverage( return {}; } - Vector2 scaled_sigma = - (effect_transform.Basis() * entity.GetTransform().Basis() * - Vector2(ScaleSigma(sigma_x_), ScaleSigma(sigma_y_))) - .Abs(); + Vector2 entity_scale_x = entity.GetTransform().Basis() * Vector2(1.0, 0.0); + Vector2 entity_scale_y = entity.GetTransform().Basis() * Vector2(0.0, 1.0); + Vector2 scaled_sigma = (Matrix::MakeScale({entity_scale_x.GetLength(), + entity_scale_y.GetLength(), 1.0}) * + Vector2(ScaleSigma(sigma_x_), ScaleSigma(sigma_y_))) + .Abs(); Vector2 blur_radius = Vector2(CalculateBlurRadius(scaled_sigma.x), CalculateBlurRadius(scaled_sigma.y)); Vector2 padding(ceil(blur_radius.x), ceil(blur_radius.y)); From d6ffe9944e91ac87931dbd741821764400306727 Mon Sep 17 00:00:00 2001 From: Aaron Clarke Date: Fri, 31 May 2024 15:31:04 -0700 Subject: [PATCH 10/13] started clamping the sigma after scaling --- .../contents/filters/gaussian_blur_filter_contents.cc | 9 +++++++-- impeller/entity/shaders/filters/gaussian.frag | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc b/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc index 8b762efca6703..9b9fc3dfe00dd 100644 --- a/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc +++ b/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc @@ -24,7 +24,8 @@ const int32_t GaussianBlurFilterContents::kBlurFilterRequiredMipCount = 4; namespace { // 48 comes from gaussian.frag. -const int32_t kMaxKernelSize = 48; +const int32_t kMaxKernelSize = 50; +constexpr Scalar kMaxSigma = 500.0f; SamplerDescriptor MakeSamplerDescriptor(MinMagFilter filter, SamplerAddressMode address_mode) { @@ -369,6 +370,8 @@ std::optional GaussianBlurFilterContents::GetFilterCoverage( entity_scale_y.GetLength(), 1.0}) * Vector2(ScaleSigma(sigma_x_), ScaleSigma(sigma_y_))) .Abs(); + scaled_sigma.x = std::min(scaled_sigma.x, kMaxSigma); + scaled_sigma.y = std::min(scaled_sigma.y, kMaxSigma); Vector2 blur_radius = Vector2(CalculateBlurRadius(scaled_sigma.x), CalculateBlurRadius(scaled_sigma.y)); Vector2 padding(ceil(blur_radius.x), ceil(blur_radius.y)); @@ -393,6 +396,8 @@ std::optional GaussianBlurFilterContents::RenderFilter( entity_scale_y.GetLength(), 1.0}) * Vector2(ScaleSigma(sigma_x_), ScaleSigma(sigma_y_))) .Abs(); + scaled_sigma.x = std::min(scaled_sigma.x, kMaxSigma); + scaled_sigma.y = std::min(scaled_sigma.y, kMaxSigma); Vector2 blur_radius = Vector2(CalculateBlurRadius(scaled_sigma.x), CalculateBlurRadius(scaled_sigma.y)); Vector2 padding(ceil(blur_radius.x), ceil(blur_radius.y)); @@ -588,7 +593,7 @@ Quad GaussianBlurFilterContents::CalculateUVs( // that puts the minima there and a f(0)=1. Scalar GaussianBlurFilterContents::ScaleSigma(Scalar sigma) { // Limit the kernel size to 1000x1000 pixels, like Skia does. - Scalar clamped = std::min(sigma, 500.0f); + Scalar clamped = std::min(sigma, kMaxSigma); constexpr Scalar a = 3.4e-06; constexpr Scalar b = -3.4e-3; constexpr Scalar c = 1.f; diff --git a/impeller/entity/shaders/filters/gaussian.frag b/impeller/entity/shaders/filters/gaussian.frag index 0fb5fb3b00ec9..f83a59940896d 100644 --- a/impeller/entity/shaders/filters/gaussian.frag +++ b/impeller/entity/shaders/filters/gaussian.frag @@ -18,7 +18,7 @@ struct KernelSample { uniform KernelSamples { int sample_count; - KernelSample samples[48]; + KernelSample samples[50]; } blur_info; From 35eb89920abf1240d9223bba798ce3e8e9870da9 Mon Sep 17 00:00:00 2001 From: Aaron Clarke Date: Fri, 31 May 2024 15:31:59 -0700 Subject: [PATCH 11/13] fixed order in golden test --- testing/impeller_golden_tests_output.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/testing/impeller_golden_tests_output.txt b/testing/impeller_golden_tests_output.txt index 0fd9d0534e3c4..8233f6a34b596 100644 --- a/testing/impeller_golden_tests_output.txt +++ b/testing/impeller_golden_tests_output.txt @@ -798,12 +798,12 @@ impeller_Play_DlGoldenTest_CanDrawPaint_Vulkan.png impeller_Play_DlGoldenTest_CanRenderImage_Metal.png impeller_Play_DlGoldenTest_CanRenderImage_OpenGLES.png impeller_Play_DlGoldenTest_CanRenderImage_Vulkan.png -impeller_Play_DlGoldenTest_GaussianVsRRectBlurScaled_Metal.png -impeller_Play_DlGoldenTest_GaussianVsRRectBlurScaled_OpenGLES.png -impeller_Play_DlGoldenTest_GaussianVsRRectBlurScaled_Vulkan.png impeller_Play_DlGoldenTest_GaussianVsRRectBlurScaledRotated_Metal.png impeller_Play_DlGoldenTest_GaussianVsRRectBlurScaledRotated_OpenGLES.png impeller_Play_DlGoldenTest_GaussianVsRRectBlurScaledRotated_Vulkan.png +impeller_Play_DlGoldenTest_GaussianVsRRectBlurScaled_Metal.png +impeller_Play_DlGoldenTest_GaussianVsRRectBlurScaled_OpenGLES.png +impeller_Play_DlGoldenTest_GaussianVsRRectBlurScaled_Vulkan.png impeller_Play_DlGoldenTest_GaussianVsRRectBlur_Metal.png impeller_Play_DlGoldenTest_GaussianVsRRectBlur_OpenGLES.png impeller_Play_DlGoldenTest_GaussianVsRRectBlur_Vulkan.png From cb9b31457ac4d79f50720f3a17000aa82ba3a287 Mon Sep 17 00:00:00 2001 From: Aaron Clarke Date: Mon, 3 Jun 2024 09:22:57 -0700 Subject: [PATCH 12/13] revert changes to solid_rrect_blur_contents --- .../entity/contents/solid_rrect_blur_contents.cc | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/impeller/entity/contents/solid_rrect_blur_contents.cc b/impeller/entity/contents/solid_rrect_blur_contents.cc index 0709861fe940a..b6bdffd2b7d81 100644 --- a/impeller/entity/contents/solid_rrect_blur_contents.cc +++ b/impeller/entity/contents/solid_rrect_blur_contents.cc @@ -6,7 +6,6 @@ #include #include "impeller/entity/contents/content_context.h" -#include "impeller/entity/contents/filters/gaussian_blur_filter_contents.h" #include "impeller/entity/entity.h" #include "impeller/geometry/color.h" #include "impeller/geometry/constants.h" @@ -49,19 +48,13 @@ Color SolidRRectBlurContents::GetColor() const { return color_; } -namespace { -Scalar ScaleSigma(Scalar sigma) { - return GaussianBlurFilterContents::ScaleSigma(sigma); -} -} // namespace - std::optional SolidRRectBlurContents::GetCoverage( const Entity& entity) const { if (!rect_.has_value()) { return std::nullopt; } - Scalar radius = PadForSigma(ScaleSigma(sigma_.sigma)); + Scalar radius = PadForSigma(sigma_.sigma); return rect_->Expand(radius).TransformBounds(entity.GetTransform()); } @@ -78,7 +71,10 @@ bool SolidRRectBlurContents::Render(const ContentContext& renderer, VertexBufferBuilder vtx_builder; - auto blur_sigma = ScaleSigma(sigma_.sigma); + // Clamp the max kernel width/height to 1000 to limit the extent + // of the blur and to kEhCloseEnough to prevent NaN calculations + // trying to evaluate a Guassian distribution with a sigma of 0. + auto blur_sigma = std::clamp(sigma_.sigma, kEhCloseEnough, 250.0f); // Increase quality by making the radius a bit bigger than the typical // sigma->radius conversion we use for slower blurs. auto blur_radius = PadForSigma(blur_sigma); From 82a120b00f02a3713501606752706f68f85058de Mon Sep 17 00:00:00 2001 From: Aaron Clarke Date: Mon, 3 Jun 2024 14:15:19 -0700 Subject: [PATCH 13/13] brought back the effect transform --- .../entity/contents/filters/gaussian_blur_filter_contents.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc b/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc index 9b9fc3dfe00dd..0dfbdf2211db4 100644 --- a/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc +++ b/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc @@ -392,7 +392,8 @@ std::optional GaussianBlurFilterContents::RenderFilter( Vector2 entity_scale_x = entity.GetTransform().Basis() * Vector2(1.0, 0.0); Vector2 entity_scale_y = entity.GetTransform().Basis() * Vector2(0.0, 1.0); - Vector2 scaled_sigma = (Matrix::MakeScale({entity_scale_x.GetLength(), + Vector2 scaled_sigma = (effect_transform.Basis() * + Matrix::MakeScale({entity_scale_x.GetLength(), entity_scale_y.GetLength(), 1.0}) * Vector2(ScaleSigma(sigma_x_), ScaleSigma(sigma_y_))) .Abs();