I think The function ConvertToDeviceZ should return (1.0/SceneDepth+View.InvDeviceZToWorldZTransform[3])/View.InvDeviceZToWorldZTransfrom[2]).The Original Code get right result When the far plane distance is infinite which make View.InvDeviceZToWorldZTransform[3] equals 0. However it will lead a small mistake when View.InvDeviceZToWorldZTransform[3] == -1.0/far and far plane is finite.