Z-depth of Microdisplacements and Adaptive Subdivision Surface
As Duarte mentioned, generated coordinates do not factor in the displaced mesh. So you need to get a Z coordinate from something that does. You can use either "object" coordinates (available from the texture coordinate node or the geometry node, their "object" outputs are the same) or "global" (aka world space) coordinates from the geometry node's "position" output. These do not rely on precalculated geometry, instead they simply use the shading point's location. 



The difference between the two is that object coordinates are in "object space", meaning they account for the transformation of the object. That means they won't move when you move the object, which is good for animated objects (otherwise they might appear to "swim through" the texture). It also means that multiple copies of an object will always have the same texture, although that one may or may not be desireable. Global/world space coordinates are absolute and do not account for object transformations and locations at all. They're nice when you need to keep consistent texture locations across multiple objects (such as adding a waterline to objects below the level of your water mesh) or if you want different textures across multiple static copies of an object