/*
WWW.TGBUS.COM
JIM POSSIBLE
*/

#ifdef SHADER_MODEL // make safe to include in resource file to enforce dependency

Texture2D Texture;
SamplerState TextureSampler;

struct PS_INPUT
{
	float4 p : SV_Position;
	float2 t : TEXCOORD0;
};

struct PS_OUTPUT
{
	float4 c : SV_Target0;
};

float overlay(float base, float blend)
{
	float result = 0.0;
	
	if( base < 0.5 ) 
		result = 2.0 * base * blend;
	else   
		result = 1.0 - (1.0 - 2.0*(base-0.5)) * (1.0-blend);
		
	return result;
}


PS_OUTPUT ps_main(PS_INPUT input)
{
	PS_OUTPUT output;

	float2 pos = input.t;
	
	float4 color = Texture.Sample(TextureSampler, pos);
	float4 blur = color * 0.22520613262190495;	

#define OFFSET 0.003

	float2 dx = float2(OFFSET, 0.0);
	float2 dy = float2(0.0, OFFSET);
	float2 dx2 = 2 * dx;
	float2 dy2 = 2 * dy;
	
	float4 colorU = Texture.Sample(TextureSampler, pos + dy);
	blur += 0.002589001911021066 * Texture.Sample(TextureSampler, pos -dx2 +dy2); 
	blur += 0.010778807494659370 * Texture.Sample(TextureSampler, pos -dx +dy2); 
	blur += 0.024146616900339800 * Texture.Sample(TextureSampler, pos +dy2); 
	blur += 0.010778807494659370 * Texture.Sample(TextureSampler, pos +dx +dy2); 
	blur += 0.002589001911021066 * Texture.Sample(TextureSampler, pos +dx2 +dy2); 

	blur += 0.010778807494659370 * Texture.Sample(TextureSampler, pos -dx2 +dy);
	blur += 0.044875475183061630 * Texture.Sample(TextureSampler, pos -dx +dy);
	blur += 0.100529757860782610 * Texture.Sample(TextureSampler, pos +dy);
	blur += 0.044875475183061630 * Texture.Sample(TextureSampler, pos +dx +dy);
	blur += 0.010778807494659370 * Texture.Sample(TextureSampler, pos +dx2 +dy);

	blur += 0.024146616900339800 * Texture.Sample(TextureSampler, pos -dx2);
	blur += 0.100529757860782610 * Texture.Sample(TextureSampler, pos -dx);	
	blur += 0.100529757860782610 * Texture.Sample(TextureSampler, pos +dx);
	blur += 0.024146616900339800 * Texture.Sample(TextureSampler, pos +dx2);

	blur += 0.010778807494659370 * Texture.Sample(TextureSampler, pos -dx2 -dy);
	blur += 0.044875475183061630 * Texture.Sample(TextureSampler, pos -dx -dy);
	blur += 0.100529757860782610 * Texture.Sample(TextureSampler, pos -dy);
	blur += 0.044875475183061630 * Texture.Sample(TextureSampler, pos +dx -dy);
	blur += 0.010778807494659370 * Texture.Sample(TextureSampler, pos +dx2 -dy);

	blur += 0.002589001911021066 * Texture.Sample(TextureSampler, pos -dx2 -dy2);
	blur += 0.010778807494659370 * Texture.Sample(TextureSampler, pos -dx -dy2);
	blur += 0.024146616900339800 * Texture.Sample(TextureSampler, pos -dy2);
	blur += 0.010778807494659370 * Texture.Sample(TextureSampler, pos +dx -dy2);
	blur += 0.002589001911021066 * Texture.Sample(TextureSampler, pos +dx2 -dy2);				
	
	float4 newcolor = float4(overlay(color.r,blur.r),
							overlay(color.g,blur.g),
							overlay(color.b,blur.b),
							1.0);
	
	output.c = lerp(color, newcolor, 0.5);
	return output;
}

#endif
