// Compute Water
#version 460
 #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 
 ///////////////////////////////////////////////////////////////////////////////

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

layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;

layout(set = 0, binding = 0, std140) uniform _208_210
{
    vec4 _m0;
    vec4 _m1;
    vec4 _m2;
    vec4 _m3;
    vec4 _m4;
    vec4 _m5;
    float _m6;
    float _m7;
    float _m8;
    float _m9;
    float _m10;
    float _m11;
    float _m12;
    float _m13;
    float _m14;
    float _m15;
    float _m16;
    float _m17;
    float _m18;
    float _m19;
    float _m20;
    float _m21;
    float _m22;
    float _m23;
    uint _m24;
    float _m25;
    float _m26;
    float _m27;
    float _m28;
    float _m29;
} _210;

layout(set = 1, binding = 0, std140) uniform _294_296
{
    vec4 _m0;
    vec4 _m1;
} _296;

layout(set = 0, binding = 1, std140) uniform _825_827
{
    vec4 _m0[64];
} _827;

layout(set = 1, binding = 1) uniform TEXTURE_2D _316;
layout(set = 0, binding = 2) uniform sampler _320;
layout(set = 1, binding = 2) uniform TEXTURE_2D _422;
layout(set = 0, binding = 3) uniform TEXTURE_2D _441;
layout(set = 0, binding = 4) uniform texture3D _501;
layout(set = 0, binding = 5) uniform sampler _503;
layout(set = 0, binding = 6) uniform texture3D _522;
layout(set = 0, binding = 7) uniform texture3D _620;
layout(set = 0, binding = 8) uniform sampler _622;
layout(set = 1, binding = 3) uniform TEXTURE_2D _654;
layout(set = 0, binding = 9) uniform TEXTURE_2D _679;
layout(set = 0, binding = 10) uniform TEXTURE_2D _712;
layout(set = 1, binding = 4) uniform TEXTURE_2D _729;
layout(set = 1, binding = 5) uniform TEXTURE_2D _751;
layout(set = 1, binding = 6) uniform TEXTURE_2D _791;
layout(set = 0, binding = 11) uniform sampler _793;
layout(set = 1, binding = 7) uniform TEXTURE_2D _803;
layout(set = 1, binding = 8, r11f_g11f_b10f) uniform writeonly IMAGE_2D _815;

uint _50;
uint _52;
uint _54;
uint _56;
uint _58;
uint _60;
uint _62;
uint _63;
uint _65;
uint _66;
uint _68;
uint _70;
uint _72;
uint _74;
uint _76;
uint _78;
uint _80;
uint _82;
uint _84;

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

float _42(inout float _40, bool _41)
{
    if (_41)
    {
        _40 += (float(_40 < _210._m6) * _210._m7);
    }
    float _228 = _40;
    return _210._m0.w / (_10(_228) + _210._m0.z);
}

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

vec3 _47(vec2 _46)
{
    return (vec3(_210._m8, _210._m9, _210._m10) + (vec3(_210._m11, _210._m12, _210._m13) * _46.x)) + (vec3(_210._m14, _210._m15, _210._m16) * _46.y);
}

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

float _27(float _26)
{
    float _129;
    if (_26 < 1.0)
    {
        _129 = _26;
    }
    else
    {
        _129 = 1.0 / _26;
    }
    float _126 = _129;
    float _137 = _126 * _126;
    float _141 = 0.087292902171611785888671875;
    _141 = (-0.3018949925899505615234375) + (_141 * _137);
    _141 = 1.0 + (_141 * _137);
    _141 *= _126;
    float _157;
    if (_26 < 1.0)
    {
        _157 = _141;
    }
    else
    {
        _157 = 1.57079637050628662109375 - _141;
    }
    return _157;
}

float _30(float _29)
{
    float _170 = abs(_29);
    float _167 = _27(_170);
    float _174;
    if (_29 < 0.0)
    {
        _174 = -_167;
    }
    else
    {
        _174 = _167;
    }
    return _174;
}

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

vec3 _21(vec3 _20)
{
    return _20 * _20;
}

void main()
{
    _50 = 2147483648u;
    _52 = 1073741824u;
    _54 = 0u;
    _56 = 268435456u;
    _58 = 536870912u;
    _60 = 805306368u;
    _62 = 1073741824u;
    _63 = 1342177280u;
    _65 = 0u;
    _66 = 16777216u;
    _68 = 33554432u;
    _70 = 50331648u;
    _72 = 67108864u;
    _74 = 83886080u;
    _76 = 100663296u;
    _78 = 117440512u;
    _80 = 134217728u;
    _82 = 150994944u;
    _84 = 167772160u;
    ivec2 _279 = ivec2(GlobalInvocationID.xy);
    vec2 _288 = (vec2(_279) + vec2(0.5)) * _296._m0.zw;
    vec2 _302 = vec2(_288.x * _210._m28, _288.y);
    vec4 _313 = TEXTURE_LOD(SAMPLER_2D(_316, _320), vec4(_302, 0.0, 0.0).xy, vec4(_302, 0.0, 0.0).w);
    uint _336 = uint(_313.z * 255.0);
    float _342 = float(_336 & 63u) / 63.0;
    bool _349 = ((_336 >> uint(6)) & 1u) != 0u;
    bool _354 = ((_336 >> uint(7)) & 1u) != 0u;
    bool _359 = (_279.x % 2) == 0;
    bool _366 = _359 ? _349 : _354;
    if (!_366)
    {
        return;
    }
    uint _376 = uint(_313.w * 255.0);
    bool _381 = (_376 & 128u) != 0u;
    _376 &= 127u;
    float _389 = float(_376) / 127.0;
    vec2 _394 = _313.xy;
    vec2 _397 = (_394 * 255.0) - vec2(128.0);
    _397 *= float(_359 || _381);
    vec2 _409 = _397 * _296._m0.zw;
    vec2 _415 = _288 + _409;
    vec3 _419 = vec3(0.0);
    vec3 _421 = TEXTURE_LOD(SAMPLER_2D(_422, _320), vec4(_415, 0.0, 0.0).xy, vec4(_415, 0.0, 0.0).w).xyz;
    vec3 _438 = _421;
    float _440 = 1.0 - TEXTURE_LOD(SAMPLER_2D(_441, _320), vec4(_302, 0.0, 0.0).xy, vec4(_302, 0.0, 0.0).w).x;
    
    float _460 = _440;
    bool _462 = false;
    float _463 = _42(_460, _462);
    float _458 = _463;
    vec2 _464 = _288 + vec2(0.0);
    float _468 = 0.119999997317790985107421875;
    float _470 = _210._m3.y;
    float _474 = _210._m4.z + 1.0;
    float _479 = pow(max(0.0, _458 - _468) / (_470 - _468), 0.5);
    float _489 = (0.5 / _474) + (_479 * (1.0 - (1.0 / _474)));
    
    // FOG on water
    _464.x -= vk3d_params[VIEW].stereo.x * 0.5;
    vec3 _498 = textureLod(sampler3D(_501, _503), vec4(_464, _489, 0.0).xyz, vec4(_464, _489, 0.0).w).xyz;
    vec2 _521 = textureLod(sampler3D(_522, _503), vec4(_464, _489, 0.0).xyz, vec4(_464, _489, 0.0).w).xy;
    float _540 = _521.x;
    float _543 = _521.y;
    float _548 = _210._m29;
    uint _546 = _15(_548);
    bool _552 = (_546 & 2u) != 0u;
    _498 *= float(_552);
    _540 = _552 ? _540 : 1.0;
    vec3 _563 = vec3(0.0);
    if ((_546 & 1u) != 0u)
    {
        vec3 _569 = vec3(_210._m17, _210._m18, _210._m19);
        vec3 _580 = _47(_288);
        vec4 _583 = vec4(0.0);
        vec3 _585 = normalize(_580);
        float _588 = _585.z;
        _583.x = clamp(_24(_588) / 3.1415927410125732421875, 0.00390625, 0.99609375);
        float _597 = _585.y;
        float _600 = _585.x;
        _583.y = _35(_597, _600) / 6.283185482025146484375;
        _583.z = sqrt(_458 * _210._m5.w);
        _583.z = clamp(_583.z, 0.015625, 0.984375);
        _563 = textureLod(sampler3D(_620, _622), _583.xyz, _583.w).xyz;
        _563 *= _543;
    }
    float _634 = 1.0 / (_540 + 9.9999999747524270787835121154785e-07);
    _438 = ((_438 - _498) * _634) - _563;
    _438 = max(vec3(0.0), _438 *0.25);
   _419 += (_438 * _389);
   
    // Reflections position but other things
    vec3 _653 =  TEXTURE_LOD(SAMPLER_2D(_654, _503), vec4(_302, 0.0, 0.0).xy, vec4(_302, 0.0, 0.0).w).xyz;
    bool _670 = _296._m1.z > 0.0;
    if (_670)
    {
        vec2 _678 = TEXTURE_LOD(SAMPLER_2D(_679, _320), vec4(_288, 0.0, 0.0).xy, vec4(_288, 0.0, 0.0).w).xy;
        uint _695 = uint((_678.y * 255.0) + 0.5);
        vec3 _701 = vec3(0.0);
        float _702 = 1.0;
        if (_695 == 3u)
        {
            float _707 = _678.x;
            _702 = 1.0 - _707;
            _701 = TEXTURE_LOD(SAMPLER_2D(_712, _503), vec4(_288, 0.0, 0.0).xy, vec4(_288, 0.0, 0.0).w).xyz;
        }
        vec3 _728 = TEXTURE_LOD(SAMPLER_2D(_729, _503), vec4(_288, 0.0, 0.0).xy, vec4(_288, 0.0, 0.0).w).xyz;
        _701 += (_728 * _702);
        vec3 _750 = TEXTURE_LOD(SAMPLER_2D(_751, _503), vec4(_288, 0.0, 0.0).xy, vec4(_288, 0.0, 0.0).w).xyz;
        vec3 _768 = _750;
        vec3 _767 = _21(_768);
        _653 += (_701 * _767);
    }
    // Reflect
    _419 += _653;
    _419 = ((_419 + _563) * _540) + _498;
    _419 = mix(_421, _419, vec3(_342));
    _419 *= (1.0 - TEXEL_FETCH(SAMPLER_2D(_791, _793), _279, 0).x);
    _419 += TEXEL_FETCH(SAMPLER_2D(_803, _793), _279, 0).xyz;
    IMAGE_STORE(_815, _279, vec4(_419, 1.0));
}











