2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * 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
,
55 const struct gl_framebuffer
*fb
)
57 GLbitfield mask
= 0x0;
59 if (_mesa_is_user_fbo(fb
)) {
60 /* A user-created renderbuffer */
61 mask
= ((1 << ctx
->Const
.MaxColorAttachments
) - 1) << BUFFER_COLOR0
;
64 /* A window system framebuffer */
66 mask
= BUFFER_BIT_FRONT_LEFT
; /* always have this */
67 if (fb
->Visual
.stereoMode
) {
68 mask
|= BUFFER_BIT_FRONT_RIGHT
;
69 if (fb
->Visual
.doubleBufferMode
) {
70 mask
|= BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_BACK_RIGHT
;
73 else if (fb
->Visual
.doubleBufferMode
) {
74 mask
|= BUFFER_BIT_BACK_LEFT
;
77 for (i
= 0; i
< fb
->Visual
.numAuxBuffers
; i
++) {
78 mask
|= (BUFFER_BIT_AUX0
<< i
);
87 * Helper routine used by glDrawBuffer and glDrawBuffersARB.
88 * Given a GLenum naming one or more color buffers (such as
89 * GL_FRONT_AND_BACK), return the corresponding bitmask of BUFFER_BIT_* flags.
92 draw_buffer_enum_to_bitmask(const struct gl_context
*ctx
, GLenum buffer
)
98 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_FRONT_RIGHT
;
100 if (_mesa_is_gles(ctx
)) {
101 /* Page 181 (page 192 of the PDF) in section 4.2.1 of the OpenGL
102 * ES 3.0.1 specification says:
104 * "When draw buffer zero is BACK, color values are written
105 * into the sole buffer for single-buffered contexts, or into
106 * the back buffer for double-buffered contexts."
108 * Since there is no stereo rendering in ES 3.0, only return the
109 * LEFT bits. This also satisfies the "n must be 1" requirement.
111 * We also do this for GLES 1 and 2 because those APIs have no
112 * concept of selecting the front and back buffer anyway and it's
113 * convenient to be able to maintain the magic behaviour of
114 * GL_BACK in that case.
116 if (ctx
->DrawBuffer
->Visual
.doubleBufferMode
)
117 return BUFFER_BIT_BACK_LEFT
;
118 return BUFFER_BIT_FRONT_LEFT
;
120 return BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_BACK_RIGHT
;
122 return BUFFER_BIT_FRONT_RIGHT
| BUFFER_BIT_BACK_RIGHT
;
124 return BUFFER_BIT_FRONT_RIGHT
;
126 return BUFFER_BIT_BACK_RIGHT
;
128 return BUFFER_BIT_BACK_LEFT
;
129 case GL_FRONT_AND_BACK
:
130 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_BACK_LEFT
131 | BUFFER_BIT_FRONT_RIGHT
| BUFFER_BIT_BACK_RIGHT
;
133 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_BACK_LEFT
;
135 return BUFFER_BIT_FRONT_LEFT
;
137 return BUFFER_BIT_AUX0
;
141 return 1 << BUFFER_COUNT
; /* invalid, but not BAD_MASK */
142 case GL_COLOR_ATTACHMENT0_EXT
:
143 return BUFFER_BIT_COLOR0
;
144 case GL_COLOR_ATTACHMENT1_EXT
:
145 return BUFFER_BIT_COLOR1
;
146 case GL_COLOR_ATTACHMENT2_EXT
:
147 return BUFFER_BIT_COLOR2
;
148 case GL_COLOR_ATTACHMENT3_EXT
:
149 return BUFFER_BIT_COLOR3
;
150 case GL_COLOR_ATTACHMENT4_EXT
:
151 return BUFFER_BIT_COLOR4
;
152 case GL_COLOR_ATTACHMENT5_EXT
:
153 return BUFFER_BIT_COLOR5
;
154 case GL_COLOR_ATTACHMENT6_EXT
:
155 return BUFFER_BIT_COLOR6
;
156 case GL_COLOR_ATTACHMENT7_EXT
:
157 return BUFFER_BIT_COLOR7
;
159 /* not an error, but also not supported */
160 if (buffer
>= GL_COLOR_ATTACHMENT8
&& buffer
<= GL_COLOR_ATTACHMENT31
)
161 return 1 << BUFFER_COUNT
;
169 * Helper routine used by glReadBuffer.
170 * Given a GLenum naming a color buffer, return the index of the corresponding
171 * renderbuffer (a BUFFER_* value).
172 * return -1 for an invalid buffer.
174 static gl_buffer_index
175 read_buffer_enum_to_index(GLenum buffer
)
179 return BUFFER_FRONT_LEFT
;
181 return BUFFER_BACK_LEFT
;
183 return BUFFER_FRONT_RIGHT
;
185 return BUFFER_FRONT_RIGHT
;
187 return BUFFER_BACK_RIGHT
;
189 return BUFFER_BACK_LEFT
;
191 return BUFFER_FRONT_LEFT
;
193 return BUFFER_FRONT_LEFT
;
199 return BUFFER_COUNT
; /* invalid, but not -1 */
200 case GL_COLOR_ATTACHMENT0_EXT
:
201 return BUFFER_COLOR0
;
202 case GL_COLOR_ATTACHMENT1_EXT
:
203 return BUFFER_COLOR1
;
204 case GL_COLOR_ATTACHMENT2_EXT
:
205 return BUFFER_COLOR2
;
206 case GL_COLOR_ATTACHMENT3_EXT
:
207 return BUFFER_COLOR3
;
208 case GL_COLOR_ATTACHMENT4_EXT
:
209 return BUFFER_COLOR4
;
210 case GL_COLOR_ATTACHMENT5_EXT
:
211 return BUFFER_COLOR5
;
212 case GL_COLOR_ATTACHMENT6_EXT
:
213 return BUFFER_COLOR6
;
214 case GL_COLOR_ATTACHMENT7_EXT
:
215 return BUFFER_COLOR7
;
217 /* not an error, but also not supported */
218 if (buffer
>= GL_COLOR_ATTACHMENT8
&& buffer
<= GL_COLOR_ATTACHMENT31
)
227 * Called by glDrawBuffer() and glNamedFramebufferDrawBuffer().
228 * Specify which renderbuffer(s) to draw into for the first color output.
229 * <buffer> can name zero, one, two or four renderbuffers!
230 * \sa _mesa_DrawBuffers
232 * \param buffer buffer token such as GL_LEFT or GL_FRONT_AND_BACK, etc.
234 * Note that the behaviour of this function depends on whether the
235 * current ctx->DrawBuffer is a window-system framebuffer or a user-created
236 * framebuffer object.
237 * In the former case, we update the per-context ctx->Color.DrawBuffer
238 * state var _and_ the FB's ColorDrawBuffer state.
239 * In the later case, we update the FB's ColorDrawBuffer state only.
241 * Furthermore, upon a MakeCurrent() or BindFramebuffer() call, if the
242 * new FB is a window system FB, we need to re-update the FB's
243 * ColorDrawBuffer state to match the context. This is handled in
244 * _mesa_update_framebuffer().
246 * See the GL_EXT_framebuffer_object spec for more info.
249 draw_buffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
250 GLenum buffer
, const char *caller
)
254 FLUSH_VERTICES(ctx
, 0);
256 if (MESA_VERBOSE
& VERBOSE_API
) {
257 _mesa_debug(ctx
, "%s %s\n", caller
, _mesa_enum_to_string(buffer
));
260 if (buffer
== GL_NONE
) {
264 const GLbitfield supportedMask
265 = supported_buffer_bitmask(ctx
, fb
);
266 destMask
= draw_buffer_enum_to_bitmask(ctx
, buffer
);
267 if (destMask
== BAD_MASK
) {
268 /* totally bogus buffer */
269 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid buffer %s)", caller
,
270 _mesa_enum_to_string(buffer
));
273 destMask
&= supportedMask
;
274 if (destMask
== 0x0) {
275 /* none of the named color buffers exist! */
276 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid buffer %s)",
277 caller
, _mesa_enum_to_string(buffer
));
282 /* if we get here, there's no error so set new state */
283 _mesa_drawbuffers(ctx
, fb
, 1, &buffer
, &destMask
);
285 /* Call device driver function only if fb is the bound draw buffer */
286 if (fb
== ctx
->DrawBuffer
) {
287 if (ctx
->Driver
.DrawBuffers
)
288 ctx
->Driver
.DrawBuffers(ctx
, 1, &buffer
);
289 else if (ctx
->Driver
.DrawBuffer
)
290 ctx
->Driver
.DrawBuffer(ctx
, buffer
);
296 _mesa_DrawBuffer(GLenum buffer
)
298 GET_CURRENT_CONTEXT(ctx
);
299 draw_buffer(ctx
, ctx
->DrawBuffer
, buffer
, "glDrawBuffer");
304 _mesa_NamedFramebufferDrawBuffer(GLuint framebuffer
, GLenum buf
)
306 GET_CURRENT_CONTEXT(ctx
);
307 struct gl_framebuffer
*fb
;
310 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
311 "glNamedFramebufferDrawBuffer");
316 fb
= ctx
->WinSysDrawBuffer
;
318 draw_buffer(ctx
, fb
, buf
, "glNamedFramebufferDrawBuffer");
323 * Called by glDrawBuffersARB() and glNamedFramebufferDrawBuffers() to specify
324 * the destination color renderbuffers for N fragment program color outputs.
325 * \sa _mesa_DrawBuffer
326 * \param n number of outputs
327 * \param buffers array [n] of renderbuffer names. Unlike glDrawBuffer, the
328 * names cannot specify more than one buffer. For example,
329 * GL_FRONT_AND_BACK is illegal.
332 draw_buffers(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
333 GLsizei n
, const GLenum
*buffers
, const char *caller
)
336 GLbitfield usedBufferMask
, supportedMask
;
337 GLbitfield destMask
[MAX_DRAW_BUFFERS
];
339 FLUSH_VERTICES(ctx
, 0);
341 /* Turns out n==0 is a valid input that should not produce an error.
342 * The remaining code below correctly handles the n==0 case.
344 * From the OpenGL 3.0 specification, page 258:
345 * "An INVALID_VALUE error is generated if n is greater than
349 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n < 0)", caller
);
353 if (n
> (GLsizei
) ctx
->Const
.MaxDrawBuffers
) {
354 _mesa_error(ctx
, GL_INVALID_VALUE
,
355 "%s(n > maximum number of draw buffers)", caller
);
359 supportedMask
= supported_buffer_bitmask(ctx
, fb
);
360 usedBufferMask
= 0x0;
362 /* From the ES 3.0 specification, page 180:
363 * "If the GL is bound to the default framebuffer, then n must be 1
364 * and the constant must be BACK or NONE."
365 * (same restriction applies with GL_EXT_draw_buffers specification)
367 if (ctx
->API
== API_OPENGLES2
&& _mesa_is_winsys_fbo(fb
) &&
368 (n
!= 1 || (buffers
[0] != GL_NONE
&& buffers
[0] != GL_BACK
))) {
369 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid buffers)", caller
);
373 /* complicated error checking... */
374 for (output
= 0; output
< n
; output
++) {
375 /* Section 4.2 (Whole Framebuffer Operations) of the OpenGL 3.0
376 * specification says:
378 * "Each buffer listed in bufs must be BACK, NONE, or one of the values
379 * from table 4.3 (NONE, COLOR_ATTACHMENTi)"
381 if (_mesa_is_gles3(ctx
) && buffers
[output
] != GL_NONE
&&
382 buffers
[output
] != GL_BACK
&&
383 (buffers
[output
] < GL_COLOR_ATTACHMENT0
||
384 buffers
[output
] >= GL_COLOR_ATTACHMENT0
+ ctx
->Const
.MaxColorAttachments
)) {
385 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawBuffers(buffer)");
389 if (buffers
[output
] == GL_NONE
) {
390 destMask
[output
] = 0x0;
393 /* Page 259 (page 275 of the PDF) in section 4.2.1 of the OpenGL 3.0
394 * spec (20080923) says:
396 * "If the GL is bound to a framebuffer object and DrawBuffers is
397 * supplied with [...] COLOR_ATTACHMENTm where m is greater than
398 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
399 * INVALID_OPERATION results."
401 if (_mesa_is_user_fbo(fb
) && buffers
[output
] >=
402 GL_COLOR_ATTACHMENT0
+ ctx
->Const
.MaxDrawBuffers
) {
403 _mesa_error(ctx
, GL_INVALID_OPERATION
,
404 "%s(buffers[%d] >= maximum number of draw buffers)",
409 destMask
[output
] = draw_buffer_enum_to_bitmask(ctx
, buffers
[output
]);
411 /* From the OpenGL 3.0 specification, page 258:
412 * "Each buffer listed in bufs must be one of the values from tables
413 * 4.5 or 4.6. Otherwise, an INVALID_ENUM error is generated.
415 if (destMask
[output
] == BAD_MASK
) {
416 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid buffer %s)",
417 caller
, _mesa_enum_to_string(buffers
[output
]));
421 /* From the OpenGL 4.0 specification, page 256:
422 * "For both the default framebuffer and framebuffer objects, the
423 * constants FRONT, BACK, LEFT, RIGHT, and FRONT_AND_BACK are not
424 * valid in the bufs array passed to DrawBuffers, and will result in
425 * the error INVALID_ENUM. This restriction is because these
426 * constants may themselves refer to multiple buffers, as shown in
428 * Previous versions of the OpenGL specification say INVALID_OPERATION,
429 * but the Khronos conformance tests expect INVALID_ENUM.
431 if (_mesa_bitcount(destMask
[output
]) > 1) {
432 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid buffer %s)",
433 caller
, _mesa_enum_to_string(buffers
[output
]));
437 /* From the OpenGL 3.0 specification, page 259:
438 * "If the GL is bound to the default framebuffer and DrawBuffers is
439 * supplied with a constant (other than NONE) that does not indicate
440 * any of the color buffers allocated to the GL context by the window
441 * system, the error INVALID_OPERATION will be generated.
443 * If the GL is bound to a framebuffer object and DrawBuffers is
444 * supplied with a constant from table 4.6 [...] then the error
445 * INVALID_OPERATION results."
447 destMask
[output
] &= supportedMask
;
448 if (destMask
[output
] == 0) {
449 _mesa_error(ctx
, GL_INVALID_OPERATION
,
450 "%s(unsupported buffer %s)",
451 caller
, _mesa_enum_to_string(buffers
[output
]));
455 /* ES 3.0 is even more restrictive. From the ES 3.0 spec, page 180:
456 * "If the GL is bound to a framebuffer object, the ith buffer listed
457 * in bufs must be COLOR_ATTACHMENTi or NONE. [...] INVALID_OPERATION."
458 * (same restriction applies with GL_EXT_draw_buffers specification)
460 if (ctx
->API
== API_OPENGLES2
&& _mesa_is_user_fbo(fb
) &&
461 buffers
[output
] != GL_NONE
&&
462 buffers
[output
] != GL_COLOR_ATTACHMENT0
+ output
) {
463 _mesa_error(ctx
, GL_INVALID_OPERATION
,
464 "%s(unsupported buffer %s)",
465 caller
, _mesa_enum_to_string(buffers
[output
]));
469 /* From the OpenGL 3.0 specification, page 258:
470 * "Except for NONE, a buffer may not appear more than once in the
471 * array pointed to by bufs. Specifying a buffer more then once will
472 * result in the error INVALID_OPERATION."
474 if (destMask
[output
] & usedBufferMask
) {
475 _mesa_error(ctx
, GL_INVALID_OPERATION
,
476 "%s(duplicated buffer %s)",
477 caller
, _mesa_enum_to_string(buffers
[output
]));
482 usedBufferMask
|= destMask
[output
];
486 /* OK, if we get here, there were no errors so set the new state */
487 _mesa_drawbuffers(ctx
, fb
, n
, buffers
, destMask
);
490 * Call device driver function if fb is the bound draw buffer.
491 * Note that n can be equal to 0,
492 * in which case we don't want to reference buffers[0], which
495 if (fb
== ctx
->DrawBuffer
) {
496 if (ctx
->Driver
.DrawBuffers
)
497 ctx
->Driver
.DrawBuffers(ctx
, n
, buffers
);
498 else if (ctx
->Driver
.DrawBuffer
)
499 ctx
->Driver
.DrawBuffer(ctx
, n
> 0 ? buffers
[0] : GL_NONE
);
505 _mesa_DrawBuffers(GLsizei n
, const GLenum
*buffers
)
507 GET_CURRENT_CONTEXT(ctx
);
508 draw_buffers(ctx
, ctx
->DrawBuffer
, n
, buffers
, "glDrawBuffers");
513 _mesa_NamedFramebufferDrawBuffers(GLuint framebuffer
, GLsizei n
,
516 GET_CURRENT_CONTEXT(ctx
);
517 struct gl_framebuffer
*fb
;
520 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
521 "glNamedFramebufferDrawBuffers");
526 fb
= ctx
->WinSysDrawBuffer
;
528 draw_buffers(ctx
, fb
, n
, bufs
, "glNamedFramebufferDrawBuffers");
533 * Performs necessary state updates when _mesa_drawbuffers makes an
537 updated_drawbuffers(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
539 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
541 if (ctx
->API
== API_OPENGL_COMPAT
&& !ctx
->Extensions
.ARB_ES2_compatibility
) {
542 /* Flag the FBO as requiring validation. */
543 if (_mesa_is_user_fbo(fb
)) {
551 * Helper function to set the GL_DRAW_BUFFER state for the given context and
552 * FBO. Called via glDrawBuffer(), glDrawBuffersARB()
554 * All error checking will have been done prior to calling this function
555 * so nothing should go wrong at this point.
557 * \param ctx current context
558 * \param fb the desired draw buffer
559 * \param n number of color outputs to set
560 * \param buffers array[n] of colorbuffer names, like GL_LEFT.
561 * \param destMask array[n] of BUFFER_BIT_* bitmasks which correspond to the
562 * colorbuffer names. (i.e. GL_FRONT_AND_BACK =>
563 * BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT).
566 _mesa_drawbuffers(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
567 GLuint n
, const GLenum
*buffers
, const GLbitfield
*destMask
)
569 GLbitfield mask
[MAX_DRAW_BUFFERS
];
573 /* compute destMask values now */
574 const GLbitfield supportedMask
= supported_buffer_bitmask(ctx
, fb
);
576 for (output
= 0; output
< n
; output
++) {
577 mask
[output
] = draw_buffer_enum_to_bitmask(ctx
, buffers
[output
]);
578 assert(mask
[output
] != BAD_MASK
);
579 mask
[output
] &= supportedMask
;
585 * destMask[0] may have up to four bits set
586 * (ex: glDrawBuffer(GL_FRONT_AND_BACK)).
587 * Otherwise, destMask[x] can only have one bit set.
589 if (n
> 0 && _mesa_bitcount(destMask
[0]) > 1) {
590 GLuint count
= 0, destMask0
= destMask
[0];
592 GLint bufIndex
= ffs(destMask0
) - 1;
593 if (fb
->_ColorDrawBufferIndexes
[count
] != bufIndex
) {
594 updated_drawbuffers(ctx
, fb
);
595 fb
->_ColorDrawBufferIndexes
[count
] = bufIndex
;
598 destMask0
&= ~(1 << bufIndex
);
600 fb
->ColorDrawBuffer
[0] = buffers
[0];
601 fb
->_NumColorDrawBuffers
= count
;
605 for (buf
= 0; buf
< n
; buf
++ ) {
607 GLint bufIndex
= ffs(destMask
[buf
]) - 1;
608 /* only one bit should be set in the destMask[buf] field */
609 assert(_mesa_bitcount(destMask
[buf
]) == 1);
610 if (fb
->_ColorDrawBufferIndexes
[buf
] != bufIndex
) {
611 updated_drawbuffers(ctx
, fb
);
612 fb
->_ColorDrawBufferIndexes
[buf
] = bufIndex
;
617 if (fb
->_ColorDrawBufferIndexes
[buf
] != -1) {
618 updated_drawbuffers(ctx
, fb
);
619 fb
->_ColorDrawBufferIndexes
[buf
] = -1;
622 fb
->ColorDrawBuffer
[buf
] = buffers
[buf
];
624 fb
->_NumColorDrawBuffers
= count
;
627 /* set remaining outputs to -1 (GL_NONE) */
628 for (buf
= fb
->_NumColorDrawBuffers
; buf
< ctx
->Const
.MaxDrawBuffers
; buf
++) {
629 if (fb
->_ColorDrawBufferIndexes
[buf
] != -1) {
630 updated_drawbuffers(ctx
, fb
);
631 fb
->_ColorDrawBufferIndexes
[buf
] = -1;
634 for (buf
= n
; buf
< ctx
->Const
.MaxDrawBuffers
; buf
++) {
635 fb
->ColorDrawBuffer
[buf
] = GL_NONE
;
638 if (_mesa_is_winsys_fbo(fb
)) {
639 /* also set context drawbuffer state */
640 for (buf
= 0; buf
< ctx
->Const
.MaxDrawBuffers
; buf
++) {
641 if (ctx
->Color
.DrawBuffer
[buf
] != fb
->ColorDrawBuffer
[buf
]) {
642 updated_drawbuffers(ctx
, fb
);
643 ctx
->Color
.DrawBuffer
[buf
] = fb
->ColorDrawBuffer
[buf
];
651 * Update the current drawbuffer's _ColorDrawBufferIndex[] list, etc.
652 * from the context's Color.DrawBuffer[] state.
653 * Use when changing contexts.
656 _mesa_update_draw_buffers(struct gl_context
*ctx
)
658 /* should be a window system FBO */
659 assert(_mesa_is_winsys_fbo(ctx
->DrawBuffer
));
661 _mesa_drawbuffers(ctx
, ctx
->DrawBuffer
, ctx
->Const
.MaxDrawBuffers
,
662 ctx
->Color
.DrawBuffer
, NULL
);
667 * Like \sa _mesa_drawbuffers(), this is a helper function for setting
668 * GL_READ_BUFFER state for the given context and FBO.
669 * Note that all error checking should have been done before calling
671 * \param ctx the rendering context
672 * \param fb the framebuffer object to update
673 * \param buffer GL_FRONT, GL_BACK, GL_COLOR_ATTACHMENT0, etc.
674 * \param bufferIndex the numerical index corresponding to 'buffer'
677 _mesa_readbuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
678 GLenum buffer
, gl_buffer_index bufferIndex
)
680 if ((fb
== ctx
->ReadBuffer
) && _mesa_is_winsys_fbo(fb
)) {
681 /* Only update the per-context READ_BUFFER state if we're bound to
682 * a window-system framebuffer.
684 ctx
->Pixel
.ReadBuffer
= buffer
;
687 fb
->ColorReadBuffer
= buffer
;
688 fb
->_ColorReadBufferIndex
= bufferIndex
;
690 ctx
->NewState
|= _NEW_BUFFERS
;
696 * Called by glReadBuffer and glNamedFramebufferReadBuffer to set the source
697 * renderbuffer for reading pixels.
698 * \param mode color buffer such as GL_FRONT, GL_BACK, etc.
701 read_buffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
702 GLenum buffer
, const char *caller
)
704 GLbitfield supportedMask
;
705 gl_buffer_index srcBuffer
;
707 FLUSH_VERTICES(ctx
, 0);
709 if (MESA_VERBOSE
& VERBOSE_API
)
710 _mesa_debug(ctx
, "%s %s\n", caller
, _mesa_enum_to_string(buffer
));
712 if (buffer
== GL_NONE
) {
713 /* This is legal--it means that no buffer should be bound for reading. */
717 /* general case / window-system framebuffer */
718 srcBuffer
= read_buffer_enum_to_index(buffer
);
719 if (srcBuffer
== -1) {
720 _mesa_error(ctx
, GL_INVALID_ENUM
,
721 "%s(invalid buffer %s)", caller
,
722 _mesa_enum_to_string(buffer
));
725 supportedMask
= supported_buffer_bitmask(ctx
, fb
);
726 if (((1 << srcBuffer
) & supportedMask
) == 0) {
727 _mesa_error(ctx
, GL_INVALID_OPERATION
,
728 "%s(invalid buffer %s)", caller
,
729 _mesa_enum_to_string(buffer
));
734 /* OK, all error checking has been completed now */
736 _mesa_readbuffer(ctx
, fb
, buffer
, srcBuffer
);
738 /* Call the device driver function only if fb is the bound read buffer */
739 if (fb
== ctx
->ReadBuffer
) {
740 if (ctx
->Driver
.ReadBuffer
)
741 ctx
->Driver
.ReadBuffer(ctx
, buffer
);
747 _mesa_ReadBuffer(GLenum buffer
)
749 GET_CURRENT_CONTEXT(ctx
);
750 read_buffer(ctx
, ctx
->ReadBuffer
, buffer
, "glReadBuffer");
755 _mesa_NamedFramebufferReadBuffer(GLuint framebuffer
, GLenum src
)
757 GET_CURRENT_CONTEXT(ctx
);
758 struct gl_framebuffer
*fb
;
761 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
762 "glNamedFramebufferReadBuffer");
767 fb
= ctx
->WinSysReadBuffer
;
769 read_buffer(ctx
, fb
, src
, "glNamedFramebufferReadBuffer");