Sign in to follow this  
lipsryme

Shadow mapping working but looks horrible (precision issue?)

Recommended Posts

lipsryme    1522

Hey guys, so I've got my CSM working for some time but the quality isn't really that great.

Cascades are working as intended if I distance myself from them.

I'm using the following values for the scene I've captured below:

 

static const float ShadowDist = 0.3f; // 0.08
static const float Backup = 5.0f;
static const float CascadeSplits[4] = { 0.125f, 0.25f, 0.5f, 1.0f };
static const float Bias = 0.008f;
static const float nearClip = 1.0f;
static const float camNear = 1.0f;
static const float camFar = 50.0f;

 

And the shadow map is being sampled like this:

 

//--------------------------------------------------------------------------------------
// Samples the shadow map cascades based on the world-space position, using edge-tap
// smoothing PCF for filtering
//--------------------------------------------------------------------------------------
float SampleShadowCascade(in float3 positionVS, in uint cascadeIdx)
{
	float4x4 shadowMatrix = ShadowMatrices[cascadeIdx];
	float3 shadowPosition = mul(float4(positionVS, 1.0f), shadowMatrix).xyz;
	float2 shadowTexCoord = shadowPosition.xy;
	float shadowDepth = shadowPosition.z;

	// Edge tap smoothing
	const int Radius = 2;
	const float ShadowMapSize = shadowMapSize.x * 2;
	const int NumSamples = (Radius * 2 + 1) * (Radius * 2 + 1);

	float2 fracs = frac(shadowTexCoord.xy * ShadowMapSize);
	float leftEdge = 1.0f - fracs.x;
	float rightEdge = fracs.x;
	float topEdge = 1.0f - fracs.y;
	float bottomEdge = fracs.y;

	float shadowVisibility = 0.0f;


	[unroll(NumSamples)]
	for (int y = -Radius; y <= Radius; y++)
	{
		[unroll(NumSamples)]
		for (int x = -Radius; x <= Radius; x++)
		{
			float2 offset = float2(x, y) * (1.0f / ShadowMapSize);
			float2 sampleCoord = shadowTexCoord + offset;
			float sample = ShadowMap.SampleCmp(ShadowMapSampler, sampleCoord, shadowDepth).x;

			float xWeight = 1;
			float yWeight = 1;

			if(x == -Radius)
				xWeight = leftEdge;
			else if(x == Radius)
				xWeight = rightEdge;

			if(y == -Radius)
				yWeight = topEdge;
			else if(y == Radius)
				yWeight = bottomEdge;

			shadowVisibility += sample * xWeight * yWeight;
		}
	}

	shadowVisibility  /= NumSamples;
	shadowVisibility *= 1.5f;


	return shadowVisibility;
}

 

video link here: http://youtu.be/2cCm_MhVA3A

I just don't get how the first cascade up close would look that blocky !?

Btw the directional light is moving in this video so that's why it's flickering/moving.

Each cascade (of 4) is 256x256. 512x512 only makes the silhouettes look finer but the problem remains.

 

I've tried different near / far clip values but that doesn't seem to fix the issues.

Any ideas ? Haven't tried storing the depth exponentially, do you think that would help me ?

Edited by lipsryme

Share this post


Link to post
Share on other sites
PixelSmasher    445

Sorry it was like I was talking to myself ^^

 

I've already encountered this problem while coding PCF, the problem was located in

float2 fracs = frac(shadowTexCoord.xy * ShadowMapSize);

 

I remember that the solution was something like:

float2 fracs = frac(shadowTexCoord.xy * ShadowMapSize + float2(0.5, 0.5));

 

EDIT : or it could be something similar when you compute your offsets in:

float2 offset = float2(x, y) * (1.0f / ShadowMapSize);

Try:

float2 offset = (float2(x, y) + float2(0.5, 0.5)) * (1.0f / ShadowMapSize);

 


 

Edited by PixelSmasher

Share this post


Link to post
Share on other sites
lipsryme    1522

This is how it looks with when I do:

float2 offset = float2(x + 0.5, y + 0.5) * (1.0f / ShadowMapSize)

Same with the other one :/
http://d.pr/i/FTWc

 

I don't actually think the problem is the filtering since this is how it looks unfiltered:

http://d.pr/i/hEXh which is to say horrible.

Edited by lipsryme

Share this post


Link to post
Share on other sites
PixelSmasher    445

Clearly the issue comes from bad weights applied to your boolean coefficients.

Try outputting the weight values and see if some artifacts appear.

 

That's the good old way of debugging your shaders ;p

Share this post


Link to post
Share on other sites
CC Ricers    1491

You do not want to add float2(0.5, 0.5) as it is a very large value compared to float2(1.0f / ShadowMapSize) given the size of the texture. That's why the stripe pattern occurs.

You need to halve the numerator. Try float2(x, y) * (0.5f / ShadowMapSize) .

Share this post


Link to post
Share on other sites
lipsryme    1522
@CC Ricers That didn't help either :/

@PixelSmasher You mean output the edge floats?(leftEdge, rightEdge,..)
But as you can see on the picture above even without the edge tap filter it looks like this blocky mess. Edited by lipsryme

Share this post


Link to post
Share on other sites
PixelSmasher    445

A blocky mess ? As no filtering is applied, it looks perfectly fine - and shitty - to me. You should add some bias to prevent banding on curved meshes though.

You are facing a nasty math-related bug and it always means having a bad time eradicating it.

 

Try outputting all the values you can to empirically find out what the issue is and be amazed when you discover it was so simple :)

(I'd really like to see the values of the variable "fracs" !)

 

Another trick that could work : use (ShadowMapSize + float2(1, 1)), it helped me in some situations

Share this post


Link to post
Share on other sites
lipsryme    1522

Hmm not sure outputting the fracs shows me anything helpful tongue.png http://d.pr/i/yxp2 (It does look the same in MJP's samples though...)

 

edit: the bias is there it's being applied in the application like this:

 

// Apply the scale / offset / bias matrix, which transforms from [-1,1]
// post-projection space to [0,1] UV space
const float bias = Bias;
XMMATRIX texScaleBias;
texScaleBias.r[0] = XMVectorSet(0.5f,  0.0f,  0.0f,  0.0f);
texScaleBias.r[1] = XMVectorSet(0.0f, -0.5f,  0.0f,  0.0f);
texScaleBias.r[2] = XMVectorSet(0.0f,  0.0f,  1.0f,  0.0f);
texScaleBias.r[3] = XMVectorSet(0.5f,  0.5f, -bias,  1.0f);
shadowMatrix = XMMatrixMultiply(shadowMatrix, texScaleBias);

 

 

Edited by lipsryme

Share this post


Link to post
Share on other sites
Seabolt    781

Haha I honestly just finished my shadow mapping implementation based on MJPs sample a week ago. I found that I had issues where you do the weighting based on the edge, so I removed it. It fixes some of the stranger banding towards the edges. Next I removed that multiply by 1.55f, and added a saturate after the calculation, and my shadow are looking fairly good.

Though tbh, I still have some banding when I get into the lower pcf taps.

Share this post


Link to post
Share on other sites
Seabolt    781

This is what my CalculateShadowTerm function looks like:

 

	float shadowTerm = 0.0f; 
	float shadowMapWidth = ShadowOffsets[0].x;
	float shadowMapHeight = ShadowOffsets[0].y;
	float2 halfTexelOffsets = float2( 0.5f * (1.0f/shadowMapWidth), 0.5f * (1.0f/shadowMapHeight) ); 
	float shadowBias = ShadowOffsets[0].z;
	float sampleCount = ShadowOffsets[0].w;

#ifdef FORCE_SAMPLE_COUNT
	float2 samplePoint = shadowTexCoord;			
	float depth = SAMPLE_TEXTURE_2D(shadow, samplePoint).x;
	float shadowSample = (shadowDepth <= depth + shadowBias);

	shadowTerm = shadowSample;
	return shadowTerm;
#else
	float radius = (sampleCount - 1.0f) / 2.0f;

	//
	// Adjust our texcoords.
	//

	shadowTexCoord -= halfTexelOffsets;
	
	for (float y = -radius; y <= radius; y++)
	{
		for (float x = -radius; x <= radius; x++)
		{
			float2 offset = float2(x / shadowMapWidth, y / shadowMapHeight );				
			float2 samplePoint = shadowTexCoord + offset;			
			float depth = SAMPLE_TEXTURE_2D(shadow, samplePoint).x;
			float shadowSample = (shadowDepth <= depth + shadowBias);

			shadowTerm += shadowSample;
		}											
	}		
	
	shadowTerm /= (sampleCount * sampleCount);
	shadowTerm = saturate( shadowTerm );
	return shadowTerm;
#endif

 

 Good Luck!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this