]> git.sesse.net Git - nageru/blobdiff - sor.frag
Compute diffusivity instead of smoothness, which saves a flow-size texture; shaves...
[nageru] / sor.frag
index 6c5333fa5ead7c960a96361ae16fd501638d7a6a..91bf831e212a52b48204af6246d28c6419c60120 100644 (file)
--- a/sor.frag
+++ b/sor.frag
@@ -1,10 +1,14 @@
 #version 450 core
 
-in vec2 tc;
+in vec2 tc, tc_left, tc_down;
+in float element_sum_idx;
 out vec2 diff_flow;
 
-uniform sampler2D diff_flow_tex, smoothness_x_tex, smoothness_y_tex;
+uniform sampler2D diff_flow_tex, diffusivity_tex;
 uniform usampler2D equation_tex;
+uniform int phase;
+
+uniform bool zero_diff_flow;
 
 // See pack_floats_shared() in equations.frag.
 vec2 unpack_floats_shared(uint c)
@@ -23,33 +27,53 @@ vec2 unpack_floats_shared(uint c)
        return vec2(a, b);
 }
 
+float zero_if_outside_border(vec4 val)
+{
+       if (val.w < 1.0f) {
+               // We hit the border (or more like half-way to it), so zero smoothness.
+               return 0.0f;
+       } else {
+               return val.x;
+       }
+}
+
 void main()
 {
+       // Red-black SOR: Every other pass, we update every other element in a
+       // checkerboard pattern. This is rather suboptimal for the GPU, as it
+       // just immediately throws away half of the warp, but it helps convergence
+       // a _lot_ (rough testing indicates that five iterations of SOR is as good
+       // as ~50 iterations of Jacobi). We could probably do better by reorganizing
+       // the data into two-values-per-pixel, so-called “twinning buffering”,
+       // but it makes for rather annoying code in the rest of the pipeline.
+       int color = int(round(element_sum_idx)) & 1;
+       if (color != phase) discard;
+
        uvec4 equation = texture(equation_tex, tc);
        float inv_A11 = uintBitsToFloat(equation.x);
        float A12 = uintBitsToFloat(equation.y);
        float inv_A22 = uintBitsToFloat(equation.z);
        vec2 b = unpack_floats_shared(equation.w);
 
-       // Subtract the missing terms from the right-hand side
-       // (it couldn't be done earlier, because we didn't know
-       // the values of the neighboring pixels; they change for
-       // each SOR iteration).
-       // TODO: Multiply by some gamma.
-       float smooth_l = textureOffset(smoothness_x_tex, tc, ivec2(-1,  0)).x;
-       float smooth_r = texture(smoothness_x_tex, tc).x;
-       float smooth_d = textureOffset(smoothness_y_tex, tc, ivec2( 0, -1)).x;
-       float smooth_u = texture(smoothness_y_tex, tc).x;
-       b += smooth_l * textureOffset(diff_flow_tex, tc, ivec2(-1,  0)).xy;
-       b += smooth_r * textureOffset(diff_flow_tex, tc, ivec2( 1,  0)).xy;
-       b += smooth_d * textureOffset(diff_flow_tex, tc, ivec2( 0, -1)).xy;
-       b += smooth_u * textureOffset(diff_flow_tex, tc, ivec2( 0,  1)).xy;
-
-       // FIXME: omega=1.6 seems to make our entire system diverge.
-       // Is this because we do Gauss-Seidel instead of Jacobi?
-       // Figure out what's going on.
-       const float omega = 1.0;
-       diff_flow = texture(diff_flow_tex, tc).xy;
+       if (zero_diff_flow) {
+               diff_flow = vec2(0.0f);
+       } else {
+               // Subtract the missing terms from the right-hand side
+               // (it couldn't be done earlier, because we didn't know
+               // the values of the neighboring pixels; they change for
+               // each SOR iteration).
+               float smooth_l = zero_if_outside_border(texture(diffusivity_tex, tc_left));
+               float smooth_r = zero_if_outside_border(textureOffset(diffusivity_tex, tc_left, ivec2(1, 0)));
+               float smooth_d = zero_if_outside_border(texture(diffusivity_tex, tc_down));
+               float smooth_u = zero_if_outside_border(textureOffset(diffusivity_tex, tc_down, ivec2(0, 1)));
+               b += smooth_l * textureOffset(diff_flow_tex, tc, ivec2(-1,  0)).xy;
+               b += smooth_r * textureOffset(diff_flow_tex, tc, ivec2( 1,  0)).xy;
+               b += smooth_d * textureOffset(diff_flow_tex, tc, ivec2( 0, -1)).xy;
+               b += smooth_u * textureOffset(diff_flow_tex, tc, ivec2( 0,  1)).xy;
+               diff_flow = texture(diff_flow_tex, tc).xy;
+       }
+
+       const float omega = 1.8;  // Marginally better than 1.6, it seems.
 
        // From https://en.wikipedia.org/wiki/Successive_over-relaxation.
        float sigma_u = A12 * diff_flow.y;