Basically, in order to speed up shadow rendering we can check whether we are shading a pixel that faces away from the light, and in that case we can skip shadow computation (we know this pixel is in shadow).
So I implemented this. Here comes the code:
float NdotL = saturate(dot(-input.lightDirection, normal));
if (NdotL > 0.0)
{
diffuse *= NdotL;
input.shadowMapTexCoord.z += sunLightShadowMapBias;
#ifdef SOFT_SHADOWS
float shadow = 0.0f;
for (int i = 0; i < SOFT_SHADOWS_SAMPLES_NUM; i++)
{
float4 texCoord = input.shadowMapTexCoord;
texCoord.xy += sunLightShadowsSoftness * poissonDiskTexCoords[i];
shadow += tex2Dproj(shadowMap, texCoord).r;
}
shadow /= (float)SOFT_SHADOWS_SAMPLES_NUM;
#else
float shadow = tex2Dproj(shadowMap, input.shadowMapTexCoord).r;
#endif
output.color += shadow * diffuse;
}
I'm using Cg and compile to D3D9 SM3.0 shaders, and OGL ARB shaders.
There is one problem however. Under D3D9 rendering is broken, giving me some random-colored flat-shaded surfaces. I would guess that hardware or the driver cannot handle texture lookups inside the if-statement but that could be a problem for a mipmapped texture. My shadow map has no mipmaps so I would expect texture lookups to work inside if-statements.
Moreover, on OpenGL everything works fine, yielding some slight performance boost when the if-statement is on.
Any ideas?






