]> git.sesse.net Git - nageru/blob - sor.frag
Fix a done TODO (gamma is for E_G, not E_S, and we multiply in the alpha in the smoot...
[nageru] / sor.frag
1 #version 450 core
2
3 in vec2 tc;
4 out vec2 diff_flow;
5
6 uniform sampler2D diff_flow_tex, smoothness_x_tex, smoothness_y_tex;
7 uniform usampler2D equation_tex;
8
9 // See pack_floats_shared() in equations.frag.
10 vec2 unpack_floats_shared(uint c)
11 {
12         // Recover the exponent, and multiply it in. Add one because
13         // we have denormalized mantissas, then another one because we
14         // already reduced the exponent by one. Then subtract 20, because
15         // we are going to shift up the number by 20 below to recover the sign bits.
16         float normalizer = uintBitsToFloat(((c >> 1) & 0x7f800000u) - (18 << 23));
17         normalizer *= (1.0 / 2047.0);
18
19         // Shift the values up so that we recover the sign bit, then normalize.
20         float a = int(uint(c & 0x000fffu) << 20) * normalizer;
21         float b = int(uint(c & 0xfff000u) << 8) * normalizer;
22
23         return vec2(a, b);
24 }
25
26 void main()
27 {
28         uvec4 equation = texture(equation_tex, tc);
29         float inv_A11 = uintBitsToFloat(equation.x);
30         float A12 = uintBitsToFloat(equation.y);
31         float inv_A22 = uintBitsToFloat(equation.z);
32         vec2 b = unpack_floats_shared(equation.w);
33
34         // Subtract the missing terms from the right-hand side
35         // (it couldn't be done earlier, because we didn't know
36         // the values of the neighboring pixels; they change for
37         // each SOR iteration).
38         float smooth_l = textureOffset(smoothness_x_tex, tc, ivec2(-1,  0)).x;
39         float smooth_r = texture(smoothness_x_tex, tc).x;
40         float smooth_d = textureOffset(smoothness_y_tex, tc, ivec2( 0, -1)).x;
41         float smooth_u = texture(smoothness_y_tex, tc).x;
42         b += smooth_l * textureOffset(diff_flow_tex, tc, ivec2(-1,  0)).xy;
43         b += smooth_r * textureOffset(diff_flow_tex, tc, ivec2( 1,  0)).xy;
44         b += smooth_d * textureOffset(diff_flow_tex, tc, ivec2( 0, -1)).xy;
45         b += smooth_u * textureOffset(diff_flow_tex, tc, ivec2( 0,  1)).xy;
46
47         // FIXME: omega=1.6 seems to make our entire system diverge.
48         // Is this because we do Gauss-Seidel instead of Jacobi?
49         // Figure out what's going on.
50         const float omega = 1.0;
51         diff_flow = texture(diff_flow_tex, tc).xy;
52
53         // From https://en.wikipedia.org/wiki/Successive_over-relaxation.
54         float sigma_u = A12 * diff_flow.y;
55         diff_flow.x += omega * ((b.x - sigma_u) * inv_A11 - diff_flow.x);
56         float sigma_v = A12 * diff_flow.x;
57         diff_flow.y += omega * ((b.y - sigma_v) * inv_A22 - diff_flow.y);
58 }