6 uniform sampler2D I_x_y_tex, I_t_tex;
7 uniform sampler2D diff_flow_tex, base_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 // Similar to packHalf2x16, but the two values share exponent, and are stored
15 // as 12-bit fixed point numbers multiplied by that exponent (the leading one
16 // can't be implicit in this kind of format). This allows us to store a much
17 // greater range of numbers (8-bit, ie., full fp32 range), and also gives us an
18 // extra mantissa bit. (Well, ostensibly two, but because the numbers have to
19 // be stored denormalized, we only really gain one.)
21 // The price we pay is that if the numbers are of very different magnitudes,
22 // the smaller number gets less precision.
23 uint pack_floats_shared(float a, float b)
25 float greatest = max(abs(a), abs(b));
27 // Find the exponent, increase it by one, and negate it.
28 // E.g., if the nonbiased exponent is 3, the number is between
29 // 2^3 and 2^4, so our normalization factor to get within -1..1
30 // is going to be 2^-4.
33 // exponent = -(exponent + 1);
38 // exponent = 252 - exponent;
39 uint e = floatBitsToUint(greatest) & 0x7f800000u;
40 float normalizer = uintBitsToFloat((252 << 23) - e);
42 // The exponent is the same range as fp32, so just copy it
43 // verbatim, shifted up to where the sign bit used to be.
46 // Quantize to 12 bits.
47 uint qa = uint(int(round(a * (normalizer * 2047.0))));
48 uint qb = uint(int(round(b * (normalizer * 2047.0))));
50 return (qa & 0xfffu) | ((qb & 0xfffu) << 12) | e;
55 // Read the flow (on top of the u0/v0 flow).
56 vec2 diff_flow = texture(diff_flow_tex, tc).xy;
57 float du = diff_flow.x;
58 float dv = diff_flow.y;
60 // Read the first derivatives.
61 vec2 I_x_y = texture(I_x_y_tex, tc).xy;
64 float I_t = texture(I_t_tex, tc).x;
66 // E_I term. Note that we don't square β_0, in line with DeepFlow,
67 // even though it's probably an error.
69 // TODO: Evaluate squaring β_0.
70 // FIXME: Should the penalizer be adjusted for 0..1 intensity range instead of 0..255?
71 // TODO: Multiply by some alpha.
72 float beta_0 = texture(beta_0_tex, tc).x;
73 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);
74 float A11 = k1 * I_x * I_x;
75 float A12 = k1 * I_x * I_y;
76 float A22 = k1 * I_y * I_y;
77 float b1 = -k1 * I_t * I_x;
78 float b2 = -k1 * I_t * I_y;
80 // Compute the second derivatives. First I_xx and I_xy.
81 vec2 I_x_y_m2 = textureOffset(I_x_y_tex, tc, ivec2(-2, 0)).xy;
82 vec2 I_x_y_m1 = textureOffset(I_x_y_tex, tc, ivec2(-1, 0)).xy;
83 vec2 I_x_y_p1 = textureOffset(I_x_y_tex, tc, ivec2( 1, 0)).xy;
84 vec2 I_x_y_p2 = textureOffset(I_x_y_tex, tc, ivec2( 2, 0)).xy;
85 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);
86 float I_xx = I_xx_yx.x;
87 float I_xy = I_xx_yx.y;
89 // And now I_yy; I_yx = I_xy, bar rounding differences, so we don't
90 // bother computing it. We still have to sample the x component,
91 // though, but we can throw it away immediately.
92 float I_y_m2 = textureOffset(I_x_y_tex, tc, ivec2(0, -2)).y;
93 float I_y_m1 = textureOffset(I_x_y_tex, tc, ivec2(0, -1)).y;
94 float I_y_p1 = textureOffset(I_x_y_tex, tc, ivec2(0, 1)).y;
95 float I_y_p2 = textureOffset(I_x_y_tex, tc, ivec2(0, 2)).y;
96 float I_yy = (I_y_p1 - I_y_m1) * (2.0/3.0) + (I_y_m2 - I_y_p2) * (1.0/12.0);
98 // Finally I_xt and I_yt. (We compute these as I_tx and I_yt.)
99 vec2 I_t_m2 = textureOffset(I_t_tex, tc, ivec2(-2, 0)).xy;
100 vec2 I_t_m1 = textureOffset(I_t_tex, tc, ivec2(-1, 0)).xy;
101 vec2 I_t_p1 = textureOffset(I_t_tex, tc, ivec2( 1, 0)).xy;
102 vec2 I_t_p2 = textureOffset(I_t_tex, tc, ivec2( 2, 0)).xy;
103 vec2 I_tx_ty = (I_t_p1 - I_t_m1) * (2.0/3.0) + (I_t_m2 - I_t_p2) * (1.0/12.0);
104 float I_xt = I_tx_ty.x;
105 float I_yt = I_tx_ty.y;
107 // E_G term. Same TODOs as E_I. Same normalization as beta_0
108 // (see derivatives.frag).
109 float beta_x = 1.0 / (I_xx * I_xx + I_xy * I_xy + 1e-7);
110 float beta_y = 1.0 / (I_xy * I_xy + I_yy * I_yy + 1e-7);
111 float k2 = inversesqrt(
112 beta_x * (I_xx * du + I_xy * dv + I_xt) * (I_xx * du + I_xy * dv + I_xt) +
113 beta_y * (I_xy * du + I_yy * dv + I_yt) * (I_xy * du + I_yy * dv + I_yt) +
115 float k_x = k2 * beta_x;
116 float k_y = k2 * beta_y;
117 A11 += k_x * I_xx * I_xx + k_y * I_xy * I_xy;
118 A12 += k_x * I_xx * I_xy + k_y * I_xy * I_yy;
119 A22 += k_x * I_xy * I_xy + k_y * I_yy * I_yy;
120 b1 -= k_x * I_xx * I_xt + k_y * I_xy * I_yt;
121 b2 -= k_x * I_xy * I_xt + k_y * I_yy * I_yt;
123 // E_S term, sans the part on the right-hand side that deals with
124 // the neighboring pixels.
125 // TODO: Multiply by some gamma.
126 float smooth_l = textureOffset(smoothness_x_tex, tc, ivec2(-1, 0)).x;
127 float smooth_r = texture(smoothness_x_tex, tc).x;
128 float smooth_d = textureOffset(smoothness_y_tex, tc, ivec2( 0, -1)).x;
129 float smooth_u = texture(smoothness_y_tex, tc).x;
130 A11 += smooth_l + smooth_r + smooth_d + smooth_u;
131 A22 += smooth_l + smooth_r + smooth_d + smooth_u;
133 // Laplacian of (u0, v0).
135 smooth_l * textureOffset(base_flow_tex, tc, ivec2(-1, 0)).xy +
136 smooth_r * textureOffset(base_flow_tex, tc, ivec2( 1, 0)).xy +
137 smooth_d * textureOffset(base_flow_tex, tc, ivec2( 0, -1)).xy +
138 smooth_u * textureOffset(base_flow_tex, tc, ivec2( 0, 1)).xy -
139 (smooth_l + smooth_r + smooth_d + smooth_u) * texture(base_flow_tex, tc).xy;
143 // Encode the equation down into four uint32s.
144 equation.x = floatBitsToUint(1.0 / A11);
145 equation.y = floatBitsToUint(A12);
146 equation.z = floatBitsToUint(1.0 / A22);
147 equation.w = pack_floats_shared(b1, b2);