#version 430 compatibility
#define WORLD_NETHER
#include "/lib/settings.glsl"
#include "/lib/voxel_light_blend.glsl"

layout(local_size_x = 4, local_size_y = 8, local_size_z = 4) in;
const ivec3 workGroups = ivec3(8, 4, 8);

layout(rgba8) readonly uniform image3D farVoxelLightTempX;
layout(rgba8) writeonly uniform image3D farVoxelLightTempY;

void main() {
    ivec3 position = ivec3(gl_GlobalInvocationID);
    vec3 weightedChroma = vec3(0.0);
    float totalWeight = 0.0;
    float strongestMagnitude = 0.0;
    int firstOffset = max(-FAR_VOXEL_LIGHT_RADIUS, -position.y);
    int lastOffset = min(FAR_VOXEL_LIGHT_RADIUS, 31 - position.y);
    for (int offset = firstOffset; offset <= lastOffset; ++offset) {
        ivec3 samplePosition = position + ivec3(0, offset, 0);
        float falloff =
            1.0 - float(abs(offset)) / float(FAR_VOXEL_LIGHT_RADIUS + 1);
        vec3 candidate =
            imageLoad(farVoxelLightTempX, samplePosition).rgb * falloff;
        psxaAccumulateVoxelLight(
            candidate,
            weightedChroma,
            totalWeight,
            strongestMagnitude
        );
    }
    vec3 blended = psxaResolveVoxelLight(
        weightedChroma,
        totalWeight,
        strongestMagnitude
    );
    imageStore(farVoxelLightTempY, position, vec4(blended, 1.0));
}
