// 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
{
    shader LightShaftsShader<int SampleCount> : ImageEffectShader, PostEffectBoundingRay<SampleCount>, LightStream, NormalStream
    {
        stage compose DirectLightGroup lightGroup;

        cbuffer PerFrame
        {
            stage float DensityFactor;
        };

        override float3 ComputeColorIn(float4 positionWS, float stepSize, int stepIndex)
        {
            // Most shadow groups use these for normal scaled bias
            ResetLightStream();
            streams.NdotL = 1;
            streams.normalWS = float3(0,1,0);
            // Needed by thickness computation (TODO: need a way to disable ComputeTransmittance when computing light shafts)
            streams.meshNormalWS = 0.0f;
            streams.PositionWS = 0.0f;

            float atten = lightGroup.ComputeAttenuation(positionWS.xyz, 0);
            float3 shadowColor = lightGroup.ComputeShadow(positionWS.xyz, 0);

            // Right now this doesn't support multi-colored shadows, since this shader only calculates the light shaft intensity, which is later multiplied by the light color
            // So take the max here
            float shadow = max(max(shadowColor.x, shadowColor.y), shadowColor.z);

            return DensityFactor * stepSize * shadow * atten;
        }
    };
}
