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"
40 #include "util/u_math.h"
47 * Return bitmask of BUFFER_BIT_* flags indicating which color buffers are
48 * available to the rendering context (for drawing or reading).
49 * This depends on the type of framebuffer. For window system framebuffers
50 * we look at the framebuffer's visual. But for user-create framebuffers we
51 * look at the number of supported color attachments.
52 * \param fb the framebuffer to draw to, or read from
53 * \return bitmask of BUFFER_BIT_* flags
56 supported_buffer_bitmask(const struct gl_context
*ctx
,
57 const struct gl_framebuffer
*fb
)
59 GLbitfield mask
= 0x0;
61 if (_mesa_is_user_fbo(fb
)) {
62 /* A user-created renderbuffer */
63 mask
= ((1 << ctx
->Const
.MaxColorAttachments
) - 1) << BUFFER_COLOR0
;
66 /* A window system framebuffer */
68 mask
= BUFFER_BIT_FRONT_LEFT
; /* always have this */
69 if (fb
->Visual
.stereoMode
) {
70 mask
|= BUFFER_BIT_FRONT_RIGHT
;
71 if (fb
->Visual
.doubleBufferMode
) {
72 mask
|= BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_BACK_RIGHT
;
75 else if (fb
->Visual
.doubleBufferMode
) {
76 mask
|= BUFFER_BIT_BACK_LEFT
;
79 for (i
= 0; i
< fb
->Visual
.numAuxBuffers
; i
++) {
80 mask
|= (BUFFER_BIT_AUX0
<< i
);
89 * Helper routine used by glDrawBuffer and glDrawBuffersARB.
90 * Given a GLenum naming one or more color buffers (such as
91 * GL_FRONT_AND_BACK), return the corresponding bitmask of BUFFER_BIT_* flags.
94 draw_buffer_enum_to_bitmask(const struct gl_context
*ctx
, GLenum buffer
)
96 /* If the front buffer is the only buffer, GL_BACK and all other flags
97 * that include BACK select the front buffer for drawing. There are
98 * several reasons we want to do this.
100 * 1) OpenGL ES 3.0 requires it:
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 * We also do this for GLES 1 and 2 because those APIs have no
110 * concept of selecting the front and back buffer anyway and it's
111 * convenient to be able to maintain the magic behaviour of
112 * GL_BACK in that case.
114 * 2) Pbuffers are back buffers from the application point of view,
115 * but they are front buffers from the Mesa point of view,
116 * because they are always single buffered.
118 if (!ctx
->DrawBuffer
->Visual
.doubleBufferMode
) {
124 buffer
= GL_FRONT_RIGHT
;
127 buffer
= GL_FRONT_LEFT
;
136 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_FRONT_RIGHT
;
138 return BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_BACK_RIGHT
;
140 return BUFFER_BIT_FRONT_RIGHT
| BUFFER_BIT_BACK_RIGHT
;
142 return BUFFER_BIT_FRONT_RIGHT
;
144 return BUFFER_BIT_BACK_RIGHT
;
146 return BUFFER_BIT_BACK_LEFT
;
147 case GL_FRONT_AND_BACK
:
148 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_BACK_LEFT
149 | BUFFER_BIT_FRONT_RIGHT
| BUFFER_BIT_BACK_RIGHT
;
151 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_BACK_LEFT
;
153 return BUFFER_BIT_FRONT_LEFT
;
155 return BUFFER_BIT_AUX0
;
159 return 1 << BUFFER_COUNT
; /* invalid, but not BAD_MASK */
160 case GL_COLOR_ATTACHMENT0_EXT
:
161 return BUFFER_BIT_COLOR0
;
162 case GL_COLOR_ATTACHMENT1_EXT
:
163 return BUFFER_BIT_COLOR1
;
164 case GL_COLOR_ATTACHMENT2_EXT
:
165 return BUFFER_BIT_COLOR2
;
166 case GL_COLOR_ATTACHMENT3_EXT
:
167 return BUFFER_BIT_COLOR3
;
168 case GL_COLOR_ATTACHMENT4_EXT
:
169 return BUFFER_BIT_COLOR4
;
170 case GL_COLOR_ATTACHMENT5_EXT
:
171 return BUFFER_BIT_COLOR5
;
172 case GL_COLOR_ATTACHMENT6_EXT
:
173 return BUFFER_BIT_COLOR6
;
174 case GL_COLOR_ATTACHMENT7_EXT
:
175 return BUFFER_BIT_COLOR7
;
177 /* not an error, but also not supported */
178 if (buffer
>= GL_COLOR_ATTACHMENT8
&& buffer
<= GL_COLOR_ATTACHMENT31
)
179 return 1 << BUFFER_COUNT
;
187 * Helper routine used by glReadBuffer.
188 * Given a GLenum naming a color buffer, return the index of the corresponding
189 * renderbuffer (a BUFFER_* value).
190 * return BUFFER_NONE for an invalid buffer.
192 static gl_buffer_index
193 read_buffer_enum_to_index(const struct gl_context
*ctx
, GLenum buffer
)
197 return BUFFER_FRONT_LEFT
;
199 if (_mesa_is_gles(ctx
)) {
200 /* In draw_buffer_enum_to_bitmask, when GLES contexts draw to
201 * GL_BACK with a single-buffered configuration, we actually end
202 * up drawing to the sole front buffer in our internal
203 * representation. For consistency, we must read from that
204 * front left buffer too.
206 if (!ctx
->DrawBuffer
->Visual
.doubleBufferMode
)
207 return BUFFER_FRONT_LEFT
;
209 return BUFFER_BACK_LEFT
;
211 return BUFFER_FRONT_RIGHT
;
213 return BUFFER_FRONT_RIGHT
;
215 return BUFFER_BACK_RIGHT
;
217 return BUFFER_BACK_LEFT
;
219 return BUFFER_FRONT_LEFT
;
221 return BUFFER_FRONT_LEFT
;
224 case GL_FRONT_AND_BACK
:
225 return BUFFER_FRONT_LEFT
;
229 return BUFFER_COUNT
; /* invalid, but not -1 */
230 case GL_COLOR_ATTACHMENT0_EXT
:
231 return BUFFER_COLOR0
;
232 case GL_COLOR_ATTACHMENT1_EXT
:
233 return BUFFER_COLOR1
;
234 case GL_COLOR_ATTACHMENT2_EXT
:
235 return BUFFER_COLOR2
;
236 case GL_COLOR_ATTACHMENT3_EXT
:
237 return BUFFER_COLOR3
;
238 case GL_COLOR_ATTACHMENT4_EXT
:
239 return BUFFER_COLOR4
;
240 case GL_COLOR_ATTACHMENT5_EXT
:
241 return BUFFER_COLOR5
;
242 case GL_COLOR_ATTACHMENT6_EXT
:
243 return BUFFER_COLOR6
;
244 case GL_COLOR_ATTACHMENT7_EXT
:
245 return BUFFER_COLOR7
;
247 /* not an error, but also not supported */
248 if (buffer
>= GL_COLOR_ATTACHMENT8
&& buffer
<= GL_COLOR_ATTACHMENT31
)
256 is_legal_es3_readbuffer_enum(GLenum buf
)
258 return buf
== GL_BACK
|| buf
== GL_NONE
||
259 (buf
>= GL_COLOR_ATTACHMENT0
&& buf
<= GL_COLOR_ATTACHMENT31
);
263 * Called by glDrawBuffer() and glNamedFramebufferDrawBuffer().
264 * Specify which renderbuffer(s) to draw into for the first color output.
265 * <buffer> can name zero, one, two or four renderbuffers!
266 * \sa _mesa_DrawBuffers
268 * \param buffer buffer token such as GL_LEFT or GL_FRONT_AND_BACK, etc.
270 * Note that the behaviour of this function depends on whether the
271 * current ctx->DrawBuffer is a window-system framebuffer or a user-created
272 * framebuffer object.
273 * In the former case, we update the per-context ctx->Color.DrawBuffer
274 * state var _and_ the FB's ColorDrawBuffer state.
275 * In the later case, we update the FB's ColorDrawBuffer state only.
277 * Furthermore, upon a MakeCurrent() or BindFramebuffer() call, if the
278 * new FB is a window system FB, we need to re-update the FB's
279 * ColorDrawBuffer state to match the context. This is handled in
280 * _mesa_update_framebuffer().
282 * See the GL_EXT_framebuffer_object spec for more info.
284 static ALWAYS_INLINE
void
285 draw_buffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
286 GLenum buffer
, const char *caller
, bool no_error
)
290 FLUSH_VERTICES(ctx
, 0);
292 if (MESA_VERBOSE
& VERBOSE_API
) {
293 _mesa_debug(ctx
, "%s %s\n", caller
, _mesa_enum_to_string(buffer
));
296 if (buffer
== GL_NONE
) {
300 const GLbitfield supportedMask
301 = supported_buffer_bitmask(ctx
, fb
);
302 destMask
= draw_buffer_enum_to_bitmask(ctx
, buffer
);
303 if (!no_error
&& destMask
== BAD_MASK
) {
304 /* totally bogus buffer */
305 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid buffer %s)", caller
,
306 _mesa_enum_to_string(buffer
));
309 destMask
&= supportedMask
;
310 if (!no_error
&& destMask
== 0x0) {
311 /* none of the named color buffers exist! */
312 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid buffer %s)",
313 caller
, _mesa_enum_to_string(buffer
));
318 /* if we get here, there's no error so set new state */
319 const GLenum16 buffer16
= buffer
;
320 _mesa_drawbuffers(ctx
, fb
, 1, &buffer16
, &destMask
);
322 /* Call device driver function only if fb is the bound draw buffer */
323 if (fb
== ctx
->DrawBuffer
) {
324 if (ctx
->Driver
.DrawBuffer
)
325 ctx
->Driver
.DrawBuffer(ctx
);
326 if (ctx
->Driver
.DrawBufferAllocate
)
327 ctx
->Driver
.DrawBufferAllocate(ctx
);
333 draw_buffer_error(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
334 GLenum buffer
, const char *caller
)
336 draw_buffer(ctx
, fb
, buffer
, caller
, false);
341 draw_buffer_no_error(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
342 GLenum buffer
, const char *caller
)
344 draw_buffer(ctx
, fb
, buffer
, caller
, true);
349 _mesa_DrawBuffer_no_error(GLenum buffer
)
351 GET_CURRENT_CONTEXT(ctx
);
352 draw_buffer_no_error(ctx
, ctx
->DrawBuffer
, buffer
, "glDrawBuffer");
357 _mesa_DrawBuffer(GLenum buffer
)
359 GET_CURRENT_CONTEXT(ctx
);
360 draw_buffer_error(ctx
, ctx
->DrawBuffer
, buffer
, "glDrawBuffer");
365 _mesa_NamedFramebufferDrawBuffer_no_error(GLuint framebuffer
, GLenum buf
)
367 GET_CURRENT_CONTEXT(ctx
);
368 struct gl_framebuffer
*fb
;
371 fb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
373 fb
= ctx
->WinSysDrawBuffer
;
376 draw_buffer_no_error(ctx
, fb
, buf
, "glNamedFramebufferDrawBuffer");
381 _mesa_NamedFramebufferDrawBuffer(GLuint framebuffer
, GLenum buf
)
383 GET_CURRENT_CONTEXT(ctx
);
384 struct gl_framebuffer
*fb
;
387 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
388 "glNamedFramebufferDrawBuffer");
393 fb
= ctx
->WinSysDrawBuffer
;
395 draw_buffer_error(ctx
, fb
, buf
, "glNamedFramebufferDrawBuffer");
400 * Called by glDrawBuffersARB() and glNamedFramebufferDrawBuffers() to specify
401 * the destination color renderbuffers for N fragment program color outputs.
402 * \sa _mesa_DrawBuffer
403 * \param n number of outputs
404 * \param buffers array [n] of renderbuffer names. Unlike glDrawBuffer, the
405 * names cannot specify more than one buffer. For example,
406 * GL_FRONT_AND_BACK is illegal. The only exception is GL_BACK
407 * that is considered special and allowed as far as n is one
410 static ALWAYS_INLINE
void
411 draw_buffers(struct gl_context
*ctx
, struct gl_framebuffer
*fb
, GLsizei n
,
412 const GLenum
*buffers
, const char *caller
, bool no_error
)
415 GLbitfield usedBufferMask
, supportedMask
;
416 GLbitfield destMask
[MAX_DRAW_BUFFERS
];
418 FLUSH_VERTICES(ctx
, 0);
421 /* Turns out n==0 is a valid input that should not produce an error.
422 * The remaining code below correctly handles the n==0 case.
424 * From the OpenGL 3.0 specification, page 258:
425 * "An INVALID_VALUE error is generated if n is greater than
429 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n < 0)", caller
);
433 if (n
> (GLsizei
) ctx
->Const
.MaxDrawBuffers
) {
434 _mesa_error(ctx
, GL_INVALID_VALUE
,
435 "%s(n > maximum number of draw buffers)", caller
);
439 /* From the ES 3.0 specification, page 180:
440 * "If the GL is bound to the default framebuffer, then n must be 1
441 * and the constant must be BACK or NONE."
442 * (same restriction applies with GL_EXT_draw_buffers specification)
444 if (ctx
->API
== API_OPENGLES2
&& _mesa_is_winsys_fbo(fb
) &&
445 (n
!= 1 || (buffers
[0] != GL_NONE
&& buffers
[0] != GL_BACK
))) {
446 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid buffers)", caller
);
451 supportedMask
= supported_buffer_bitmask(ctx
, fb
);
452 usedBufferMask
= 0x0;
454 /* complicated error checking... */
455 for (output
= 0; output
< n
; output
++) {
457 /* From the OpenGL 4.5 specification, page 493 (page 515 of the PDF)
458 * "An INVALID_ENUM error is generated if any value in bufs is FRONT,
459 * LEFT, RIGHT, or FRONT_AND_BACK . This restriction applies to both
460 * the default framebuffer and framebuffer objects, and exists because
461 * these constants may themselves refer to multiple buffers, as shown
464 * From the OpenGL 4.5 specification, page 492 (page 514 of the PDF):
465 * "If the default framebuffer is affected, then each of the constants
466 * must be one of the values listed in table 17.6 or the special value
467 * BACK. When BACK is used, n must be 1 and color values are written
468 * into the left buffer for single-buffered contexts, or into the back
469 * left buffer for double-buffered contexts."
471 * Note "special value BACK". GL_BACK also refers to multiple buffers,
472 * but it is consider a special case here. This is a change on 4.5.
473 * For OpenGL 4.x we check that behaviour. For any previous version we
474 * keep considering it wrong (as INVALID_ENUM).
476 if (buffers
[output
] == GL_BACK
&&
477 _mesa_is_winsys_fbo(fb
) &&
478 _mesa_is_desktop_gl(ctx
) &&
479 ctx
->Version
>= 40) {
481 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(with GL_BACK n must be 1)",
485 } else if (buffers
[output
] == GL_FRONT
||
486 buffers
[output
] == GL_LEFT
||
487 buffers
[output
] == GL_RIGHT
||
488 buffers
[output
] == GL_FRONT_AND_BACK
||
489 (buffers
[output
] == GL_BACK
&&
490 _mesa_is_desktop_gl(ctx
))) {
491 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid buffer %s)",
492 caller
, _mesa_enum_to_string(buffers
[output
]));
497 destMask
[output
] = draw_buffer_enum_to_bitmask(ctx
, buffers
[output
]);
500 /* From the OpenGL 3.0 specification, page 258:
501 * "Each buffer listed in bufs must be one of the values from tables
502 * 4.5 or 4.6. Otherwise, an INVALID_ENUM error is generated.
504 if (destMask
[output
] == BAD_MASK
) {
505 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid buffer %s)",
506 caller
, _mesa_enum_to_string(buffers
[output
]));
510 /* Section 4.2 (Whole Framebuffer Operations) of the OpenGL ES 3.0
511 * specification says:
513 * "If the GL is bound to a draw framebuffer object, the ith
514 * buffer listed in bufs must be COLOR_ATTACHMENTi or NONE .
515 * Specifying a buffer out of order, BACK , or COLOR_ATTACHMENTm
516 * where m is greater than or equal to the value of MAX_-
517 * COLOR_ATTACHMENTS , will generate the error INVALID_OPERATION .
519 if (_mesa_is_gles3(ctx
) && _mesa_is_user_fbo(fb
) &&
520 buffers
[output
] != GL_NONE
&&
521 (buffers
[output
] < GL_COLOR_ATTACHMENT0
||
522 buffers
[output
] >= GL_COLOR_ATTACHMENT0
+ ctx
->Const
.MaxColorAttachments
)) {
523 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawBuffers(buffer)");
528 if (buffers
[output
] == GL_NONE
) {
529 destMask
[output
] = 0x0;
532 /* Page 259 (page 275 of the PDF) in section 4.2.1 of the OpenGL 3.0
533 * spec (20080923) says:
535 * "If the GL is bound to a framebuffer object and DrawBuffers is
536 * supplied with [...] COLOR_ATTACHMENTm where m is greater than
537 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
538 * INVALID_OPERATION results."
540 if (!no_error
&& _mesa_is_user_fbo(fb
) && buffers
[output
] >=
541 GL_COLOR_ATTACHMENT0
+ ctx
->Const
.MaxDrawBuffers
) {
542 _mesa_error(ctx
, GL_INVALID_OPERATION
,
543 "%s(buffers[%d] >= maximum number of draw buffers)",
548 /* From the OpenGL 3.0 specification, page 259:
549 * "If the GL is bound to the default framebuffer and DrawBuffers is
550 * supplied with a constant (other than NONE) that does not indicate
551 * any of the color buffers allocated to the GL context by the window
552 * system, the error INVALID_OPERATION will be generated.
554 * If the GL is bound to a framebuffer object and DrawBuffers is
555 * supplied with a constant from table 4.6 [...] then the error
556 * INVALID_OPERATION results."
558 destMask
[output
] &= supportedMask
;
560 if (destMask
[output
] == 0) {
561 _mesa_error(ctx
, GL_INVALID_OPERATION
,
562 "%s(unsupported buffer %s)",
563 caller
, _mesa_enum_to_string(buffers
[output
]));
567 /* ES 3.0 is even more restrictive. From the ES 3.0 spec, page 180:
568 * "If the GL is bound to a framebuffer object, the ith buffer
569 * listed in bufs must be COLOR_ATTACHMENTi or NONE. [...]
570 * INVALID_OPERATION." (same restriction applies with
571 * GL_EXT_draw_buffers specification)
573 if (ctx
->API
== API_OPENGLES2
&& _mesa_is_user_fbo(fb
) &&
574 buffers
[output
] != GL_NONE
&&
575 buffers
[output
] != GL_COLOR_ATTACHMENT0
+ output
) {
576 _mesa_error(ctx
, GL_INVALID_OPERATION
,
577 "%s(unsupported buffer %s)",
578 caller
, _mesa_enum_to_string(buffers
[output
]));
582 /* From the OpenGL 3.0 specification, page 258:
583 * "Except for NONE, a buffer may not appear more than once in the
584 * array pointed to by bufs. Specifying a buffer more then once
585 * will result in the error INVALID_OPERATION."
587 if (destMask
[output
] & usedBufferMask
) {
588 _mesa_error(ctx
, GL_INVALID_OPERATION
,
589 "%s(duplicated buffer %s)",
590 caller
, _mesa_enum_to_string(buffers
[output
]));
596 usedBufferMask
|= destMask
[output
];
600 /* OK, if we get here, there were no errors so set the new state */
601 GLenum16 buffers16
[MAX_DRAW_BUFFERS
];
602 for (int i
= 0; i
< n
; i
++)
603 buffers16
[i
] = buffers
[i
];
605 _mesa_drawbuffers(ctx
, fb
, n
, buffers16
, destMask
);
608 * Call device driver function if fb is the bound draw buffer.
609 * Note that n can be equal to 0,
610 * in which case we don't want to reference buffers[0], which
613 if (fb
== ctx
->DrawBuffer
) {
614 if (ctx
->Driver
.DrawBuffer
)
615 ctx
->Driver
.DrawBuffer(ctx
);
616 if (ctx
->Driver
.DrawBufferAllocate
)
617 ctx
->Driver
.DrawBufferAllocate(ctx
);
623 draw_buffers_error(struct gl_context
*ctx
, struct gl_framebuffer
*fb
, GLsizei n
,
624 const GLenum
*buffers
, const char *caller
)
626 draw_buffers(ctx
, fb
, n
, buffers
, caller
, false);
631 draw_buffers_no_error(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
632 GLsizei n
, const GLenum
*buffers
, const char *caller
)
634 draw_buffers(ctx
, fb
, n
, buffers
, caller
, true);
639 _mesa_DrawBuffers_no_error(GLsizei n
, const GLenum
*buffers
)
641 GET_CURRENT_CONTEXT(ctx
);
642 draw_buffers_no_error(ctx
, ctx
->DrawBuffer
, n
, buffers
, "glDrawBuffers");
647 _mesa_DrawBuffers(GLsizei n
, const GLenum
*buffers
)
649 GET_CURRENT_CONTEXT(ctx
);
650 draw_buffers_error(ctx
, ctx
->DrawBuffer
, n
, buffers
, "glDrawBuffers");
655 _mesa_NamedFramebufferDrawBuffers_no_error(GLuint framebuffer
, GLsizei n
,
658 GET_CURRENT_CONTEXT(ctx
);
659 struct gl_framebuffer
*fb
;
662 fb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
664 fb
= ctx
->WinSysDrawBuffer
;
667 draw_buffers_no_error(ctx
, fb
, n
, bufs
, "glNamedFramebufferDrawBuffers");
672 _mesa_NamedFramebufferDrawBuffers(GLuint framebuffer
, GLsizei n
,
675 GET_CURRENT_CONTEXT(ctx
);
676 struct gl_framebuffer
*fb
;
679 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
680 "glNamedFramebufferDrawBuffers");
685 fb
= ctx
->WinSysDrawBuffer
;
687 draw_buffers_error(ctx
, fb
, n
, bufs
, "glNamedFramebufferDrawBuffers");
692 * Performs necessary state updates when _mesa_drawbuffers makes an
696 updated_drawbuffers(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
698 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
700 if (ctx
->API
== API_OPENGL_COMPAT
&& !ctx
->Extensions
.ARB_ES2_compatibility
) {
701 /* Flag the FBO as requiring validation. */
702 if (_mesa_is_user_fbo(fb
)) {
710 * Helper function to set the GL_DRAW_BUFFER state for the given context and
711 * FBO. Called via glDrawBuffer(), glDrawBuffersARB()
713 * All error checking will have been done prior to calling this function
714 * so nothing should go wrong at this point.
716 * \param ctx current context
717 * \param fb the desired draw buffer
718 * \param n number of color outputs to set
719 * \param buffers array[n] of colorbuffer names, like GL_LEFT.
720 * \param destMask array[n] of BUFFER_BIT_* bitmasks which correspond to the
721 * colorbuffer names. (i.e. GL_FRONT_AND_BACK =>
722 * BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT).
725 _mesa_drawbuffers(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
726 GLuint n
, const GLenum16
*buffers
,
727 const GLbitfield
*destMask
)
729 GLbitfield mask
[MAX_DRAW_BUFFERS
];
733 /* compute destMask values now */
734 const GLbitfield supportedMask
= supported_buffer_bitmask(ctx
, fb
);
736 for (output
= 0; output
< n
; output
++) {
737 mask
[output
] = draw_buffer_enum_to_bitmask(ctx
, buffers
[output
]);
738 assert(mask
[output
] != BAD_MASK
);
739 mask
[output
] &= supportedMask
;
745 * destMask[0] may have up to four bits set
746 * (ex: glDrawBuffer(GL_FRONT_AND_BACK)).
747 * Otherwise, destMask[x] can only have one bit set.
749 if (n
> 0 && util_bitcount(destMask
[0]) > 1) {
750 GLuint count
= 0, destMask0
= destMask
[0];
752 const gl_buffer_index bufIndex
= u_bit_scan(&destMask0
);
753 if (fb
->_ColorDrawBufferIndexes
[count
] != bufIndex
) {
754 updated_drawbuffers(ctx
, fb
);
755 fb
->_ColorDrawBufferIndexes
[count
] = bufIndex
;
759 fb
->ColorDrawBuffer
[0] = buffers
[0];
760 fb
->_NumColorDrawBuffers
= count
;
764 for (buf
= 0; buf
< n
; buf
++ ) {
766 gl_buffer_index bufIndex
= ffs(destMask
[buf
]) - 1;
767 /* only one bit should be set in the destMask[buf] field */
768 assert(util_bitcount(destMask
[buf
]) == 1);
769 if (fb
->_ColorDrawBufferIndexes
[buf
] != bufIndex
) {
770 updated_drawbuffers(ctx
, fb
);
771 fb
->_ColorDrawBufferIndexes
[buf
] = bufIndex
;
776 if (fb
->_ColorDrawBufferIndexes
[buf
] != BUFFER_NONE
) {
777 updated_drawbuffers(ctx
, fb
);
778 fb
->_ColorDrawBufferIndexes
[buf
] = BUFFER_NONE
;
781 fb
->ColorDrawBuffer
[buf
] = buffers
[buf
];
783 fb
->_NumColorDrawBuffers
= count
;
786 /* set remaining outputs to BUFFER_NONE */
787 for (buf
= fb
->_NumColorDrawBuffers
; buf
< ctx
->Const
.MaxDrawBuffers
; buf
++) {
788 if (fb
->_ColorDrawBufferIndexes
[buf
] != BUFFER_NONE
) {
789 updated_drawbuffers(ctx
, fb
);
790 fb
->_ColorDrawBufferIndexes
[buf
] = BUFFER_NONE
;
793 for (buf
= n
; buf
< ctx
->Const
.MaxDrawBuffers
; buf
++) {
794 fb
->ColorDrawBuffer
[buf
] = GL_NONE
;
797 if (_mesa_is_winsys_fbo(fb
)) {
798 /* also set context drawbuffer state */
799 for (buf
= 0; buf
< ctx
->Const
.MaxDrawBuffers
; buf
++) {
800 if (ctx
->Color
.DrawBuffer
[buf
] != fb
->ColorDrawBuffer
[buf
]) {
801 updated_drawbuffers(ctx
, fb
);
802 ctx
->Color
.DrawBuffer
[buf
] = fb
->ColorDrawBuffer
[buf
];
810 * Update the current drawbuffer's _ColorDrawBufferIndex[] list, etc.
811 * from the context's Color.DrawBuffer[] state.
812 * Use when changing contexts.
815 _mesa_update_draw_buffers(struct gl_context
*ctx
)
817 /* should be a window system FBO */
818 assert(_mesa_is_winsys_fbo(ctx
->DrawBuffer
));
820 _mesa_drawbuffers(ctx
, ctx
->DrawBuffer
, ctx
->Const
.MaxDrawBuffers
,
821 ctx
->Color
.DrawBuffer
, NULL
);
826 * Like \sa _mesa_drawbuffers(), this is a helper function for setting
827 * GL_READ_BUFFER state for the given context and FBO.
828 * Note that all error checking should have been done before calling
830 * \param ctx the rendering context
831 * \param fb the framebuffer object to update
832 * \param buffer GL_FRONT, GL_BACK, GL_COLOR_ATTACHMENT0, etc.
833 * \param bufferIndex the numerical index corresponding to 'buffer'
836 _mesa_readbuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
837 GLenum buffer
, gl_buffer_index bufferIndex
)
839 if ((fb
== ctx
->ReadBuffer
) && _mesa_is_winsys_fbo(fb
)) {
840 /* Only update the per-context READ_BUFFER state if we're bound to
841 * a window-system framebuffer.
843 ctx
->Pixel
.ReadBuffer
= buffer
;
846 fb
->ColorReadBuffer
= buffer
;
847 fb
->_ColorReadBufferIndex
= bufferIndex
;
849 ctx
->NewState
|= _NEW_BUFFERS
;
855 * Called by glReadBuffer and glNamedFramebufferReadBuffer to set the source
856 * renderbuffer for reading pixels.
857 * \param mode color buffer such as GL_FRONT, GL_BACK, etc.
859 static ALWAYS_INLINE
void
860 read_buffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
861 GLenum buffer
, const char *caller
, bool no_error
)
863 gl_buffer_index srcBuffer
;
865 FLUSH_VERTICES(ctx
, 0);
867 if (MESA_VERBOSE
& VERBOSE_API
)
868 _mesa_debug(ctx
, "%s %s\n", caller
, _mesa_enum_to_string(buffer
));
870 if (buffer
== GL_NONE
) {
871 /* This is legal--it means that no buffer should be bound for reading. */
872 srcBuffer
= BUFFER_NONE
;
875 /* general case / window-system framebuffer */
876 if (!no_error
&&_mesa_is_gles3(ctx
) &&
877 !is_legal_es3_readbuffer_enum(buffer
))
878 srcBuffer
= BUFFER_NONE
;
880 srcBuffer
= read_buffer_enum_to_index(ctx
, buffer
);
883 GLbitfield supportedMask
;
885 if (srcBuffer
== BUFFER_NONE
) {
886 _mesa_error(ctx
, GL_INVALID_ENUM
,
887 "%s(invalid buffer %s)", caller
,
888 _mesa_enum_to_string(buffer
));
892 supportedMask
= supported_buffer_bitmask(ctx
, fb
);
893 if (((1 << srcBuffer
) & supportedMask
) == 0) {
894 _mesa_error(ctx
, GL_INVALID_OPERATION
,
895 "%s(invalid buffer %s)", caller
,
896 _mesa_enum_to_string(buffer
));
902 /* OK, all error checking has been completed now */
904 _mesa_readbuffer(ctx
, fb
, buffer
, srcBuffer
);
906 /* Call the device driver function only if fb is the bound read buffer */
907 if (fb
== ctx
->ReadBuffer
) {
908 if (ctx
->Driver
.ReadBuffer
)
909 ctx
->Driver
.ReadBuffer(ctx
, buffer
);
915 read_buffer_err(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
916 GLenum buffer
, const char *caller
)
918 read_buffer(ctx
, fb
, buffer
, caller
, false);
923 read_buffer_no_error(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
924 GLenum buffer
, const char *caller
)
926 read_buffer(ctx
, fb
, buffer
, caller
, true);
931 _mesa_ReadBuffer_no_error(GLenum buffer
)
933 GET_CURRENT_CONTEXT(ctx
);
934 read_buffer_no_error(ctx
, ctx
->ReadBuffer
, buffer
, "glReadBuffer");
939 _mesa_ReadBuffer(GLenum buffer
)
941 GET_CURRENT_CONTEXT(ctx
);
942 read_buffer_err(ctx
, ctx
->ReadBuffer
, buffer
, "glReadBuffer");
947 _mesa_NamedFramebufferReadBuffer_no_error(GLuint framebuffer
, GLenum src
)
949 GET_CURRENT_CONTEXT(ctx
);
951 struct gl_framebuffer
*fb
;
954 fb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
956 fb
= ctx
->WinSysReadBuffer
;
959 read_buffer_no_error(ctx
, fb
, src
, "glNamedFramebufferReadBuffer");
964 _mesa_NamedFramebufferReadBuffer(GLuint framebuffer
, GLenum src
)
966 GET_CURRENT_CONTEXT(ctx
);
967 struct gl_framebuffer
*fb
;
970 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
971 "glNamedFramebufferReadBuffer");
976 fb
= ctx
->WinSysReadBuffer
;
978 read_buffer_err(ctx
, fb
, src
, "glNamedFramebufferReadBuffer");