uniform mat4 invertedProjectionMatrix;

vec3 viewSpacePositionFromDepth(float depth, float u, float v)
{
    float z = depth;
    // Get x/w and y/w from the viewport position
    float x = u * 2.0 - 1.0;
    float y = v * 2.0 - 1.0;
    vec4 vProjectedPos = vec4(x, y, z, 1.0);
    // Transform by the inverse projection matrix
    vec4 viewspacePosition = invertedProjectionMatrix * vProjectedPos;
    // Divide by w to get the view-space position
    return viewspacePosition.xyz / viewspacePosition.w;
}