texture vd_srctexture;
float4 vd_texsize;

sampler src = sampler_state {
	Texture = <vd_srctexture>;
	AddressU = clamp;
	AddressV = clamp;
	MinFilter = Linear;
	MagFilter = Linear;
	MipFilter = None;
};

void VS(
	float4 pos : POSITION,
	float2 t0 : TEXCOORD0,
	out float4 oPos : POSITION,
	out float2 oT0 : TEXCOORD0,
	out float2 oT1 : TEXCOORD1,
	out float2 oT2 : TEXCOORD2,
	out float2 oT3 : TEXCOORD3
)
{
	oPos = pos;
		
	oT0 = t0 + vd_texsize.wz * float2(-0.5, -0.5);
	oT1 = t0 + vd_texsize.wz * float2(+0.5, -0.5);
	oT2 = t0 + vd_texsize.wz * float2(-0.5, +0.5);
	oT3 = t0 + vd_texsize.wz * float2(+0.5, +0.5);
}

float4 PS(
	float2 t0 : TEXCOORD0,
	float2 t1 : TEXCOORD1,
	float2 t2 : TEXCOORD2,
	float2 t3 : TEXCOORD3
) : COLOR0
{
	float4 p0 = tex2D(src, t0);
	float4 p1 = tex2D(src, t1);
	float4 p2 = tex2D(src, t2);
	float4 p3 = tex2D(src, t3);

	const float3 luma = {0.30, 0.59, 0.11};
	float x0 = dot((p0-p3).xyz, luma);
	float x1 = dot((p1-p2).xyz, luma);

	return float4(x0+x1, x0-x1, 0, 0)*0.5+0.5;
}

technique {
	pass {
		VertexShader = compile vs_1_1 VS();
//		PixelShader = compile ps_1_1 PS();

		PixelShader = asm {
			ps_1_1
			def c0, 0.30, 0.59, 0.11, 0.5
			def c1, 0, 0, 1, 0
			def c2, 0, 1, 0, 0
			tex t0
			tex t1
			tex t2
			tex t3
			add t0.rgb, t0, -t3
			add t1.rgb, t1, -t2
			dp3 t0.rgb, t0, c0
			dp3 t1.rgb, t1, c0
			add_d2 r0.rgb, t0, t1
			+add_d2 r0.a, t0.b, -t1.b
			mad r0.rgb, r0, c1, c0.a
			mad r0.rgb, r0.a, c2, r0
		};

		Sampler[0] = <src>;
		Sampler[1] = <src>;
		Sampler[2] = <src>;
		Sampler[3] = <src>;
	}
}
