•      Sign In
• Create Account

# Self-shadowing on curved shapes problem

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.

9 replies to this topic

### #1Tournicoti  Prime Members   -  Reputation: 699

Like
1Likes
Like

Posted 19 September 2012 - 03:52 AM

Hello

I have a problem of self-shadowing that I can't solve by changing the depth bias :

I use (colored) cascaded shadow maps, each map has its own (constant) depth bias.

I wonder if there's a way to use partial derivatives (ddx ddy) to adjust the depth bias for each pixel ?

Thank you for any suggestion, or your help

Nico

### #2larspensjo  Members   -  Reputation: 1561

Like
2Likes
Like

Posted 19 September 2012 - 04:26 AM

Use an adaptive depth bias that depends on the normal compared to the light. It is explained in the www.opengl-tutorial.org.

In principle, you set a bias to (as copied from this tutorial):
0.005*tan(acos(cosTheta)), where cosTheta is the clamped dot product with the normal.

I do something similar, but found that the equation can be simplified and still get a similar effect. I don't have the source available now, but I think I use something like 0.005/cosTheta (which check for 0 vaues).
Current project: Ephenation.
Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

### #3Tournicoti  Prime Members   -  Reputation: 699

Like
0Likes
Like

Posted 19 September 2012 - 04:40 AM

Thank you very much it seems to be exactly what I need !

### #4CryZe  Members   -  Reputation: 768

Like
5Likes
Like

Posted 19 September 2012 - 05:17 AM

It's faster if implemented this way though:
and are expensive instructions, that's why you should avoid them as often as possible.

Edited by CryZe, 19 September 2012 - 05:26 AM.

### #5Tournicoti  Prime Members   -  Reputation: 699

Like
2Likes
Like

Posted 20 September 2012 - 05:53 AM

Thanks, it solved the problem

If you want to have a look on my HLSL colored csm sampling functions that use it :

#define CSM_MAXSPLITS 8

SamplerState shadowMapSampler
{
Filter = MIN_MAG_MIP_POINT;
AddressU = BORDER;
AddressV = BORDER;
BorderCOLOR = float4(1.0f,1.0f,1.0f,1.0f);
};

bool getSplitUV(in float posProjZ,in float3 posWorld,out uint split,out float2 uv,out float posLightZ)
{
split=1;
uv=0;
posLightZ=0;
[unroll (CSM_MAXSPLITS)]
for (;split<=g_CSM_nbSplits;split++)
if (posProjZ<g_CSM_depths[split].x) break;

split--;
if (split==g_CSM_nbSplits) return false;

float4 posLight=mul(float4(posWorld,1.0f),g_CSM_VP[split]);
posLight/=posLight.w;
posLightZ=posLight.z;

uv=(posLight.xy)*float2(0.5f,-0.5f)+0.5f;

return true;
}

float3 sampleColorCSM(in float posProjZ,in float3 posWorld,in float3 normalWorld)
{
uint split;
float2 uv;
float posLightZ,factor;
if (getSplitUV(posProjZ,posWorld,split,uv,posLightZ))
{
factor=saturate(dot(normalWorld,g_vDirectionalLightDirection));
factor=saturate(sqrt(1.0f-factor*factor)/factor)*g_CSM_depths[split].y;
factor=(g_CSMMaps.Sample(shadowMapSampler,float3(uv,split)).x+factor<posLightZ) ? 0.0f : 1.0f;
}
else
factor=1.0f;
return factor*g_ColorCSMMaps.Sample(shadowMapSampler,float3(uv,split)).xyz;
}


PS :
g_CSM_depths[split].x is the minimal depth of the #split map, g_CSM_depths[split+1].x is the maximal depth of the #split map.
g_CSM_depths[split].y is the depth bias of the #split map
Any suggestion or improvement is welcome

Edited by Tournicoti, 20 September 2012 - 06:01 AM.

### #6CC Ricers  Members   -  Reputation: 1487

Like
2Likes
Like

Posted 20 September 2012 - 09:23 PM

Thanks, it solved the problem

If you want to have a look on my HLSL colored csm sampling functions that use it :
....

PS :
g_CSM_depths[split].x is the minimal depth of the #split map, g_CSM_depths[split+1].x is the maximal depth of the #split map.
g_CSM_depths[split].y is the depth bias of the #split map
Any suggestion or improvement is welcome

Instead of using if statements while looping through split indices get the correct split index by adding up conditional statements for all the split tests. This does away with the need for branching.

For instance, here is what I use for my single-pass CSM function:

cascadeDistances[NUM_CASCADES];

// In the pixel shader
splitIndex = 0;
for (int i = 0; i < NUM_CASCADES; i++)
splitIndex += (linearZ > cascadeDistances[i]);

// Get shadow map position projected in light view
float4 shadowMapPos = mul(position, lightViewProj[splitIndex]);



Here, linear Z depth is a value between the near and far world unit distances of the entire view frustum, not the splits. The cascadeDistances array is the list of far split planes in world units, listed from smallest to greatest. If linearZ > cascadeDistances[1], it also follows that linearZ > cascadeDistances[0] is true, therefore splitIndex is 2.

SplitIndex will always be less than the number of cascades, because cascadeDistances[n] is the farthest depth in the view, which linearZ will always be less than.

Edited by CC Ricers, 20 September 2012 - 09:24 PM.

New game in progress: Project SeedWorld

My development blog: Electronic Meteor

### #7Tournicoti  Prime Members   -  Reputation: 699

Like
1Likes
Like

Posted 20 September 2012 - 10:24 PM

Instead of using if statements while looping through split indices get the correct split index by adding up conditional statements for all the split tests. This does away with the need for branching.
...

thanks

So here's my last version [EDITED] :

#define CSM_MAXSPLITS 8

SamplerState shadowMapSampler
{
Filter = MIN_MAG_MIP_POINT;
AddressU = BORDER;
AddressV = BORDER;
BorderCOLOR = float4(1.0f,1.0f,1.0f,1.0f);
};

float3 sampleColorCSM(in float posProjZ,in float3 posWorld,in float3 normalWorld)
{
float3 uv;
float bias;
uint split=0;
float4 posLight;

if (posProjZ<g_CSM_depths[0].x || posProjZ>g_CSM_depths[g_CSM_nbSplits].x) return float3(1.0,1.0f,1.0f);

[unroll (CSM_MAXSPLITS)]
for (uint i=1;i<=g_CSM_nbSplits;i++)
split+=posProjZ>g_CSM_depths[i].x;

posLight=mul(float4(posWorld,1.0f),g_CSM_VP[split]);
posLight/=posLight.w;

bias=dot(normalWorld,-g_vDirectionalLightDirection);
bias=clamp(g_CSM_depths[split].y*sqrt(1.0f-bias*bias)/bias,g_CSM_depths[split].y,g_CSM_depths[split].z);

uv=float3(posLight.xy*float2(0.5f,-0.5f)+0.5f,split);

return (g_CSMMaps.Sample(shadowMapSampler,uv).x+bias>posLight.z)*g_ColorCSMMaps.Sample(shadowMapSampler,uv).xyz;
}

float sampleCSM(in float posProjZ,in float3 posWorld,in float3 normalWorld)
{
float3 uv;
float bias;
uint split=0;
float4 posLight;

if (posProjZ<g_CSM_depths[0].x || posProjZ>g_CSM_depths[g_CSM_nbSplits].x) return 1.0f;

[unroll (CSM_MAXSPLITS)]
for (uint i=1;i<=g_CSM_nbSplits;i++)
split+=posProjZ>g_CSM_depths[i].x;

posLight=mul(float4(posWorld,1.0f),g_CSM_VP[split]);
posLight/=posLight.w;

bias=dot(normalWorld,-g_vDirectionalLightDirection);
bias=clamp(g_CSM_depths[split].y*sqrt(1.0f-bias*bias)/bias,g_CSM_depths[split].y,g_CSM_depths[split].z);

uv=float3(posLight.xy*float2(0.5f,-0.5f)+0.5f,split);

return g_CSMMaps.Sample(shadowMapSampler,uv).x+bias>posLight.z;
}


g_CSM_depths[split].x is the minimal depth of the #split map, g_CSM_depths[split+1].x is the maximal depth of the #split map.
g_CSM_depths[split].y is the minimal depth bias of the #split map (remove acne on flat surfaces)
g_CSM_depths[split].z is the maximal depth bias of the #split map (remove acne on bumped surfaces)

Thanks for reading

Edited by Tournicoti, 24 September 2012 - 07:23 AM.

### #8CC Ricers  Members   -  Reputation: 1487

Like
2Likes
Like

Posted 21 September 2012 - 11:36 AM

Well the way I see it, you should always be sampling from a shadow map because as long as you transform your view-projection matrices correctly, all visible pixels will be overlapped with it.

The splitIndex is determined by the distance of the pixel in the depth map, which then is used to choose what cascade to sample from and what transformation matrix to use for the cascade. No if statements needed for that, just begin with splitIndex at 0 and increase it (if necessary) through comparison statements.

New game in progress: Project SeedWorld

My development blog: Electronic Meteor

### #9Tournicoti  Prime Members   -  Reputation: 699

Like
0Likes
Like

Posted 21 September 2012 - 02:11 PM

Thanks for your help

The splitIndex is determined by the distance of the pixel in the depth map, which then is used to choose what cascade to sample from and what transformation matrix to use for the cascade. No if statements needed for that, just begin with splitIndex at 0 and increase it (if necessary) through comparison statements.

I think that's what I've done in getSplitUV() ?
Except I start with index #1 because index #0 is the near depth of the first split.
(btw theses depthes are expressed in clip space)

Well the way I see it, you should always be sampling from a shadow map because as long as you transform your view-projection matrices correctly, all visible pixels will be overlapped with it.

Yes, I always sample the shadow map when possible :
factor=(g_CSMMaps.Sample(shadowMapSampler,float3(uv,split)).x+factor<posLightZ) ? 0.0f : 1.0f;


.... but it is also possible that a visible pixel is out of the shadow map, if its depth is greater than the maximal depth of the last split. In this case, the pixel is fully lighted ( color filter (1,1,1) )

My point is that sampling the color map can be avoided if shadowed, because the returned color filter is always (0,0,0)

Edited by Tournicoti, 22 September 2012 - 07:59 AM.

### #10Tournicoti  Prime Members   -  Reputation: 699

Like
1Likes
Like

Posted 22 September 2012 - 07:39 AM

Maybe should I be more specific about how it works ?

There are 2 maps of the same dimensions :
• a depth map, like in standard shadow mapping
• a color map, that stores the filtering colors
Generation of the shadow map :
• the color map is filled with (1,1,1)
• the opaque geometry is rendered on the depth map only
• the transparent geometry is alpha-blended on the color map, reading the depth map.
Scene rendering with colored shadows :

The point is shadowed (depth test) ?
• Yes, return (0,0,0)
• No, sampling of the color map.
This filter is then multiplied by the light color, to get the filtered light color that can be used in lighting calculations afterwards

NB :
Since this shadow map stores the depth and the color filter of a light ray reaching an opaque geometry, it can be used on opaque geometry only.
I still use standard shadow mapping on transparent geometry, just taking into account the depth data of this shadow map.

Thank you so much for the help I got

Edited by Tournicoti, 23 September 2012 - 01:35 PM.

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.

PARTNERS