-uniform sampler2D flow_tex, grad0_tex, image0_tex, image1_tex;
-uniform vec2 image_size, inv_image_size;
+uniform sampler2DArray flow_tex, image_tex;
+uniform usampler2DArray grad_tex; // Also contains the corresponding reference image.
+uniform vec2 inv_image_size, inv_prev_level_size;
+uniform uint patch_size;
+uniform uint num_iterations;
+
+vec3 unpack_gradients(uint v)
+{
+ uint vi = v & 0xffu;
+ uint xi = (v >> 8) & 0xfffu;
+ 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(vec3 tc)
+{
+ vec3 grad = unpack_gradients(texture(grad_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.xy, vec2(0.0f))) || any(greaterThan(tc.xy, vec2(1.0f)))) {
+ grad.xy = vec2(0.0f);
+ }
+
+ return grad;
+}