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