• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
lipsryme

Shadow mapping working but looks horrible (precision issue?)

11 posts in this topic

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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites
@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
0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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!

1

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  
Followers 0