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

#define STEREO 1

#define VIEW gl_ViewIndex

// 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 
 ///////////////////////////////////////////////////////////////////////////////

// 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 = 0, binding = 0, std140) uniform _270_272
{
    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;
} _272;

layout(set = 2, binding = 0, std140) uniform _349_351
{
    vec4 _m0;
    vec4 _m1;
    vec4 _m2;
    vec4 _m3;
    vec4 _m4;
    vec4 _m5;
    vec4 _m6;
    vec4 _m7;
} _351;

layout(set = 1, binding = 0, std140) uniform _397_399
{
    vec4 _m0;
    vec4 _m1;
    vec4 _m2;
    vec4 _m3;
    vec4 _m4;
    vec4 _m5;
    vec4 _m6;
    vec4 _m7;
} _399;

layout(set = 0, binding = 1, std430) restrict readonly buffer _759_761
{
    vec4 _m0[];
} _761;

layout(push_constant, std430) uniform _755_757
{
    int _m0;
    int _m1;
} _757;

layout(set = 0, binding = 2) uniform SAMPLER_2D _281;
layout(set = 0, binding = 3) uniform SAMPLER_2D _304;
layout(set = 2, binding = 1) uniform SAMPLER_2D _359;
layout(set = 2, binding = 2) uniform SAMPLER_2D _386;
layout(set = 0, binding = 4) uniform sampler3D _510;
layout(set = 0, binding = 5) uniform sampler3D _531;
layout(set = 0, binding = 6) uniform sampler3D _653;

layout(location = 2) in vec4 _360;
layout(location = 0) in vec4 _382;
layout(location = 0) out vec4 _427;
layout(location = 1) out vec4 _701;
layout(location = 2) out vec4 _702;

float _58(vec4 _57)
{
    return _57.z;
}

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

float _18(float _17)
{
    return _17 * ((_17 * ((_17 * 0.305306017398834228515625) + 0.6821711063385009765625)) + 0.01252287812530994415283203125);
}

vec3 _24(vec3 _23)
{
    float _93 = _23.x;
    vec3 _92;
    _92.x = _18(_93);
    float _99 = _23.y;
    _92.y = _18(_99);
    float _105 = _23.z;
    _92.z = _18(_105);
    return _92;
}

vec4 _30(vec4 _29)
{
    vec3 _115 = _29.xyz;
    vec4 _114 = vec4(_24(_115), 1.0);
    float _123 = _29.w;
    _114.w = _18(_123);
    return _114;
}

vec4 _54(SAMPLER_2D _52, vec2 _53)
{
    vec4 _234 = SAMPLE_TEXTURE(_52, _53);
    vec4 _238 = _234;
    _234 = _30(_238);
    return _234;
}

uint _15(float _14)
{
    return floatBitsToUint(_14);
}

float _67(float _63, vec4 _64, float _65, bool _66)
{
    float _248 = _64.w / (_63 + _64.z);
    return _248 * _65;
}

float _33(float _32)
{
    float _132 = (((-0.15658299624919891357421875) * abs(_32)) + 1.57079637050628662109375) * sqrt(1.0 - abs(_32));
    float _146;
    if (_32 >= 0.0)
    {
        _146 = _132;
    }
    else
    {
        _146 = 3.1415927410125732421875 - _132;
    }
    return _146;
}

float _36(float _35)
{
    float _160;
    if (_35 < 1.0)
    {
        _160 = _35;
    }
    else
    {
        _160 = 1.0 / _35;
    }
    float _157 = _160;
    float _168 = _157 * _157;
    float _172 = 0.087292902171611785888671875;
    _172 = (-0.3018949925899505615234375) + (_172 * _168);
    _172 = 1.0 + (_172 * _168);
    _172 *= _157;
    float _188;
    if (_35 < 1.0)
    {
        _188 = _172;
    }
    else
    {
        _188 = 1.57079637050628662109375 - _172;
    }
    return _188;
}

float _39(float _38)
{
    float _201 = abs(_38);
    float _198 = _36(_201);
    float _205;
    if (_38 < 0.0)
    {
        _205 = -_198;
    }
    else
    {
        _205 = _198;
    }
    return _205;
}

float _44(float _42, float _43)
{
    float _219 = _42 / _43;
    float _215 = _39(_219);
    if (_43 < 0.0)
    {
        _215 += ((_42 >= 0.0) ? 3.1415927410125732421875 : (-3.1415927410125732421875));
    }
    return _215;
}

void main()
{
    vec4 _264 = gl_FragCoord;
    float _261 = _58(_264);
    vec2 _267 = gl_FragCoord.xy * _272._m4.zw;
    
    float _280 = min(TEXTURE_LOD(_281, vec4(_267 * _272._m5.zw, 0.0, 0.0).xy, vec4(_267 * _272._m5.zw, 0.0, 0.0).w).x, TEXTURE_LOD(_304, vec4(_267, 0.0, 0.0).xy, vec4(_267, 0.0, 0.0).w).x);
    float _319 = 1.0 / (1.0 - _280);
    float _323 = 1.0 / (1.0 - _261);
    float _327 = _319 - _323;
    bool _331 = _272._m6.z > 0.0;
    float _339;
    if (_331)
    {
        _339 = abs(_327);
    }
    else
    {
        _339 = _327;
    }
    float _337 = _339;
    float _356 = _337 * (1.0 / _351._m6.x);
    float _347 = _10(_356);
    vec2 _380 = (_360.xy * _351._m7.xy) + vec2(_272._m7.x * _351._m7.z, _272._m7.y * _351._m7.w);
    vec4 _358 = _54(_359, _380) * _382;
    vec2 _387 = _360.xy;
    vec4 _385 = _54(_386, _387);
    _358 *= _351._m4;
    vec3 _412 = _358.xyz + ((vec3(_351._m5.x * _399._m7.x) * _358.xyz) * _385.xyz);
    _358 = vec4(_412.x, _412.y, _412.z, _358.w);
    _358.w *= _347;
    _358.w *= _385.w;
    _427 = _358;
    vec2 _429 = gl_FragCoord.xy * _272._m4.zw;
    
    // FIX for SKY
    _429.x -= vk3d_params[VIEW].stereo.x*0.5;
    
    float _436 = gl_FragCoord.z;
    vec4 _440 = vec4(0.0);
    float _442 = 1.0;
    float _446 = _272._m8.w;
    uint _444 = _15(_446);
    if ((_444 & 6u) != 0u)
    {
        vec2 _456 = _429 + vec2(0.0);
        vec3 _460 = _272._m9.xyz;
        float _465 = 0.100000001490116119384765625;
        float _467 = _272._m10.y;
        float _471 = _272._m9.z;
        float _474 = 0.0;
        float _478 = _436;
        vec4 _480 = _272._m11;
        float _483 = 1.0;
        bool _484 = false;
        float _475 = _67(_478, _480, _483, _484);
        _474 = pow((_475 - _465) / (_467 - _465), 0.5);
        _474 = (_474 * (_471 - 1.0)) / _471;
        vec4 _501 = vec4(_456, _474, 0.0);
        vec3 _527 = textureLod(_510, vec4(_501.xyz, 0.0).xyz, vec4(_501.xyz, 0.0).w).xyz;
        _440 = vec4(_527.x, _527.y, _527.z, _440.w);
        vec2 _530 = textureLod(_531, vec4(_501.xyz, 0.0).xyz, vec4(_501.xyz, 0.0).w).xy;
        _440.w = _530.x;
        _442 = _530.y;
    }
    if ((_444 & 2u) != 0u)
    {
        vec4 _559 = vec4(0.0);
        vec3 _560 = _272._m12.xyz;
        float _566 = _436;
        vec4 _568 = _272._m11;
        float _571 = 1.0;
        bool _572 = false;
        float _565 = _67(_566, _568, _571, _572);
        vec3 _574 = mix(_272._m13.xyz, _272._m14.xyz, vec3(_429.x * _272._m5.z));
        vec3 _590 = mix(_272._m15.xyz, _272._m16.xyz, vec3(_429.x * _272._m5.z));
        vec3 _606 = mix(_590, _574, vec3(1.0 - (_429.y * _272._m5.w)));
        vec4 _617 = vec4(0.0);
        vec3 _618 = normalize(_606);
        float _621 = _618.z;
        _617.x = clamp(_33(_621) / 3.1415927410125732421875, 0.00390625, 0.99609375);
        float _630 = _618.y;
        float _633 = _618.x;
        _617.y = _44(_630, _633) / 6.283185482025146484375;
        _617.z = sqrt(_565 * _272._m17.w);
        _617.z = clamp(_617.z, 0.015625, 0.984375);

         vec3 _660 = textureLod(_653, _617.xyz, _617.w).xyz;
        _559 = vec4(_660.x, _660.y, _660.z, _559.w);
        vec3 _669 = _427.xyz + (_559.xyz * _442);
        _427 = vec4(_669.x, _669.y, _669.z, _427.w);
    }
    if ((_444 & 4u) != 0u)
    {
        vec3 _685 = (_427.xyz * _440.w) + _440.xyz;
        _427 = vec4(_685.x, _685.y, _685.z, _427.w);
    }
    float _688 = _427.w;
    _427.w = _10(_688);
    vec3 _698 = _427.xyz * _427.w;
    _427 = vec4(_698.x, _698.y, _698.z, _427.w);
    _701 = vec4(0.0);
    _702 = vec4(0.0);
    if (_272._m18.w > 0.0)
    {
        vec2 _709 = gl_FragCoord.xy * _272._m4.xy;
        bool _716 = (gl_FragCoord.z < TEXTURE_LOD(_281, vec4(_709, 0.0, 0.0).xy, vec4(_709, 0.0, 0.0).w).x) ? true : false;
        if (_716 == false)
        {
            _701 = _427;
        }
        _427 *= float(_716 == true);
        _702.x = _427.w;
    }
    if (!_331)
    {
        _702.x = _358.w;
    }
}


