]> git.sesse.net Git - nageru/blob - equations.frag
Fix an algebra error in the E_I term.
[nageru] / equations.frag
1 #version 450 core
2
3 in vec2 tc;
4 out uvec4 equation;
5
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;
10
11 // TODO: Consider a specialized version for the case where we know that du = dv = 0,
12 // since we run so few iterations.
13
14
15 void main()
16 {
17         // Read the flow (on top of the u0/v0 flow).
18         vec2 diff_flow = texture(diff_flow_tex, tc).xy;
19         float du = diff_flow.x;
20         float dv = diff_flow.y;
21
22         // Read the first derivatives.
23         vec2 I_x_y = texture(I_x_y_tex, tc).xy;
24         float I_x = I_x_y.x;
25         float I_y = I_x_y.y;
26         float I_t = texture(I_t_tex, tc).x;
27
28         // E_I term. Note that we don't square β_0, in line with DeepFlow,
29         // even though it's probably an error.
30         //
31         // TODO: Evaluate squaring β_0.
32         // FIXME: Should the penalizer be adjusted for 0..1 intensity range instead of 0..255?
33         // TODO: Multiply by some alpha.
34         float beta_0 = texture(beta_0_tex, tc).x;
35         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);
36         float A11 = k1 * I_x * I_x;
37         float A12 = k1 * I_x * I_y;
38         float A22 = k1 * I_y * I_y;
39         float b1 = -k1 * I_t * I_x;
40         float b2 = -k1 * I_t * I_y;
41
42         // Compute the second derivatives. First I_xx and I_xy.
43         vec2 I_x_y_m2 = textureOffset(I_x_y_tex, tc, ivec2(-2,  0)).xy;
44         vec2 I_x_y_m1 = textureOffset(I_x_y_tex, tc, ivec2(-1,  0)).xy;
45         vec2 I_x_y_p1 = textureOffset(I_x_y_tex, tc, ivec2( 1,  0)).xy;
46         vec2 I_x_y_p2 = textureOffset(I_x_y_tex, tc, ivec2( 2,  0)).xy;
47         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);
48         float I_xx = I_xx_yx.x;
49         float I_xy = I_xx_yx.y;
50
51         // And now I_yy; I_yx = I_xy, bar rounding differences, so we don't
52         // bother computing it. We still have to sample the x component,
53         // though, but we can throw it away immediately.
54         float I_y_m2 = textureOffset(I_x_y_tex, tc, ivec2(0, -2)).y;
55         float I_y_m1 = textureOffset(I_x_y_tex, tc, ivec2(0, -1)).y;
56         float I_y_p1 = textureOffset(I_x_y_tex, tc, ivec2(0,  1)).y;
57         float I_y_p2 = textureOffset(I_x_y_tex, tc, ivec2(0,  2)).y;
58         float I_yy = (I_y_p1 - I_y_m1) * (2.0/3.0) + (I_y_m2 - I_y_p2) * (1.0/12.0);
59
60         // Finally I_xt and I_yt. (We compute these as I_tx and I_yt.)
61         vec2 I_t_m2 = textureOffset(I_t_tex, tc, ivec2(-2,  0)).xy;
62         vec2 I_t_m1 = textureOffset(I_t_tex, tc, ivec2(-1,  0)).xy;
63         vec2 I_t_p1 = textureOffset(I_t_tex, tc, ivec2( 1,  0)).xy;
64         vec2 I_t_p2 = textureOffset(I_t_tex, tc, ivec2( 2,  0)).xy;
65         vec2 I_tx_ty = (I_t_p1 - I_t_m1) * (2.0/3.0) + (I_t_m2 - I_t_p2) * (1.0/12.0);
66         float I_xt = I_tx_ty.x;
67         float I_yt = I_tx_ty.y;
68
69         // E_G term. Same TODOs as E_I. Same normalization as beta_0
70         // (see derivatives.frag).
71         float beta_x = 1.0 / (I_xx * I_xx + I_xy * I_xy + 1e-7);
72         float beta_y = 1.0 / (I_xy * I_xy + I_yy * I_yy + 1e-7);
73         float k2 = inversesqrt(
74                 beta_x * (I_xx * du + I_xy * dv + I_xt) * (I_xx * du + I_xy * dv + I_xt) +
75                 beta_y * (I_xy * du + I_yy * dv + I_yt) * (I_xy * du + I_yy * dv + I_yt) +
76                 1e-6);
77         float k_x = k2 * beta_x;
78         float k_y = k2 * beta_y;
79         A11 += k_x * I_xx * I_xx + k_y * I_xy * I_xy;
80         A12 += k_x * I_xx * I_xy + k_y * I_xy * I_yy;
81         A22 += k_x * I_xy * I_xy + k_y * I_yy * I_yy;
82         b1 -= k_x * I_xx * I_xt + k_y * I_xy * I_yt;
83         b2 -= k_x * I_xy * I_xt + k_y * I_yy * I_yt;
84
85         // E_S term, sans the part on the right-hand side that deals with
86         // the neighboring pixels.
87         // TODO: Multiply by some gamma.
88         float smooth_l = textureOffset(smoothness_x_tex, tc, ivec2(-1,  0)).x;
89         float smooth_r = texture(smoothness_x_tex, tc).x;
90         float smooth_d = textureOffset(smoothness_y_tex, tc, ivec2( 0, -1)).x;
91         float smooth_u = texture(smoothness_y_tex, tc).x;
92         A11 += smooth_l + smooth_r + smooth_d + smooth_u;
93         A22 += smooth_l + smooth_r + smooth_d + smooth_u;
94
95         // Laplacian of (u0, v0).
96         vec2 laplacian =
97                 smooth_l * textureOffset(base_flow_tex, tc, ivec2(-1,  0)).xy +
98                 smooth_r * textureOffset(base_flow_tex, tc, ivec2( 1,  0)).xy +
99                 smooth_d * textureOffset(base_flow_tex, tc, ivec2( 0, -1)).xy +
100                 smooth_u * textureOffset(base_flow_tex, tc, ivec2( 0,  1)).xy -
101                 (smooth_l + smooth_r + smooth_d + smooth_u) * texture(base_flow_tex, tc).xy;
102         b1 += laplacian.x;
103         b2 += laplacian.y;
104
105         // Encode the equation down into four uint32s.
106         equation.x = floatBitsToUint(1.0 / A11);
107         equation.y = floatBitsToUint(A12);
108         equation.z = floatBitsToUint(1.0 / A22);
109         equation.w = packHalf2x16(vec2(b1, b2));
110 }