From 91526aaded027cc5fb37e943794123236bfecdb2 Mon Sep 17 00:00:00 2001 From: Matan Lurey Date: Tue, 8 Aug 2023 16:23:38 -0700 Subject: [PATCH] [Impeller] Add gradient dithering for Radial/Sweep/Conical gradients (flutter/engine#44331) Partial work towards https://github.com/flutter/flutter/issues/131450 similar to https://github.com/flutter/engine/pull/44181. --- Run the Playground locally: ```bash $ENGINE/out/host_debug_unopt/impeller_unittests \ --enable_playground \ --gtest_filter="*CanRender*GradientWithDithering*" ``` ## Radial ### Before Screenshot 2023-08-03 at 10 08 53 AM ### After Screenshot 2023-08-03 at 10 13 57 AM ## Sweep ### Before Screenshot 2023-08-03 at 10 27 35 AM ### After Screenshot 2023-08-03 at 10 33 11 AM ### After Screenshot 2023-08-08 at 1 11 40 PM --- .../flutter/impeller/aiks/aiks_unittests.cc | 91 ++++++++++++++++++- .../src/flutter/impeller/aiks/color_source.cc | 3 + .../contents/conical_gradient_contents.cc | 5 + .../contents/conical_gradient_contents.h | 3 + .../contents/radial_gradient_contents.cc | 5 + .../contents/radial_gradient_contents.h | 3 + .../contents/sweep_gradient_contents.cc | 5 + .../entity/contents/sweep_gradient_contents.h | 3 + .../shaders/conical_gradient_ssbo_fill.frag | 6 ++ .../shaders/radial_gradient_ssbo_fill.frag | 6 ++ .../shaders/sweep_gradient_ssbo_fill.frag | 6 ++ engine/src/flutter/impeller/tools/malioc.json | 48 +++++----- 12 files changed, 156 insertions(+), 28 deletions(-) diff --git a/engine/src/flutter/impeller/aiks/aiks_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_unittests.cc index d6ed92840a..dbd507233d 100644 --- a/engine/src/flutter/impeller/aiks/aiks_unittests.cc +++ b/engine/src/flutter/impeller/aiks/aiks_unittests.cc @@ -419,9 +419,8 @@ TEST_P(AiksTest, CanRenderLinearGradientDecalWithColorFilter) { ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); } -namespace { -void CanRenderLinearGradientWithDithering(AiksTest* aiks_test, - bool use_dithering) { +static void CanRenderLinearGradientWithDithering(AiksTest* aiks_test, + bool use_dithering) { Canvas canvas; Paint paint; canvas.Translate({100.0, 100.0, 0}); @@ -439,7 +438,6 @@ void CanRenderLinearGradientWithDithering(AiksTest* aiks_test, canvas.DrawRect({0, 0, 800, 500}, paint); ASSERT_TRUE(aiks_test->OpenPlaygroundHere(canvas.EndRecordingAsPicture())); } -} // namespace TEST_P(AiksTest, CanRenderLinearGradientWithDitheringDisabled) { CanRenderLinearGradientWithDithering(this, false); @@ -449,6 +447,91 @@ TEST_P(AiksTest, CanRenderLinearGradientWithDitheringEnabled) { CanRenderLinearGradientWithDithering(this, true); } // namespace +static void CanRenderRadialGradientWithDithering(AiksTest* aiks_test, + bool use_dithering) { + Canvas canvas; + Paint paint; + canvas.Translate({100.0, 100.0, 0}); + + // #FFF -> #000 + std::vector colors = {Color{1.0, 1.0, 1.0, 1.0}, + Color{0.0, 0.0, 0.0, 1.0}}; + std::vector stops = {0.0, 1.0}; + + paint.color_source = ColorSource::MakeRadialGradient( + {600, 600}, 600, std::move(colors), std::move(stops), + Entity::TileMode::kClamp, {}); + paint.dither = use_dithering; + canvas.DrawRect({0, 0, 1200, 1200}, paint); + ASSERT_TRUE(aiks_test->OpenPlaygroundHere(canvas.EndRecordingAsPicture())); +} + +TEST_P(AiksTest, CanRenderRadialGradientWithDitheringDisabled) { + CanRenderRadialGradientWithDithering(this, false); +} + +TEST_P(AiksTest, CanRenderRadialGradientWithDitheringEnabled) { + CanRenderRadialGradientWithDithering(this, true); +} + +static void CanRenderSweepGradientWithDithering(AiksTest* aiks_test, + bool use_dithering) { + Canvas canvas; + canvas.Scale(aiks_test->GetContentScale()); + Paint paint; + canvas.Translate({100.0, 100.0, 0}); + + // #FFF -> #000 + std::vector colors = {Color{1.0, 1.0, 1.0, 1.0}, + Color{0.0, 0.0, 0.0, 1.0}}; + std::vector stops = {0.0, 1.0}; + + paint.color_source = ColorSource::MakeSweepGradient( + {100, 100}, Degrees(45), Degrees(135), std::move(colors), + std::move(stops), Entity::TileMode::kMirror, {}); + paint.dither = use_dithering; + + canvas.DrawRect({0, 0, 600, 600}, paint); + ASSERT_TRUE(aiks_test->OpenPlaygroundHere(canvas.EndRecordingAsPicture())); +} + +TEST_P(AiksTest, CanRenderSweepGradientWithDitheringDisabled) { + CanRenderSweepGradientWithDithering(this, false); +} + +TEST_P(AiksTest, CanRenderSweepGradientWithDitheringEnabled) { + CanRenderSweepGradientWithDithering(this, true); +} + +static void CanRenderConicalGradientWithDithering(AiksTest* aiks_test, + bool use_dithering) { + Canvas canvas; + canvas.Scale(aiks_test->GetContentScale()); + Paint paint; + canvas.Translate({100.0, 100.0, 0}); + + // #FFF -> #000 + std::vector colors = {Color{1.0, 1.0, 1.0, 1.0}, + Color{0.0, 0.0, 0.0, 1.0}}; + std::vector stops = {0.0, 1.0}; + + paint.color_source = ColorSource::MakeConicalGradient( + {100, 100}, 100, std::move(colors), std::move(stops), {0, 1}, 0, + Entity::TileMode::kMirror, {}); + paint.dither = use_dithering; + + canvas.DrawRect({0, 0, 600, 600}, paint); + ASSERT_TRUE(aiks_test->OpenPlaygroundHere(canvas.EndRecordingAsPicture())); +} + +TEST_P(AiksTest, CanRenderConicalGradientWithDitheringDisabled) { + CanRenderConicalGradientWithDithering(this, false); +} + +TEST_P(AiksTest, CanRenderConicalGradientWithDitheringEnabled) { + CanRenderConicalGradientWithDithering(this, true); +} + namespace { void CanRenderLinearGradientWithOverlappingStops(AiksTest* aiks_test, Entity::TileMode tile_mode) { diff --git a/engine/src/flutter/impeller/aiks/color_source.cc b/engine/src/flutter/impeller/aiks/color_source.cc index c7532eea7e..082225dd50 100644 --- a/engine/src/flutter/impeller/aiks/color_source.cc +++ b/engine/src/flutter/impeller/aiks/color_source.cc @@ -88,6 +88,7 @@ ColorSource ColorSource::MakeConicalGradient(Point center, contents->SetStops(stops); contents->SetCenterAndRadius(center, radius); contents->SetTileMode(tile_mode); + contents->SetDither(paint.dither); contents->SetEffectTransform(effect_transform); contents->SetFocus(focus_center, focus_radius); @@ -119,6 +120,7 @@ ColorSource ColorSource::MakeRadialGradient(Point center, contents->SetStops(stops); contents->SetCenterAndRadius(center, radius); contents->SetTileMode(tile_mode); + contents->SetDither(paint.dither); contents->SetEffectTransform(effect_transform); auto radius_pt = Point(radius, radius); @@ -150,6 +152,7 @@ ColorSource ColorSource::MakeSweepGradient(Point center, contents->SetColors(colors); contents->SetStops(stops); contents->SetTileMode(tile_mode); + contents->SetDither(paint.dither); contents->SetEffectTransform(effect_transform); return contents; diff --git a/engine/src/flutter/impeller/entity/contents/conical_gradient_contents.cc b/engine/src/flutter/impeller/entity/contents/conical_gradient_contents.cc index b3705bf858..6295e4839e 100644 --- a/engine/src/flutter/impeller/entity/contents/conical_gradient_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/conical_gradient_contents.cc @@ -29,6 +29,10 @@ void ConicalGradientContents::SetTileMode(Entity::TileMode tile_mode) { tile_mode_ = tile_mode; } +void ConicalGradientContents::SetDither(bool dither) { + dither_ = dither; +} + void ConicalGradientContents::SetColors(std::vector colors) { colors_ = std::move(colors); } @@ -72,6 +76,7 @@ bool ConicalGradientContents::RenderSSBO(const ContentContext& renderer, frag_info.tile_mode = static_cast(tile_mode_); frag_info.decal_border_color = decal_border_color_; frag_info.alpha = GetOpacityFactor(); + frag_info.dither = dither_; if (focus_) { frag_info.focus = focus_.value(); frag_info.focus_radius = focus_radius_; diff --git a/engine/src/flutter/impeller/entity/contents/conical_gradient_contents.h b/engine/src/flutter/impeller/entity/contents/conical_gradient_contents.h index f9701a5a44..55a2ac16a3 100644 --- a/engine/src/flutter/impeller/entity/contents/conical_gradient_contents.h +++ b/engine/src/flutter/impeller/entity/contents/conical_gradient_contents.h @@ -45,6 +45,8 @@ class ConicalGradientContents final : public ColorSourceContents { void SetTileMode(Entity::TileMode tile_mode); + void SetDither(bool dither); + void SetFocus(std::optional focus, Scalar radius); private: @@ -63,6 +65,7 @@ class ConicalGradientContents final : public ColorSourceContents { Color decal_border_color_ = Color::BlackTransparent(); std::optional focus_; Scalar focus_radius_ = 0.0f; + bool dither_ = false; FML_DISALLOW_COPY_AND_ASSIGN(ConicalGradientContents); }; diff --git a/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.cc b/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.cc index 2527b54898..c10169278a 100644 --- a/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.cc @@ -29,6 +29,10 @@ void RadialGradientContents::SetTileMode(Entity::TileMode tile_mode) { tile_mode_ = tile_mode; } +void RadialGradientContents::SetDither(bool dither) { + dither_ = dither; +} + void RadialGradientContents::SetColors(std::vector colors) { colors_ = std::move(colors); } @@ -78,6 +82,7 @@ bool RadialGradientContents::RenderSSBO(const ContentContext& renderer, frag_info.tile_mode = static_cast(tile_mode_); frag_info.decal_border_color = decal_border_color_; frag_info.alpha = GetOpacityFactor(); + frag_info.dither = dither_; auto& host_buffer = pass.GetTransientsBuffer(); auto colors = CreateGradientColors(colors_, stops_); diff --git a/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.h b/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.h index 5db915289c..6464943f4c 100644 --- a/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.h +++ b/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.h @@ -48,6 +48,8 @@ class RadialGradientContents final : public ColorSourceContents { void SetTileMode(Entity::TileMode tile_mode); + void SetDither(bool dither); + private: bool RenderTexture(const ContentContext& renderer, const Entity& entity, @@ -62,6 +64,7 @@ class RadialGradientContents final : public ColorSourceContents { std::vector stops_; Entity::TileMode tile_mode_; Color decal_border_color_ = Color::BlackTransparent(); + bool dither_ = false; FML_DISALLOW_COPY_AND_ASSIGN(RadialGradientContents); }; diff --git a/engine/src/flutter/impeller/entity/contents/sweep_gradient_contents.cc b/engine/src/flutter/impeller/entity/contents/sweep_gradient_contents.cc index c730bb3d70..f8da6776dc 100644 --- a/engine/src/flutter/impeller/entity/contents/sweep_gradient_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/sweep_gradient_contents.cc @@ -42,6 +42,10 @@ void SweepGradientContents::SetTileMode(Entity::TileMode tile_mode) { tile_mode_ = tile_mode; } +void SweepGradientContents::SetDither(bool dither) { + dither_ = dither; +} + const std::vector& SweepGradientContents::GetColors() const { return colors_; } @@ -84,6 +88,7 @@ bool SweepGradientContents::RenderSSBO(const ContentContext& renderer, frag_info.tile_mode = static_cast(tile_mode_); frag_info.decal_border_color = decal_border_color_; frag_info.alpha = GetOpacityFactor(); + frag_info.dither = dither_; auto& host_buffer = pass.GetTransientsBuffer(); auto colors = CreateGradientColors(colors_, stops_); diff --git a/engine/src/flutter/impeller/entity/contents/sweep_gradient_contents.h b/engine/src/flutter/impeller/entity/contents/sweep_gradient_contents.h index bb86437dbb..6d76baf39a 100644 --- a/engine/src/flutter/impeller/entity/contents/sweep_gradient_contents.h +++ b/engine/src/flutter/impeller/entity/contents/sweep_gradient_contents.h @@ -45,6 +45,8 @@ class SweepGradientContents final : public ColorSourceContents { void SetTileMode(Entity::TileMode tile_mode); + void SetDither(bool dither); + const std::vector& GetColors() const; const std::vector& GetStops() const; @@ -65,6 +67,7 @@ class SweepGradientContents final : public ColorSourceContents { std::vector stops_; Entity::TileMode tile_mode_; Color decal_border_color_ = Color::BlackTransparent(); + bool dither_ = false; FML_DISALLOW_COPY_AND_ASSIGN(SweepGradientContents); }; diff --git a/engine/src/flutter/impeller/entity/shaders/conical_gradient_ssbo_fill.frag b/engine/src/flutter/impeller/entity/shaders/conical_gradient_ssbo_fill.frag index 63c86698bf..39084ae1c6 100644 --- a/engine/src/flutter/impeller/entity/shaders/conical_gradient_ssbo_fill.frag +++ b/engine/src/flutter/impeller/entity/shaders/conical_gradient_ssbo_fill.frag @@ -5,6 +5,7 @@ precision mediump float; #include +#include #include #include #include @@ -28,6 +29,7 @@ uniform FragInfo { int colors_length; vec2 focus; float focus_radius; + bool dither; } frag_info; @@ -63,4 +65,8 @@ void main() { } } frag_color = IPPremultiply(result_color) * frag_info.alpha; + + if (frag_info.dither) { + frag_color = IPOrderedDither8x8(frag_color, v_position); + } } diff --git a/engine/src/flutter/impeller/entity/shaders/radial_gradient_ssbo_fill.frag b/engine/src/flutter/impeller/entity/shaders/radial_gradient_ssbo_fill.frag index aab3cf3e2e..13fc105905 100644 --- a/engine/src/flutter/impeller/entity/shaders/radial_gradient_ssbo_fill.frag +++ b/engine/src/flutter/impeller/entity/shaders/radial_gradient_ssbo_fill.frag @@ -5,6 +5,7 @@ precision mediump float; #include +#include #include #include #include @@ -26,6 +27,7 @@ uniform FragInfo { vec4 decal_border_color; float alpha; int colors_length; + bool dither; } frag_info; @@ -59,4 +61,8 @@ void main() { } } frag_color = IPPremultiply(result_color) * frag_info.alpha; + + if (frag_info.dither) { + frag_color = IPOrderedDither8x8(frag_color, v_position); + } } diff --git a/engine/src/flutter/impeller/entity/shaders/sweep_gradient_ssbo_fill.frag b/engine/src/flutter/impeller/entity/shaders/sweep_gradient_ssbo_fill.frag index cdc7347c4f..39f14d6b39 100644 --- a/engine/src/flutter/impeller/entity/shaders/sweep_gradient_ssbo_fill.frag +++ b/engine/src/flutter/impeller/entity/shaders/sweep_gradient_ssbo_fill.frag @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -26,6 +27,7 @@ uniform FragInfo { vec4 decal_border_color; float alpha; int colors_length; + bool dither; } frag_info; @@ -60,4 +62,8 @@ void main() { } } frag_color = IPPremultiply(result_color) * frag_info.alpha; + + if (frag_info.dither) { + frag_color = IPOrderedDither8x8(frag_color, v_position); + } } diff --git a/engine/src/flutter/impeller/tools/malioc.json b/engine/src/flutter/impeller/tools/malioc.json index 49e15fa8ff..5ff0a65b09 100644 --- a/engine/src/flutter/impeller/tools/malioc.json +++ b/engine/src/flutter/impeller/tools/malioc.json @@ -2147,9 +2147,9 @@ "varying" ], "shortest_path_cycles": [ - 0.1875, + 0.21875, 0.0625, - 0.1875, + 0.21875, 0.0, 0.0, 0.25, @@ -2159,10 +2159,10 @@ "load_store" ], "total_cycles": [ - 1.3125, - 0.8125, - 1.3125, - 0.375, + 1.4874999523162842, + 0.875, + 1.4874999523162842, + 0.875, 4.0, 0.25, 0.0 @@ -2170,8 +2170,8 @@ }, "stack_spill_bytes": 0, "thread_occupancy": 100, - "uniform_registers_used": 36, - "work_registers_used": 15 + "uniform_registers_used": 46, + "work_registers_used": 20 } } } @@ -12440,7 +12440,7 @@ "uses_late_zs_update": false, "variants": { "Main": { - "fp16_arithmetic": 52, + "fp16_arithmetic": 55, "has_stack_spilling": false, "performance": { "longest_path_bound_pipelines": [ @@ -12468,9 +12468,9 @@ "varying" ], "shortest_path_cycles": [ + 0.203125, 0.171875, - 0.171875, - 0.171875, + 0.203125, 0.0625, 0.0, 0.25, @@ -12480,10 +12480,10 @@ "load_store" ], "total_cycles": [ - 0.609375, - 0.375, - 0.609375, - 0.125, + 0.78125, + 0.4375, + 0.78125, + 0.625, 4.0, 0.25, 0.0 @@ -12491,8 +12491,8 @@ }, "stack_spill_bytes": 0, "thread_occupancy": 100, - "uniform_registers_used": 14, - "work_registers_used": 15 + "uniform_registers_used": 22, + "work_registers_used": 20 } } } @@ -13277,7 +13277,7 @@ "shortest_path_cycles": [ 0.390625, 0.390625, - 0.25, + 0.28125, 0.3125, 0.0, 0.25, @@ -13287,10 +13287,10 @@ "load_store" ], "total_cycles": [ - 0.6875, - 0.65625, - 0.6875, - 0.375, + 0.875, + 0.71875, + 0.84375, + 0.875, 4.0, 0.25, 0.0 @@ -13298,8 +13298,8 @@ }, "stack_spill_bytes": 0, "thread_occupancy": 100, - "uniform_registers_used": 20, - "work_registers_used": 19 + "uniform_registers_used": 30, + "work_registers_used": 23 } } }