// Some decals
#version 450

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

struct lightingInput_t
{
    vec3 albedo;
    vec3 colorMask;
    vec3 specular;
    float smoothness;
    float maskSSS;
    float thickness;
    vec3 normal;
    vec3 normalTS;
    vec3 normalSSS;
    vec3 lightmap;
    vec3 emissive;
    float alpha;
    float ssdoDiffuseMul;
    vec3 view;
    vec3 position;
    vec4 texCoords;
    vec4 fragCoord;
    mat3 invTS;
    vec3 ambient_lighting;
    vec3 diffuse_lighting;
    vec3 specular_lighting;
    vec3 output_lighting;
    uint albedo_packed;
    uint specular_packed;
    uint diffuse_lighting_packed;
    uint specular_lighting_packed;
    vec3 transmitance;
    vec3 envprobes_lighting;
    uvec4 ticksDecals;
    uvec4 ticksProbes;
    uvec4 ticksLights;
    vec3 debugMaterialBlend;
    vec3 debugProbeAssignment;
    ivec3 debugClusterAssignment;
    vec4 ambientSH0;
    vec4 ambientSH1;
    vec4 ambientSH2;
    float sunVolumeShadow;
    uint deferredFlags;
};

struct clusternumlights_t
{
    int offset;
    int numItems;
};

struct light_t
{
    vec3 pos;
    uint lightParms;
    vec4 posShadow;
    vec4 falloffR;
    vec4 projS;
    vec4 projT;
    vec4 projQ;
    uvec4 scaleBias;
    vec4 boxMin;
    vec4 boxMax;
    vec4 areaPlane;
    uint lightParms2;
    uint colorPacked;
    float specMultiplier;
    uint shadowBleedReduce_lightScattering;
};

struct shadowparms_t
{
    vec4 shadowLightS;
    vec4 shadowLightT;
    vec4 shadowLightR;
    vec4 shadowLightQ;
    vec4 shadowAtlasScaleBias;
};

layout(set = 0, binding = 0, std140) uniform _viewUniforms_ubo
{
    vec4 viewprojectionmatrixx;
    vec4 viewprojectionmatrixy;
    vec4 viewprojectionmatrixz;
    vec4 viewprojectionmatrixw;
    vec4 ambientviewparms;
    vec4 globalvieworigin;
    vec4 projectionmatrixz;
    vec4 clusteredlighting;
    vec4 envirradiancescale;
    vec4 sungoboscaleoffset;
    vec4 parallellightslicescale;
    vec4 nummultisamples;
    vec4 randomint;
    vec4 shadowatlasresolution;
} _viewUniforms;

layout(set = 1, binding = 0, std140) uniform _instanceUniforms_ubo
{
    vec4 modelmatrixx;
    vec4 modelmatrixy;
    vec4 modelmatrixz;
    vec4 particlestscalebias;
    vec4 modelmatrixw;
    vec4 ambientinstanceparms;
    vec4 staticmodel;
    vec4 instanceirradiancescale;
    vec4 particleshadowdisable;
    vec4 particletranslucencyscale;
} _instanceUniforms;

layout(set = 0, binding = 3, std430) restrict readonly buffer clusternumlights_b
{
    clusternumlights_t clusternumlights[];
} _483;

layout(set = 0, binding = 4, std430) restrict readonly buffer clusterlightsid_b
{
    uint clusterlightsid[];
} _631;

layout(set = 0, binding = 5, std140) uniform lightparms1_ubo
{
    vec4 lightparms1[4096];
} _659;

layout(set = 0, binding = 6, std140) uniform lightparms2_ubo
{
    vec4 lightparms2[4096];
} _698;

layout(set = 0, binding = 7, std140) uniform lightparms3_ubo
{
    vec4 lightparms3[3072];
} _767;

layout(set = 0, binding = 9, std140) uniform shadowsparmsubo_ubo
{
    vec4 shadowsparmsubo[4095];
} _1430;

layout(push_constant, std430) uniform PushConstants
{
    int lodLevel;
    int modelIndex;
} pushConstants;

layout(set = 2, binding = 1) uniform sampler2D samp_transmap;
layout(set = 0, binding = 8) uniform sampler2D samp_lightsatlasmap;
layout(set = 0, binding = 10) uniform sampler2DShadow samp_shadowatlasdepthcomparemap;
layout(set = 0, binding = 1) uniform sampler3D samp_ambientatlas;
layout(set = 0, binding = 2) uniform sampler3D samp_ambientshatlas;

layout(location = 2) in vec4 vofi_TexCoord0;
layout(location = 5) in vec4 vofi_TexCoord3;
layout(location = 7) in vec4 vofi_TexCoord5;
layout(location = 4) in vec4 vofi_TexCoord2;
layout(location = 6) in vec4 vofi_TexCoord4;
layout(location = 10) in vec4 vofi_TexCoord8;
layout(location = 11) in vec4 vofi_TexCoord9;
layout(location = 12) in vec4 vofi_TexCoord10;
layout(location = 0) out vec4 out_FragColor0;

float saturate(float v)
{
    return clamp(v, 0.0, 1.0);
}

float GetLinearDepth(float ndcZ, vec4 projectionMatrixZ, float rcpFarZ, bool bFirstPersonArmsRescale)
{
    float linearZ = projectionMatrixZ.w / (ndcZ + projectionMatrixZ.z);
    return linearZ * rcpFarZ;
}

uint asuint(float x)
{
    return floatBitsToUint(x);
}

float fmin3(float f1, float f2, float f3)
{
    return min(f1, min(f2, f3));
}

float fmax3(float f1, float f2, float f3)
{
    return max(f1, max(f2, f3));
}

vec4 unpackR15G15B15A15(uvec2 value)
{
    return vec4(float((value.x >> uint(16)) & 65535u), float(value.x & 65535u), float((value.y >> uint(16)) & 65535u), float(value.y & 65535u)) * 3.0518509447574615478515625e-05;
}

float FastSign(float x)
{
    return (step(0.0, x) * 2.0) - 1.0;
}

vec3 unpackR11G11B10(uint value)
{
    return vec3(float((value >> uint(21)) & 2047u), float((value >> uint(10)) & 2047u), float(value & 1023u)) / vec3(2047.0, 2047.0, 1023.0);
}

uint getCubeMapFaceID(float x, float y, float z)
{
    bool _98 = abs(z) >= abs(x);
    bool _106;
    if (_98)
    {
        _106 = abs(z) >= abs(y);
    }
    else
    {
        _106 = _98;
    }
    if (_106)
    {
        return uint((z < 0.0) ? 5 : 4);
    }
    if (abs(y) >= abs(x))
    {
        return uint((y < 0.0) ? 3 : 2);
    }
    return uint(int(x < 0.0));
}

vec4 unpackR8G8B8A8(uint value)
{
    return unpackUnorm4x8(value).wzyx;
}

float ApproxExp2(float f)
{
    return exp2(f);
}

vec3 unpackRGBE(uint value)
{
    uint param = value;
    vec4 rgbe = unpackR8G8B8A8(param);
    float param_1 = (rgbe.w * 255.0) - 128.0;
    return rgbe.xyz * ApproxExp2(param_1);
}

void main()
{
    vec4 curr_frame = texture(samp_transmap, vofi_TexCoord0.xy);
    vec3 texture_normal = (curr_frame.xyz * vec3(2.0)) - vec3(1.0);
    vec2 _262 = mix(vec2(0.0), texture_normal.xy, vec2(curr_frame.w));
    texture_normal = vec3(_262.x, _262.y, texture_normal.z);
    float param = dot(texture_normal.xy, texture_normal.xy);
    texture_normal.z = sqrt(1.0 - saturate(param));
    vec3 n = vofi_TexCoord3.xyz;
    vec3 t = vofi_TexCoord5.xyz;
    vec3 b = cross(n, t);
    vec3 normal = normalize(((t * texture_normal.x) + (b * texture_normal.y)) + (n * texture_normal.z));
    vec3 position = vofi_TexCoord2.xyz;
    vec4 fragCoord = vofi_TexCoord4 / vec4(vofi_TexCoord4.w);
    vec2 _320 = (fragCoord.xy * 0.5) + vec2(0.5);
    fragCoord = vec4(_320.x, _320.y, fragCoord.z, fragCoord.w);
    fragCoord.y = 1.0 - fragCoord.y;
    float opacity = 1.0;
    float smoothness = 0.5;
    vec3 albedo = vec3(1.0);
    vec3 specular = vec3(0.0);
    lightingInput_t lightInput = lightingInput_t(vec3(0.0), vec3(0.0), vec3(0.0), 0.0, 0.0, 0.0, vec3(0.0), vec3(0.0, 0.0, 1.0), vec3(0.0, 0.0, 1.0), vec3(0.0), vec3(0.0), 1.0, 1.0, vec3(0.0), vec3(0.0), vec4(0.0), vec4(0.0), mat3(vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0)), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), 0u, 0u, 0u, 0u, vec3(0.0), vec3(0.0), uvec4(0u), uvec4(0u), uvec4(0u), vec3(0.0), vec3(0.0), ivec3(0), vec4(0.0), vec4(0.0), vec4(0.0), 1.0, 0u);
    lightInput.albedo = albedo;
    lightInput.smoothness = smoothness;
    lightInput.specular = specular;
    lightInput.alpha = opacity;
    lightInput.fragCoord = fragCoord;
    lightInput.normal = normal;
    lightInput.position = position;
    lightInput.view = normalize(_viewUniforms.globalvieworigin.xyz - position);
    lightInput.ambientSH0 = vofi_TexCoord8;
    lightInput.ambientSH1 = vofi_TexCoord9;
    lightInput.ambientSH2 = vofi_TexCoord10;
    uint skipStaticModel = uint((_instanceUniforms.staticmodel.x > 0.0) ? 8 : 0);
    float NUM_CLUSTERS_X = 16.0;
    float NUM_CLUSTERS_Y = 8.0;
    float NUM_CLUSTERS_Z = 24.0;
    vec3 clusterCoordinate;
    clusterCoordinate = vec3(lightInput.fragCoord.xy.x, lightInput.fragCoord.xy.y, clusterCoordinate.z);
    clusterCoordinate.y = 1.0 - clusterCoordinate.y;

  // FIX FOR DECALS CLIPPING
   clusterCoordinate.x -= vk3d_params.stereo.x * 0.5;


    vec2 _420 = floor(clusterCoordinate.xy * vec2(NUM_CLUSTERS_X, NUM_CLUSTERS_Y));
    clusterCoordinate = vec3(_420.x, _420.y, clusterCoordinate.z);
    float param_1 = lightInput.fragCoord.z;
    vec4 param_2 = _viewUniforms.projectionmatrixz;
    float param_3 = 1.0;
    bool param_4 = false;
    float curr_z = GetLinearDepth(param_1, param_2, param_3, param_4);
    float slice = log2(max(1.0, curr_z / _viewUniforms.clusteredlighting.z)) * _viewUniforms.clusteredlighting.w;
    clusterCoordinate.z = min(NUM_CLUSTERS_Z - 1.0, floor(slice));
    uint clusterOffset = uint((clusterCoordinate.x + (clusterCoordinate.y * NUM_CLUSTERS_X)) + ((clusterCoordinate.z * NUM_CLUSTERS_X) * NUM_CLUSTERS_Y));
    uint lightsMin = 0u;
    uint lightsMax = 0u;
    uint decalsMin = 0u;
    uint decalsMax = 0u;
    uint probesMin = 0u;
    uint probesMax = 0u;
    int MAX_LIGHTS_PER_CLUSTER = 256;
    clusternumlights_t cluster;
    cluster.offset = _483.clusternumlights[clusterOffset].offset;
    cluster.numItems = _483.clusternumlights[clusterOffset].numItems;
    int dataOffset = cluster.offset & (~(cluster.offset >> 31));
    int numItems = cluster.numItems & (~(cluster.numItems >> 31));
    lightsMin = uint((dataOffset >> 16) * MAX_LIGHTS_PER_CLUSTER);
    lightsMax = lightsMin + uint(numItems & 255);
    decalsMin = lightsMin;
    decalsMax = decalsMin + uint((numItems >> 8) & 255);
    probesMin = lightsMin;
    probesMax = probesMin + uint((numItems >> 16) & 255);
    vec3 ambient = vec3(0.0);
    vec3 shc0 = vec3(lightInput.ambientSH0.x, lightInput.ambientSH0.y, lightInput.ambientSH0.z);
    vec3 shc1 = vec3(lightInput.ambientSH0.w, lightInput.ambientSH1.x, lightInput.ambientSH1.y);
    vec3 shc2 = vec3(lightInput.ambientSH1.z, lightInput.ambientSH1.w, lightInput.ambientSH2.x);
    vec3 shc3 = vec3(lightInput.ambientSH2.y, lightInput.ambientSH2.z, lightInput.ambientSH2.w);
    vec3 indirectDiffuse = ((shc0 - (shc1 * lightInput.normal.y)) + (shc2 * lightInput.normal.z)) - (shc3 * lightInput.normal.x);
    float param_5 = _instanceUniforms.instanceirradiancescale.x * _viewUniforms.envirradiancescale.x;
    ambient += max(indirectDiffuse * saturate(param_5), vec3(0.0));
    float _600;
    if (dot(lightInput.emissive, vec3(1.0)) > 0.0)
    {
        _600 = -lightInput.smoothness;
    }
    else
    {
        _600 = lightInput.smoothness;
    }
    lightInput.smoothness = _600;
    lightInput.ambient_lighting = ambient;
    lightInput.diffuse_lighting = ambient;
    uint lightIdx = lightsMin;
    light_t lightParms;
    vec3 _957;
    float _1027;
    float _1045;
    shadowparms_t shadow_parms;
    uint _1547;
    shadowparms_t shadow_parms_1;
    while (lightIdx < lightsMax)
    {
        uint temp = _631.clusterlightsid[lightIdx];
        uint divergentLightId = temp & 4095u;
        uint light_id = divergentLightId;
        if (light_id >= divergentLightId)
        {
            lightIdx++;
        }
        int lightParms1Size = 4;
        int lightParms2Size = 4;
        int lightParms3Size = 3;
        lightParms.pos = _659.lightparms1[(uint(lightParms1Size) * light_id) + 0u].xyz;
        float param_6 = _659.lightparms1[(uint(lightParms1Size) * light_id) + 0u].w;
        lightParms.lightParms = asuint(param_6);
        lightParms.posShadow = _659.lightparms1[(uint(lightParms1Size) * light_id) + 1u];
        lightParms.falloffR = _659.lightparms1[(uint(lightParms1Size) * light_id) + 2u];
        lightParms.projS = _698.lightparms2[(uint(lightParms2Size) * light_id) + 0u];
        lightParms.projT = _698.lightparms2[(uint(lightParms2Size) * light_id) + 1u];
        lightParms.projQ = _698.lightparms2[(uint(lightParms2Size) * light_id) + 2u];
        float param_7 = _659.lightparms1[(uint(lightParms1Size) * light_id) + 3u].x;
        lightParms.scaleBias.x = asuint(param_7);
        float param_8 = _659.lightparms1[(uint(lightParms1Size) * light_id) + 3u].y;
        lightParms.scaleBias.y = asuint(param_8);
        float param_9 = _659.lightparms1[(uint(lightParms1Size) * light_id) + 3u].z;
        lightParms.scaleBias.z = asuint(param_9);
        float param_10 = _659.lightparms1[(uint(lightParms1Size) * light_id) + 3u].w;
        lightParms.scaleBias.w = asuint(param_10);
        lightParms.boxMin = _767.lightparms3[(uint(lightParms3Size) * light_id) + 0u];
        lightParms.boxMax = _767.lightparms3[(uint(lightParms3Size) * light_id) + 1u];
        lightParms.areaPlane = _767.lightparms3[(uint(lightParms3Size) * light_id) + 2u];
        float param_11 = _698.lightparms2[(uint(lightParms2Size) * light_id) + 3u].x;
        lightParms.lightParms2 = asuint(param_11);
        float param_12 = _698.lightparms2[(uint(lightParms2Size) * light_id) + 3u].y;
        lightParms.colorPacked = asuint(param_12);
        lightParms.specMultiplier = _698.lightparms2[(uint(lightParms2Size) * light_id) + 3u].z;
        float param_13 = _698.lightparms2[(uint(lightParms2Size) * light_id) + 3u].w;
        lightParms.shadowBleedReduce_lightScattering = asuint(param_13);
        float clip_min = 0.0039215688593685626983642578125;
        vec4 posW = vec4(lightInput.position, 1.0);
        float rcpProjQ = 1.0 / dot(posW, lightParms.projQ);
        vec3 projTC = vec3(dot(posW, lightParms.projS) * rcpProjQ, dot(posW, lightParms.projT) * rcpProjQ, dot(posW, lightParms.falloffR));
        uint light_parms = lightParms.lightParms;
        uint light_flags = skipStaticModel;
        light_flags |= 128u;
        float param_14 = projTC.x;
        float param_15 = projTC.y;
        float param_16 = projTC.z;
        bool _884 = fmin3(param_14, param_15, param_16) <= clip_min;
        bool _901;
        if (!_884)
        {
            float param_17 = projTC.x;
            float param_18 = projTC.y;
            float param_19 = projTC.z;
            _901 = fmax3(param_17, param_18, param_19) >= (1.0 - clip_min);
        }
        else
        {
            _901 = _884;
        }
        bool _909;
        if (!_901)
        {
            _909 = (light_parms & light_flags) != 0u;
        }
        else
        {
            _909 = _901;
        }
        if (_909)
        {
            continue;
        }
        if ((light_parms & 3u) == 2u)
        {
            vec2 _928 = fract((projTC.xy * _viewUniforms.sungoboscaleoffset.xy) + _viewUniforms.sungoboscaleoffset.zw);
            projTC = vec3(_928.x, _928.y, projTC.z);
        }
        vec3 light_position = lightParms.pos;
        float light_attenuation = 1.0;
        float light_spec_multiplier = 1.0;
        vec3 diffuse_dir = vec3(0.0, 0.0, 1.0);
        vec3 specular_dir = vec3(0.0, 0.0, 1.0);
        uint light_parms_1 = lightParms.lightParms;
        vec3 localBox = (projTC * 2.0) - vec3(1.0);
        vec2 projUV = projTC.xy;
        vec3 paralell_light_dir = lightParms.areaPlane.xyz;
        if ((light_parms_1 & 3u) == 2u)
        {
            _957 = paralell_light_dir;
        }
        else
        {
            _957 = normalize(light_position - lightInput.position);
        }
        diffuse_dir = _957;
        diffuse_dir = diffuse_dir;
        uvec2 param_20 = lightParms.scaleBias.zw;
        vec4 projFilterScaleBias = unpackR15G15B15A15(param_20);
        float projFilter = textureLod(samp_lightsatlasmap, vec4((projUV * projFilterScaleBias.xy) + projFilterScaleBias.zw, 0.0, 0.0).xy, vec4((projUV * projFilterScaleBias.xy) + projFilterScaleBias.zw, 0.0, 0.0).w).x;
        float projSq = projFilter * projFilter;
        uint light_type_mask = light_parms_1 & 3u;
        bool _1012 = light_type_mask == 1u;
        bool _1024;
        if (!_1012)
        {
            bool _1017 = light_type_mask == 0u;
            bool _1023;
            if (_1017)
            {
                _1023 = lightParms.boxMin.z != 0.0;
            }
            else
            {
                _1023 = _1017;
            }
            _1024 = _1023;
        }
        else
        {
            _1024 = _1012;
        }
        bool falloff2d = _1024;
        if (falloff2d)
        {
            float param_21 = lightParms.boxMin.w;
            _1027 = saturate(param_21);
        }
        else
        {
            _1027 = 0.5;
        }
        float falloffSlice = _1027;
        bool _1038 = light_type_mask == 0u;
        bool _1044;
        if (_1038)
        {
            _1044 = lightParms.boxMin.z != 0.0;
        }
        else
        {
            _1044 = _1038;
        }
        if (_1044)
        {
            float param_22 = sqrt(dot(localBox, localBox));
            _1045 = min(0.9921875, saturate(param_22));
        }
        else
        {
            _1045 = projTC.z;
        }
        float falloffLookup = _1045;
        uvec2 param_23 = lightParms.scaleBias.xy;
        vec4 falloffScaleBias = unpackR15G15B15A15(param_23);
        vec2 falloffUV = vec2(falloffLookup, falloffSlice);
        float falloff = textureLod(samp_lightsatlasmap, vec4((falloffUV * falloffScaleBias.xy) + falloffScaleBias.zw, 0.0, 0.0).xy, vec4((falloffUV * falloffScaleBias.xy) + falloffScaleBias.zw, 0.0, 0.0).w).x;
        falloff *= falloff;
        light_attenuation = falloff * projSq;
        if ((light_parms_1 & 48u) != 0u)
        {
            vec4 areaLightPlane = lightParms.areaPlane;
            vec4 areaLightPlaneU = lightParms.boxMin;
            vec4 areaLightPlaneV = lightParms.boxMax;
            float projToOrigin = dot(lightInput.position, areaLightPlane.xyz) + areaLightPlane.w;
            if (projToOrigin < 0.0)
            {
                continue;
            }
            float falloffDistance = length(areaLightPlane.xyz);
            float param_24 = projToOrigin * 50.0;
            float falloff_1 = saturate(param_24);
            light_attenuation *= (falloff_1 * falloff_1);
            projToOrigin /= falloffDistance;
            areaLightPlane /= vec4(falloffDistance);
            vec3 mirrorDir = reflect(-lightInput.view, lightInput.normal);
            if (dot(mirrorDir, areaLightPlane.xyz) > 0.0)
            {
                mirrorDir = reflect(mirrorDir, areaLightPlane.xyz);
            }
            float projToPlane = dot(mirrorDir, areaLightPlane.xyz);
            float projLength = projToOrigin / projToPlane;
            vec3 hitPos = lightInput.position - (mirrorDir * projLength);
            vec3 hitDir = hitPos - light_position;
            float u = dot(hitDir, areaLightPlaneU.xyz) / areaLightPlaneU.w;
            float v = dot(hitDir, areaLightPlaneV.xyz) / areaLightPlaneV.w;
            if ((light_parms_1 & 48u) == 32u)
            {
                vec2 circle = vec2(u, v);
                float rcpR = 1.0 / max(1.0, length(vec2(u, v)));
                u *= (rcpR * areaLightPlaneU.w);
                v *= (rcpR * areaLightPlaneV.w);
            }
            else
            {
                float param_25 = abs(u);
                float param_26 = u;
                u = (saturate(param_25) * FastSign(param_26)) * areaLightPlaneU.w;
                float param_27 = abs(v);
                float param_28 = v;
                v = (saturate(param_27) * FastSign(param_28)) * areaLightPlaneV.w;
            }
            vec3 clampedHitPos = (areaLightPlaneU.xyz * u) + (areaLightPlaneV.xyz * v);
            light_position += clampedHitPos;
            diffuse_dir = normalize(light_position - lightInput.position);
            diffuse_dir = diffuse_dir;
        }
        else
        {
            uint clipFlags = (lightParms.shadowBleedReduce_lightScattering >> uint(16)) & 255u;
            if (clipFlags == 0u)
            {
                vec4 clipPlane = lightParms.boxMax;
                if (dot(clipPlane.xyz, lightInput.position) < (clipPlane.w - clip_min))
                {
                    continue;
                }
            }
            else
            {
                if (clipFlags == 1u)
                {
                    vec3 clipBoxSize = lightParms.boxMax.xyz;
                    float param_29 = lightParms.boxMax.w;
                    uint param_30 = asuint(param_29);
                    vec3 clipBoxOffset = (((unpackR11G11B10(param_30) * 2.0) - vec3(1.0)) * clipBoxSize) + light_position;
                    vec3 clippedPos = abs((lightInput.position - clipBoxOffset) / clipBoxSize);
                    float param_31 = clippedPos.x;
                    float param_32 = clippedPos.y;
                    float param_33 = clippedPos.z;
                    if (fmax3(param_31, param_32, param_33) > 1.0)
                    {
                        continue;
                    }
                }
            }
        }
        if (light_attenuation <= (clip_min / 256.0))
        {
            continue;
        }
        float shadow = 1.0;
        bool _1362 = (light_parms & 4u) != 0u;
        bool _1368;
        if (_1362)
        {
            _1368 = _instanceUniforms.particleshadowdisable.x == 0.0;
        }
        else
        {
            _1368 = _1362;
        }
        if (_1368)
        {
            float shadowBleedReduce = float(lightParms.shadowBleedReduce_lightScattering & 255u) / 255.0;
            float normal_bias_scale = 0.00999999977648258209228515625;
            vec3 pos = lightInput.position + (lightInput.normal * normal_bias_scale);
            uint idx = 0u;
            if ((light_parms & 3u) == 0u)
            {
                vec3 light_dir = pos - lightParms.posShadow.xyz;
                float param_34 = light_dir.x;
                float param_35 = light_dir.y;
                float param_36 = light_dir.z;
                idx = getCubeMapFaceID(param_34, param_35, param_36);
            }
            else
            {
                if ((light_parms & 3u) == 2u)
                {
                    float fraction = 0.0;
                    for (; idx < 3u; idx++)
                    {
                        int shadowParmSize = 5;
                        shadow_parms.shadowLightS = _1430.shadowsparmsubo[(uint(shadowParmSize) * ((light_parms >> uint(22)) + idx)) + 0u];
                        shadow_parms.shadowLightT = _1430.shadowsparmsubo[(uint(shadowParmSize) * ((light_parms >> uint(22)) + idx)) + 1u];
                        shadow_parms.shadowLightR = _1430.shadowsparmsubo[(uint(shadowParmSize) * ((light_parms >> uint(22)) + idx)) + 2u];
                        shadow_parms.shadowLightQ = _1430.shadowsparmsubo[(uint(shadowParmSize) * ((light_parms >> uint(22)) + idx)) + 3u];
                        shadow_parms.shadowAtlasScaleBias = _1430.shadowsparmsubo[(uint(shadowParmSize) * ((light_parms >> uint(22)) + idx)) + 4u];
                        vec2 shadowTC = vec2(dot(pos, shadow_parms.shadowLightS.xyz) + shadow_parms.shadowLightS.w, dot(pos, shadow_parms.shadowLightT.xyz) + shadow_parms.shadowLightT.w);
                        shadowTC = abs((shadowTC * 2.0) - vec2(1.0));
                        bool _1512 = shadowTC.x < 1.0;
                        bool _1518;
                        if (_1512)
                        {
                            _1518 = shadowTC.y < 1.0;
                        }
                        else
                        {
                            _1518 = _1512;
                        }
                        if (_1518)
                        {
                            fraction = (1.0 - max(shadowTC.x, shadowTC.y)) * _viewUniforms.parallellightslicescale.w;
                            break;
                        }
                    }
                    idx = min(2u, idx);
                    uint screenNoise = uint(lightInput.fragCoord.x) ^ (uint(lightInput.fragCoord.y) << uint(1));
                    if (_viewUniforms.nummultisamples.w > 0.0)
                    {
                        _1547 = uint(_viewUniforms.randomint.x);
                    }
                    else
                    {
                        _1547 = 0u;
                    }
                    screenNoise ^= _1547;
                    idx += uint(int(((float(screenNoise & 3u) * 0.25) + 0.125) > fraction));
                }
            }
            int shadowParmSize_1 = 5;
            shadow_parms_1.shadowLightS = _1430.shadowsparmsubo[(uint(shadowParmSize_1) * ((light_parms >> uint(22)) + idx)) + 0u];
            shadow_parms_1.shadowLightT = _1430.shadowsparmsubo[(uint(shadowParmSize_1) * ((light_parms >> uint(22)) + idx)) + 1u];
            shadow_parms_1.shadowLightR = _1430.shadowsparmsubo[(uint(shadowParmSize_1) * ((light_parms >> uint(22)) + idx)) + 2u];
            shadow_parms_1.shadowLightQ = _1430.shadowsparmsubo[(uint(shadowParmSize_1) * ((light_parms >> uint(22)) + idx)) + 3u];
            shadow_parms_1.shadowAtlasScaleBias = _1430.shadowsparmsubo[(uint(shadowParmSize_1) * ((light_parms >> uint(22)) + idx)) + 4u];
            vec4 shadowTC_1 = vec4((pos.x * shadow_parms_1.shadowLightS.x) + ((pos.y * shadow_parms_1.shadowLightS.y) + ((pos.z * shadow_parms_1.shadowLightS.z) + shadow_parms_1.shadowLightS.w)), (pos.x * shadow_parms_1.shadowLightT.x) + ((pos.y * shadow_parms_1.shadowLightT.y) + ((pos.z * shadow_parms_1.shadowLightT.z) + shadow_parms_1.shadowLightT.w)), (pos.x * shadow_parms_1.shadowLightR.x) + ((pos.y * shadow_parms_1.shadowLightR.y) + ((pos.z * shadow_parms_1.shadowLightR.z) + shadow_parms_1.shadowLightR.w)), (pos.x * shadow_parms_1.shadowLightQ.x) + ((pos.y * shadow_parms_1.shadowLightQ.y) + ((pos.z * shadow_parms_1.shadowLightQ.z) + shadow_parms_1.shadowLightQ.w)));
            vec3 _1714 = shadowTC_1.xyz / vec3(shadowTC_1.w);
            shadowTC_1 = vec4(_1714.x, _1714.y, _1714.z, shadowTC_1.w);
            bool _1722 = min(shadowTC_1.x, shadowTC_1.y) > 0.0;
            bool _1731;
            if (_1722)
            {
                _1731 = max(shadowTC_1.x, shadowTC_1.y) < 1.0;
            }
            else
            {
                _1731 = _1722;
            }
            if (_1731)
            {
                vec2 _1743 = (shadowTC_1.xy * shadow_parms_1.shadowAtlasScaleBias.xy) + shadow_parms_1.shadowAtlasScaleBias.zw;
                shadowTC_1 = vec4(_1743.x, _1743.y, shadowTC_1.z, shadowTC_1.w);
                if (false)
                {
                    shadow = textureLod(samp_shadowatlasdepthcomparemap, vec3(shadowTC_1.xyz.xy, shadowTC_1.xyz.z), 0.0);
                }
                else
                {
                    vec2 uv = (shadowTC_1.xy * _viewUniforms.shadowatlasresolution.xy) + vec2(0.5);
                    vec2 base_uv = floor(uv);
                    vec2 st = uv - base_uv;
                    base_uv -= vec2(0.5);
                    vec2 uvw0 = vec2(4.0) - (st * 3.0);
                    vec2 uvw1 = vec2(7.0);
                    vec2 uvw2 = vec2(1.0) + (st * 3.0);
                    vec2 uv0 = (base_uv + (((vec2(3.0) - (st * 2.0)) / uvw0) - vec2(2.0))) * _viewUniforms.shadowatlasresolution.zw;
                    vec2 uv1 = (base_uv + ((vec2(3.0) + st) / uvw1)) * _viewUniforms.shadowatlasresolution.zw;
                    vec2 uv2 = (base_uv + ((st / uvw2) + vec2(2.0))) * _viewUniforms.shadowatlasresolution.zw;
                    vec3 _1843 = vec3(uv0.x, uv0.y, shadowTC_1.z);
                    shadow = (uvw0.x * uvw0.y) * textureLod(samp_shadowatlasdepthcomparemap, vec3(_1843.xy, _1843.z), 0.0);
                    vec3 _1859 = vec3(uv1.x, uv0.y, shadowTC_1.z);
                    shadow += ((uvw1.x * uvw0.y) * textureLod(samp_shadowatlasdepthcomparemap, vec3(_1859.xy, _1859.z), 0.0));
                    vec3 _1877 = vec3(uv2.x, uv0.y, shadowTC_1.z);
                    shadow += ((uvw2.x * uvw0.y) * textureLod(samp_shadowatlasdepthcomparemap, vec3(_1877.xy, _1877.z), 0.0));
                    vec3 _1895 = vec3(uv0.x, uv1.y, shadowTC_1.z);
                    shadow += ((uvw0.x * uvw1.y) * textureLod(samp_shadowatlasdepthcomparemap, vec3(_1895.xy, _1895.z), 0.0));
                    vec3 _1913 = vec3(uv1.x, uv1.y, shadowTC_1.z);
                    shadow += ((uvw1.x * uvw1.y) * textureLod(samp_shadowatlasdepthcomparemap, vec3(_1913.xy, _1913.z), 0.0));
                    vec3 _1931 = vec3(uv2.x, uv1.y, shadowTC_1.z);
                    shadow += ((uvw2.x * uvw1.y) * textureLod(samp_shadowatlasdepthcomparemap, vec3(_1931.xy, _1931.z), 0.0));
                    vec3 _1949 = vec3(uv0.x, uv2.y, shadowTC_1.z);
                    shadow += ((uvw0.x * uvw2.y) * textureLod(samp_shadowatlasdepthcomparemap, vec3(_1949.xy, _1949.z), 0.0));
                    vec3 _1967 = vec3(uv1.x, uv2.y, shadowTC_1.z);
                    shadow += ((uvw1.x * uvw2.y) * textureLod(samp_shadowatlasdepthcomparemap, vec3(_1967.xy, _1967.z), 0.0));
                    vec3 _1985 = vec3(uv2.x, uv2.y, shadowTC_1.z);
                    shadow += ((uvw2.x * uvw2.y) * textureLod(samp_shadowatlasdepthcomparemap, vec3(_1985.xy, _1985.z), 0.0));
                    shadow *= 0.0069444444961845874786376953125;
                }
            }
            else
            {
                shadow = float((light_parms & 3u) == 2u);
            }
            float shadow_fade = float((light_parms >> uint(16)) & 63u) / 63.0;
            float param_37 = ((shadow * shadow_fade) + 1.0) - shadow_fade;
            shadow = saturate(param_37);
        }
        uint param_38 = lightParms.colorPacked;
        vec3 light_color = (unpackRGBE(param_38) * shadow) * light_attenuation;
        uint param_39 = lightParms.lightParms2;
        vec4 light_parms2 = unpackR8G8B8A8(param_39);
        light_color *= light_parms2.w;
        light_spec_multiplier *= lightParms.specMultiplier;
        vec3 light_color_final = light_color;
        vec3 light_dir_1 = diffuse_dir;
        float param_40 = ((dot(lightInput.normal, light_dir_1) * _instanceUniforms.particletranslucencyscale.x) * 0.5) + 0.5;
        float NdotL = saturate(param_40);
        vec3 diffuse_light_color = mix(light_color_final, vec3(0.0), bvec3((light_parms & 256u) > 0u));
        lightInput.diffuse_lighting += (diffuse_light_color * NdotL);
    }
    lightInput.diffuse_lighting = lightInput.diffuse_lighting;
    lightInput.specular_lighting += lightInput.emissive;
    lightInput.diffuse_lighting *= lightInput.albedo;
    lightInput.output_lighting = lightInput.diffuse_lighting + lightInput.specular_lighting;
    out_FragColor0 = vec4(lightInput.diffuse_lighting.x, lightInput.diffuse_lighting.y, lightInput.diffuse_lighting.z, out_FragColor0.w);
    out_FragColor0.w = 1.0;
}


