]> git.sesse.net Git - nageru/blob - sor.frag
Implement SOR.
[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 void main()
10 {
11         uvec4 equation = texture(equation_tex, tc);
12         float inv_A11 = uintBitsToFloat(equation.x);
13         float A12 = uintBitsToFloat(equation.y);
14         float inv_A22 = uintBitsToFloat(equation.z);
15         vec2 b = unpackHalf2x16(equation.w);
16
17         // Subtract the missing terms from the right-hand side
18         // (it couldn't be done earlier, because we didn't know
19         // the values of the neighboring pixels; they change for
20         // each SOR iteration).
21         // TODO: Multiply by some gamma.
22         float smooth_l = textureOffset(smoothness_x_tex, tc, ivec2(-1,  0)).x;
23         float smooth_r = texture(smoothness_x_tex, tc).x;
24         float smooth_d = textureOffset(smoothness_y_tex, tc, ivec2( 0, -1)).x;
25         float smooth_u = texture(smoothness_y_tex, tc).x;
26         b -= smooth_l * textureOffset(diff_flow_tex, tc, ivec2(-1,  0)).xy;
27         b -= smooth_r * textureOffset(diff_flow_tex, tc, ivec2( 1,  0)).xy;
28         b -= smooth_d * textureOffset(diff_flow_tex, tc, ivec2( 0, -1)).xy;
29         b -= smooth_u * textureOffset(diff_flow_tex, tc, ivec2( 0,  1)).xy;
30
31         const float omega = 1.6;
32         diff_flow = texture(diff_flow_tex, tc).xy;
33
34         // From https://en.wikipedia.org/wiki/Successive_over-relaxation.
35         float sigma_u = A12 * diff_flow.y;
36         diff_flow.x += omega * ((b.x - sigma_u) * inv_A11 - diff_flow.x);
37         float sigma_v = A12 * diff_flow.x;
38         diff_flow.y += omega * ((b.y - sigma_v) * inv_A22 - diff_flow.y);
39 }