// 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>
    /// The U2Filmic tonemap operator.
    /// </summary>
    internal shader ToneMapU2FilmicOperatorShader : ToneMapOperatorShader
    {
        float ShoulderStrength = 0.22f;
        float LinearStrength = 0.25f;
        float LinearAngle = 0.1f;
        float ToeStrength = 0.2f;
        float ToeNumerator = 0.01f;
        float ToeDenominator = 0.3f;
        float LinearWhite = 11.2f;

        // Function used by the Uncharted2 tone mapping curve
        float3 U2Func(float3 x)
        {
	        float A = ShoulderStrength;
	        float B = LinearStrength;
	        float C = LinearAngle;
	        float D = ToeStrength;
	        float E = ToeNumerator;
	        float F = ToeDenominator;
	        return ((x*(A*x+C*B)+D*E)/(x*(A*x+B)+D*F)) - E/F;
        }

        override float4 Compute(float4 color)
        {
            // Applies the Uncharted 2 filmic tone mapping curve
	        float3 numerator = U2Func(color.rgb);        
	        float3 denominator = U2Func(LinearWhite);

	        return float4(numerator / denominator, 1);
        }
    };
}
