Reading depth buffer with glReadPixels

Is there a portable way to read depth buffer values with glReadPixels()?

I have tried the following (using OpenTK bindings)

GL.ReadPixels<uint>(px, py, 1, 1, PixelFormat.DepthComponent, PixelType.UnsignedInt, depthsi);
GL.ReadPixels<float>(px, py, 1, 1, PixelFormat.DepthComponent, PixelType.Float, depthsf);

Both of these work on NVidia. Unfortunately on AMD the first one only gives me 0 and the second gives me INVALID_OPERATION. Querying the framebuffer attachment says the depth attachment has float component type.

I have converted my app to read ID from the ID buffer framebuffer color attachment and depth from the default back framebuffer. This works. So it appears on AMD reading depth from non-default framebuffer is not supported at least on my framebuffer configuration - or I am doing something else wrong…

Is your FBO multisampled? It is not allowed to call glReadPixels on a multisampled buffer.

No, it was not multisampled. This would not have a good idea anyway, since this was for picking, like color picking, although I had a unsigned int red component renderbuffer attached to color.

One day I decided I also want to support 2.1 so I removed the use of FBO and integer/red buffer. Converting picking to use just the normal RGB color back buffer was pretty simple after all and it works just as good.