Here an explanation of shadow mapping :
1) You generate a distance map from the light position, it's called shadow map, it's like depth map but for the light and not the camera.
2) You compare in the pixel shader the shadow map with the distance of the pixel in light space, this is the distance of the pixel view from the light.
If the distance of the pixel in light space is upper than the value from the shadow map, that mean the point is behind something.
If the point is behind something that mean the pixel is in shadow.
For the compare, you have to use an epsilon value because of floating point precision.
Here a the HLSL shader code for hard shadow (shadow without filtering) :
float ComputHardShadowFactor(in SURFACE_DATA SurfaceData)
{
// Transform world space to light space.
float4 PositionLightCS = mul(SurfaceData.PositionW, SunViewProj);
// Light depth in NDC space.
float LightDepth = PositionLightCS.z - 0.006f;
// Transform from NDC space to texture space.
float2 ShadowTexCoord = 0.5f * float2(PositionLightCS.x, -PositionLightCS.y) + 0.5f;
// Shadow depth test.
float ShadowMapSample = SunShadowMap.SampleLevel(ShadowSampler, ShadowTexCoord, 0).r;
return LightDepth <= ShadowMapSample;
}
Here the lighting calcule :
Light.Intensity * Light.Color * (SurfaceData.DiffuseColor * NdotL + SpecFactor) * ShadowFactor;