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.
46 * Return bitmask of BUFFER_BIT_* flags indicating which color buffers are
47 * available to the rendering context (for drawing or reading).
48 * This depends on the type of framebuffer. For window system framebuffers
49 * we look at the framebuffer's visual. But for user-create framebuffers we
50 * look at the number of supported color attachments.
51 * \param fb the framebuffer to draw to, or read from
52 * \return bitmask of BUFFER_BIT_* flags
55 supported_buffer_bitmask(const GLcontext
*ctx
, const struct gl_framebuffer
*fb
)
57 GLbitfield mask
= 0x0;
60 /* A user-created renderbuffer */
62 ASSERT(ctx
->Extensions
.EXT_framebuffer_object
);
63 for (i
= 0; i
< ctx
->Const
.MaxColorAttachments
; i
++) {
64 mask
|= (BUFFER_BIT_COLOR0
<< i
);
68 /* A window system framebuffer */
70 mask
= BUFFER_BIT_FRONT_LEFT
; /* always have this */
71 if (fb
->Visual
.stereoMode
) {
72 mask
|= BUFFER_BIT_FRONT_RIGHT
;
73 if (fb
->Visual
.doubleBufferMode
) {
74 mask
|= BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_BACK_RIGHT
;
77 else if (fb
->Visual
.doubleBufferMode
) {
78 mask
|= BUFFER_BIT_BACK_LEFT
;
81 for (i
= 0; i
< fb
->Visual
.numAuxBuffers
; i
++) {
82 mask
|= (BUFFER_BIT_AUX0
<< i
);
91 * Helper routine used by glDrawBuffer and glDrawBuffersARB.
92 * Given a GLenum naming one or more color buffers (such as
93 * GL_FRONT_AND_BACK), return the corresponding bitmask of BUFFER_BIT_* flags.
96 draw_buffer_enum_to_bitmask(GLenum buffer
)
102 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_FRONT_RIGHT
;
104 return BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_BACK_RIGHT
;
106 return BUFFER_BIT_FRONT_RIGHT
| BUFFER_BIT_BACK_RIGHT
;
108 return BUFFER_BIT_FRONT_RIGHT
;
110 return BUFFER_BIT_BACK_RIGHT
;
112 return BUFFER_BIT_BACK_LEFT
;
113 case GL_FRONT_AND_BACK
:
114 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_BACK_LEFT
115 | BUFFER_BIT_FRONT_RIGHT
| BUFFER_BIT_BACK_RIGHT
;
117 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_BACK_LEFT
;
119 return BUFFER_BIT_FRONT_LEFT
;
121 return BUFFER_BIT_AUX0
;
125 return 1 << BUFFER_COUNT
; /* invalid, but not BAD_MASK */
126 case GL_COLOR_ATTACHMENT0_EXT
:
127 return BUFFER_BIT_COLOR0
;
128 case GL_COLOR_ATTACHMENT1_EXT
:
129 return BUFFER_BIT_COLOR1
;
130 case GL_COLOR_ATTACHMENT2_EXT
:
131 return BUFFER_BIT_COLOR2
;
132 case GL_COLOR_ATTACHMENT3_EXT
:
133 return BUFFER_BIT_COLOR3
;
134 case GL_COLOR_ATTACHMENT4_EXT
:
135 return BUFFER_BIT_COLOR4
;
136 case GL_COLOR_ATTACHMENT5_EXT
:
137 return BUFFER_BIT_COLOR5
;
138 case GL_COLOR_ATTACHMENT6_EXT
:
139 return BUFFER_BIT_COLOR6
;
140 case GL_COLOR_ATTACHMENT7_EXT
:
141 return BUFFER_BIT_COLOR7
;
150 * Helper routine used by glReadBuffer.
151 * Given a GLenum naming a color buffer, return the index of the corresponding
152 * renderbuffer (a BUFFER_* value).
153 * return -1 for an invalid buffer.
156 read_buffer_enum_to_index(GLenum buffer
)
160 return BUFFER_FRONT_LEFT
;
162 return BUFFER_BACK_LEFT
;
164 return BUFFER_FRONT_RIGHT
;
166 return BUFFER_FRONT_RIGHT
;
168 return BUFFER_BACK_RIGHT
;
170 return BUFFER_BACK_LEFT
;
172 return BUFFER_FRONT_LEFT
;
174 return BUFFER_FRONT_LEFT
;
180 return BUFFER_COUNT
; /* invalid, but not -1 */
181 case GL_COLOR_ATTACHMENT0_EXT
:
182 return BUFFER_COLOR0
;
183 case GL_COLOR_ATTACHMENT1_EXT
:
184 return BUFFER_COLOR1
;
185 case GL_COLOR_ATTACHMENT2_EXT
:
186 return BUFFER_COLOR2
;
187 case GL_COLOR_ATTACHMENT3_EXT
:
188 return BUFFER_COLOR3
;
189 case GL_COLOR_ATTACHMENT4_EXT
:
190 return BUFFER_COLOR4
;
191 case GL_COLOR_ATTACHMENT5_EXT
:
192 return BUFFER_COLOR5
;
193 case GL_COLOR_ATTACHMENT6_EXT
:
194 return BUFFER_COLOR6
;
195 case GL_COLOR_ATTACHMENT7_EXT
:
196 return BUFFER_COLOR7
;
205 * Called by glDrawBuffer().
206 * Specify which renderbuffer(s) to draw into for the first color output.
207 * <buffer> can name zero, one, two or four renderbuffers!
208 * \sa _mesa_DrawBuffersARB
210 * \param buffer buffer token such as GL_LEFT or GL_FRONT_AND_BACK, etc.
212 * Note that the behaviour of this function depends on whether the
213 * current ctx->DrawBuffer is a window-system framebuffer (Name=0) or
214 * a user-created framebuffer object (Name!=0).
215 * In the former case, we update the per-context ctx->Color.DrawBuffer
216 * state var _and_ the FB's ColorDrawBuffer state.
217 * In the later case, we update the FB's ColorDrawBuffer state only.
219 * Furthermore, upon a MakeCurrent() or BindFramebuffer() call, if the
220 * new FB is a window system FB, we need to re-update the FB's
221 * ColorDrawBuffer state to match the context. This is handled in
222 * _mesa_update_framebuffer().
224 * See the GL_EXT_framebuffer_object spec for more info.
227 _mesa_DrawBuffer(GLenum buffer
)
230 GET_CURRENT_CONTEXT(ctx
);
231 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex... */
233 if (MESA_VERBOSE
& VERBOSE_API
) {
234 _mesa_debug(ctx
, "glDrawBuffer %s\n", _mesa_lookup_enum_by_nr(buffer
));
237 if (buffer
== GL_NONE
) {
241 const GLbitfield supportedMask
242 = supported_buffer_bitmask(ctx
, ctx
->DrawBuffer
);
243 destMask
= draw_buffer_enum_to_bitmask(buffer
);
244 if (destMask
== BAD_MASK
) {
245 /* totally bogus buffer */
246 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawBuffer(buffer=0x%x)", buffer
);
249 destMask
&= supportedMask
;
250 if (destMask
== 0x0) {
251 /* none of the named color buffers exist! */
252 _mesa_error(ctx
, GL_INVALID_OPERATION
,
253 "glDrawBuffer(buffer=0x%x)", buffer
);
258 /* if we get here, there's no error so set new state */
259 _mesa_drawbuffers(ctx
, 1, &buffer
, &destMask
);
262 * Call device driver function.
264 if (ctx
->Driver
.DrawBuffers
)
265 ctx
->Driver
.DrawBuffers(ctx
, 1, &buffer
);
266 else if (ctx
->Driver
.DrawBuffer
)
267 ctx
->Driver
.DrawBuffer(ctx
, buffer
);
272 * Called by glDrawBuffersARB; specifies the destination color renderbuffers
273 * for N fragment program color outputs.
274 * \sa _mesa_DrawBuffer
275 * \param n number of outputs
276 * \param buffers array [n] of renderbuffer names. Unlike glDrawBuffer, the
277 * names cannot specify more than one buffer. For example,
278 * GL_FRONT_AND_BACK is illegal.
281 _mesa_DrawBuffersARB(GLsizei n
, const GLenum
*buffers
)
284 GLbitfield usedBufferMask
, supportedMask
;
285 GLbitfield destMask
[MAX_DRAW_BUFFERS
];
286 GET_CURRENT_CONTEXT(ctx
);
287 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
289 /* Turns out n==0 is a valid input that should not produce an error.
290 * The remaining code below correctly handles the n==0 case.
292 if (n
< 0 || n
> (GLsizei
) ctx
->Const
.MaxDrawBuffers
) {
293 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawBuffersARB(n)");
297 supportedMask
= supported_buffer_bitmask(ctx
, ctx
->DrawBuffer
);
298 usedBufferMask
= 0x0;
300 /* complicated error checking... */
301 for (output
= 0; output
< n
; output
++) {
302 if (buffers
[output
] == GL_NONE
) {
303 destMask
[output
] = 0x0;
306 destMask
[output
] = draw_buffer_enum_to_bitmask(buffers
[output
]);
307 if (destMask
[output
] == BAD_MASK
308 || _mesa_bitcount(destMask
[output
]) > 1) {
309 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawBuffersARB(buffer)");
312 destMask
[output
] &= supportedMask
;
313 if (destMask
[output
] == 0) {
314 _mesa_error(ctx
, GL_INVALID_OPERATION
,
315 "glDrawBuffersARB(unsupported buffer)");
318 if (destMask
[output
] & usedBufferMask
) {
319 /* can't specify a dest buffer more than once! */
320 _mesa_error(ctx
, GL_INVALID_OPERATION
,
321 "glDrawBuffersARB(duplicated buffer)");
326 usedBufferMask
|= destMask
[output
];
330 /* OK, if we get here, there were no errors so set the new state */
331 _mesa_drawbuffers(ctx
, n
, buffers
, destMask
);
334 * Call device driver function. Note that n can be equal to 0,
335 * in which case we don't want to reference buffers[0], which
338 if (ctx
->Driver
.DrawBuffers
)
339 ctx
->Driver
.DrawBuffers(ctx
, n
, buffers
);
340 else if (ctx
->Driver
.DrawBuffer
)
341 ctx
->Driver
.DrawBuffer(ctx
, n
> 0 ? buffers
[0] : GL_NONE
);
346 * Helper function to set the GL_DRAW_BUFFER state in the context and
347 * current FBO. Called via glDrawBuffer(), glDrawBuffersARB()
349 * All error checking will have been done prior to calling this function
350 * so nothing should go wrong at this point.
352 * \param ctx current context
353 * \param n number of color outputs to set
354 * \param buffers array[n] of colorbuffer names, like GL_LEFT.
355 * \param destMask array[n] of BUFFER_BIT_* bitmasks which correspond to the
356 * colorbuffer names. (i.e. GL_FRONT_AND_BACK =>
357 * BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT).
360 _mesa_drawbuffers(GLcontext
*ctx
, GLuint n
, const GLenum
*buffers
,
361 const GLbitfield
*destMask
)
363 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
364 GLbitfield mask
[MAX_DRAW_BUFFERS
];
365 GLboolean newState
= GL_FALSE
;
368 /* compute destMask values now */
369 const GLbitfield supportedMask
= supported_buffer_bitmask(ctx
, fb
);
371 for (output
= 0; output
< n
; output
++) {
372 mask
[output
] = draw_buffer_enum_to_bitmask(buffers
[output
]);
373 ASSERT(mask
[output
] != BAD_MASK
);
374 mask
[output
] &= supportedMask
;
380 * If n==1, destMask[0] may have up to four bits set.
381 * Otherwise, destMask[x] can only have one bit set.
384 GLuint count
= 0, destMask0
= destMask
[0];
386 GLint bufIndex
= _mesa_ffs(destMask0
) - 1;
387 if (fb
->_ColorDrawBufferIndexes
[count
] != bufIndex
) {
388 fb
->_ColorDrawBufferIndexes
[count
] = bufIndex
;
392 destMask0
&= ~(1 << bufIndex
);
394 fb
->ColorDrawBuffer
[0] = buffers
[0];
395 if (fb
->_NumColorDrawBuffers
!= count
) {
396 fb
->_NumColorDrawBuffers
= count
;
401 GLuint buf
, count
= 0;
402 for (buf
= 0; buf
< n
; buf
++ ) {
404 GLint bufIndex
= _mesa_ffs(destMask
[buf
]) - 1;
405 /* only one bit should be set in the destMask[buf] field */
406 ASSERT(_mesa_bitcount(destMask
[buf
]) == 1);
407 if (fb
->_ColorDrawBufferIndexes
[buf
] != bufIndex
) {
408 fb
->_ColorDrawBufferIndexes
[buf
] = bufIndex
;
411 fb
->ColorDrawBuffer
[buf
] = buffers
[buf
];
415 if (fb
->_ColorDrawBufferIndexes
[buf
] != -1) {
416 fb
->_ColorDrawBufferIndexes
[buf
] = -1;
421 /* set remaining outputs to -1 (GL_NONE) */
422 while (buf
< ctx
->Const
.MaxDrawBuffers
) {
423 if (fb
->_ColorDrawBufferIndexes
[buf
] != -1) {
424 fb
->_ColorDrawBufferIndexes
[buf
] = -1;
427 fb
->ColorDrawBuffer
[buf
] = GL_NONE
;
430 fb
->_NumColorDrawBuffers
= count
;
434 /* also set context drawbuffer state */
436 for (buf
= 0; buf
< ctx
->Const
.MaxDrawBuffers
; buf
++) {
437 if (ctx
->Color
.DrawBuffer
[buf
] != fb
->ColorDrawBuffer
[buf
]) {
438 ctx
->Color
.DrawBuffer
[buf
] = fb
->ColorDrawBuffer
[buf
];
445 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
450 * Like \sa _mesa_drawbuffers(), this is a helper function for setting
451 * GL_READ_BUFFER state in the context and current FBO.
452 * \param ctx the rendering context
453 * \param buffer GL_FRONT, GL_BACK, GL_COLOR_ATTACHMENT0, etc.
454 * \param bufferIndex the numerical index corresponding to 'buffer'
457 _mesa_readbuffer(GLcontext
*ctx
, GLenum buffer
, GLint bufferIndex
)
459 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
462 /* Only update the per-context READ_BUFFER state if we're bound to
463 * a window-system framebuffer.
465 ctx
->Pixel
.ReadBuffer
= buffer
;
468 fb
->ColorReadBuffer
= buffer
;
469 fb
->_ColorReadBufferIndex
= bufferIndex
;
471 ctx
->NewState
|= _NEW_BUFFERS
;
477 * Called by glReadBuffer to set the source renderbuffer for reading pixels.
478 * \param mode color buffer such as GL_FRONT, GL_BACK, etc.
481 _mesa_ReadBuffer(GLenum buffer
)
483 struct gl_framebuffer
*fb
;
484 GLbitfield supportedMask
;
486 GET_CURRENT_CONTEXT(ctx
);
487 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
489 if (MESA_VERBOSE
& VERBOSE_API
)
490 _mesa_debug(ctx
, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer
));
492 fb
= ctx
->ReadBuffer
;
494 if (MESA_VERBOSE
& VERBOSE_API
)
495 _mesa_debug(ctx
, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer
));
497 if (fb
->Name
> 0 && buffer
== GL_NONE
) {
498 /* This is legal for user-created framebuffer objects */
502 /* general case / window-system framebuffer */
503 srcBuffer
= read_buffer_enum_to_index(buffer
);
504 if (srcBuffer
== -1) {
505 _mesa_error(ctx
, GL_INVALID_ENUM
,
506 "glReadBuffer(buffer=0x%x)", buffer
);
509 supportedMask
= supported_buffer_bitmask(ctx
, fb
);
510 if (((1 << srcBuffer
) & supportedMask
) == 0) {
511 _mesa_error(ctx
, GL_INVALID_OPERATION
,
512 "glReadBuffer(buffer=0x%x)", buffer
);
517 /* OK, all error checking has been completed now */
519 _mesa_readbuffer(ctx
, buffer
, srcBuffer
);
520 ctx
->NewState
|= _NEW_BUFFERS
;
523 * Call device driver function.
525 if (ctx
->Driver
.ReadBuffer
)
526 (*ctx
->Driver
.ReadBuffer
)(ctx
, buffer
);