#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 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 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, comp)
	#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 
 ///////////////////////////////////////////////////////////////////////////////
#extension GL_KHR_shader_subgroup_shuffle : require
#extension GL_KHR_shader_subgroup_basic : require
#extension GL_KHR_shader_subgroup_ballot : require
layout(local_size_x = 32, local_size_y = 32, local_size_z = 1) in;

layout(set = 0, binding = 0, std140) uniform _98_100
{
    vec4 _m0;
    vec4 _m1;
    vec4 _m2;
    vec4 _m3;
    vec4 _m4;
    vec4 _m5;
    vec4 _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;
    float _m24;
    uint _m25;
    float _m26;
    float _m27;
    float _m28;
    float _m29[2];
} _100;

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

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

layout(set = 1, binding = 1) uniform TEXTURE_2D _291;
layout(set = 0, binding = 2) uniform sampler _295;
layout(set = 0, binding = 3) uniform TEXTURE_2D _311;
layout(set = 1, binding = 2, rg16f) uniform writeonly IMAGE_2D _376;
layout(set = 1, binding = 3, rg16f) uniform writeonly IMAGE_2D _695;

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;
shared vec3 _645[32];

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

float _17(inout float _15, bool _16)
{
    if (_16)
    {
        _15 += (float(_15 < _100._m7) * _100._m8);
    }
    float _118 = _15;
    return _100._m0.w / (_10(_118) + _100._m0.z);
}

vec3 _23(vec2 _22)
{
    return (vec3(_100._m9, _100._m10, _100._m11) + (vec3(_100._m12, _100._m13, _100._m14) * _22.x)) + (vec3(_100._m15, _100._m16, _100._m17) * _22.y);
}

vec3 _28(vec2 _26, float _27)
{
    vec3 _167 = _23(_26);
    return vec3(_100._m21, _100._m22, _100._m23) + (_167 * _27);
}

vec2 _46(vec2 _43, float _44, vec2 _45)
{
    if (_43.x < 1.0)
    {
        return _43;
    }
    else
    {
        float _222 = _44;
        bool _223 = false;
        float _224 = _17(_222, _223);
        float _220 = _224;
        vec3 _225 = _28(_45, _220);
        float _228 = 1.0 / (dot(_225, _100._m3.xyz) + _100._m3.w);
        vec2 _240 = vec2(dot(_225, _100._m4.xyz) + _100._m4.w, dot(_225, _100._m5.xyz) + _100._m5.w) * _228;
        return _45 - _240;
    }
}

float _32(vec2 _31)
{
    return dot(_31, _31);
}

vec2 _40(vec4 _38, inout vec2 _39)
{
    _39 *= _38.z;
    float _191 = inversesqrt(dot(_39, _39));
    float _200 = _38.w * _191;
    _39 *= _10(_200);
    _39 *= _38.xy;
    return vec2(_39);
}

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;
    vec2 _266 = (vec2(ivec2(GlobalInvocationID.xy * uvec2(2u))) + vec2(0.5)) * _283._m0.xy;
    vec4 _288 = TEXTURE_GATHER1(SAMPLER_2D(_291, _295), _266).wzxy;
    vec4 _302 = TEXTURE_GATHER(SAMPLER_2D(_291, _295), _266, 1).wzxy;
    vec4 _310 = vec4(1.0) - TEXTURE_GATHER1(SAMPLER_2D(_311, _295), _266).wzxy;
    vec2 _320 = _46(vec2(_288.x, _302.x), _310.x, _266);
    vec2 _330 = _46(vec2(_288.y, _302.y), _310.y, _266 + vec2(_283._m0.x, 0.0));
    float _344 = _32(_330);
    vec3 _347 = vec3(_320, _32(_320));
    vec3 _358;
    if (_347.z > _344)
    {
        _358 = _347;
    }
    else
    {
        _358 = vec3(_330, _344);
    }
    _347 = _358;
    if (_288.x >= 1.0)
    {
        IMAGE_STORE(_376, ivec2((GlobalInvocationID.xy * uvec2(2u)) + uvec2(0u)), vec4(_320, 0.0, 0.0));
    }
    if (_288.y >= 1.0)
    {
        IMAGE_STORE(_376, ivec2((GlobalInvocationID.xy * uvec2(2u)) + uvec2(1u, 0u)), vec4(_330, 0.0, 0.0));
    }
    vec2 _406 = _46(vec2(_288.z, _302.z), _310.z, _266 + vec2(0.0, _283._m0.y));
    _344 = _32(_406);
    vec3 _426;
    if (_347.z > _344)
    {
        _426 = _347;
    }
    else
    {
        _426 = vec3(_406, _344);
    }
    _347 = _426;
    if (_288.z >= 1.0)
    {
        IMAGE_STORE(_376, ivec2((GlobalInvocationID.xy * uvec2(2u)) + uvec2(0u, 1u)), vec4(_406, 0.0, 0.0));
    }
    vec2 _454 = _46(vec2(_288.w, _302.w), _310.w, _266 + _283._m0.xy);
    _344 = _32(_454);
    vec3 _474;
    if (_347.z > _344)
    {
        _474 = _347;
    }
    else
    {
        _474 = vec3(_454, _344);
    }
    _347 = _474;
    if (_288.w >= 1.0)
    {
        IMAGE_STORE(_376, ivec2((GlobalInvocationID.xy * uvec2(2u)) + uvec2(1u)), vec4(_454, 0.0, 0.0));
    }
    vec3 _502 = vec3(subgroupShuffleXor(_347.x, 1u), subgroupShuffleXor(_347.y, 1u), subgroupShuffleXor(_347.z, 1u));
    _347 = mix(_502, _347, bvec3(_347.z > _502.z));
    _502 = vec3(subgroupShuffleXor(_347.x, 2u), subgroupShuffleXor(_347.y, 2u), subgroupShuffleXor(_347.z, 2u));
    _347 = mix(_502, _347, bvec3(_347.z > _502.z));
    _502 = vec3(subgroupShuffleXor(_347.x, 4u), subgroupShuffleXor(_347.y, 4u), subgroupShuffleXor(_347.z, 4u));
    _347 = mix(_502, _347, bvec3(_347.z > _502.z));
    _502 = vec3(subgroupShuffleXor(_347.x, 8u), subgroupShuffleXor(_347.y, 8u), subgroupShuffleXor(_347.z, 8u));
    _347 = mix(_502, _347, bvec3(_347.z > _502.z));
    _502 = vec3(subgroupShuffleXor(_347.x, 16u), subgroupShuffleXor(_347.y, 16u), subgroupShuffleXor(_347.z, 16u));
    _347 = mix(_502, _347, bvec3(_347.z > _502.z));
    if (gl_SubgroupSize > 32u)
    {
        _502 = vec3(subgroupBroadcast(_347.x, 32u), subgroupBroadcast(_347.y, 32u), subgroupBroadcast(_347.z, 32u));
        _347 = mix(_502, _347, bvec3(_347.z > _502.z));
    }
    uint _629 = (gl_LocalInvocationID.x + (gl_LocalInvocationID.y * 32u)) / gl_SubgroupSize;
    uint _639 = 1024u / gl_SubgroupSize;
    _645[_629] = _347;
    memoryBarrier();
    barrier();
    if ((gl_LocalInvocationID.x + gl_LocalInvocationID.y) == 0u)
    {
        for (int _661 = 1; uint(_661) < _639; _661++)
        {
            _502 = _645[_661];
            _347 = mix(_502, _347, bvec3(_347.z > _502.z));
        }
        vec4 _686 = _100._m6;
        vec2 _689 = _347.xy;
        vec2 _692 = _40(_686, _689);
        _347 = vec3(_692.x, _692.y, _347.z);
        IMAGE_STORE(_695, ivec2(WorkGroupID.xy), vec4(_347.xy, 0.0, 0.0));
    }
}

