Shadow map size-based artifact

Started by
14 comments, last by belfegor 11 years ago
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?
Advertisement

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();

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!

shadowmapsurface_zpsbe691522.png

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.

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.

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.

This topic is closed to new replies.

Advertisement