Search code examples
c++opengldepth-bufferfbostencil-buffer

How would I store the depth information for multiple color targets if my OpenGL FBO allows multiple color attachments but only one depth attachment?


If OpenGL FBOs have a provision for multiple colour targets with GL_COLOR_ATTACHMENT0 to GL_COLOR_ATTACHMENT31 but only one depth/stencil target with GL_DEPTH_STENCIL_ATTACHMENT, then how would I be able to store the depth information of multiple colour targets? If I am rendering two different scenes to two different colour targets at different colour attachment locations on the same FBO, surely I would need two different depth/stencil targets to store the depth information for each of the two colour targets, right? How would I go about achieving this? I'm just confused as to why there is provision for only one depth/stencil target while there can be multiple colour targets on the same FBO.


Solution

  • Multiple color attachments aren't meant to render different scenes or viewports. They are meant to store multiple outputs from the same render pass, which means that they only require a single depth buffer.

    Rendering multiple scenes or viewports would (in addition to an additional depth buffer) also require a different render pass which also means running the depth test, all shader, stencil tests and everything else in the rendering pipeline separately. There wouldn't be any advantage of rendering the scene to different color attachments of the same FBO compared to rendering to different FBOs.

    In contrast, allowing multiple color outputs from the same render pass provides significant performance improvements compared to rendering the same viewport twice because all the stages (shader, rasterization, depth testing) are run only once. Multiple color outputs are often used for special effects, for example, for outputting normals or outputting motion vectors for motion blur. They are also used when implementing a deferred shading pipeline.