/* COMPATIBILITY 
   - HLSL compilers
   - Cg   compilers
*/

/*
   Hyllian's DDT Sharp Shader
   
   Copyright (C) 2011-2016 Hyllian/Jararaca - 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 WP1  1.0
#define WP2  1.0
#define WP3 -1.0

const static float3 Y = float3(.2126, .7152, .0722);

float luma(float3 color)
{
  return dot(color, Y);
}

float3 bilinear(float p, float q, float3 A, float3 B, float3 C, float3 D)
{
	return ((1-p)*(1-q)*A + p*(1-q)*B + (1-p)*q*C + p*q*D);
}

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;
	float2 texCoord : TEXCOORD0;
	float4 t1 : TEXCOORD1;
	float2 loc : TEXCOORD2;
};


/*    VERTEX_SHADER    */
out_vertex main_vertex
(
	float4 position	: POSITION,
	float2 texCoord1 : TEXCOORD0,

   	uniform float4x4 modelViewProj,
	uniform input IN
)
{
	out_vertex OUT;

	OUT.position = mul(modelViewProj, position);

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

	// This line fix a bug in ATI cards.
	float2 texCoord = texCoord1 + float2(0.0000001, 0.0000001);

	OUT.texCoord = texCoord;
	OUT.t1.xy = float2( dx,  0); // F
	OUT.t1.zw = float2(  0, dy); // H
	OUT.loc = texCoord*IN.texture_size;

	return OUT;
}


/*    FRAGMENT SHADER    */
float4 main_fragment(in out_vertex VAR, uniform sampler2D decal : TEXUNIT0, uniform input IN) : COLOR
{
	float2 pos = frac(VAR.loc)-float2(0.5, 0.5); // pos = pixel position
	float2 dir = sign(pos); // dir = pixel direction

	float2 g1 = dir*VAR.t1.xy;
	float2 g2 = dir*VAR.t1.zw;

//    A1 B1
// A0 A  B  B2
// C0 C  D  D2
//    C3 D3

	float3 A = tex2D(decal, VAR.texCoord       ).xyz;
	float3 B = tex2D(decal, VAR.texCoord +g1   ).xyz;
	float3 C = tex2D(decal, VAR.texCoord    +g2).xyz;
	float3 D = tex2D(decal, VAR.texCoord +g1+g2).xyz;

	float3 A1 = tex2D(decal, VAR.texCoord    -g2).xyz;
	float3 B1 = tex2D(decal, VAR.texCoord +g1-g2).xyz;
	float3 A0 = tex2D(decal, VAR.texCoord -g1   ).xyz;
	float3 C0 = tex2D(decal, VAR.texCoord -g1+g2).xyz;

	float3 B2 = tex2D(decal, VAR.texCoord +2*g1     ).xyz;
	float3 D2 = tex2D(decal, VAR.texCoord +2*g1+  g2).xyz;
	float3 C3 = tex2D(decal, VAR.texCoord      +2*g2).xyz;
	float3 D3 = tex2D(decal, VAR.texCoord   +g1+2*g2).xyz;

	float a = luma(A);
	float b = luma(B);
	float c = luma(C);
	float d = luma(D);

	float a1 = luma(A1);
	float b1 = luma(B1);
	float a0 = luma(A0);
	float c0 = luma(C0);

	float b2 = luma(B2);
	float d2 = luma(D2);
	float c3 = luma(C3);
	float d3 = luma(D3);

	float p = abs(pos.x);
	float q = abs(pos.y);

	float k = distance(pos,g1);
	float l = distance(pos,g2);

	float wd1 = WP1*abs(a-d) + WP2*(abs(b-a1) + abs(b-d2) + abs(c-a0) + abs(c-d3)) + WP3*(abs(a1-d2) + abs(a0-d3));
	float wd2 = WP1*abs(b-c) + WP2*(abs(a-b1) + abs(a-c0) + abs(d-b2) + abs(d-c3)) + WP3*(abs(b1-c0) + abs(b2-c3));

	if ( wd1 < wd2 )
	{
		if (k < l)
		{
			C = A + D - B;
		}
		else
		{
			B = A + D - C;
		}
	}
	else if (wd1 > wd2)
	{
		D = B + C - A;
	}

	float3 color = bilinear(p, q, A, B, C, D);

	return float4(color, 1.0);
}

