// SOME SSAO STUFF
#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 = 1, binding = 0, std140) uniform _101_103
{
    vec4 _m0;
    vec4 _m1;
    vec4 _m2;
} _103;

layout(set = 0, binding = 0, std140) uniform _111_113
{
    vec4 _m0;
    vec4 _m1;
    vec4 _m2;
    vec4 _m3;
    vec4 _m4;
    vec4 _m5;
    vec4 _m6;
    vec4 _m7;
    vec4 _m8;
    vec4 _m9;
} _113;

layout(push_constant, std430) uniform _762_764
{
    int _m0;
    int _m1;
} _764;

layout(set = 0, binding = 1) uniform SAMPLER_2D _118;
layout(set = 1, binding = 1) uniform SAMPLER_2D _145;
layout(set = 1, binding = 2) uniform SAMPLER_2D _321;
layout(set = 1, binding = 3) uniform SAMPLER_2D _714;

layout(location = 0) out vec4 _754;

vec4 _36(SAMPLER_2D _33, ivec2 _34, int _35)
{
    return TEXEL_FETCH(_33, _34, _35);
}

float _18(float _15, float _16, float _17)
{
    return max(_15, max(_16, _17));
}

float _23(float _20, float _21, float _22)
{
    return min(_20, min(_21, _22));
}

vec4 _11(vec4 _10)
{
    return clamp(_10, vec4(0.0), vec4(1.0));
}

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

vec2 _50(ivec2 _49)
{
    vec2 _98 = vec2(_49) * _103._m0.zw;
    vec2 _110 = _113._m0.zw;
    float _117 = TEXTURE_LOD(_118, vec4(_98, 0.0, 0.0).xy, vec4(_98, 0.0, 0.0).w).x;
    float _136 = _117;
    vec4 _138 = _113._m1;
    float _141 = 1.0;
    bool _142 = true;
    float _133 = _45(_136, _138, _141, _142);
    vec2 _144 = TEXTURE_LOD(_145, vec4(_98, 0.0, 0.0).xy, vec4(_98, 0.0, 0.0).w).xy;
    vec3 _161 = mix(_113._m2.xyz, _113._m3.xyz, vec3(_98.x * _113._m0.z));
    vec3 _178 = mix(_113._m4.xyz, _113._m5.xyz, vec3(_98.x * _113._m0.z));
    vec3 _194 = mix(_178, _161, vec3(1.0 - (_98.y * _113._m0.w)));
    vec3 _206 = _113._m6.xyz + (_194 * _133);
    	// Frustum corners appear to be in _fa_freqLow[0,1,3,4], let's use 0 & 1 as the top two corners:
vec3 camera_horizontal_world = (_113._m3.xyz - _113._m2.xyz);
// In this case I think that the frustum is already a unit distance from the 
// camera, otherwise it would need to be rescaled.
// Find the width of the frustum using Pythagorus:
float frustum_width = sqrt(dot(camera_horizontal_world.xyz, camera_horizontal_world.xyz));
// Calculate the horizontal unit vector:
vec3 camera_horizontal_world_normalized = camera_horizontal_world / frustum_width;
// Calculate the stereo adjustment magnitude, using frustum_width/2 in place of
// tan(fov_horizontal / 2) normally obtained from the inverse projection matrix:
float adjustment_magnitude = vk3d_params[VIEW].stereo.x * (_133 - vk3d_params[VIEW].stereo.y) * frustum_width;
// World space adjustment is just the magitude * horizontal:
_206.xyz -= adjustment_magnitude * camera_horizontal_world_normalized;
    float _215 = 1.0 / ((_206.x * _113._m7.x) + ((_206.y * _113._m7.y) + ((_206.z * _113._m7.z) + _113._m7.w)));
    vec2 _238 = vec2((_206.x * _113._m8.x) + ((_206.y * _113._m8.y) + ((_206.z * _113._m8.z) + _113._m8.w)), (_206.x * _113._m9.x) + ((_206.y * _113._m9.y) + ((_206.z * _113._m9.z) + _113._m9.w))) * _215;
    vec2 _284;
    if (_144.x < 1.0)
    {
        _284 = _144;
    }
    else
    {
        _284 = _98 - (_238 * _113._m0.xy);
    }
    return _284;
}

void main()
{
    vec2 _302 = gl_FragCoord.xy * _103._m0.zw;
    ivec2 _311 = ivec2(gl_FragCoord.xy);
    vec4 _315 = vec4(0.0);
    vec4 _317 = vec4(0.0);
    vec4 _318 = vec4(0.0);
    vec4 _319 = vec4(0.0);
    ivec2 _322 = _311;
    int _324 = 0;
    vec4 _320 = _36(_321, _322, _324);
    ivec2 _331 = _311 + ivec2(-1, 0);
    int _332 = 0;
    vec4 _326 = _36(_321, _331, _332);
    ivec2 _338 = _311 + ivec2(1, 0);
    int _339 = 0;
    vec4 _334 = _36(_321, _338, _339);
    ivec2 _345 = _311 + ivec2(0, -1);
    int _346 = 0;
    vec4 _341 = _36(_321, _345, _346);
    ivec2 _352 = _311 + ivec2(0, 1);
    int _353 = 0;
    vec4 _348 = _36(_321, _352, _353);
    float _355 = _341.x;
    float _358 = _320.x;
    float _361 = _348.x;
    float _365 = _18(_355, _358, _361);
    float _366 = _326.x;
    float _369 = _334.x;
    float _373 = _341.y;
    float _376 = _320.y;
    float _379 = _348.y;
    float _383 = _18(_373, _376, _379);
    float _384 = _326.y;
    float _387 = _334.y;
    float _391 = _341.z;
    float _394 = _320.z;
    float _397 = _348.z;
    float _401 = _18(_391, _394, _397);
    float _402 = _326.z;
    float _405 = _334.z;
    vec3 _409 = vec3(_18(_365, _366, _369), _18(_383, _384, _387), _18(_401, _402, _405));
    _318 = vec4(_409.x, _409.y, _409.z, _318.w);
    float _412 = _341.x;
    float _415 = _320.x;
    float _418 = _348.x;
    float _422 = _23(_412, _415, _418);
    float _423 = _326.x;
    float _426 = _334.x;
    float _430 = _341.y;
    float _433 = _320.y;
    float _436 = _348.y;
    float _440 = _23(_430, _433, _436);
    float _441 = _326.y;
    float _444 = _334.y;
    float _448 = _341.z;
    float _451 = _320.z;
    float _454 = _348.z;
    float _458 = _23(_448, _451, _454);
    float _459 = _326.z;
    float _462 = _334.z;
    vec3 _466 = vec3(_23(_422, _423, _426), _23(_440, _441, _444), _23(_458, _459, _462));
    _319 = vec4(_466.x, _466.y, _466.z, _319.w);
    float _469 = _341.w;
    float _472 = _320.w;
    float _475 = _348.w;
    float _479 = _18(_469, _472, _475);
    float _480 = _326.w;
    float _483 = _334.w;
    _318.w = _18(_479, _480, _483);
    float _488 = _341.w;
    float _491 = _320.w;
    float _494 = _348.w;
    float _498 = _23(_488, _491, _494);
    float _499 = _326.w;
    float _502 = _334.w;
    _319.w = _23(_498, _499, _502);
    ivec2 _511 = _311 + ivec2(-1);
    int _512 = 0;
    vec4 _507 = _36(_321, _511, _512);
    ivec2 _518 = _311 + ivec2(1, -1);
    int _519 = 0;
    vec4 _514 = _36(_321, _518, _519);
    ivec2 _525 = _311 + ivec2(-1, 1);
    int _526 = 0;
    vec4 _521 = _36(_321, _525, _526);
    ivec2 _532 = _311 + ivec2(1);
    int _533 = 0;
    vec4 _528 = _36(_321, _532, _533);
    float _535 = _507.x;
    float _538 = _514.x;
    float _541 = _521.x;
    float _545 = _18(_535, _538, _541);
    float _546 = _528.x;
    float _549 = _318.x;
    float _553 = _507.y;
    float _556 = _514.y;
    float _559 = _521.y;
    float _563 = _18(_553, _556, _559);
    float _564 = _528.y;
    float _567 = _318.y;
    float _571 = _507.z;
    float _574 = _514.z;
    float _577 = _521.z;
    float _581 = _18(_571, _574, _577);
    float _582 = _528.z;
    float _585 = _318.z;
    vec3 _594 = (vec3(_18(_545, _546, _549), _18(_563, _564, _567), _18(_581, _582, _585)) + _318.xyz) * 0.5;
    _318 = vec4(_594.x, _594.y, _594.z, _318.w);
    float _597 = _507.x;
    float _600 = _514.x;
    float _603 = _521.x;
    float _607 = _23(_597, _600, _603);
    float _608 = _528.x;
    float _611 = _319.x;
    float _615 = _507.y;
    float _618 = _514.y;
    float _621 = _521.y;
    float _625 = _23(_615, _618, _621);
    float _626 = _528.y;
    float _629 = _319.y;
    float _633 = _507.z;
    float _636 = _514.z;
    float _639 = _521.z;
    float _643 = _23(_633, _636, _639);
    float _644 = _528.z;
    float _647 = _319.z;
    vec3 _655 = (vec3(_23(_607, _608, _611), _23(_625, _626, _629), _23(_643, _644, _647)) + _319.xyz) * 0.5;
    _319 = vec4(_655.x, _655.y, _655.z, _319.w);
    float _658 = _507.w;
    float _661 = _514.w;
    float _664 = _521.w;
    float _668 = _18(_658, _661, _664);
    float _669 = _528.w;
    float _672 = _318.w;
    _318.w = (_18(_668, _669, _672) + _318.w) * 0.5;
    float _681 = _507.w;
    float _684 = _514.w;
    float _687 = _521.w;
    float _691 = _23(_681, _684, _687);
    float _692 = _528.w;
    float _695 = _319.w;
    _319.w = (_23(_691, _692, _695) + _319.w) * 0.5;
    vec4 _704 = _320;
    _315 = _11(_704);
    ivec2 _711 = ivec2(gl_FragCoord.xy);
    vec2 _707 = _50(_711);
    vec4 _713 = TEXTURE_LOD(_714, vec4((_302 * _103._m1.xy) - _707, 0.0, 0.0).xy, vec4((_302 * _103._m1.xy) - _707, 0.0, 0.0).w);
    vec4 _747 = (_318 - _713) / (_318 - _319);
    vec4 _739 = _11(_747);
    // FIX
    //_713 = mix(_318, _319, _739);
    _713 = mix(_318, _318, _739);
    _754 = max(vec4(0.0), mix(_713, _315, vec4(_103._m2.x)));
}

