glDrawBuffers
Connects the fragment shader's outputs to colour attachments of the bound framebuffer (multiple render targets).
Signature
void glDrawBuffers(GLsizei n, const GLenum *bufs);
How it works
Output location i of the fragment shader goes to bufs[i]. A deferred renderer's G-buffer writes position, normal and albedo in one pass:
layout(location = 0) out vec3 gPosition; layout(location = 1) out vec3 gNormal; layout(location = 2) out vec4 gAlbedoSpec;
and glDrawBuffers lists GL_COLOR_ATTACHMENT0, 1 and 2. Without it only attachment 0 receives anything.
The list is stored in the framebuffer currently bound to GL_DRAW_FRAMEBUFFER, so call it while that framebuffer is bound, once, during setup.
Parameters
Number of entries, up to GL_MAX_DRAW_BUFFERS (at least 8).
GL_COLOR_ATTACHMENTi or GL_NONE for each output location.
Tips & pitfalls
The DSA form,
glNamedFramebufferDrawBuffers, takes the framebuffer as an argument and needs no bind.glClearclears every listed attachment to the same colour; to clear them to different values use glClearBufferfv (orglClearNamedFramebufferfv).
Errors
When the call is invalid it does nothing and sets an error, which glGetError returns:
- GL_INVALID_VALUE
- n is larger than GL_MAX_DRAW_BUFFERS.
- GL_INVALID_OPERATION
- An attachment is listed twice, or a value is invalid for the bound framebuffer.
Example
1glBindFramebuffer(GL_FRAMEBUFFER, gBuffer);
2GLenum attachments[3] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 };
3glDrawBuffers(3, attachments);