////////////////////////////////////////////////
//
// separable blur with up to 7 taps
//
////////////////////////////////////////////////

blurshader = [
    shader 0 $arg1 [
        in vec4 vvertex;
        @(screentexcoord 0)
        uniform float offsets[8];
        out vec2 texcoord0, texcoordp1, texcoordn1;
        @(loopconcat+ i 2 (min (- $arg2 1) 2) [result [
            out vec2 texcoordp@i, texcoordn@i;
        ]])
        void main(void)
        {
            gl_Position = vvertex;
            texcoord0 = vtexcoord0; 
            vec2 tcp = vtexcoord0, tcn = vtexcoord0; 
            tcp.@arg3 += offsets[1];
            tcn.@arg3 -= offsets[1];
            texcoordp1 = tcp;
            texcoordn1 = tcn;
            @(loopconcat+ i 2 (min (- $arg2 1) 2) [result [
                tcp.@arg3 = vtexcoord0.@arg3 + offsets[@@i];
                tcn.@arg3 = vtexcoord0.@arg3 - offsets[@@i];
                texcoordp@i = tcp;
                texcoordn@i = tcn;
            ]])
        }
    ] [
        uniform float weights[8];
        uniform float offsets[8];
        uniform sampler@[arg4] tex0;
        in vec2 texcoord0, texcoordp1, texcoordn1;
        @(loopconcat+ i 2 (min (- $arg2 1) 2) [result [
            in vec2 texcoordp@i, texcoordn@i;
        ]])
        layout(location = 0) out vec4 fragcolor;
        void main(void)
        {
            #define texval(coords) texture@[arg4](tex0, (coords))
            vec4 val = texval(texcoord0) * weights[0];
            @(loopconcat+ i 1 $arg2 [
                if (< $i 4) [result [
                    val += weights[@@i] * (texval(texcoordp@i) + texval(texcoordn@i));
                ]] [result [
                    val += weights[@@i] * 
                    @(if (=s $arg3 "x") [result [
                        (texval(vec2(texcoord0.x + offsets[@@i], texcoord0.y)) + texval(vec2(texcoord0.x - offsets[@@i], texcoord0.y)));
                    ]] [result [
                        (texval(vec2(texcoord0.x, texcoord0.y + offsets[@@i])) + texval(vec2(texcoord0.x, texcoord0.y - offsets[@@i])));
                    ]])
                ]]
            ])
            fragcolor = val;
        }
    ]
]

loop+ i 1 7 [
    blurshader (format "blurx%1" $i) $i x 2D
    blurshader (format "blury%1" $i) $i y 2D
    blurshader (format "blurx%1rect" $i) $i x 2DRect
    blurshader (format "blury%1rect" $i) $i y 2DRect
]

