// 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.Lights
{
    /// <summary>
    /// Defines a group of spot lights
    /// </summary>
    shader LightSpotGroup<int TMaxLightCount> :
        DirectLightGroupPerDraw,    // Required for "PrepareDirectLightCore()", "PrepareDirectLight()", "ComputeAttenuation()" and other stuff.
        LightSpot,  // Required for "SpotLightData".
        LightSpotAttenuationDefault    // Required for "ComputeAttenuation()"
    {
        cbuffer PerDraw.Lighting
        {
            SpotLightData Lights[TMaxLightCount];
        }

        override int GetMaxLightCount()
        {
            return TMaxLightCount;
        }

        /// <summary>
        /// Compute the light color/direction for the specified index within this group
        /// </summary>
        override void PrepareDirectLightCore(int lightIndex)
        {
            // TODO: Workaraound for SPIR-V compiler. Revert later
            SpotLightDataInternal data;
            data.PositionWS = Lights[lightIndex].PositionWS;
            data.DirectionWS = Lights[lightIndex].DirectionWS;
            data.AngleOffsetAndInvSquareRadius = Lights[lightIndex].AngleOffsetAndInvSquareRadius;
            data.Color = Lights[lightIndex].Color;

            ProcessLight(data);
        }

        override float ComputeAttenuation(float3 position, int lightIndex)
        {
            // TODO: Workaraound for SPIR-V compiler. Revert later
            SpotLightDataInternal data;
            data.PositionWS = Lights[lightIndex].PositionWS;
            data.DirectionWS = Lights[lightIndex].DirectionWS;
            data.AngleOffsetAndInvSquareRadius = Lights[lightIndex].AngleOffsetAndInvSquareRadius;

            float3 lightVectorNorm;
            //return ComputeAttenuation(data, position, lightVectorNorm);
            return ComputeAttenuation(data.PositionWS,  // TODO: Revert to the above line as soon as the shader compiler is fixed.
                                      data.AngleOffsetAndInvSquareRadius,
                                      data.DirectionWS,
                                      position, lightVectorNorm);
        }
    };
}
