Sign in to follow this  
george7378

Shadow map size-based artifact

Recommended Posts

george7378    1441

Hi everyone,

 

I've noticed that when I raise the size of my shadow map above 512x512, the following artifact creeps in at the bottom:

 

[attachment=14836:artifact1.jpg]

 

I'm not sure why - it only appears when I raise the shadow map size! Is it a simple issue?

 

Thanks.

Share this post


Link to post
Share on other sites
Jason Z    6436

Can you show the same screen shot with a smaller resolution shadow map?  Also, what is the artifact that you are describing - the shape of the shadow itself?

Share this post


Link to post
Share on other sites
george7378    1441

Hi, sorry if it wasn't visible.

 

The only thing casting a shadow should be the pyramid, but in the first pic there's a long rectangle added onto the shadow for some reason. Here's how it looks with a 512 shadow map:

 

[attachment=14838:arti2.jpg]

 

There's no rectangle at the bottom. Also, in the first picture, the artefact is shadowing the object itself too, so for some reason it seems to think there's an object blocking the light there.

Share this post


Link to post
Share on other sites
george7378    1441

I thought it might have been, but this is what I get when I render the shadow map straight to the screen - they both look the same:

 

512:

 

[attachment=14839:512sm.jpg]

 

1024:

 

[attachment=14840:1024sm.jpg]

 

Is there a way to save the shadow texture itself to a file? I guess that would be a better diagnostic!

Share this post


Link to post
Share on other sites
george7378    1441

Ah, well I dumped my render surface to a png file and it came out with this at a size of 1024x1024. I guess that's the problem. I should also say that the cutoff starts at 768 pixels down - the size of my back buffer. So does that mean I can only use maps smaller than my back buffer?

 

[attachment=14841:test.png]

Share this post


Link to post
Share on other sites
phil_t    8084

No, the backbuffer size shouldn't matter. Maybe check to make sure your viewport is correct when you're rendering your shadow map?

Share this post


Link to post
Share on other sites
Juliean    7077

Its been a while, but don't you need to set a z/depth-buffer thats at least the size of your current render target? Experienced some issues like this a while ago, I swear it was something with depth buffer, but I can't really tell anymore.

Share this post


Link to post
Share on other sites
belfegor    2835

As said above, did you created separate depth/stencil buffer beside your RT on which you render shadow depth? And set it before render to shadow RT?

Did you set new viewport based on shadow map size/camera properties?

Edited by belfegor

Share this post


Link to post
Share on other sites
george7378    1441
Ah, that'll be the problem then! So I suppose I need to use this:

http://msdn.microsoft.com/en-gb/library/windows/desktop/bb174356(v=vs.85).aspx

Followed by this:

http://msdn.microsoft.com/en-gb/library/windows/desktop/bb174431(v=vs.85).aspx

At the moment I'm already using a surface which I acquired by using GetSurfaceLevel() on my shadow map texture - is this not the same thing? I'm using this surface as my render target, do I need to create another separate surface as my depth buffer?

Share this post


Link to post
Share on other sites
belfegor    2835

When you create your device, and you specify this:

presentParams.EnableAutoDepthStencil     = TRUE;

 

there is one depth/stencil (DS) buffer created for you behind the scene and its size is as backbuffer. Then when you set render target (RT) for shadow depth, DS size must be larger then your RT. If you had 1024x768 window with shadow 512x512 its ok, but with 1024x1024 you see its larger? Its best to create yet another DS buffer with size of your RT.

And then you render something like this:

 

// backup old buffers
LPDIRECT3DSURFACE9 oldBackBuffer;
    LPDIRECT3DSURFACE9 oldDepthStencil;
    D3DVIEWPORT9 oldViewport;
    d3d9device->GetRenderTarget(0, &oldBackBuffer);
    d3d9device->GetDepthStencilSurface(&oldDepthStencil);
    d3d9device->GetViewport(&oldViewport);
 
    // set shadow buffers
    D3DVIEWPORT9 shadowViewport;
    shadowViewport.X      = 0;
    shadowViewport.Y      = 0;
    shadowViewport.Width  = SHADOW_RT_WIDTH;
    shadowViewport.Height = SHADOW_RT_HEIGHT;
    shadowViewport.MinZ   = 0;
    shadowViewport.MaxZ   = 1;
    d3d9device->SetViewport(&shadowViewport);
    d3d9device->SetRenderTarget(0, shadowSurface);
    d3d9device->SetDepthStencilSurface(shadowDS);
 
   DrawFromShadowView();
 
  // restore old buffers
d3d9device->SetViewport(&oldViewport);
    d3d9device->SetRenderTarget(0, oldBackBuffer);
    d3d9device->SetDepthStencilSurface(oldDepthStencil);
    // you must call Release to avoid memry leaks as Get...Surface increment ref count
    oldBackBuffer->Release();
    oldDepthStencil->Release();
 
    DrawFromPlayerCamera();

Share this post


Link to post
Share on other sites
george7378    1441

OK, I tried that, and yes, setting the viewport and the new depth stencil surface did work! I can now do any-size shadow maps! Thanks a lot for your help.

 

I also decided to look at the sample shadow map program in the SDK and the same routines are done in there too, including backing up the old buffers on the fly.

 

I do have one small question though, regarding D3DXSaveSurfaceToFile. I tried using it to save my ShadowDepthStencilSurface to a screenshot, but it didn't save anyhting. It works fine for the shadow surface itself, but not for the depth surface. I guess there's a good reason! Also, when I render my shadow surface to the screen, I get the usual gradient of black to white denoting depth values. However, when I save the surface to a file, I get a strange looking blue outline without any depth gradient info - why is this? I ask more out of curiosity, because it seems to be the correct thing since the shadow map works!

 

[url=http://s302.photobucket.com/user/george7378/media/shadowmapsurface_zpsbe691522.png.html]shadowmapsurface_zpsbe691522.png[/URL]

 

Share this post


Link to post
Share on other sites
belfegor    2835

What is the format of your RT surface? What is the file format that you use to save? With which image editor you visualise that image?

 

I don't think that D3DXSaveSurfaceToFile can save DS surface.

Share this post


Link to post
Share on other sites
george7378    1441

The format of my SM texture is D3DFMT_R32F and then I use GetSurfaceLevel(0, &shadowMapSurface) to get the surface. I saved it as a PNG using D3DXSaveSurfaceToFile, and then just uploaded the PNG file as it was. I guess because it's not an RGB format that it only has a red channel.

Share this post


Link to post
Share on other sites
belfegor    2835

Exactly. Also there is a 32 bits in RT red channel and on conversion to RGB png ( where is each channel 8 bits) some values are clamped (probably), and then you loose proper image representation so you can't rely on that output.

You could save as DDS witch supports that format, but then again it depends how would image editor/browser display 32 bits floating point image.

Edited by belfegor

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