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