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
)
100 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_FRONT_RIGHT
;
102 if (_mesa_is_gles(ctx
)) {
103 /* Page 181 (page 192 of the PDF) in section 4.2.1 of the OpenGL
104 * ES 3.0.1 specification says:
106 * "When draw buffer zero is BACK, color values are written
107 * into the sole buffer for single-buffered contexts, or into
108 * the back buffer for double-buffered contexts."
110 * Since there is no stereo rendering in ES 3.0, only return the
111 * LEFT bits. This also satisfies the "n must be 1" requirement.
113 * We also do this for GLES 1 and 2 because those APIs have no
114 * concept of selecting the front and back buffer anyway and it's
115 * convenient to be able to maintain the magic behaviour of
116 * GL_BACK in that case.
118 if (ctx
->DrawBuffer
->Visual
.doubleBufferMode
)
119 return BUFFER_BIT_BACK_LEFT
;
120 return BUFFER_BIT_FRONT_LEFT
;
122 return BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_BACK_RIGHT
;
124 return BUFFER_BIT_FRONT_RIGHT
| BUFFER_BIT_BACK_RIGHT
;
126 return BUFFER_BIT_FRONT_RIGHT
;
128 return BUFFER_BIT_BACK_RIGHT
;
130 return BUFFER_BIT_BACK_LEFT
;
131 case GL_FRONT_AND_BACK
:
132 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_BACK_LEFT
133 | BUFFER_BIT_FRONT_RIGHT
| BUFFER_BIT_BACK_RIGHT
;
135 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_BACK_LEFT
;
137 return BUFFER_BIT_FRONT_LEFT
;
139 return BUFFER_BIT_AUX0
;
143 return 1 << BUFFER_COUNT
; /* invalid, but not BAD_MASK */
144 case GL_COLOR_ATTACHMENT0_EXT
:
145 return BUFFER_BIT_COLOR0
;
146 case GL_COLOR_ATTACHMENT1_EXT
:
147 return BUFFER_BIT_COLOR1
;
148 case GL_COLOR_ATTACHMENT2_EXT
:
149 return BUFFER_BIT_COLOR2
;
150 case GL_COLOR_ATTACHMENT3_EXT
:
151 return BUFFER_BIT_COLOR3
;
152 case GL_COLOR_ATTACHMENT4_EXT
:
153 return BUFFER_BIT_COLOR4
;
154 case GL_COLOR_ATTACHMENT5_EXT
:
155 return BUFFER_BIT_COLOR5
;
156 case GL_COLOR_ATTACHMENT6_EXT
:
157 return BUFFER_BIT_COLOR6
;
158 case GL_COLOR_ATTACHMENT7_EXT
:
159 return BUFFER_BIT_COLOR7
;
161 /* not an error, but also not supported */
162 if (buffer
>= GL_COLOR_ATTACHMENT8
&& buffer
<= GL_COLOR_ATTACHMENT31
)
163 return 1 << BUFFER_COUNT
;
171 * Helper routine used by glReadBuffer.
172 * Given a GLenum naming a color buffer, return the index of the corresponding
173 * renderbuffer (a BUFFER_* value).
174 * return BUFFER_NONE for an invalid buffer.
176 static gl_buffer_index
177 read_buffer_enum_to_index(const struct gl_context
*ctx
, GLenum buffer
)
181 return BUFFER_FRONT_LEFT
;
183 if (_mesa_is_gles(ctx
)) {
184 /* In draw_buffer_enum_to_bitmask, when GLES contexts draw to
185 * GL_BACK with a single-buffered configuration, we actually end
186 * up drawing to the sole front buffer in our internal
187 * representation. For consistency, we must read from that
188 * front left buffer too.
190 if (!ctx
->DrawBuffer
->Visual
.doubleBufferMode
)
191 return BUFFER_FRONT_LEFT
;
193 return BUFFER_BACK_LEFT
;
195 return BUFFER_FRONT_RIGHT
;
197 return BUFFER_FRONT_RIGHT
;
199 return BUFFER_BACK_RIGHT
;
201 return BUFFER_BACK_LEFT
;
203 return BUFFER_FRONT_LEFT
;
205 return BUFFER_FRONT_LEFT
;
208 case GL_FRONT_AND_BACK
:
209 return BUFFER_FRONT_LEFT
;
213 return BUFFER_COUNT
; /* invalid, but not -1 */
214 case GL_COLOR_ATTACHMENT0_EXT
:
215 return BUFFER_COLOR0
;
216 case GL_COLOR_ATTACHMENT1_EXT
:
217 return BUFFER_COLOR1
;
218 case GL_COLOR_ATTACHMENT2_EXT
:
219 return BUFFER_COLOR2
;
220 case GL_COLOR_ATTACHMENT3_EXT
:
221 return BUFFER_COLOR3
;
222 case GL_COLOR_ATTACHMENT4_EXT
:
223 return BUFFER_COLOR4
;
224 case GL_COLOR_ATTACHMENT5_EXT
:
225 return BUFFER_COLOR5
;
226 case GL_COLOR_ATTACHMENT6_EXT
:
227 return BUFFER_COLOR6
;
228 case GL_COLOR_ATTACHMENT7_EXT
:
229 return BUFFER_COLOR7
;
231 /* not an error, but also not supported */
232 if (buffer
>= GL_COLOR_ATTACHMENT8
&& buffer
<= GL_COLOR_ATTACHMENT31
)
240 is_legal_es3_readbuffer_enum(GLenum buf
)
242 return buf
== GL_BACK
|| buf
== GL_NONE
||
243 (buf
>= GL_COLOR_ATTACHMENT0
&& buf
<= GL_COLOR_ATTACHMENT31
);
247 * Called by glDrawBuffer() and glNamedFramebufferDrawBuffer().
248 * Specify which renderbuffer(s) to draw into for the first color output.
249 * <buffer> can name zero, one, two or four renderbuffers!
250 * \sa _mesa_DrawBuffers
252 * \param buffer buffer token such as GL_LEFT or GL_FRONT_AND_BACK, etc.
254 * Note that the behaviour of this function depends on whether the
255 * current ctx->DrawBuffer is a window-system framebuffer or a user-created
256 * framebuffer object.
257 * In the former case, we update the per-context ctx->Color.DrawBuffer
258 * state var _and_ the FB's ColorDrawBuffer state.
259 * In the later case, we update the FB's ColorDrawBuffer state only.
261 * Furthermore, upon a MakeCurrent() or BindFramebuffer() call, if the
262 * new FB is a window system FB, we need to re-update the FB's
263 * ColorDrawBuffer state to match the context. This is handled in
264 * _mesa_update_framebuffer().
266 * See the GL_EXT_framebuffer_object spec for more info.
268 static ALWAYS_INLINE
void
269 draw_buffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
270 GLenum buffer
, const char *caller
, bool no_error
)
274 FLUSH_VERTICES(ctx
, 0);
276 if (MESA_VERBOSE
& VERBOSE_API
) {
277 _mesa_debug(ctx
, "%s %s\n", caller
, _mesa_enum_to_string(buffer
));
280 if (buffer
== GL_NONE
) {
284 const GLbitfield supportedMask
285 = supported_buffer_bitmask(ctx
, fb
);
286 destMask
= draw_buffer_enum_to_bitmask(ctx
, buffer
);
287 if (!no_error
&& destMask
== BAD_MASK
) {
288 /* totally bogus buffer */
289 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid buffer %s)", caller
,
290 _mesa_enum_to_string(buffer
));
293 destMask
&= supportedMask
;
294 if (!no_error
&& destMask
== 0x0) {
295 /* none of the named color buffers exist! */
296 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid buffer %s)",
297 caller
, _mesa_enum_to_string(buffer
));
302 /* if we get here, there's no error so set new state */
303 const GLenum16 buffer16
= buffer
;
304 _mesa_drawbuffers(ctx
, fb
, 1, &buffer16
, &destMask
);
306 /* Call device driver function only if fb is the bound draw buffer */
307 if (fb
== ctx
->DrawBuffer
) {
308 if (ctx
->Driver
.DrawBuffer
)
309 ctx
->Driver
.DrawBuffer(ctx
);
310 if (ctx
->Driver
.DrawBufferAllocate
)
311 ctx
->Driver
.DrawBufferAllocate(ctx
);
317 draw_buffer_error(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
318 GLenum buffer
, const char *caller
)
320 draw_buffer(ctx
, fb
, buffer
, caller
, false);
325 draw_buffer_no_error(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
326 GLenum buffer
, const char *caller
)
328 draw_buffer(ctx
, fb
, buffer
, caller
, true);
333 _mesa_DrawBuffer_no_error(GLenum buffer
)
335 GET_CURRENT_CONTEXT(ctx
);
336 draw_buffer_no_error(ctx
, ctx
->DrawBuffer
, buffer
, "glDrawBuffer");
341 _mesa_DrawBuffer(GLenum buffer
)
343 GET_CURRENT_CONTEXT(ctx
);
344 draw_buffer_error(ctx
, ctx
->DrawBuffer
, buffer
, "glDrawBuffer");
349 _mesa_NamedFramebufferDrawBuffer_no_error(GLuint framebuffer
, GLenum buf
)
351 GET_CURRENT_CONTEXT(ctx
);
352 struct gl_framebuffer
*fb
;
355 fb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
357 fb
= ctx
->WinSysDrawBuffer
;
360 draw_buffer_no_error(ctx
, fb
, buf
, "glNamedFramebufferDrawBuffer");
365 _mesa_NamedFramebufferDrawBuffer(GLuint framebuffer
, GLenum buf
)
367 GET_CURRENT_CONTEXT(ctx
);
368 struct gl_framebuffer
*fb
;
371 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
372 "glNamedFramebufferDrawBuffer");
377 fb
= ctx
->WinSysDrawBuffer
;
379 draw_buffer_error(ctx
, fb
, buf
, "glNamedFramebufferDrawBuffer");
384 * Called by glDrawBuffersARB() and glNamedFramebufferDrawBuffers() to specify
385 * the destination color renderbuffers for N fragment program color outputs.
386 * \sa _mesa_DrawBuffer
387 * \param n number of outputs
388 * \param buffers array [n] of renderbuffer names. Unlike glDrawBuffer, the
389 * names cannot specify more than one buffer. For example,
390 * GL_FRONT_AND_BACK is illegal. The only exception is GL_BACK
391 * that is considered special and allowed as far as n is one
394 static ALWAYS_INLINE
void
395 draw_buffers(struct gl_context
*ctx
, struct gl_framebuffer
*fb
, GLsizei n
,
396 const GLenum
*buffers
, const char *caller
, bool no_error
)
399 GLbitfield usedBufferMask
, supportedMask
;
400 GLbitfield destMask
[MAX_DRAW_BUFFERS
];
402 FLUSH_VERTICES(ctx
, 0);
405 /* Turns out n==0 is a valid input that should not produce an error.
406 * The remaining code below correctly handles the n==0 case.
408 * From the OpenGL 3.0 specification, page 258:
409 * "An INVALID_VALUE error is generated if n is greater than
413 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n < 0)", caller
);
417 if (n
> (GLsizei
) ctx
->Const
.MaxDrawBuffers
) {
418 _mesa_error(ctx
, GL_INVALID_VALUE
,
419 "%s(n > maximum number of draw buffers)", caller
);
423 /* From the ES 3.0 specification, page 180:
424 * "If the GL is bound to the default framebuffer, then n must be 1
425 * and the constant must be BACK or NONE."
426 * (same restriction applies with GL_EXT_draw_buffers specification)
428 if (ctx
->API
== API_OPENGLES2
&& _mesa_is_winsys_fbo(fb
) &&
429 (n
!= 1 || (buffers
[0] != GL_NONE
&& buffers
[0] != GL_BACK
))) {
430 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid buffers)", caller
);
435 supportedMask
= supported_buffer_bitmask(ctx
, fb
);
436 usedBufferMask
= 0x0;
438 /* complicated error checking... */
439 for (output
= 0; output
< n
; output
++) {
441 /* From the OpenGL 4.5 specification, page 493 (page 515 of the PDF)
442 * "An INVALID_ENUM error is generated if any value in bufs is FRONT,
443 * LEFT, RIGHT, or FRONT_AND_BACK . This restriction applies to both
444 * the default framebuffer and framebuffer objects, and exists because
445 * these constants may themselves refer to multiple buffers, as shown
448 * From the OpenGL 4.5 specification, page 492 (page 514 of the PDF):
449 * "If the default framebuffer is affected, then each of the constants
450 * must be one of the values listed in table 17.6 or the special value
451 * BACK. When BACK is used, n must be 1 and color values are written
452 * into the left buffer for single-buffered contexts, or into the back
453 * left buffer for double-buffered contexts."
455 * Note "special value BACK". GL_BACK also refers to multiple buffers,
456 * but it is consider a special case here. This is a change on 4.5.
457 * For OpenGL 4.x we check that behaviour. For any previous version we
458 * keep considering it wrong (as INVALID_ENUM).
460 if (buffers
[output
] == GL_BACK
&&
461 _mesa_is_winsys_fbo(fb
) &&
462 _mesa_is_desktop_gl(ctx
) &&
463 ctx
->Version
>= 40) {
465 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(with GL_BACK n must be 1)",
469 } else if (buffers
[output
] == GL_FRONT
||
470 buffers
[output
] == GL_LEFT
||
471 buffers
[output
] == GL_RIGHT
||
472 buffers
[output
] == GL_FRONT_AND_BACK
||
473 (buffers
[output
] == GL_BACK
&&
474 _mesa_is_desktop_gl(ctx
))) {
475 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid buffer %s)",
476 caller
, _mesa_enum_to_string(buffers
[output
]));
481 destMask
[output
] = draw_buffer_enum_to_bitmask(ctx
, buffers
[output
]);
484 /* From the OpenGL 3.0 specification, page 258:
485 * "Each buffer listed in bufs must be one of the values from tables
486 * 4.5 or 4.6. Otherwise, an INVALID_ENUM error is generated.
488 if (destMask
[output
] == BAD_MASK
) {
489 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid buffer %s)",
490 caller
, _mesa_enum_to_string(buffers
[output
]));
494 /* Section 4.2 (Whole Framebuffer Operations) of the OpenGL ES 3.0
495 * specification says:
497 * "If the GL is bound to a draw framebuffer object, the ith
498 * buffer listed in bufs must be COLOR_ATTACHMENTi or NONE .
499 * Specifying a buffer out of order, BACK , or COLOR_ATTACHMENTm
500 * where m is greater than or equal to the value of MAX_-
501 * COLOR_ATTACHMENTS , will generate the error INVALID_OPERATION .
503 if (_mesa_is_gles3(ctx
) && _mesa_is_user_fbo(fb
) &&
504 buffers
[output
] != GL_NONE
&&
505 (buffers
[output
] < GL_COLOR_ATTACHMENT0
||
506 buffers
[output
] >= GL_COLOR_ATTACHMENT0
+ ctx
->Const
.MaxColorAttachments
)) {
507 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawBuffers(buffer)");
512 if (buffers
[output
] == GL_NONE
) {
513 destMask
[output
] = 0x0;
516 /* Page 259 (page 275 of the PDF) in section 4.2.1 of the OpenGL 3.0
517 * spec (20080923) says:
519 * "If the GL is bound to a framebuffer object and DrawBuffers is
520 * supplied with [...] COLOR_ATTACHMENTm where m is greater than
521 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
522 * INVALID_OPERATION results."
524 if (!no_error
&& _mesa_is_user_fbo(fb
) && buffers
[output
] >=
525 GL_COLOR_ATTACHMENT0
+ ctx
->Const
.MaxDrawBuffers
) {
526 _mesa_error(ctx
, GL_INVALID_OPERATION
,
527 "%s(buffers[%d] >= maximum number of draw buffers)",
532 /* From the OpenGL 3.0 specification, page 259:
533 * "If the GL is bound to the default framebuffer and DrawBuffers is
534 * supplied with a constant (other than NONE) that does not indicate
535 * any of the color buffers allocated to the GL context by the window
536 * system, the error INVALID_OPERATION will be generated.
538 * If the GL is bound to a framebuffer object and DrawBuffers is
539 * supplied with a constant from table 4.6 [...] then the error
540 * INVALID_OPERATION results."
542 destMask
[output
] &= supportedMask
;
544 if (destMask
[output
] == 0) {
545 _mesa_error(ctx
, GL_INVALID_OPERATION
,
546 "%s(unsupported buffer %s)",
547 caller
, _mesa_enum_to_string(buffers
[output
]));
551 /* ES 3.0 is even more restrictive. From the ES 3.0 spec, page 180:
552 * "If the GL is bound to a framebuffer object, the ith buffer
553 * listed in bufs must be COLOR_ATTACHMENTi or NONE. [...]
554 * INVALID_OPERATION." (same restriction applies with
555 * GL_EXT_draw_buffers specification)
557 if (ctx
->API
== API_OPENGLES2
&& _mesa_is_user_fbo(fb
) &&
558 buffers
[output
] != GL_NONE
&&
559 buffers
[output
] != GL_COLOR_ATTACHMENT0
+ output
) {
560 _mesa_error(ctx
, GL_INVALID_OPERATION
,
561 "%s(unsupported buffer %s)",
562 caller
, _mesa_enum_to_string(buffers
[output
]));
566 /* From the OpenGL 3.0 specification, page 258:
567 * "Except for NONE, a buffer may not appear more than once in the
568 * array pointed to by bufs. Specifying a buffer more then once
569 * will result in the error INVALID_OPERATION."
571 if (destMask
[output
] & usedBufferMask
) {
572 _mesa_error(ctx
, GL_INVALID_OPERATION
,
573 "%s(duplicated buffer %s)",
574 caller
, _mesa_enum_to_string(buffers
[output
]));
580 usedBufferMask
|= destMask
[output
];
584 /* OK, if we get here, there were no errors so set the new state */
585 GLenum16 buffers16
[MAX_DRAW_BUFFERS
];
586 for (int i
= 0; i
< n
; i
++)
587 buffers16
[i
] = buffers
[i
];
589 _mesa_drawbuffers(ctx
, fb
, n
, buffers16
, destMask
);
592 * Call device driver function if fb is the bound draw buffer.
593 * Note that n can be equal to 0,
594 * in which case we don't want to reference buffers[0], which
597 if (fb
== ctx
->DrawBuffer
) {
598 if (ctx
->Driver
.DrawBuffer
)
599 ctx
->Driver
.DrawBuffer(ctx
);
600 if (ctx
->Driver
.DrawBufferAllocate
)
601 ctx
->Driver
.DrawBufferAllocate(ctx
);
607 draw_buffers_error(struct gl_context
*ctx
, struct gl_framebuffer
*fb
, GLsizei n
,
608 const GLenum
*buffers
, const char *caller
)
610 draw_buffers(ctx
, fb
, n
, buffers
, caller
, false);
615 draw_buffers_no_error(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
616 GLsizei n
, const GLenum
*buffers
, const char *caller
)
618 draw_buffers(ctx
, fb
, n
, buffers
, caller
, true);
623 _mesa_DrawBuffers_no_error(GLsizei n
, const GLenum
*buffers
)
625 GET_CURRENT_CONTEXT(ctx
);
626 draw_buffers_no_error(ctx
, ctx
->DrawBuffer
, n
, buffers
, "glDrawBuffers");
631 _mesa_DrawBuffers(GLsizei n
, const GLenum
*buffers
)
633 GET_CURRENT_CONTEXT(ctx
);
634 draw_buffers_error(ctx
, ctx
->DrawBuffer
, n
, buffers
, "glDrawBuffers");
639 _mesa_NamedFramebufferDrawBuffers_no_error(GLuint framebuffer
, GLsizei n
,
642 GET_CURRENT_CONTEXT(ctx
);
643 struct gl_framebuffer
*fb
;
646 fb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
648 fb
= ctx
->WinSysDrawBuffer
;
651 draw_buffers_no_error(ctx
, fb
, n
, bufs
, "glNamedFramebufferDrawBuffers");
656 _mesa_NamedFramebufferDrawBuffers(GLuint framebuffer
, GLsizei n
,
659 GET_CURRENT_CONTEXT(ctx
);
660 struct gl_framebuffer
*fb
;
663 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
664 "glNamedFramebufferDrawBuffers");
669 fb
= ctx
->WinSysDrawBuffer
;
671 draw_buffers_error(ctx
, fb
, n
, bufs
, "glNamedFramebufferDrawBuffers");
676 * Performs necessary state updates when _mesa_drawbuffers makes an
680 updated_drawbuffers(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
682 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
684 if (ctx
->API
== API_OPENGL_COMPAT
&& !ctx
->Extensions
.ARB_ES2_compatibility
) {
685 /* Flag the FBO as requiring validation. */
686 if (_mesa_is_user_fbo(fb
)) {
694 * Helper function to set the GL_DRAW_BUFFER state for the given context and
695 * FBO. Called via glDrawBuffer(), glDrawBuffersARB()
697 * All error checking will have been done prior to calling this function
698 * so nothing should go wrong at this point.
700 * \param ctx current context
701 * \param fb the desired draw buffer
702 * \param n number of color outputs to set
703 * \param buffers array[n] of colorbuffer names, like GL_LEFT.
704 * \param destMask array[n] of BUFFER_BIT_* bitmasks which correspond to the
705 * colorbuffer names. (i.e. GL_FRONT_AND_BACK =>
706 * BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT).
709 _mesa_drawbuffers(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
710 GLuint n
, const GLenum16
*buffers
,
711 const GLbitfield
*destMask
)
713 GLbitfield mask
[MAX_DRAW_BUFFERS
];
717 /* compute destMask values now */
718 const GLbitfield supportedMask
= supported_buffer_bitmask(ctx
, fb
);
720 for (output
= 0; output
< n
; output
++) {
721 mask
[output
] = draw_buffer_enum_to_bitmask(ctx
, buffers
[output
]);
722 assert(mask
[output
] != BAD_MASK
);
723 mask
[output
] &= supportedMask
;
729 * destMask[0] may have up to four bits set
730 * (ex: glDrawBuffer(GL_FRONT_AND_BACK)).
731 * Otherwise, destMask[x] can only have one bit set.
733 if (n
> 0 && util_bitcount(destMask
[0]) > 1) {
734 GLuint count
= 0, destMask0
= destMask
[0];
736 const gl_buffer_index bufIndex
= u_bit_scan(&destMask0
);
737 if (fb
->_ColorDrawBufferIndexes
[count
] != bufIndex
) {
738 updated_drawbuffers(ctx
, fb
);
739 fb
->_ColorDrawBufferIndexes
[count
] = bufIndex
;
743 fb
->ColorDrawBuffer
[0] = buffers
[0];
744 fb
->_NumColorDrawBuffers
= count
;
748 for (buf
= 0; buf
< n
; buf
++ ) {
750 gl_buffer_index bufIndex
= ffs(destMask
[buf
]) - 1;
751 /* only one bit should be set in the destMask[buf] field */
752 assert(util_bitcount(destMask
[buf
]) == 1);
753 if (fb
->_ColorDrawBufferIndexes
[buf
] != bufIndex
) {
754 updated_drawbuffers(ctx
, fb
);
755 fb
->_ColorDrawBufferIndexes
[buf
] = bufIndex
;
760 if (fb
->_ColorDrawBufferIndexes
[buf
] != BUFFER_NONE
) {
761 updated_drawbuffers(ctx
, fb
);
762 fb
->_ColorDrawBufferIndexes
[buf
] = BUFFER_NONE
;
765 fb
->ColorDrawBuffer
[buf
] = buffers
[buf
];
767 fb
->_NumColorDrawBuffers
= count
;
770 /* set remaining outputs to BUFFER_NONE */
771 for (buf
= fb
->_NumColorDrawBuffers
; buf
< ctx
->Const
.MaxDrawBuffers
; buf
++) {
772 if (fb
->_ColorDrawBufferIndexes
[buf
] != BUFFER_NONE
) {
773 updated_drawbuffers(ctx
, fb
);
774 fb
->_ColorDrawBufferIndexes
[buf
] = BUFFER_NONE
;
777 for (buf
= n
; buf
< ctx
->Const
.MaxDrawBuffers
; buf
++) {
778 fb
->ColorDrawBuffer
[buf
] = GL_NONE
;
781 if (_mesa_is_winsys_fbo(fb
)) {
782 /* also set context drawbuffer state */
783 for (buf
= 0; buf
< ctx
->Const
.MaxDrawBuffers
; buf
++) {
784 if (ctx
->Color
.DrawBuffer
[buf
] != fb
->ColorDrawBuffer
[buf
]) {
785 updated_drawbuffers(ctx
, fb
);
786 ctx
->Color
.DrawBuffer
[buf
] = fb
->ColorDrawBuffer
[buf
];
794 * Update the current drawbuffer's _ColorDrawBufferIndex[] list, etc.
795 * from the context's Color.DrawBuffer[] state.
796 * Use when changing contexts.
799 _mesa_update_draw_buffers(struct gl_context
*ctx
)
801 /* should be a window system FBO */
802 assert(_mesa_is_winsys_fbo(ctx
->DrawBuffer
));
804 _mesa_drawbuffers(ctx
, ctx
->DrawBuffer
, ctx
->Const
.MaxDrawBuffers
,
805 ctx
->Color
.DrawBuffer
, NULL
);
810 * Like \sa _mesa_drawbuffers(), this is a helper function for setting
811 * GL_READ_BUFFER state for the given context and FBO.
812 * Note that all error checking should have been done before calling
814 * \param ctx the rendering context
815 * \param fb the framebuffer object to update
816 * \param buffer GL_FRONT, GL_BACK, GL_COLOR_ATTACHMENT0, etc.
817 * \param bufferIndex the numerical index corresponding to 'buffer'
820 _mesa_readbuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
821 GLenum buffer
, gl_buffer_index bufferIndex
)
823 if ((fb
== ctx
->ReadBuffer
) && _mesa_is_winsys_fbo(fb
)) {
824 /* Only update the per-context READ_BUFFER state if we're bound to
825 * a window-system framebuffer.
827 ctx
->Pixel
.ReadBuffer
= buffer
;
830 fb
->ColorReadBuffer
= buffer
;
831 fb
->_ColorReadBufferIndex
= bufferIndex
;
833 ctx
->NewState
|= _NEW_BUFFERS
;
839 * Called by glReadBuffer and glNamedFramebufferReadBuffer to set the source
840 * renderbuffer for reading pixels.
841 * \param mode color buffer such as GL_FRONT, GL_BACK, etc.
843 static ALWAYS_INLINE
void
844 read_buffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
845 GLenum buffer
, const char *caller
, bool no_error
)
847 gl_buffer_index srcBuffer
;
849 FLUSH_VERTICES(ctx
, 0);
851 if (MESA_VERBOSE
& VERBOSE_API
)
852 _mesa_debug(ctx
, "%s %s\n", caller
, _mesa_enum_to_string(buffer
));
854 if (buffer
== GL_NONE
) {
855 /* This is legal--it means that no buffer should be bound for reading. */
856 srcBuffer
= BUFFER_NONE
;
859 /* general case / window-system framebuffer */
860 if (!no_error
&&_mesa_is_gles3(ctx
) &&
861 !is_legal_es3_readbuffer_enum(buffer
))
862 srcBuffer
= BUFFER_NONE
;
864 srcBuffer
= read_buffer_enum_to_index(ctx
, buffer
);
867 GLbitfield supportedMask
;
869 if (srcBuffer
== BUFFER_NONE
) {
870 _mesa_error(ctx
, GL_INVALID_ENUM
,
871 "%s(invalid buffer %s)", caller
,
872 _mesa_enum_to_string(buffer
));
876 supportedMask
= supported_buffer_bitmask(ctx
, fb
);
877 if (((1 << srcBuffer
) & supportedMask
) == 0) {
878 _mesa_error(ctx
, GL_INVALID_OPERATION
,
879 "%s(invalid buffer %s)", caller
,
880 _mesa_enum_to_string(buffer
));
886 /* OK, all error checking has been completed now */
888 _mesa_readbuffer(ctx
, fb
, buffer
, srcBuffer
);
890 /* Call the device driver function only if fb is the bound read buffer */
891 if (fb
== ctx
->ReadBuffer
) {
892 if (ctx
->Driver
.ReadBuffer
)
893 ctx
->Driver
.ReadBuffer(ctx
, buffer
);
899 read_buffer_err(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
900 GLenum buffer
, const char *caller
)
902 read_buffer(ctx
, fb
, buffer
, caller
, false);
907 read_buffer_no_error(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
908 GLenum buffer
, const char *caller
)
910 read_buffer(ctx
, fb
, buffer
, caller
, true);
915 _mesa_ReadBuffer_no_error(GLenum buffer
)
917 GET_CURRENT_CONTEXT(ctx
);
918 read_buffer_no_error(ctx
, ctx
->ReadBuffer
, buffer
, "glReadBuffer");
923 _mesa_ReadBuffer(GLenum buffer
)
925 GET_CURRENT_CONTEXT(ctx
);
926 read_buffer_err(ctx
, ctx
->ReadBuffer
, buffer
, "glReadBuffer");
931 _mesa_NamedFramebufferReadBuffer_no_error(GLuint framebuffer
, GLenum src
)
933 GET_CURRENT_CONTEXT(ctx
);
935 struct gl_framebuffer
*fb
;
938 fb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
940 fb
= ctx
->WinSysReadBuffer
;
943 read_buffer_no_error(ctx
, fb
, src
, "glNamedFramebufferReadBuffer");
948 _mesa_NamedFramebufferReadBuffer(GLuint framebuffer
, GLenum src
)
950 GET_CURRENT_CONTEXT(ctx
);
951 struct gl_framebuffer
*fb
;
954 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
955 "glNamedFramebufferReadBuffer");
960 fb
= ctx
->WinSysReadBuffer
;
962 read_buffer_err(ctx
, fb
, src
, "glNamedFramebufferReadBuffer");