/*
 * 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 <viewsrg.srgi>

#include "SubpassInputModelSrg.azsli"

struct VSInput
{
    float3 m_position : POSITION;
    float3 m_normal : NORMAL;
};

struct VSOutput
{
    float4 m_position : SV_Position;
    float4 m_worldPos : POSITION;
    float3 m_normal : NORMAL;    
};

VSOutput MainVS(VSInput vsInput)
{
    VSOutput OUT;
    OUT.m_worldPos = mul(SubpassInputModelSrg::m_modelMatrix, float4(vsInput.m_position, 1.0));
    OUT.m_position = mul(ViewSrg::m_viewProjectionMatrix, OUT.m_worldPos);
    OUT.m_normal = vsInput.m_normal;
    return OUT;
}

struct PSOutput
{
    float4 m_position : SV_Target0;
    float4 m_normal : SV_Target1;
    float4 m_albedo : SV_Target2;
    float4 m_outScene : SV_Target3;
};

float linearDepth(float depth)
{
    const float NEAR_PLANE = 0.1f;
    const float FAR_PLANE = 256.0f;

    float z = depth * 2.0f - 1.0f; 
    return (2.0f * NEAR_PLANE * FAR_PLANE) / (FAR_PLANE + NEAR_PLANE - z * (FAR_PLANE - NEAR_PLANE));   
}

PSOutput MainPS(VSOutput vsOutput)
{
    PSOutput OUT;
    OUT.m_albedo = SubpassInputModelSrg::m_color;
    OUT.m_normal = float4(normalize(vsOutput.m_normal), 1.0);
    OUT.m_position = vsOutput.m_worldPos;
    OUT.m_position.a = linearDepth(OUT.m_position.z);
    OUT.m_outScene = OUT.m_albedo;
    return OUT;
}
