//Author: peachykeen
//Title: Bokeh
//Pack: MGE Shader Library
//Desc:
//		Does some crazy shit.
//

texture lastpass;
texture thisframe;
texture depthframe;
vector rcpres;
float4 HDR;
sampler s0 = sampler_state { texture=<thisframe>; minfilter = linear; magfilter = linear; mipfilter = linear; addressu=clamp; addressv = clamp; };
sampler s1 = sampler_state { texture=<lastpass>; minfilter = linear; magfilter = linear; mipfilter = linear; addressu=clamp; addressv = clamp; };
sampler s2 = sampler_state { texture=<depthframe>; minfilter = linear; magfilter = linear; mipfilter = linear; addressu=clamp; addressv = clamp;};

float blur = 16;

float4 brightpass(in float2 Tex : TEXCOORD) : COLOR0
{
	float4 fC = tex2D(s0, Tex);
	fC = smoothstep(0.7, 1.4, fC);
	return fC;
}

float4 blurHor(in float2 Tex : TEXCOORD) : COLOR0
{
	float4 Color = tex2D(s1, Tex);

	Color += tex2D(s1, float2(Tex.x-(rcpres.x), Tex.y)) * 0.7;
	Color += tex2D(s1, float2(Tex.x+(rcpres.x), Tex.y)) * 0.7;

	Color += tex2D(s1, float2(Tex.x-(rcpres.x*2), Tex.y)) * 0.4;
	Color += tex2D(s1, float2(Tex.x+(rcpres.x*2), Tex.y)) * 0.4;

	Color += tex2D(s1, float2(Tex.x-(rcpres.x*3), Tex.y)) * 0.2;
	Color += tex2D(s1, float2(Tex.x+(rcpres.x*3), Tex.y)) * 0.2;

	Color += tex2D(s1, float2(Tex.x-(rcpres.x*4), Tex.y)) * 0.2;
	Color += tex2D(s1, float2(Tex.x+(rcpres.x*4), Tex.y)) * 0.2;

	Color /= 4;

	return Color;
}

float4 blurVert(in float2 Tex : TEXCOORD) : COLOR0
{
	float4 Color = tex2D(s1, Tex);

	Color += tex2D(s1, float2(Tex.x, Tex.y-(rcpres.y))) * 0.7;
	Color += tex2D(s1, float2(Tex.x, Tex.y+(rcpres.y))) * 0.7;

	Color += tex2D(s1, float2(Tex.x, Tex.y-(rcpres.y*2))) * 0.4;
	Color += tex2D(s1, float2(Tex.x, Tex.y+(rcpres.y*2))) * 0.4;

	Color += tex2D(s1, float2(Tex.x, Tex.y-(rcpres.y*3))) * 0.2;
	Color += tex2D(s1, float2(Tex.x, Tex.y+(rcpres.y*3))) * 0.2;

	Color += tex2D(s1, float2(Tex.x, Tex.y-(rcpres.y*4))) * 0.2;
	Color += tex2D(s1, float2(Tex.x, Tex.y+(rcpres.y*4))) * 0.2;

	Color /=4;

	return Color;
}

float4 bokeh_bloom(in float2 Tex : TEXCOORD) : COLOR0
{
	float4 oC = tex2D(s0, Tex);
	float4 bC = tex2D(s1, Tex);
	float d = tex2D(s2, Tex).r*1000;

	float4 toplights = pow(smoothstep(0,0.98,oC), 15)+pow(smoothstep(0,0.18,bC), 15);
	float4 fC = bC + oC + toplights;

	//fC = smoothstep(0, fC, (smoothstep(250, 4500, d)*3));
	//fC += oC;

	return toplights ;
}

technique T0
{
	pass p0
	{
		//PixelShader = compile ps_2_0 brightpass();
	}
	pass p1
	{
		PixelShader = compile ps_2_0 blurHor();
	}
	pass p2
	{
		PixelShader = compile ps_2_0 blurVert();
	}
	pass p3
	{
		PixelShader = compile ps_2_0 blurHor();
	}
	pass p4
	{
		PixelShader = compile ps_2_0 blurVert();
	}
	pass p5
	{
		PixelShader = compile ps_2_0 blurHor();
	}
	pass p6
	{
		PixelShader = compile ps_2_0 blurVert();
	}
	pass p7
	{
		PixelShader = compile ps_2_0 bokeh_bloom();
	}
}
