Using glDrawBuffers for multiple rendering purposes under OS X

I have very strange behavior from my code with multiple renderers, and I began to wonder if I am disastrously misunderstood how this should work.

I work in the context of version 2.1. This is where the kernel bit of the rendering setup code runs, which I do:

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_fbo);
GLenum buffers[] = { GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT };
glDrawBuffers(2,buffers);  

My shader then writes color data to gl_FragColor[0]and glFragColor[1].

This is essentially the same situation that was discussed in this matter . However, when I run this on OS X, my shader only outputs to the first rendering target. OpenGL does not cause errors during the construction of FBO with two color attachments, nor during the rendering process.

When I examine what happens through the OpenGL Profiler OSX "trace" view, it shows the driver side of this code execution as:

2.86 ยตs glBindFramebufferEXT(GL_FRAMEBUFFER, 1);
3.48 ยตs glDrawBuffersARB(2, {GL_COLOR_ATTACHMENT0, GL_ZERO});

Which perhaps explains why nothing was written to GL_COLOR_ATTACHMENT1; he seems to be replaced on GL_ZEROcall glDrawBuffers!

If I switch the order of the buffers in the array buffers[]as GL_COLOR_ATTACHMENT1_EXT, and then GL_COLOR_ATTACHMENT0_EXT, then my shader is only written to GL_COLOR_ATTACHMENT1_EXT, and GL_COLOR_ATTACHMENT0_EXTreplaced with GL_ZERO>.

Here where it gets weird. If I use the code:

GLenum buffers[] = { GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT };
glDrawBuffers(3, buffers);

Then a statistics view shows this:

0.46 ยตs glDrawBuffersARB(3, {GL_COLOR_ATTACHMENT0, GL_ZERO, GL_COLOR_ATTACHMENT1});

OpenGL is still error free, and my shader successfully writes data to both color attachments, even if it is written to gl_FragColor[0]and gl_FragColor[1].

So, although my program is working now, it seems to me that this should not work. And I was curious to see how far I can advance this, hoping that pushing OpenGL towards a possible glitch would be educational. So I tried to compile this code:

GLenum buffers[] = { GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT };
glDrawBuffers(4, buffers);

When you run this view, the OpenGL Profiler "trace" shows this as executable:

4.26 ยตs glDrawBuffersARB(4, {GL_COLOR_ATTACHMENT0, GL_ZERO, GL_COLOR_ATTACHMENT1, GL_ZERO});

And now OpenGL throws "invalid framebuffer operations" everywhere, but my shader still successfully writes color data to both color anchor points.

Does all this make sense to everyone? Have I really catastrophically misunderstood the way that should be called glDrawBuffers?

According to the OpenGL Profiler Resources view, my framebuffer (number 1) looks great; It has two attached colors, as expected.

Attached Objects:
{
  {
    GL_FRAMEBUFFER_ATTACHMENT: GL_COLOR_ATTACHMENT0
    GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT: GL_TEXTURE
    GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT: 1
    GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT: 0
    GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT: 0
    GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT: 0
  }
  {
    GL_FRAMEBUFFER_ATTACHMENT: GL_COLOR_ATTACHMENT1
    GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT: GL_TEXTURE
    GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT: 2
    GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT: 0
    GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT: 0
    GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT: 0
  }
  {
    GL_FRAMEBUFFER_ATTACHMENT: GL_COLOR_ATTACHMENT2
    GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT: GL_NONE
  }
  {
    GL_FRAMEBUFFER_ATTACHMENT: GL_COLOR_ATTACHMENT3
    GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT: GL_NONE
  }
  {
    GL_FRAMEBUFFER_ATTACHMENT: GL_COLOR_ATTACHMENT4
    GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT: GL_NONE
  }
  {
    GL_FRAMEBUFFER_ATTACHMENT: GL_COLOR_ATTACHMENT5
    GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT: GL_NONE
  }
  {
    GL_FRAMEBUFFER_ATTACHMENT: GL_COLOR_ATTACHMENT6
    GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT: GL_NONE
  }
  {
    GL_FRAMEBUFFER_ATTACHMENT: GL_COLOR_ATTACHMENT7
    GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT: GL_NONE
  }
  {
    GL_FRAMEBUFFER_ATTACHMENT: GL_DEPTH_ATTACHMENT
    GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT: GL_TEXTURE
    GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT: 3
    GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT: 0
    GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT: 0
    GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT: 0
  }
  {
    GL_FRAMEBUFFER_ATTACHMENT: GL_STENCIL_ATTACHMENT
    GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT: GL_NONE
  }
}
+5
1

... , , StackOverflow. , , OSX.

- , 64- OSX, GLenum 8- , OpenGL , 32- glDrawBuffers. :

uint32_t buffers[] = { GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT };
glDrawBuffers(2,(GLenum*)buffers);  

, . ( GL_ZERO , )

+6

All Articles