Get distance to camera with shaders and buffers

I believe that cs_position.z gives the distance along the dimension aligned with the camera barrel, which is not what I want (I want the distance from the camera to the object). In the illustration below (where the camera points straight down), I want d, not z.

            (camera)
---------------o---------------
               |\
               | \
             z |  \ d
               |   \
               -----x (object)

The issue with the depth buffer, is I believe it also uses z (rescaled), so to recover d you need to do (slow) geometry with the depth buffer value and the pixel position. Unless I’m misunderstanding something, this means it might be best to use shaders.

However, I’ve been trying all day to get the (simplest?) shaders example working, to no avail. I’ve asked this as a separate question since I thought it would be independently useful. Once I figure out how to get that simple example running, I’ll return to this and see if I can get it working.