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.
39 #include "util/bitscan.h"
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 struct gl_context
*ctx
,
56 const struct gl_framebuffer
*fb
)
58 GLbitfield mask
= 0x0;
60 if (_mesa_is_user_fbo(fb
)) {
61 /* A user-created renderbuffer */
62 mask
= ((1 << ctx
->Const
.MaxColorAttachments
) - 1) << BUFFER_COLOR0
;
65 /* A window system framebuffer */
67 mask
= BUFFER_BIT_FRONT_LEFT
; /* always have this */
68 if (fb
->Visual
.stereoMode
) {
69 mask
|= BUFFER_BIT_FRONT_RIGHT
;
70 if (fb
->Visual
.doubleBufferMode
) {
71 mask
|= BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_BACK_RIGHT
;
74 else if (fb
->Visual
.doubleBufferMode
) {
75 mask
|= BUFFER_BIT_BACK_LEFT
;
78 for (i
= 0; i
< fb
->Visual
.numAuxBuffers
; i
++) {
79 mask
|= (BUFFER_BIT_AUX0
<< i
);
88 * Helper routine used by glDrawBuffer and glDrawBuffersARB.
89 * Given a GLenum naming one or more color buffers (such as
90 * GL_FRONT_AND_BACK), return the corresponding bitmask of BUFFER_BIT_* flags.
93 draw_buffer_enum_to_bitmask(const struct gl_context
*ctx
, GLenum buffer
)
99 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_FRONT_RIGHT
;
101 if (_mesa_is_gles(ctx
)) {
102 /* Page 181 (page 192 of the PDF) in section 4.2.1 of the OpenGL
103 * ES 3.0.1 specification says:
105 * "When draw buffer zero is BACK, color values are written
106 * into the sole buffer for single-buffered contexts, or into
107 * the back buffer for double-buffered contexts."
109 * Since there is no stereo rendering in ES 3.0, only return the
110 * LEFT bits. This also satisfies the "n must be 1" requirement.
112 * We also do this for GLES 1 and 2 because those APIs have no
113 * concept of selecting the front and back buffer anyway and it's
114 * convenient to be able to maintain the magic behaviour of
115 * GL_BACK in that case.
117 if (ctx
->DrawBuffer
->Visual
.doubleBufferMode
)
118 return BUFFER_BIT_BACK_LEFT
;
119 return BUFFER_BIT_FRONT_LEFT
;
121 return BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_BACK_RIGHT
;
123 return BUFFER_BIT_FRONT_RIGHT
| BUFFER_BIT_BACK_RIGHT
;
125 return BUFFER_BIT_FRONT_RIGHT
;
127 return BUFFER_BIT_BACK_RIGHT
;
129 return BUFFER_BIT_BACK_LEFT
;
130 case GL_FRONT_AND_BACK
:
131 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_BACK_LEFT
132 | BUFFER_BIT_FRONT_RIGHT
| BUFFER_BIT_BACK_RIGHT
;
134 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_BACK_LEFT
;
136 return BUFFER_BIT_FRONT_LEFT
;
138 return BUFFER_BIT_AUX0
;
142 return 1 << BUFFER_COUNT
; /* invalid, but not BAD_MASK */
143 case GL_COLOR_ATTACHMENT0_EXT
:
144 return BUFFER_BIT_COLOR0
;
145 case GL_COLOR_ATTACHMENT1_EXT
:
146 return BUFFER_BIT_COLOR1
;
147 case GL_COLOR_ATTACHMENT2_EXT
:
148 return BUFFER_BIT_COLOR2
;
149 case GL_COLOR_ATTACHMENT3_EXT
:
150 return BUFFER_BIT_COLOR3
;
151 case GL_COLOR_ATTACHMENT4_EXT
:
152 return BUFFER_BIT_COLOR4
;
153 case GL_COLOR_ATTACHMENT5_EXT
:
154 return BUFFER_BIT_COLOR5
;
155 case GL_COLOR_ATTACHMENT6_EXT
:
156 return BUFFER_BIT_COLOR6
;
157 case GL_COLOR_ATTACHMENT7_EXT
:
158 return BUFFER_BIT_COLOR7
;
160 /* not an error, but also not supported */
161 if (buffer
>= GL_COLOR_ATTACHMENT8
&& buffer
<= GL_COLOR_ATTACHMENT31
)
162 return 1 << BUFFER_COUNT
;
170 * Helper routine used by glReadBuffer.
171 * Given a GLenum naming a color buffer, return the index of the corresponding
172 * renderbuffer (a BUFFER_* value).
173 * return -1 for an invalid buffer.
175 static gl_buffer_index
176 read_buffer_enum_to_index(const struct gl_context
*ctx
, GLenum buffer
)
180 return BUFFER_FRONT_LEFT
;
182 if (_mesa_is_gles(ctx
)) {
183 /* In draw_buffer_enum_to_bitmask, when GLES contexts draw to
184 * GL_BACK with a single-buffered configuration, we actually end
185 * up drawing to the sole front buffer in our internal
186 * representation. For consistency, we must read from that
187 * front left buffer too.
189 if (!ctx
->DrawBuffer
->Visual
.doubleBufferMode
)
190 return BUFFER_FRONT_LEFT
;
192 return BUFFER_BACK_LEFT
;
194 return BUFFER_FRONT_RIGHT
;
196 return BUFFER_FRONT_RIGHT
;
198 return BUFFER_BACK_RIGHT
;
200 return BUFFER_BACK_LEFT
;
202 return BUFFER_FRONT_LEFT
;
204 return BUFFER_FRONT_LEFT
;
210 return BUFFER_COUNT
; /* invalid, but not -1 */
211 case GL_COLOR_ATTACHMENT0_EXT
:
212 return BUFFER_COLOR0
;
213 case GL_COLOR_ATTACHMENT1_EXT
:
214 return BUFFER_COLOR1
;
215 case GL_COLOR_ATTACHMENT2_EXT
:
216 return BUFFER_COLOR2
;
217 case GL_COLOR_ATTACHMENT3_EXT
:
218 return BUFFER_COLOR3
;
219 case GL_COLOR_ATTACHMENT4_EXT
:
220 return BUFFER_COLOR4
;
221 case GL_COLOR_ATTACHMENT5_EXT
:
222 return BUFFER_COLOR5
;
223 case GL_COLOR_ATTACHMENT6_EXT
:
224 return BUFFER_COLOR6
;
225 case GL_COLOR_ATTACHMENT7_EXT
:
226 return BUFFER_COLOR7
;
228 /* not an error, but also not supported */
229 if (buffer
>= GL_COLOR_ATTACHMENT8
&& buffer
<= GL_COLOR_ATTACHMENT31
)
237 is_legal_es3_readbuffer_enum(GLenum buf
)
239 return buf
== GL_BACK
|| buf
== GL_NONE
||
240 (buf
>= GL_COLOR_ATTACHMENT0
&& buf
<= GL_COLOR_ATTACHMENT31
);
244 * Called by glDrawBuffer() and glNamedFramebufferDrawBuffer().
245 * Specify which renderbuffer(s) to draw into for the first color output.
246 * <buffer> can name zero, one, two or four renderbuffers!
247 * \sa _mesa_DrawBuffers
249 * \param buffer buffer token such as GL_LEFT or GL_FRONT_AND_BACK, etc.
251 * Note that the behaviour of this function depends on whether the
252 * current ctx->DrawBuffer is a window-system framebuffer or a user-created
253 * framebuffer object.
254 * In the former case, we update the per-context ctx->Color.DrawBuffer
255 * state var _and_ the FB's ColorDrawBuffer state.
256 * In the later case, we update the FB's ColorDrawBuffer state only.
258 * Furthermore, upon a MakeCurrent() or BindFramebuffer() call, if the
259 * new FB is a window system FB, we need to re-update the FB's
260 * ColorDrawBuffer state to match the context. This is handled in
261 * _mesa_update_framebuffer().
263 * See the GL_EXT_framebuffer_object spec for more info.
266 draw_buffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
267 GLenum buffer
, const char *caller
)
271 FLUSH_VERTICES(ctx
, 0);
273 if (MESA_VERBOSE
& VERBOSE_API
) {
274 _mesa_debug(ctx
, "%s %s\n", caller
, _mesa_enum_to_string(buffer
));
277 if (buffer
== GL_NONE
) {
281 const GLbitfield supportedMask
282 = supported_buffer_bitmask(ctx
, fb
);
283 destMask
= draw_buffer_enum_to_bitmask(ctx
, buffer
);
284 if (destMask
== BAD_MASK
) {
285 /* totally bogus buffer */
286 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid buffer %s)", caller
,
287 _mesa_enum_to_string(buffer
));
290 destMask
&= supportedMask
;
291 if (destMask
== 0x0) {
292 /* none of the named color buffers exist! */
293 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid buffer %s)",
294 caller
, _mesa_enum_to_string(buffer
));
299 /* if we get here, there's no error so set new state */
300 _mesa_drawbuffers(ctx
, fb
, 1, &buffer
, &destMask
);
302 /* Call device driver function only if fb is the bound draw buffer */
303 if (fb
== ctx
->DrawBuffer
) {
304 if (ctx
->Driver
.DrawBuffers
)
305 ctx
->Driver
.DrawBuffers(ctx
, 1, &buffer
);
306 else if (ctx
->Driver
.DrawBuffer
)
307 ctx
->Driver
.DrawBuffer(ctx
, buffer
);
313 _mesa_DrawBuffer(GLenum buffer
)
315 GET_CURRENT_CONTEXT(ctx
);
316 draw_buffer(ctx
, ctx
->DrawBuffer
, buffer
, "glDrawBuffer");
321 _mesa_NamedFramebufferDrawBuffer(GLuint framebuffer
, GLenum buf
)
323 GET_CURRENT_CONTEXT(ctx
);
324 struct gl_framebuffer
*fb
;
327 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
328 "glNamedFramebufferDrawBuffer");
333 fb
= ctx
->WinSysDrawBuffer
;
335 draw_buffer(ctx
, fb
, buf
, "glNamedFramebufferDrawBuffer");
340 * Called by glDrawBuffersARB() and glNamedFramebufferDrawBuffers() to specify
341 * the destination color renderbuffers for N fragment program color outputs.
342 * \sa _mesa_DrawBuffer
343 * \param n number of outputs
344 * \param buffers array [n] of renderbuffer names. Unlike glDrawBuffer, the
345 * names cannot specify more than one buffer. For example,
346 * GL_FRONT_AND_BACK is illegal. The only exception is GL_BACK
347 * that is considered special and allowed as far as n is one
351 draw_buffers(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
352 GLsizei n
, const GLenum
*buffers
, const char *caller
)
355 GLbitfield usedBufferMask
, supportedMask
;
356 GLbitfield destMask
[MAX_DRAW_BUFFERS
];
358 FLUSH_VERTICES(ctx
, 0);
360 /* Turns out n==0 is a valid input that should not produce an error.
361 * The remaining code below correctly handles the n==0 case.
363 * From the OpenGL 3.0 specification, page 258:
364 * "An INVALID_VALUE error is generated if n is greater than
368 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n < 0)", caller
);
372 if (n
> (GLsizei
) ctx
->Const
.MaxDrawBuffers
) {
373 _mesa_error(ctx
, GL_INVALID_VALUE
,
374 "%s(n > maximum number of draw buffers)", caller
);
378 supportedMask
= supported_buffer_bitmask(ctx
, fb
);
379 usedBufferMask
= 0x0;
381 /* From the ES 3.0 specification, page 180:
382 * "If the GL is bound to the default framebuffer, then n must be 1
383 * and the constant must be BACK or NONE."
384 * (same restriction applies with GL_EXT_draw_buffers specification)
386 if (ctx
->API
== API_OPENGLES2
&& _mesa_is_winsys_fbo(fb
) &&
387 (n
!= 1 || (buffers
[0] != GL_NONE
&& buffers
[0] != GL_BACK
))) {
388 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid buffers)", caller
);
392 /* complicated error checking... */
393 for (output
= 0; output
< n
; output
++) {
394 destMask
[output
] = draw_buffer_enum_to_bitmask(ctx
, buffers
[output
]);
396 /* From the OpenGL 3.0 specification, page 258:
397 * "Each buffer listed in bufs must be one of the values from tables
398 * 4.5 or 4.6. Otherwise, an INVALID_ENUM error is generated.
400 if (destMask
[output
] == BAD_MASK
) {
401 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid buffer %s)",
402 caller
, _mesa_enum_to_string(buffers
[output
]));
406 /* From the OpenGL 4.5 specification, page 493 (page 515 of the PDF)
407 * "An INVALID_ENUM error is generated if any value in bufs is FRONT,
408 * LEFT, RIGHT, or FRONT_AND_BACK . This restriction applies to both
409 * the default framebuffer and framebuffer objects, and exists because
410 * these constants may themselves refer to multiple buffers, as shown
413 * And on page 492 (page 514 of the PDF):
414 * "If the default framebuffer is affected, then each of the constants
415 * must be one of the values listed in table 17.6 or the special value
416 * BACK. When BACK is used, n must be 1 and color values are written
417 * into the left buffer for single-buffered contexts, or into the back
418 * left buffer for double-buffered contexts."
420 * Note "special value BACK". GL_BACK also refers to multiple buffers,
421 * but it is consider a special case here. This is a change on 4.5. For
422 * OpenGL 4.x we check that behaviour. For any previous version we keep
423 * considering it wrong (as INVALID_ENUM).
425 if (_mesa_bitcount(destMask
[output
]) > 1) {
426 if (_mesa_is_winsys_fbo(fb
) && ctx
->Version
>= 40 &&
427 buffers
[output
] == GL_BACK
) {
429 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(with GL_BACK n must be 1)",
434 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid buffer %s)",
435 caller
, _mesa_enum_to_string(buffers
[output
]));
440 /* Section 4.2 (Whole Framebuffer Operations) of the OpenGL ES 3.0
441 * specification says:
443 * "If the GL is bound to a draw framebuffer object, the ith buffer
444 * listed in bufs must be COLOR_ATTACHMENTi or NONE . Specifying a
445 * buffer out of order, BACK , or COLOR_ATTACHMENTm where m is greater
446 * than or equal to the value of MAX_- COLOR_ATTACHMENTS , will
447 * generate the error INVALID_OPERATION .
449 if (_mesa_is_gles3(ctx
) && _mesa_is_user_fbo(fb
) &&
450 buffers
[output
] != GL_NONE
&&
451 (buffers
[output
] < GL_COLOR_ATTACHMENT0
||
452 buffers
[output
] >= GL_COLOR_ATTACHMENT0
+ ctx
->Const
.MaxColorAttachments
)) {
453 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawBuffers(buffer)");
457 if (buffers
[output
] == GL_NONE
) {
458 destMask
[output
] = 0x0;
461 /* Page 259 (page 275 of the PDF) in section 4.2.1 of the OpenGL 3.0
462 * spec (20080923) says:
464 * "If the GL is bound to a framebuffer object and DrawBuffers is
465 * supplied with [...] COLOR_ATTACHMENTm where m is greater than
466 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
467 * INVALID_OPERATION results."
469 if (_mesa_is_user_fbo(fb
) && buffers
[output
] >=
470 GL_COLOR_ATTACHMENT0
+ ctx
->Const
.MaxDrawBuffers
) {
471 _mesa_error(ctx
, GL_INVALID_OPERATION
,
472 "%s(buffers[%d] >= maximum number of draw buffers)",
477 /* From the OpenGL 3.0 specification, page 259:
478 * "If the GL is bound to the default framebuffer and DrawBuffers is
479 * supplied with a constant (other than NONE) that does not indicate
480 * any of the color buffers allocated to the GL context by the window
481 * system, the error INVALID_OPERATION will be generated.
483 * If the GL is bound to a framebuffer object and DrawBuffers is
484 * supplied with a constant from table 4.6 [...] then the error
485 * INVALID_OPERATION results."
487 destMask
[output
] &= supportedMask
;
488 if (destMask
[output
] == 0) {
489 _mesa_error(ctx
, GL_INVALID_OPERATION
,
490 "%s(unsupported buffer %s)",
491 caller
, _mesa_enum_to_string(buffers
[output
]));
495 /* ES 3.0 is even more restrictive. From the ES 3.0 spec, page 180:
496 * "If the GL is bound to a framebuffer object, the ith buffer listed
497 * in bufs must be COLOR_ATTACHMENTi or NONE. [...] INVALID_OPERATION."
498 * (same restriction applies with GL_EXT_draw_buffers specification)
500 if (ctx
->API
== API_OPENGLES2
&& _mesa_is_user_fbo(fb
) &&
501 buffers
[output
] != GL_NONE
&&
502 buffers
[output
] != GL_COLOR_ATTACHMENT0
+ output
) {
503 _mesa_error(ctx
, GL_INVALID_OPERATION
,
504 "%s(unsupported buffer %s)",
505 caller
, _mesa_enum_to_string(buffers
[output
]));
509 /* From the OpenGL 3.0 specification, page 258:
510 * "Except for NONE, a buffer may not appear more than once in the
511 * array pointed to by bufs. Specifying a buffer more then once will
512 * result in the error INVALID_OPERATION."
514 if (destMask
[output
] & usedBufferMask
) {
515 _mesa_error(ctx
, GL_INVALID_OPERATION
,
516 "%s(duplicated buffer %s)",
517 caller
, _mesa_enum_to_string(buffers
[output
]));
522 usedBufferMask
|= destMask
[output
];
526 /* OK, if we get here, there were no errors so set the new state */
527 _mesa_drawbuffers(ctx
, fb
, n
, buffers
, destMask
);
530 * Call device driver function if fb is the bound draw buffer.
531 * Note that n can be equal to 0,
532 * in which case we don't want to reference buffers[0], which
535 if (fb
== ctx
->DrawBuffer
) {
536 if (ctx
->Driver
.DrawBuffers
)
537 ctx
->Driver
.DrawBuffers(ctx
, n
, buffers
);
538 else if (ctx
->Driver
.DrawBuffer
)
539 ctx
->Driver
.DrawBuffer(ctx
, n
> 0 ? buffers
[0] : GL_NONE
);
545 _mesa_DrawBuffers(GLsizei n
, const GLenum
*buffers
)
547 GET_CURRENT_CONTEXT(ctx
);
548 draw_buffers(ctx
, ctx
->DrawBuffer
, n
, buffers
, "glDrawBuffers");
553 _mesa_NamedFramebufferDrawBuffers(GLuint framebuffer
, GLsizei n
,
556 GET_CURRENT_CONTEXT(ctx
);
557 struct gl_framebuffer
*fb
;
560 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
561 "glNamedFramebufferDrawBuffers");
566 fb
= ctx
->WinSysDrawBuffer
;
568 draw_buffers(ctx
, fb
, n
, bufs
, "glNamedFramebufferDrawBuffers");
573 * Performs necessary state updates when _mesa_drawbuffers makes an
577 updated_drawbuffers(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
579 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
581 if (ctx
->API
== API_OPENGL_COMPAT
&& !ctx
->Extensions
.ARB_ES2_compatibility
) {
582 /* Flag the FBO as requiring validation. */
583 if (_mesa_is_user_fbo(fb
)) {
591 * Helper function to set the GL_DRAW_BUFFER state for the given context and
592 * FBO. Called via glDrawBuffer(), glDrawBuffersARB()
594 * All error checking will have been done prior to calling this function
595 * so nothing should go wrong at this point.
597 * \param ctx current context
598 * \param fb the desired draw buffer
599 * \param n number of color outputs to set
600 * \param buffers array[n] of colorbuffer names, like GL_LEFT.
601 * \param destMask array[n] of BUFFER_BIT_* bitmasks which correspond to the
602 * colorbuffer names. (i.e. GL_FRONT_AND_BACK =>
603 * BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT).
606 _mesa_drawbuffers(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
607 GLuint n
, const GLenum
*buffers
, const GLbitfield
*destMask
)
609 GLbitfield mask
[MAX_DRAW_BUFFERS
];
613 /* compute destMask values now */
614 const GLbitfield supportedMask
= supported_buffer_bitmask(ctx
, fb
);
616 for (output
= 0; output
< n
; output
++) {
617 mask
[output
] = draw_buffer_enum_to_bitmask(ctx
, buffers
[output
]);
618 assert(mask
[output
] != BAD_MASK
);
619 mask
[output
] &= supportedMask
;
625 * destMask[0] may have up to four bits set
626 * (ex: glDrawBuffer(GL_FRONT_AND_BACK)).
627 * Otherwise, destMask[x] can only have one bit set.
629 if (n
> 0 && _mesa_bitcount(destMask
[0]) > 1) {
630 GLuint count
= 0, destMask0
= destMask
[0];
632 const int bufIndex
= u_bit_scan(&destMask0
);
633 if (fb
->_ColorDrawBufferIndexes
[count
] != bufIndex
) {
634 updated_drawbuffers(ctx
, fb
);
635 fb
->_ColorDrawBufferIndexes
[count
] = bufIndex
;
639 fb
->ColorDrawBuffer
[0] = buffers
[0];
640 fb
->_NumColorDrawBuffers
= count
;
644 for (buf
= 0; buf
< n
; buf
++ ) {
646 GLint bufIndex
= ffs(destMask
[buf
]) - 1;
647 /* only one bit should be set in the destMask[buf] field */
648 assert(_mesa_bitcount(destMask
[buf
]) == 1);
649 if (fb
->_ColorDrawBufferIndexes
[buf
] != bufIndex
) {
650 updated_drawbuffers(ctx
, fb
);
651 fb
->_ColorDrawBufferIndexes
[buf
] = bufIndex
;
656 if (fb
->_ColorDrawBufferIndexes
[buf
] != -1) {
657 updated_drawbuffers(ctx
, fb
);
658 fb
->_ColorDrawBufferIndexes
[buf
] = -1;
661 fb
->ColorDrawBuffer
[buf
] = buffers
[buf
];
663 fb
->_NumColorDrawBuffers
= count
;
666 /* set remaining outputs to -1 (GL_NONE) */
667 for (buf
= fb
->_NumColorDrawBuffers
; buf
< ctx
->Const
.MaxDrawBuffers
; buf
++) {
668 if (fb
->_ColorDrawBufferIndexes
[buf
] != -1) {
669 updated_drawbuffers(ctx
, fb
);
670 fb
->_ColorDrawBufferIndexes
[buf
] = -1;
673 for (buf
= n
; buf
< ctx
->Const
.MaxDrawBuffers
; buf
++) {
674 fb
->ColorDrawBuffer
[buf
] = GL_NONE
;
677 if (_mesa_is_winsys_fbo(fb
)) {
678 /* also set context drawbuffer state */
679 for (buf
= 0; buf
< ctx
->Const
.MaxDrawBuffers
; buf
++) {
680 if (ctx
->Color
.DrawBuffer
[buf
] != fb
->ColorDrawBuffer
[buf
]) {
681 updated_drawbuffers(ctx
, fb
);
682 ctx
->Color
.DrawBuffer
[buf
] = fb
->ColorDrawBuffer
[buf
];
690 * Update the current drawbuffer's _ColorDrawBufferIndex[] list, etc.
691 * from the context's Color.DrawBuffer[] state.
692 * Use when changing contexts.
695 _mesa_update_draw_buffers(struct gl_context
*ctx
)
697 /* should be a window system FBO */
698 assert(_mesa_is_winsys_fbo(ctx
->DrawBuffer
));
700 _mesa_drawbuffers(ctx
, ctx
->DrawBuffer
, ctx
->Const
.MaxDrawBuffers
,
701 ctx
->Color
.DrawBuffer
, NULL
);
706 * Like \sa _mesa_drawbuffers(), this is a helper function for setting
707 * GL_READ_BUFFER state for the given context and FBO.
708 * Note that all error checking should have been done before calling
710 * \param ctx the rendering context
711 * \param fb the framebuffer object to update
712 * \param buffer GL_FRONT, GL_BACK, GL_COLOR_ATTACHMENT0, etc.
713 * \param bufferIndex the numerical index corresponding to 'buffer'
716 _mesa_readbuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
717 GLenum buffer
, gl_buffer_index bufferIndex
)
719 if ((fb
== ctx
->ReadBuffer
) && _mesa_is_winsys_fbo(fb
)) {
720 /* Only update the per-context READ_BUFFER state if we're bound to
721 * a window-system framebuffer.
723 ctx
->Pixel
.ReadBuffer
= buffer
;
726 fb
->ColorReadBuffer
= buffer
;
727 fb
->_ColorReadBufferIndex
= bufferIndex
;
729 ctx
->NewState
|= _NEW_BUFFERS
;
735 * Called by glReadBuffer and glNamedFramebufferReadBuffer to set the source
736 * renderbuffer for reading pixels.
737 * \param mode color buffer such as GL_FRONT, GL_BACK, etc.
740 read_buffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
741 GLenum buffer
, const char *caller
)
743 GLbitfield supportedMask
;
744 gl_buffer_index srcBuffer
;
746 FLUSH_VERTICES(ctx
, 0);
748 if (MESA_VERBOSE
& VERBOSE_API
)
749 _mesa_debug(ctx
, "%s %s\n", caller
, _mesa_enum_to_string(buffer
));
751 if (buffer
== GL_NONE
) {
752 /* This is legal--it means that no buffer should be bound for reading. */
756 /* general case / window-system framebuffer */
757 if (_mesa_is_gles3(ctx
) && !is_legal_es3_readbuffer_enum(buffer
))
760 srcBuffer
= read_buffer_enum_to_index(ctx
, buffer
);
762 if (srcBuffer
== -1) {
763 _mesa_error(ctx
, GL_INVALID_ENUM
,
764 "%s(invalid buffer %s)", caller
,
765 _mesa_enum_to_string(buffer
));
768 supportedMask
= supported_buffer_bitmask(ctx
, fb
);
769 if (((1 << srcBuffer
) & supportedMask
) == 0) {
770 _mesa_error(ctx
, GL_INVALID_OPERATION
,
771 "%s(invalid buffer %s)", caller
,
772 _mesa_enum_to_string(buffer
));
777 /* OK, all error checking has been completed now */
779 _mesa_readbuffer(ctx
, fb
, buffer
, srcBuffer
);
781 /* Call the device driver function only if fb is the bound read buffer */
782 if (fb
== ctx
->ReadBuffer
) {
783 if (ctx
->Driver
.ReadBuffer
)
784 ctx
->Driver
.ReadBuffer(ctx
, buffer
);
790 _mesa_ReadBuffer(GLenum buffer
)
792 GET_CURRENT_CONTEXT(ctx
);
793 read_buffer(ctx
, ctx
->ReadBuffer
, buffer
, "glReadBuffer");
798 _mesa_NamedFramebufferReadBuffer(GLuint framebuffer
, GLenum src
)
800 GET_CURRENT_CONTEXT(ctx
);
801 struct gl_framebuffer
*fb
;
804 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
805 "glNamedFramebufferReadBuffer");
810 fb
= ctx
->WinSysReadBuffer
;
812 read_buffer(ctx
, fb
, src
, "glNamedFramebufferReadBuffer");