Search code examples
opengltexturesfbo

OpenGL: Render into FBO with depth attachment, while reading from same depth texture, with glDepthMask being false


I know that it is an error to render into an FBO while simultaneously fetching from the depth texture attached to this FBO (at least without the texture_barrier extension).

However, is it also an error to do so if glDepthMask is false?


Solution

  • Feedback loops are not errors; they're undefined behavior. If they were errors, you could just try it and see if it fails.

    If you are not on GL 4.5 or ARB_texture_barrier (or NV_texture_barrier) is not available, then doing as you suggest will yield undefined behavior.

    The rules for feedback loops before that feature were quite draconian. Those rules only cares about what is attached and bound. The simple act of attaching the texture to the FBO and binding it for access by the shader invokes undefined behavior. The only state that could prevent it is the use of mipmap base/max levels to make it impossible for the shader to access the mipmap level attached to the FBO.

    If you did have access to GL 4.5/ARB_texture_barrier/NV_texture_barrier, then it should work... provided that you use a barrier before you switch over to reading. The new text makes it clear that it is reading data that has been written (by fragment shader outputs) that is the problem, and the depth mask prevents writing from happening. And with the texture barrier in place, you make the previously written data available to shaders.