Skip to main content
GameDev.net gamedev.net
🔒 Locked

Simple shadow map antialiasing?

Started by lsdogsdnbgj May 4, 2013 at 7:19 PM 23 replies 8.2k views
Original Post
lsdogsdnbgj
lsdogsdnbgj

Hi everyone,

At the moment I've got basic shadow maps, but I'd like to try and remove the blocky pixel edges by adding some antialiasing. I'm not looking for soft shadows - I want to keep them hard, but I'd like to try and make the edges smoother.

Thanks!

Veiled Glitch
Veiled Glitch

You are basing your tutorials from RasterTek aren't you?

Just asking...

lsdogsdnbgj
lsdogsdnbgj

I sometimes use Rastertek but I got my shadow mapping stuff from Riemers (XNA) and changed it to DirectX.

MJP
MJP

The standard technique is called Percentage Closer Filtering, or PCF for short. It basically amounts to sampling the shadow map multiple times in a small radius, performing the shadow comparison for each sample, and averaging the result. Which version of D3D are you using?

lsdogsdnbgj
lsdogsdnbgj

OK - I'm using D3D9 and pixel/vertex shader 2.0 - I did hear about PCF but I thought that was primarily for making shadows softer. If it reduces the visibility of texels at the edge of the shadow, then I guess I'll use that!

MJP
MJP

Well, it's actually a form of filtering but it will have the result of "softening" your shadows by giving them a penumbra. Filtering is definitely a good way to reduce aliasing (the jagged, stair-step artifacts that you're talking about), but it will also reduce the sharp details. If you want really sharp details without aliasing, then the only good solution is to increase the effective resolution of your shadow map (either by increasing the size of the shadow map, or reducing the amount of screen space that it covers). High shadow map resolution + filtering will give you sharp, unaliased shadows.

lsdogsdnbgj
lsdogsdnbgj

That's useful, thanks! Since my game takes place on the Moon, I need to try and maintain reasonably hard shadows. I'm willing to trade off a little bit of hardness for antialiasing though, so I guess PCF it is! Thanks for your help!

VladR
VladR

I'm not sure what the effective screen resolution of your shadow map is, but I found that bumping the render target of the shadow map to 4096x4096 did the trick for me - I have both the hard shadows and enough detail. And I did not have to implement PCF.

MJP
MJP

I'm not sure what the effective screen resolution of your shadow map is, but I found that bumping the render target of the shadow map to 4096x4096 did the trick for me - I have both the hard shadows and enough detail. And I did not have to implement PCF.

If your shadow map resolution is actually higher than your rendering resolution and you don't apply filtering, than you'll actually undersample the shadow map which can result in aliasing.

VladR
VladR


I'm not sure what the effective screen resolution of your shadow map is, but I found that bumping the render target of the shadow map to 4096x4096 did the trick for me - I have both the hard shadows and enough detail. And I did not have to implement PCF.

If your shadow map resolution is actually higher than your rendering resolution and you don't apply filtering, than you'll actually undersample the shadow map which can result in aliasing.

Sure, but since it is impossible to miss such an artifact (when it happens), you spend about 5 more minutes tweaking the SM's projection matrix until it is just right. And you didn't have to pay the price of PCF (or other method). Time well spent :-)

SM is just another type of effect where you can easily spend more time tweaking it than it took you to implement it. Though, that's probably true of most effects...

On the other hand, just as phil_t mentioned, this game might be a perfect candidate for Shadow Volumes (though, it's a mess compared to SM) with its razor sharp shadows.

Hodgman
Hodgman
Nowadays most shadow research is directed at soft shadows, but there used to be a popular category of "edge warping" (not sure what to call them) algorithms like this one below, which give you hard shadows with a much higher effective resolution:
http://www.cs.berkeley.edu/~ravir/6160/papers/p521-sen.pdf

There was a similar one with a cute name like "fixies", or something, but I can't recall.
Hodgman
Hodgman


There was a similar one with a cute name like "fixies", or something, but I can't recall.


Smoothies? More of a soft shadow technique though. http://people.csail.mit.edu/ericchan/papers/smoothie/

Yeah that's the one I was thinking of! Except I misremembered it being more about hiding aliasing artefacts rather than being about penumbra softening unsure.png

LancerSolurus
LancerSolurus

This is the technique I ended up emulating

http://www.gamedev.net/page/resources/_/technical/graphics-programming-and-theory/soft-edged-shadows-r2193

Works out very well for large shadow maps and you can complement it with cascaded shadow maps. It give a nice soft penumbra for all maps and all jaggies are pretty much removed. It is a bit expensive on the sampling though, I ended up doing a 27 point tap filter which can thrash the texture cache if your not careful. In my case I limited it to a 0 to 1 sample offset which helped quite a bit.

******************************************************************************************
Youtube Channel
belfegor
belfegor

^ You then need to isolate shadowmap projection in separate render pipe? I have combine it with lightning, so i cannot do it this way because it would blur things that i don't want blured.

How do you avoid same "penumbra spread" for close and far shadows? If i have not missed something from that article.

lsdogsdnbgj
lsdogsdnbgj

By the way, I added a PCF into my shader, and it actuall works pretty well - the edges are now soft, but not soft enough to make the boundaries look too blurred. I might tune it later on, but it looks fine for now!

lsdogsdnbgj
lsdogsdnbgj

It's just a 2x2 at the moment, which works fine for my needs - I haven't benchmarked it, but I couldn't see a visible drop in performance.

menohack
menohack

	#define width 4
	#define height 4

		float sum = 0.0f;
		for (int x=-width/2; x < width/2; x++)
			for (int y=-height/2; y < height/2; y++)
				sum += shadow.SampleCmpLevelZero(shadowSampler, input.ShadowPosH.xy, input.ShadowPosH.z - depthEpsilon, int2(x,y));

		sum /= 9.0f;
		sum = saturate(sum);

This is how I am doing shadow mapping. I noticed that since I am using a fixed width the shadows get smaller as the frustum of the shadow map increases in size. Before my width and height were 7, giving 49 samples. This means that if the shadow map were so large that only one pixel indicates a shadow I would still sample 48 lit pixels in both directions, causing the shadow to shrink significantly. This led me to conclude that the sampling rate should depend on the width of the camera that generates the shadow map.

The i represents each of many shadow maps for multiple lights.

Someone referenced Sen's paper on Shadow Silhouette Maps. I have tried to implement them as well and let me tell you they are a bit difficult. I still haven't got the silhouette map building correctly. I have also implemented shadow volumes which, aside from generating the volumes in a geometry shader, is pretty straight-forward. Shadow volumes give you incredibly sharp shadow boundaries but the complexity is fundamentally higher than shadow mapping because it is geometry-bound instead of resolution-bound.

Since most people like fake soft shadows from shadow mapping, and since they are fast and relatively easy, I would go with them.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.