//Lighting PS 3.
#version 450
#extension GL_EXT_nonuniform_qualifier : require

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;
};

struct LightingData
{
    vec3 L;
    vec2 energyDiffuseSpecular;
    vec3 posWS;
    vec3 posWSa;
    vec3 posVS;
    vec3 cameraPosWS;
    vec3 V;
    vec3 N;
    vec3 NVtx;
    vec3 T;
    vec4 lightingWeights0;
    vec4 lightingWeights1;
    vec3 baseColor;
    float metalness;
    float specular;
    float roughness;
    float occlusion;
    float translucency;
    float ambientOcclusion;
    vec3 emissive;
    vec3 irradiance;
    vec4 materialBlendMask;
    uint materialId;
    MaterialData materialData;
    vec4 ssrSample;
    vec3 diffuseColor;
    vec3 specularColor;
    vec3 R;
    vec3 H;
    float NdL;
    float NdV;
    float NdH;
    float VdH;
    float LdH;
    float roughness2;
    float roughness4;
    vec3 posWSneighborX;
    vec3 posWSneighborY;
    float time;
    float depthFixup;
};

struct LightingDataDesc
{
    vec3 baseColor;
    float roughness;
    float metalness;
    float occlusion;
    float translucency;
    vec3 N;
    vec3 NVtx;
    float ambientOcclusion;
    vec3 emissive;
    vec3 irradiance;
    vec4 materialBlendMask;
    vec4 lightingWeights0;
    vec4 lightingWeights1;
    vec3 cameraPosWS;
    vec3 posWS;
    vec3 posWSa;
    vec3 posVS;
    uint materialId;
    MaterialData materialData;
};

struct SH9
{
    vec3 L[9];
};

struct FlrInput
{
    vec3 normal;
    vec3 binormal;
    vec4 tangent;
    vec2 uv0;
    vec3 posWS;
    vec4 pos;
    vec4 prevPos;
};

struct FlrOutput
{
    vec4 color;
    vec2 motionVector;
};

struct SH9Compressed
{
    uvec4 data0[2];
    uvec4 data1;
};

struct ExtendedLightData
{
    mat4 boxInverseWorldMatrix;
    mat4 viewProjectionMatrix;
    uvec4 params0;
    vec4 projectorTransform;
};

struct SH9Compressed_1
{
    uvec4 data0[2];
    uvec4 data1;
};

// 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 = 27, std140) uniform PerView
{
    mat4 invViewProjMatrix;
    mat4 invProjMatrix;
    vec4 nearFar;
    vec4 nearFarWidthHeight;
    vec4 cameraPositionWS;
    vec4 dirLightDirWS;
    vec4 dirLightColorAndIntens;
    uvec4 dirLightProjectorParams;
    vec4 dirLightProjectorTransform;
    mat4 dirLightProjectorViewProjMatrix;
    SH9Compressed skyModelSH;
    SH9Compressed globalBackgroundSH;
    mat4 pssmViewProjMatrices[5];
    mat4 localLightViewProjMatrices[16];
    vec4 shadowParameters0;
    vec4 playerWorldPosition;
    vec4 lightingWeights0;
    vec4 lightingWeights1;
    vec4 dynamicResolutionParameters;
    vec4 frameParameters0;
    vec4 frameParameters1;
    vec4 projectionOffsets;
    vec4 irradianceGridWorldPosition;
    vec4 irradianceGridHalfExtents;
    uvec4 irradianceGridResolution;
    mat4 viewMatrix;
    vec4 volumetricsParams2;
} _1828;

layout(set = 0, binding = 31, std140) uniform PerMaterial
{
    uvec4 paramIntData0;
    vec4 paramData0;
    vec4 paramData1;
    vec4 paramData2;
    vec4 paramData3;
    vec4 paramData4;
    vec4 paramData5;
    vec4 paramData6;
    vec4 paramData7;
    vec4 paramData8;
    vec4 paramData9;
    uvec4 paramImgIDs0;
    uvec4 paramImgIDs1;
    uvec4 paramImgIDs2;
} _2131;

layout(set = 0, binding = 30, std140) uniform PerInstance
{
    uvec4 params0;
    vec4 materialParams0;
    vec4 materialParams1;
    vec4 materialParams2;
    vec4 materialParams3;
} _2616;

layout(set = 0, binding = 41, std430) readonly buffer extendedLightBuffer
{
    ExtendedLightData _extendedLightBuffer[];
} _2621;

layout(set = 0, binding = 42, std430) readonly buffer irradianceGridBuffer
{
    SH9Compressed_1 _irradianceGridBuffer[];
} _2627;

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

layout(set = 2, binding = 0) uniform texture2D globalTextures[];
layout(set = 2, binding = 0) uniform textureCube globalCubeTextures[];
layout(set = 1, binding = 1) uniform sampler linearRepeatSampler;
layout(set = 0, binding = 40) uniform sampler2DArrayShadow shadowBufferTex;
layout(set = 0, binding = 20) uniform sampler2D albedoTex;
layout(set = 0, binding = 22) uniform sampler2D parameterTex;
layout(set = 0, binding = 21) uniform sampler2D normalTex;
layout(set = 1, binding = 0) uniform sampler linearClampSampler;
layout(set = 0, binding = 23) uniform sampler3D scatterBufferTex;
layout(set = 2, binding = 0) uniform texture3D global3DTextures[];
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 = 1) in vec2 inUV0;
layout(location = 0) in vec3 inNormal;
layout(location = 2) in vec4 inTangent;
layout(location = 3) in vec3 inBinormal;
layout(location = 4) in vec3 inPosWS;
layout(location = 6) in vec4 inPosition;
layout(location = 7) in vec4 inPrevPosition;
layout(location = 0) out vec4 outColor;
layout(location = 1) out vec4 outMotionVector;

vec3 decodeNormalMap(vec4 normalSample)
{
    vec2 _345 = (normalSample.wy - vec2(0.5)) * 2.0;
    vec3 normal;
    normal = vec3(_345.x, _345.y, normal.z);
    normal.z = sqrt(1.0 - ((normal.x * normal.x) - (normal.y * normal.y)));
    return normal;
}

float roughnessToMipIdx(float roughness)
{
    return max((max(roughness - 0.0500000007450580596923828125, 0.0) / 0.949999988079071044921875) * 7.0, 0.0);
}

vec4 unpackUNorm4x8(uint p_PackedValue)
{
    return vec4(float(p_PackedValue & 255u), float((p_PackedValue >> uint(8)) & 255u), float((p_PackedValue >> uint(16)) & 255u), float((p_PackedValue >> uint(24)) & 255u)) * 0.00392160005867481231689453125;
}

SH9 decodeSH9(vec4 data[7])
{
    SH9 result;
    result.L[0] = data[0].xyz;
    result.L[1] = vec3(data[0].w, data[1].xy);
    result.L[2] = vec3(data[1].zw, data[2].x);
    result.L[3] = data[2].yzw;
    result.L[4] = data[3].xyz;
    result.L[5] = vec3(data[3].w, data[4].xy);
    result.L[6] = vec3(data[4].zw, data[5].x);
    result.L[7] = data[5].yzw;
    result.L[8] = data[6].xyz;
    return result;
}

SH9 decodeSH9(uvec4 data0[2], uvec4 data1)
{
    uint param = data0[0].x;
    vec4 tempCoeff = (unpackUNorm4x8(param) * 2.0) - vec4(1.0);
    float tempCoeffLen = unpackHalf2x16(data1.x & 65535u).x;
    vec4 coeffs[7];
    coeffs[0] = tempCoeff * tempCoeffLen;
    uint param_1 = data0[0].y;
    tempCoeff = (unpackUNorm4x8(param_1) * 2.0) - vec4(1.0);
    tempCoeffLen = unpackHalf2x16((data1.x >> uint(16)) & 65535u).x;
    coeffs[1] = tempCoeff * tempCoeffLen;
    uint param_2 = data0[0].z;
    tempCoeff = (unpackUNorm4x8(param_2) * 2.0) - vec4(1.0);
    tempCoeffLen = unpackHalf2x16(data1.y & 65535u).x;
    coeffs[2] = tempCoeff * tempCoeffLen;
    uint param_3 = data0[0].w;
    tempCoeff = (unpackUNorm4x8(param_3) * 2.0) - vec4(1.0);
    tempCoeffLen = unpackHalf2x16((data1.y >> uint(16)) & 65535u).x;
    coeffs[3] = tempCoeff * tempCoeffLen;
    uint param_4 = data0[1].x;
    tempCoeff = (unpackUNorm4x8(param_4) * 2.0) - vec4(1.0);
    tempCoeffLen = unpackHalf2x16(data1.z & 65535u).x;
    coeffs[4] = tempCoeff * tempCoeffLen;
    uint param_5 = data0[1].y;
    tempCoeff = (unpackUNorm4x8(param_5) * 2.0) - vec4(1.0);
    tempCoeffLen = unpackHalf2x16((data1.z >> uint(16)) & 65535u).x;
    coeffs[5] = tempCoeff * tempCoeffLen;
    uint param_6 = data0[1].z;
    tempCoeff = (unpackUNorm4x8(param_6) * 2.0) - vec4(1.0);
    tempCoeffLen = unpackHalf2x16(data1.w & 65535u).x;
    coeffs[6] = tempCoeff * tempCoeffLen;
    vec4 param_7[7] = coeffs;
    return decodeSH9(param_7);
}

SH9 projectOntoSH9(vec3 n)
{
    SH9 sh;
    sh.L[0] = vec3(0.88622760772705078125);
    vec3 a1 = n.yzx * 0.48860299587249755859375;
    sh.L[1] = vec3(2.094399929046630859375) * a1.x;
    sh.L[2] = vec3(2.094399929046630859375) * a1.y;
    sh.L[3] = vec3(2.094399929046630859375) * a1.z;
    sh.L[4] = vec3(0.785398006439208984375) * ((1.09254801273345947265625 * n.x) * n.y);
    sh.L[5] = vec3(0.785398006439208984375) * ((1.09254801273345947265625 * n.y) * n.z);
    sh.L[6] = vec3(0.785398006439208984375) * (0.3153919875621795654296875 * (((3.0 * n.z) * n.z) - 1.0));
    sh.L[7] = vec3(0.785398006439208984375) * ((1.09254801273345947265625 * n.x) * n.z);
    sh.L[8] = vec3(0.785398006439208984375) * (0.546274006366729736328125 * ((n.x * n.x) - (n.y * n.y)));
    return sh;
}

vec3 shDotProduct(SH9 a, SH9 b)
{
    vec3 result = a.L[0] * b.L[0];
    result += (a.L[1] * b.L[1]);
    result += (a.L[2] * b.L[2]);
    result += (a.L[3] * b.L[3]);
    result += (a.L[4] * b.L[4]);
    result += (a.L[5] * b.L[5]);
    result += (a.L[6] * b.L[6]);
    result += (a.L[7] * b.L[7]);
    result += (a.L[8] * b.L[8]);
    return result;
}

vec3 sampleSH(uvec4 data0[2], uvec4 data1, vec3 dir)
{
    uvec4 param[2] = data0;
    uvec4 param_1 = data1;
    SH9 sh = decodeSH9(param, param_1);
    vec3 param_2 = dir;
    SH9 dirSH = projectOntoSH9(param_2);
    SH9 param_3 = dirSH;
    SH9 param_4 = sh;
    return max(shDotProduct(param_3, param_4), vec3(0.0));
}

void initLightingData(inout LightingData d, LightingDataDesc desc)
{
    d.L = vec3(0.0);
    d.V = vec3(0.0, 0.0, 1.0);
    d.energyDiffuseSpecular = vec2(0.0);
    d.diffuseColor = vec3(0.0);
    d.specularColor = vec3(0.0);
    d.R = vec3(0.0);
    d.H = vec3(0.0);
    d.NdL = 0.0;
    d.NdV = 0.0;
    d.NdH = 0.0;
    d.VdH = 0.0;
    d.LdH = 0.0;
    d.roughness2 = 0.0;
    d.roughness4 = 0.0;
    d.ssrSample = vec4(0.0);
    d.baseColor = desc.baseColor;
    d.roughness = desc.roughness;
    d.metalness = desc.metalness;
    d.occlusion = desc.occlusion;
    d.translucency = desc.translucency;
    d.materialBlendMask = desc.materialBlendMask;
    d.materialId = desc.materialId;
    d.materialData = desc.materialData;
    d.specular = desc.materialData.paramFloatData8.x;
    d.lightingWeights0 = desc.lightingWeights0;
    d.lightingWeights1 = desc.lightingWeights1;
    d.cameraPosWS = desc.cameraPosWS;
    d.posWS = desc.posWS;
    d.posWSa = desc.posWSa;
    d.posVS = desc.posVS;
    d.N = desc.N;
    d.NVtx = desc.NVtx;
    d.T = vec3(0.0);
    d.ambientOcclusion = desc.ambientOcclusion;
    d.emissive = desc.emissive;
    d.irradiance = desc.irradiance;
    d.posWSneighborX = vec3(0.0);
    d.posWSneighborY = vec3(0.0);
    d.time = 0.0;
    d.depthFixup = 1.0;
}

void initLightingDataFromGBuffer(inout LightingData d)
{
    float adjRough = (d.roughness * 0.949999988079071044921875) + 0.0500000007450580596923828125;
    d.roughness2 = adjRough * adjRough;
    d.roughness4 = d.roughness2 * d.roughness2;
    d.metalness = mix(0.0, d.metalness, d.lightingWeights1.y);
    d.diffuseColor = d.baseColor - (d.baseColor * d.metalness);
    d.specularColor = mix(vec3(d.specular) * 0.07999999821186065673828125, d.baseColor, vec3(d.metalness));
}

void calculateLightingDataBase(inout LightingData d)
{
    d.V = normalize(d.cameraPosWS - d.posWSa);
    d.NdV = clamp(abs(dot(d.N, d.V)) + 9.9999999392252902907785028219223e-09, 0.0, 1.0);
    d.R = reflect(-d.V, d.N);
    d.R = normalize(mix(d.N, d.R, vec3((1.0 - d.roughness2) * (sqrt(1.0 - d.roughness2) + d.roughness2))));
}

vec4 calcPosLS(vec3 posWS, mat4 shadowViewProjMatrix)
{
    vec4 posLS = shadowViewProjMatrix * vec4(posWS, 1.0);
    vec3 _654 = posLS.xyz / posLS.www;
    posLS = vec4(_654.x, _654.y, _654.z, posLS.w);
    vec2 _661 = (posLS.xy * 0.5) + vec2(0.5);
    posLS = vec4(_661.x, _661.y, posLS.z, posLS.w);
    return posLS;
}

uint findBestFittingSplit(vec3 posWS, inout vec4 posLS)
{
    for (uint i = 0u; i < uint(_1828.shadowParameters0.x); i++)
    {
        vec3 param = posWS;
        mat4 param_1 = _1828.pssmViewProjMatrices[i];
        posLS = calcPosLS(param, param_1);
        bool _1846 = all(lessThanEqual(posLS.xyz, vec3(1.0)));
        bool _1853;
        if (_1846)
        {
            _1853 = all(greaterThanEqual(posLS.xyz, vec3(0.0)));
        }
        else
        {
            _1853 = _1846;
        }
        if (_1853)
        {
            return i;
        }
    }
    return 4294967295u;
}

vec2 calcBiasedShadowUV(vec3 posWS, vec3 normalVtx, vec3 L, mat4 projMatrix, float factor)
{
    float weight = max(1.0 - max(dot(normalVtx, L), 0.0), 0.1500000059604644775390625);
    vec3 offsetWS = (normalVtx * weight) * factor;
    vec3 offsetPosWS = posWS + offsetWS;
    vec4 offsetPosLS = projMatrix * vec4(offsetPosWS, 1.0);
    vec2 offsetUV = ((offsetPosLS.xy / vec2(offsetPosLS.w)) * 0.5) + vec2(0.5);
    return offsetUV;
}

float sampleShadowMap(vec2 baseUv, float u, float v, vec2 shadowMapSizeInv, float depth, uint shadowMapIdx, sampler2DArrayShadow shadowBufferTex_1)
{
    vec2 uv = baseUv + (vec2(u, v) * shadowMapSizeInv);
    vec4 _686 = vec4(vec3(uv, float(shadowMapIdx)), depth);
    return texture(shadowBufferTex_1, vec4(_686.xyz, _686.w));
}

float pcf(vec3 shadowPos, uint shadowMapIdx, sampler2DArrayShadow shadowBufferTex_1)
{
    ivec3 size = textureSize(shadowBufferTex_1, 0);
    vec2 shadowMapSize;
    shadowMapSize.x = float(size.x);
    shadowMapSize.y = float(size.y);
    float elements = float(size.z);
    vec2 shadowMapSizeInv = vec2(1.0) / shadowMapSize;
    float lightDepth = shadowPos.z;
    vec2 uv = shadowPos.xy * shadowMapSize;
    vec2 baseUv;
    baseUv.x = floor(uv.x + 0.5);
    baseUv.y = floor(uv.y + 0.5);
    float s = (uv.x + 0.5) - baseUv.x;
    float t = (uv.y + 0.5) - baseUv.y;
    baseUv -= vec2(0.5);
    baseUv *= shadowMapSizeInv;
    float sum = 0.0;
    float uw0 = 3.0 - (2.0 * s);
    float uw1 = 1.0 + (2.0 * s);
    float u0 = ((2.0 - s) / uw0) - 1.0;
    float u1 = (s / uw1) + 1.0;
    float vw0 = 3.0 - (2.0 * t);
    float vw1 = 1.0 + (2.0 * t);
    float v0 = ((2.0 - t) / vw0) - 1.0;
    float v1 = (t / vw1) + 1.0;
    vec2 param = baseUv;
    float param_1 = u0;
    float param_2 = v0;
    vec2 param_3 = shadowMapSizeInv;
    float param_4 = lightDepth;
    uint param_5 = shadowMapIdx;
    sum += ((uw0 * vw0) * sampleShadowMap(param, param_1, param_2, param_3, param_4, param_5, shadowBufferTex_1));
    vec2 param_6 = baseUv;
    float param_7 = u1;
    float param_8 = v0;
    vec2 param_9 = shadowMapSizeInv;
    float param_10 = lightDepth;
    uint param_11 = shadowMapIdx;
    sum += ((uw1 * vw0) * sampleShadowMap(param_6, param_7, param_8, param_9, param_10, param_11, shadowBufferTex_1));
    vec2 param_12 = baseUv;
    float param_13 = u0;
    float param_14 = v1;
    vec2 param_15 = shadowMapSizeInv;
    float param_16 = lightDepth;
    uint param_17 = shadowMapIdx;
    sum += ((uw0 * vw1) * sampleShadowMap(param_12, param_13, param_14, param_15, param_16, param_17, shadowBufferTex_1));
    vec2 param_18 = baseUv;
    float param_19 = u1;
    float param_20 = v1;
    vec2 param_21 = shadowMapSizeInv;
    float param_22 = lightDepth;
    uint param_23 = shadowMapIdx;
    sum += ((uw1 * vw1) * sampleShadowMap(param_18, param_19, param_20, param_21, param_22, param_23, shadowBufferTex_1));
    return sum * 0.0625;
}

float calcFading(inout vec3 posLS)
{
    posLS = abs((posLS * 2.0) - vec3(1.0));
    float l = max(max(posLS.x, posLS.y), posLS.z);
    return smoothstep(0.89999997615814208984375, 1.0, l);
}

float calcDirShadowAttenuation(inout vec3 posWS, vec3 normalVtx, vec3 L, float shadowFactor)
{
    float shadowAttenuation = 1.0;
    posWS += (L * 6.0);
    vec3 param = posWS;
    vec4 param_1;
    uint _2012 = findBestFittingSplit(param, param_1);
    vec4 posLS = param_1;
    uint shadowMapIdx = _2012;
    if (shadowMapIdx != 4294967295u)
    {
        vec3 param_2 = posWS;
        vec3 param_3 = normalVtx;
        vec3 param_4 = L;
        mat4 param_5 = _1828.pssmViewProjMatrices[shadowMapIdx];
        float param_6 = 10.0 + ((10.0 * float(shadowMapIdx)) * 1.0);
        vec2 _2035 = calcBiasedShadowUV(param_2, param_3, param_4, param_5, param_6);
        posLS = vec4(_2035.x, _2035.y, posLS.z, posLS.w);
        vec3 param_7 = posLS.xyz;
        uint param_8 = shadowMapIdx;
        shadowAttenuation = pcf(param_7, param_8, shadowBufferTex);
        float shadowAttenuation2 = 1.0;
        vec3 param_9 = posLS.xyz;
        float _2050 = calcFading(param_9);
        float fadeSplits = _2050;
        uint nextShadowMapIdx = shadowMapIdx + 1u;
        if (nextShadowMapIdx < uint(_1828.shadowParameters0.x))
        {
            vec3 param_10 = posWS;
            mat4 param_11 = _1828.pssmViewProjMatrices[nextShadowMapIdx];
            vec4 tempPosLS = calcPosLS(param_10, param_11);
            vec3 param_12 = posWS;
            vec3 param_13 = normalVtx;
            vec3 param_14 = L;
            mat4 param_15 = _1828.pssmViewProjMatrices[nextShadowMapIdx];
            float param_16 = 10.0 + ((10.0 * float(shadowMapIdx)) * 1.0);
            vec2 _2085 = calcBiasedShadowUV(param_12, param_13, param_14, param_15, param_16);
            tempPosLS = vec4(_2085.x, _2085.y, tempPosLS.z, tempPosLS.w);
            vec3 param_17 = tempPosLS.xyz;
            uint param_18 = nextShadowMapIdx;
            shadowAttenuation2 = pcf(param_17, param_18, shadowBufferTex);
            shadowAttenuation = mix(shadowAttenuation, shadowAttenuation2, fadeSplits);
        }
        else
        {
            if (shadowMapIdx == (uint(_1828.shadowParameters0.x) - 1u))
            {
                vec3 param_19 = posWS;
                mat4 param_20 = _1828.pssmViewProjMatrices[shadowMapIdx];
                vec4 posLSLastSplit = calcPosLS(param_19, param_20);
                vec3 param_21 = posLSLastSplit.xyz;
                float _2119 = calcFading(param_21);
                float fadeGlobal = _2119;
                shadowAttenuation = mix(shadowAttenuation, 1.0, fadeGlobal);
            }
        }
    }
    return mix(1.0, shadowAttenuation, shadowFactor);
}

vec4 flrSampleLevelBindless(sampler samplerState, vec2 uv, float lod, uint textureIdx)
{
    return textureLod(sampler2D(globalTextures[nonuniformEXT(textureIdx)], samplerState), uv, lod);
}

vec3 calcDirProjectorAttenuation(vec3 posWS)
{
    vec3 att = vec3(1.0);
    uint projImageId = _1828.dirLightProjectorParams.x;
    if (projImageId != 4294967295u)
    {
        vec3 posLS = (_1828.dirLightProjectorViewProjMatrix * vec4(posWS, 1.0)).xyw;
        vec2 _1906 = posLS.xy / vec2(posLS.z);
        posLS = vec3(_1906.x, _1906.y, posLS.z);
        vec2 _1915 = posLS.xy * _1828.dirLightProjectorTransform.xy;
        posLS = vec3(_1915.x, _1915.y, posLS.z);
        vec2 _1930 = posLS.xy + ((_1828.dirLightProjectorTransform.zw * _1828.frameParameters0.x) * _1828.dirLightProjectorTransform.xy);
        posLS = vec3(_1930.x, _1930.y, posLS.z);
        vec2 _1937 = (posLS.xy * 0.5) + vec2(0.5);
        posLS = vec3(_1937.x, _1937.y, posLS.z);
        float projectorFactor = uintBitsToFloat(_1828.dirLightProjectorParams.y);
        vec2 param = posLS.xy;
        float param_1 = 0.0;
        uint param_2 = projImageId;
        att *= max(mix(vec3(1.0), flrSampleLevelBindless(linearRepeatSampler, param, param_1, param_2).xyz, vec3(projectorFactor)), vec3(0.0));
    }
    return att;
}

void calculateLDisc(inout LightingData d, float discRadius)
{
    float discR = sin(discRadius);
    float discD = cos(discRadius);
    float DdR = dot(d.L, d.R);
    vec3 S = d.R - (d.L * DdR);
    vec3 _1011;
    if (DdR < discD)
    {
        _1011 = normalize((d.L * discD) + (normalize(S) * discR));
    }
    else
    {
        _1011 = d.R;
    }
    d.L = _1011;
}

void calculateLightingData(inout LightingData d)
{
    d.H = normalize(d.L + d.V);
    d.NdH = dot(d.N, d.H);
    d.VdH = dot(d.V, d.H);
    d.LdH = dot(d.L, d.H);
}

float calcMicroShadowing(float AO, float NdL, float opacity)
{
    float aperture = (2.0 * AO) * AO;
    float microshadow = clamp((NdL + aperture) - 1.0, 0.0, 1.0);
    return mix(1.0, microshadow, opacity);
}

float diffuseLambert()
{
    return 1.0;
}

vec3 calcDiffuse(LightingData d)
{
    return (((d.diffuseColor * (d.lightingWeights0.x * d.energyDiffuseSpecular.x)) * 0.3183098733425140380859375) * diffuseLambert()) * d.NdL;
}

vec3 calcAOMultiBounce(vec3 ao, vec3 albedo)
{
    vec3 a = (albedo * 2.040400028228759765625) - vec3(0.3323999941349029541015625);
    vec3 b = (albedo * (-4.79510021209716796875)) + vec3(0.6417000293731689453125);
    vec3 c = (albedo * 2.755199909210205078125) + vec3(0.69029998779296875);
    return max(ao, ((((ao * a) + b) * ao) + c) * ao);
}

vec3 calcAOMultiBounce(float ao, vec3 albedo)
{
    vec3 param = vec3(ao);
    vec3 param_1 = albedo;
    return calcAOMultiBounce(param, param_1);
}

vec3 calcDiffuseOcclusionDirectLighting(float ssao, vec3 albedo, float intens)
{
    float param = ssao;
    vec3 param_1 = albedo;
    return mix(vec3(1.0), calcAOMultiBounce(param, param_1), vec3(intens));
}

float D_GGX(float NdH, float roughness4)
{
    float denom = ((NdH * NdH) * (roughness4 - 1.0)) + 1.0;
    return roughness4 / ((3.1415927410125732421875 * denom) * denom);
}

float V_Schlick(float NdL, float NdV, float roughness2)
{
    float k = roughness2 * 0.5;
    float V = (NdV * (1.0 - k)) + k;
    float L = (NdL * (1.0 - k)) + k;
    return 0.25 / (V * L);
}

vec3 F_Schlick(vec3 specularColor, float VdH)
{
    float fc = pow(abs(1.0 - VdH), 5.0);
    return vec3(clamp(50.0 * specularColor.y, 0.0, 1.0) * fc) + (specularColor * (1.0 - fc));
}

vec3 calcSpecular(LightingData d, float r2, float r4)
{
    float param = d.NdH;
    float param_1 = r4;
    float D = D_GGX(param, param_1) * d.energyDiffuseSpecular.y;
    float param_2 = d.NdL;
    float param_3 = d.NdV;
    float param_4 = r2;
    float V = V_Schlick(param_2, param_3, param_4);
    vec3 param_5 = d.specularColor;
    float param_6 = d.VdH;
    vec3 F = F_Schlick(param_5, param_6);
    return (F * ((d.lightingWeights0.y * D) * V)) * d.NdL;
}

vec4 decodeARGB8uint(uint colorU)
{
    return vec4(float((colorU & 16711680u) >> uint(16)) * 0.003921560011804103851318359375, float((colorU & 65280u) >> uint(8)) * 0.003921560011804103851318359375, float(colorU & 255u) * 0.003921560011804103851318359375, float((colorU & 4278190080u) >> uint(24)) * 0.003921560011804103851318359375);
}

vec3 gammaToLinear(vec3 color)
{
    return pow(clamp(color, vec3(0.0), vec3(1.0)), vec3(2.2000000476837158203125));
}

vec3 calcTranslucentLighting(vec3 N, vec3 L, vec3 V, float pixelTranslucency, float materialTranslucencyPower, float materialTranslucencyDistortion, vec3 materialTranslucencyColor)
{
    vec3 translucencyColor = materialTranslucencyColor * pixelTranslucency;
    vec3 sssLightVector = L + ((-N) * materialTranslucencyDistortion);
    float sssDot = exp2((clamp(dot(V, -sssLightVector), 0.0, 1.0) * materialTranslucencyPower) - materialTranslucencyPower);
    return translucencyColor * sssDot;
}

void applyTranslucentLighting(inout vec3 outLightingDiffuse, LightingData d)
{
    bool _1120 = (d.materialData.paramIntData0.x & 4u) > 0u;
    bool _1126;
    if (_1120)
    {
        _1126 = d.translucency > 0.0;
    }
    else
    {
        _1126 = _1120;
    }
    if (_1126)
    {
        float materialTranslucencyPower = d.materialData.paramFloatData7.x;
        float materialTranslucencyDistortion = d.materialData.paramFloatData7.y;
        float materialTranslucencyStrength = d.materialData.paramFloatData7.z;
        uint param = floatBitsToUint(d.materialData.paramFloatData7.w);
        vec3 param_1 = decodeARGB8uint(param).xyz;
        vec3 materialTranslucencyColor = gammaToLinear(param_1);
        vec3 param_2 = d.N;
        vec3 param_3 = d.L;
        vec3 param_4 = d.V;
        float param_5 = d.translucency;
        float param_6 = materialTranslucencyPower;
        float param_7 = materialTranslucencyDistortion;
        vec3 param_8 = materialTranslucencyColor;
        vec3 translucentLighting = calcTranslucentLighting(param_2, param_3, param_4, param_5, param_6, param_7, param_8);
        vec3 translucencyScalar = (((translucentLighting * materialTranslucencyStrength) * d.lightingWeights0.x) * d.energyDiffuseSpecular.x) * 0.3183098733425140380859375;
        outLightingDiffuse += translucencyScalar;
    }
}

void calcDirLightLighting(inout LightingData d, float energy, vec3 lightColor, vec3 L, inout vec3 outLighting0, inout vec3 outLighting1, vec4 diffuseSpecularWeights, vec3 shadowAttenuation)
{
    d.L = L;
    d.energyDiffuseSpecular = vec2(energy);
    LightingData param = d;
    float param_1 = 0.0670000016689300537109375;
    calculateLDisc(param, param_1);
    d = param;
    d.NdL = dot(d.N, d.L);
    LightingData param_2 = d;
    calculateLightingData(param_2);
    d = param_2;
    vec3 lightDiffuse = vec3(0.0);
    vec3 lightSpecular = vec3(0.0);
    if (d.NdL > 0.0)
    {
        float param_3 = d.occlusion;
        float param_4 = d.NdL;
        float param_5 = 0.75;
        vec3 attenuation = (shadowAttenuation * calcMicroShadowing(param_3, param_4, param_5)) * lightColor;
        LightingData param_6 = d;
        float param_7 = d.ambientOcclusion;
        vec3 param_8 = d.baseColor;
        float param_9 = 0.85000002384185791015625;
        lightDiffuse = (calcDiffuse(param_6) * attenuation) * calcDiffuseOcclusionDirectLighting(param_7, param_8, param_9);
        float r = max(d.roughness, 0.60000002384185791015625);
        float r2 = r * r;
        float r4 = r2 * r2;
        LightingData param_10 = d;
        float param_11 = r2;
        float param_12 = r4;
        lightSpecular = calcSpecular(param_10, param_11, param_12) * attenuation;
    }
    else
    {
        vec3 param_13 = lightDiffuse;
        LightingData param_14 = d;
        applyTranslucentLighting(param_13, param_14);
        lightDiffuse = param_13;
        lightDiffuse *= 0.100000001490116119384765625;
        lightDiffuse *= lightColor;
    }
    outLighting0 += ((lightDiffuse * diffuseSpecularWeights.x) + (lightSpecular * diffuseSpecularWeights.y));
    outLighting1 += ((lightDiffuse * diffuseSpecularWeights.z) + (lightSpecular * diffuseSpecularWeights.w));
}

vec4 flrSampleLevelBindlessCube(sampler samplerState, vec3 normal, float lod, uint textureIdx)
{
    return textureLod(samplerCube(globalCubeTextures[nonuniformEXT(textureIdx)], samplerState), normal, lod);
}

vec3 envBRDFApprox(vec3 specularColor, float roughness, float NdV)
{
    vec4 r = (vec4(-1.0, -0.0274999998509883880615234375, -0.572000026702880859375, 0.02199999988079071044921875) * roughness) + vec4(1.0, 0.0425000004470348358154296875, 1.03999996185302734375, -0.039999999105930328369140625);
    float a = (min(r.x * r.x, exp2((-9.27999973297119140625) * NdV)) * r.x) + r.y;
    vec2 AB = (vec2(-1.03999996185302734375, 1.03999996185302734375) * a) + r.zw;
    AB.y *= clamp(50.0 * specularColor.y, 0.0, 1.0);
    return (specularColor * AB.x) + vec3(AB.y);
}

vec2 getMotionVector(vec4 currPos, vec4 prevPos, vec4 projectionOffsets)
{
    vec2 currPosNDC = (currPos.xy / vec2(currPos.w)) + projectionOffsets.xy;
    vec2 prevPosNDC = (prevPos.xy / vec2(prevPos.w)) + projectionOffsets.zw;
    currPosNDC.y = 1.0 - currPosNDC.y;
    prevPosNDC.y = 1.0 - prevPosNDC.y;
    vec2 motionVector = (prevPosNDC - currPosNDC) * vec2(0.5, -0.5);
    motionVector = clamp(motionVector, vec2(-1.0), vec2(1.0));
    return motionVector;
}

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

float nativeDepthToVolumeZ(float depth, float invDepthExp, float depthRange, vec2 nearFar)
{
    float param = depth;
    float param_1 = nearFar.x;
    float param_2 = nearFar.y;
    float depthLinear = -deviceDepthToLinear(param, param_1, param_2);
    return pow(clamp((depthLinear - nearFar.x) / depthRange, 0.0, 1.0) + 9.9999999392252902907785028219223e-09, invDepthExp);
}

vec3 cellCenterSSToUVW(inout vec3 cellCenter, float invDepthExp, float depthRange, vec2 nearFar)
{
    float param = cellCenter.z;
    float param_1 = invDepthExp;
    float param_2 = depthRange;
    vec2 param_3 = nearFar;
    cellCenter.z = nativeDepthToVolumeZ(param, param_1, param_2, param_3);
    return cellCenter;
}

void flrMain(FlrInput flrInput, inout FlrOutput flrOutput)
{
    vec2 uvOffset = _2131.paramData0.xy;
    if ((_2131.paramIntData0.x & 8u) > 0u)
    {
        uvOffset = _2131.paramData0.xy * _1828.frameParameters0.x;
    }
    vec2 uv = flrInput.uv0 + uvOffset;
    vec4 albedo = texture(albedoTex, uv);
    vec4 parameter = texture(parameterTex, uv);
    vec4 param = texture(normalTex, uv);
    vec3 normal = decodeNormalMap(param);
    normal = normalize(((flrInput.tangent.xyz * normal.x) + (flrInput.binormal * normal.y)) + (flrInput.normal * normal.z));
    vec3 viewDirWS = flrInput.posWS - _1828.cameraPositionWS.xyz;
    vec3 V = normalize(viewDirWS);
    vec3 N = normal;
    vec3 R = reflect(V, N);
    float roughness = clamp((parameter.x * clamp(_2131.paramData0.w, 0.0, 1.0)) + clamp(_2131.paramData0.w - 1.0, 0.0, 1.0), 0.0, 1.0);
    float param_1 = roughness;
    float specMipIdx = roughnessToMipIdx(param_1);
    uint specularIdToUse = _2131.paramImgIDs0.w;
    vec3 irrad = vec3(0.0);
    if ((_2131.paramIntData0.x & 1u) > 0u)
    {
        specularIdToUse = _1828.irradianceGridResolution.w;
        uvec4 param_2[2];
        param_2[0] = _1828.globalBackgroundSH.data0[0];
        param_2[1] = _1828.globalBackgroundSH.data0[1];
        uvec4 param_3 = _1828.globalBackgroundSH.data1;
        vec3 param_4 = N;
        irrad = sampleSH(param_2, param_3, param_4);
    }
    else
    {
        uvec4 param_5[2];
        param_5[0] = _1828.skyModelSH.data0[0];
        param_5[1] = _1828.skyModelSH.data0[1];
        uvec4 param_6 = _1828.skyModelSH.data1;
        vec3 param_7 = N;
        irrad = sampleSH(param_5, param_6, param_7);
    }
    vec3 outLighting0 = vec3(0.0);
    vec3 outLighting1 = vec3(0.0);
    if ((_2131.paramIntData0.x & 2u) > 0u)
    {
        LightingDataDesc desc;
        desc.lightingWeights0 = _1828.lightingWeights0;
        desc.lightingWeights1 = _1828.lightingWeights1;
        desc.baseColor = albedo.xyz;
        desc.roughness = roughness;
        desc.metalness = parameter.y;
        desc.occlusion = parameter.z;
        desc.translucency = parameter.w;
        desc.materialBlendMask = vec4(1.0);
        desc.materialId = 0u;
        desc.materialData.paramIntData0 = uvec4(0u);
        desc.materialData.paramFloatData0 = vec4(0.0);
        desc.materialData.paramFloatData1 = vec4(0.0);
        desc.materialData.paramFloatData2 = vec4(0.0);
        desc.materialData.paramFloatData3 = vec4(0.0);
        desc.materialData.paramFloatData4 = vec4(0.0);
        desc.materialData.paramFloatData5 = vec4(0.0);
        desc.materialData.paramFloatData6 = vec4(0.0);
        desc.materialData.paramFloatData7 = vec4(0.0);
        desc.materialData.paramFloatData8 = vec4(0.0);
        desc.materialData.paramFloatData9 = vec4(0.0);
        desc.materialData.paramImgIDs0 = uvec4(0u);
        desc.materialData.paramImgIDs1 = uvec4(0u);
        desc.materialData.paramImgIDs2 = uvec4(0u);
        desc.cameraPosWS = _1828.cameraPositionWS.xyz;
        desc.posWS = flrInput.posWS;
        
        //Fix here.
        vec3 posWSa = flrInput.posWS;
        if (vk3d_params.custom_params.x!=1) {
            posWSa.x -= vk3d_params.stereo.x * vk3d_params.stereo.y*_1828.viewMatrix[0].x*_1828.invProjMatrix[0].x;
            posWSa.y -= vk3d_params.stereo.x * vk3d_params.stereo.y*_1828.viewMatrix[1].x*_1828.invProjMatrix[0].x;
            posWSa.z -= vk3d_params.stereo.x * vk3d_params.stereo.y*_1828.viewMatrix[2].x*_1828.invProjMatrix[0].x;
        }
        desc.posWSa = posWSa;
        
        desc.posVS = (_1828.viewMatrix * vec4(flrInput.posWS, 1.0)).xyz;
        desc.N = N;
        desc.NVtx = flrInput.normal;
        desc.ambientOcclusion = 1.0;
        desc.emissive = vec3(0.0);
        desc.irradiance = vec3(0.0);
        LightingData d;
        LightingData param_8 = d;
        LightingDataDesc param_9 = desc;
        initLightingData(param_8, param_9);
        d = param_8;
        desc = param_9;
        d.T = flrInput.tangent.xyz;
        d.specular = 0.5;
        LightingData param_10 = d;
        initLightingDataFromGBuffer(param_10);
        d = param_10;
        LightingData param_11 = d;
        calculateLightingDataBase(param_11);
        d = param_11;
        vec3 shadowBiasNormal = d.NVtx;
        float shadowFactor = _1828.dirLightDirWS.w;
        vec3 param_12 = d.posWS;
        vec3 param_13 = shadowBiasNormal;
        vec3 param_14 = _1828.dirLightDirWS.xyz;
        float param_15 = shadowFactor;
        float _2376 = calcDirShadowAttenuation(param_12, param_13, param_14, param_15);
        vec3 param_16 = flrInput.posWS;
        vec3 dirLightAttenuation = vec3(_2376) * calcDirProjectorAttenuation(param_16);
        LightingData param_17 = d;
        float param_18 = _1828.dirLightColorAndIntens.w;
        vec3 param_19 = _1828.dirLightColorAndIntens.xyz;
        vec3 param_20 = _1828.dirLightDirWS.xyz;
        vec3 param_21 = outLighting0;
        vec3 param_22 = outLighting1;
        vec4 param_23 = vec4(1.0, 1.0, 0.0, 0.0);
        vec3 param_24 = dirLightAttenuation;
        calcDirLightLighting(param_17, param_18, param_19, param_20, param_21, param_22, param_23, param_24);
        d = param_17;
        outLighting0 = param_21;
        outLighting1 = param_22;
        vec3 specular = vec3(0.0);
        vec3 param_25 = d.R;
        float param_26 = specMipIdx;
        uint param_27 = specularIdToUse;
        vec3 specSample = flrSampleLevelBindlessCube(linearClampSampler, param_25, param_26, param_27).xyz;
        vec3 param_28 = d.specularColor;
        float param_29 = d.roughness;
        float param_30 = d.NdV;
        specular = specSample * envBRDFApprox(param_28, param_29, param_30);
        irrad *= ((d.diffuseColor * (_1828.lightingWeights0.z * min(d.occlusion, d.ambientOcclusion))) / vec3(3.1415927410125732421875));
        specular *= _1828.lightingWeights0.w;
        outLighting0 += ((irrad * 1.0) + (specular * 1.0));
        outLighting1 += ((irrad * 0.0) + (specular * 0.0));
        vec3 _2467 = outLighting0 + outLighting1;
        flrOutput.color = vec4(_2467.x, _2467.y, _2467.z, flrOutput.color.w);
        flrOutput.color.w = albedo.w;
    }
    else
    {
        flrOutput.color = albedo;
        vec3 param_31 = R;
        float param_32 = specMipIdx;
        uint param_33 = specularIdToUse;
        vec3 envmapColor = flrSampleLevelBindlessCube(linearClampSampler, param_31, param_32, param_33).xyz;
        vec3 _2496 = flrOutput.color.xyz + ((envmapColor * _2131.paramData0.z) * parameter.z);
        flrOutput.color = vec4(_2496.x, _2496.y, _2496.z, flrOutput.color.w);
    }
    vec4 param_34 = flrInput.pos;
    vec4 param_35 = flrInput.prevPos;
    vec4 param_36 = _1828.projectionOffsets;
    flrOutput.motionVector = getMotionVector(param_34, param_35, param_36);
    vec3 screenUV = flrInput.pos.xyz / vec3(flrInput.pos.w);
    vec2 _2523 = (screenUV.xy * 0.5) + vec2(0.5);
    screenUV = vec3(_2523.x, _2523.y, screenUV.z);
    vec3 param_37 = screenUV;
    float param_38 = _1828.volumetricsParams2.y;
    float param_39 = _1828.volumetricsParams2.z;
    vec2 param_40 = _1828.nearFar.xy;
    
    //Fix here.
    param_37.x -= vk3d_params.stereo.x *(1-vk3d_params.stereo.y*param_37.z*0.0)*0.5;
    
    vec3 _2539 = cellCenterSSToUVW(param_37, param_38, param_39, param_40);
    vec3 scatteringUVW = _2539;
    vec4 volSample = textureLod(scatterBufferTex, scatteringUVW, 0.0);
    float weight = (1.0 - volSample.w) * _1828.volumetricsParams2.w;
    vec3 _2562 = mix(flrOutput.color.xyz, volSample.xyz, vec3(weight));
    flrOutput.color = vec4(_2562.x, _2562.y, _2562.z, flrOutput.color.w);
}

void main()
{
    FlrInput flrInput;
    flrInput.uv0 = inUV0;
    flrInput.normal = inNormal;
    flrInput.tangent = inTangent;
    flrInput.binormal = inBinormal;
    flrInput.posWS = inPosWS;
    flrInput.pos = inPosition;
    flrInput.prevPos = inPrevPosition;
    FlrInput param = flrInput;
    FlrOutput param_1;
    flrMain(param, param_1);
    FlrOutput flrOutput = param_1;
    outColor = flrOutput.color;
    outMotionVector = vec4(flrOutput.motionVector.x, flrOutput.motionVector.y, outMotionVector.z, outMotionVector.w);
    outMotionVector = vec4(outMotionVector.x, outMotionVector.y, vec2(0.0).x, vec2(0.0).y);
}


