]> git.sesse.net Git - movit/blobdiff - overlay_effect.frag
Add the rest of the files for the premultiplied alpha commit.
[movit] / overlay_effect.frag
index 38970eab7e85469943353b85e9690d87ba59e5a8..36c1a7996cf48e0112908d93aa1b9634698ed2fa 100644 (file)
@@ -1,15 +1,22 @@
-// If we didn't have to worry about alpha in the bottom layer,
-// this would be a simple mix(). However, since people might
-// compose multiple layers together and we don't really have
-// any control over the order, it's better to do it right.
+// It's actually (but surprisingly) not correct to do a mix() here;
+// it would be if we had postmultiplied alpha and didn't have to worry
+// about alpha in the bottom layer, but given that we use premultiplied
+// alpha all over, top shouldn't actually be multiplied by anything.
 //
 // These formulas come from Wikipedia:
 //
 //   http://en.wikipedia.org/wiki/Alpha_compositing
 //
 // These formulas come from Wikipedia:
 //
 //   http://en.wikipedia.org/wiki/Alpha_compositing
+//
+// We use the associative version given. However, note that since we want
+// _output_ to be premultiplied, C_o from Wikipedia is not what we want,
+// but rather c_o (which is not explicitly given, but obviously is just
+// C_o without the division by alpha_o).
 
 vec4 FUNCNAME(vec2 tc) {
        vec4 bottom = INPUT1(tc);
        vec4 top = INPUT2(tc);
 
 vec4 FUNCNAME(vec2 tc) {
        vec4 bottom = INPUT1(tc);
        vec4 top = INPUT2(tc);
+#if 0
+       // Postmultiplied version.
        float new_alpha = mix(bottom.a, 1.0, top.a);
        if (new_alpha < 1e-6) {
                // new_alpha = 0 only if top.a = bottom.a = 0, at least as long as
        float new_alpha = mix(bottom.a, 1.0, top.a);
        if (new_alpha < 1e-6) {
                // new_alpha = 0 only if top.a = bottom.a = 0, at least as long as
@@ -23,4 +30,7 @@ vec4 FUNCNAME(vec2 tc) {
                vec3 color = premultiplied_color / new_alpha;
                return vec4(color.r, color.g, color.b, new_alpha);
        }
                vec3 color = premultiplied_color / new_alpha;
                return vec4(color.r, color.g, color.b, new_alpha);
        }
+#else
+       return top + (1.0 - top.a) * bottom;
+#endif
 }
 }