openglgraphicsopengl-4

Clearing FBO depth buffer without considering glDephMask


I'm currently writing a render pass which renders to a framebuffer with attached color and depth attachments. Before the pass starts, it needs to clear the depth attachment to a set value. I'm trying to accomplish this with glClearNamedFramebufferfv(fbo, GL_DEPTH, 0, &depth_value). However, this seems to only work if glDepthMask was set to true before/not set to 'false'.

I find it a bit weird to have the clear operation depend on global pipeline state (or perhaps I have worked with Vulkan a bit too long before this task), so I'd first like to ask whether this is indeed how it works (the spec says "enable or disable writing into the depth buffer", not just rendering, so it seems to be intended).

The second question would then be if there are alternatives that don't rely/respect this global flag. Since the underlying FBO attachment is a texture and not a renderbuffer, could I use glClearTexImage instead or does this also respect glDepthMask? Are there performance costs when clearing a texture like this instead of via the framebuffer?

Thank you in advance


Solution

  • Yes, the OpenGL standard explicitly requires that masks are applied to framebuffer clearing operations. This is still true for the DSA-style functions. This is mainly because they inherit their functionality from the non-DSA equivalents. glClearNamedFramebufferfv is conceptually equivalent to glClearBufferfv, and that function uses masks. Therefore, the "named" equivalent uses masks.

    Texture clearing does not use the mask state. However, it is also not a framebuffer operation. As such, it is possible that this clear will be performed in a more inefficient manner than using a proper framebuffer clear.

    So it'd be better to just reset the mask first.