// LENS FLARES
#version 450


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

layout(set = 0, binding = 1, std140) uniform freqLow_vertexUniforms_ubo
{
    vec4 vertexxyzscale;
    vec4 vertexxyzbias;
    vec4 vertexstscalebias;
    vec4 flaresmult;
    vec4 resolutionscale;
} freqLow_vertexUniforms;

layout(set = 0, binding = 0, std140) uniform freqHigh_vertexUniforms_ubo
{
    vec4 mvpmatrixx;
    vec4 mvpmatrixy;
    vec4 mvpmatrixz;
    vec4 mvpmatrixw;
    vec4 color;
    vec4 renderpositiontoviewtexture;
} freqHigh_vertexUniforms;

layout(set = 0, binding = 2) uniform sampler2D samp_viewdepthmap;

layout(location = 0) in vec4 in_Position;
layout(location = 2) out vec4 vofi_TexCoord0;
layout(location = 1) in vec2 in_TexCoord;
layout(location = 0) out vec4 vofi_Color;
layout(location = 3) in vec4 in_Color;
layout(location = 12) in vec2 in_VmtrTC;
layout(location = 3) out vec4 vofi_TexCoord1;

float dot4(vec4 a, vec4 b)
{
    return dot(a, b);
}

float SRGBlinearApprox(float value)
{
    return value * ((value * ((value * 0.305306017398834228515625) + 0.6821711063385009765625)) + 0.01252287812530994415283203125);
}

vec3 SRGBlinearApprox(vec3 sRGB)
{
    float param = sRGB.x;
    vec3 outLinear;
    outLinear.x = SRGBlinearApprox(param);
    float param_1 = sRGB.y;
    outLinear.y = SRGBlinearApprox(param_1);
    float param_2 = sRGB.z;
    outLinear.z = SRGBlinearApprox(param_2);
    return outLinear;
}

vec4 SRGBlinearApprox(vec4 sRGBA)
{
    vec3 param = sRGBA.xyz;
    vec4 outLinear = vec4(SRGBlinearApprox(param), 1.0);
    float param_1 = sRGBA.w;
    outLinear.w = SRGBlinearApprox(param_1);
    return outLinear;
}

vec4 tex2Dlod(sampler2D image, vec4 texcoord)
{
    return textureLod(image, texcoord.xy, texcoord.w);
}

float saturate(float v)
{
    return clamp(v, 0.0, 1.0);
}

vec4 saturate(vec4 v)
{
    return clamp(v, vec4(0.0), vec4(1.0));
}

void main()
{
    vec4 position = (vec4(in_Position.xy, 0.0, 1.0) * freqLow_vertexUniforms.vertexxyzscale) + freqLow_vertexUniforms.vertexxyzbias;
    vec4 param = position;
    vec4 param_1 = freqHigh_vertexUniforms.mvpmatrixx;
    gl_Position.x = dot4(param, param_1);
    vec4 param_2 = position;
    vec4 param_3 = freqHigh_vertexUniforms.mvpmatrixy;
    gl_Position.y = dot4(param_2, param_3);
    vec4 param_4 = position;
    vec4 param_5 = freqHigh_vertexUniforms.mvpmatrixz;
    gl_Position.z = dot4(param_4, param_5);
    vec4 param_6 = position;
    vec4 param_7 = freqHigh_vertexUniforms.mvpmatrixw;
    gl_Position.w = dot4(param_6, param_7);
    
    gl_Position.x += vk3d_params.stereo.x;// * (gl_Position.w - vk3d_params.stereo.y); 
    
    vofi_TexCoord0 = ((in_TexCoord * freqLow_vertexUniforms.vertexstscalebias.xy) + freqLow_vertexUniforms.vertexstscalebias.zw).xyxy;
    vec4 param_8 = in_Color;
    vofi_Color = (SRGBlinearApprox(param_8) * freqHigh_vertexUniforms.color) * freqLow_vertexUniforms.flaresmult.x;
    vec2 pos = (in_VmtrTC * 0.5) + vec2(0.5);
    
    // Fix for occlussion
    pos.x += vk3d_params.stereo.x * 0.5;
    
    float centerZ = in_Position.z;
    int kernel_radius = 3;
    float occlusion_ratio = 0.0;
    float _277;
    for (int y = -kernel_radius; y <= kernel_radius; y++)
    {
        for (int x = -kernel_radius; x <= kernel_radius; x++)
        {
        
            vec2 tc = pos + (vec2(float(x), float(y)) * freqHigh_vertexUniforms.renderpositiontoviewtexture.zw);
            tc.y = 1.0 - tc.y;
            vec4 param_9 = vec4(tc * freqLow_vertexUniforms.resolutionscale.xy, 0.0, 0.0);
            
            float ndcZ = tex2Dlod(samp_viewdepthmap, param_9).x;
            if (ndcZ > centerZ)
            {
                _277 = 1.0;
            }
            else
            {
                _277 = 0.0;
            }
            float param_10 = _277;
            occlusion_ratio += saturate(param_10);
        }
    }
    occlusion_ratio /= float(((kernel_radius * 2) + 1) * ((kernel_radius * 2) + 1));
    vec4 param_11 = vec4(occlusion_ratio);
    vofi_TexCoord1 = saturate(param_11);
    

    
    if (true)
    {
        float opacityMin = 0.0039215688593685626983642578125;
        float opacityScale = 1.0 / (1.0 - opacityMin);
        if (vofi_Color.w < opacityMin)
        {
            gl_Position = vec4(vec3(0.0).x, vec3(0.0).y, vec3(0.0).z, gl_Position.w);
        }
        vofi_Color.w = (vofi_Color.w - opacityMin) * opacityScale;
    }
    gl_Position.y = -gl_Position.y;

// Vk3DVision (http://3dsurroundgaming.com/) - Stereo Injection
}



