7.10. Multiple Render Targets
Another feature added to OpenGL in version 2.0 was the ability to render into multiple buffers simultaneously. The OpenGL Shading Language makes provisions for this capability by including a fragment shader output variable defined as an array called gl_FragData. The size of this array is implementation dependent, but must be at least 1. The elements of this array are defined to be of type vec4.
With this capability, applications can develop fragment shaders that compute multiple values for each fragment and store them in offscreen memory. These values can be accessed during a future rendering pass. Among other things, this lets applications implement complex multipass algorithms and use the graphics hardware for general-purpose computation.
To set up OpenGL for rendering into multiple target buffers, use
void glDrawBuffers(GLsizei n,
const GLenum *bufs)
Defines an array of buffers into which fragment color values or fragment data will be written. If no fragment shader is active, rendering operations generate only one fragment color per fragment and it is written into each of the buffers specified by bufs. If a fragment shader is active and it writes a value to the output variable gl_FragColor, then that value is written into each of the buffers specified by bufs. If a fragment shader is active and it writes a value to one or more elements of the output array variable gl_FragData, then the value of gl_FragData is written into the first buffer specified by bufs, the value of gl_FragData is written into the second buffer specified by bufs, and so on up to gl_FragData[n-1]. The draw buffer used for gl_FragData[n] and beyond is implicitly set to be GL_NONE.
The symbolic constants contained in bufs are defined in Table 7.4. Except for GL_NONE, none of the symbolic constants may appear more than once in bufs. The maximum number of draw buffers supported is implementation dependent and can be queried by calling glGet with the argument GL_MAX_DRAW_BUFFERS. The number of auxiliary buffers can be queried by calling glGet with the argument GL_AUX_BUFFERS.
Table 7.4. Buffer names for use with the glDrawBuffers call
The fragment color/data value is not written into any color buffer.
The fragment color/data value is written into the front-left color buffer.
The fragment color/data value is written into the front-right color buffer.
The fragment color/data value is written into the back-left color buffer.
The fragment color/data value is written into the back-right color buffer.
The fragment color/data value is written into auxiliary buffer i.
An error is generated if glDrawBuffers specifies a buffer that does not exist in the current GL context. If more than one buffer is selected for drawing, blending and logical operations are computed and applied independently for each element of gl_FragData and its corresponding buffer. Furthermore, the alpha value (i.e., the fourth component) of gl_FragData is used to determine the result of the alpha test. Operations such as scissor, depth, and stencil tests (if enabled) may cause the entire fragment (including all of the values in the gl_FragData array) to be discarded without any updates to the framebuffer.