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.
27 * glReadBuffer, DrawBuffer functions.
35 * Return bitmask of BUFFER_BIT_* flags indicating which color buffers are
36 * available to the rendering context (for drawing or reading).
37 * This depends on the type of framebuffer. For window system framebuffers
38 * we look at the framebuffer's visual. But for user-create framebuffers we
39 * look at the number of supported color attachments.
40 * \param fb the framebuffer to draw to, or read from
41 * \return bitmask of BUFFER_BIT_* flags
44 supported_buffer_bitmask(const struct gl_context
*ctx
, const struct gl_framebuffer
*fb
)
46 GLbitfield mask
= 0x0;
48 /* A window system framebuffer */
50 mask
= BUFFER_BIT_FRONT_LEFT
; /* always have this */
51 if (fb
->Visual
.stereoMode
) {
52 mask
|= BUFFER_BIT_FRONT_RIGHT
;
53 if (fb
->Visual
.doubleBufferMode
) {
54 mask
|= BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_BACK_RIGHT
;
57 else if (fb
->Visual
.doubleBufferMode
) {
58 mask
|= BUFFER_BIT_BACK_LEFT
;
61 for (i
= 0; i
< fb
->Visual
.numAuxBuffers
; i
++) {
62 mask
|= (BUFFER_BIT_AUX0
<< i
);
70 * Helper routine used by glDrawBuffer and glDrawBuffersARB.
71 * Given a GLenum naming one or more color buffers (such as
72 * GL_FRONT_AND_BACK), return the corresponding bitmask of BUFFER_BIT_* flags.
75 draw_buffer_enum_to_bitmask(GLenum buffer
)
81 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_FRONT_RIGHT
;
83 return BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_BACK_RIGHT
;
85 return BUFFER_BIT_FRONT_RIGHT
| BUFFER_BIT_BACK_RIGHT
;
87 return BUFFER_BIT_FRONT_RIGHT
;
89 return BUFFER_BIT_BACK_RIGHT
;
91 return BUFFER_BIT_BACK_LEFT
;
92 case GL_FRONT_AND_BACK
:
93 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_BACK_LEFT
94 | BUFFER_BIT_FRONT_RIGHT
| BUFFER_BIT_BACK_RIGHT
;
96 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_BACK_LEFT
;
98 return BUFFER_BIT_FRONT_LEFT
;
100 return BUFFER_BIT_AUX0
;
104 return 1 << BUFFER_COUNT
; /* invalid, but not BAD_MASK */
113 * Helper routine used by glReadBuffer.
114 * Given a GLenum naming a color buffer, return the index of the corresponding
115 * renderbuffer (a BUFFER_* value).
116 * return -1 for an invalid buffer.
119 read_buffer_enum_to_index(GLenum buffer
)
123 return BUFFER_FRONT_LEFT
;
125 return BUFFER_BACK_LEFT
;
127 return BUFFER_FRONT_RIGHT
;
129 return BUFFER_FRONT_RIGHT
;
131 return BUFFER_BACK_RIGHT
;
133 return BUFFER_BACK_LEFT
;
135 return BUFFER_FRONT_LEFT
;
137 return BUFFER_FRONT_LEFT
;
143 return BUFFER_COUNT
; /* invalid, but not -1 */
152 * Called by glDrawBuffer().
153 * Specify which renderbuffer(s) to draw into for the first color output.
154 * <buffer> can name zero, one, two or four renderbuffers!
155 * \sa _mesa_DrawBuffersARB
157 * \param buffer buffer token such as GL_LEFT or GL_FRONT_AND_BACK, etc.
159 * Note that the behaviour of this function depends on whether the
160 * current ctx->DrawBuffer is a window-system framebuffer (Name=0) or
161 * a user-created framebuffer object (Name!=0).
162 * In the former case, we update the per-context ctx->Color.DrawBuffer
163 * state var _and_ the FB's ColorDrawBuffer state.
164 * In the later case, we update the FB's ColorDrawBuffer state only.
166 * Furthermore, upon a MakeCurrent() or BindFramebuffer() call, if the
167 * new FB is a window system FB, we need to re-update the FB's
168 * ColorDrawBuffer state to match the context. This is handled in
169 * _mesa_update_framebuffer().
171 * See the GL_EXT_framebuffer_object spec for more info.
174 _mesa_DrawBuffer(GLenum buffer
)
177 GET_CURRENT_CONTEXT(ctx
);
178 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex... */
180 if (MESA_VERBOSE
& VERBOSE_API
) {
181 _mesa_debug(ctx
, "glDrawBuffer %s\n", _mesa_lookup_enum_by_nr(buffer
));
184 if (buffer
== GL_NONE
) {
188 const GLbitfield supportedMask
189 = supported_buffer_bitmask(ctx
, ctx
->DrawBuffer
);
190 destMask
= draw_buffer_enum_to_bitmask(buffer
);
191 if (destMask
== BAD_MASK
) {
192 /* totally bogus buffer */
193 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawBuffer(buffer=0x%x)", buffer
);
196 destMask
&= supportedMask
;
197 if (destMask
== 0x0) {
198 /* none of the named color buffers exist! */
199 _mesa_error(ctx
, GL_INVALID_OPERATION
,
200 "glDrawBuffer(buffer=0x%x)", buffer
);
205 /* if we get here, there's no error so set new state */
206 _mesa_drawbuffer(ctx
, buffer
, destMask
);
209 * Call device driver function.
211 if (ctx
->Driver
.DrawBuffer
)
212 ctx
->Driver
.DrawBuffer(ctx
, buffer
);
216 * Performs necessary state updates when _mesa_drawbuffers makes an
220 updated_drawbuffers(struct gl_context
*ctx
)
222 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
226 * Helper function to set the GL_DRAW_BUFFER state in the context and
227 * current FBO. Called via glDrawBuffer(), glDrawBuffersARB()
229 * All error checking will have been done prior to calling this function
230 * so nothing should go wrong at this point.
232 * \param ctx current context
233 * \param n number of color outputs to set
234 * \param buffers array[n] of colorbuffer names, like GL_LEFT.
235 * \param destMask array[n] of BUFFER_BIT_* bitmasks which correspond to the
236 * colorbuffer names. (i.e. GL_FRONT_AND_BACK =>
237 * BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT).
240 _mesa_drawbuffer(struct gl_context
*ctx
, const GLenum buffers
, GLbitfield destMask
)
242 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
246 /* compute destMask values now */
247 const GLbitfield supportedMask
= supported_buffer_bitmask(ctx
, fb
);
248 destMask
= draw_buffer_enum_to_bitmask(buffers
);
249 ASSERT(destmask
!= BAD_MASK
);
250 destMask
&= supportedMask
;
253 bufIndex
= _mesa_ffs(destMask
) - 1;
255 if (fb
->_ColorDrawBufferIndex
!= bufIndex
) {
256 updated_drawbuffers(ctx
);
257 fb
->_ColorDrawBufferIndex
= bufIndex
;
259 fb
->ColorDrawBuffer
= buffers
;
261 if (ctx
->Color
.DrawBuffer
!= fb
->ColorDrawBuffer
) {
262 updated_drawbuffers(ctx
);
263 ctx
->Color
.DrawBuffer
= fb
->ColorDrawBuffer
;
269 * Update the current drawbuffer's _ColorDrawBufferIndex[] list, etc.
270 * from the context's Color.DrawBuffer[] state.
271 * Use when changing contexts.
274 _mesa_update_draw_buffer(struct gl_context
*ctx
)
276 _mesa_drawbuffer(ctx
, ctx
->Color
.DrawBuffer
, 0);
281 * Like \sa _mesa_drawbuffers(), this is a helper function for setting
282 * GL_READ_BUFFER state in the context and current FBO.
283 * \param ctx the rendering context
284 * \param buffer GL_FRONT, GL_BACK, GL_COLOR_ATTACHMENT0, etc.
285 * \param bufferIndex the numerical index corresponding to 'buffer'
288 _mesa_readbuffer(struct gl_context
*ctx
, GLenum buffer
, GLint bufferIndex
)
290 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
292 ctx
->Pixel
.ReadBuffer
= buffer
;
294 fb
->ColorReadBuffer
= buffer
;
295 fb
->_ColorReadBufferIndex
= bufferIndex
;
297 ctx
->NewState
|= _NEW_BUFFERS
;
303 * Called by glReadBuffer to set the source renderbuffer for reading pixels.
304 * \param mode color buffer such as GL_FRONT, GL_BACK, etc.
307 _mesa_ReadBuffer(GLenum buffer
)
309 struct gl_framebuffer
*fb
;
310 GLbitfield supportedMask
;
312 GET_CURRENT_CONTEXT(ctx
);
313 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
315 if (MESA_VERBOSE
& VERBOSE_API
)
316 _mesa_debug(ctx
, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer
));
318 fb
= ctx
->ReadBuffer
;
320 if (MESA_VERBOSE
& VERBOSE_API
)
321 _mesa_debug(ctx
, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer
));
323 /* general case / window-system framebuffer */
324 srcBuffer
= read_buffer_enum_to_index(buffer
);
325 if (srcBuffer
== -1) {
326 _mesa_error(ctx
, GL_INVALID_ENUM
,
327 "glReadBuffer(buffer=0x%x)", buffer
);
330 supportedMask
= supported_buffer_bitmask(ctx
, fb
);
331 if (((1 << srcBuffer
) & supportedMask
) == 0) {
332 _mesa_error(ctx
, GL_INVALID_OPERATION
,
333 "glReadBuffer(buffer=0x%x)", buffer
);
337 /* OK, all error checking has been completed now */
339 _mesa_readbuffer(ctx
, buffer
, srcBuffer
);
340 ctx
->NewState
|= _NEW_BUFFERS
;
343 * Call device driver function.
345 if (ctx
->Driver
.ReadBuffer
)
346 (*ctx
->Driver
.ReadBuffer
)(ctx
, buffer
);