/*
 * 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 ComputeSrg : SRG_PerObject
{
    float2 m_center;
    RWTexture2D<uint2> m_shadingRateTexture;
    struct Pattern
    {
        float4 m_distance;
        uint4 m_rate;
    };

    Pattern m_pattern[NumShadingRates];
};

#define ThreadGroupSize 16
[numthreads(ThreadGroupSize, ThreadGroupSize, 1)]
void MainCS(uint3 groupID : SV_GroupID, uint3 groupThreadID : SV_GroupThreadID)
{
    uint2 samplePos = groupID.xy * ThreadGroupSize + groupThreadID.xy;
    uint2 dimensions;
    ComputeSrg::m_shadingRateTexture.GetDimensions(dimensions.x, dimensions.y);
    if (samplePos.x >= dimensions.x || samplePos.y >= dimensions.y)
    {
        return;
    }

    // Set a default value
    ComputeSrg::m_shadingRateTexture[samplePos] = uint2(ComputeSrg::m_pattern[NumShadingRates - 1].m_rate.x, ComputeSrg::m_pattern[NumShadingRates - 1].m_rate.y);
    // Create a circular pattern from the "center" with decreasing shading rates.
    for (int i = 0; i < NumShadingRates; ++i)
    {
        float deltaX = ((float)ComputeSrg::m_center.x - (float)samplePos.x) / dimensions.x * 100.0f;
        float deltaY = ((float)ComputeSrg::m_center.y - (float)samplePos.y) / dimensions.y * 100.0f;
        float distance = sqrt(deltaX * deltaX + deltaY * deltaY);
        if (distance < ComputeSrg::m_pattern[i].m_distance.x)
        {
            ComputeSrg::m_shadingRateTexture[samplePos] = uint2(ComputeSrg::m_pattern[i].m_rate.x, ComputeSrg::m_pattern[i].m_rate.y);
            break;
        }
    }
} 