////////////////////////////////////////////////
//
// post effects
//
////////////////////////////////////////////////

fsvs = [result [
    in vec4 vvertex;
    @(screentexcoord 0)
    out vec2 texcoord0;
    @arg2
    void main(void)
    {
        gl_Position = vvertex;   // woohoo, no mvp :) 
        texcoord0 = vtexcoord0; 
        @arg1
    }
]]

fsps = [result [
    uniform sampler2DRect tex0; 
    in vec2 texcoord0;
    layout(location = 0) out vec4 fragcolor;
    @arg2
    void main(void)
    {
        vec4 color = texture2DRect(tex0, texcoord0);
        @arg1
    }
]]

fsvs4 = [
    fsvs [
        texcoord1 = vtexcoord0 + vec2(-1.5, -1.5);
        texcoord2 = vtexcoord0 + vec2( 1.5, -1.5);
        texcoord3 = vtexcoord0 + vec2(-1.5,  1.5);
        texcoord4 = vtexcoord0 + vec2( 1.5,  1.5);
        @arg1
    ] [
        out vec2 texcoord1, texcoord2, texcoord3, texcoord4;
    ]
]

fsps4 = [
    fsps [
        vec4 s00 = texture2DRect(tex0, texcoord1);
        vec4 s02 = texture2DRect(tex0, texcoord2);
        vec4 s20 = texture2DRect(tex0, texcoord3);
        vec4 s22 = texture2DRect(tex0, texcoord4);
        @arg1
    ] [
        in vec2 texcoord1, texcoord2, texcoord3, texcoord4;
    ]
]

// some simple ones that just do an effect on the RGB value...

lazyshader 0 "invert" (fsvs) (fsps [fragcolor = 1.0 - color;])
lazyshader 0 "gbr"    (fsvs) (fsps [fragcolor = color.yzxw;])
lazyshader 0 "bw"     (fsvs) (fsps [fragcolor = vec4(dot(color.xyz, vec3(0.333)));])

// sobel

lazyshader 0 "sobel" (fsvs4) (fsps4 [
    vec4 t = s00 + s20 - s02 - s22;
    vec4 u = s00 + s02 - s20 - s22;
    fragcolor = color + t*t + u*u;
])

// rotoscope

lazyshader 0 "rotoscope" [
    in vec4 vvertex;
    @(screentexcoord 0)
    uniform vec4 params;
    out vec2 t11, t00, t12, t01, t20, t02, t21, t10, t22;
    void main(void)
    {
        gl_Position = vvertex;
        t11 = vtexcoord0;
        t00 = vec2(-1.0, -1.0)*params.x + vtexcoord0;
        t12 = vec2( 0.0,  1.0)*params.x + vtexcoord0;
        t01 = vec2(-1.0,  0.0)*params.x + vtexcoord0;
        t20 = vec2( 1.0, -1.0)*params.x + vtexcoord0;
        t02 = vec2(-1.0,  1.0)*params.x + vtexcoord0;
        t21 = vec2( 1.0,  0.0)*params.x + vtexcoord0;
        t10 = vec2( 0.0, -1.0)*params.x + vtexcoord0;
        t22 = vec2( 1.0,  1.0)*params.x + vtexcoord0;
    }
] [
    uniform sampler2DRect tex0; 
    in vec2 t11, t00, t12, t01, t20, t02, t21, t10, t22;
    layout(location = 0) out vec4 fragcolor;
    void main(void)
    {
        vec4 c00 = texture2DRect(tex0, t00);
        vec4 c01 = texture2DRect(tex0, t01);
        vec4 c02 = texture2DRect(tex0, t02);
        vec4 c10 = texture2DRect(tex0, t10);
        vec4 c11 = texture2DRect(tex0, t11);
        vec4 c12 = texture2DRect(tex0, t12);
        vec4 c20 = texture2DRect(tex0, t20);
        vec4 c21 = texture2DRect(tex0, t21);
        vec4 c22 = texture2DRect(tex0, t22);

        vec4 diag1 = c00 - c22;
        vec4 diag2 = c02 - c20;
        vec4 xedge = (c01 - c21)*2.0 + diag1 + diag2;
        vec4 yedge = (c10 - c12)*2.0 + diag1 - diag2;
        xedge *= xedge;
        yedge *= yedge;

        vec4 xyedge = xedge + yedge;
        float sobel = step(max(xyedge.x, max(xyedge.y, xyedge.z)), 0.1);

        float hue = dot(c11.xyz, vec3(1.0));
        c11 /= hue;
        vec3 cc = step(vec3(0.2, 0.8, 1.5), vec3(hue));
        c11 *= dot(cc, vec3(0.5, 0.5, 1.5)); 

        fragcolor = c11 * max(cc.z, sobel);

    }
]

blur3shader = [
    lazyshader 0 $arg1 [
        in vec4 vvertex;
        @(screentexcoord 0)
        out vec2 texcoord0, texcoord1;
        void main(void)
        {
            gl_Position = vvertex;
            texcoord0 = vtexcoord0 + vec2(@(? $arg2 -0.5 0.0), @(? $arg3 -0.5 0.0));
            texcoord1 = vtexcoord0 + vec2(@(? $arg2  0.5 0.0), @(? $arg3  0.5 0.0));
        }
    ] [
        uniform sampler2DRect tex0; 
        in vec2 texcoord0, texcoord1;
        layout(location = 0) out vec4 fragcolor;
        void main(void)
        {
            fragcolor = 0.5*(texture2DRect(tex0, texcoord0) + texture2DRect(tex0, texcoord1));
        }
    ]
]
blur3shader hblur3 1 0
blur3shader vblur3 0 1

blur5shader = [
    lazyshader 0 $arg1 [
        in vec4 vvertex;
        @(screentexcoord 0)
        out vec2 texcoord0, texcoord1, texcoord2;
        void main(void)
        {
            gl_Position = vvertex;
            texcoord0 = vtexcoord0;
            texcoord1 = vtexcoord0 + vec2(@(? $arg2 -1.333 0.0), @(? $arg3 -1.333 0.0));
            texcoord2 = vtexcoord0 + vec2(@(? $arg2  1.333 0.0), @(? $arg3  1.333 0.0));
        }
    ] [
        uniform sampler2DRect tex0; 
        in vec2 texcoord0, texcoord1, texcoord2;
        layout(location = 0) out vec4 fragcolor;
        void main(void)
        {
            fragcolor = 0.4*texture2DRect(tex0, texcoord0) + 0.3*(texture2DRect(tex0, texcoord1) + texture2DRect(tex0, texcoord2));
        }
    ]
]
blur5shader hblur5 1 0
blur5shader vblur5 0 1

rotoscope = [
    clearpostfx
    if (>= $numargs 1) [addpostfx rotoscope 0 0 0 $arg1]
    if (>= $numargs 2) [
        if (= $arg2 1) [addpostfx hblur3; addpostfx vblur3]
        if (= $arg2 2) [addpostfx hblur5; addpostfx vblur5]
    ]
]

