]> git.sesse.net Git - nageru/blob - sor.frag
Finally get SOR working.
[nageru] / sor.frag
1 #version 450 core
2
3 in vec2 tc;
4 in float element_sum_idx;
5 out vec2 diff_flow;
6
7 uniform sampler2D diff_flow_tex, smoothness_x_tex, smoothness_y_tex;
8 uniform usampler2D equation_tex;
9 uniform vec2 image_size;
10 uniform int phase;
11
12 // See pack_floats_shared() in equations.frag.
13 vec2 unpack_floats_shared(uint c)
14 {
15         // Recover the exponent, and multiply it in. Add one because
16         // we have denormalized mantissas, then another one because we
17         // already reduced the exponent by one. Then subtract 20, because
18         // we are going to shift up the number by 20 below to recover the sign bits.
19         float normalizer = uintBitsToFloat(((c >> 1) & 0x7f800000u) - (18 << 23));
20         normalizer *= (1.0 / 2047.0);
21
22         // Shift the values up so that we recover the sign bit, then normalize.
23         float a = int(uint(c & 0x000fffu) << 20) * normalizer;
24         float b = int(uint(c & 0xfff000u) << 8) * normalizer;
25
26         return vec2(a, b);
27 }
28
29 void main()
30 {
31         // Red-black SOR: Every other pass, we update every other element in a
32         // checkerboard pattern. This is rather suboptimal for the GPU, as it
33         // just immediately throws away half of the warp, but it helps convergence
34         // a _lot_ (rough testing indicates that five iterations of SOR is as good
35         // as ~50 iterations of Jacobi). We could probably do better by reorganizing
36         // the data into two-values-per-pixel, so-called “twinning buffering”,
37         // but it makes for rather annoying code in the rest of the pipeline.
38         int color = int(round(element_sum_idx)) & 1;
39         if (color != phase) discard;
40
41         uvec4 equation = texture(equation_tex, tc);
42         float inv_A11 = uintBitsToFloat(equation.x);
43         float A12 = uintBitsToFloat(equation.y);
44         float inv_A22 = uintBitsToFloat(equation.z);
45         vec2 b = unpack_floats_shared(equation.w);
46
47         // Subtract the missing terms from the right-hand side
48         // (it couldn't be done earlier, because we didn't know
49         // the values of the neighboring pixels; they change for
50         // each SOR iteration).
51         float smooth_l = textureOffset(smoothness_x_tex, tc, ivec2(-1,  0)).x;
52         float smooth_r = texture(smoothness_x_tex, tc).x;
53         float smooth_d = textureOffset(smoothness_y_tex, tc, ivec2( 0, -1)).x;
54         float smooth_u = texture(smoothness_y_tex, tc).x;
55         b += smooth_l * textureOffset(diff_flow_tex, tc, ivec2(-1,  0)).xy;
56         b += smooth_r * textureOffset(diff_flow_tex, tc, ivec2( 1,  0)).xy;
57         b += smooth_d * textureOffset(diff_flow_tex, tc, ivec2( 0, -1)).xy;
58         b += smooth_u * textureOffset(diff_flow_tex, tc, ivec2( 0,  1)).xy;
59
60         const float omega = 1.8;  // Marginally better than 1.6, it seems.
61         diff_flow = texture(diff_flow_tex, tc).xy;
62
63         // From https://en.wikipedia.org/wiki/Successive_over-relaxation.
64         float sigma_u = A12 * diff_flow.y;
65         diff_flow.x += omega * ((b.x - sigma_u) * inv_A11 - diff_flow.x);
66         float sigma_v = A12 * diff_flow.x;
67         diff_flow.y += omega * ((b.y - sigma_v) * inv_A22 - diff_flow.y);
68 }