
#ifdef VSPS_3
#define zzzpow( a, b ) pow((a),(b))

shared float4x4 projTM: register(c0);
shared float4x4 worldTM: register(c4);
shared float4 invprojTM[ 4 ]: register(c8);
shared float4x4 Bones[ 32 ]: register(c12);
shared float4 eyePosns[ 32 ]: register(c140);
shared float4 lightPosns[ 32 ]: register(c172);

shared float4 Colour_1: register(c204);
shared float4 Colour_2: register(c205);
shared float4 Specular_Colour: register(c206);
shared float4 OpacityAmount: register(c207);
shared float4 LightingBlend: register(c208);
shared float4 BumpReflectPerb: register(c209);
shared float4 FogDepth: register(c210);

shared float4 uvOffset_0: register(c211);
shared float4 uvOffset_1: register(c212);
shared float4 eyePos: register(c213);
shared float4 lightPos: register(c214);

// Samplers
sampler2D TextureSampler_1 : register(s0);
sampler2D ReflectionSampler_1 : register(s1);
sampler2D Normal_MapSampler : register(s2);
sampler2D TextureSampler_2 : register(s3);
sampler2D ReflectionSampler_2 : register(s4);
sampler2D PerbTextureSampler_1 : register(s5);
#else
#ifdef VSPS_2
#define zzzpow( a, b ) pow((a),(b))

shared float4x4 projTM;
shared float4x4 worldTM;
shared float4 invprojTM[ 4 ];
shared float4x4 Bones[ 32 ];
shared float4 eyePosns[ 32 ];
shared float4 lightPosns[ 32 ];

shared float4 Colour_1;
shared float4 Colour_2;
shared float4 Specular_Colour;
shared float4 OpacityAmount;
shared float4 LightingBlend;
shared float4 BumpReflectPerb;
shared float4 FogDepth;

shared float4 uvOffset_0;
shared float4 uvOffset_1;
shared float4 eyePos;
shared float4 lightPos;

// Samplers
sampler2D TextureSampler_1 : register(s0);
sampler2D ReflectionSampler_1 : register(s1);
sampler2D Normal_MapSampler : register(s2);
sampler2D TextureSampler_2 : register(s3);
sampler2D ReflectionSampler_2 : register(s4);
sampler2D PerbTextureSampler_1 : register(s5);
#else
#define zzzpow( a, b ) pow(abs((a)),(b))

uniform float4x4 projTM: C0;
uniform float4x4 worldTM: C4;
uniform float4 invprojTM[ 4 ]: C8;
uniform float4x4 Bones[ 32 ]: C12;
uniform float4 eyePosns[ 32 ]: C140;
uniform float4 lightPosns[ 32 ]: C172;

uniform float4 Colour_1: C204;
uniform float4 Colour_2: C205;
uniform float4 Specular_Colour: C206;
uniform float4 OpacityAmount: C207;
uniform float4 LightingBlend: C208;
uniform float4 BumpReflectPerb: C209;
uniform float4 FogDepth: C210;

uniform float4 uvOffset_0: C211;
uniform float4 uvOffset_1: C212;
uniform float4 eyePos: C213;
uniform float4 lightPos: C214;

// Samplers
uniform sampler2D TextureSampler_1 : register(s0);
uniform sampler2D ReflectionSampler_1 : register(s1);
uniform sampler2D Normal_MapSampler : register(s2);
uniform sampler2D TextureSampler_2 : register(s3);
uniform sampler2D ReflectionSampler_2 : register(s4);
uniform sampler2D PerbTextureSampler_1 : register(s5);
#endif
#endif


struct Technique1_Pass1_VS_OUT 
{
	float4 position : POSITION;
	float4 uv0 : TEXCOORD0;
	float4 uv1 : TEXCOORD1;
};

struct Technique3_Pass1_VS_OUT 
{
	float4 position : POSITION;
	float4 uv0 : TEXCOORD0;
};


Technique1_Pass1_VS_OUT Technique1_Pass1_VS(
float4 pos : POSITION,
float2 uv0 : TEXCOORD0)
{
	Technique1_Pass1_VS_OUT OUT;
	OUT.position = pos; // mul(pos,projTM);
	OUT.uv0 = uv0.xyxy+uvOffset_0;
	OUT.uv1 = uv0.xyxy+uvOffset_1;
	return OUT;
}

Technique3_Pass1_VS_OUT Technique3_Pass1_VS(
float4 pos : POSITION,
float2 uv0 : TEXCOORD0)
{
	Technique3_Pass1_VS_OUT OUT;
	OUT.position = pos; // mul(pos,projTM);
	OUT.uv0 = uv0.xyxy+uvOffset_0;
	return OUT;
}


float4 Technique1_Pass1_PS_Blur(
float4 uv0 : TEXCOORD0,
float4 uv1 : TEXCOORD1
) : COLOR
{
float4 average = {0.25f,0.25f,0.25f,0.25f};
float4 combiner;

	combiner = average*tex2D(TextureSampler_1,uv0.xy);
	combiner += average*tex2D(ReflectionSampler_1,uv0.zw);
	combiner += average*tex2D(Normal_MapSampler,uv1.xy);
	combiner += average*tex2D(TextureSampler_2,uv1.zw);
	return combiner;		
}


float4 DepthofField_Final1_PS(
float4 uv0 : TEXCOORD0
) : COLOR
{
	float4	vHighCol = tex2D( TextureSampler_1, uv0.xy );
	float4	vLowCol = tex2D( ReflectionSampler_1, uv0.zw );
	float	vBlur = vLowCol.w; // vHighCol.w
	vBlur *= vBlur;
	float4	vOutColor = lerp( vLowCol, vHighCol, vBlur );
//	vOutColor = vHighCol.aaaa;
//	vOutColor = vHighCol;
//	vOutColor = vLowCol.aaaa;
	return( vOutColor );
}


float4 DepthofField_Final2_PS(
float4 uv0 : TEXCOORD0
) : COLOR
{
	float4	vHighCol = tex2D( TextureSampler_1, uv0.xy );
	float4	vLowCol = tex2D( ReflectionSampler_1, uv0.zw );
	float	vBlur = vLowCol.w - Colour_1.a; // vHighCol.w
	vBlur *= vBlur;
	float4	vOutColor = lerp( vLowCol, vHighCol, vBlur );
	vOutColor *= Colour_1;
	return( vOutColor );
}


technique DepthofField_Start
{
	pass Pass1
	{
#ifdef VSPS_3
		VertexShader = compile vs_3_0 Technique1_Pass1_VS();
		PixelShader = compile ps_3_0 Technique1_Pass1_PS_Blur();
#else
		VertexShader = compile vs_2_0 Technique1_Pass1_VS();
		PixelShader = compile ps_2_0 Technique1_Pass1_PS_Blur();
#endif
	}
}


technique DepthofField_Final1
{
	pass Pass1
	{
#ifdef VSPS_3
		VertexShader = compile vs_3_0 Technique3_Pass1_VS();		
		PixelShader = compile ps_3_0 DepthofField_Final1_PS();
#else
		VertexShader = compile vs_2_0 Technique3_Pass1_VS();		
		PixelShader = compile ps_2_0 DepthofField_Final1_PS();
#endif
	}
}

technique DepthofField_Final2
{
	pass Pass1
	{
#ifdef VSPS_3
		VertexShader = compile vs_3_0 Technique3_Pass1_VS();		
		PixelShader = compile ps_3_0 DepthofField_Final2_PS();
#else
		VertexShader = compile vs_2_0 Technique3_Pass1_VS();		
		PixelShader = compile ps_2_0 DepthofField_Final2_PS();
#endif
	}
}



float4 Glare_PS(
float4 uv0 : TEXCOORD0,
float4 uv1 : TEXCOORD1
) : COLOR
{
float4 glare = {0.25f,0.25f,0.25f,0.0f};
float4 vScreen;

	vScreen = glare*tex2D(TextureSampler_1,uv0.xy);
	vScreen += glare*tex2D(ReflectionSampler_1,uv0.zw);
	vScreen += glare*tex2D(Normal_MapSampler,uv1.xy);
	vScreen += glare*tex2D(TextureSampler_2,uv1.zw);

	vScreen *= vScreen;
	float4 vBrightest = saturate((vScreen-Colour_1));
	vScreen = vBrightest*Colour_2;

	return vScreen;
}

float4 Glare_Final1_PS(
float4 uv0 : TEXCOORD0
) : COLOR
{
	float4 vScreenMap=tex2D(TextureSampler_1,uv0.xy);
	float4 vGlareMap=tex2D(ReflectionSampler_1,uv0.zw);
	float4 vFinal=vGlareMap+vScreenMap;
	return vFinal;
}

float4 Glare_Final2_PS(
float4 uv0 : TEXCOORD0
) : COLOR
{
	float4 vScreenMap=tex2D(TextureSampler_1,uv0.xy);
	float4 vGlareMap=tex2D(ReflectionSampler_1,uv0.zw);
	float4 vFinal=vGlareMap+vScreenMap;
#ifdef VSPS_3
#else
#ifdef VSPS_2
#else // ps3
	vFinal = saturate( vFinal );
#endif
#endif
	vFinal = vFinal*Colour_1;
	return vFinal;
}


technique Glare_Start
{
	pass Pass1
	{
#ifdef VSPS_3
		VertexShader = compile vs_3_0 Technique1_Pass1_VS();		
		PixelShader = compile ps_3_0 Glare_PS();
#else
		VertexShader = compile vs_2_0 Technique1_Pass1_VS();		
		PixelShader = compile ps_2_0 Glare_PS();
#endif
	}
}

technique Glare_Final1
{
	pass Pass1
	{
#ifdef VSPS_3
		VertexShader = compile vs_3_0 Technique3_Pass1_VS();		
		PixelShader = compile ps_3_0 Glare_Final1_PS();
#else
		VertexShader = compile vs_2_0 Technique3_Pass1_VS();		
		PixelShader = compile ps_2_0 Glare_Final1_PS();
#endif
	}
}

technique Glare_Final2
{
	pass Pass1
	{
#ifdef VSPS_3
		VertexShader = compile vs_3_0 Technique3_Pass1_VS();		
		PixelShader = compile ps_3_0 Glare_Final2_PS();
#else
		VertexShader = compile vs_2_0 Technique3_Pass1_VS();		
		PixelShader = compile ps_2_0 Glare_Final2_PS();
#endif
	}
}



void DownSampleMSAA_PS(
float4 uv0 : TEXCOORD0,
out float depth : DEPTH,
out float4 colour : COLOR
)
{
	float4 colour1 = tex2D(TextureSampler_1,uv0.xy);
	float4 colour2 = tex2D(TextureSampler_1,uv0.zw);
    colour = lerp( colour1, colour2, 0.5f );

	float3 depthFactor = float3(65536.0f / 16777215.0f, 256.0f / 16777215.0f, 1.0f / 16777215.0f);

	float3 depthColor1 = tex2D(ReflectionSampler_1,uv0.xy).rgb;
	float3 depthColor2 = tex2D(ReflectionSampler_1,uv0.zw).rgb;
	float depth1 = dot(round(depthColor1 * 255.0f), depthFactor);
	float depth2 = dot(round(depthColor2 * 255.0f), depthFactor);
	depth = lerp( depth1, depth2, 0.5f );
}


technique DownSampleMSAA
{
	pass Pass1
	{
#ifdef VSPS_3
		VertexShader = compile vs_3_0 Technique3_Pass1_VS();		
		PixelShader = compile ps_3_0 DownSampleMSAA_PS();
#else
		VertexShader = compile vs_2_0 Technique3_Pass1_VS();		
		PixelShader = compile ps_2_0 DownSampleMSAA_PS();
#endif
	}
}



float4 FadeOut_PS(
float4 uv0 : TEXCOORD0
) : COLOR
{
	return Colour_1;
}


technique FadeOut
{
	pass Pass1
	{
#ifdef VSPS_3
		VertexShader = compile vs_3_0 Technique1_Pass1_VS();		
		PixelShader = compile ps_3_0 FadeOut_PS();
#else
		VertexShader = compile vs_2_0 Technique1_Pass1_VS();		
		PixelShader = compile ps_2_0 FadeOut_PS();
#endif
	}
}
