We need to write a raytracer in OpenGL. Now, I decided I would shoot a ray for every fragment shader call since, as far as I understand, a fragment is a screen pixel that could be written to by a geometry object. So I was wondering if a fragment shader would only run for visible pixels or for all pixels. If it only runs for visible ones, it would be a given that the primary ray (from screen to object) is not obstructed. This would save a lot of calculations.
1 Answers
There is absolutely no guarantee that the execution of a fragment shader means that the fragment is certainly visible.
Early depth test by itself will not save you. Rendering each triangle front-to-back will not save you; there is no guarantee in OpenGL that fragments are generated in order (only "as if" in order). And that's ignoring cases of overlap where it's impossible to have a proper ordering. Even issuing each triangle in its own separate rendering command guarantees nothing as far as OpenGL is concerned.
The only thing you can do to ensure this is to perform a depth pre-pass. That is, render your entire scene, but without a fragment shader active (and turn off color writes to the framebuffer). That will write all of the depth data to the depth buffer. That way, if you use early depth tests, when you render your scene again, the only fragments that pass the depth test will be those that are visible.
Depth pre-passes can be pretty fast, depending on your vertex shader and other aspects of your rendering pipeline.