/*
 * Copyright (c) Contributors to the Open 3D Engine Project.
 * For complete copyright and license terms please see the LICENSE at the root of this distribution.
 *
 * SPDX-License-Identifier: Apache-2.0 OR MIT
 *
 */

#include <Atom/Features/SrgSemantics.azsli>

float CalcLuminance(float3 color)
{
    return dot(color, float3(0.299f, 0.587f, 0.114f));
}

ShaderResourceGroup TextureInstanceSrg : SRG_PerObject
{
    Texture2D m_texture;

    Sampler m_sampler
    {
        MaxAnisotropy = 16;
        AddressU = Wrap;
        AddressV = Wrap;
        AddressW = Wrap;
    };
}

struct VSInput 
{
    float3 m_position : POSITION;
    float2 m_uv : UV0;
};

struct VSOutput
{
    float4 m_position : SV_Position;
    float2 m_uv : UV0;
};


VSOutput MainVS(VSInput vsInput)
{
    VSOutput OUT;
    OUT.m_position = float4(vsInput.m_position, 1.0);
    OUT.m_uv = vsInput.m_uv;
    return OUT;
}

struct PSOutput
{
    float4 m_color : SV_Target0;
};

PSOutput MainPS(VSOutput IN)
{
    PSOutput OUT;
    float3 color = TextureInstanceSrg::m_texture.Sample(TextureInstanceSrg::m_sampler, IN.m_uv).rgb;
    float luminance = log(max(CalcLuminance(color), 0.00001f));
    OUT.m_color = float4(luminance, 1.0f, 1.0f, 1.0f);
    return OUT;
};
