From 5e80d9e9e1b28fed25ff785233b0ea17bdf0d11c Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Thu, 25 Jan 2024 14:53:44 -0800 Subject: [PATCH] [Impeller] Fix advanced blend alpha issue, improve blend goldens. (flutter/engine#50035) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Figured out one of the problems... Multiply before (notice weird alpha behavior on the bird): Screenshot 2024-01-25 at 12 07 50 AM Multiply after: Screenshot 2024-01-25 at 12 06 43 AM --- .../flutter/impeller/aiks/aiks_unittests.cc | 64 +++++++++++++------ .../shaders/blending/advanced_blend.frag | 2 +- .../shaders/blending/framebuffer_blend.frag | 2 +- engine/src/flutter/impeller/tools/malioc.json | 24 +++---- 4 files changed, 60 insertions(+), 32 deletions(-) diff --git a/engine/src/flutter/impeller/aiks/aiks_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_unittests.cc index 715ab8c1da..9e36f2e3ed 100644 --- a/engine/src/flutter/impeller/aiks/aiks_unittests.cc +++ b/engine/src/flutter/impeller/aiks/aiks_unittests.cc @@ -2197,7 +2197,8 @@ TEST_P(AiksTest, SrgbToLinearFilterSubpassCollapseOptimization) { ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); } -static Picture BlendModeTest(BlendMode blend_mode, +static Picture BlendModeTest(Vector2 content_scale, + BlendMode blend_mode, const std::shared_ptr& src_image, const std::shared_ptr& dst_image) { Color destination_color = Color::CornflowerBlue().WithAlpha(0.75); @@ -2206,7 +2207,6 @@ static Picture BlendModeTest(BlendMode blend_mode, Color::Black().WithAlpha(0.75)}); Canvas canvas; - canvas.DrawPaint({.color = Color::Black()}); //---------------------------------------------------------------------------- @@ -2217,7 +2217,7 @@ static Picture BlendModeTest(BlendMode blend_mode, for (const auto& color : source_colors) { canvas.Save(); { - canvas.ClipRect(Rect::MakeXYWH(50, 50, 100, 100)); + canvas.ClipRect(Rect::MakeXYWH(25, 25, 100, 100)); // Perform the blend in a SaveLayer so that the initial backdrop color is // fully transparent black. SourceOver blend the result onto the parent // pass. @@ -2228,7 +2228,7 @@ static Picture BlendModeTest(BlendMode blend_mode, // pass. canvas.SaveLayer({.blend_mode = blend_mode}); { // - canvas.DrawRect(Rect::MakeXYWH(50, 50, 100, 100), {.color = color}); + canvas.DrawRect(Rect::MakeXYWH(25, 25, 100, 100), {.color = color}); } canvas.Restore(); } @@ -2249,10 +2249,9 @@ static Picture BlendModeTest(BlendMode blend_mode, // Perform the blend in a SaveLayer so that the initial backdrop color is // fully transparent black. SourceOver blend the result onto the parent pass. canvas.SaveLayer({}); - // canvas.DrawPaint({.color = destination_color}); for (const auto& color : source_colors) { // Simply write the CPU blended color to the pass. - canvas.DrawRect(Rect::MakeXYWH(50, 50, 100, 100), + canvas.DrawRect(Rect::MakeXYWH(25, 25, 100, 100), {.color = destination_color.Blend(color, blend_mode), .blend_mode = BlendMode::kSourceOver}); canvas.Translate(Vector2(100, 0)); @@ -2260,32 +2259,61 @@ static Picture BlendModeTest(BlendMode blend_mode, canvas.RestoreToCount(0); //---------------------------------------------------------------------------- - /// 3. Image blending (top right). + /// 3. Image blending (bottom images). /// /// Compare these results with the images in the Flutter blend mode /// documentation: https://api.flutter.dev/flutter/dart-ui/BlendMode.html /// + canvas.Translate({0, 250}); + + // Draw grid behind the images. + canvas.DrawRect(Rect::MakeLTRB(0, 0, 800, 400), + {.color = Color::MakeRGBA8(41, 41, 41, 255)}); + Paint square_paint = {.color = Color::MakeRGBA8(15, 15, 15, 255)}; + for (int y = 0; y < 400 / 8; y++) { + for (int x = 0; x < 800 / 16; x++) { + canvas.DrawRect(Rect::MakeXYWH(x * 16 + (y % 2) * 8, y * 8, 8, 8), + square_paint); + } + } + + // Uploaded image source (unpremultiplied source texture). canvas.Save(); - // canvas.ClipRect(Rect::MakeXYWH(500, 0, 500, 500)); canvas.SaveLayer({.blend_mode = BlendMode::kSourceOver}); { - canvas.DrawImage(dst_image, {400, 50}, {.blend_mode = BlendMode::kSource}); - canvas.DrawImage(src_image, {400, 50}, {.blend_mode = blend_mode}); + canvas.DrawImage(dst_image, {0, 0}, {.blend_mode = BlendMode::kSourceOver}); + canvas.DrawImage(src_image, {0, 0}, {.blend_mode = blend_mode}); } canvas.RestoreToCount(0); + // Rendered image source (premultiplied source texture). + canvas.Save(); + canvas.SaveLayer({.blend_mode = BlendMode::kSourceOver}); + { + canvas.DrawImage(dst_image, {400, 0}, + {.blend_mode = BlendMode::kSourceOver}); + canvas.SaveLayer({.blend_mode = blend_mode}); + { + canvas.DrawImage(src_image, {400, 0}, + {.blend_mode = BlendMode::kSourceOver}); + } + canvas.Restore(); + } + canvas.Restore(); + canvas.RestoreToCount(0); + return canvas.EndRecordingAsPicture(); } -#define BLEND_MODE_TEST(blend_mode) \ - TEST_P(AiksTest, BlendMode##blend_mode) { \ - auto src_image = std::make_shared( \ - CreateTextureForFixture("blend_mode_src.png")); \ - auto dst_image = std::make_shared( \ - CreateTextureForFixture("blend_mode_dst.png")); \ - OpenPlaygroundHere( \ - BlendModeTest(BlendMode::k##blend_mode, src_image, dst_image)); \ +#define BLEND_MODE_TEST(blend_mode) \ + TEST_P(AiksTest, BlendMode##blend_mode) { \ + auto src_image = std::make_shared( \ + CreateTextureForFixture("blend_mode_src.png")); \ + auto dst_image = std::make_shared( \ + CreateTextureForFixture("blend_mode_dst.png")); \ + OpenPlaygroundHere(BlendModeTest( \ + GetContentScale(), BlendMode::k##blend_mode, src_image, dst_image)); \ } IMPELLER_FOR_EACH_BLEND_MODE(BLEND_MODE_TEST) diff --git a/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend.frag b/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend.frag index e69186f478..b15c5db7fe 100644 --- a/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend.frag +++ b/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend.frag @@ -50,6 +50,6 @@ void main() { blend_info.src_input_alpha; f16vec3 blend_result = AdvancedBlend(dst.rgb, src.rgb, int(blend_type)); - f16vec4 blended = mix(src, f16vec4(blend_result, dst.a), dst.a); + f16vec4 blended = mix(src, f16vec4(blend_result, src.a), dst.a); frag_color = mix(dst_sample, blended, src.a); } diff --git a/engine/src/flutter/impeller/entity/shaders/blending/framebuffer_blend.frag b/engine/src/flutter/impeller/entity/shaders/blending/framebuffer_blend.frag index 4ad3650fd0..58cedbd56b 100644 --- a/engine/src/flutter/impeller/entity/shaders/blending/framebuffer_blend.frag +++ b/engine/src/flutter/impeller/entity/shaders/blending/framebuffer_blend.frag @@ -62,6 +62,6 @@ void main() { frag_info.src_input_alpha; f16vec3 blend_result = AdvancedBlend(dst.rgb, src.rgb, int(blend_type)); - f16vec4 blended = mix(src, f16vec4(blend_result, dst.a), dst.a); + f16vec4 blended = mix(src, f16vec4(blend_result, src.a), dst.a); frag_color = vec4(mix(dst, blended, src.a)); } diff --git a/engine/src/flutter/impeller/tools/malioc.json b/engine/src/flutter/impeller/tools/malioc.json index fac8742c99..377fe50b82 100644 --- a/engine/src/flutter/impeller/tools/malioc.json +++ b/engine/src/flutter/impeller/tools/malioc.json @@ -20,8 +20,8 @@ "texture" ], "longest_path_cycles": [ - 0.46875, - 0.46875, + 0.4375, + 0.4375, 0.015625, 0.0, 0.0, @@ -42,8 +42,8 @@ "arith_fma" ], "shortest_path_cycles": [ - 0.40625, - 0.40625, + 0.375, + 0.375, 0.09375, 0.0, 0.0, @@ -55,8 +55,8 @@ "texture" ], "total_cycles": [ - 0.46875, - 0.46875, + 0.4375, + 0.4375, 0.09375, 0.0, 0.0, @@ -1431,8 +1431,8 @@ "texture" ], "longest_path_cycles": [ - 0.46875, - 0.46875, + 0.4375, + 0.4375, 0.0625, 0.0, 0.0, @@ -1453,8 +1453,8 @@ "arith_fma" ], "shortest_path_cycles": [ - 0.40625, - 0.40625, + 0.375, + 0.375, 0.109375, 0.0, 0.0, @@ -1466,8 +1466,8 @@ "texture" ], "total_cycles": [ - 0.46875, - 0.46875, + 0.4375, + 0.4375, 0.140625, 0.0, 0.0,