Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit ff2b811

Browse files
[Impeller] fix plus blend mode in porterduff shader. (#51792)
DrawAtlas already has the optimization in #51778 and so it is still rendering incorrectly with wide gamut.
1 parent 5bf8b94 commit ff2b811

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

impeller/aiks/aiks_unittests.cc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3073,6 +3073,42 @@ TEST_P(AiksTest, MipmapGenerationWorksCorrectly) {
30733073
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
30743074
}
30753075

3076+
TEST_P(AiksTest, DrawAtlasPlusWideGamut) {
3077+
if (GetParam() != PlaygroundBackend::kMetal) {
3078+
GTEST_SKIP_("This backend doesn't yet support wide gamut.");
3079+
}
3080+
3081+
EXPECT_EQ(GetContext()->GetCapabilities()->GetDefaultColorFormat(),
3082+
PixelFormat::kR16G16B16A16Float);
3083+
3084+
// Draws the image as four squares stiched together.
3085+
auto atlas =
3086+
std::make_shared<Image>(CreateTextureForFixture("bay_bridge.jpg"));
3087+
auto size = atlas->GetSize();
3088+
// Divide image into four quadrants.
3089+
Scalar half_width = size.width / 2;
3090+
Scalar half_height = size.height / 2;
3091+
std::vector<Rect> texture_coordinates = {
3092+
Rect::MakeLTRB(0, 0, half_width, half_height),
3093+
Rect::MakeLTRB(half_width, 0, size.width, half_height),
3094+
Rect::MakeLTRB(0, half_height, half_width, size.height),
3095+
Rect::MakeLTRB(half_width, half_height, size.width, size.height)};
3096+
// Position quadrants adjacent to eachother.
3097+
std::vector<Matrix> transforms = {
3098+
Matrix::MakeTranslation({0, 0, 0}),
3099+
Matrix::MakeTranslation({half_width, 0, 0}),
3100+
Matrix::MakeTranslation({0, half_height, 0}),
3101+
Matrix::MakeTranslation({half_width, half_height, 0})};
3102+
std::vector<Color> colors = {Color::Red(), Color::Green(), Color::Blue(),
3103+
Color::Yellow()};
3104+
3105+
Canvas canvas;
3106+
canvas.DrawAtlas(atlas, transforms, texture_coordinates, colors,
3107+
BlendMode::kPlus, {}, std::nullopt, {});
3108+
3109+
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
3110+
}
3111+
30763112
} // namespace testing
30773113
} // namespace impeller
30783114

impeller/entity/shaders/blending/porter_duff_blend.frag

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ f16vec4 Sample(f16sampler2D texture_sampler, vec2 texture_coords) {
3636
return IPHalfSampleDecal(texture_sampler, texture_coords);
3737
}
3838

39+
float16_t ClampAlpha(float16_t alpha) {
40+
float16_t min = 0.0hf;
41+
float16_t max = 1.0hf;
42+
return clamp(alpha, min, max);
43+
}
44+
3945
void main() {
4046
f16vec4 dst =
4147
texture(texture_sampler_dst, v_texture_coords) * frag_info.input_alpha;
@@ -45,4 +51,9 @@ void main() {
4551
dst * (frag_info.dst_coeff + src.a * frag_info.dst_coeff_src_alpha +
4652
src * frag_info.dst_coeff_src_color);
4753
frag_color *= frag_info.output_alpha;
54+
// This currently needs a clamp so that floating point textures blend
55+
// correctly in wide gamut. Remove if we switch to a fixed point extended
56+
// range format.
57+
// See https://github.com/flutter/flutter/issues/145933 .
58+
frag_color.a = ClampAlpha(frag_color.a);
4859
}

impeller/tools/malioc.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8434,8 +8434,8 @@
84348434
"varying"
84358435
],
84368436
"longest_path_cycles": [
8437-
0.21875,
8438-
0.21875,
8437+
0.234375,
8438+
0.234375,
84398439
0.0,
84408440
0.0,
84418441
0.0,
@@ -8455,8 +8455,8 @@
84558455
"varying"
84568456
],
84578457
"shortest_path_cycles": [
8458-
0.21875,
8459-
0.21875,
8458+
0.234375,
8459+
0.234375,
84608460
0.0,
84618461
0.0,
84628462
0.0,
@@ -8467,8 +8467,8 @@
84678467
"varying"
84688468
],
84698469
"total_cycles": [
8470-
0.21875,
8471-
0.21875,
8470+
0.234375,
8471+
0.234375,
84728472
0.0,
84738473
0.0,
84748474
0.0,

testing/impeller_golden_tests_output.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ impeller_Play_AiksTest_DrawAdvancedBlendPartlyOffscreen_Vulkan.png
436436
impeller_Play_AiksTest_DrawAtlasAdvancedAndTransform_Metal.png
437437
impeller_Play_AiksTest_DrawAtlasAdvancedAndTransform_OpenGLES.png
438438
impeller_Play_AiksTest_DrawAtlasAdvancedAndTransform_Vulkan.png
439+
impeller_Play_AiksTest_DrawAtlasPlusWideGamut_Metal.png
439440
impeller_Play_AiksTest_DrawAtlasWithColorAdvancedAndTransform_Metal.png
440441
impeller_Play_AiksTest_DrawAtlasWithColorAdvancedAndTransform_OpenGLES.png
441442
impeller_Play_AiksTest_DrawAtlasWithColorAdvancedAndTransform_Vulkan.png

0 commit comments

Comments
 (0)