2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * glReadBuffer, DrawBuffer functions.
45 * Return bitmask of BUFFER_BIT_* flags indicating which color buffers are
46 * available to the rendering context (for drawing or reading).
47 * This depends on the type of framebuffer. For window system framebuffers
48 * we look at the framebuffer's visual. But for user-create framebuffers we
49 * look at the number of supported color attachments.
50 * \param fb the framebuffer to draw to, or read from
51 * \return bitmask of BUFFER_BIT_* flags
54 supported_buffer_bitmask(const struct gl_context
*ctx
, const struct gl_framebuffer
*fb
)
56 GLbitfield mask
= 0x0;
58 /* A window system framebuffer */
60 mask
= BUFFER_BIT_FRONT_LEFT
; /* always have this */
61 if (fb
->Visual
.stereoMode
) {
62 mask
|= BUFFER_BIT_FRONT_RIGHT
;
63 if (fb
->Visual
.doubleBufferMode
) {
64 mask
|= BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_BACK_RIGHT
;
67 else if (fb
->Visual
.doubleBufferMode
) {
68 mask
|= BUFFER_BIT_BACK_LEFT
;
71 for (i
= 0; i
< fb
->Visual
.numAuxBuffers
; i
++) {
72 mask
|= (BUFFER_BIT_AUX0
<< i
);
80 * Helper routine used by glDrawBuffer and glDrawBuffersARB.
81 * Given a GLenum naming one or more color buffers (such as
82 * GL_FRONT_AND_BACK), return the corresponding bitmask of BUFFER_BIT_* flags.
85 draw_buffer_enum_to_bitmask(GLenum buffer
)
91 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_FRONT_RIGHT
;
93 return BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_BACK_RIGHT
;
95 return BUFFER_BIT_FRONT_RIGHT
| BUFFER_BIT_BACK_RIGHT
;
97 return BUFFER_BIT_FRONT_RIGHT
;
99 return BUFFER_BIT_BACK_RIGHT
;
101 return BUFFER_BIT_BACK_LEFT
;
102 case GL_FRONT_AND_BACK
:
103 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_BACK_LEFT
104 | BUFFER_BIT_FRONT_RIGHT
| BUFFER_BIT_BACK_RIGHT
;
106 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_BACK_LEFT
;
108 return BUFFER_BIT_FRONT_LEFT
;
110 return BUFFER_BIT_AUX0
;
114 return 1 << BUFFER_COUNT
; /* invalid, but not BAD_MASK */
123 * Helper routine used by glReadBuffer.
124 * Given a GLenum naming a color buffer, return the index of the corresponding
125 * renderbuffer (a BUFFER_* value).
126 * return -1 for an invalid buffer.
129 read_buffer_enum_to_index(GLenum buffer
)
133 return BUFFER_FRONT_LEFT
;
135 return BUFFER_BACK_LEFT
;
137 return BUFFER_FRONT_RIGHT
;
139 return BUFFER_FRONT_RIGHT
;
141 return BUFFER_BACK_RIGHT
;
143 return BUFFER_BACK_LEFT
;
145 return BUFFER_FRONT_LEFT
;
147 return BUFFER_FRONT_LEFT
;
153 return BUFFER_COUNT
; /* invalid, but not -1 */
162 * Called by glDrawBuffer().
163 * Specify which renderbuffer(s) to draw into for the first color output.
164 * <buffer> can name zero, one, two or four renderbuffers!
165 * \sa _mesa_DrawBuffersARB
167 * \param buffer buffer token such as GL_LEFT or GL_FRONT_AND_BACK, etc.
169 * Note that the behaviour of this function depends on whether the
170 * current ctx->DrawBuffer is a window-system framebuffer (Name=0) or
171 * a user-created framebuffer object (Name!=0).
172 * In the former case, we update the per-context ctx->Color.DrawBuffer
173 * state var _and_ the FB's ColorDrawBuffer state.
174 * In the later case, we update the FB's ColorDrawBuffer state only.
176 * Furthermore, upon a MakeCurrent() or BindFramebuffer() call, if the
177 * new FB is a window system FB, we need to re-update the FB's
178 * ColorDrawBuffer state to match the context. This is handled in
179 * _mesa_update_framebuffer().
181 * See the GL_EXT_framebuffer_object spec for more info.
184 _mesa_DrawBuffer(GLenum buffer
)
187 GET_CURRENT_CONTEXT(ctx
);
188 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex... */
190 if (MESA_VERBOSE
& VERBOSE_API
) {
191 _mesa_debug(ctx
, "glDrawBuffer %s\n", _mesa_lookup_enum_by_nr(buffer
));
194 if (buffer
== GL_NONE
) {
198 const GLbitfield supportedMask
199 = supported_buffer_bitmask(ctx
, ctx
->DrawBuffer
);
200 destMask
= draw_buffer_enum_to_bitmask(buffer
);
201 if (destMask
== BAD_MASK
) {
202 /* totally bogus buffer */
203 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawBuffer(buffer=0x%x)", buffer
);
206 destMask
&= supportedMask
;
207 if (destMask
== 0x0) {
208 /* none of the named color buffers exist! */
209 _mesa_error(ctx
, GL_INVALID_OPERATION
,
210 "glDrawBuffer(buffer=0x%x)", buffer
);
215 /* if we get here, there's no error so set new state */
216 _mesa_drawbuffer(ctx
, buffer
, destMask
);
219 * Call device driver function.
221 if (ctx
->Driver
.DrawBuffer
)
222 ctx
->Driver
.DrawBuffer(ctx
, buffer
);
226 * Performs necessary state updates when _mesa_drawbuffers makes an
230 updated_drawbuffers(struct gl_context
*ctx
)
232 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
236 * Helper function to set the GL_DRAW_BUFFER state in the context and
237 * current FBO. Called via glDrawBuffer(), glDrawBuffersARB()
239 * All error checking will have been done prior to calling this function
240 * so nothing should go wrong at this point.
242 * \param ctx current context
243 * \param n number of color outputs to set
244 * \param buffers array[n] of colorbuffer names, like GL_LEFT.
245 * \param destMask array[n] of BUFFER_BIT_* bitmasks which correspond to the
246 * colorbuffer names. (i.e. GL_FRONT_AND_BACK =>
247 * BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT).
250 _mesa_drawbuffer(struct gl_context
*ctx
, const GLenum buffers
, GLbitfield destMask
)
252 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
256 /* compute destMask values now */
257 const GLbitfield supportedMask
= supported_buffer_bitmask(ctx
, fb
);
258 destMask
= draw_buffer_enum_to_bitmask(buffers
);
259 ASSERT(destmask
!= BAD_MASK
);
260 destMask
&= supportedMask
;
263 bufIndex
= _mesa_ffs(destMask
) - 1;
265 if (fb
->_ColorDrawBufferIndex
!= bufIndex
) {
266 updated_drawbuffers(ctx
);
267 fb
->_ColorDrawBufferIndex
= bufIndex
;
269 fb
->ColorDrawBuffer
= buffers
;
271 if (ctx
->Color
.DrawBuffer
!= fb
->ColorDrawBuffer
) {
272 updated_drawbuffers(ctx
);
273 ctx
->Color
.DrawBuffer
= fb
->ColorDrawBuffer
;
279 * Update the current drawbuffer's _ColorDrawBufferIndex[] list, etc.
280 * from the context's Color.DrawBuffer[] state.
281 * Use when changing contexts.
284 _mesa_update_draw_buffer(struct gl_context
*ctx
)
286 _mesa_drawbuffer(ctx
, ctx
->Color
.DrawBuffer
, 0);
291 * Like \sa _mesa_drawbuffers(), this is a helper function for setting
292 * GL_READ_BUFFER state in the context and current FBO.
293 * \param ctx the rendering context
294 * \param buffer GL_FRONT, GL_BACK, GL_COLOR_ATTACHMENT0, etc.
295 * \param bufferIndex the numerical index corresponding to 'buffer'
298 _mesa_readbuffer(struct gl_context
*ctx
, GLenum buffer
, GLint bufferIndex
)
300 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
302 ctx
->Pixel
.ReadBuffer
= buffer
;
304 fb
->ColorReadBuffer
= buffer
;
305 fb
->_ColorReadBufferIndex
= bufferIndex
;
307 ctx
->NewState
|= _NEW_BUFFERS
;
313 * Called by glReadBuffer to set the source renderbuffer for reading pixels.
314 * \param mode color buffer such as GL_FRONT, GL_BACK, etc.
317 _mesa_ReadBuffer(GLenum buffer
)
319 struct gl_framebuffer
*fb
;
320 GLbitfield supportedMask
;
322 GET_CURRENT_CONTEXT(ctx
);
323 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
325 if (MESA_VERBOSE
& VERBOSE_API
)
326 _mesa_debug(ctx
, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer
));
328 fb
= ctx
->ReadBuffer
;
330 if (MESA_VERBOSE
& VERBOSE_API
)
331 _mesa_debug(ctx
, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer
));
333 /* general case / window-system framebuffer */
334 srcBuffer
= read_buffer_enum_to_index(buffer
);
335 if (srcBuffer
== -1) {
336 _mesa_error(ctx
, GL_INVALID_ENUM
,
337 "glReadBuffer(buffer=0x%x)", buffer
);
340 supportedMask
= supported_buffer_bitmask(ctx
, fb
);
341 if (((1 << srcBuffer
) & supportedMask
) == 0) {
342 _mesa_error(ctx
, GL_INVALID_OPERATION
,
343 "glReadBuffer(buffer=0x%x)", buffer
);
347 /* OK, all error checking has been completed now */
349 _mesa_readbuffer(ctx
, buffer
, srcBuffer
);
350 ctx
->NewState
|= _NEW_BUFFERS
;
353 * Call device driver function.
355 if (ctx
->Driver
.ReadBuffer
)
356 (*ctx
->Driver
.ReadBuffer
)(ctx
, buffer
);