]> git.sesse.net Git - movit/blobdiff - padding_effect.frag
Release Movit 1.3.2. (From a branch, since I do not want to break ABI compatibility...
[movit] / padding_effect.frag
index 344b34fb4677faf419fb4c7036ec86a6a993bf53..80b9633bb17e9f71fb5b6cce6c41a7ae6bad1838 100644 (file)
@@ -1,30 +1,25 @@
-uniform vec2 PREFIX(offset);
-uniform vec2 PREFIX(scale);
-uniform vec2 PREFIX(texcoord_min);
-uniform vec2 PREFIX(texcoord_max);
+// Implicit uniforms:
+// uniform vec2 PREFIX(offset);
+// uniform vec2 PREFIX(scale);
+//
+// uniform vec2 PREFIX(normalized_coords_to_texels);
+// uniform vec2 PREFIX(offset_bottomleft);
+// uniform vec2 PREFIX(offset_topright);
 
 vec4 FUNCNAME(vec2 tc) {
        tc -= PREFIX(offset);
        tc *= PREFIX(scale);
 
-#if 1
-       if (any(lessThan(tc, PREFIX(texcoord_min))) ||
-           any(greaterThan(tc, PREFIX(texcoord_max)))) {
-               return PREFIX(border_color);
-       }
-#endif
-       if (any(lessThan(tc, vec2(0.0))) ||
-           any(greaterThan(tc, vec2(1.0)))) {
-               return PREFIX(border_color);
-       } 
-#if 0
-       // In theory, maybe we should test on the outmost textel centers
-       // (e.g. (0.5/width, 0.5/height) for the top-left) instead of the
-       // texture border. However, since we only support integer padding
-       if (any(lessThan(tc, vec2(0.0))) || any(greaterThan(tc, vec2(1.0)))) {
+       vec2 tc_texels = tc * PREFIX(normalized_coords_to_texels);
+       vec2 coverage_bottomleft = clamp(tc_texels + PREFIX(offset_bottomleft), 0.0f, 1.0f);
+       vec2 coverare_topright = clamp(PREFIX(offset_topright) - tc_texels, 0.0f, 1.0f);
+       vec2 coverage_both = coverage_bottomleft * coverare_topright;
+       float coverage = coverage_both.x * coverage_both.y;
+
+       if (coverage <= 0.0f) {
+               // Short-circuit in case the underlying function is expensive to call.
                return PREFIX(border_color);
+       } else {
+               return mix(PREFIX(border_color), INPUT(tc), coverage);
        }
-#endif
-
-       return INPUT(tc);
 }