Advertisement

PCSS Shadow Sample ?

Started by August 29, 2018 07:03 PM
2 comments, last by karnaltaB 6 years, 5 months ago

Hi,

I am looking for HLSL shader sample on PCSS shadowing. My current implementation is quite buggy and doesn't produce a soft shadow. I really have hard time with shader programming. If someone could lead me to a working sample I could try to implement in my learning 3D engine, it would be really cool.

Here is my current shader, just for info :


//
// Compute PCSS shadow.
//
float PCSS_Shadow(float3 uvd, Texture2D shadowMap, float pixelSize, float lightSize)
{	
	// Search for blockers
	float avgBlockerDepth = 0;
	float blockerCount = 0;

	for (int i = 0; i < SEARCH_POISSON_COUNT; ++i)
	{
		float2 offset = SEARCH_POISSON[i];
		float d4 = shadowMap.SampleLevel(PointWrapSampler, uvd.xy + offset, 0);
		float4 b4 = (uvd.z <= d4) ? 0.0 : 1.0;

		blockerCount += dot(b4, 1.0);
		avgBlockerDepth += dot(d4, b4);
	}

	// Check if we can early out
	if (blockerCount <= 0.0)
		return 1.0;
	else if (blockerCount == SEARCH_POISSON_COUNT)
		return 0.0;

	// Penumbra width calculation
	avgBlockerDepth /= blockerCount;
	float fRatio = 1 + (((uvd.z - avgBlockerDepth) * lightSize) / avgBlockerDepth);
	fRatio *= fRatio;

	// Apply the filter
	float att = 0;

	for (i = 0; i < PCF_POISSON_COUNT; i++)
	{
		float2 offset = fRatio * pixelSize.xx * PCF_POISSON[i];
		att += shadowMap.SampleCmpLevelZero(PCFSampler, uvd.xy + offset, uvd.z);
	}

	// Devide to normalize
	return att / PCF_POISSON_COUNT;
}

//
// PCSS Shadow for a spot light
//
float SpotShadowPCSS(float4 position, float4x4 lightViewProj, Texture2D shadowMap, float pixelSize, float lightSize)
{
	// Transform the world position to shadow projected space
	float4 posShadowMap = mul(position, lightViewProj);

	// Transform the position to shadow clip space
	float3 uvd = posShadowMap.xyz / posShadowMap.w;

	// Convert to shadow map UV values
	uvd.xy = 0.5 * uvd.xy + 0.5;
	uvd.y = 1.0 - uvd.y;

	return PCSS_Shadow(uvd, shadowMap, pixelSize, lightSize);
}

 

Nvidia has a sample that you can look at.
 

Advertisement
5 hours ago, MJP said:

Nvidia has a sample that you can look at.
 

Thank you, I will have a look at it.

I solved some of my issues thought, I now render my shadow map with back culling and so avoid self shadow issue I was encountering.

This topic is closed to new replies.

Advertisement