6 uniform sampler2D I_x_y_tex, I_t_tex;
7 uniform sampler2D diff_flow_tex, flow_tex;
8 uniform sampler2D beta_0_tex;
9 uniform sampler2D smoothness_x_tex, smoothness_y_tex;
11 // TODO: Consider a specialized version for the case where we know that du = dv = 0,
12 // since we run so few iterations.
14 // The base flow needs to be normalized.
15 // TODO: Should we perhaps reduce this to a separate two-component
16 // texture when calculating the derivatives?
17 vec2 normalize_flow(vec3 flow)
19 return flow.xy / flow.z;
22 // This must be a macro, since the offset needs to be a constant expression.
23 #define get_flow(x_offs, y_offs) \
24 (normalize_flow(textureOffset(flow_tex, tc, ivec2((x_offs), (y_offs))).xyz) + \
25 textureOffset(diff_flow_tex, tc, ivec2((x_offs), (y_offs))).xy)
29 // Read the flow (on top of the u0/v0 flow).
30 vec2 diff_flow = texture(diff_flow_tex, tc).xy;
31 float du = diff_flow.x; // FIXME: convert to pixels?
32 float dv = diff_flow.y;
34 // Read the first derivatives.
35 vec2 I_x_y = texture(I_x_y_tex, tc).xy;
38 float I_t = texture(I_t_tex, tc).x;
40 // E_I term. Note that we don't square β_0, in line with DeepFlow,
41 // even though it's probably an error.
43 // TODO: Evaluate squaring β_0.
44 // FIXME: Should the penalizer be adjusted for 0..1 intensity range instead of 0..255?
45 // TODO: Multiply by some alpha.
46 float beta_0 = texture(beta_0_tex, tc).x;
47 float k1 = beta_0 * inversesqrt(beta_0 * (I_x * du + I_y * dv + I_t) * (I_x * du + I_y * dv + I_t) + 1e-6);
48 float A11 = k1 * I_x * I_x;
49 float A12 = k1 * I_x * I_y;
50 float A22 = k1 * I_y * I_y;
54 // Compute the second derivatives. First I_xx and I_xy.
55 vec2 I_x_y_m2 = textureOffset(I_x_y_tex, tc, ivec2(-2, 0)).xy;
56 vec2 I_x_y_m1 = textureOffset(I_x_y_tex, tc, ivec2(-1, 0)).xy;
57 vec2 I_x_y_p1 = textureOffset(I_x_y_tex, tc, ivec2( 1, 0)).xy;
58 vec2 I_x_y_p2 = textureOffset(I_x_y_tex, tc, ivec2( 2, 0)).xy;
59 vec2 I_xx_yx = (I_x_y_p1 - I_x_y_m1) * (2.0/3.0) + (I_x_y_m2 - I_x_y_p2) * (1.0/12.0);
60 float I_xx = I_xx_yx.x;
61 float I_xy = I_xx_yx.y;
63 // And now I_yy; I_yx = I_xy, bar rounding differences, so we don't
64 // bother computing it. We still have to sample the x component,
65 // though, but we can throw it away immediately.
66 float I_y_m2 = textureOffset(I_x_y_tex, tc, ivec2(0, -2)).y;
67 float I_y_m1 = textureOffset(I_x_y_tex, tc, ivec2(0, -1)).y;
68 float I_y_p1 = textureOffset(I_x_y_tex, tc, ivec2(0, 1)).y;
69 float I_y_p2 = textureOffset(I_x_y_tex, tc, ivec2(0, 2)).y;
70 float I_yy = (I_y_p1 - I_y_m1) * (2.0/3.0) + (I_y_m2 - I_y_p2) * (1.0/12.0);
72 // Finally I_xt and I_yt. (We compute these as I_tx and I_yt.)
73 vec2 I_t_m2 = textureOffset(I_t_tex, tc, ivec2(-2, 0)).xy;
74 vec2 I_t_m1 = textureOffset(I_t_tex, tc, ivec2(-1, 0)).xy;
75 vec2 I_t_p1 = textureOffset(I_t_tex, tc, ivec2( 1, 0)).xy;
76 vec2 I_t_p2 = textureOffset(I_t_tex, tc, ivec2( 2, 0)).xy;
77 vec2 I_tx_ty = (I_t_p1 - I_t_m1) * (2.0/3.0) + (I_t_m2 - I_t_p2) * (1.0/12.0);
78 float I_xt = I_tx_ty.x;
79 float I_yt = I_tx_ty.y;
81 // E_G term. Same TODOs as E_I. Same normalization as beta_0
82 // (see derivatives.frag).
83 float beta_x = 1.0 / (I_xx * I_xx + I_xy * I_xy + 1e-7);
84 float beta_y = 1.0 / (I_xy * I_xy + I_yy * I_yy + 1e-7);
85 float k2 = inversesqrt(
86 beta_x * (I_xx * du + I_xy * dv + I_xt) * (I_xx * du + I_xy * dv + I_xt) +
87 beta_y * (I_xy * du + I_yy * dv + I_yt) * (I_xy * du + I_yy * dv + I_yt) +
89 float k_x = k2 * beta_x;
90 float k_y = k2 * beta_y;
91 A11 += k_x * I_xx * I_xx + k_y * I_xy * I_xy;
92 A12 += k_x * I_xx * I_xy + k_y * I_xy * I_yy;
93 A22 += k_x * I_xy * I_xy + k_y * I_yy * I_yy;
94 b1 -= k_x * I_xx * I_xt + k_y * I_xy * I_yt;
95 b2 -= k_x * I_xy * I_xt + k_y * I_yy * I_yt;
97 // E_S term, sans the part on the right-hand side that deals with
98 // the neighboring pixels.
99 // TODO: Multiply by some gamma.
100 float smooth_l = textureOffset(smoothness_x_tex, tc, ivec2(-1, 0)).x;
101 float smooth_r = texture(smoothness_x_tex, tc).x;
102 float smooth_d = textureOffset(smoothness_y_tex, tc, ivec2( 0, -1)).x;
103 float smooth_u = texture(smoothness_y_tex, tc).x;
104 A11 -= smooth_l + smooth_r + smooth_d + smooth_u;
105 A22 -= smooth_l + smooth_r + smooth_d + smooth_u;
107 // Laplacian of (u0 + du, v0 + dv), sans the central term.
109 smooth_l * get_flow(-1, 0) +
110 smooth_r * get_flow(1, 0) +
111 smooth_d * get_flow(0, -1) +
112 smooth_u * get_flow(0, 1);
116 // The central term of the Laplacian, for (u0, v0) only.
117 // (The central term for (du, dv) is what we are solving for.)
118 vec2 central = (smooth_l + smooth_r + smooth_d + smooth_u) * normalize_flow(texture(flow_tex, tc).xyz);
122 // Encode the equation down into four uint32s.
123 equation.x = floatBitsToUint(1.0 / A11);
124 equation.y = floatBitsToUint(A12);
125 equation.z = floatBitsToUint(1.0 / A22);
126 equation.w = packHalf2x16(vec2(b1, b2));