Why is the depth test not done on geometry before rasterization?

It seems the only time depth is used to discard data is during rasterization, i.e. at the fragment level.

In the geometry stage, I've only see culling and clipping of vertices. Is it not possible to determine whether if a triangle is behind another triangle using just vertex data (depth?) and discard if so? Wouldn't this save all the work later in scan conversion?

It doesn't work well to do depth tests at the vertex level.

Triangles are surfaces. All three vertices of a triangle could be occluded by other triangles in the scene and the middle of that triangle could still be visible. The only way to determine whether that triangle is completely behind something else is to interpolate position or depth along the triangle's surface and determine if, at every step of that interpolation, the triangle is still occluded.

This is precisely what the per-fragment depth test is already doing.

Thus, while you could do some "clever tricks" on a per-vertex basis to try to test if a triangle is fully occluded by others prior to the per-fragment test, they will never be completely accurate unless you do exactly the thing you're trying to avoid: step along the surface of the triangle as your rasterization resolution and test the triangle there.

• Here's an example using some basic cubes. – Draco18s Jan 12 '18 at 18:07
• Also, if you discard data at the vertex level, you'd have to be careful around transparent polygons, like glass, water, or foliage. – Nolonar Jan 12 '18 at 20:58
• @Nolonar Although to be fair, that doesn’t work well with per-fragment depth tests either (out-of-the-box; of course there are solutions like depth-peeling). – Jonas Schäfer Jan 13 '18 at 16:02

You can determine if a triangle is behind another triangle. However, that's not the operation you would need to do if you wanted to do culling at the triangle level. You would have to determine if the triangle is behind a forest of triangles: you have to determine if the triangle is behind any one triangle in the scene.

Triangle to triangle culling is expensive. Fragment by fragment culling is much much cheaper. Eventually the number of triangles will start to add up and fragment culling will be far cheaper.

This will also happen sooner than you think because we can't just test individual triangles. We have to test them together. If you have a triangle that's behind a quad (two triangles), it may be that your triangle being tested isn't occluded by either of the triangles that make up the quad on their own. This means now you need complex geometry tests to see if a triangle is covered in this way.

In the end, testing the triangle will be substantially more expensive than just doing the fragment tests.

This may be of interest: