// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
namespace Stride.Rendering.Shadows
{
    /// <summary>
    /// Performs variance filtering.
    /// </summary>
    shader ShadowMapFilterVsm<MemberName PerLighting> : ShadowMapFilterBase<PerLighting>
    {
        cbuffer PerLighting
        {
            float BleedingFactor;
            float MinVariance;
        };

        float FilterShadow(float2 position, float shadowMapDistance)
        {
            float2 moments = (float2)ShadowMapTexture.SampleLevel(LinearBorderSampler, position, 0.0);
            float variance = moments.y - moments.x * moments.x;
            // Clamp variance to min
            variance = max(variance, MinVariance);
            float dist = moments.x - shadowMapDistance;
            float pMax = variance / (variance + dist * dist);
            // Light bleeding reduction (See http://http.developer.nvidia.com/GPUGems3/gpugems3_ch08.html  Light Bleeding 8.4.3)
            pMax = saturate((pMax - BleedingFactor) / (1.0 - BleedingFactor));
            float p = shadowMapDistance <= moments.x;
            return max(p, pMax);        
        }
    };
}
