//More ingame 3D HUD.
#version 450
#extension GL_EXT_nonuniform_qualifier : require

struct FlrInput
{
    vec4 position;
    vec4 color;
    vec2 UV0;
    vec2 UV1;
};

struct FlrOutput
{
    vec4 position;
    vec4 UV0;
    vec2 UV1;
    vec4 color;
    vec4 sdfParameters;
    vec4 colorOutline;
};


// Vk3DVision (http://3dsurroundgaming.com/) - Stereo Injection
layout(set = 0, binding = 110, std140) uniform Vk3DParams
{
    vec4 stereo;
    vec4 custom_params;
} vk3d_params;

layout(set = 0, binding = 0, std140) uniform PerInstance
{
    mat4 projMatrix;
    mat4 viewProjMatrix;
    mat4 worldViewProjMatrix;
    vec4 sdfParsContour;
    vec4 sdfParsContourOutline;
    vec4 colorText;
    vec4 colorOutline;
    vec4 posOffset;
} _61;

layout(set = 0, binding = 99, std430) buffer DebugBuffer
{
    uint debugBuffer[];
} _301;

layout(set = 2, binding = 0) uniform texture2D globalTextures[];
layout(set = 2, binding = 0) uniform textureCube globalCubeTextures[];
layout(set = 2, binding = 0) uniform texture3D global3DTextures[];
layout(set = 1, binding = 0) uniform sampler linearClampSampler;
layout(set = 1, binding = 1) uniform sampler linearRepeatSampler;
layout(set = 1, binding = 2) uniform sampler nearestClampSampler;
layout(set = 1, binding = 3) uniform sampler nearestRepeatSampler;
layout(set = 1, binding = 4) uniform sampler shadowSampler;

layout(location = 0) in vec4 inPosition;
layout(location = 3) in vec4 inColor;
layout(location = 1) in vec2 inUV0;
layout(location = 2) in vec2 inUV1;
layout(location = 0) out vec4 outUV0;
layout(location = 1) out vec2 outUV1;
layout(location = 2) out vec4 outColor;
layout(location = 3) out vec4 outSdfParameters;
layout(location = 4) out vec4 outColorOutline;

float sRGBToLinear(float val)
{
    float ret;
    if (val <= 0.0)
    {
        ret = 0.0;
    }
    else
    {
        if (val <= 0.040449999272823333740234375)
        {
            ret = val / 12.9200000762939453125;
        }
        else
        {
            if (val <= 1.0)
            {
                ret = pow((val + 0.054999999701976776123046875) / 1.05499994754791259765625, 2.400000095367431640625);
            }
            else
            {
                ret = 1.0;
            }
        }
    }
    return ret;
}

FlrOutput flrMain(FlrInput flrInput)
{
    FlrOutput flrOutput;
    flrOutput.position = _61.viewProjMatrix * vec4(flrInput.position.xyz, 1.0);
    
    //Fix here.
    flrOutput.position.x += vk3d_params.stereo.x * (flrOutput.position.w - vk3d_params.stereo.y);
    
    float param = abs(flrInput.color.x);
    flrOutput.color.x = sRGBToLinear(param);
    float param_1 = abs(flrInput.color.y);
    flrOutput.color.y = sRGBToLinear(param_1);
    float param_2 = abs(flrInput.color.z);
    flrOutput.color.z = sRGBToLinear(param_2);
    flrOutput.color.w = flrInput.color.w;
    flrOutput.UV0 = vec4(flrInput.UV0.x, flrInput.UV0.y, flrOutput.UV0.z, flrOutput.UV0.w);
    flrOutput.UV0.z = flrInput.position.z;
    flrOutput.UV0.w = flrInput.position.w;
    flrOutput.UV1 = flrInput.UV1;
    vec2 ssPos;
    ssPos.x = flrOutput.position.x / flrOutput.position.w;
    ssPos.y = flrOutput.position.y / flrOutput.position.w;
    ssPos.x += 1.0;
    ssPos.x *= 0.5;
    ssPos.y += 1.0;
    ssPos.y *= 0.5;
    ssPos = clamp(ssPos, vec2(0.0), vec2(1.0));
    if (ssPos.x < 0.02500000037252902984619140625)
    {
        float d = 40.0 * ssPos.x;
        float alphaMod = d;
        flrOutput.color.w *= alphaMod;
    }
    else
    {
        if (ssPos.x > 0.97500002384185791015625)
        {
            float d_1 = 40.0 * (ssPos.x - 0.97500002384185791015625);
            float alphaMod_1 = 1.0 - d_1;
            flrOutput.color.w *= alphaMod_1;
        }
    }
    if (ssPos.y < 0.02500000037252902984619140625)
    {
        float d_2 = 40.0 * ssPos.y;
        float alphaMod_2 = d_2;
        flrOutput.color.w *= alphaMod_2;
    }
    else
    {
        if (ssPos.y > 0.97500002384185791015625)
        {
            float d_3 = 40.0 * (ssPos.y - 0.97500002384185791015625);
            float alphaMod_3 = 1.0 - d_3;
            flrOutput.color.w *= alphaMod_3;
        }
    }
    flrOutput.UV1 = ssPos;
    flrOutput.sdfParameters = vec4(0.0);
    flrOutput.colorOutline = vec4(0.0);
    return flrOutput;
}

void main()
{
    FlrInput flrInput;
    flrInput.position = inPosition;
    flrInput.color = inColor;
    flrInput.UV0 = inUV0;
    flrInput.UV1 = inUV1;
    FlrInput param = flrInput;
    FlrOutput flrOutput = flrMain(param);
    outUV0 = flrOutput.UV0;
    outUV1 = flrOutput.UV1;
    outColor = flrOutput.color;
    outSdfParameters = flrOutput.sdfParameters;
    outColorOutline = flrOutput.colorOutline;
    gl_Position = flrOutput.position;
}


