# Self-shadowing on curved shapes problem

This topic is 1950 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello

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.

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

##### Share on other sites
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).

##### Share on other sites
Thank you very much it seems to be exactly what I need !

##### Share on other sites
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 :
[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 Edited by Tournicoti

##### Share on other sites

Thanks, it solved the problem

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

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

##### Share on other sites

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

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.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.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; } 

[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 (remove acne on flat surfaces)
[font=courier new,courier,monospace]g_CSM_depths[split].z[/font] is the maximal depth bias of the #split map (remove acne on bumped surfaces)

Thanks for reading Edited by Tournicoti

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

##### Share on other sites

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 [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 near 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]
[/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.
[/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] factor=(g_CSMMaps.Sample(shadowMapSampler,float3(uv,split)).x+factor<posLightZ) ? 0.0f : 1.0f; [/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 on other sites
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

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