Sign in to follow this  
Adaline

Self-shadowing on curved shapes problem

Recommended Posts

Hello [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

I have a problem of self-shadowing that I can't solve by changing the depth bias :
[attachment=11351:sm.PNG]

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

[url="http://msdn.microsoft.com/en-us/library/windows/desktop/ee416307(v=vs.85).aspx"]I wonder if there's a way to use partial derivatives (ddx ddy) to adjust the depth bias for each pixel ?[/url]

Thank you for any suggestion, or your help [img]http://public.gamedev.net//public/style_emoticons/default/rolleyes.gif[/img]

Nico

Share this post


Link to post
Share on other sites
Use an adaptive depth bias that depends on the normal compared to the light. It is explained in the [url="http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-16-shadow-mapping/#header-8"]www.opengl-tutorial.org[/url].

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

Share this post


Link to post
Share on other sites
Thanks, it solved the problem [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

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

[CODE]
#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;
}
[/CODE]

PS :
[font=courier new,courier,monospace]g_CSM_depths[split].x[/font] is the minimal depth of the [font=courier new,courier,monospace]#split[/font] map, [font=courier new,courier,monospace]g_CSM_depths[split+1].x[/font] is the maximal depth of the [font=courier new,courier,monospace]#split[/font] map.
[font=courier new,courier,monospace]g_CSM_depths[split].y[/font] is the depth bias of the [font=courier new,courier,monospace]#split[/font] map
Any suggestion or improvement is welcome [img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img] Edited by Tournicoti

Share this post


Link to post
Share on other sites
[quote name='Tournicoti' timestamp='1348142037' post='4981993']
Thanks, it solved the problem [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

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

PS :
[font=courier new,courier,monospace]g_CSM_depths[split].x[/font] is the minimal depth of the [font=courier new,courier,monospace]#split[/font] map, [font=courier new,courier,monospace]g_CSM_depths[split+1].x[/font] is the maximal depth of the [font=courier new,courier,monospace]#split[/font] map.
[font=courier new,courier,monospace]g_CSM_depths[split].y[/font] is the depth bias of the [font=courier new,courier,monospace]#split[/font] map
Any suggestion or improvement is welcome [img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img]
[/quote]

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:

[code]
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]);

[/code]

Here, linear Z depth is a value between the near and far world unit distances of the [i]entire[/i] 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

Share this post


Link to post
Share on other sites
[quote]
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.
...
[/quote]

thanks [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]


So here's my last version [EDITED] :

[CODE]
#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;
}
[/CODE]

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

[i]Thanks for reading [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img][/i] Edited by Tournicoti

Share this post


Link to post
Share on other sites
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 [i]splitIndex[/i] 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 [i]splitIndex[/i] at 0 and increase it (if necessary) through comparison statements.

Share this post


Link to post
Share on other sites
Thanks for your help


[quote name='CC Ricers' timestamp='1348249009' post='4982443']
The [i]splitIndex[/i] 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 [i]splitIndex[/i] at 0 and increase it (if necessary) through comparison statements.
[/quote]

I think that's what I've done in [font=courier new,courier,monospace]getSplitUV() [/font][font=arial, helvetica, sans-serif]?[/font]
[font=arial, helvetica, sans-serif]Except I start with [font=courier new,courier,monospace]index #1[/font] because [font=courier new,courier,monospace]index #0 [font=arial,helvetica,sans-serif]is the [i]near[/i] depth of the first split.[/font][/font][/font]
[font=arial, helvetica, sans-serif][font=courier new,courier,monospace][font=arial,helvetica,sans-serif](btw theses depthes are expressed in clip space)[/font][/font][/font]

[font=arial, helvetica, sans-serif][quote name='CC Ricers' timestamp='1348249009' post='4982443'][/font]
[font=arial, helvetica, sans-serif][font=courier new,courier,monospace][font=arial,helvetica,sans-serif]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.
[/quote][/font][/font][/font]
[font=arial, helvetica, sans-serif][font=courier new,courier,monospace][font=arial,helvetica,sans-serif]Yes, I always sample the shadow map when possible :[/font][/font][/font]
[font=arial, helvetica, sans-serif][font=courier new,courier,monospace][font=arial,helvetica,sans-serif][CODE]
factor=(g_CSMMaps.Sample(shadowMapSampler,float3(uv,split)).x+factor<posLightZ) ? 0.0f : 1.0f;
[/CODE][/font][/font][/font]
.... 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) )

[font=arial, helvetica, sans-serif][font=courier new,courier,monospace][font=arial,helvetica,sans-serif]My point is that sampling the color map can be avoided if shadowed, because the returned color filter is always (0,0,0)[/font][/font][/font] Edited by Tournicoti

Share this post


Link to post
Share on other sites
Maybe should I be more specific about how it works ?

There are 2 maps of the same dimensions :[list]
[*]a depth map, like in standard shadow mapping
[*]a color map, that stores the filtering colors
[/list]
[u]Generation of the shadow map :[/u][list]
[*]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.
[/list]
[u]Scene rendering with colored shadows :[/u]

[i]The point is shadowed (depth test) ?[/i][list]
[*]Yes, return (0,0,0)
[*]No, sampling of the color map.
[/list]
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 [i]opaque geometry[/i], it can be used on [i]opaque geometry only.[/i]
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 [img]http://public.gamedev.net//public/style_emoticons/default/rolleyes.gif[/img]


[attachment=11421:alwaysbetterthanblabla.PNG] Edited by Tournicoti

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