/* attempt to deal with prototype, bootstrap, jquery conflicts */ /* for dropdown menus */

\$22

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

Posted 27 July 2013 - 09:58 AM

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

D3DXVECTOR3 eye(0.0f, 8.0f, 0.0f);
D3DXVECTOR3 look(8.0f, 0.0f, 8.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);



VS_OUTPUT vertexShader(VS_INPUT input)
{
VS_OUTPUT output;

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

return output;
}

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

Edited by Endemoniada, 27 July 2013 - 10:00 AM.

### #2unbird  Members

Posted 27 July 2013 - 10:23 AM

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)

Posted 27 July 2013 - 03:59 PM

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 output;
output.position=mul(input.position,WorldViewProjection);
output.lpos=mul(input.position,LightWorldViewProjection);
....
}

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

Edited by Endemoniada, 27 July 2013 - 04:01 PM.

### #4unbird  Members

Posted 27 July 2013 - 05:09 PM

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.

Posted 27 July 2013 - 09:22 PM

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.

### #6unbird  Members

Posted 27 July 2013 - 11:48 PM

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 .

Edited by unbird, 27 July 2013 - 11:49 PM.

Posted 28 July 2013 - 02:00 PM

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



//
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



Posted 28 July 2013 - 02:05 PM

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



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

### #9unbird  Members

Posted 28 July 2013 - 02:46 PM

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.

Edited by unbird, 28 July 2013 - 02:48 PM.

Posted 28 July 2013 - 06:20 PM

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):

Edited by Endemoniada, 28 July 2013 - 06:27 PM.

### #11unbird  Members

Posted 29 July 2013 - 01:42 AM

Looks like self-shadowing (though weird you don't get the same effect on the floor). Use a higher bias for the depth comparison (SHADOW_EPSILON in Luna's code) or only render the backfaces to your shadow map by flipping the cull mode.

To high an epsilon will result in so-called peter-panning (objects look like they hover above ground). It's hard to get straight forward shadow mapping right - if possible at all.

Edit:

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.

You're right, it should be. Depends, though. The SDK sample e.g. is "indoors" so I guess every pixel gets drawn anyway (you wouldn't even need a target clear).

Edited by unbird, 29 July 2013 - 08:32 AM.

Posted 29 July 2013 - 01:31 PM

Thanks a lot unbird. I realized SSM has lots of inherent problems so I think it's time to switch over to variance or more probably exponential shadow mapping. Exponential seems like the way to go but it's hard to find much about it. I'll probabky start a new post once I get something up and running. I'm learning a lot and that's a good thing

Thanks again.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.