FBO multiple color attachments and the depth attachment

I could write a small test app but hopefully someone knows:

Just wondering if I have an FBO with multiple color attachments and render to them in sequence if they all share the depth attachment.

Suppose I render a full screen quad at the near clip plane in one attachment and then switch to another attachment and render a cube further back. Should I expect that the cube will not render to the second attachment?

Either you are confused with multiple attachments or I am with how you work with them but if you have multiple attachments at one FBO you draw to all of them at the same time, not in sequence. In case I didn’t quite get how you want to use FBOs, please give us some (pseudo) code.

Well, you certainly can draw to them in sequence. Perhaps it’s a case of ‘you shouldn’t do that’. If I’m drawing to multiple attachments simultaneously then that makes sense. But I can set the draw buffer to be one attachment then draw something. Then, I can set the draw buffer to another attachment and draw something entirely different. Normally, when drawing to multiple attachments at the same time, you are drawing the same object.

This may be unconventional:

//pseudo code
FBO.Bind()
glDrawBuffer(GL_COLOR_ATTACHMENT0);
Sphere.Render();
//at this point I have updated the DEPTH_ATTACHMENT with the sphere.
glDrawBuffer(GL_COLOR_ATTACHMENT1);
Cube.Render();

So, I’ve rendered a sphere to the first color attachment and a cube to the second. Is there any contention with the depth buffer at this point? I would imagine there has to be.

This is probably a case of doing something stupid that shouldn’t be done.

It should have the same state it had after drawing the first object. The result in attachment1 should be the same as drawing the sphere with color write off and then drawing the cube.

That’s what I thought should happen.

Cheers.