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 BUFFER_NONE 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
;
207 case GL_FRONT_AND_BACK
:
208 return BUFFER_FRONT_LEFT
;
212 return BUFFER_COUNT
; /* invalid, but not -1 */
213 case GL_COLOR_ATTACHMENT0_EXT
:
214 return BUFFER_COLOR0
;
215 case GL_COLOR_ATTACHMENT1_EXT
:
216 return BUFFER_COLOR1
;
217 case GL_COLOR_ATTACHMENT2_EXT
:
218 return BUFFER_COLOR2
;
219 case GL_COLOR_ATTACHMENT3_EXT
:
220 return BUFFER_COLOR3
;
221 case GL_COLOR_ATTACHMENT4_EXT
:
222 return BUFFER_COLOR4
;
223 case GL_COLOR_ATTACHMENT5_EXT
:
224 return BUFFER_COLOR5
;
225 case GL_COLOR_ATTACHMENT6_EXT
:
226 return BUFFER_COLOR6
;
227 case GL_COLOR_ATTACHMENT7_EXT
:
228 return BUFFER_COLOR7
;
230 /* not an error, but also not supported */
231 if (buffer
>= GL_COLOR_ATTACHMENT8
&& buffer
<= GL_COLOR_ATTACHMENT31
)
239 is_legal_es3_readbuffer_enum(GLenum buf
)
241 return buf
== GL_BACK
|| buf
== GL_NONE
||
242 (buf
>= GL_COLOR_ATTACHMENT0
&& buf
<= GL_COLOR_ATTACHMENT31
);
246 * Called by glDrawBuffer() and glNamedFramebufferDrawBuffer().
247 * Specify which renderbuffer(s) to draw into for the first color output.
248 * <buffer> can name zero, one, two or four renderbuffers!
249 * \sa _mesa_DrawBuffers
251 * \param buffer buffer token such as GL_LEFT or GL_FRONT_AND_BACK, etc.
253 * Note that the behaviour of this function depends on whether the
254 * current ctx->DrawBuffer is a window-system framebuffer or a user-created
255 * framebuffer object.
256 * In the former case, we update the per-context ctx->Color.DrawBuffer
257 * state var _and_ the FB's ColorDrawBuffer state.
258 * In the later case, we update the FB's ColorDrawBuffer state only.
260 * Furthermore, upon a MakeCurrent() or BindFramebuffer() call, if the
261 * new FB is a window system FB, we need to re-update the FB's
262 * ColorDrawBuffer state to match the context. This is handled in
263 * _mesa_update_framebuffer().
265 * See the GL_EXT_framebuffer_object spec for more info.
267 static ALWAYS_INLINE
void
268 draw_buffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
269 GLenum buffer
, const char *caller
, bool no_error
)
273 FLUSH_VERTICES(ctx
, 0);
275 if (MESA_VERBOSE
& VERBOSE_API
) {
276 _mesa_debug(ctx
, "%s %s\n", caller
, _mesa_enum_to_string(buffer
));
279 if (buffer
== GL_NONE
) {
283 const GLbitfield supportedMask
284 = supported_buffer_bitmask(ctx
, fb
);
285 destMask
= draw_buffer_enum_to_bitmask(ctx
, buffer
);
286 if (!no_error
&& destMask
== BAD_MASK
) {
287 /* totally bogus buffer */
288 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid buffer %s)", caller
,
289 _mesa_enum_to_string(buffer
));
292 destMask
&= supportedMask
;
293 if (!no_error
&& destMask
== 0x0) {
294 /* none of the named color buffers exist! */
295 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid buffer %s)",
296 caller
, _mesa_enum_to_string(buffer
));
301 /* if we get here, there's no error so set new state */
302 const GLenum16 buffer16
= buffer
;
303 _mesa_drawbuffers(ctx
, fb
, 1, &buffer16
, &destMask
);
305 /* Call device driver function only if fb is the bound draw buffer */
306 if (fb
== ctx
->DrawBuffer
) {
307 if (ctx
->Driver
.DrawBuffer
)
308 ctx
->Driver
.DrawBuffer(ctx
, buffer
);
314 draw_buffer_error(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
315 GLenum buffer
, const char *caller
)
317 draw_buffer(ctx
, fb
, buffer
, caller
, false);
322 draw_buffer_no_error(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
323 GLenum buffer
, const char *caller
)
325 draw_buffer(ctx
, fb
, buffer
, caller
, true);
330 _mesa_DrawBuffer_no_error(GLenum buffer
)
332 GET_CURRENT_CONTEXT(ctx
);
333 draw_buffer_no_error(ctx
, ctx
->DrawBuffer
, buffer
, "glDrawBuffer");
338 _mesa_DrawBuffer(GLenum buffer
)
340 GET_CURRENT_CONTEXT(ctx
);
341 draw_buffer_error(ctx
, ctx
->DrawBuffer
, buffer
, "glDrawBuffer");
346 _mesa_NamedFramebufferDrawBuffer_no_error(GLuint framebuffer
, GLenum buf
)
348 GET_CURRENT_CONTEXT(ctx
);
349 struct gl_framebuffer
*fb
;
352 fb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
354 fb
= ctx
->WinSysDrawBuffer
;
357 draw_buffer_no_error(ctx
, fb
, buf
, "glNamedFramebufferDrawBuffer");
362 _mesa_NamedFramebufferDrawBuffer(GLuint framebuffer
, GLenum buf
)
364 GET_CURRENT_CONTEXT(ctx
);
365 struct gl_framebuffer
*fb
;
368 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
369 "glNamedFramebufferDrawBuffer");
374 fb
= ctx
->WinSysDrawBuffer
;
376 draw_buffer_error(ctx
, fb
, buf
, "glNamedFramebufferDrawBuffer");
381 * Called by glDrawBuffersARB() and glNamedFramebufferDrawBuffers() to specify
382 * the destination color renderbuffers for N fragment program color outputs.
383 * \sa _mesa_DrawBuffer
384 * \param n number of outputs
385 * \param buffers array [n] of renderbuffer names. Unlike glDrawBuffer, the
386 * names cannot specify more than one buffer. For example,
387 * GL_FRONT_AND_BACK is illegal. The only exception is GL_BACK
388 * that is considered special and allowed as far as n is one
391 static ALWAYS_INLINE
void
392 draw_buffers(struct gl_context
*ctx
, struct gl_framebuffer
*fb
, GLsizei n
,
393 const GLenum
*buffers
, const char *caller
, bool no_error
)
396 GLbitfield usedBufferMask
, supportedMask
;
397 GLbitfield destMask
[MAX_DRAW_BUFFERS
];
399 FLUSH_VERTICES(ctx
, 0);
402 /* Turns out n==0 is a valid input that should not produce an error.
403 * The remaining code below correctly handles the n==0 case.
405 * From the OpenGL 3.0 specification, page 258:
406 * "An INVALID_VALUE error is generated if n is greater than
410 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n < 0)", caller
);
414 if (n
> (GLsizei
) ctx
->Const
.MaxDrawBuffers
) {
415 _mesa_error(ctx
, GL_INVALID_VALUE
,
416 "%s(n > maximum number of draw buffers)", caller
);
420 /* From the ES 3.0 specification, page 180:
421 * "If the GL is bound to the default framebuffer, then n must be 1
422 * and the constant must be BACK or NONE."
423 * (same restriction applies with GL_EXT_draw_buffers specification)
425 if (ctx
->API
== API_OPENGLES2
&& _mesa_is_winsys_fbo(fb
) &&
426 (n
!= 1 || (buffers
[0] != GL_NONE
&& buffers
[0] != GL_BACK
))) {
427 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid buffers)", caller
);
432 supportedMask
= supported_buffer_bitmask(ctx
, fb
);
433 usedBufferMask
= 0x0;
435 /* complicated error checking... */
436 for (output
= 0; output
< n
; output
++) {
437 destMask
[output
] = draw_buffer_enum_to_bitmask(ctx
, buffers
[output
]);
440 /* From the OpenGL 3.0 specification, page 258:
441 * "Each buffer listed in bufs must be one of the values from tables
442 * 4.5 or 4.6. Otherwise, an INVALID_ENUM error is generated.
444 if (destMask
[output
] == BAD_MASK
) {
445 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid buffer %s)",
446 caller
, _mesa_enum_to_string(buffers
[output
]));
450 /* From the OpenGL 4.5 specification, page 493 (page 515 of the PDF)
451 * "An INVALID_ENUM error is generated if any value in bufs is FRONT,
452 * LEFT, RIGHT, or FRONT_AND_BACK . This restriction applies to both
453 * the default framebuffer and framebuffer objects, and exists because
454 * these constants may themselves refer to multiple buffers, as shown
457 * And on page 492 (page 514 of the PDF):
458 * "If the default framebuffer is affected, then each of the constants
459 * must be one of the values listed in table 17.6 or the special value
460 * BACK. When BACK is used, n must be 1 and color values are written
461 * into the left buffer for single-buffered contexts, or into the back
462 * left buffer for double-buffered contexts."
464 * Note "special value BACK". GL_BACK also refers to multiple buffers,
465 * but it is consider a special case here. This is a change on 4.5.
466 * For OpenGL 4.x we check that behaviour. For any previous version we
467 * keep considering it wrong (as INVALID_ENUM).
469 if (_mesa_bitcount(destMask
[output
]) > 1) {
470 if (_mesa_is_winsys_fbo(fb
) && ctx
->Version
>= 40 &&
471 buffers
[output
] == GL_BACK
) {
473 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(with GL_BACK n must be 1)",
478 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid buffer %s)",
479 caller
, _mesa_enum_to_string(buffers
[output
]));
484 /* Section 4.2 (Whole Framebuffer Operations) of the OpenGL ES 3.0
485 * specification says:
487 * "If the GL is bound to a draw framebuffer object, the ith
488 * buffer listed in bufs must be COLOR_ATTACHMENTi or NONE .
489 * Specifying a buffer out of order, BACK , or COLOR_ATTACHMENTm
490 * where m is greater than or equal to the value of MAX_-
491 * COLOR_ATTACHMENTS , will generate the error INVALID_OPERATION .
493 if (_mesa_is_gles3(ctx
) && _mesa_is_user_fbo(fb
) &&
494 buffers
[output
] != GL_NONE
&&
495 (buffers
[output
] < GL_COLOR_ATTACHMENT0
||
496 buffers
[output
] >= GL_COLOR_ATTACHMENT0
+ ctx
->Const
.MaxColorAttachments
)) {
497 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawBuffers(buffer)");
502 if (buffers
[output
] == GL_NONE
) {
503 destMask
[output
] = 0x0;
506 /* Page 259 (page 275 of the PDF) in section 4.2.1 of the OpenGL 3.0
507 * spec (20080923) says:
509 * "If the GL is bound to a framebuffer object and DrawBuffers is
510 * supplied with [...] COLOR_ATTACHMENTm where m is greater than
511 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
512 * INVALID_OPERATION results."
514 if (!no_error
&& _mesa_is_user_fbo(fb
) && buffers
[output
] >=
515 GL_COLOR_ATTACHMENT0
+ ctx
->Const
.MaxDrawBuffers
) {
516 _mesa_error(ctx
, GL_INVALID_OPERATION
,
517 "%s(buffers[%d] >= maximum number of draw buffers)",
522 /* From the OpenGL 3.0 specification, page 259:
523 * "If the GL is bound to the default framebuffer and DrawBuffers is
524 * supplied with a constant (other than NONE) that does not indicate
525 * any of the color buffers allocated to the GL context by the window
526 * system, the error INVALID_OPERATION will be generated.
528 * If the GL is bound to a framebuffer object and DrawBuffers is
529 * supplied with a constant from table 4.6 [...] then the error
530 * INVALID_OPERATION results."
532 destMask
[output
] &= supportedMask
;
534 if (destMask
[output
] == 0) {
535 _mesa_error(ctx
, GL_INVALID_OPERATION
,
536 "%s(unsupported buffer %s)",
537 caller
, _mesa_enum_to_string(buffers
[output
]));
541 /* ES 3.0 is even more restrictive. From the ES 3.0 spec, page 180:
542 * "If the GL is bound to a framebuffer object, the ith buffer
543 * listed in bufs must be COLOR_ATTACHMENTi or NONE. [...]
544 * INVALID_OPERATION." (same restriction applies with
545 * GL_EXT_draw_buffers specification)
547 if (ctx
->API
== API_OPENGLES2
&& _mesa_is_user_fbo(fb
) &&
548 buffers
[output
] != GL_NONE
&&
549 buffers
[output
] != GL_COLOR_ATTACHMENT0
+ output
) {
550 _mesa_error(ctx
, GL_INVALID_OPERATION
,
551 "%s(unsupported buffer %s)",
552 caller
, _mesa_enum_to_string(buffers
[output
]));
556 /* From the OpenGL 3.0 specification, page 258:
557 * "Except for NONE, a buffer may not appear more than once in the
558 * array pointed to by bufs. Specifying a buffer more then once
559 * will result in the error INVALID_OPERATION."
561 if (destMask
[output
] & usedBufferMask
) {
562 _mesa_error(ctx
, GL_INVALID_OPERATION
,
563 "%s(duplicated buffer %s)",
564 caller
, _mesa_enum_to_string(buffers
[output
]));
570 usedBufferMask
|= destMask
[output
];
574 /* OK, if we get here, there were no errors so set the new state */
575 GLenum16 buffers16
[MAX_DRAW_BUFFERS
];
576 for (int i
= 0; i
< n
; i
++)
577 buffers16
[i
] = buffers
[i
];
579 _mesa_drawbuffers(ctx
, fb
, n
, buffers16
, destMask
);
582 * Call device driver function if fb is the bound draw buffer.
583 * Note that n can be equal to 0,
584 * in which case we don't want to reference buffers[0], which
587 if (fb
== ctx
->DrawBuffer
) {
588 if (ctx
->Driver
.DrawBuffer
)
589 ctx
->Driver
.DrawBuffer(ctx
, n
> 0 ? buffers
[0] : GL_NONE
);
595 draw_buffers_error(struct gl_context
*ctx
, struct gl_framebuffer
*fb
, GLsizei n
,
596 const GLenum
*buffers
, const char *caller
)
598 draw_buffers(ctx
, fb
, n
, buffers
, caller
, false);
603 draw_buffers_no_error(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
604 GLsizei n
, const GLenum
*buffers
, const char *caller
)
606 draw_buffers(ctx
, fb
, n
, buffers
, caller
, true);
611 _mesa_DrawBuffers_no_error(GLsizei n
, const GLenum
*buffers
)
613 GET_CURRENT_CONTEXT(ctx
);
614 draw_buffers_no_error(ctx
, ctx
->DrawBuffer
, n
, buffers
, "glDrawBuffers");
619 _mesa_DrawBuffers(GLsizei n
, const GLenum
*buffers
)
621 GET_CURRENT_CONTEXT(ctx
);
622 draw_buffers_error(ctx
, ctx
->DrawBuffer
, n
, buffers
, "glDrawBuffers");
627 _mesa_NamedFramebufferDrawBuffers_no_error(GLuint framebuffer
, GLsizei n
,
630 GET_CURRENT_CONTEXT(ctx
);
631 struct gl_framebuffer
*fb
;
634 fb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
636 fb
= ctx
->WinSysDrawBuffer
;
639 draw_buffers_no_error(ctx
, fb
, n
, bufs
, "glNamedFramebufferDrawBuffers");
644 _mesa_NamedFramebufferDrawBuffers(GLuint framebuffer
, GLsizei n
,
647 GET_CURRENT_CONTEXT(ctx
);
648 struct gl_framebuffer
*fb
;
651 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
652 "glNamedFramebufferDrawBuffers");
657 fb
= ctx
->WinSysDrawBuffer
;
659 draw_buffers_error(ctx
, fb
, n
, bufs
, "glNamedFramebufferDrawBuffers");
664 * Performs necessary state updates when _mesa_drawbuffers makes an
668 updated_drawbuffers(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
670 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
672 if (ctx
->API
== API_OPENGL_COMPAT
&& !ctx
->Extensions
.ARB_ES2_compatibility
) {
673 /* Flag the FBO as requiring validation. */
674 if (_mesa_is_user_fbo(fb
)) {
682 * Helper function to set the GL_DRAW_BUFFER state for the given context and
683 * FBO. Called via glDrawBuffer(), glDrawBuffersARB()
685 * All error checking will have been done prior to calling this function
686 * so nothing should go wrong at this point.
688 * \param ctx current context
689 * \param fb the desired draw buffer
690 * \param n number of color outputs to set
691 * \param buffers array[n] of colorbuffer names, like GL_LEFT.
692 * \param destMask array[n] of BUFFER_BIT_* bitmasks which correspond to the
693 * colorbuffer names. (i.e. GL_FRONT_AND_BACK =>
694 * BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT).
697 _mesa_drawbuffers(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
698 GLuint n
, const GLenum16
*buffers
,
699 const GLbitfield
*destMask
)
701 GLbitfield mask
[MAX_DRAW_BUFFERS
];
705 /* compute destMask values now */
706 const GLbitfield supportedMask
= supported_buffer_bitmask(ctx
, fb
);
708 for (output
= 0; output
< n
; output
++) {
709 mask
[output
] = draw_buffer_enum_to_bitmask(ctx
, buffers
[output
]);
710 assert(mask
[output
] != BAD_MASK
);
711 mask
[output
] &= supportedMask
;
717 * destMask[0] may have up to four bits set
718 * (ex: glDrawBuffer(GL_FRONT_AND_BACK)).
719 * Otherwise, destMask[x] can only have one bit set.
721 if (n
> 0 && _mesa_bitcount(destMask
[0]) > 1) {
722 GLuint count
= 0, destMask0
= destMask
[0];
724 const gl_buffer_index bufIndex
= u_bit_scan(&destMask0
);
725 if (fb
->_ColorDrawBufferIndexes
[count
] != bufIndex
) {
726 updated_drawbuffers(ctx
, fb
);
727 fb
->_ColorDrawBufferIndexes
[count
] = bufIndex
;
731 fb
->ColorDrawBuffer
[0] = buffers
[0];
732 fb
->_NumColorDrawBuffers
= count
;
736 for (buf
= 0; buf
< n
; buf
++ ) {
738 gl_buffer_index bufIndex
= ffs(destMask
[buf
]) - 1;
739 /* only one bit should be set in the destMask[buf] field */
740 assert(_mesa_bitcount(destMask
[buf
]) == 1);
741 if (fb
->_ColorDrawBufferIndexes
[buf
] != bufIndex
) {
742 updated_drawbuffers(ctx
, fb
);
743 fb
->_ColorDrawBufferIndexes
[buf
] = bufIndex
;
748 if (fb
->_ColorDrawBufferIndexes
[buf
] != BUFFER_NONE
) {
749 updated_drawbuffers(ctx
, fb
);
750 fb
->_ColorDrawBufferIndexes
[buf
] = BUFFER_NONE
;
753 fb
->ColorDrawBuffer
[buf
] = buffers
[buf
];
755 fb
->_NumColorDrawBuffers
= count
;
758 /* set remaining outputs to BUFFER_NONE */
759 for (buf
= fb
->_NumColorDrawBuffers
; buf
< ctx
->Const
.MaxDrawBuffers
; buf
++) {
760 if (fb
->_ColorDrawBufferIndexes
[buf
] != BUFFER_NONE
) {
761 updated_drawbuffers(ctx
, fb
);
762 fb
->_ColorDrawBufferIndexes
[buf
] = BUFFER_NONE
;
765 for (buf
= n
; buf
< ctx
->Const
.MaxDrawBuffers
; buf
++) {
766 fb
->ColorDrawBuffer
[buf
] = GL_NONE
;
769 if (_mesa_is_winsys_fbo(fb
)) {
770 /* also set context drawbuffer state */
771 for (buf
= 0; buf
< ctx
->Const
.MaxDrawBuffers
; buf
++) {
772 if (ctx
->Color
.DrawBuffer
[buf
] != fb
->ColorDrawBuffer
[buf
]) {
773 updated_drawbuffers(ctx
, fb
);
774 ctx
->Color
.DrawBuffer
[buf
] = fb
->ColorDrawBuffer
[buf
];
782 * Update the current drawbuffer's _ColorDrawBufferIndex[] list, etc.
783 * from the context's Color.DrawBuffer[] state.
784 * Use when changing contexts.
787 _mesa_update_draw_buffers(struct gl_context
*ctx
)
789 /* should be a window system FBO */
790 assert(_mesa_is_winsys_fbo(ctx
->DrawBuffer
));
792 _mesa_drawbuffers(ctx
, ctx
->DrawBuffer
, ctx
->Const
.MaxDrawBuffers
,
793 ctx
->Color
.DrawBuffer
, NULL
);
798 * Like \sa _mesa_drawbuffers(), this is a helper function for setting
799 * GL_READ_BUFFER state for the given context and FBO.
800 * Note that all error checking should have been done before calling
802 * \param ctx the rendering context
803 * \param fb the framebuffer object to update
804 * \param buffer GL_FRONT, GL_BACK, GL_COLOR_ATTACHMENT0, etc.
805 * \param bufferIndex the numerical index corresponding to 'buffer'
808 _mesa_readbuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
809 GLenum buffer
, gl_buffer_index bufferIndex
)
811 if ((fb
== ctx
->ReadBuffer
) && _mesa_is_winsys_fbo(fb
)) {
812 /* Only update the per-context READ_BUFFER state if we're bound to
813 * a window-system framebuffer.
815 ctx
->Pixel
.ReadBuffer
= buffer
;
818 fb
->ColorReadBuffer
= buffer
;
819 fb
->_ColorReadBufferIndex
= bufferIndex
;
821 ctx
->NewState
|= _NEW_BUFFERS
;
827 * Called by glReadBuffer and glNamedFramebufferReadBuffer to set the source
828 * renderbuffer for reading pixels.
829 * \param mode color buffer such as GL_FRONT, GL_BACK, etc.
831 static ALWAYS_INLINE
void
832 read_buffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
833 GLenum buffer
, const char *caller
, bool no_error
)
835 gl_buffer_index srcBuffer
;
837 FLUSH_VERTICES(ctx
, 0);
839 if (MESA_VERBOSE
& VERBOSE_API
)
840 _mesa_debug(ctx
, "%s %s\n", caller
, _mesa_enum_to_string(buffer
));
842 if (buffer
== GL_NONE
) {
843 /* This is legal--it means that no buffer should be bound for reading. */
844 srcBuffer
= BUFFER_NONE
;
847 /* general case / window-system framebuffer */
848 if (!no_error
&&_mesa_is_gles3(ctx
) &&
849 !is_legal_es3_readbuffer_enum(buffer
))
850 srcBuffer
= BUFFER_NONE
;
852 srcBuffer
= read_buffer_enum_to_index(ctx
, buffer
);
855 GLbitfield supportedMask
;
857 if (srcBuffer
== BUFFER_NONE
) {
858 _mesa_error(ctx
, GL_INVALID_ENUM
,
859 "%s(invalid buffer %s)", caller
,
860 _mesa_enum_to_string(buffer
));
864 supportedMask
= supported_buffer_bitmask(ctx
, fb
);
865 if (((1 << srcBuffer
) & supportedMask
) == 0) {
866 _mesa_error(ctx
, GL_INVALID_OPERATION
,
867 "%s(invalid buffer %s)", caller
,
868 _mesa_enum_to_string(buffer
));
874 /* OK, all error checking has been completed now */
876 _mesa_readbuffer(ctx
, fb
, buffer
, srcBuffer
);
878 /* Call the device driver function only if fb is the bound read buffer */
879 if (fb
== ctx
->ReadBuffer
) {
880 if (ctx
->Driver
.ReadBuffer
)
881 ctx
->Driver
.ReadBuffer(ctx
, buffer
);
887 read_buffer_err(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
888 GLenum buffer
, const char *caller
)
890 read_buffer(ctx
, fb
, buffer
, caller
, false);
895 read_buffer_no_error(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
896 GLenum buffer
, const char *caller
)
898 read_buffer(ctx
, fb
, buffer
, caller
, true);
903 _mesa_ReadBuffer_no_error(GLenum buffer
)
905 GET_CURRENT_CONTEXT(ctx
);
906 read_buffer_no_error(ctx
, ctx
->ReadBuffer
, buffer
, "glReadBuffer");
911 _mesa_ReadBuffer(GLenum buffer
)
913 GET_CURRENT_CONTEXT(ctx
);
914 read_buffer_err(ctx
, ctx
->ReadBuffer
, buffer
, "glReadBuffer");
919 _mesa_NamedFramebufferReadBuffer_no_error(GLuint framebuffer
, GLenum src
)
921 GET_CURRENT_CONTEXT(ctx
);
923 struct gl_framebuffer
*fb
;
926 fb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
928 fb
= ctx
->WinSysReadBuffer
;
931 read_buffer_no_error(ctx
, fb
, src
, "glNamedFramebufferReadBuffer");
936 _mesa_NamedFramebufferReadBuffer(GLuint framebuffer
, GLenum src
)
938 GET_CURRENT_CONTEXT(ctx
);
939 struct gl_framebuffer
*fb
;
942 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
943 "glNamedFramebufferReadBuffer");
948 fb
= ctx
->WinSysReadBuffer
;
950 read_buffer_err(ctx
, fb
, src
, "glNamedFramebufferReadBuffer");