/*
   Hyllian's xBR LV2 - noblend - pass1 Shader
  
   Copyright (C) 2011-2015 Hyllian - sergiogdb@gmail.com

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   THE SOFTWARE.

*/

#define round(X) floor((X)+0.5)

const static float y_weight        = 48.0;
const static float u_weight        = 7.0;
const static float v_weight        = 6.0;
const static float3x3 yuv          = float3x3(0.299, 0.587, 0.114, -0.169, -0.331, 0.499, 0.499, -0.418, -0.0813);
const static float3x3 yuv_weighted = float3x3(y_weight*yuv[0], u_weight*yuv[1], v_weight*yuv[2]);


float4 df(float4 A, float4 B)
{
    return float4(abs(A-B));
}


float4 weighted_distance(float4 a, float4 b, float4 c, float4 d, float4 e, float4 f, float4 g, float4 h)
{
    return (df(a,b) + df(a,c) + df(d,e) + df(d,f) + 4.0*df(g,h));
}


float3 remapFrom01(float3 v, float3 low, float3 high)
{
    return round(lerp(low, high, v));
}

float4 unpack_info(float i)
{
    float4 info;
    info.w = round(modf(i/2.0f, i));
    info.z = round(modf(i/2.0f, i));
    info.y = round(modf(i/2.0f, i));
    info.x = i;

    return info;
}

struct orig
{
    float2 tex_coord;
    uniform float2 texture_size;
    uniform sampler2D texture;
};


struct input
{
    float2 video_size;
    float2 texture_size;
    float2 output_size;
    float frame_count;
    float frame_direction;
    float frame_rotation;
};

struct out_vertex {
    float4 position : POSITION;
    float4 color    : COLOR;
    float2 texCoord : TEXCOORD0;
    float4 t1       : TEXCOORD1;
    float4 t2       : TEXCOORD2;
    float4 t3       : TEXCOORD3;
    float2 orig_tex;
};

/*    VERTEX_SHADER    */
void main_vertex
(
    float4 position    : POSITION,
    float4 color    : COLOR,
    float2 texCoord : TEXCOORD0,

    uniform float4x4 modelViewProj,
    orig ORIG,
    out out_vertex co
)
{
    co.position = mul(modelViewProj, position);
    co.color = color;

    float2 ps = float2(1.0/ORIG.texture_size.x, 1.0/ORIG.texture_size.y);
    float dx = ps.x;
    float dy = ps.y;

    //    A1 B1 C1
    // A0  A  B  C C4
    // D0  D  E  F F4
    // G0  G  H  I I4
    //    G5 H5 I5

    co.texCoord = texCoord;
    co.orig_tex = ORIG.tex_coord;
    co.t1 = ORIG.tex_coord.xyyy + float4(   0, -dy,  0, dy);  //  B
    co.t2 = ORIG.tex_coord.xxxy + float4( -dx,   0, dx,  0);  //D E F
                                                              //  H
}


/*    FRAGMENT SHADER    */
float4 main_fragment(in out_vertex co, uniform sampler2D decal : TEXUNIT0, orig ORIG, uniform input IN) : COLOR
{

    bool4 px; // px = pixel, edr = edge detection rule
    float4 edr, edr_left, edr_up;
    bool4 nc; // new_color
    float4 fx, fx_left, fx_up; // inequations of straight lines.
    float3 res1, res2;

    float2 fp = frac(co.texCoord*IN.texture_size);

    float3 B  = tex2D(ORIG.texture, co.t1.xy).rgb;
    float3 D  = tex2D(ORIG.texture, co.t2.xw).rgb;
    float3 E  = tex2D(ORIG.texture, co.t2.yw).rgb;
    float3 F  = tex2D(ORIG.texture, co.t2.zw).rgb;
    float3 H  = tex2D(ORIG.texture, co.t1.xw).rgb;

    float4 b = mul( float4x3(B, D, H, F), yuv_weighted[0] );
    float4 e = mul( float4x3(E, E, E, E), yuv_weighted[0] );
    float4 d = b.yzwx;
    float4 f = b.wxyz;
    float4 h = b.zwxy;

    float4 Ao = float4( 1.0, -1.0, -1.0, 1.0 );
    float4 Bo = float4( 1.0,  1.0, -1.0,-1.0 );
    float4 Co = float4( 1.5,  0.5, -0.5, 0.5 );
    float4 Ax = float4( 1.0, -1.0, -1.0, 1.0 );
    float4 Bx = float4( 0.5,  2.0, -0.5,-2.0 );
    float4 Cx = float4( 1.0,  1.0, -0.5, 0.0 );
    float4 Ay = float4( 1.0, -1.0, -1.0, 1.0 );
    float4 By = float4( 2.0,  0.5, -2.0,-0.5 );
    float4 Cy = float4( 2.0,  0.0, -1.0, 0.5 );

    // These inequations define the line below which interpolation occurs.
    fx      = step(Co, Ao*fp.y+Bo*fp.x);
    fx_left = step(Cx, Ax*fp.y+Bx*fp.x);
    fx_up   = step(Cy, Ay*fp.y+By*fp.x);

    float4 info  = tex2D(decal, co.texCoord);

    float3 i = remapFrom01(info.xyz, float3(0.0f), float3(15.0f));

    edr      = unpack_info(i.x);
    edr_left = unpack_info(i.y);
    edr_up   = unpack_info(i.z);

    fx = edr*fx;
    fx_left = edr_left*fx_left;
    fx_up = edr_up*fx_up;

    nc = bool4(max(fx, max(fx_left, fx_up)));
    px = (df(e,f) <= df(e,h));

    res1 = nc.x ? px.x ? F : H : nc.y ? px.y ? B : F : nc.z ? px.z ? D : B : E;
    res2 = nc.w ? px.w ? H : D : nc.z ? px.z ? D : B : nc.y ? px.y ? B : F : E;

    float2 df12 = abs(mul(float2x3(res1, res2), yuv_weighted[0]) - e.xy);
    float3 res = lerp(res1, res2, step(df12.x, df12.y));

    return float4(res, 1.0);
}

