Simple Shadow Mapping

Started by
10 comments, last by Endemoniada 10 years, 8 months ago

Hi guys, I'm trying to get simple shadow mapping working before I move on to percentage-closer and/or variance methods. I can't seem to get it to work so first I want to make sure I'm making the map correctly.

Here is the pertinent code:


d3d_device->CreateDepthStencilSurface(512,512,D3DFMT_D16,D3DMULTISAMPLE_NONE,0,TRUE,&shadow_depth_surface,NULL);
d3d_device->CreateTexture(512,512,1,D3DUSAGE_RENDERTARGET,D3DFMT_A8R8G8B8,D3DPOOL_DEFAULT,&shadow_texture,NULL);
 
D3DXVECTOR3 eye(0.0f, 8.0f, 0.0f);
D3DXVECTOR3 look(8.0f, 0.0f, 8.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
 
D3DXMatrixLookAtLH(&shadow_view_matrix,&eye,&look,&up);
 
D3DXMatrixPerspectiveFovLH(&shadow_projection_matrix, pi/2.0f, 1.0f, 1.0f, 100.0f);
 
D3DXMatrixMultiply(&shadow_world_view_projection,&shadow_view_matrix,&shadow_projection_matrix);

Here is the shader:


VS_OUTPUT vertexShader(VS_INPUT input)
{
 VS_OUTPUT output;

 output.position=mul(input.position,WorldViewProjection);
 output.depth=output.position.zw;

 return output;
}
 
float4 pixelShader(VS_OUTPUT input) : COLOR
{
 float d=input.depth.x/input.depth.y;

 return float4(d,d,d,1);
}

The shadow map seems alright but it's mostly white. Am I using the correct formats when I create the depth surface and texture ? The near clipping value in the projection matrix is very sensitive and makes a huge difference in how the map looks, it's not very satisfying.

I don't think my problem is with the map generation but I want to make sure it's correct before asking about the rest of the algorithm.

Thanks a lot.

Advertisement

Shader looks fine, depth-stencil format is ok-ish, but you might play with higher precision (D24X8 or even D32). I'm concerned about your render target format though. 8 bit is definitively too little. Usually you use a full float (but single channel) format for the depth map, i.e. R32F.

Not sure why it's all white. This can happen if your scene ends up mostly at the far plane - which in turn may indicates that your shadow view-projection is wrong.

As always: Running such things through PIX or similar really helps. One can inspect both depth and render-target after each draw call (which in this case should look practically the same)

I feel like I'm getting closer but it's still not right. I changed my texture creation format to D3DFMT_R32F.

Using the same code above here is the part that renders the scene with the shadow:


struct VS_OUTPUT
{
 float4 position : POSITION;
 float4 lpos : TEXCOORD1;
...
};
 
VS_OUTPUT vertexShader(VS_INPUT input)
{
 VS_OUTPUT output;
 output.position=mul(input.position,WorldViewProjection);
 output.lpos=mul(input.position,LightWorldViewProjection);
 ....
}
 
float4 pixelShader(VS_OUTPUT input) : COLOR
{
 ...
 float I=lighting model result

 input.lpos.xyz/=input.lpos.w;
 
 input.lpos.x=input.lpos.x/2+0.5;
 input.lpos.y=input.lpos.y/-2+0.5;

 float depth=tex2D(Sampler0,input.lpos.xy).r;

 if(depth < input.lpos.z)
  I*=0.1f;

 return float4(color.rgb*I,1);
}

I think my shader is alright and the problem is how I'm setting it all up but I don't really know. I'm also not sure if I should be doing the depth test int the light's eye space or it's clip space. Thanks for helping.

You're welcome. Shader looks fine, yes.

Clip space ? Hmm, not sure if this is possible - or even makes sense. Haven't seen such an approach yet.

I think you either have a wrong setup (read: transformations) or you experience now the "fun" stuff with shadow mapping, i.e. acne or self-shadowing. Screenshots might help.

Yay, I got it to work.

I want to make them softer now, what do you guys recommend for a topdown/isometric view ? The shadows will be pretty dim so they don't need to be perfect, I just don't want any obvious eye sores.

Thanks.

You've already mentioned approaches in your original post. PCF is the next thing to do. Try a 4-tap first, then increase the kernel (and taps). You might also want to play with the shadow map resolution. Also, the 4-tap might be available through special formats by the hardware directly (so-called hardware PCF. Search the forum and for NVidia papers).

Since even whole books are written about this topic, one probably can't tell you what approach is best for your use case wink.png.

Hi, I'm trying to do a simple 3x3 averaging and I can't understand why it doesn't work:

 
pixel shader:
 
//
input.lpos.xyz/=input.lpos.w;
input.lpos.x=(input.lpos.x*0.5f)+0.5f;
input.lpos.y=(-input.lpos.y*0.5f)+0.5f;
 
// the following works ok
 
float depth=tex2D(Sampler0,input.lpos.xy).r;
 
if(depth < input.lpos.z)
 I*=0.5f; // shadowed area is half intensity
 
// but this doesn't
 
float2 offset;
float size=1024.0f; // shadow map size in pixels
float depth=0.0f;
 
for(int x=-1;x<=1;x++){
 for(int y=-1;y<=1;y++){
  offset=float2(x,y)/size;
  depth+=tex2D(Sampler0,input.lpos.xy+offset).r;
 }
}
 
depth=depth/9.0f;

if(depth < input.lpos.z)
 I*=0.5f; // shadowed area is half intensity
 

Hi, I'm trying to do a simple 3x3 averaging and I can't understand why it doesn't work:

 
pixel shader:
 
// the position transformed by the light matrix
input.lpos.xyz/=input.lpos.w;
input.lpos.x=(input.lpos.x*0.5f)+0.5f;
input.lpos.y=(-input.lpos.y*0.5f)+0.5f;
 
// the following works ok
 
float depth=tex2D(Sampler0,input.lpos.xy).r;
 
if(depth < input.lpos.z)
 I*=0.5f; // shadowed area is half intensity
 
// but this doesn't
 
float2 offset;
float size=1024.0f; // shadow map size in pixels
float depth=0.0f;
 
for(int x=-1;x<=1;x++){
 for(int y=-1;y<=1;y++){
  offset=float2(x,y)/size;
  depth+=tex2D(Sampler0,input.lpos.xy+offset).r;
 }
}
 
depth=depth/9.0f;

if(depth < input.lpos.z)
 I*=0.5f; // shadowed area is half intensity
 

I get areas that shouldn't be in shadow shadowed (the parts perpendicular to the light ray) and the shadow edge looks the same, it's not smoothed even slightly, plus a few other problems.

This stuff is complicated.

Thanks.

Even more if I link to a source which makes you skip a step, sorry about that. The GPU Gems article seems to already use hardware PCF. Look closely: Your original shader has a depth comparison, your latest just adds values from the depth map without comparing (which is wrong). You can't use that source if you haven't setup hardware PCF explicitly.

Here's a better example. Not that bare, maybe you wanna look at a the original paper or grab F.Luna's book's source code (chapter 22)

PS: And yes, shadows are a pain.

Hi, I see what you mean about the depth comparison. In any case, I pretty much copied and pasted the Frank Luna code (which seems identical to the DirectXSDK sample) and the shadows seem alright but I'm getting Moire patterns, the code is the same so it's weird.

Also, in his code he does this to clear the buffer before making the shadow map:

gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);

Notice the color is 0, I don't see how that can possibly work, shouldn't it be 0xffffffff ? In fact, that's why my original setup wasn't working, I was clearing it to zero and not one.

I wonder why my code isn't working, it's exactly like his and the DXSDK sample. Here is how it looks (finally a screen):

5pip.jpg

This topic is closed to new replies.

Advertisement