/* COMPATIBILITY 
   - HLSL compilers
   - Cg   compilers
*/

/*
   Bob-Deinterlacing
   Author: hunterk
   License: Public domain
   
   Note: This shader is designed to work with the typical interlaced output from an emulator, which displays both even and odd fields twice.
   As such, it is inappropriate for general video use unless the video has already been similarly woven beforehand.
*/

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

void main_vertex
(
   float4 position : POSITION,
   out float4 oPosition : POSITION,
   uniform float4x4 modelViewProj,

   float4 color : COLOR,
   out float4 oColor : COLOR,

   float2 texCoord : TEXCOORD,
   out float2 oTexCoord : TEXCOORD,

   uniform input IN
)
{
   oPosition = mul(modelViewProj, position);
   oColor = color;
   oTexCoord = texCoord;
}

#define one_pixel float2(1.0 / IN.texture_size)
   
float4 main_fragment (in float2 texCoord : TEXCOORD, in sampler2D s0 : TEXUNIT0, uniform input IN) : COLOR
{
   float4 res = tex2D(s0, texCoord);
   float y = 0.0;

   // assume anything with a vertical resolution greater than 400 lines is interlaced
   if (IN.video_size.y > 400.0) y = IN.texture_size.y * texCoord.y + IN.frame_count;
   else
      y = 2.0 * IN.texture_size.y * texCoord.y;

   if (fmod(y, 2.0) > 0.99999) return float4(tex2D(s0, texCoord + float2(0.0, one_pixel.y)));
   else
      return res;
}
