texture g_DiffuseTexture;
sampler DiffuseSampler = 
sampler_state
{
    Texture = <g_DiffuseTexture>;
    MinFilter = LINEAR;  
    MagFilter = LINEAR;
	AddressU = CLAMP;
    AddressV = CLAMP;
};

texture g_OpacityTexture;
sampler OpacitySampler = 
sampler_state
{
    Texture = <g_OpacityTexture>;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
	AddressU = CLAMP;
    AddressV = CLAMP;
};

float g_fTexWidth;
float g_fTexHeight;


float4 EdgeFilterPS(float2 Tex:TEXCOORD0) : COLOR0
{
	float t[9];
	for(int i = 0; i < 9; ++i)
	{
		float4 tx = tex2D(DiffuseSampler, Tex - float2(((i%3)-1) / g_fTexWidth, ((int)(i/3)-1) / g_fTexHeight));
		t[i] = (tx.x + tx.y + tx.z) / 3;
	}
	float w[9] = {
		{-1, 0, 1},
		{-2, 0, 2},
		{-1, 0, 1},
	};
	float h[9] = {
		{-1, -2, -1},
		{0, 0, 0},
		{1, 2, 1},
	};

	float x = 0, y = 0;
	for(int i = 0; i < 9; ++i)
	{
		x += w[i] * t[i];
	}
	for(int i = 0; i < 9; ++i)
	{
		y += h[i] * t[i];
	}

	//float e = (x*x + y*y < 0.04) ? 0 : .4;
	float e = saturate(floor(sqrt(x*x + y*y) * 8) / 7) * 0.75;
	// 가려지는 외곽선은 그리지 않음
	/*if(tex2D(DiffuseSampler, Tex).a - tex2D(OpacitySampler, Tex).a >  0.001)
	{
		e = 0;
	}*/
	return float4(0, 0, 0, e);
}


technique EdgeFilterRender
{
	pass P0
	{
		AlphaBlendEnable = TRUE;
		BlendOp = ADD;
		DestBlend = INVSRCALPHA;
		SrcBlend = SRCALPHA;
		ZWriteEnable = FALSE;
		PixelShader = compile ps_3_0 EdgeFilterPS();
	}
}

float g_BlurAmount = 0.5;
static const int nSamples = 9;

float4 MotionBlurFilterPS(float2 Tex:TEXCOORD0) : COLOR0
{
	float2 vel = tex2D(OpacitySampler, Tex);
	float2 pixelVel = vel.xy * g_BlurAmount;
	pixelVel.y = -pixelVel.y;
	float3 blurred = float3(0, 0, 0);
	for(int i = 0; i < nSamples; ++i)
	{
		float2 lookup = -pixelVel * i / nSamples + Tex;
		blurred += tex2D(DiffuseSampler, lookup);
	}
	return float4(blurred / nSamples , 1.f);
}

technique MotionBlurFilterRender
{
	pass P0
	{
		PixelShader = compile ps_3_0 MotionBlurFilterPS();
	}
}

float4 BasicPS(float2 Tex:TEXCOORD0) : COLOR0
{
	return tex2Dproj(DiffuseSampler, float4(Tex, 0, 1));
}

technique BasicRender
{
	pass P0
	{
		PixelShader = compile ps_3_0 BasicPS();
	}
}