
#pragma warning(disable:3553)	// warning X3553: can't use gradient instructions in loops with break, forcing loop to unroll

// 輝度テクスチャ
sampler2D IntensityTex :register(s0);

//
uniform float u_invAspect;
uniform float u_invIntensityTexSize;

//
struct PS_INPUT_T
{
	float2 UV  : TEXCOORD0;
};

// クロスグレア
float3 CrossGlare(sampler2D tex, float2 texCoord, float step)
{
	// 適当にそれらしくした
	float4 texCol, texCol2;
//	float  tbl[] = {1.0, 1.0/1.5, 1.0/2.0, 1.0/2.0, 1.0/2.5, 1.0/2.5, 1.0/3.0, 1.0/3.0};
	float  tbl[] = {1.0, 1.0/1.5, 1.0/2.0, 1.0/2.5, 1.0/3.0, 1.0/4.0, 1.0/5.5, 1.0/7.0};
	float  v, ofx, st, ed;
	int   i;
	texCol = 0.0;
	//st = (0.0+0.5)*step;
	//ed = (7.0+0.5)*step;
	st = (0.0)*step;
	ed = (7.0)*step;
	for(v=st, i=0; v<=ed && i<8; v+=step, i++){
		ofx = v * u_invAspect;
		texCol += tex2D( tex, float2(texCoord.x+ofx, texCoord.y+v)) * tbl[i];
		texCol += tex2D( tex, float2(texCoord.x-ofx, texCoord.y-v)) * tbl[i];
		texCol += tex2D( tex, float2(texCoord.x-ofx, texCoord.y+v)) * tbl[i];
		texCol += tex2D( tex, float2(texCoord.x+ofx, texCoord.y-v)) * tbl[i];
	}
	texCol /= (32.0*0.08);

	texCol2 = tex2D( tex, float2(texCoord.x, texCoord.y));
	//texCol = texCol2 * texCol2 + texCol * (1.0 - texCol2);
	texCol = lerp(texCol, texCol2, texCol2);
	return(texCol.rgb);
}

//
float4 main( PS_INPUT_T Input ) : COLOR0
{
	float4 a;
	float step;
	step = u_invIntensityTexSize;
	a.rgb = CrossGlare(IntensityTex, Input.UV, step);
	a.a = 1.0;
	return(a);

	//return( tex2D( IntensityTex, Input.UV ) );
	//return( Input.Col );
}
