Skip to content

Commit 1885bd3

Browse files
committed
Fix Vulkan MSAA diagonal seam in FramebufferBlend (flutter#179547)
1 parent acade0f commit 1885bd3

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "flutter/display_list/dl_paint.h"
1919
#include "flutter/impeller/display_list/dl_image_impeller.h"
2020
#include "flutter/impeller/geometry/scalar.h"
21+
#include "fml/synchronization/count_down_latch.h"
22+
#include "impeller/core/device_buffer.h"
2123
#include "impeller/display_list/aiks_context.h"
2224
#include "impeller/display_list/dl_dispatcher.h"
2325
#include "impeller/playground/playground.h"
@@ -827,6 +829,57 @@ TEST_P(AiksTest, FramebufferAdvancedBlendCoverage) {
827829
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
828830
}
829831

832+
// Regression test for https://github.com/flutter/flutter/issues/179547.
833+
TEST_P(AiksTest, FramebufferBlendDifferenceNoDiagonalSeam) {
834+
DisplayListBuilder builder;
835+
builder.DrawPaint(DlPaint().setColor(DlColor::kBlack()));
836+
837+
// Use a non-fullscreen rect so Canvas does not fold it into clear_color.
838+
DlPaint rect_paint;
839+
rect_paint.setColor(DlColor::kWhite());
840+
rect_paint.setBlendMode(DlBlendMode::kDifference);
841+
builder.DrawRect(DlRect::MakeXYWH(2, 2, 12, 12), rect_paint);
842+
843+
std::shared_ptr<Context> context = GetContext();
844+
AiksContext renderer(context, nullptr);
845+
auto out_texture =
846+
DisplayListToTexture(builder.Build(), ISize{16, 16}, renderer);
847+
ASSERT_TRUE(out_texture);
848+
849+
DeviceBufferDescriptor readback_desc;
850+
readback_desc.size =
851+
out_texture->GetTextureDescriptor().GetByteSizeOfBaseMipLevel();
852+
readback_desc.readback = true;
853+
readback_desc.storage_mode = StorageMode::kHostVisible;
854+
auto readback = context->GetResourceAllocator()->CreateBuffer(readback_desc);
855+
ASSERT_TRUE(readback);
856+
857+
auto cmd = context->CreateCommandBuffer();
858+
auto blit = cmd->CreateBlitPass();
859+
ASSERT_TRUE(blit->AddCopy(out_texture, readback));
860+
ASSERT_TRUE(blit->EncodeCommands());
861+
auto latch = std::make_shared<fml::CountDownLatch>(1u);
862+
ASSERT_TRUE(
863+
context->GetCommandQueue()
864+
->Submit({cmd},
865+
[latch](CommandBuffer::Status) { latch->CountDown(); })
866+
.ok());
867+
latch->Wait();
868+
869+
// |Black(0) - White(1)| = White(255) across all 4 RGBA8/BGRA8 channels inside
870+
// [2..14, 2..14]. Without masking subpassLoad by gl_SampleMaskIn, pixels
871+
// along the FramebufferBlend quad's diagonal seam drop to ~191.
872+
const uint8_t* bytes = readback->OnGetContents();
873+
for (int y = 3; y < 13; ++y) {
874+
for (int x = 3; x < 13; ++x) {
875+
const uint8_t* px = bytes + (y * 16 + x) * 4;
876+
EXPECT_NEAR(px[0], 255, 2) << "Seam at (" << x << ", " << y << ")";
877+
EXPECT_NEAR(px[1], 255, 2) << "Seam at (" << x << ", " << y << ")";
878+
EXPECT_NEAR(px[2], 255, 2) << "Seam at (" << x << ", " << y << ")";
879+
}
880+
}
881+
}
882+
830883
TEST_P(AiksTest, ColorWheel) {
831884
// Compare with https://fiddle.skia.org/c/@BlendModes
832885

‎engine/src/flutter/impeller/entity/shaders/blending/framebuffer_blend.frag‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,29 @@ layout(constant_id = 1) const float supports_decal = 1;
1919
layout(input_attachment_index = 0) uniform subpassInputMS uSub;
2020

2121
vec4 ReadDestination() {
22+
#ifdef IMPELLER_TARGET_VULKAN
23+
// On Metal and OpenGL ES, SPIRV-Cross lowers `subpassInputMS` to native
24+
// framebuffer fetch (`[[color(0)]]` and `gl_LastFragData[0]`), which the
25+
// hardware/driver scopes to the current primitive's covered samples (and
26+
// `gl_SampleMaskIn` is unavailable in unextended GLSL ES 1.00/3.00).
27+
//
28+
// On Vulkan, `subpassLoad(uSub, i)` remains an explicit pixel-rate MSAA
29+
// input-attachment read of sample `i`. Unconditionally averaging samples 0..3
30+
// reads samples outside this primitive's coverage, including samples along a
31+
// triangle strip's shared edge already written by the adjacent triangle.
32+
int mask = gl_SampleMaskIn[0] & 0xf;
33+
vec4 result = vec4(0.0);
34+
for (int i = 0; i < 4; i++) {
35+
if ((mask & (1 << i)) != 0) {
36+
result += subpassLoad(uSub, i);
37+
}
38+
}
39+
return result / float(max(bitCount(mask), 1));
40+
#else
2241
return (subpassLoad(uSub, 0) + subpassLoad(uSub, 1) + subpassLoad(uSub, 2) +
2342
subpassLoad(uSub, 3)) /
2443
vec4(4.0);
44+
#endif
2545
}
2646

2747
uniform sampler2D texture_sampler_src;

0 commit comments

Comments
 (0)