+vec3 unpack_gradients(uint v)
+{
+ uint vi = v & 0xff;
+ uint xi = (v >> 8) & 0xfff;
+ uint yi = v >> 20;
+ vec3 r = vec3(xi * (1.0f / 4095.0f) - 0.5f, yi * (1.0f / 4095.0f) - 0.5f, vi * (1.0f / 255.0f));
+ return r;
+}
+
+// Note: The third variable is the actual pixel value.
+vec3 get_gradients(vec2 tc)
+{
+ vec3 grad = unpack_gradients(texture(grad0_tex, tc).x);
+
+ // Zero gradients outside the image. (We'd do this with a sampler,
+ // but we want the repeat behavior for the actual texels, in the
+ // z channel.)
+ if (any(lessThan(tc, vec2(0.0f))) || any(greaterThan(tc, vec2(1.0f)))) {
+ grad.xy = vec2(0.0f);
+ }
+
+ return grad;
+}
+