Search code examples
openglglslframebufferdepth-buffer

What is the binding between color and depth buffer in OpenGL when there are multiple color buffers


I do understand the concepts behind and Color and Depth Buffers both in case of default FB and a Frame Buffer Object when there is ONE color buffer. But what I dont understand is how the depth buffer is "tied" to the color buffers when there are multiple color buffers.

For instance:

  1. In case of multiple render targets, we can have N number of color buffers attached to different color attachment points. But we have only 1 depth attachment point. Does this mean that between all the color attachment points, only the first's (COLOR_ATTACHMENT0) final pixel color is computed based on the depth values from depth buffer? What about the color of remaining color buffers. Do they ignore the depth comparison to determine their final pixel color?

  2. What about the case of layered rendering. Suppose I attach a texture array(GL_TEXTURE_2D_ARRAY) of size N to the first color buffer (COLOR_ATTACHMENT0). I now must attach a depth texture too of type GL_TEXTURE_2D_ARRAY (otherwise its a incomplete attachment). What should be the size of this of depth attachment. If I make it N, will each of the layers get depth values? Or N must be 1?

Can an expert please answer both questions. Thanks.


Solution

    1. The depth buffer is used once per fragment. If the depth test fails, the entire fragment is discarded, so none of the writes to any of the color buffers are performed.

    2. The fragments will use the depth buffer corresponding to the layer they are on. For example, if a fragment is written to gl_Layer 2, it will use the 3rd layer of both the depth buffer and color buffer.