// FLARES FIX
#version 450
 #extension GL_EXT_multiview : enable
// Vk3DVision SinglePass
///////////////////////////////////////////////////////////////////////////////

#define STEREO 1


#define VIEW gl_WorkGroupID.z / (gl_NumWorkGroups.z /2)
#define NumWorkGroups uvec3(gl_NumWorkGroups.x, gl_NumWorkGroups.y, gl_NumWorkGroups.z / 2)
#define WorkGroupID uvec3(gl_WorkGroupID.x, gl_WorkGroupID.y, (gl_WorkGroupID.z < (gl_WorkGroupID.z + 1) / 2 ? gl_WorkGroupID.z : gl_WorkGroupID.z - (gl_WorkGroupID.z + 1) / 2))
#define GlobalInvocationID uvec3(WorkGroupID * gl_WorkGroupSize + gl_LocalInvocationID)

// Stereo and Mono Texture Sampling
#if(STEREO == 1)
	#define SAMPLER_2D sampler2DArray
	#define USAMPLER_2D usampler2DArray
	#define ISAMPLER_2D isampler2DArray
	#define IMAGE_2D image2DArray
	#define UIMAGE_2D uimage2DArray
	#define UTEXTURE_2D utexture2DArray
	#define TEXTURE_2D texture2DArray
	#define SAMPLE_TEXTURE(Sampler, uv) texture(Sampler, vec3(uv, VIEW))
	#define SAMPLE_TEXTURE1(Sampler, uv, bias) texture(Sampler, vec3(uv, VIEW), bias)
	#define IMAGE_LOAD(Image2D, pos) imageLoad(Image2D, ivec3(pos, VIEW))
	#define IMAGE_STORE(Image2D, pos, result) imageStore(Image2D, ivec3(pos, VIEW), result)
	#define TEXEL_FETCH(Sampler, uv, lod) texelFetch(Sampler, ivec3(uv, VIEW), lod)
	#define TEXEL_FETCH_OFFSET(Sampler, uv, lod, offset) texelFetchOffset(Sampler, ivec3(uv, VIEW), lod, offset)
	#define TEXTURE_LOD(Sampler, uv, lod) textureLod(Sampler, vec3(uv, VIEW), lod)
	#define TEXTURE_LOD_OFFSET(Sampler, uv, lod, offset) textureLodOffset(Sampler, vec3(uv, VIEW), lod, offset)
	#define TEXTURE_GATHER(Sampler, uv, comp) textureGather(Sampler, vec3(uv, VIEW), comp)
	#define TEXTURE_GATHER1(Sampler, uv) textureGather(Sampler, vec3(uv, VIEW))
	#define TEXTURE_GATHER_OFFSET(Sampler, uv, offset, comp) textureGatherOffset(Sampler, vec3(uv, VIEW), offset, comp)
	#define TEXTURE_GATHER_OFFSETS(Sampler, uv, offset) textureGatherOffsets(Sampler, vec3(uv, VIEW), offset)
	#define TEXTURE_GATHER_OFFSETS1(Sampler, uv, offset, comp) textureGatherOffsets(Sampler, vec3(uv, VIEW), offset, comp)
	#define TEXTURE_SIZE(Sampler, lod) textureSize(Sampler, lod).xy
	#define TEXTURE_GRAD(Sampler, P, dPdx, dPdy) textureGrad(Sampler, vec3(P, VIEW), dPdx, dPdy)
#else
	#define SAMPLER_2D sampler2D
	#define USAMPLER_2D usampler2D
	#define ISAMPLER_2D isampler2D
	#define IMAGE_2D image2D
	#define UIMAGE_2D uimage2D
	#define UTEXTURE_2D utexture2D
	#define TEXTURE_2D texture2D
	#define SAMPLE_TEXTURE(Sampler, uv) texture(Sampler, uv)
	#define SAMPLE_TEXTURE1(Sampler, uv, bias) texture(Sampler, uv, bias)
	#define IMAGE_LOAD(Image2D, pos) imageLoad(Image2D, pos)
	#define IMAGE_STORE(Image2D, pos, result) imageStore(Image2D, pos, result)
	#define TEXEL_FETCH(Sampler, uv, lod) texelFetch(Sampler, uv, lod)
	#define TEXEL_FETCH_OFFSET(Sampler, uv, lod, offset) texelFetchOffset(Sampler, uv, lod, offset)
	#define TEXTURE_LOD(Sampler, uv, lod) textureLod(Sampler, uv, lod)
	#define TEXTURE_LOD_OFFSET(Sampler, uv, lod, offset) textureLodOffset(Sampler, uv, lod, offset)
	#define TEXTURE_GATHER(Sampler, uv, comp) textureGather(Sampler, uv, comp)
	#define TEXTURE_GATHER1(Sampler, uv, comp) textureGather(Sampler, uv)
	#define TEXTURE_GATHER_OFFSET(Sampler, uv, offset, comp) textureGatherOffset(Sampler, uv, offset, comp)
	#define TEXTURE_GATHER_OFFSETS(Sampler, uv, offset) textureGatherOffsets(Sampler, uv, offset)
	#define TEXTURE_GATHER_OFFSETS1(Sampler, uv, offset, comp) textureGatherOffsets(Sampler, uv, offset, comp)
	#define TEXTURE_SIZE(Sampler, lod) textureSize(Sampler, lod)
	#define TEXTURE_GRAD(Sampler, P, dPdx, dPdy) textureGrad(Sampler, P, dPdx, dPdy)
#endif 
 ///////////////////////////////////////////////////////////////////////////////
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;

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


   

layout(set = 1, binding = 0, std140) uniform _292_294
{
    vec4 _m0;
} _294;

layout(set = 0, binding = 0, std140) uniform _428_430
{
    vec4 _m0;
    vec4 _m1;
    vec4 _m2;
    vec4 _m3;
    vec4 _m4;
    vec4 _m5;
    vec4 _m6;
    vec4 _m7;
    vec4 _m8;
    vec4 _m9;
    vec4 _m10;
    vec4 _m11;
    vec4 _m12;
    vec4 _m13;
    vec4 _m14;
    vec4 _m15;
    vec4 _m16;
    vec4 _m17;
    vec4 _m18;
    vec4 _m19;
    vec4 _m20;
} _430;

layout(push_constant, std430) uniform _1044_1046
{
    int _m0;
    int _m1;
} _1046;

layout(set = 0, binding = 1) uniform SAMPLER_2D _362;
layout(set = 1, binding = 1) uniform TEXTURE_2D _382;
layout(set = 0, binding = 2) uniform sampler _386;
layout(set = 0, binding = 3) uniform SAMPLER_2D _405;
layout(set = 1, binding = 2) uniform TEXTURE_2D _564;
layout(set = 1, binding = 3) uniform SAMPLER_2D _585;
layout(set = 1, binding = 4) uniform TEXTURE_2D _611;
layout(set = 1, binding = 5, r11f_g11f_b10f) uniform IMAGE_2D _645;
layout(set = 0, binding = 4) uniform sampler3D _777;
layout(set = 0, binding = 5) uniform sampler3D _798;
layout(set = 0, binding = 6) uniform sampler3D _920;
layout(set = 1, binding = 6, r11f_g11f_b10f) uniform writeonly IMAGE_2D _1035;
layout(set = 1, binding = 1) uniform SAMPLER_2D _1047;
layout(set = 1, binding = 2) uniform SAMPLER_2D _1048;
layout(set = 1, binding = 4) uniform SAMPLER_2D _1049;

shared float _357[64];
shared float _379[64];

vec2 _16(vec2 _15)
{
    return clamp(_15, vec2(0.0), vec2(1.0));
}

uint _51(inout uint _50)
{
    _50 = ((_50 & 5u) << uint(1)) | ((_50 & 10u) >> uint(1));
    _50 = ((_50 & 3u) << uint(2)) | ((_50 & 12u) >> uint(2));
    return _50;
}

float _63(uvec2 _61, uint _62)
{
    uvec2 _241 = _61 % uvec2(3u);
    uint _245 = ((_241.x + (_241.y * 3u)) + _62) % 9u;
    float _259;
    if (_245 == 0u)
    {
        _259 = 0.0;
    }
    else
    {
        _259 = float((2168608311u >> ((_245 - 1u) << uint(2))) & 15u) / 9.0;
    }
    return _259;
}

vec2 _56(uint _54, uint _55)
{
    float _221 = 1.0 / float(_54);
    uint _233 = _55 + 1u;
    uint _234 = _51(_233);
    return vec2(float(_55 + 1u) * _221, float(_234) / 16.0);
}

uint _21(float _20)
{
    return floatBitsToUint(_20);
}

float _46(float _42, vec4 _43, float _44, bool _45)
{
    float _183 = _43.w / (_42 + _43.z);
    return _183 * _44;
}

float _24(float _23)
{
    float _81 = (((-0.15658299624919891357421875) * abs(_23)) + 1.57079637050628662109375) * sqrt(1.0 - abs(_23));
    float _95;
    if (_23 >= 0.0)
    {
        _95 = _81;
    }
    else
    {
        _95 = 3.1415927410125732421875 - _81;
    }
    return _95;
}

float _27(float _26)
{
    float _109;
    if (_26 < 1.0)
    {
        _109 = _26;
    }
    else
    {
        _109 = 1.0 / _26;
    }
    float _106 = _109;
    float _117 = _106 * _106;
    float _121 = 0.087292902171611785888671875;
    _121 = (-0.3018949925899505615234375) + (_121 * _117);
    _121 = 1.0 + (_121 * _117);
    _121 *= _106;
    float _137;
    if (_26 < 1.0)
    {
        _137 = _121;
    }
    else
    {
        _137 = 1.57079637050628662109375 - _121;
    }
    return _137;
}

float _30(float _29)
{
    float _150 = abs(_29);
    float _147 = _27(_150);
    float _154;
    if (_29 < 0.0)
    {
        _154 = -_147;
    }
    else
    {
        _154 = _147;
    }
    return _154;
}

float _35(float _33, float _34)
{
    float _168 = _33 / _34;
    float _164 = _30(_168);
    if (_34 < 0.0)
    {
        _164 += ((_33 >= 0.0) ? 3.1415927410125732421875 : (-3.1415927410125732421875));
    }
    return _164;
}

float _10(float _9)
{
    return clamp(_9, 0.0, 1.0);
}

void main()
{

    ivec3 _278 = ivec3(GlobalInvocationID);
    vec2 _284 = (vec2(_278.xy) + vec2(0.5)) * _294._m0.zw;
    vec2 _301 = vec2(_278.xy) * _294._m0.zw;
    
    // Actual FIX
    _301.x -= vk3d_params[VIEW].stereo.x * 0.5; 
    uint _309 = uint((int(gl_LocalInvocationID.x) / 2) + ((int(gl_LocalInvocationID.y) / 2) * 8));
    vec2 _324 = (vec2(GlobalInvocationID.xy - gl_LocalInvocationID.xy) - vec2(2.0)) + (vec2(gl_LocalInvocationID.xy) * vec2(2.0));
    _324 *= _294._m0.zw;
    
    uint _344 = uint(int(gl_LocalInvocationID.x) + (int(gl_LocalInvocationID.y) * 8));
    _357[_344] = 1.0 - TEXTURE_LOD(_362, vec4(_324, 0.0, 0.0).xy, vec4(_324, 0.0, 0.0).w).x;
    _379[_344] = TEXTURE_LOD(SAMPLER_2D(_382, _386), vec4(_324, 0.0, 0.0).xy, vec4(_324, 0.0, 0.0).w).x;
    memoryBarrier();
    barrier();
    float _404 = TEXTURE_LOD(_405, vec4(_284, 0.0, 0.0).xy, vec4(_284, 0.0, 0.0).w).x;
    vec4 _419 = vec4(0.0, 0.0, 0.0, 1.0);
    if (_404 < 1.0)
    {
        vec2 _425 = vec2(1.0);
        float _427 = _430._m0.x;
        if (_427 > 0.0)
        {
            float _438 = _404;
            vec2 _440 = vec2(0.0);
            for (uint _442 = 0u; _442 < 4u; _442++)
            {
                for (uint _451 = 0u; _451 < 4u; _451++)
                {
                    uint _459 = (_309 + _451) + (_442 * 8u);
                    float _467 = _357[_459] - _438;
                    float _473 = exp(((-_467) * _467) * 1000000.0);
                    _440 += vec2(_379[_459] * _473, _473);
                }
            }
            vec2 _494 = _430._m0.zw;
            vec2 _506 = pow(vec2(_440.x / _440.y), _494);
            _425 = _16(_506);
            bool _508 = (dot(_430._m1.zw, vec2(1.0)) > 0.0) ? true : false;
            if (_508)
            {
                uint _523;
                if (_508 == true)
                {
                    uint _530 = uint(_430._m2.x) % 9u;
                    uint _531 = _51(_530);
                    _523 = _531;
                }
                else
                {
                    _523 = 0u;
                }
                uint _520 = _523;
                uvec2 _538 = uvec2(_278.xy);
                uint _539 = _520;
                float _534 = (_63(_538, _539) - 0.5) * 0.19607843458652496337890625;
                vec2 _548 = _425 * 2.0;
                vec2 _545 = _16(_548);
                vec2 _555 = _425 + (_545 * _534);
                _425 = _16(_555);
            }
        }
        float _557 = _430._m3.x;
        
        vec3 _563 = TEXTURE_LOD(SAMPLER_2D(_564, _386), vec4(_284, 0.0, 0.0).xy, vec4(_284, 0.0, 0.0).w).xyz;
        if (_557 > 0.0)
        {
            vec3 _584 = TEXTURE_LOD(_585, vec4(_284 + (_294._m0.zw * 0.25), 0.0, 0.0).xy, vec4(_284 + (_294._m0.zw * 0.25), 0.0, 0.0).w).xyz;
            float _610 = TEXTURE_LOD(SAMPLER_2D(_611, _386), vec4(_284, 0.0, 0.0).xy, vec4(_284, 0.0, 0.0).w).x;
            _563 = mix(_563, _584, vec3(_610));
        }
       // Reflection
       _419 = vec4(_563 * _425.y, _425.x);
    }
    
    
    
    
    vec3 _642 = IMAGE_LOAD(_645, _278.xy).xyz;
    vec3 _653;
    if (_404 == 1.0)
    {
        _653 = vec3(0.0);
    }
    else
    {
        _653 = _419.xyz + (_642 * _419.w);
    }
    _642 = _653;
    uvec2 _666 = uvec2(_278.xy) & uvec2(3u);
    vec2 _678;
    if (dot(_430._m1.zw, vec2(1.0)) > 0.0)
    {
        uint _695 = 16u;
        uint _696 = ((_666.x + (_666.y * 4u)) + uint(int(_430._m2.x))) & 7u;
        _678 = (_56(_695, _696) * 2.0) - vec2(1.0);
    }
    else
    {
        _678 = vec2(0.0);
    }
    vec2 _672 = _678;
    _672 = (_672 + vec2(0.5)) / _430._m4.xy;
    vec4 _711 = vec4(0.0);
    float _713 = 1.0;
    float _716 = _430._m5.w;
    uint _714 = _21(_716);
    if ((_714 & 6u) != 0u)
    {
        vec2 _726 = _301 + _672;
        
       //_726.x -=vk3d_params[VIEW].stereo.x *0.5;
        
        vec3 _730 = _430._m4.xyz;
        float _734 = 0.100000001490116119384765625;
        float _736 = _430._m6.y;
        float _740 = _430._m4.z;
        float _743 = 0.0;
        float _746 = _404;
        vec4 _748 = _430._m7;
        float _751 = 1.0;
        bool _752 = false;
        float _744 = _46(_746, _748, _751, _752);
        _743 = pow((_744 - _734) / (_736 - _734), 0.5);
        _743 = (_743 * (_740 - 1.0)) / _740;
        vec4 _768 = vec4(_726, _743, 0.0);
        
        // FIX?
//        _768.x -=vk3d_params[VIEW].stereo.x *0.5;        
        
        vec3 _794 =  textureLod(_777, vec4(_768.xyz, 0.0).xyz, vec4(_768.xyz, 0.0).w).xyz;
        _711 = vec4(_794.x, _794.y, _794.z, _711.w);
        vec2 _797 = textureLod(_798, vec4(_768.xyz, 0.0).xyz, vec4(_768.xyz, 0.0).w).xy;
        _711.w = _797.x;
        _713 = _797.y;
    }
    if ((_714 & 2u) != 0u)
    {
        vec4 _826 = vec4(0.0);
        vec3 _827 = _430._m8.xyz;
        float _832 = _404;
        vec4 _834 = _430._m7;
        float _837 = 1.0;
        bool _838 = false;
        float _831 = _46(_832, _834, _837, _838);
        vec3 _840 = mix(_430._m9.xyz, _430._m10.xyz, vec3(_301.x * _430._m11.z));
        vec3 _857 = mix(_430._m12.xyz, _430._m13.xyz, vec3(_301.x * _430._m11.z));
        vec3 _873 = mix(_857, _840, vec3(1.0 - (_301.y * _430._m11.w)));
        vec4 _884 = vec4(0.0);
        vec3 _885 = normalize(_873);
        float _888 = _885.z;
        _884.x = clamp(_24(_888) / 3.1415927410125732421875, 0.00390625, 0.99609375);
        float _897 = _885.y;
        float _900 = _885.x;
        _884.y = _35(_897, _900) / 6.283185482025146484375;
        _884.z = sqrt(_831 * _430._m14.w);
        _884.z = clamp(_884.z, 0.015625, 0.984375);
        
         //_884.x +=vk3d_params[VIEW].stereo.x * (1- _884.z * vk3d_params[VIEW].stereo.y) *0.5;
        
        vec3 _927 = textureLod(_920, _884.xyz, _884.w).xyz;
        _826 = vec4(_927.x, _927.y, _927.z, _826.w);
        float _930 = _430._m15.w;
        if (_930 > 0.0)
        {
            float _938 = _430._m16.x * 1.57079637050628662109375;
            float _943 = _430._m17.x * 3.1415927410125732421875;
            vec3 _948 = vec3(sin(_938) * cos(_943), sin(_938) * sin(_943), cos(_938));
            float _962 = dot(normalize(_873), _948);
            vec3 _977;
            if (_430._m19.x > 0.0)
            {
                _977 = vec3(1.0);
            }
            else
            {
                _977 = _430._m20.xyz;
            }
            vec3 _967 = _977 * (10.0 * _430._m18.x);
            float _992 = _962;
            float _1001 = pow(_10(_992), 10000.0) * float(_404 == 1.0);
            vec3 _1006 = _826.xyz + ((_967 * (_930 * 10.0)) * _10(_1001));
            _826 = vec4(_1006.x, _1006.y, _1006.z, _826.w);
        }
       _642 += (_826.xyz * _713);
    }
    if ((_714 & 4u) != 0u)
    {
    	//FOG
        _642 = (_642 * _711.w) + _711.xyz;
    }
    IMAGE_STORE(_645, _278.xy, vec4(_642, 1.0));
    IMAGE_STORE(_1035, _278.xy, vec4(_642, 1.0));
}






















