//*****************************************************************************
// Dynamic light shader
//*****************************************************************************
//-----------------------------------------------------------------------------
// Structures
//-----------------------------------------------------------------------------
struct ConnectData
{
   float4 texCoord        : TEXCOORD0;
   float4 position        : TEXCOORD1;
};


struct Fragout
{
   float4 col : COLOR0;
};


//-----------------------------------------------------------------------------
// Main
//-----------------------------------------------------------------------------
Fragout main( ConnectData IN,
              uniform sampler2D depthMap        : register(S0),
              uniform sampler2D shadowMap       : register(S1),
              uniform float4x4  invProj         : register(C0),
              uniform float4x4  shadowProj      : register(C4)
)
{
   Fragout OUT;

   //reconstruct original view-space position
   float4 position;
   position.xyz = mul( invProj, float3( IN.position.xy / IN.position.z, 1.0 ) ).xyz;
   float  z = tex2Dproj(depthMap, IN.texCoord).r;
   position.xy *= z;    // solve parametrically - fire the ray!
   position.z = z;
   position.w = 1.0;    //  could get proper w by multiplying by projection matrix if necessary to test


   float4 finalTexCoord = mul( shadowProj, position );
   OUT.col = tex2Dproj( shadowMap, finalTexCoord );

   return OUT;
}
