#version 430 core

struct explosion {
        vec4        positionVelocity;
        vec4        color;
};

layout (std430, binding = 0) buffer entities {
    explosion   e[];
};

layout(binding = 1, offset = 0) uniform atomic_uint lastCounter;

layout (local_size_x = 512) in;

void main() {
    uint gid = gl_GlobalInvocationID.x;
    uint last = atomicCounter(lastCounter);

    if (gid < last && last > 0) {
        if (e[gid].color.a == 0.0) {
            uint old = atomicCounterDecrement(lastCounter);

            e[gid].positionVelocity = e[old].positionVelocity;
            e[gid].color = e[old].color;

            e[old].color.a = 0;
        }
    }
}
