3 in vec3 tc0, tc_left0, tc_down0;
4 in vec3 tc1, tc_left1, tc_down1;
6 out uvec4 equation_red, equation_black;
8 uniform sampler2DArray I_x_y_tex, I_t_tex;
9 uniform sampler2DArray diff_flow_tex, base_flow_tex;
10 uniform sampler2DArray beta_0_tex;
11 uniform sampler2DArray diffusivity_tex;
13 // Relative weighting of intensity term.
16 // Relative weighting of gradient term.
19 uniform bool zero_diff_flow;
21 // Similar to packHalf2x16, but the two values share exponent, and are stored
22 // as 12-bit fixed point numbers multiplied by that exponent (the leading one
23 // can't be implicit in this kind of format). This allows us to store a much
24 // greater range of numbers (8-bit, ie., full fp32 range), and also gives us an
25 // extra mantissa bit. (Well, ostensibly two, but because the numbers have to
26 // be stored denormalized, we only really gain one.)
28 // The price we pay is that if the numbers are of very different magnitudes,
29 // the smaller number gets less precision.
30 uint pack_floats_shared(float a, float b)
32 float greatest = max(abs(a), abs(b));
34 // Find the exponent, increase it by one, and negate it.
35 // E.g., if the nonbiased exponent is 3, the number is between
36 // 2^3 and 2^4, so our normalization factor to get within -1..1
37 // is going to be 2^-4.
40 // exponent = -(exponent + 1);
45 // exponent = 252 - exponent;
46 uint e = floatBitsToUint(greatest) & 0x7f800000u;
47 float normalizer = uintBitsToFloat((252 << 23) - e);
49 // The exponent is the same range as fp32, so just copy it
50 // verbatim, shifted up to where the sign bit used to be.
53 // Quantize to 12 bits.
54 uint qa = uint(int(round(a * (normalizer * 2047.0))));
55 uint qb = uint(int(round(b * (normalizer * 2047.0))));
57 return (qa & 0xfffu) | ((qb & 0xfffu) << 12) | e;
60 float zero_if_outside_border(vec4 val)
63 // We hit the border (or more like half-way to it), so zero smoothness.
70 uvec4 compute_equation(vec3 tc, vec3 tc_left, vec3 tc_down)
72 // Read the flow (on top of the u0/v0 flow).
77 vec2 diff_flow = texture(diff_flow_tex, tc).xy;
82 // Read the first derivatives.
83 vec2 I_x_y = texture(I_x_y_tex, tc).xy;
86 float I_t = texture(I_t_tex, tc).x;
88 // E_I term. Note that we don't square β_0, in line with DeepFlow;
89 // it's probably an error (see variational_refinement.txt),
90 // but squaring it seems to give worse results.
91 float beta_0 = texture(beta_0_tex, tc).x;
92 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);
93 float A11 = k1 * I_x * I_x;
94 float A12 = k1 * I_x * I_y;
95 float A22 = k1 * I_y * I_y;
96 float b1 = -k1 * I_t * I_x;
97 float b2 = -k1 * I_t * I_y;
99 // Compute the second derivatives. First I_xx and I_xy.
100 vec2 I_x_y_m2 = textureOffset(I_x_y_tex, tc, ivec2(-2, 0)).xy;
101 vec2 I_x_y_m1 = textureOffset(I_x_y_tex, tc, ivec2(-1, 0)).xy;
102 vec2 I_x_y_p1 = textureOffset(I_x_y_tex, tc, ivec2( 1, 0)).xy;
103 vec2 I_x_y_p2 = textureOffset(I_x_y_tex, tc, ivec2( 2, 0)).xy;
104 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);
105 float I_xx = I_xx_yx.x;
106 float I_xy = I_xx_yx.y;
108 // And now I_yy; I_yx = I_xy, bar rounding differences, so we don't
109 // bother computing it. We still have to sample the x component,
110 // though, but we can throw it away immediately.
111 float I_y_m2 = textureOffset(I_x_y_tex, tc, ivec2(0, -2)).y;
112 float I_y_m1 = textureOffset(I_x_y_tex, tc, ivec2(0, -1)).y;
113 float I_y_p1 = textureOffset(I_x_y_tex, tc, ivec2(0, 1)).y;
114 float I_y_p2 = textureOffset(I_x_y_tex, tc, ivec2(0, 2)).y;
115 float I_yy = (I_y_p1 - I_y_m1) * (2.0/3.0) + (I_y_m2 - I_y_p2) * (1.0/12.0);
117 // Finally I_xt and I_yt. (We compute these as I_tx and I_yt.)
118 vec2 I_t_m2 = textureOffset(I_t_tex, tc, ivec2(-2, 0)).xy;
119 vec2 I_t_m1 = textureOffset(I_t_tex, tc, ivec2(-1, 0)).xy;
120 vec2 I_t_p1 = textureOffset(I_t_tex, tc, ivec2( 1, 0)).xy;
121 vec2 I_t_p2 = textureOffset(I_t_tex, tc, ivec2( 2, 0)).xy;
122 vec2 I_tx_ty = (I_t_p1 - I_t_m1) * (2.0/3.0) + (I_t_m2 - I_t_p2) * (1.0/12.0);
123 float I_xt = I_tx_ty.x;
124 float I_yt = I_tx_ty.y;
126 // E_G term. Same normalization as beta_0 (see derivatives.frag).
127 float beta_x = 1.0 / (I_xx * I_xx + I_xy * I_xy + 1e-7);
128 float beta_y = 1.0 / (I_xy * I_xy + I_yy * I_yy + 1e-7);
129 float k2 = gamma * inversesqrt(
130 beta_x * (I_xx * du + I_xy * dv + I_xt) * (I_xx * du + I_xy * dv + I_xt) +
131 beta_y * (I_xy * du + I_yy * dv + I_yt) * (I_xy * du + I_yy * dv + I_yt) +
133 float k_x = k2 * beta_x;
134 float k_y = k2 * beta_y;
135 A11 += k_x * I_xx * I_xx + k_y * I_xy * I_xy;
136 A12 += k_x * I_xx * I_xy + k_y * I_xy * I_yy;
137 A22 += k_x * I_xy * I_xy + k_y * I_yy * I_yy;
138 b1 -= k_x * I_xx * I_xt + k_y * I_xy * I_yt;
139 b2 -= k_x * I_xy * I_xt + k_y * I_yy * I_yt;
141 // E_S term, sans the part on the right-hand side that deals with
142 // the neighboring pixels. The gamma is multiplied in in smoothness.frag.
144 // Note that we sample in-between two texels, which gives us the 0.5 *
145 // (x[-1] + x[0]) part for free. If one of the texels is a border
146 // texel, it will have zero alpha, and zero_if_outside_border() will
147 // set smoothness to zero.
148 float smooth_l = zero_if_outside_border(texture(diffusivity_tex, tc_left));
149 float smooth_r = zero_if_outside_border(textureOffset(diffusivity_tex, tc_left, ivec2(1, 0)));
150 float smooth_d = zero_if_outside_border(texture(diffusivity_tex, tc_down));
151 float smooth_u = zero_if_outside_border(textureOffset(diffusivity_tex, tc_down, ivec2(0, 1)));
152 A11 += smooth_l + smooth_r + smooth_d + smooth_u;
153 A22 += smooth_l + smooth_r + smooth_d + smooth_u;
155 // Laplacian of (u0, v0).
157 smooth_l * textureOffset(base_flow_tex, tc, ivec2(-1, 0)).xy +
158 smooth_r * textureOffset(base_flow_tex, tc, ivec2( 1, 0)).xy +
159 smooth_d * textureOffset(base_flow_tex, tc, ivec2( 0, -1)).xy +
160 smooth_u * textureOffset(base_flow_tex, tc, ivec2( 0, 1)).xy -
161 (smooth_l + smooth_r + smooth_d + smooth_u) * texture(base_flow_tex, tc).xy;
165 // Encode the equation down into four uint32s.
167 ret.x = floatBitsToUint(1.0 / A11);
168 ret.y = floatBitsToUint(A12);
169 ret.z = floatBitsToUint(1.0 / A22);
170 ret.w = pack_floats_shared(b1, b2);
176 uvec4 eq0 = compute_equation(tc0, tc_left0, tc_down0);
177 uvec4 eq1 = compute_equation(tc1, tc_left1, tc_down1);
179 if ((int(round(line_offset)) & 1) == 1) {
180 // Odd line, so the right value is red.
182 equation_black = eq0;
185 equation_black = eq1;