I am rendering a large number of points (~10 million) and drawing a rectangle in front of them to occlude some of the points.
As more points get occluded, performance should improve. However, in certain situations, I observed that performance actually drops as more points are occluded, compared to when more points are visible. I have confirmed that the occluded points do not execute pixel shader operations.
Here is the relevant code:
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
glClear(GL_DEPTH_BUFFER_BIT);
GLuint newFBO;
glGenFramebuffers(1, &newFBO);
glBindFramebuffer(GL_FRAMEBUFFER, newFBO);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderBuffer);
constexpr bool bShareDepthBuffer = true;
if (bShareDepthBuffer)
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderBuffer);
else
{
GLuint newDepthRenderBuffer;
glGenRenderbuffers(1, &newDepthRenderBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, newDepthRenderBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, m_width, m_height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, newDepthRenderBuffer);
}
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
DrawBaseObjects2(*camera);
FBO is the primary framebuffer that I normally use, and colorRenderBuffer and depthRenderBuffer are attached to it.
During rendering, I create a new framebuffer, and only when bShareDepthBuffer = true, I attach the same depth buffer (depthRenderBuffer) to it.
When bShareDepthBuffer = true, the performance drops as more points get occluded, even if the newly created framebuffer is not used afterward. However, if I call glClear(GL_DEPTH_BUFFER_BIT); after glFramebufferRenderbuffer, the performance issue disappears.
If I instead create a separate depth buffer for the new framebuffer (bShareDepthBuffer = false), performance improves as expected, with better stability when more points are occluded.
Why does sharing the same depth renderbuffer across multiple framebuffers lead to a performance drop? The post states that binding the same renderbuffer to multiple FBOs should work. Is there something I might have overlooked?
Thanks in advance!