// 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>
    /// A shader performing Lambertian pre-filtering.
    /// </summary>
    internal shader SphericalHarmonicsRenderer<int THarmonicsOrder>: SphericalHarmonicsBase<THarmonicsOrder>, ImageEffectShader, Texturing
    {    
        [Color] stage float3 SHCoefficients[CoefficientsCount];

        // Shading of the sprite
        stage override void PSMain()
        {
            float3 ColorTargets[6];
            for( uint i=0; i<6; ++i)
            {
                var direction = normalize(CubemapUtils.ConvertTexcoordsNoFlip(streams.TexCoord, i)); // remarks: TexCoord points to the center of the pixel (what we want)

                EvaluateSHBases(direction);
                
                ColorTargets[i] = float3(0, 0, 0);
                for(int k=0; k<CoefficientsCount; ++k)
                    ColorTargets[i] += SHCoefficients[k] * streams.SHBaseValues[k];
            }
            
            streams.ColorTarget  = float4(ColorTargets[0], 1);
            streams.ColorTarget1 = float4(ColorTargets[1], 1);
            streams.ColorTarget2 = float4(ColorTargets[2], 1);
            streams.ColorTarget3 = float4(ColorTargets[3], 1);
            streams.ColorTarget4 = float4(ColorTargets[4], 1);
            streams.ColorTarget5 = float4(ColorTargets[5], 1);
        }
    };
}
