/*
 * 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>

#define NumShadingRates 7
ShaderResourceGroup InstanceSrg : SRG_PerObject
{
    Texture2D<uint2> m_texture;
    struct PatternColor
    {
        float4 m_color;
        uint4 m_rate;
    };
    PatternColor m_colors[NumShadingRates];
}

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_uv = vsInput.m_uv;
    OUT.m_uv.y = 1.0f - OUT.m_uv.y;
    OUT.m_position = float4(vsInput.m_position.x, vsInput.m_position.y, vsInput.m_position.z, 1);
    return OUT;
}

struct PSOutput
{
    float4 m_color : SV_Target0;
};

PSOutput MainPS(VSOutput vsOutput)
{
    uint2 dimensions;
    InstanceSrg::m_texture.GetDimensions(dimensions.x, dimensions.y);
    uint2 rate = InstanceSrg::m_texture.Load(int3(vsOutput.m_uv.x * (dimensions.x - 1), vsOutput.m_uv.y * (dimensions.y - 1), 0));

    PSOutput OUT;
    float4 color = float4(0, 0, 0, 1);
    for (int i = 0; i < NumShadingRates; ++i)
    {
        InstanceSrg::PatternColor pattern = InstanceSrg::m_colors[i];
        if (pattern.m_rate.x == rate.x && pattern.m_rate.y == rate.y)
        {
            color = pattern.m_color;
        }
    }
    OUT.m_color = color;
    return OUT;
}
