////////////////////////////////////////////////////////////////////////////////////////////////////
// Includes
////////////////////////////////////////////////////////////////////////////////////////////////////

#include "../../../types.txt"

////////////////////////////////////////////////////////////////////////////////////////////////////
// Structures
////////////////////////////////////////////////////////////////////////////////////////////////////

struct PS_INPUT
{
#if MIX_SM_HIGH
	float4 pos : SV_POSITION;
#endif //MIX_SM_HIGH
	float2 tex : TEXCOORD0;
};

////////////////////////////////////////////////////////////////////////////////////////////////////
// Grobal Values
////////////////////////////////////////////////////////////////////////////////////////////////////

#if MIX_SM_HIGH

	cbuffer cbInput : register( b0 )
	{
		float4 g_Color;
		float4 g_Params;
	};

	// Color
	Texture2D    g_ColorTexture : register( t0 );
	SamplerState g_ColorSampler : register( s0 );

	// Depth
	Texture2D    g_DepthTexture : register( t1 );
	SamplerState g_DepthSampler : register( s1 );
	
#else //MIX_SM_HIGH

	float4 g_Color  : register( c0 );
	float4 g_Params : register( c1 );

	sampler g_ColorSampler  : register( s0 ); // Color
	sampler g_DepthSampler  : register( s1 ); // Depth

#endif //MIX_SM_HIGH

////////////////////////////////////////////////////////////////////////////////////////////////////
// }N
////////////////////////////////////////////////////////////////////////////////////////////////////

#if MIX_SM_HIGH

	#define TEX_COLOR( uv ) g_ColorTexture.Sample( g_ColorSampler, uv )
	#define TEX_DEPTH( uv ) g_DepthTexture.Sample( g_DepthSampler, uv )

#else //MIX_SM_HIGH

	#define TEX_COLOR( uv ) tex2D( g_ColorSampler, uv )
	#define TEX_DEPTH( uv ) tex2D( g_DepthSampler, uv )

#endif //MIX_SM_HIGH

#define W_DENSITY   ( g_Params.x )
#define W_INTENSITY ( g_Params.y )
#define W_THRESHOLD ( g_Params.z )
#define W_OFFSET    ( g_Params.w )

////////////////////////////////////////////////////////////////////////////////////////////////////
// C֐̒`
////////////////////////////////////////////////////////////////////////////////////////////////////

//ʉtB^[
float4 main( PS_INPUT input ) : MSV_TARGET0
{
	float depth = TEX_DEPTH( input.tex ).r;
	float4 color = TEX_COLOR( input.tex );
	float ratio = 1.0 - exp( -pow( abs( depth * W_DENSITY ), W_INTENSITY ) );

	float4 output;

	////////////////////////////////////////////////////////////////////////////////////////////////////
	// 
	////////////////////////////////////////////////////////////////////////////////////////////////////

	ratio = saturate( ratio );

	////////////////////////////////////////////////////////////////////////////////////////////////////
	// {F
	////////////////////////////////////////////////////////////////////////////////////////////////////

//	color.rgb = color.rgb + ( g_Color.rgb * g_Color.a ) * ( 1.0 - ratio );
	color.rgb = lerp( color.rgb, g_Color.rgb, g_Color.a );

	////////////////////////////////////////////////////////////////////////////////////////////////////
	// ŏIF
	////////////////////////////////////////////////////////////////////////////////////////////////////

	output.rgb = lerp( color.rgb, ( g_Color.rgb * g_Color.a ), ratio );
	output.rgb -= W_THRESHOLD;
	output.rgb *= W_OFFSET;
	output.a = color.a;

	return saturate( output );
}
