View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Shadow mapping working but looks horrible (precision issue?)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1lipsryme  Members

Posted 07 February 2013 - 09:24 AM

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
//--------------------------------------------------------------------------------------
{

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

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 xWeight = 1;
float yWeight = 1;

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

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

shadowVisibility += sample * xWeight * yWeight;
}
}

}


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, 07 February 2013 - 09:31 AM.

### #2PixelSmasher  Members

Posted 07 February 2013 - 12:10 PM

Hmmm sounds like an half-pixel issue to me...

### #3lipsryme  Members

Posted 07 February 2013 - 12:21 PM

Hmmm sounds like an half-pixel issue to me...

meaning...?

### #4PixelSmasher  Members

Posted 07 February 2013 - 12:31 PM

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, 07 February 2013 - 12:39 PM.

### #5lipsryme  Members

Posted 07 February 2013 - 12:39 PM

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, 07 February 2013 - 12:47 PM.

### #6PixelSmasher  Members

Posted 07 February 2013 - 12:54 PM

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

### #7CC Ricers  Members

Posted 07 February 2013 - 12:57 PM

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) .

New game in progress: Project SeedWorld

My development blog: Electronic Meteor

### #8lipsryme  Members

Posted 07 February 2013 - 01:12 PM

@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, 07 February 2013 - 01:13 PM.

### #9PixelSmasher  Members

Posted 08 February 2013 - 04:13 AM

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

### #10lipsryme  Members

Posted 08 February 2013 - 06:28 AM

Hmm not sure outputting the fracs shows me anything helpful  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);


Edited by lipsryme, 08 February 2013 - 08:15 AM.

### #11Seabolt  Members

Posted 19 February 2013 - 02:18 PM

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.

Perception is when one imagination clashes with another

### #12Seabolt  Members

Posted 19 February 2013 - 02:20 PM

This is what my CalculateShadowTerm function looks like:

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

#ifdef FORCE_SAMPLE_COUNT
float2 samplePoint = shadowTexCoord;
float depth = SAMPLE_TEXTURE_2D(shadow, samplePoint).x;

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

//
// Adjust our texcoords.
//

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;

}
}

shadowTerm /= (sampleCount * sampleCount);
#endif


Good Luck!

Perception is when one imagination clashes with another

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.