//////////////////////////////////////////////////////////////////////
//
// upscaling shaders
//
//////////////////////////////////////////////////////////////////////

shader 0 "scalelinear" [
    in vec4 vvertex;
    @(screentexcoord 0)
    out vec2 texcoord0;

    void main(void)
    {
        gl_Position = vvertex;
        texcoord0 = vtexcoord0; 
    }
] [
    uniform sampler2DRect tex0;
    in vec2 texcoord0;
    layout(location = 0) out vec4 fragcolor;

    void main(void)
    {
        fragcolor = texture2DRect(tex0, texcoord0);
    }
]

loop i 2 [
  lazyshader 0 (? $i "scalecubicy" "scalecubicx") [
    in vec4 vvertex;
    @(screentexcoord 0)
    out vec2 texcoord0;

    void main(void)
    {
        gl_Position = vvertex;
        texcoord0 = vtexcoord0; 
    }
  ] [
    uniform sampler2DRect tex0;
    in vec2 texcoord0;
    layout(location = 0) out vec4 fragcolor;

    vec4 cubic(float s)
    {
        const float B = @gscalecubicsoft, C = @(divf (-f 1 $gscalecubicsoft) 2);
        float s2 = s*s, s3 = s2*s;
        return vec4((-1.0/6.0*B - C) * s3 + (0.5*B + 2.0*C) * s2 + (-0.5*B - C) * s + 1.0/6.0*B,
                    (2.0 - 1.5*B - C) * s3 + (-3.0 + 2.0*B + C) * s2 + (1.0 - 1.0/3.0*B),
                    (-2.0 + 1.5*B + C) * s3 + (3.0 - 2.5*B - 2.0*C) * s2 + (0.5*B + C)*s + 1.0/6.0*B,
                    (1.0/6.0*B + C) * s3 - C * s2);
    }

    void main(void)
    {
        vec2 center = texcoord0;
        @(if $i [result [
            float offset = fract(texcoord0.y-0.5);
            center.y -= offset;
            #define texval(tap) texture2DRect(tex0, center + vec2(0.0, tap))
            #define texvaloffset(tap) texture2DRectOffset(tex0, center, ivec2(0, tap))
        ]] [result [
            float offset = fract(texcoord0.x-0.5);
            center.x -= offset;
            #define texval(tap) texture2DRect(tex0, center + vec2(tap, 0.0))
            #define texvaloffset(tap) texture2DRectOffset(tex0, center, ivec2(tap, 0))
        ]])
        vec4 weight = cubic(offset);
        weight.y += weight.z;
        weight.z /= weight.y;
        fragcolor = weight.x*texvaloffset(-1) + weight.y*texval(weight.z) + weight.w*texvaloffset(2);
    }
  ]
]

shader 0 "reorient" [
    in vec4 vvertex;
    uniform vec3 reorientx, reorienty;
    out vec2 texcoord0;

    void main(void)
    {
        gl_Position = vvertex;
        texcoord0.x = dot(vvertex.xy, reorientx.xy) + reorientx.z;
        texcoord0.y = dot(vvertex.xy, reorienty.xy) + reorienty.z;
    }
] [
    uniform sampler2DRect tex0;
    in vec2 texcoord0;
    layout(location = 0) out vec4 fragcolor;

    void main(void)
    {
        fragcolor = texture2DRect(tex0, texcoord0);
    }
]


