// 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>
    /// Combines the 3 rhombi blurs into an hexagonal blur. (Final pass of the TripleRhombi bokeh effect.)
    /// Expects as input: 
    ///  - Texture0: a color buffer with the top-left rhombi blur
    ///  - Texture1: a color buffer with the top-right rhombi blur
    ///  - Texture2: a color buffer with the bottom rhombi blur
    /// </summary>
    shader TripleRhombiCombineShader : ImageEffectShader
    {
        // Offset to apply when reading a texture coordinate (for each of the 3 rhombis)
        stage float2 RhombiTapOffsets[3];

        stage override float4 Shading()
        {
            float4 tapColor[3]; 

            tapColor[0] = Texture0.Sample( LinearSampler, streams.TexCoord + RhombiTapOffsets[0] * Texture0TexelSize );
            tapColor[1] = Texture1.Sample( LinearSampler, streams.TexCoord + RhombiTapOffsets[1] * Texture1TexelSize );
            tapColor[2] = Texture2.Sample( LinearSampler, streams.TexCoord + RhombiTapOffsets[2] * Texture2TexelSize );

            float4 result = float4(0.0, 0.0, 0.0, 0.0);
            [unroll]
            for (int i = 0; i < 3; i++) 
            {
                float4 color = tapColor[i];
                color.rgb *= color.a; //Pre-multiply alpha
                result += color;
            }

            result /= 3.0;

            if (result.a > 0) result.rgb /= result.a; // Converts back to non-pre-multiplied alpha

            return result;
        }
    };
}
