# Why are depth buffer values clamped to (0, 1)?

This is a simple question.

I know that in OpenGL or other graphics API, the values stored in the depth buffer are automatically clamped to (0,1).

I just want to know why people do this instead of just keeping the actual depth values as float. Is that just because of the non-linear depth equation which makes the closer depth values more precise and the further depth values less precies?

Thank you!

(Just converting comment into an answer) It seems you might be asking one of two possible questions.

If you meant "Why doesn't the Depth buffer contain camera space Z"
then it's perhaps worth reading Why do GPUs divide clip space Z by W, for position?

If, OTOH, you just meant "why is it restricted to [0,1)?", then that's probably because old systems represented the Z-buffer depth with fixed point numbers, hence a natural [0,1) range.

Of course, later hardware arrived that could use floating-point depths; IIRC Dreamcast's depth representation was an arbitrary positive float representing $\frac{1}{w}$.

• Thanks! I just mean "why is it restricted to [0,1)?". I came up with this question when I was implementing shadow mapping. When using shadow maps, if the depth values stored in the shadow map are all between [0,1), we have to clamp the depth value of the pixel to [0,1) first and then do the comparison. If the shadow map stores the actual depth values as floats, we can just compare them directly. – Yuchen Sep 18 '18 at 17:52

Depth buffer values are clamped to that range because usually they are using fixed-point representation, so if clipping is disabled (by glEnable(GL_DEPTH_CLAMP)) then they can't hold anything beyond that range.

However, there isn't really a good reason for this clamping when floating point depth buffers are used. In fact GL_NV_depth_buffer_float extensions allows you to disable this clamping on Nvidia hardware for floating point buffers. Unfortunately other vendors don't have any documented way of doing that.

Also it's worth noting that unclamped floating point depth values have a minor complication: their depth offset is trickier to define. OpenGL spec resolves this by making the depth offset depend on the range of z-coordinates of the primitive. In fixed-point depth buffers, on the other hand, the offset is a function of the triangle slope only.