/*varying vec2 		texcoord;
uniform sampler2D 	tex,
					pos;
uniform float		scale;
uniform float		ofs;
varying vec2		t0,t1,t2,t3,t4,t5;

//vec2			d=vec2(ofs*0.5,0.0);

void main()
{
	float dc=-texture2D(pos, texcoord);
	vec4 color=texture2D(tex, texcoord);

	// is out focus
	if(color.a<1.0)
	{
		gl_FragColor =	(
				texture2D(tex, t0)*0.25+ // *step(dc,-texture2D(pos, t0))+
				texture2D(tex, t1)*0.125+// *step(dc,-texture2D(pos, t1))+
				texture2D(tex, t2)*0.0625+ //*step(dc,-texture2D(pos, t2))+
				
				texture2D(tex, t3)*0.25+ //*step(dc,-texture2D(pos, t3))+
				texture2D(tex, t4)*0.125+ //*step(dc,-texture2D(pos, t4))+
				texture2D(tex, t5)*0.0625+ //*step(dc,-texture2D(pos, t5))+
		
				color*0.5
				)*scale;
	}
	else
	{
		// in focus
		gl_FragColor = color;
	}
}*/

varying vec2 		texcoord;
uniform sampler2D 	tex,
					pos;
//uniform float		scale;
uniform vec2		filterTaps[12];
uniform float		focus;
uniform float		FLen;
uniform float		cocRadius;
//varying vec2		t0,t1,t2,t3,t4,t5;

//vec2			d=vec2(0.0,ofs*0.5);

/*vec2 t0 = vec2(-0.326212 * ofs, -0.40581 * ofs);
vec2 t1 = vec2(-0.840144 * ofs, -0.07358 * ofs);
vec2 t2 = vec2(-0.695914 * ofs, 0.457137 * ofs);
vec2 t3 = vec2(-0.203345 * ofs, 0.620716 * ofs);
vec2 t4 = vec2(0.96234 * ofs, -0.194983 * ofs);
vec2 t5 = vec2(0.473434 * ofs, -0.480026 * ofs);
vec2 t6 = vec2(0.519456 * ofs, 0.767022 * ofs);
vec2 t7 = vec2(0.185461 * ofs, -0.893124 * ofs);
vec2 t8 = vec2(0.507431 * ofs, 0.064425 * ofs);
vec2 t9 = vec2(0.89642 * ofs, 0.412458 * ofs);
vec2 t10 = vec2(-0.32194 * ofs, -0.932615 *ofs);
vec2 t11 = vec2(-0.791559 *ofs, -0.59771 * ofs);*/

void main()
{
	vec4 color=texture2D(tex, texcoord);
	float centerDepthBlur=-texture2D(pos, texcoord).r;
	
	/*float t0=(centerDepthBlur-focus)/focus;
	float t1=(FLen-centerDepthBlur)/FLen;
	float coc=clamp(min(t0,t1),0.0,1.0);*/
	float coc=(1.0-color.a)*cocRadius;//6.0;
	float totalContribution=1.0;
	vec4  acc=color;
	
	//float xofs=ofs*(1.0-color.a);
	// is out focus
	if(coc>0.0)
	{
	//	float 	centerDepthBlur=-texture2D(pos, texcoord).r;
		
		vec4 	tapBlur;
		
		/*vec4	c0=texture2D(tex,texcoord+vec2(-xofs,0.0));
		vec4	c3=texture2D(tex,texcoord+vec2(xofs,0.0));
		xofs+=ofs;
		vec4	c1=texture2D(tex, texcoord+vec2(-xofs,0.0));
		vec4	c4=texture2D(tex, texcoord+vec2(xofs,0.0));
		xofs+=ofs;
		vec4	c2=texture2D(tex, texcoord+vec2(-xofs,0.0));
		vec4	c5=texture2D(tex, texcoord+vec2(xofs,0.0));
			
		acc.rgb=c0.rgb*0.25+c1.rgb*0.125+c2.rgb*0.0625+c3.rgb*0.25+c4.rgb*0.125+c5.rgb*0.0625+color.rgb*0.5;*/
		
		vec2 tccoords=texcoord+filterTaps[0]*coc;
		float tapDepthBlur=-texture2D(pos, tccoords).r;
		tapBlur=texture2D(tex,tccoords);
		float tapContribution = (tapDepthBlur > centerDepthBlur) ? 1.0 : (1.0-tapBlur.a);
		acc += tapBlur*tapContribution;
		totalContribution += tapContribution;

		tccoords=texcoord+filterTaps[1]*coc;
		tapDepthBlur=-texture2D(pos, tccoords).r;
		tapBlur=texture2D(tex,tccoords);
		tapContribution = (tapDepthBlur > centerDepthBlur) ? 1.0 : (1.0-tapBlur.a);
		acc	+=tapBlur*tapContribution;
		totalContribution += tapContribution;
		
		tccoords=texcoord+filterTaps[2]*coc;
		tapDepthBlur=-texture2D(pos, tccoords).r;
		tapBlur=texture2D(tex,tccoords);
		tapContribution = (tapDepthBlur > centerDepthBlur) ? 1.0 : (1.0-tapBlur.a);
		acc	+=tapBlur*tapContribution;
		totalContribution += tapContribution;
		
		tccoords=texcoord+filterTaps[3]*coc;
		tapDepthBlur=-texture2D(pos, tccoords).r;
		tapBlur=texture2D(tex,tccoords);
		tapContribution = (tapDepthBlur > centerDepthBlur) ? 1.0 : (1.0-tapBlur.a);
		acc	+=tapBlur*tapContribution;
		totalContribution += tapContribution;
		
		tccoords=texcoord+filterTaps[4]*coc;
		tapDepthBlur=-texture2D(pos, tccoords).r;
		tapBlur=texture2D(tex,tccoords);
		tapContribution = (tapDepthBlur > centerDepthBlur) ? 1.0 : (1.0-tapBlur.a);
		acc	+=tapBlur*tapContribution;
		totalContribution += tapContribution;
		
		tccoords=texcoord+filterTaps[5]*coc;
		tapDepthBlur=-texture2D(pos, tccoords).r;
		tapBlur=texture2D(tex,tccoords);
		tapContribution = (tapDepthBlur > centerDepthBlur) ? 1.0 : (1.0-tapBlur.a);
		acc	+=tapBlur*tapContribution;
		totalContribution += tapContribution;
		
		tccoords=texcoord+filterTaps[6]*coc;
		tapDepthBlur=-texture2D(pos, tccoords).r;
		tapBlur=texture2D(tex,tccoords);
		tapContribution = (tapDepthBlur > centerDepthBlur) ? 1.0 : (1.0-tapBlur.a);
		acc	+=tapBlur*tapContribution;
		totalContribution += tapContribution;
		
		tccoords=texcoord+filterTaps[7]*coc;
		tapDepthBlur=-texture2D(pos, tccoords).r;
		tapBlur=texture2D(tex,tccoords);
		tapContribution = (tapDepthBlur > centerDepthBlur) ? 1.0 : (1.0-tapBlur.a);
		acc	+=tapBlur*tapContribution;
		totalContribution += tapContribution;
		
		tccoords=texcoord+filterTaps[8]*coc;
		tapDepthBlur=-texture2D(pos, tccoords).r;
		tapBlur=texture2D(tex,tccoords);
		tapContribution = (tapDepthBlur > centerDepthBlur) ? 1.0 : (1.0-tapBlur.a);
		acc	+=tapBlur*tapContribution;
		totalContribution += tapContribution;
		
		tccoords=texcoord+filterTaps[9]*coc;
		tapDepthBlur=-texture2D(pos, tccoords).r;
		tapBlur=texture2D(tex,tccoords);
		tapContribution = (tapDepthBlur > centerDepthBlur) ? 1.0 : (1.0-tapBlur.a);
		acc	+=tapBlur*tapContribution;
		totalContribution += tapContribution;
		
		tccoords=texcoord+filterTaps[10]*coc;
		tapDepthBlur=-texture2D(pos, tccoords).r;
		tapBlur=texture2D(tex,tccoords);
		tapContribution = (tapDepthBlur > centerDepthBlur) ? 1.0 : (1.0-tapBlur.a);
		acc	+=tapBlur*tapContribution;
		totalContribution += tapContribution;
		
		tccoords=texcoord+filterTaps[11]*coc;
		tapDepthBlur=-texture2D(pos, tccoords).r;
		tapBlur=texture2D(tex,tccoords);
		tapContribution = (tapDepthBlur > centerDepthBlur) ? 1.0 : (1.0-tapBlur.a);
		acc	+=tapBlur*tapContribution;
		totalContribution += tapContribution;
		
	//	acc.rgb=c0.rgb+c1.rgb+c2.rgb+c3.rgb+c4.rgb+c5.rgb+color.rgb*0.5;
	//	acc.rgb*=vec3(scale);
		
	/*	if(c0.a<1.0 || dc<-texture2D(pos, t0))
			acc.a+=c0.a*0.25;
		
		if(c1.a<1.0 || dc<-texture2D(pos, t1))
			acc.a+=c1.a*0.125;
			
		if(c2.a<1.0 || dc<-texture2D(pos, t2))
			acc.a+=c2.a*0.0625;

		if(c3.a<1.0 || dc<-texture2D(pos, t3))				
			acc.a+=c3.a*0.25;
		
		if(c4.a<1.0 || dc<-texture2D(pos, t4))			
			acc.a+=c4.a*0.125;
		
		if(c5.a<1.0 || dc<-texture2D(pos, t5))			
			acc.a+=c5.a*0.0625;*/
	}			
	
	gl_FragColor.rgb = acc.rgb/totalContribution;	
	
	/*	gl_FragColor.a=color.a;
	}
	else
	{
		// in focus
		gl_FragColor = color;
	}*/
}