Why do identical polygons not z-fight in OpenGL? Typically when coplanar or nearly coplanar polygons are rendered in OpenGL, the textures of each polygon mix together in a non-deterministic way. My understanding is that this is caused by the limited precision of the z-buffer as it uses floating point values. However, there seems to be an exception to this case. I've noticed that when two polygons are rendered with the exact same vertex positions, z-fighting does not occur. The polygon that was rendered last appears on top of the other coplanar polygon.

Why does this occur? Does this behavior always occur? Is this behavior guaranteed across all OpenGL implementations?
There are explicit rules in the OpenGL specification about invariance guarantees. For the purpose of this discussion, these rules boil down to the following.

Given a particular series of vertex processing shaders (VS, tessellation, and GS), there are some set of input values, uniforms, and the like which lead to the computation of the final gl_Position value for the primitives in that process. If all of these values are binary identical (including the way the vertex format leads to any VS inputs), then you're guaranteed that gl_Position will have binary identical values. And if two primitive of the same type have binary identical positions, the rasterizer is required to generate binary identical fragment positions.

And thus, no z-fighting.

Now, that's kind of restrictive, as it only works with the same shaders. You can use different shaders, so long as:


all shaders involved get binary identical values on expressions leading to the computation of gl_Position,
all shaders involved use the exact same expressions (and I mean exact; a + b isn't the "same expression" as b + a, no matter what mathematics says) to compute gl_Position, and
gl_Position and any inter-shader in/out variables leading to its computation are qualified with the invariant qualifier in all shaders. This tells the compiler not to optimize certain things away in expressions leading to the computation of such values.