Skip to content

Commit

Permalink
Fix the brightness and invert filters to work with premultiplied alpha.
Browse files Browse the repository at this point in the history
Fixes #1507.
Fixes #1508.
  • Loading branch information
gw3583 committed Sep 25, 2017
1 parent 6cc2c6a commit b2f8f0c
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 2 deletions.
16 changes: 14 additions & 2 deletions webrender/res/ps_blend.glsl
Expand Up @@ -116,7 +116,10 @@ vec4 HueRotate(vec4 Cs, float amount) {
}

vec4 Invert(vec4 Cs, float amount) {
return mix(Cs, vec4(1.0, 1.0, 1.0, Cs.a) - vec4(Cs.rgb, 0.0), amount);
vec4 color = mix(Cs, vec4(Cs.a) - vec4(Cs.rgb, 0.0), amount);

// Pre-multiply the alpha into the output value.
return vec4(color.rgb * clamp(color.a, 0.0, 1.0), color.a);
}

vec4 Saturate(vec4 Cs, float amount) {
Expand All @@ -132,7 +135,16 @@ vec4 Sepia(vec4 Cs, float amount) {
}

vec4 Brightness(vec4 Cs, float amount) {
return vec4(Cs.rgb * amount, Cs.a);
// 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);
}

vec4 Opacity(vec4 Cs, float amount) {
Expand Down
9 changes: 9 additions & 0 deletions wrench/reftests/filters/filter-brightness-2-ref.yaml
@@ -0,0 +1,9 @@
---
root:
items:
- type: stacking-context
bounds: [0, 0, 120, 120]
items:
- type: rect
bounds: [10, 10, 100, 100]
color: [0, 0, 0, 1]
13 changes: 13 additions & 0 deletions wrench/reftests/filters/filter-brightness-2.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: brightness(0)
items:
- type: rect
bounds: [0, 0, 100, 100]
color: [255, 0, 0, 1]
9 changes: 9 additions & 0 deletions wrench/reftests/filters/filter-brightness-3-ref.yaml
@@ -0,0 +1,9 @@
---
root:
items:
- type: stacking-context
bounds: [0, 0, 120, 120]
items:
- type: rect
bounds: [10, 10, 100, 100]
color: [0, 128, 0, 1]
13 changes: 13 additions & 0 deletions wrench/reftests/filters/filter-brightness-3.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: brightness(4)
items:
- type: rect
bounds: [0, 0, 100, 100]
color: [0, 32, 0, 1]
9 changes: 9 additions & 0 deletions wrench/reftests/filters/filter-brightness-4-ref.yaml
@@ -0,0 +1,9 @@
---
root:
items:
- type: stacking-context
bounds: [0, 0, 120, 120]
items:
- type: rect
bounds: [10, 10, 100, 100]
color: [0, 64, 0, 1]
13 changes: 13 additions & 0 deletions wrench/reftests/filters/filter-brightness-4.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: brightness(0.25)
items:
- type: rect
bounds: [0, 0, 100, 100]
color: [0, 255, 0, 1]
13 changes: 13 additions & 0 deletions wrench/reftests/filters/filter-brightness-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: [0, 0, 0, 1]

- type: rect
bounds: [10, 10, 100, 100]
color: [64, 64, 64, 1]
17 changes: 17 additions & 0 deletions wrench/reftests/filters/filter-brightness.yaml
@@ -0,0 +1,17 @@
---
root:
items:
- type: stacking-context
bounds: [0, 0, 120, 120]
items:
- type: rect
bounds: [0, 0, 120, 120]
color: [0, 0, 0, 1]

- type: stacking-context
bounds: [10, 10, 100, 100]
filters: brightness(2)
items:
- type: rect
bounds: [0, 0, 100, 100]
color: [255, 255, 255, 0.25]
9 changes: 9 additions & 0 deletions wrench/reftests/filters/filter-invert-ref.yaml
@@ -0,0 +1,9 @@
---
root:
items:
- type: stacking-context
bounds: [0, 0, 120, 120]
items:
- type: rect
bounds: [10, 10, 100, 100]
color: [0, 0, 0, 0.25]
13 changes: 13 additions & 0 deletions wrench/reftests/filters/filter-invert.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: invert(1)
items:
- type: rect
bounds: [0, 0, 100, 100]
color: [255, 255, 255, 0.25]
5 changes: 5 additions & 0 deletions wrench/reftests/filters/reftest.list
Expand Up @@ -5,3 +5,8 @@
== opacity.yaml opacity-ref.yaml
== opacity-combined.yaml opacity-combined-ref.yaml
== opacity-overlap.yaml opacity-overlap-ref.yaml
== filter-brightness.yaml filter-brightness-ref.yaml
== 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-invert.yaml filter-invert-ref.yaml

0 comments on commit b2f8f0c

Please sign in to comment.