////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//structs                                                                                                                                 //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

struct input
{
    float2 video_size;
    float2 texture_size;
    float2 output_size;
    float frame_count;
    sampler2D texture	: TEXUNIT0;
};

struct pass_1
{
    sampler2D texture	: TEXUNIT1;
};

struct blur_coords
{
    fixed2 tex_coord_1 : TEXCOORD1;
    fixed2 tex_coord_2 : TEXCOORD2;
    fixed2 tex_coord_3 : TEXCOORD3;
    fixed2 tex_coord_4 : TEXCOORD4;
    fixed2 lower_bound : TEXCOORD5;
    fixed2 upper_bound : TEXCOORD6;
};

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//vertex shader                                                                                                                           //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void main_vertex( float4 position      			: POSITION,
  		  out float4 oPosition 			: POSITION,
  		  float2 texCoord      			: TEXCOORD0,
  		  out float2 oTexCoord 			: TEXCOORD0,
		  out blur_coords oBlurCoords,
  		  uniform float4x4 modelViewProj,
  		  uniform input IN )
{
    oPosition = mul(modelViewProj, position);
    oTexCoord = texCoord;


    fixed2 texel = 1.0 / IN.texture_size;	//size of one texel

    oBlurCoords = blur_coords 
    (
	texCoord + fixed2(0.0, texel.y),	//down
	texCoord + fixed2(0.0, -texel.y),	//up
	texCoord + fixed2(texel.x, 0.0),	//right
	texCoord + fixed2(-texel.x, 0.0),	//left
	fixed2(0.0),				//lower texture bounds
	texel * (IN.output_size - 2.0)		//upper texture bounds
    );
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//fragment functions                                                                                                                      //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//a simple blur technique that softens harsh color transitions
//specialized to only blur alpha values and limited to only blurring texels lying in the spaces between two or more texels

//fixed4 simple_blur(pass_1 IN, fixed4 COLOR, blur_coords BLUR_COORDS)
//fixed4 simple_blur(input IN, fixed4 COLOR, blur_coords BLUR_COORDS)
fixed4 simple_blur(input IN, pass_1 P1, fixed4 COLOR, blur_coords BLUR_COORDS)
{
  //clamp the blur coords to the input texture size so it doesn't attempt to sample off the texture (it'll retrieve float4(0.0) and darken the edges otherwise)

    BLUR_COORDS.tex_coord_1 = clamp(BLUR_COORDS.tex_coord_1, BLUR_COORDS.lower_bound, BLUR_COORDS.upper_bound);
    BLUR_COORDS.tex_coord_2 = clamp(BLUR_COORDS.tex_coord_2, BLUR_COORDS.lower_bound, BLUR_COORDS.upper_bound);
    BLUR_COORDS.tex_coord_3 = clamp(BLUR_COORDS.tex_coord_3, BLUR_COORDS.lower_bound, BLUR_COORDS.upper_bound);
    BLUR_COORDS.tex_coord_4 = clamp(BLUR_COORDS.tex_coord_4, BLUR_COORDS.lower_bound, BLUR_COORDS.upper_bound);


  //sample adjacent texels based on the coordinates above

    fixed4 adjacent_texel_1 = tex2D(IN.texture, BLUR_COORDS.tex_coord_1);
    fixed4 adjacent_texel_2 = tex2D(IN.texture, BLUR_COORDS.tex_coord_2);
    fixed4 adjacent_texel_3 = tex2D(IN.texture, BLUR_COORDS.tex_coord_3);
    fixed4 adjacent_texel_4 = tex2D(IN.texture, BLUR_COORDS.tex_coord_4);


  //sum the alpha differences between neighboring texels, apply modifiers, then subtract the result from the current fragment alpha value

    COLOR.rgb -= ( (COLOR.rgb - adjacent_texel_1.rgb) + 
		   (COLOR.rgb - adjacent_texel_2.rgb) ) * 0.4;


    adjacent_texel_1 = tex2D(P1.texture, BLUR_COORDS.tex_coord_1);
    adjacent_texel_2 = tex2D(P1.texture, BLUR_COORDS.tex_coord_2);

    COLOR.rgb -= ( (COLOR.rgb - adjacent_texel_1.rgb) + 
		   (COLOR.rgb - adjacent_texel_2.rgb) ) * 0.3;

/*
    COLOR.rgb -= ( (COLOR.rgb - adjacent_texel_1.rgb) + 
		   (COLOR.rgb - adjacent_texel_2.rgb) + 
		   (COLOR.rgb - adjacent_texel_3.rgb) + 
		   (COLOR.rgb - adjacent_texel_4.rgb) ) * 0.1255;

    COLOR.a -= ( (COLOR.a - adjacent_texel_1.a) + 
		 (COLOR.a - adjacent_texel_2.a) + 
		 (COLOR.a - adjacent_texel_3.a) + 
		 (COLOR.a - adjacent_texel_4.a) ) * 0.1255;
*/

COLOR.a = 1.0;

  //return new alpha value

    return COLOR;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//fragment shader                                                                                                                         //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

float4 main_fragment( float2 texCoord        		: TEXCOORD0, 
		      in blur_coords blur_coords,
		      uniform pass_1 PASS1,
		      uniform input IN ) : COLOR
{
float4 out_color = tex2D(PASS1.texture, texCoord);

if (out_color.a == 0.0) { out_color = simple_blur(IN, PASS1, out_color, blur_coords); }

return out_color;
}