HLSL - how to streamline this shadow map filter?

Started by
1 comment, last by JohnnyCode 4 years, 6 months ago

Hey everyone,

I have the following loop in my main shader which samples my shadow map with a 3x3 PCF grid to achieve anti-aliased shadows:


int shadowFactor = 0;
for (int y = -1; y <= 1; y++)
{
	for (int x = -1; x <= 1; x++)
	{
		float2 shadowSampleOffset = float2(x/ShadowMapSize, y/ShadowMapSize);
		if (tex2D(ShadowMapTextureSampler, projectedTextureCoordinatesLight + shadowSampleOffset).r < input.ProjectedPositionLight.z - 0.002f)
		{
			shadowFactor += 1;
		}
	}
}
float lightingCorrectionFactor = 1 - shadowFactor/9.0f;
diffuseLightingFactor *= lightingCorrectionFactor;
specularLightingFactor *= lightingCorrectionFactor;

...however, it always pushes the number of instructions up to the point where I normally need to use a higher shader model (in MonoGame, it causes me to go from the default 4_0_level_9_1 up to 4_0_level_9_3).

Can anyone see a more obvious and efficient way to do the PCF operation?

Thanks!

Advertisement

Since the samples are pixel tight, you might consider derivatives on sampler value, ddx, and ddy instead? I don't see a way I'd optimize it though the way it is, better than the compiler.

This topic is closed to new replies.

Advertisement