]> git.sesse.net Git - nageru/blob - derivatives.frag
Allow symlinked frame files. Useful for testing.
[nageru] / derivatives.frag
1 #version 450 core
2
3 in vec3 tc;
4 out vec2 derivatives;
5 out float beta_0;
6
7 uniform sampler2DArray tex;
8
9 void main()
10 {
11         float x_m2 = textureOffset(tex, tc, ivec2(-2,  0)).x;
12         float x_m1 = textureOffset(tex, tc, ivec2(-1,  0)).x;
13         float x_p1 = textureOffset(tex, tc, ivec2( 1,  0)).x;
14         float x_p2 = textureOffset(tex, tc, ivec2( 2,  0)).x;
15
16         float y_m2 = textureOffset(tex, tc, ivec2( 0, -2)).x;
17         float y_m1 = textureOffset(tex, tc, ivec2( 0, -1)).x;
18         float y_p1 = textureOffset(tex, tc, ivec2( 0,  1)).x;
19         float y_p2 = textureOffset(tex, tc, ivec2( 0,  2)).x;
20
21         derivatives.x = (x_p1 - x_m1) * (2.0/3.0) + (x_m2 - x_p2) * (1.0/12.0);
22         derivatives.y = (y_p1 - y_m1) * (2.0/3.0) + (y_m2 - y_p2) * (1.0/12.0);
23
24         // The nudge term in the square root in the DeepFlow paper is ζ² = 0.1² = 0.01.
25         // But this is assuming a 0..255 level. Given the nonlinearities in the expression
26         // where β_0 appears, there's no 100% equivalent way to adjust this
27         // constant that I can see, but taking it to (0.1/255)² ~= 1.53e-7 ~=
28         // 1e-7 ought to be good enough. I guess the basic idea is that it
29         // will only matter for near-zero derivatives anyway. I am a tiny
30         // bit worried about fp16 precision when storing these numbers, but OK.
31         beta_0 = 1.0 / (derivatives.x * derivatives.x + derivatives.y * derivatives.y + 1e-7);
32 }