The submarine measures its general depth by detecting the pressure of the water outside relative to the atmospheric pressure at the surface. So the deeper the sub. goes the greater the weight of water above it, and a simple pressure gauge is all that is required.
On the surface the pressure of water is nominally zero so the water line around the ship is the datum for measurement, i.e. the point of zero depth.
When submerged and nearing underwater obstacles, or indeed the bottom, sonar (SOund Navigation And Ranging) is a much more accurate means of measuring proximity, and distance is measured from the point on the hull that the sonar transponder is fitted. So the bottom facing transponder on the underside of the boat detects distance below the keel. Forward, rearward and upward facing transponders detect distance from the bow, stern and topside respectively. The clever transponders are able to swivel to detect direction and range simultaneously. Planar Flank Array is a strip of transponders along the sides of the boat with phenomenal passive detection power to alert the crew of nearby ships that can be switched to active mode to fix distance and range from just a couple of pings.