// 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.Images
{
    /// <summary>
    /// Computes the Circle of Confusion map.
    /// </summary>
    shader CircleOfConfusion
    {
        // TODO Might want to replace this with a real formula from camera lens parameters, 
        // but for now we'll keep these simple parameters for easy debugging. 
        // [Near Start, Near End, Far Start, Far End]
        stage float4 depthAreas;

        //Gets the circle of confusion strength for a certain depth. 
        float getCoCFactor(float linearDepth) 
        {
            //CoC factor for the front area
            float nearLength = max(depthAreas.y - depthAreas.x, 0.01f);
            float nearCoC = 1.0 - saturate( (linearDepth - depthAreas.x) / nearLength);

            //CoC factor for the back area
            float farLength = max(depthAreas.w - depthAreas.z, 0.01f);
            float farCoC = saturate( (linearDepth - depthAreas.z) / farLength);

            float result = saturate(nearCoC + farCoC);

            // We need to be able to distinguish the out-of-focus near area from the far area. 
            if (linearDepth < depthAreas.y) result = -result;

            return result;
        }
    };
}
