]> git.sesse.net Git - nageru/blobdiff - equations.frag
Allow symlinked frame files. Useful for testing.
[nageru] / equations.frag
index 51e1394e13bfcc00d37ac70d3c8366a195e5930d..04e5370fd9d53baf5afda39ee140a526d6937945 100644 (file)
@@ -1,12 +1,14 @@
 #version 450 core
 
-in vec2 tc;
-out uvec4 equation;
+in vec3 tc0, tc_left0, tc_down0;
+in vec3 tc1, tc_left1, tc_down1;
+in float line_offset;
+out uvec4 equation_red, equation_black;
 
-uniform sampler2D I_x_y_tex, I_t_tex;
-uniform sampler2D diff_flow_tex, base_flow_tex;
-uniform sampler2D beta_0_tex;
-uniform sampler2D smoothness_x_tex, smoothness_y_tex;
+uniform sampler2DArray I_x_y_tex, I_t_tex;
+uniform sampler2DArray diff_flow_tex, base_flow_tex;
+uniform sampler2DArray beta_0_tex;
+uniform sampler2DArray diffusivity_tex;
 
 // Relative weighting of intensity term.
 uniform float delta;
@@ -14,6 +16,8 @@ uniform float delta;
 // Relative weighting of gradient term.
 uniform float gamma;
 
+uniform bool zero_diff_flow;
+
 // Similar to packHalf2x16, but the two values share exponent, and are stored
 // as 12-bit fixed point numbers multiplied by that exponent (the leading one
 // can't be implicit in this kind of format). This allows us to store a much
@@ -53,12 +57,27 @@ uint pack_floats_shared(float a, float b)
        return (qa & 0xfffu) | ((qb & 0xfffu) << 12) | e;
 }
 
-void main()
+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;
+       }
+}
+
+uvec4 compute_equation(vec3 tc, vec3 tc_left, vec3 tc_down)
 {
        // Read the flow (on top of the u0/v0 flow).
-       vec2 diff_flow = texture(diff_flow_tex, tc).xy;
-       float du = diff_flow.x;
-       float dv = diff_flow.y;
+       float du, dv;
+       if (zero_diff_flow) {
+               du = dv = 0.0f;
+       } else {
+               vec2 diff_flow = texture(diff_flow_tex, tc).xy;
+               du = diff_flow.x;
+               dv = diff_flow.y;
+       }
 
        // Read the first derivatives.
        vec2 I_x_y = texture(I_x_y_tex, tc).xy;
@@ -121,10 +140,15 @@ void main()
 
        // E_S term, sans the part on the right-hand side that deals with
        // the neighboring pixels. The gamma is multiplied in in smoothness.frag.
-       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;
+       //
+       // Note that we sample in-between two texels, which gives us the 0.5 *
+       // (x[-1] + x[0]) part for free. If one of the texels is a border
+       // texel, it will have zero alpha, and zero_if_outside_border() will
+       // set smoothness to zero.
+       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)));
        A11 += smooth_l + smooth_r + smooth_d + smooth_u;
        A22 += smooth_l + smooth_r + smooth_d + smooth_u;
 
@@ -139,8 +163,25 @@ void main()
        b2 += laplacian.y;
 
        // Encode the equation down into four uint32s.
-       equation.x = floatBitsToUint(1.0 / A11);
-       equation.y = floatBitsToUint(A12);
-       equation.z = floatBitsToUint(1.0 / A22);
-       equation.w = pack_floats_shared(b1, b2);
+       uvec4 ret;
+       ret.x = floatBitsToUint(1.0 / A11);
+       ret.y = floatBitsToUint(A12);
+       ret.z = floatBitsToUint(1.0 / A22);
+       ret.w = pack_floats_shared(b1, b2);
+       return ret;
+}
+
+void main()
+{
+       uvec4 eq0 = compute_equation(tc0, tc_left0, tc_down0);
+       uvec4 eq1 = compute_equation(tc1, tc_left1, tc_down1);
+
+       if ((int(round(line_offset)) & 1) == 1) {
+               // Odd line, so the right value is red.
+               equation_red = eq1;
+               equation_black = eq0;
+       } else {
+               equation_red = eq0;
+               equation_black = eq1;
+       }
 }