• Advertisement
Sign in to follow this  

Simple shadow map antialiasing?

This topic is 1704 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement

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?

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Shadow volumes aren't a very popular technique these days, but if you want perfect sharp-edged shadows they might be worth considering.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

Great news. What filter are you using - 3x3 ?  What kind of a performance impact did you notice ?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
	#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[i].SampleCmpLevelZero(shadowSampler, input.ShadowPosH[i].xy, input.ShadowPosH[i].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.

Share this post


Link to post
Share on other sites

If you use a sampler state with D3D11_FILTER_COMPARISON_MIN_MAG_MIP_LINEAR, the hardware will automatically perform 2x2 PCF when you call SampleCmp.

Share this post


Link to post
Share on other sites

In D3D9 the procedure is a bit, hacky...

http://aras-p.info/texts/D3D9GPUHacks.html#shadowmap

http://developer.amd.com/wordpress/media/2012/10/Advanced-DX9-Capabilities-for-ATI-Radeon-Cards_v2.pdf

 

You use CreateTexture to make a depth-stencil usage texture with a regular depth format like D24S8, and then you retrieve the surface from that texture to bind it as your depth-stencil target. After drawing to it, you can then bind the texture that you created to your pixel shader, and sample from it as usual (ensuring that linear filtering is enabled on the sampler), except that you put your depth value in the z coordinate of the tex-coords and the hardware will do the PCF for you.

Share this post


Link to post
Share on other sites

^ In addition to what Hodgman said. That DS surface must be followed by a proper RT, and since we don't use it in this case you might want to save memory by checking if NULL RT is supported (code chopped from my old projects):

#define D3DFMT_NULL      ((D3DFORMAT)(MAKEFOURCC('N','U','L','L')))
 
hr = d3d9->CheckDeviceFormat(adapter, deviceType, displayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL);
    if(SUCCEDDED(hr))
    {
        hr = d3d9device->CreateTexture(SMAP_DIM.cx, SMAP_DIM.cy, 1, D3DUSAGE_RENDERTARGET, D3DFMT_NULL, D3DPOOL_DEFAULT, &nullTex, NULL);
        if(FAILED(hr))
...

 

First check if it is supported and then create DS texture:

// D3DFMT_D24X8 we don't need stencil for this
hr = d3d9device->CreateTexture(SMAP_DIM.cx, SMAP_DIM.cy, 1, D3DUSAGE_DEPTHSTENCIL, D3DFMT_D24X8, D3DPOOL_DEFAULT, &smapTex, NULL);
    if(FAILED(hr))
    {
        ...// handle error
    }
    hr = smapTex->GetSurfaceLevel(0, &smapSurface);

Share this post


Link to post
Share on other sites

Interesting, but it looks like it would probably take as much effort, if not more, to implement than the current PCF that I use. Also, I understand how the PCF works, and I can't say I get this other method!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement