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;
59 /* A user-created renderbuffer */
61 ASSERT(ctx
->Extensions
.EXT_framebuffer_object
);
62 for (i
= 0; i
< ctx
->Const
.MaxColorAttachments
; i
++) {
63 mask
|= (BUFFER_BIT_COLOR0
<< i
);
67 /* A window system framebuffer */
69 mask
= BUFFER_BIT_FRONT_LEFT
; /* always have this */
70 if (fb
->Visual
.stereoMode
) {
71 mask
|= BUFFER_BIT_FRONT_RIGHT
;
72 if (fb
->Visual
.doubleBufferMode
) {
73 mask
|= BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_BACK_RIGHT
;
76 else if (fb
->Visual
.doubleBufferMode
) {
77 mask
|= BUFFER_BIT_BACK_LEFT
;
80 for (i
= 0; i
< fb
->Visual
.numAuxBuffers
; i
++) {
81 mask
|= (BUFFER_BIT_AUX0
<< i
);
90 * Helper routine used by glDrawBuffer and glDrawBuffersARB.
91 * Given a GLenum naming one or more color buffers (such as
92 * GL_FRONT_AND_BACK), return the corresponding bitmask of BUFFER_BIT_* flags.
95 draw_buffer_enum_to_bitmask(GLenum buffer
)
101 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_FRONT_RIGHT
;
103 return BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_BACK_RIGHT
;
105 return BUFFER_BIT_FRONT_RIGHT
| BUFFER_BIT_BACK_RIGHT
;
107 return BUFFER_BIT_FRONT_RIGHT
;
109 return BUFFER_BIT_BACK_RIGHT
;
111 return BUFFER_BIT_BACK_LEFT
;
112 case GL_FRONT_AND_BACK
:
113 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_BACK_LEFT
114 | BUFFER_BIT_FRONT_RIGHT
| BUFFER_BIT_BACK_RIGHT
;
116 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_BACK_LEFT
;
118 return BUFFER_BIT_FRONT_LEFT
;
120 return BUFFER_BIT_AUX0
;
124 return 1 << BUFFER_COUNT
; /* invalid, but not BAD_MASK */
125 case GL_COLOR_ATTACHMENT0_EXT
:
126 return BUFFER_BIT_COLOR0
;
127 case GL_COLOR_ATTACHMENT1_EXT
:
128 return BUFFER_BIT_COLOR1
;
129 case GL_COLOR_ATTACHMENT2_EXT
:
130 return BUFFER_BIT_COLOR2
;
131 case GL_COLOR_ATTACHMENT3_EXT
:
132 return BUFFER_BIT_COLOR3
;
133 case GL_COLOR_ATTACHMENT4_EXT
:
134 return BUFFER_BIT_COLOR4
;
135 case GL_COLOR_ATTACHMENT5_EXT
:
136 return BUFFER_BIT_COLOR5
;
137 case GL_COLOR_ATTACHMENT6_EXT
:
138 return BUFFER_BIT_COLOR6
;
139 case GL_COLOR_ATTACHMENT7_EXT
:
140 return BUFFER_BIT_COLOR7
;
149 * Helper routine used by glReadBuffer.
150 * Given a GLenum naming a color buffer, return the index of the corresponding
151 * renderbuffer (a BUFFER_* value).
152 * return -1 for an invalid buffer.
155 read_buffer_enum_to_index(GLenum buffer
)
159 return BUFFER_FRONT_LEFT
;
161 return BUFFER_BACK_LEFT
;
163 return BUFFER_FRONT_RIGHT
;
165 return BUFFER_FRONT_RIGHT
;
167 return BUFFER_BACK_RIGHT
;
169 return BUFFER_BACK_LEFT
;
171 return BUFFER_FRONT_LEFT
;
173 return BUFFER_FRONT_LEFT
;
179 return BUFFER_COUNT
; /* invalid, but not -1 */
180 case GL_COLOR_ATTACHMENT0_EXT
:
181 return BUFFER_COLOR0
;
182 case GL_COLOR_ATTACHMENT1_EXT
:
183 return BUFFER_COLOR1
;
184 case GL_COLOR_ATTACHMENT2_EXT
:
185 return BUFFER_COLOR2
;
186 case GL_COLOR_ATTACHMENT3_EXT
:
187 return BUFFER_COLOR3
;
188 case GL_COLOR_ATTACHMENT4_EXT
:
189 return BUFFER_COLOR4
;
190 case GL_COLOR_ATTACHMENT5_EXT
:
191 return BUFFER_COLOR5
;
192 case GL_COLOR_ATTACHMENT6_EXT
:
193 return BUFFER_COLOR6
;
194 case GL_COLOR_ATTACHMENT7_EXT
:
195 return BUFFER_COLOR7
;
204 * Called by glDrawBuffer().
205 * Specify which renderbuffer(s) to draw into for the first color output.
206 * <buffer> can name zero, one, two or four renderbuffers!
207 * \sa _mesa_DrawBuffersARB
209 * \param buffer buffer token such as GL_LEFT or GL_FRONT_AND_BACK, etc.
211 * Note that the behaviour of this function depends on whether the
212 * current ctx->DrawBuffer is a window-system framebuffer (Name=0) or
213 * a user-created framebuffer object (Name!=0).
214 * In the former case, we update the per-context ctx->Color.DrawBuffer
215 * state var _and_ the FB's ColorDrawBuffer state.
216 * In the later case, we update the FB's ColorDrawBuffer state only.
218 * Furthermore, upon a MakeCurrent() or BindFramebuffer() call, if the
219 * new FB is a window system FB, we need to re-update the FB's
220 * ColorDrawBuffer state to match the context. This is handled in
221 * _mesa_update_framebuffer().
223 * See the GL_EXT_framebuffer_object spec for more info.
226 _mesa_DrawBuffer(GLenum buffer
)
229 GET_CURRENT_CONTEXT(ctx
);
230 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex... */
232 if (MESA_VERBOSE
& VERBOSE_API
) {
233 _mesa_debug(ctx
, "glDrawBuffer %s\n", _mesa_lookup_enum_by_nr(buffer
));
236 if (buffer
== GL_NONE
) {
240 const GLbitfield supportedMask
241 = supported_buffer_bitmask(ctx
, ctx
->DrawBuffer
);
242 destMask
= draw_buffer_enum_to_bitmask(buffer
);
243 if (destMask
== BAD_MASK
) {
244 /* totally bogus buffer */
245 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawBuffer(buffer=0x%x)", buffer
);
248 destMask
&= supportedMask
;
249 if (destMask
== 0x0) {
250 /* none of the named color buffers exist! */
251 _mesa_error(ctx
, GL_INVALID_OPERATION
,
252 "glDrawBuffer(buffer=0x%x)", buffer
);
257 /* if we get here, there's no error so set new state */
258 _mesa_drawbuffers(ctx
, 1, &buffer
, &destMask
);
261 * Call device driver function.
263 if (ctx
->Driver
.DrawBuffers
)
264 ctx
->Driver
.DrawBuffers(ctx
, 1, &buffer
);
265 else if (ctx
->Driver
.DrawBuffer
)
266 ctx
->Driver
.DrawBuffer(ctx
, buffer
);
271 * Called by glDrawBuffersARB; specifies the destination color renderbuffers
272 * for N fragment program color outputs.
273 * \sa _mesa_DrawBuffer
274 * \param n number of outputs
275 * \param buffers array [n] of renderbuffer names. Unlike glDrawBuffer, the
276 * names cannot specify more than one buffer. For example,
277 * GL_FRONT_AND_BACK is illegal.
280 _mesa_DrawBuffersARB(GLsizei n
, const GLenum
*buffers
)
283 GLbitfield usedBufferMask
, supportedMask
;
284 GLbitfield destMask
[MAX_DRAW_BUFFERS
];
285 GET_CURRENT_CONTEXT(ctx
);
286 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
288 /* Turns out n==0 is a valid input that should not produce an error.
289 * The remaining code below correctly handles the n==0 case.
291 if (n
< 0 || n
> (GLsizei
) ctx
->Const
.MaxDrawBuffers
) {
292 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawBuffersARB(n)");
296 supportedMask
= supported_buffer_bitmask(ctx
, ctx
->DrawBuffer
);
297 usedBufferMask
= 0x0;
299 /* complicated error checking... */
300 for (output
= 0; output
< n
; output
++) {
301 if (buffers
[output
] == GL_NONE
) {
302 destMask
[output
] = 0x0;
305 destMask
[output
] = draw_buffer_enum_to_bitmask(buffers
[output
]);
306 if (destMask
[output
] == BAD_MASK
307 || _mesa_bitcount(destMask
[output
]) > 1) {
308 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawBuffersARB(buffer)");
311 destMask
[output
] &= supportedMask
;
312 if (destMask
[output
] == 0) {
313 _mesa_error(ctx
, GL_INVALID_OPERATION
,
314 "glDrawBuffersARB(unsupported buffer)");
317 if (destMask
[output
] & usedBufferMask
) {
318 /* can't specify a dest buffer more than once! */
319 _mesa_error(ctx
, GL_INVALID_OPERATION
,
320 "glDrawBuffersARB(duplicated buffer)");
325 usedBufferMask
|= destMask
[output
];
329 /* OK, if we get here, there were no errors so set the new state */
330 _mesa_drawbuffers(ctx
, n
, buffers
, destMask
);
333 * Call device driver function. Note that n can be equal to 0,
334 * in which case we don't want to reference buffers[0], which
337 if (ctx
->Driver
.DrawBuffers
)
338 ctx
->Driver
.DrawBuffers(ctx
, n
, buffers
);
339 else if (ctx
->Driver
.DrawBuffer
)
340 ctx
->Driver
.DrawBuffer(ctx
, n
> 0 ? buffers
[0] : GL_NONE
);
344 * Performs necessary state updates when _mesa_drawbuffers makes an
348 updated_drawbuffers(struct gl_context
*ctx
)
350 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
353 if (ctx
->API
== API_OPENGL
&& !ctx
->Extensions
.ARB_ES2_compatibility
) {
354 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
356 /* Flag the FBO as requiring validation. */
365 * Helper function to set the GL_DRAW_BUFFER state in the context and
366 * current FBO. Called via glDrawBuffer(), glDrawBuffersARB()
368 * All error checking will have been done prior to calling this function
369 * so nothing should go wrong at this point.
371 * \param ctx current context
372 * \param n number of color outputs to set
373 * \param buffers array[n] of colorbuffer names, like GL_LEFT.
374 * \param destMask array[n] of BUFFER_BIT_* bitmasks which correspond to the
375 * colorbuffer names. (i.e. GL_FRONT_AND_BACK =>
376 * BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT).
379 _mesa_drawbuffers(struct gl_context
*ctx
, GLuint n
, const GLenum
*buffers
,
380 const GLbitfield
*destMask
)
382 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
383 GLbitfield mask
[MAX_DRAW_BUFFERS
];
387 /* compute destMask values now */
388 const GLbitfield supportedMask
= supported_buffer_bitmask(ctx
, fb
);
390 for (output
= 0; output
< n
; output
++) {
391 mask
[output
] = draw_buffer_enum_to_bitmask(buffers
[output
]);
392 ASSERT(mask
[output
] != BAD_MASK
);
393 mask
[output
] &= supportedMask
;
399 * If n==1, destMask[0] may have up to four bits set.
400 * Otherwise, destMask[x] can only have one bit set.
403 GLuint count
= 0, destMask0
= destMask
[0];
405 GLint bufIndex
= _mesa_ffs(destMask0
) - 1;
406 if (fb
->_ColorDrawBufferIndexes
[count
] != bufIndex
) {
407 updated_drawbuffers(ctx
);
408 fb
->_ColorDrawBufferIndexes
[count
] = bufIndex
;
411 destMask0
&= ~(1 << bufIndex
);
413 fb
->ColorDrawBuffer
[0] = buffers
[0];
414 fb
->_NumColorDrawBuffers
= count
;
418 for (buf
= 0; buf
< n
; buf
++ ) {
420 GLint bufIndex
= _mesa_ffs(destMask
[buf
]) - 1;
421 /* only one bit should be set in the destMask[buf] field */
422 ASSERT(_mesa_bitcount(destMask
[buf
]) == 1);
423 if (fb
->_ColorDrawBufferIndexes
[buf
] != bufIndex
) {
424 updated_drawbuffers(ctx
);
425 fb
->_ColorDrawBufferIndexes
[buf
] = bufIndex
;
430 if (fb
->_ColorDrawBufferIndexes
[buf
] != -1) {
431 updated_drawbuffers(ctx
);
432 fb
->_ColorDrawBufferIndexes
[buf
] = -1;
435 fb
->ColorDrawBuffer
[buf
] = buffers
[buf
];
437 fb
->_NumColorDrawBuffers
= count
;
440 /* set remaining outputs to -1 (GL_NONE) */
441 for (buf
= fb
->_NumColorDrawBuffers
; buf
< ctx
->Const
.MaxDrawBuffers
; buf
++) {
442 if (fb
->_ColorDrawBufferIndexes
[buf
] != -1) {
443 updated_drawbuffers(ctx
);
444 fb
->_ColorDrawBufferIndexes
[buf
] = -1;
447 for (buf
= n
; buf
< ctx
->Const
.MaxDrawBuffers
; buf
++) {
448 fb
->ColorDrawBuffer
[buf
] = GL_NONE
;
452 /* also set context drawbuffer state */
453 for (buf
= 0; buf
< ctx
->Const
.MaxDrawBuffers
; buf
++) {
454 if (ctx
->Color
.DrawBuffer
[buf
] != fb
->ColorDrawBuffer
[buf
]) {
455 updated_drawbuffers(ctx
);
456 ctx
->Color
.DrawBuffer
[buf
] = fb
->ColorDrawBuffer
[buf
];
464 * Update the current drawbuffer's _ColorDrawBufferIndex[] list, etc.
465 * from the context's Color.DrawBuffer[] state.
466 * Use when changing contexts.
469 _mesa_update_draw_buffers(struct gl_context
*ctx
)
471 GLenum buffers
[MAX_DRAW_BUFFERS
];
474 /* should be a window system FBO */
475 assert(ctx
->DrawBuffer
->Name
== 0);
477 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++)
478 buffers
[i
] = ctx
->Color
.DrawBuffer
[i
];
480 _mesa_drawbuffers(ctx
, ctx
->Const
.MaxDrawBuffers
, buffers
, NULL
);
485 * Like \sa _mesa_drawbuffers(), this is a helper function for setting
486 * GL_READ_BUFFER state in the context and current FBO.
487 * \param ctx the rendering context
488 * \param buffer GL_FRONT, GL_BACK, GL_COLOR_ATTACHMENT0, etc.
489 * \param bufferIndex the numerical index corresponding to 'buffer'
492 _mesa_readbuffer(struct gl_context
*ctx
, GLenum buffer
, GLint bufferIndex
)
494 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
497 /* Only update the per-context READ_BUFFER state if we're bound to
498 * a window-system framebuffer.
500 ctx
->Pixel
.ReadBuffer
= buffer
;
503 fb
->ColorReadBuffer
= buffer
;
504 fb
->_ColorReadBufferIndex
= bufferIndex
;
506 ctx
->NewState
|= _NEW_BUFFERS
;
512 * Called by glReadBuffer to set the source renderbuffer for reading pixels.
513 * \param mode color buffer such as GL_FRONT, GL_BACK, etc.
516 _mesa_ReadBuffer(GLenum buffer
)
518 struct gl_framebuffer
*fb
;
519 GLbitfield supportedMask
;
521 GET_CURRENT_CONTEXT(ctx
);
522 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
524 if (MESA_VERBOSE
& VERBOSE_API
)
525 _mesa_debug(ctx
, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer
));
527 fb
= ctx
->ReadBuffer
;
529 if (MESA_VERBOSE
& VERBOSE_API
)
530 _mesa_debug(ctx
, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer
));
532 if (fb
->Name
> 0 && buffer
== GL_NONE
) {
533 /* This is legal for user-created framebuffer objects */
537 /* general case / window-system framebuffer */
538 srcBuffer
= read_buffer_enum_to_index(buffer
);
539 if (srcBuffer
== -1) {
540 _mesa_error(ctx
, GL_INVALID_ENUM
,
541 "glReadBuffer(buffer=0x%x)", buffer
);
544 supportedMask
= supported_buffer_bitmask(ctx
, fb
);
545 if (((1 << srcBuffer
) & supportedMask
) == 0) {
546 _mesa_error(ctx
, GL_INVALID_OPERATION
,
547 "glReadBuffer(buffer=0x%x)", buffer
);
552 /* OK, all error checking has been completed now */
554 _mesa_readbuffer(ctx
, buffer
, srcBuffer
);
555 ctx
->NewState
|= _NEW_BUFFERS
;
558 * Call device driver function.
560 if (ctx
->Driver
.ReadBuffer
)
561 (*ctx
->Driver
.ReadBuffer
)(ctx
, buffer
);