From 82cc7df75b6c631b9ef54700b844df3c0729fd0b Mon Sep 17 00:00:00 2001 From: Ethan Lin Date: Thu, 16 Nov 2017 14:13:12 +0800 Subject: [PATCH] Make sure we un-premultiply alpha before applying filters. --- webrender/res/ps_blend.glsl | 25 ++++++++----------- .../filter-contrast-gray-alpha-1-ref.yaml | 13 ++++++++++ .../filters/filter-contrast-gray-alpha-1.yaml | 13 ++++++++++ wrench/reftests/filters/reftest.list | 1 + 4 files changed, 37 insertions(+), 15 deletions(-) create mode 100644 wrench/reftests/filters/filter-contrast-gray-alpha-1-ref.yaml create mode 100644 wrench/reftests/filters/filter-contrast-gray-alpha-1.yaml diff --git a/webrender/res/ps_blend.glsl b/webrender/res/ps_blend.glsl index 218cb9f9a1..a6175e20e6 100644 --- a/webrender/res/ps_blend.glsl +++ b/webrender/res/ps_blend.glsl @@ -91,33 +91,22 @@ void main(void) { * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ vec4 Contrast(vec4 Cs, float amount) { - return vec4(Cs.rgb * amount - 0.5 * amount + 0.5, 1.0); + return vec4(Cs.rgb * amount - 0.5 * amount + 0.5, Cs.a); } vec4 Invert(vec4 Cs, float amount) { - Cs.rgb /= Cs.a; - - vec3 color = mix(Cs.rgb, vec3(1.0) - Cs.rgb, amount); - - // Pre-multiply the alpha into the output value. - return vec4(color.rgb * Cs.a, Cs.a); + return vec4(mix(Cs.rgb, vec3(1.0) - Cs.rgb, amount), Cs.a); } vec4 Brightness(vec4 Cs, float amount) { - // Un-premultiply the input. - Cs.rgb /= Cs.a; - // Apply the brightness factor. // Resulting color needs to be clamped to output range // since we are pre-multiplying alpha in the shader. - vec3 color = clamp(Cs.rgb * amount, vec3(0.0), vec3(1.0)); - - // Pre-multiply the alpha into the output value. - return vec4(color.rgb * Cs.a, Cs.a); + return vec4(clamp(Cs.rgb * amount, vec3(0.0), vec3(1.0)), Cs.a); } vec4 Opacity(vec4 Cs, float amount) { - return Cs * amount; + return vec4(Cs.rgb, Cs.a * amount); } void main(void) { @@ -128,6 +117,9 @@ void main(void) { discard; } + // Un-premultiply the input. + Cs.rgb /= Cs.a; + switch (vOp) { case 0: oFragColor = Cs; @@ -147,5 +139,8 @@ void main(void) { default: oFragColor = vColorMat * Cs; } + + // Pre-multiply the alpha into the output value. + oFragColor.rgb *= oFragColor.a; } #endif diff --git a/wrench/reftests/filters/filter-contrast-gray-alpha-1-ref.yaml b/wrench/reftests/filters/filter-contrast-gray-alpha-1-ref.yaml new file mode 100644 index 0000000000..6b176d9570 --- /dev/null +++ b/wrench/reftests/filters/filter-contrast-gray-alpha-1-ref.yaml @@ -0,0 +1,13 @@ +--- +root: + items: + - type: stacking-context + bounds: [0, 0, 120, 120] + items: + - type: rect + bounds: [0, 0, 120, 120] + color: [255, 255, 255, 1] + + - type: rect + bounds: [10, 10, 100, 100] + color: [223, 223, 223, 1] diff --git a/wrench/reftests/filters/filter-contrast-gray-alpha-1.yaml b/wrench/reftests/filters/filter-contrast-gray-alpha-1.yaml new file mode 100644 index 0000000000..87758d3f4a --- /dev/null +++ b/wrench/reftests/filters/filter-contrast-gray-alpha-1.yaml @@ -0,0 +1,13 @@ +--- +root: + items: + - type: stacking-context + bounds: [0, 0, 120, 120] + items: + - type: stacking-context + bounds: [10, 10, 100, 100] + filters: contrast(0) + items: + - type: rect + bounds: [0, 0, 100, 100] + color: [128, 128, 128, 0.25] diff --git a/wrench/reftests/filters/reftest.list b/wrench/reftests/filters/reftest.list index c24406800c..2339954e63 100644 --- a/wrench/reftests/filters/reftest.list +++ b/wrench/reftests/filters/reftest.list @@ -9,6 +9,7 @@ == filter-brightness-2.yaml filter-brightness-2-ref.yaml == filter-brightness-3.yaml filter-brightness-3-ref.yaml == filter-brightness-4.yaml filter-brightness-4-ref.yaml +== filter-contrast-gray-alpha-1.yaml filter-contrast-gray-alpha-1-ref.yaml == filter-invert.yaml filter-invert-ref.yaml == filter-invert-2.yaml filter-invert-2-ref.yaml == filter-large-blur-radius.yaml filter-large-blur-radius.png