//Flickering lighting?
#version 450
#extension GL_EXT_nonuniform_qualifier : require

const vec4 _190[17] = vec4[](vec4(0.53634297847747802734375, 0.6246240139007568359375, 0.748866975307464599609375, 0.0), vec4(0.003173939883708953857421875, 0.00013482299982570111751556396484375, 3.7726898881373926997184753417969e-05, -2.0), vec4(0.010038600303232669830322265625, 0.000914679025299847126007080078125, 0.00027570201200433075428009033203125, -1.53125), vec4(0.014460899867117404937744140625, 0.00317269004881381988525390625, 0.001063990057446062564849853515625, -1.125), vec4(0.021630100905895233154296875, 0.00794618017971515655517578125, 0.00376990996301174163818359375, -0.78125), vec4(0.03473170101642608642578125, 0.0151084996759891510009765625, 0.00871982984244823455810546875, -0.5), vec4(0.057105600833892822265625, 0.028743200004100799560546875, 0.017284400761127471923828125, -0.28125), vec4(0.058241598308086395263671875, 0.0659959018230438232421875, 0.0411329008638858795166015625, -0.125), vec4(0.032446198165416717529296875, 0.06567180156707763671875, 0.0532821007072925567626953125, -0.03125), vec4(0.032446198165416717529296875, 0.06567180156707763671875, 0.0532821007072925567626953125, 0.03125), vec4(0.058241598308086395263671875, 0.0659959018230438232421875, 0.0411329008638858795166015625, 0.125), vec4(0.057105600833892822265625, 0.028743200004100799560546875, 0.017284400761127471923828125, 0.28125), vec4(0.03473170101642608642578125, 0.0151084996759891510009765625, 0.00871982984244823455810546875, 0.5), vec4(0.021630100905895233154296875, 0.00794618017971515655517578125, 0.00376990996301174163818359375, 0.78125), vec4(0.014460899867117404937744140625, 0.00317269004881381988525390625, 0.001063990057446062564849853515625, 1.125), vec4(0.010038600303232669830322265625, 0.000914679025299847126007080078125, 0.00027570201200433075428009033203125, 1.53125), vec4(0.003173939883708953857421875, 0.00013482299982570111751556396484375, 3.7726898881373926997184753417969e-05, 2.0));

struct MaterialData
{
    uvec4 paramIntData0;
    vec4 paramFloatData0;
    vec4 paramFloatData1;
    vec4 paramFloatData2;
    vec4 paramFloatData3;
    vec4 paramFloatData4;
    vec4 paramFloatData5;
    vec4 paramFloatData6;
    vec4 paramFloatData7;
    vec4 paramFloatData8;
    vec4 paramFloatData9;
    uvec4 paramImgIDs0;
    uvec4 paramImgIDs1;
    uvec4 paramImgIDs2;
};

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

layout(set = 0, binding = 11, std140) uniform PerView
{
    vec4 dynamicResolutionParameters;
} _228;

layout(set = 0, binding = 10, std430) readonly buffer materialDataBuffer
{
    MaterialData _materialDataBuffer[];
} _256;

layout(set = 0, binding = 100, std430) buffer DebugBuffer
{
    uint debugBuffer[];
} _377;

layout(set = 0, binding = 2) uniform sampler2D motionVectorsTex;
layout(set = 0, binding = 0) uniform sampler2D inputTex;
layout(set = 0, binding = 1) uniform sampler2D depthBufferTex;
layout(set = 0, binding = 3) uniform sampler2D albedoTex;
layout(set = 2, binding = 0) uniform texture2D globalTextures[];
layout(set = 2, binding = 0) uniform textureCube globalCubeTextures[];
layout(set = 2, binding = 0) uniform texture3D global3DTextures[];
layout(set = 1, binding = 0) uniform sampler linearClampSampler;
layout(set = 1, binding = 1) uniform sampler linearRepeatSampler;
layout(set = 1, binding = 2) uniform sampler nearestClampSampler;
layout(set = 1, binding = 3) uniform sampler nearestRepeatSampler;
layout(set = 1, binding = 4) uniform sampler shadowSampler;

layout(location = 0) in vec2 inUV0;
layout(location = 0) out vec4 outColor;

uint decodeMaterialId(float source)
{
    return packSnorm2x16(vec2(source, 0.0));
}

float deviceDepthToLinear(float deviceDepth, float near, float far)
{
    return -((far * near) / ((deviceDepth * (far - near)) + near));
}

vec4 SSSSBlurPS(vec2 texcoord, sampler2D colorTex, sampler2D depthTex, float sssWidth, vec2 dir, bool initStencil, float sssStrength, vec2 nearFarClip)
{
    vec4 colorM = texture(colorTex, texcoord);
    if (initStencil)
    {
        if (sssStrength == 0.0)
        {
            discard;
        }
    }
    float depthM = texture(depthTex, texcoord).x;
    float param = depthM;
    float param_1 = nearFarClip.x;
    float param_2 = nearFarClip.y;
    depthM = deviceDepthToLinear(param, param_1, param_2);
    float distanceToProjectionWindow = 1.73205077648162841796875;
    float scale = distanceToProjectionWindow / depthM;
    vec2 finalStep = dir * (sssWidth * scale);
    finalStep *= sssStrength;
    finalStep *= 0.3333333432674407958984375;
    vec4 colorBlurred = colorM;
    vec3 _114 = colorBlurred.xyz * vec3(0.53634297847747802734375, 0.6246240139007568359375, 0.748866975307464599609375);
    colorBlurred = vec4(_114.x, _114.y, _114.z, colorBlurred.w);
    for (int i = 1; i < 17; i++)
    {
        vec2 offset = texcoord + (finalStep * _190[i].w);
        vec4 color = texture(colorTex, offset);
        vec3 _214 = colorBlurred.xyz + (_190[i].xyz * color.xyz);
        colorBlurred = vec4(_214.x, _214.y, _214.z, colorBlurred.w);
    }
    return colorBlurred;
}

void main()
{
    vec2 sampleUv = inUV0;
    
    
    
    sampleUv *= _228.dynamicResolutionParameters.xy;
    vec4 motionVectors = texture(motionVectorsTex, sampleUv);
    float param = motionVectors.z;
    uint materialId = decodeMaterialId(param);
    uint _259 = clamp(materialId, 0u, 4096u);
    MaterialData materialData;
    materialData.paramIntData0 = _256._materialDataBuffer[_259].paramIntData0;
    materialData.paramFloatData0 = _256._materialDataBuffer[_259].paramFloatData0;
    materialData.paramFloatData1 = _256._materialDataBuffer[_259].paramFloatData1;
    materialData.paramFloatData2 = _256._materialDataBuffer[_259].paramFloatData2;
    materialData.paramFloatData3 = _256._materialDataBuffer[_259].paramFloatData3;
    materialData.paramFloatData4 = _256._materialDataBuffer[_259].paramFloatData4;
    materialData.paramFloatData5 = _256._materialDataBuffer[_259].paramFloatData5;
    materialData.paramFloatData6 = _256._materialDataBuffer[_259].paramFloatData6;
    materialData.paramFloatData7 = _256._materialDataBuffer[_259].paramFloatData7;
    materialData.paramFloatData8 = _256._materialDataBuffer[_259].paramFloatData8;
    materialData.paramFloatData9 = _256._materialDataBuffer[_259].paramFloatData9;
    materialData.paramImgIDs0 = _256._materialDataBuffer[_259].paramImgIDs0;
    materialData.paramImgIDs1 = _256._materialDataBuffer[_259].paramImgIDs1;
    materialData.paramImgIDs2 = _256._materialDataBuffer[_259].paramImgIDs2;
    if (!((materialData.paramIntData0.x & 256u) > 0u))
    {
        discard;
    }
    float sssStrength = materialData.paramFloatData8.y;
    float sssWidth = materialData.paramFloatData8.z;
    vec2 dir = vec2(0.0, 1.0);
    vec2 param_1 = sampleUv;
    float param_2 = sssWidth;
    vec2 param_3 = dir;
    bool param_4 = false;
    float param_5 = sssStrength;
    vec2 param_6 = vec2(5.0, 20000.0);
    
    //Fix here.
    //sampleUv.x -= vk3d_params.stereo.x *(1-vk3d_params.stereo.y*110.2)*0.5;
    
    vec4 _340 = SSSSBlurPS(param_1, inputTex, depthBufferTex, param_2, param_3, param_4, param_5, param_6);
    outColor = _340;
    vec3 albedo = texture(albedoTex, sampleUv).xyz;
    vec3 _351 = outColor.xyz * albedo;
    outColor = vec4(_351.x, _351.y, _351.z, outColor.w);
    outColor.w = 1.0;
}


