[Impeller] Make color matrix filter work as expected (flutter/engine#35926)

This commit is contained in:
ColdPaleLight
2022-09-07 08:55:17 +08:00
committed by GitHub
parent ee87ce6517
commit 3b14069222
3 changed files with 12 additions and 38 deletions

View File

@@ -77,16 +77,16 @@ std::optional<Snapshot> ColorMatrixFilterContents::RenderFilter(
FS::FragInfo frag_info;
const float* matrix = matrix_.array;
frag_info.color_v = Vector4(matrix[4], matrix[9], matrix[14], matrix[19]);
frag_info.texture_sampler_y_coord_scale =
input_snapshot->texture->GetYCoordScale();
// clang-format off
frag_info.color_m =
Matrix(
matrix[ 0], matrix[ 1], matrix[ 2], matrix[ 3],
matrix[ 5], matrix[ 6], matrix[ 7], matrix[ 8],
matrix[10], matrix[11], matrix[12], matrix[13],
matrix[15], matrix[16], matrix[17], matrix[18]
);
frag_info.color_m = Matrix(
matrix[0], matrix[5], matrix[10], matrix[15],
matrix[1], matrix[6], matrix[11], matrix[16],
matrix[2], matrix[7], matrix[12], matrix[17],
matrix[3], matrix[8], matrix[13], matrix[18]
);
// clang-format on
auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
FS::BindInputTexture(cmd, input_snapshot->texture, sampler);
FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info));

View File

@@ -1556,35 +1556,6 @@ TEST_P(EntityTest, ColorMatrixFilterCoverageIsCorrect) {
ASSERT_RECT_NEAR(actual.value(), expected);
}
TEST_P(EntityTest, ColorMatrixFilter) {
auto image = CreateTextureForFixture("boston.jpg");
ASSERT_TRUE(image);
auto callback = [&](ContentContext& context, RenderPass& pass) -> bool {
// Set the color matrix filter.
FilterContents::ColorMatrix matrix = {
1, 0, 0, 0, 0, //
0, 3, 0, 0, 0, //
0, 0, 1, 0, 0, //
0, 0, 0, 1, 0, //
};
auto filter =
FilterContents::MakeColorMatrix(FilterInput::Make(image), matrix);
// Define the entity with the color matrix filter.
Entity entity;
entity.SetTransformation(Matrix::MakeScale(GetContentScale()) *
Matrix::MakeTranslation({500, 300}) *
Matrix::MakeScale(Vector2{0.5, 0.5}));
entity.SetContents(filter);
return entity.Render(context, pass);
};
// Should output the boston image with a green filter.
ASSERT_TRUE(OpenPlaygroundHere(callback));
}
TEST_P(EntityTest, ColorMatrixFilterEditable) {
auto bay_bridge = CreateTextureForFixture("bay_bridge.jpg");
ASSERT_TRUE(bay_bridge);

View File

@@ -3,6 +3,7 @@
// found in the LICENSE file.
#include <impeller/color.glsl>
#include <impeller/texture.glsl>
// A color filter that transforms colors through a 4x5 color matrix.
//
@@ -28,6 +29,7 @@
uniform FragInfo {
mat4 color_m;
vec4 color_v;
float texture_sampler_y_coord_scale;
} frag_info;
uniform sampler2D input_texture;
@@ -36,7 +38,8 @@ in vec2 v_position;
out vec4 frag_color;
void main() {
vec4 input_color = texture(input_texture, v_position);
vec4 input_color = IPSample(input_texture, v_position,
frag_info.texture_sampler_y_coord_scale);
// unpremultiply first, as filter inputs are premultiplied.
vec4 color = IPUnpremultiply(input_color);