// Controls Temporal Sharpenning? Runs across multiple-frames!
// Can't be fixed in sequential frames
#version 450
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;

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

struct POST_PROCESSING_SSRBILATERAL_UP_SAMPLING_CONSTANT_BUFFER
{
    ivec4 _viViewportOrigin_Size;
    ivec4 _viSSRFringe;
    float _fMinExposure;
    int _iFrameIndex;
    int _iScaleNeo4kY;
    float _fInvUpsamplingFactor;
    vec4 _vfSrcTexelSize;
    int _iUseTileTexture;
    int _iVisualizeSSRFringe;
    int _iSSREnabledDebugFlag;
    float _fUpsamplingFactor;
    vec4 _vfFragCoordToSSRUVScaleBias;
};

layout(set = 0, binding = 52, std140) uniform dyn_CONSTANT_BUFFER_BLOCK_0
{
    POST_PROCESSING_SSRBILATERAL_UP_SAMPLING_CONSTANT_BUFFER _Upsample;
} _251;

layout(set = 0, binding = 0) uniform sampler2D InputSSR;
layout(set = 0, binding = 16, r32ui) uniform writeonly uimage2D OutputColor;
layout(set = 0, binding = 4) uniform sampler2D AvgLumMap;
layout(set = 0, binding = 6) uniform sampler2D TAAFlagsTexture;
layout(set = 0, binding = 2) uniform sampler2D InputSpecularFactor;
layout(set = 0, binding = 5) uniform sampler2D InputColor;
layout(set = 0, binding = 3) uniform sampler2D IBLFullResRGBE;
layout(set = 0, binding = 7) uniform usampler2D SSRTileTexture;

ivec2 _1393;

void main()
{
    for (;;)
    {
        ivec2 _292 = ivec2(gl_GlobalInvocationID.xy);
        if (_292.x >= _251._Upsample._viViewportOrigin_Size.z)
        {
            break;
        }
        int _302 = _292.y;
        if (_302 >= _251._Upsample._viViewportOrigin_Size.w)
        {
            break;
        }
        if (_251._Upsample._iVisualizeSSRFringe != 0)
        {
            ivec2 _324 = _292 >> ivec2(1);
            ivec2 _1221 = _1393;
            _1221.x = clamp(_324.x, _251._Upsample._viViewportOrigin_Size.x >> 1, ((_251._Upsample._viViewportOrigin_Size.x + _251._Upsample._viViewportOrigin_Size.z) >> 1) - 1);
            ivec2 _1224 = _1221;
            _1224.y = clamp(_324.y, _251._Upsample._viViewportOrigin_Size.y >> 1, ((_251._Upsample._viViewportOrigin_Size.y + _251._Upsample._viViewportOrigin_Size.w) >> 1) - 1);
            vec3 _852 = max(vec3(0.0), texelFetch(InputSSR, ivec2(vec2(_1224) * _251._Upsample._fInvUpsamplingFactor), 0).xyz);
            float _854 = _852.x;
            float _856 = _852.y;
            float _859 = _852.z;
            int _865 = max(931135488, (floatBitsToInt(max(max(_854, _856), _859)) & 2139095040));
            float _870 = uintBitsToFloat(uint(_865 + 125829120));
            imageStore(OutputColor, _292, uvec4(uint((((floatBitsToInt(uintBitsToFloat(uint(floatBitsToInt(_854) & (-32768))) + _870) | (floatBitsToInt(uintBitsToFloat(uint(floatBitsToInt(_856) & (-32768))) + _870) << 9)) & 262143) | (floatBitsToInt(uintBitsToFloat(uint(floatBitsToInt(_859) & (-32768))) + _870) << 18)) | ((_865 + (-931135488)) << 4)), 0u, 0u, 0u));
            break;
        }
        bool _1396 = (_302 > ((_251._Upsample._viViewportOrigin_Size.w - 1) - _251._Upsample._viSSRFringe.y)) ? true : false;
        ivec2 _370 = _292 + _251._Upsample._viViewportOrigin_Size.xy;
        float _1368;
        if (!_1396)
        {
            _1368 = float(int(texelFetch(TAAFlagsTexture, _370, 0).x * 255.0) & 15) * 0.066666670143604278564453125;
        }
        else
        {
            _1368 = 1.0;
        }
        vec4 _421 = texelFetch(InputSpecularFactor, _370, 0);
        vec4 _427 = texelFetch(InputColor, _370, 0);
        ivec2 _431 = _370 >> ivec2(1);
        if (_1396)
        {
            vec3 _942 = max(vec3(0.0), _427.xyz + ((texelFetch(IBLFullResRGBE, _370, 0).xyz * _421.xyz) * _427.w));
            float _944 = _942.x;
            float _946 = _942.y;
            float _949 = _942.z;
            int _955 = max(931135488, (floatBitsToInt(max(max(_944, _946), _949)) & 2139095040));
            float _960 = uintBitsToFloat(uint(_955 + 125829120));
            imageStore(OutputColor, _370, uvec4(uint((((floatBitsToInt(uintBitsToFloat(uint(floatBitsToInt(_944) & (-32768))) + _960) | (floatBitsToInt(uintBitsToFloat(uint(floatBitsToInt(_946) & (-32768))) + _960) << 9)) & 262143) | (floatBitsToInt(uintBitsToFloat(uint(floatBitsToInt(_949) & (-32768))) + _960) << 18)) | ((_955 + (-931135488)) << 4)), 0u, 0u, 0u));
            break;
        }
        ivec2 _507 = ivec2(vec2(gl_GlobalInvocationID.xy) * _251._Upsample._fInvUpsamplingFactor) / ivec2(16);
        bool _510 = _251._Upsample._iScaleNeo4kY != 1;
        ivec2 _1355;
        if (_510)
        {
            ivec2 _1278 = _507;
            _1278.y = _507.y >> 1;
            _1355 = _1278;
        }
        else
        {
            _1355 = _507;
        }
        bool _527 = _251._Upsample._iUseTileTexture == 0;
        bool _535;
        if (!_527)
        {
            _535 = (texelFetch(SSRTileTexture, _1355, 0).x & 64u) != 0u;
        }
        else
        {
            _535 = _527;
        }
        vec4 _1381;
        vec4 _1386;
        if (_535)
        {
            ivec2 _1363;
            if (_510)
            {
                ivec2 _1281 = _431;
                _1281.y = _431.y >> 1;
                _1363 = _1281;
            }
            else
            {
                _1363 = _431;
            }
            vec2 _1361;
            if (_510)
            {
                vec2 _1285 = vec2(0.5);
                _1285.x = 0.5 + (0.5 * float(_370.x & 1));
                vec2 _1289 = _1285;
                _1289.y = 0.5 + (0.25 * float(_370.y & 3));
                _1361 = _1289;
            }
            else
            {
                vec2 _1293 = vec2(0.5);
                _1293.x = 0.5 + (0.5 * float(_370.x & 1));
                vec2 _1297 = _1293;
                _1297.y = 0.5 + (0.5 * float(_370.y & 1));
                _1361 = _1297;
            }
            float _599 = (-1.0) + (2.0 * _251._Upsample._fUpsamplingFactor);
            vec2 _616 = (vec2(_1363) * _251._Upsample._fInvUpsamplingFactor) + (_1361 * _599);
            vec4 _618 = textureLod(InputSSR, _251._Upsample._vfSrcTexelSize.xy * _616, 0.0);
            vec2 offsets[3] = vec2[](vec2(1.0, 0.0), vec2(-1.0, 0.0), vec2(0.0, ((((_251._Upsample._iFrameIndex ^ _1363.x) ^ _1363.y) & 1) != 0) ? (-1.0) : 1.0));
            vec4 _1370;
            vec4 _1371;
            vec4 _1372;
            _1372 = _618;
            _1371 = _618;
            _1370 = _618;
            for (int _1369 = 0; _1369 < 3; )
            {
                vec4 _676 = textureLod(InputSSR, _251._Upsample._vfSrcTexelSize.xy * (_616 + (offsets[_1369] * _1368)), 0.0);
                vec4 _1304 = _1370;
                _1304.w = _1370.w + _676.w;
                _1372 = max(_1372, _676);
                _1371 = min(_1371, _676);
                _1370 = _1304;
                _1369++;
                continue;
            }
            vec4 _1307 = _1370;
            _1307.w = _1370.w * 0.25;
            vec4 _702 = vec4((((packHalf2x16(vec2(_618.w, 0.0)) & 65535u) & 1u) != 0u) ? 0.75 : 0.0);
            int _1375;
            if (_510)
            {
                _1375 = (_370.y ^ _251._Upsample._iFrameIndex) & 1;
            }
            else
            {
                _1375 = ((_370.x ^ _370.y) ^ _251._Upsample._iFrameIndex) & 1;
            }
            vec4 _737 = mix(mix(_1371, _1307, _702), mix(_1372, _1307, _702), bvec4(_1375 != 0));
            vec4 _1389;
            if (_251._Upsample._fUpsamplingFactor < 1.0)
            {
                _1389 = mix(textureLod(InputSSR, (vec2(_370) * _251._Upsample._vfFragCoordToSSRUVScaleBias.xy) + _251._Upsample._vfFragCoordToSSRUVScaleBias.zx, 0.0), _737, vec4(_599));
            }
            else
            { 
                _1389 = _737;
            }
            _1386 = _1389;
            _1381 = _1307;
        }
        else
        {
            _1386 = vec4(0.0);
            _1381 = vec4(0.0);
        }
        vec4 _764 = texelFetch(IBLFullResRGBE, _370, 0);
        float _1057 = max(0.001000000047497451305389404296875, max(_251._Upsample._fMinExposure, textureLod(AvgLumMap, vec2(0.5), 0.0).y));
        vec3 _794 = mix(_764.xyz * (_1057 * (1.0 / (1.0 + (max(max(_764.x, _764.y), _764.z) * _1057)))), _1386.xyz * (_1057 * (1.0 / (1.0 + (max(max(_1386.x, _1386.y), _1386.z) * _1057)))), vec3(clamp((_1381.w - 0.100000001490116119384765625) * 1.111111164093017578125, 0.0, 1.0)));
        vec3 _1134 = max(vec3(0.0), ((_421.xyz * _427.w) * (_794 * (1.0 / (_1057 * (1.0 - max(max(_794.x, _794.y), _794.z)))))) + _427.xyz);
        float _1136 = _1134.x;
        float _1138 = _1134.y;
        float _1141 = _1134.z;
        int _1147 = max(931135488, (floatBitsToInt(max(max(_1136, _1138), _1141)) & 2139095040));
        float _1152 = uintBitsToFloat(uint(_1147 + 125829120));
        //if ( vk3d_params.stereo.x > 0)
        imageStore(OutputColor, _370, uvec4(uint((((floatBitsToInt(uintBitsToFloat(uint(floatBitsToInt(_1136) & (-32768))) + _1152) | (floatBitsToInt(uintBitsToFloat(uint(floatBitsToInt(_1138) & (-32768))) + _1152) << 9)) & 262143) | (floatBitsToInt(uintBitsToFloat(uint(floatBitsToInt(_1141) & (-32768))) + _1152) << 18)) | ((_1147 + (-931135488)) << 4)), 0u, 0u, 0u));
        break;
    }
}




