uniform float4x4 ViewProj;
uniform texture2d image;

uniform texture2d target;
uniform float4 color;
uniform float2 mul_val;
uniform float2 add_val;

sampler_state textureSampler {
	Filter    = Linear;
	AddressU  = Clamp;
	AddressV  = Clamp;
};

struct VertDataIn {
	float4 pos : POSITION;
	float2 uv  : TEXCOORD0;
};

struct VertDataOut {
	float4 pos : POSITION;
	float2 uv  : TEXCOORD0;
	float2 uv2 : TEXCOORD1;
};

VertDataOut VSDefault(VertDataIn v_in)
{
	VertDataOut vert_out;
	vert_out.pos = mul(float4(v_in.pos.xyz, 1.0), ViewProj);
	vert_out.uv  = v_in.uv;
	vert_out.uv2 = v_in.uv * mul_val + add_val;
	return vert_out;
}

float4 PSSubtractImageRGBA(VertDataOut v_in) : TARGET
{
	float4 rgba = image.Sample(textureSampler, v_in.uv);
	rgba.rgb = max(float3(0.0, 0.0, 0.0), rgba.rgb / rgba.a);
	rgba *= color;

	float3 targetRGB = target.Sample(textureSampler, v_in.uv2).rgb;
	rgba.rgb = saturate(rgba.rgb - targetRGB);
	rgba.rgb *= rgba.a;
	return rgba;
}

technique Draw
{
	pass
	{
		vertex_shader = VSDefault(v_in);
		pixel_shader  = PSSubtractImageRGBA(v_in);
	}
}
