18 hours ago, JoeJ said:
Hey, you could apply the temporal filter just to the shadow map itself! E.g. shadowMapTexel = shadowMapTexel * 0.95 + newValue * 0.05.
Should I add this code to the pixel shader in the first pass, render to shadow map?
This is the code I use in the shader for rendering the shadow map texture.
VS_OUTPUT VSMain( VS_INPUT Input )
{
VS_OUTPUT Output;
Output.vPosition = mul( Input.vPosition, g_mWorldViewProjection );
return Output;
}
float2 PSMain (VS_OUTPUT Input) : SV_TARGET
{
float2 rt;
rt.x = Input.vPosition.z;
rt.y = rt.x * rt.x;
return rt;
}
This is also the code I use to calculate the percent lit in the scene rendering in the variance shadow mapping.
void CalculateVarianceShadow ( in float4 vShadowTexCoord, in float4 vShadowMapTextureCoordViewSpace, int iCascade, out float fPercentLit , in float depth)
{
fPercentLit = 0.0f;
// This loop could be unrolled, and texture immediate offsets could be used if the kernel size were fixed.
// This would be a performance improvment.
float2 mapDepth = 0;
// In orderto pull the derivative out of divergent flow control we calculate the
// derivative off of the view space coordinates an then scale the deriviative.
float3 vShadowTexCoordDDX =
ddx(vShadowMapTextureCoordViewSpace );
//float3 vShadowTexCoordDDX = vShadowMapTextureCoordViewSpace;
vShadowTexCoordDDX *= m_vCascadeScale[iCascade].xyz;
float3 vShadowTexCoordDDY =
ddy(vShadowMapTextureCoordViewSpace );
//float3 vShadowTexCoordDDY = vShadowMapTextureCoordViewSpace;
vShadowTexCoordDDY *= m_vCascadeScale[iCascade].xyz;
mapDepth += g_txShadow.SampleGrad( g_samShadow, vShadowTexCoord.xyz,
vShadowTexCoordDDX,
vShadowTexCoordDDY);
//mapDepth += g_txShadow.Sample(g_samShadow, vShadowTexCoord.xyz);
// The sample instruction uses gradients for some filters.
float fAvgZ = mapDepth.x; // Filtered z
float fAvgZ2 = mapDepth.y; // Filtered z-squared
if ( vShadowTexCoord.w <= fAvgZ ) // We put the z value in w so that we can index the texture array with Z.
{
fPercentLit = 1;
}
else
{
float variance = ( fAvgZ2 ) - ( fAvgZ * fAvgZ );
//variance = min( 1.0f, max( 0.0f, variance + 0.00001f ) );
variance = min(1.0f, max(0.0f, variance + 0.00001f));
float mean = fAvgZ;
float d = vShadowTexCoord.w - mean; // We put the z value in w so that we can index the texture array with Z.
float p_max = variance / ( variance + d*d );
// To combat light-bleeding, experiment with raising p_max to some power
// (Try values from 0.1 to 100.0, if you like.)
fPercentLit = pow(p_max, 2);
}