////////////////////////////////////////////////////////////////////////////////////////////////////
// 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_Params0;
		float4 g_Params1;
		float4 g_SamplingTable[12];
	};

	//Depth
	Texture2D    g_DepthTexture  : register( t0 );
	SamplerState g_DepthSampler  : register( s0 );

	//Data
	Texture2D    g_DataTexture   : register( t1 );
	SamplerState g_DataSampler   : register( s1 );

	//Color
	Texture2D    g_ColorTexture0 : register( t2 );
	SamplerState g_ColorSampler0 : register( s2 );

	//Color( shadings 1 )
	Texture2D    g_ColorTexture1 : register( t3 );
	SamplerState g_ColorSampler1 : register( s3 );

	//Color( shadings 2 )
	Texture2D    g_ColorTexture2 : register( t4 );
	SamplerState g_ColorSampler2 : register( s4 );
	
#else //MIX_SM_HIGH

	float4 g_Params0           : register( c0 );
	float4 g_Params1           : register( c1 );
	float4 g_SamplingTable[12] : register( c2 );

	//Depth
	sampler g_DepthSampler  : register( s0 );

	//Data
	sampler g_DataSampler   : register( s1 );

	//Color
	sampler g_ColorSampler0 : register( s2 );
	
	//Color( shadings 1 )
	sampler g_ColorSampler1 : register( s3 );

	//Color( shadings 2 )
	sampler g_ColorSampler2 : register( s4 );

#endif //MIX_SM_HIGH

////////////////////////////////////////////////////////////////////////////////////////////////////
// Macros
////////////////////////////////////////////////////////////////////////////////////////////////////

#if MIX_SM_HIGH

	#define TEX_DEPTH( uv )  g_DepthTexture.Sample( g_DepthSampler, uv )
	#define TEX_DATA( uv )   g_DataTexture.Sample( g_DataSampler, uv )
	#define TEX_COLOR0( uv ) g_ColorTexture0.Sample( g_ColorSampler0, uv )
	#define TEX_COLOR1( uv ) g_ColorTexture1.Sample( g_ColorSampler1, uv )
	#define TEX_COLOR2( uv ) g_ColorTexture2.Sample( g_ColorSampler2, uv )

#else //MIX_SM_HIGH

	#define TEX_DEPTH( uv )  tex2D( g_DepthSampler, uv )
	#define TEX_DATA( uv )   tex2D( g_DataSampler, uv )
	#define TEX_COLOR0( uv ) tex2D( g_ColorSampler0, uv )
	#define TEX_COLOR1( uv ) tex2D( g_ColorSampler1, uv )
	#define TEX_COLOR2( uv ) tex2D( g_ColorSampler2, uv )

#endif //MIX_SM_HIGH


#define P_NEAR g_Params0.x
#define P_FAR g_Params0.y
#define P_INV_NEAR_DIST g_Params0.z
#define P_INV_FAR_DIST g_Params0.w

#define BT g_Params1.x
#define INV_BT_N g_Params1.y
#define INV_BT_F g_Params1.z

#define SAMPLING_TABLE12 g_SamplingTable

////////////////////////////////////////////////////////////////////////////////////////////////////
// メイン関数の定義
////////////////////////////////////////////////////////////////////////////////////////////////////

float calcRatio( float z, float opacity )
{
	float ratio = 0.0;
	
	if( P_NEAR > z )
	{
		ratio = ( P_NEAR - z ) * P_INV_NEAR_DIST;
	}
	else if( P_FAR < z )
	{
		ratio = ( z - P_FAR ) * P_INV_FAR_DIST;
	}

	return saturate( ratio ) * opacity;
}

float4 main( PS_INPUT input ) : MSV_TARGET0
{
	float curZ;
	float curRatio;
	float mixRatio;

	float2 smpTex;
	float smpZ;
	float smpRatio;
	float smpCount;

	float mr0;
	float mr1;

	float4 color0;
	float4 color1;
	float4 color2;

	float4 output;

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

	curZ = TEX_DEPTH( input.tex ).x;
	curRatio = calcRatio( curZ, TEX_DATA( input.tex ).a );
	mixRatio = curRatio;
	smpCount = 1.0;
	
	for( int i = 0; i < 12; i++ )
	{
		smpTex = input.tex + SAMPLING_TABLE12[i].xy;
		smpZ = TEX_DEPTH( smpTex ).x;

		smpRatio = calcRatio( smpZ, TEX_DATA( smpTex ).a );

		if( curRatio < ( smpRatio * step( smpZ, curZ ) ) )
		{
			mixRatio += smpRatio;
			smpCount += 1.0;
		}
	}

	mixRatio *= rcp( smpCount );

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

	mr0 = saturate( mixRatio * INV_BT_N );
	mr1 = saturate( ( mixRatio - BT ) * INV_BT_F );

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

	color0 = TEX_COLOR0( input.tex );
	color1 = TEX_COLOR1( input.tex );// * float4( 1.0, 0.0, 0.0, 1.0 );
	color2 = TEX_COLOR2( input.tex );// * float4( 0.0, 0.0, 1.0, 1.0 );

	output.rgb = lerp( color0.rgb, color1.rgb, mr0 );
	output.rgb = lerp( output.rgb, color2.rgb, mr1 );
	output.a = color0.a;

	return output;
}
