float4x4 g_matInvViewProj;

float g_fTCRNear;
float g_fTCRFar;

#include "ShadowMapping.fx"

texture g_texDepth;
sampler2D g_sDepth = sampler_state
{
    Texture = <g_texDepth>;
    AddressU = Clamp;
    AddressV = Clamp;
    MipFilter = NONE;
    MagFilter = POINT;
    MinFilter = POINT;
};

texture g_texOptConf;
sampler2D g_sOptConf = sampler_state
{
    Texture = <g_texOptConf>;
    AddressU = Clamp;
    AddressV = Clamp;
    MipFilter = NONE;
    MagFilter = POINT;
    MinFilter = POINT;
};

struct SHADOW_OUT
{
	float4 Shadow : COLOR0;
	float Z : DEPTH;
	//float4 SPass : COLOR1;
};

SHADOW_OUT PSTestDepthConfReached(float2 ScreenCoord:TEXCOORD0)
{
	SHADOW_OUT o;

	float depth = tex2D(g_sDepth, ScreenCoord).r;
	//clip if outside range of interrest
	if(depth > g_fTCRFar || depth < g_fTCRNear)
		clip(-1);

	o.Z = 1;
	o.Shadow = 1;

	return o;
}

//renders a quad and cause auf depth test only not converged pixels get rendered
SHADOW_OUT PSTestConverged(float2 ScreenCoord:TEXCOORD0)
{
	SHADOW_OUT o;

	o.Z = 1;
	o.Shadow = 1;

	return o;
}

technique TestDepthConfReached
{
    pass P0
    {      
		CullMode = NONE;
		ZEnable = true;
		ZWriteEnable = false;
		ZFunc = GREATER;
		AlphaBlendEnable = false;
		AlphaTestEnable = false;
		//ColorWriteEnable = 0; //set by app

        VertexShader = null;
        PixelShader  = compile ps_2_0 PSTestDepthConfReached();
    }
}

//renders a quad and cause auf depth test only not converged pixels get rendered
technique TestConverged
{
    pass P0
    {      
		CullMode = NONE;
		ZEnable = true;
		ZWriteEnable = false;
		ZFunc = GREATER;
		AlphaBlendEnable = false;
		AlphaTestEnable = false;
		//ColorWriteEnable = 0; //set by app

        VertexShader = null;
        PixelShader  = compile ps_2_0 PSTestConverged();
    }
}