// 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 point lights in clustered shading.
    /// </summary>
    shader LightClusteredPointGroup : DirectLightGroup, LightClustered, LightPoint
    {
        rgroup PerView.Lighting
        {
            stage Buffer<float4> PointLights;
        }

        override void PrepareDirectLights()
        {
            PrepareLightData();
        }

        override int GetMaxLightCount()
        {
            return streams.lightData.y & 0xFFFF;
        }

        override int GetLightCount()
        {
            return streams.lightData.y & 0xFFFF;
        }

        /// <summary>
        /// Compute the light color/direction for the specified index within this group
        /// </summary>
        override void PrepareDirectLightCore(int lightIndexIgnored)
        {
            // What we had so far was just a loop index
            // Note: we have lightIndex as a parameter but we ignore it since we want to preserve it between point and spot lights
            int realLightIndex = LightIndices.Load(streams.lightIndex);
            streams.lightIndex++;

            // Build PointLightData
            PointLightDataInternal pointLight;
            float4 pointLight1 = PointLights.Load(realLightIndex * 2);
            float4 pointLight2 = PointLights.Load(realLightIndex * 2 + 1);
            pointLight.PositionWS = pointLight1.xyz;
            pointLight.InvSquareRadius = pointLight1.w;
            pointLight.Color = pointLight2.xyz;

            // Perform lighting
            ProcessLight(pointLight);
        }
    };
}
