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
;
123 return BUFFER_BIT_AUX1
;
125 return BUFFER_BIT_AUX2
;
127 return BUFFER_BIT_AUX3
;
128 case GL_COLOR_ATTACHMENT0_EXT
:
129 return BUFFER_BIT_COLOR0
;
130 case GL_COLOR_ATTACHMENT1_EXT
:
131 return BUFFER_BIT_COLOR1
;
132 case GL_COLOR_ATTACHMENT2_EXT
:
133 return BUFFER_BIT_COLOR2
;
134 case GL_COLOR_ATTACHMENT3_EXT
:
135 return BUFFER_BIT_COLOR3
;
136 case GL_COLOR_ATTACHMENT4_EXT
:
137 return BUFFER_BIT_COLOR4
;
138 case GL_COLOR_ATTACHMENT5_EXT
:
139 return BUFFER_BIT_COLOR5
;
140 case GL_COLOR_ATTACHMENT6_EXT
:
141 return BUFFER_BIT_COLOR6
;
142 case GL_COLOR_ATTACHMENT7_EXT
:
143 return BUFFER_BIT_COLOR7
;
152 * Helper routine used by glReadBuffer.
153 * Given a GLenum naming a color buffer, return the index of the corresponding
154 * renderbuffer (a BUFFER_* value).
155 * return -1 for an invalid buffer.
158 read_buffer_enum_to_index(GLenum buffer
)
162 return BUFFER_FRONT_LEFT
;
164 return BUFFER_BACK_LEFT
;
166 return BUFFER_FRONT_RIGHT
;
168 return BUFFER_FRONT_RIGHT
;
170 return BUFFER_BACK_RIGHT
;
172 return BUFFER_BACK_LEFT
;
174 return BUFFER_FRONT_LEFT
;
176 return BUFFER_FRONT_LEFT
;
185 case GL_COLOR_ATTACHMENT0_EXT
:
186 return BUFFER_COLOR0
;
187 case GL_COLOR_ATTACHMENT1_EXT
:
188 return BUFFER_COLOR1
;
189 case GL_COLOR_ATTACHMENT2_EXT
:
190 return BUFFER_COLOR2
;
191 case GL_COLOR_ATTACHMENT3_EXT
:
192 return BUFFER_COLOR3
;
193 case GL_COLOR_ATTACHMENT4_EXT
:
194 return BUFFER_COLOR4
;
195 case GL_COLOR_ATTACHMENT5_EXT
:
196 return BUFFER_COLOR5
;
197 case GL_COLOR_ATTACHMENT6_EXT
:
198 return BUFFER_COLOR6
;
199 case GL_COLOR_ATTACHMENT7_EXT
:
200 return BUFFER_COLOR7
;
209 * Called by glDrawBuffer().
210 * Specify which renderbuffer(s) to draw into for the first color output.
211 * <buffer> can name zero, one, two or four renderbuffers!
212 * \sa _mesa_DrawBuffersARB
214 * \param buffer buffer token such as GL_LEFT or GL_FRONT_AND_BACK, etc.
216 * Note that the behaviour of this function depends on whether the
217 * current ctx->DrawBuffer is a window-system framebuffer (Name=0) or
218 * a user-created framebuffer object (Name!=0).
219 * In the former case, we update the per-context ctx->Color.DrawBuffer
220 * state var _and_ the FB's ColorDrawBuffer state.
221 * In the later case, we update the FB's ColorDrawBuffer state only.
223 * Furthermore, upon a MakeCurrent() or BindFramebuffer() call, if the
224 * new FB is a window system FB, we need to re-update the FB's
225 * ColorDrawBuffer state to match the context. This is handled in
226 * _mesa_update_framebuffer().
228 * See the GL_EXT_framebuffer_object spec for more info.
231 _mesa_DrawBuffer(GLenum buffer
)
234 GET_CURRENT_CONTEXT(ctx
);
235 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex... */
237 if (MESA_VERBOSE
& VERBOSE_API
) {
238 _mesa_debug(ctx
, "glDrawBuffer %s\n", _mesa_lookup_enum_by_nr(buffer
));
241 if (buffer
== GL_NONE
) {
245 const GLbitfield supportedMask
246 = supported_buffer_bitmask(ctx
, ctx
->DrawBuffer
);
247 destMask
= draw_buffer_enum_to_bitmask(buffer
);
248 if (destMask
== BAD_MASK
) {
249 /* totally bogus buffer */
250 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawBuffer(buffer)");
253 destMask
&= supportedMask
;
254 if (destMask
== 0x0) {
255 /* none of the named color buffers exist! */
256 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawBuffer(buffer)");
261 /* if we get here, there's no error so set new state */
262 _mesa_drawbuffers(ctx
, 1, &buffer
, &destMask
);
265 * Call device driver function.
267 if (ctx
->Driver
.DrawBuffers
)
268 ctx
->Driver
.DrawBuffers(ctx
, 1, &buffer
);
269 else if (ctx
->Driver
.DrawBuffer
)
270 ctx
->Driver
.DrawBuffer(ctx
, buffer
);
275 * Called by glDrawBuffersARB; specifies the destination color renderbuffers
276 * for N fragment program color outputs.
277 * \sa _mesa_DrawBuffer
278 * \param n number of outputs
279 * \param buffers array [n] of renderbuffer names. Unlike glDrawBuffer, the
280 * names cannot specify more than one buffer. For example,
281 * GL_FRONT_AND_BACK is illegal.
284 _mesa_DrawBuffersARB(GLsizei n
, const GLenum
*buffers
)
287 GLbitfield usedBufferMask
, supportedMask
;
288 GLbitfield destMask
[MAX_DRAW_BUFFERS
];
289 GET_CURRENT_CONTEXT(ctx
);
290 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
292 if (!ctx
->Extensions
.ARB_draw_buffers
) {
293 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawBuffersARB");
296 if (n
< 1 || n
> (GLsizei
) ctx
->Const
.MaxDrawBuffers
) {
297 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawBuffersARB(n)");
301 supportedMask
= supported_buffer_bitmask(ctx
, ctx
->DrawBuffer
);
302 usedBufferMask
= 0x0;
304 /* complicated error checking... */
305 for (output
= 0; output
< n
; output
++) {
306 if (buffers
[output
] == GL_NONE
) {
307 destMask
[output
] = 0x0;
310 destMask
[output
] = draw_buffer_enum_to_bitmask(buffers
[output
]);
311 if (destMask
[output
] == BAD_MASK
312 || _mesa_bitcount(destMask
[output
]) > 1) {
313 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawBuffersARB(buffer)");
316 destMask
[output
] &= supportedMask
;
317 if (destMask
[output
] == 0) {
318 _mesa_error(ctx
, GL_INVALID_OPERATION
,
319 "glDrawBuffersARB(unsupported buffer)");
322 if (destMask
[output
] & usedBufferMask
) {
323 /* can't specify a dest buffer more than once! */
324 _mesa_error(ctx
, GL_INVALID_OPERATION
,
325 "glDrawBuffersARB(duplicated buffer)");
330 usedBufferMask
|= destMask
[output
];
334 /* OK, if we get here, there were no errors so set the new state */
335 _mesa_drawbuffers(ctx
, n
, buffers
, destMask
);
338 * Call device driver function.
340 if (ctx
->Driver
.DrawBuffers
)
341 ctx
->Driver
.DrawBuffers(ctx
, n
, buffers
);
342 else if (ctx
->Driver
.DrawBuffer
)
343 ctx
->Driver
.DrawBuffer(ctx
, buffers
[0]);
348 * Helper function to set the GL_DRAW_BUFFER state in the context and
351 * All error checking will have been done prior to calling this function
352 * so nothing should go wrong at this point.
354 * \param ctx current context
355 * \param n number of color outputs to set
356 * \param buffers array[n] of colorbuffer names, like GL_LEFT.
357 * \param destMask array[n] of BUFFER_BIT_* bitmasks which correspond to the
358 * colorbuffer names. (i.e. GL_FRONT_AND_BACK =>
359 * BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT).
362 _mesa_drawbuffers(GLcontext
*ctx
, GLuint n
, const GLenum
*buffers
,
363 const GLbitfield
*destMask
)
365 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
366 GLbitfield mask
[MAX_DRAW_BUFFERS
];
369 /* compute destMask values now */
370 const GLbitfield supportedMask
= supported_buffer_bitmask(ctx
, fb
);
372 for (output
= 0; output
< n
; output
++) {
373 mask
[output
] = draw_buffer_enum_to_bitmask(buffers
[output
]);
374 ASSERT(mask
[output
] != BAD_MASK
);
375 mask
[output
] &= supportedMask
;
381 GLuint buf
, count
= 0;
382 /* init to -1 to help catch errors */
383 fb
->_ColorDrawBufferIndexes
[0] = -1;
384 for (buf
= 0; buf
< BUFFER_COUNT
; buf
++) {
385 if (destMask
[0] & (1 << buf
)) {
386 fb
->_ColorDrawBufferIndexes
[count
] = buf
;
390 fb
->ColorDrawBuffer
[0] = buffers
[0];
391 fb
->_NumColorDrawBuffers
= count
;
394 GLuint buf
, count
= 0;
395 for (buf
= 0; buf
< n
; buf
++ ) {
397 fb
->_ColorDrawBufferIndexes
[buf
] = _mesa_ffs(destMask
[buf
]) - 1;
398 fb
->ColorDrawBuffer
[buf
] = buffers
[buf
];
402 fb
->_ColorDrawBufferIndexes
[buf
] = -1;
405 /* set remaining outputs to -1 (GL_NONE) */
406 while (buf
< ctx
->Const
.MaxDrawBuffers
) {
407 fb
->_ColorDrawBufferIndexes
[buf
] = -1;
408 fb
->ColorDrawBuffer
[buf
] = GL_NONE
;
411 fb
->_NumColorDrawBuffers
= count
;
415 /* also set context drawbuffer state */
417 for (buf
= 0; buf
< ctx
->Const
.MaxDrawBuffers
; buf
++) {
418 ctx
->Color
.DrawBuffer
[buf
] = fb
->ColorDrawBuffer
[buf
];
422 ctx
->NewState
|= _NEW_BUFFERS
;
427 * Like \sa _mesa_drawbuffers(), this is a helper function for setting
428 * GL_READ_BUFFER state in the context and current FBO.
429 * \param ctx the rendering context
430 * \param buffer GL_FRONT, GL_BACK, GL_COLOR_ATTACHMENT0, etc.
431 * \param bufferIndex the numerical index corresponding to 'buffer'
434 _mesa_readbuffer(GLcontext
*ctx
, GLenum buffer
, GLint bufferIndex
)
436 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
439 /* Only update the per-context READ_BUFFER state if we're bound to
440 * a window-system framebuffer.
442 ctx
->Pixel
.ReadBuffer
= buffer
;
445 fb
->ColorReadBuffer
= buffer
;
446 fb
->_ColorReadBufferIndex
= bufferIndex
;
448 ctx
->NewState
|= _NEW_PIXEL
;
454 * Called by glReadBuffer to set the source renderbuffer for reading pixels.
455 * \param mode color buffer such as GL_FRONT, GL_BACK, etc.
458 _mesa_ReadBuffer(GLenum buffer
)
460 struct gl_framebuffer
*fb
;
461 GLbitfield supportedMask
;
463 GET_CURRENT_CONTEXT(ctx
);
464 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
466 if (MESA_VERBOSE
& VERBOSE_API
)
467 _mesa_debug(ctx
, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer
));
469 fb
= ctx
->ReadBuffer
;
471 if (MESA_VERBOSE
& VERBOSE_API
)
472 _mesa_debug(ctx
, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer
));
474 if (fb
->Name
> 0 && buffer
== GL_NONE
) {
475 /* This is legal for user-created framebuffer objects */
479 /* general case / window-system framebuffer */
480 srcBuffer
= read_buffer_enum_to_index(buffer
);
481 if (srcBuffer
== -1) {
482 _mesa_error(ctx
, GL_INVALID_ENUM
,
483 "glReadBuffer(buffer=0x%x)", buffer
);
486 supportedMask
= supported_buffer_bitmask(ctx
, fb
);
487 if (((1 << srcBuffer
) & supportedMask
) == 0) {
488 _mesa_error(ctx
, GL_INVALID_OPERATION
,
489 "glReadBuffer(buffer=0x%x)", buffer
);
494 /* OK, all error checking has been completed now */
496 _mesa_readbuffer(ctx
, buffer
, srcBuffer
);
497 ctx
->NewState
|= _NEW_BUFFERS
;
500 * Call device driver function.
502 if (ctx
->Driver
.ReadBuffer
)
503 (*ctx
->Driver
.ReadBuffer
)(ctx
, buffer
);