/* COMPATIBILITY 
   - HLSL compilers
   - Cg   compilers
*/

#define hardPix -10.0
#define brightboost 1
#define shape 2.0

struct prev
{
   float2 video_size;
   float2 texture_size;
   float2 output_size;
   sampler2D texture;
};

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

input IN_global;
sampler2D s0_global;

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

    uniform float4x4 modelViewProj,

	out float4 oPosition : POSITION,
	out float4 oColor    : COLOR,
	out float2 otex : TEXCOORD
)
{
	oPosition = mul(modelViewProj, position);
	oColor = color;
	otex = texCoord;
}

struct output 
{
  float4 color    : COLOR;
};

// Nearest emulated sample given floating point position and texel offset.
// Also zero's off screen.
float3 Fetch(float2 pos,float2 off){
//  pos=(floor(pos*IN_global.texture_size.xy+off)+float2(0.5,0.5))/IN_global.texture_size.xy;
  return brightboost * tex2D(s0_global,pos.xy).rgb;
}

// Distance in emulated pixels to nearest texel.
float2 Dist(float2 pos){pos=pos*IN_global.texture_size.xy;return -((pos-floor(pos))-float2(0.5));}
    
// 1D Gaussian.
float Gaus(float pos,float scale){return exp2(scale*pow(abs(pos),shape));}

// 3-tap Gaussian filter along horz line.
float3 Horz3(float2 pos,float off){
  float3 b=Fetch(pos,float2(-1.0,off));
  float3 c=Fetch(pos,float2( 0.0,off));
  float3 d=Fetch(pos,float2( 1.0,off));
  float dst=Dist(pos).x;
  // Convert distance to weight.
  float scale=hardPix;
  float wb=Gaus(dst-1.0,scale);
  float wc=Gaus(dst+0.0,scale);
  float wd=Gaus(dst+1.0,scale);
  // Return filtered sample.
  return (b*wb+c*wc+d*wd)/(wb+wc+wd);}

output main_fragment(float2 tex : TEXCOORD0, uniform sampler2D s0 : TEXUNIT0, uniform input IN, uniform prev REFERENCE) 
{
  s0_global = REFERENCE.texture;
  IN_global = IN;

   output OUT;
   OUT.color = float4(Horz3(tex, 1.0), 1.0);
   return OUT;
}

