Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the brightness and invert filters to work with premultiplied alpha. #1722

Merged
merged 1 commit into from Sep 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions webrender/res/ps_blend.glsl
Expand Up @@ -116,7 +116,12 @@ 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);
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);
}

vec4 Saturate(vec4 Cs, float amount) {
Expand All @@ -132,7 +137,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-2-ref.yaml
@@ -0,0 +1,9 @@
---
root:
items:
- type: stacking-context
bounds: [0, 0, 120, 120]
items:
- type: rect
bounds: [0, 0, 120, 120]
color: [0, 0, 255, 1]
17 changes: 17 additions & 0 deletions wrench/reftests/filters/filter-invert-2.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, 255, 1]

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