]> git.sesse.net Git - nageru/blob - equations.frag
Compute diffusivity instead of smoothness, which saves a flow-size texture; shaves...
[nageru] / equations.frag
1 #version 450 core
2
3 in vec2 tc, tc_left, tc_down;
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 diffusivity_tex;
10
11 // Relative weighting of intensity term.
12 uniform float delta;
13
14 // Relative weighting of gradient term.
15 uniform float gamma;
16
17 uniform bool zero_diff_flow;
18
19 // Similar to packHalf2x16, but the two values share exponent, and are stored
20 // as 12-bit fixed point numbers multiplied by that exponent (the leading one
21 // can't be implicit in this kind of format). This allows us to store a much
22 // greater range of numbers (8-bit, ie., full fp32 range), and also gives us an
23 // extra mantissa bit. (Well, ostensibly two, but because the numbers have to
24 // be stored denormalized, we only really gain one.)
25 //
26 // The price we pay is that if the numbers are of very different magnitudes,
27 // the smaller number gets less precision.
28 uint pack_floats_shared(float a, float b)
29 {
30         float greatest = max(abs(a), abs(b));
31
32         // Find the exponent, increase it by one, and negate it.
33         // E.g., if the nonbiased exponent is 3, the number is between
34         // 2^3 and 2^4, so our normalization factor to get within -1..1
35         // is going to be 2^-4.
36         //
37         // exponent -= 127;
38         // exponent = -(exponent + 1);
39         // exponent += 127;
40         //
41         // is the same as
42         //
43         // exponent = 252 - exponent;
44         uint e = floatBitsToUint(greatest) & 0x7f800000u;
45         float normalizer = uintBitsToFloat((252 << 23) - e);
46
47         // The exponent is the same range as fp32, so just copy it
48         // verbatim, shifted up to where the sign bit used to be.
49         e <<= 1;
50
51         // Quantize to 12 bits.
52         uint qa = uint(int(round(a * (normalizer * 2047.0))));
53         uint qb = uint(int(round(b * (normalizer * 2047.0))));
54
55         return (qa & 0xfffu) | ((qb & 0xfffu) << 12) | e;
56 }
57
58 float zero_if_outside_border(vec4 val)
59 {
60         if (val.w < 1.0f) {
61                 // We hit the border (or more like half-way to it), so zero smoothness.
62                 return 0.0f;
63         } else {
64                 return val.x;
65         }
66 }
67
68 void main()
69 {
70         // Read the flow (on top of the u0/v0 flow).
71         float du, dv;
72         if (zero_diff_flow) {
73                 du = dv = 0.0f;
74         } else {
75                 vec2 diff_flow = texture(diff_flow_tex, tc).xy;
76                 du = diff_flow.x;
77                 dv = diff_flow.y;
78         }
79
80         // Read the first derivatives.
81         vec2 I_x_y = texture(I_x_y_tex, tc).xy;
82         float I_x = I_x_y.x;
83         float I_y = I_x_y.y;
84         float I_t = texture(I_t_tex, tc).x;
85
86         // E_I term. Note that we don't square β_0, in line with DeepFlow;
87         // it's probably an error (see variational_refinement.txt),
88         // but squaring it seems to give worse results.
89         float beta_0 = texture(beta_0_tex, tc).x;
90         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);
91         float A11 = k1 * I_x * I_x;
92         float A12 = k1 * I_x * I_y;
93         float A22 = k1 * I_y * I_y;
94         float b1 = -k1 * I_t * I_x;
95         float b2 = -k1 * I_t * I_y;
96
97         // Compute the second derivatives. First I_xx and I_xy.
98         vec2 I_x_y_m2 = textureOffset(I_x_y_tex, tc, ivec2(-2,  0)).xy;
99         vec2 I_x_y_m1 = textureOffset(I_x_y_tex, tc, ivec2(-1,  0)).xy;
100         vec2 I_x_y_p1 = textureOffset(I_x_y_tex, tc, ivec2( 1,  0)).xy;
101         vec2 I_x_y_p2 = textureOffset(I_x_y_tex, tc, ivec2( 2,  0)).xy;
102         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);
103         float I_xx = I_xx_yx.x;
104         float I_xy = I_xx_yx.y;
105
106         // And now I_yy; I_yx = I_xy, bar rounding differences, so we don't
107         // bother computing it. We still have to sample the x component,
108         // though, but we can throw it away immediately.
109         float I_y_m2 = textureOffset(I_x_y_tex, tc, ivec2(0, -2)).y;
110         float I_y_m1 = textureOffset(I_x_y_tex, tc, ivec2(0, -1)).y;
111         float I_y_p1 = textureOffset(I_x_y_tex, tc, ivec2(0,  1)).y;
112         float I_y_p2 = textureOffset(I_x_y_tex, tc, ivec2(0,  2)).y;
113         float I_yy = (I_y_p1 - I_y_m1) * (2.0/3.0) + (I_y_m2 - I_y_p2) * (1.0/12.0);
114
115         // Finally I_xt and I_yt. (We compute these as I_tx and I_yt.)
116         vec2 I_t_m2 = textureOffset(I_t_tex, tc, ivec2(-2,  0)).xy;
117         vec2 I_t_m1 = textureOffset(I_t_tex, tc, ivec2(-1,  0)).xy;
118         vec2 I_t_p1 = textureOffset(I_t_tex, tc, ivec2( 1,  0)).xy;
119         vec2 I_t_p2 = textureOffset(I_t_tex, tc, ivec2( 2,  0)).xy;
120         vec2 I_tx_ty = (I_t_p1 - I_t_m1) * (2.0/3.0) + (I_t_m2 - I_t_p2) * (1.0/12.0);
121         float I_xt = I_tx_ty.x;
122         float I_yt = I_tx_ty.y;
123
124         // E_G term. Same normalization as beta_0 (see derivatives.frag).
125         float beta_x = 1.0 / (I_xx * I_xx + I_xy * I_xy + 1e-7);
126         float beta_y = 1.0 / (I_xy * I_xy + I_yy * I_yy + 1e-7);
127         float k2 = gamma * inversesqrt(
128                 beta_x * (I_xx * du + I_xy * dv + I_xt) * (I_xx * du + I_xy * dv + I_xt) +
129                 beta_y * (I_xy * du + I_yy * dv + I_yt) * (I_xy * du + I_yy * dv + I_yt) +
130                 1e-6);
131         float k_x = k2 * beta_x;
132         float k_y = k2 * beta_y;
133         A11 += k_x * I_xx * I_xx + k_y * I_xy * I_xy;
134         A12 += k_x * I_xx * I_xy + k_y * I_xy * I_yy;
135         A22 += k_x * I_xy * I_xy + k_y * I_yy * I_yy;
136         b1 -= k_x * I_xx * I_xt + k_y * I_xy * I_yt;
137         b2 -= k_x * I_xy * I_xt + k_y * I_yy * I_yt;
138
139         // E_S term, sans the part on the right-hand side that deals with
140         // the neighboring pixels. The gamma is multiplied in in smoothness.frag.
141         //
142         // Note that we sample in-between two texels, which gives us the 0.5 *
143         // (x[-1] + x[0]) part for free. If one of the texels is a border
144         // texel, it will have zero alpha, and zero_if_outside_border() will
145         // set smoothness to zero.
146         float smooth_l = zero_if_outside_border(texture(diffusivity_tex, tc_left));
147         float smooth_r = zero_if_outside_border(textureOffset(diffusivity_tex, tc_left, ivec2(1, 0)));
148         float smooth_d = zero_if_outside_border(texture(diffusivity_tex, tc_down));
149         float smooth_u = zero_if_outside_border(textureOffset(diffusivity_tex, tc_down, ivec2(0, 1)));
150         A11 += smooth_l + smooth_r + smooth_d + smooth_u;
151         A22 += smooth_l + smooth_r + smooth_d + smooth_u;
152
153         // Laplacian of (u0, v0).
154         vec2 laplacian =
155                 smooth_l * textureOffset(base_flow_tex, tc, ivec2(-1,  0)).xy +
156                 smooth_r * textureOffset(base_flow_tex, tc, ivec2( 1,  0)).xy +
157                 smooth_d * textureOffset(base_flow_tex, tc, ivec2( 0, -1)).xy +
158                 smooth_u * textureOffset(base_flow_tex, tc, ivec2( 0,  1)).xy -
159                 (smooth_l + smooth_r + smooth_d + smooth_u) * texture(base_flow_tex, tc).xy;
160         b1 += laplacian.x;
161         b2 += laplacian.y;
162
163         // Encode the equation down into four uint32s.
164         equation.x = floatBitsToUint(1.0 / A11);
165         equation.y = floatBitsToUint(A12);
166         equation.z = floatBitsToUint(1.0 / A22);
167         equation.w = pack_floats_shared(b1, b2);
168 }